Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fix: Various improvements.
Signed-off-by: Paolo Insogna <paolo@cowtech.it>
  • Loading branch information
ShogunPanda committed Apr 11, 2026
commit 55a94dfcf80c72f9cbeb0cabd40484c89c162a9c
9 changes: 9 additions & 0 deletions .github/workflows/tools.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ on:
- histogram
- icu
- inspector_protocol
- libffi
- libuv
- llhttp
- minimatch
Expand Down Expand Up @@ -161,6 +162,14 @@ jobs:
cat temp-output
tail -n1 temp-output | grep "NEW_VERSION=" >> "$GITHUB_ENV" || true
rm temp-output
- id: libffi
subsystem: deps
label: dependencies, ffi
run: |
./tools/dep_updaters/update-libffi.sh > temp-output
cat temp-output
tail -n1 temp-output | grep "NEW_VERSION=" >> "$GITHUB_ENV" || true
rm temp-output
- id: libuv
subsystem: deps
label: dependencies
Expand Down
4 changes: 2 additions & 2 deletions configure.py
Original file line number Diff line number Diff line change
Expand Up @@ -2214,8 +2214,8 @@ def without_sqlite_error(option):

def bundled_ffi_supported(os_name, target_arch):
supported = {
'freebsd': {'arm', 'arm64', 'ia32', 'x64'},
'linux': {'arm', 'arm64', 'ia32', 'x64'},
'freebsd': {'arm', 'arm64', 'x64'},
'linux': {'arm', 'arm64', 'x64'},
'mac': {'arm64', 'x64'},
'win': {'arm', 'arm64', 'x64'},
}
Expand Down
5 changes: 0 additions & 5 deletions deps/libffi/generate-headers.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,14 @@
TARGETS = {
('freebsd', 'arm'): ('ARM', 'arm'),
('freebsd', 'arm64'): ('AARCH64', 'aarch64'),
('freebsd', 'ia32'): ('X86_FREEBSD', 'x86'),
('freebsd', 'x64'): ('X86_64', 'x86'),
('freebsd', 'x86'): ('X86_FREEBSD', 'x86'),
('linux', 'arm'): ('ARM', 'arm'),
('linux', 'arm64'): ('AARCH64', 'aarch64'),
('linux', 'ia32'): ('X86', 'x86'),
('linux', 'x64'): ('X86_64', 'x86'),
('linux', 'x86'): ('X86', 'x86'),
('mac', 'arm64'): ('AARCH64', 'aarch64'),
('mac', 'x64'): ('X86_64', 'x86'),
('win', 'arm'): ('ARM_WIN32', 'arm'),
('win', 'arm64'): ('ARM_WIN64', 'aarch64'),
('win', 'ia32'): ('X86', 'x86'),
('win', 'x64'): ('X86_WIN64', 'x86'),
}

Expand Down
14 changes: 0 additions & 14 deletions deps/libffi/libffi.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,6 @@
],
},
}],
['target_arch == "ia32" or target_arch == "x86"', {
# Windows x86 is not supported by libffi
'variables': {
'libffi_arch_sources': [],
},
}],
],
}],
['OS == "linux" or OS == "freebsd"', {
Expand All @@ -65,14 +59,6 @@
],
},
}],
['target_arch == "ia32" or target_arch == "x86"', {
'variables': {
'libffi_arch_sources': [
'src/x86/ffi.c',
'src/x86/sysv.S',
],
},
}],
['target_arch == "arm"', {
'variables': {
'libffi_arch_sources': [
Expand Down
7 changes: 6 additions & 1 deletion doc/api/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,8 @@ added: REPLACEME
When using the [Permission Model][], the process will not be able to use FFI
APIs by default. Attempts to use FFI APIs will throw an `ERR_ACCESS_DENIED`
exception unless the user explicitly passes the `--allow-ffi` flag when
starting Node.js.
starting Node.js. The [`node:ffi`][] module also requires the
`--experimental-ffi` flag and is only available in builds with FFI support.

Example:

Expand Down Expand Up @@ -1185,6 +1186,8 @@ added: REPLACEME

Enable the experimental [`node:ffi`][] module.

This flag is only available in builds with FFI support.

### `--experimental-import-meta-resolve`

<!-- YAML
Expand Down Expand Up @@ -1996,6 +1999,8 @@ added: REPLACEME

Disable the experimental [`node:ffi`][] module.

This flag is only available in builds with FFI support.

### `--no-experimental-sqlite`

<!-- YAML
Expand Down
10 changes: 8 additions & 2 deletions doc/api/ffi.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ import ffi from 'node:ffi';
const ffi = require('node:ffi');
```

This module is only available under the `node:` scheme and is gated by the
`--experimental-ffi` flag.
This module is only available under the `node:` scheme in builds with FFI
support and is gated by the `--experimental-ffi` flag.

When using the [Permission Model][], FFI APIs are restricted unless the
[`--allow-ffi`][] flag is provided.
Expand Down Expand Up @@ -69,6 +69,12 @@ Supported type names:
Pointer-like types (`pointer`, `string`, `buffer`, `arraybuffer`, and
`function`) are all passed through the native layer as pointers.

The `char` type follows the platform C ABI. On platforms where plain C `char`
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Include an example (or function) to check the platform c abi?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is unnecessary. First of all, this is transparent to node:ffi so users won't really care about this. The few that might care probably know which platforms they're developing on.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This and the request for a suffix helper are intended for code that doesn't know what platforms it will run on - I think developers would write similar code to map platform to C ABI or dll suffix which seems like a good opportunity for a built in helper.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But how would they see this on JS side?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe I don't understand the question, but:

Bun exports suffix so libraries can write dlopen('common_file_name.' + suffix).
The implementation is a switch over process.platform, just like what you used in tests.

The char stuff can probably be ignored

is signed it behaves like `i8`; otherwise it behaves like `u8`.

The `bool` type is marshaled as an 8-bit unsigned integer. Pass numeric values
such as `0` and `1`; JavaScript `true` and `false` are not accepted.

## Signature objects

Functions and callbacks are described with signature objects.
Expand Down
3 changes: 2 additions & 1 deletion doc/api/permissions.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ using the [`--allow-child-process`][] and [`--allow-worker`][] respectively.
To allow network access, use [`--allow-net`][] and for allowing native addons
when using permission model, use the [`--allow-addons`][]
flag. For WASI, use the [`--allow-wasi`][] flag. For FFI, use the
[`--allow-ffi`][] flag.
[`--allow-ffi`][] flag. The [`node:ffi`][] module also requires the
`--experimental-ffi` flag and is only available in builds with FFI support.

#### Runtime API

Expand Down
7 changes: 7 additions & 0 deletions doc/contributing/maintaining/maintaining-dependencies.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ This a list of all the dependencies:
* [histogram][]
* [icu-small][]
* [inspector\_protocol][inspector_protocol]
* [libffi][]
* [libuv][]
* [llhttp][]
* [minimatch][]
Expand Down Expand Up @@ -279,6 +280,11 @@ The [inspector\_protocol](https://chromium.googlesource.com/deps/inspector_proto
is Chromium's of code generators and templates for the inspector protocol.
See [this doc](../../../tools/inspector_protocol/README.md) for more information.

### libffi

The [libffi](https://github.com/libffi/libffi) dependency is a portable foreign
function interface library used by `node:ffi`.

### libuv

The [libuv](https://github.com/libuv/libuv) dependency is a
Expand Down Expand Up @@ -418,6 +424,7 @@ according to [RFC 8878](https://datatracker.ietf.org/doc/html/rfc8878).
[histogram]: #histogram
[icu-small]: #icu-small
[inspector_protocol]: #inspector_protocol
[libffi]: #libffi
[libuv]: #libuv
[llhttp]: #llhttp
[maintaining-V8]: ./maintaining-V8.md
Expand Down
6 changes: 3 additions & 3 deletions doc/node-config-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
},
"allow-ffi": {
"type": "boolean",
"description": "allow use of FFI when any permissions are set"
"description": "allow use of FFI when any permissions are set (only in builds with FFI support)"
},
"allow-fs-read": {
"oneOf": [
Expand Down Expand Up @@ -176,7 +176,7 @@
},
"experimental-ffi": {
"type": "boolean",
"description": "experimental node:ffi module"
"description": "experimental node:ffi module (only in builds with FFI support)"
},
"experimental-global-navigator": {
"type": "boolean",
Expand Down Expand Up @@ -774,7 +774,7 @@
},
"allow-ffi": {
"type": "boolean",
"description": "allow use of FFI when any permissions are set"
"description": "allow use of FFI when any permissions are set (only in builds with FFI support)"
},
"allow-child-process": {
"type": "boolean",
Expand Down
13 changes: 10 additions & 3 deletions lib/ffi.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const {
} = require('internal/errors');
const permission = require('internal/process/permission');
const {
validateInteger,
validateString,
} = require('internal/validators');

Expand Down Expand Up @@ -45,7 +46,9 @@ function checkFFIPermission() {
return;
}

throw new ERR_ACCESS_DENIED('Access to this API has been restricted', 'FFI');
throw new ERR_ACCESS_DENIED(
'Access to this API has been restricted. Use --allow-ffi to manage permissions.',
'FFI');
}

function dlopen(path, definitions) {
Expand All @@ -58,6 +61,7 @@ function dlopen(path, definitions) {
}

function dlclose(handle) {
checkFFIPermission();
handle.close();
}

Expand All @@ -70,8 +74,9 @@ function exportString(str, data, len, encoding = 'utf8') {
checkFFIPermission();
validateString(str, 'string');
validateString(encoding, 'encoding');
validateInteger(len, 'len', 0);

if (len <= 0) {
if (len === 0) {
return;
}

Expand All @@ -90,7 +95,9 @@ function exportBuffer(buffer, data, len) {
throw new ERR_INVALID_ARG_TYPE('buffer', 'Buffer', buffer);
}

if (len <= 0) {
validateInteger(len, 'len', 0);

if (len === 0) {
return;
}

Expand Down
10 changes: 8 additions & 2 deletions lib/internal/process/permission.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const { Buffer } = require('buffer');
const { isBuffer } = Buffer;

let _permission;
const hasFFI = Boolean(process.config.variables.node_use_ffi);

module.exports = ObjectFreeze({
__proto__: null,
Expand All @@ -34,16 +35,21 @@ module.exports = ObjectFreeze({
return permission.has(scope, reference);
},
availableFlags() {
return [
const flags = [
'--allow-fs-read',
'--allow-fs-write',
'--allow-addons',
'--allow-child-process',
'--allow-ffi',
'--allow-net',
'--allow-inspector',
'--allow-wasi',
'--allow-worker',
];

if (hasFFI) {
flags.splice(4, 0, '--allow-ffi');
}

return flags;
},
});
4 changes: 3 additions & 1 deletion lib/internal/process/pre_execution.js
Original file line number Diff line number Diff line change
Expand Up @@ -665,11 +665,13 @@ function initializePermission() {
const warnFlags = [
'--allow-addons',
'--allow-child-process',
'--allow-ffi',
'--allow-inspector',
'--allow-wasi',
'--allow-worker',
];
if (process.config.variables.node_use_ffi) {
warnFlags.splice(2, 0, '--allow-ffi');
}
for (const flag of warnFlags) {
if (getOptionValue(flag)) {
process.emitWarning(
Expand Down
3 changes: 2 additions & 1 deletion node.gni
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,11 @@ declare_args() {
node_use_sqlite = true

# Build node with FFI support.
# Disabled in GN builds until libffi is wired up there.
# Disabled in GN builds. The unofficial GN build does not support node:ffi.
node_use_ffi = false

# Link against a shared libffi when FFI support is enabled.
# Unsupported in GN builds.
node_shared_ffi = false

# Use the specified path to system CA (PEM format) in addition to
Expand Down
Loading