ffi: validate fast pointer BigInt argument ranges

Optimized V8 fast API calls truncate out-of-range pointer BigInts.
Validate them against uintptrMax before invoking the raw function
so optimized calls match the generic and shared-buffer paths.

Signed-off-by: Kamat, Trivikram <16024985+trivikr@users.noreply.github.com>
Assisted-by: codex:gpt-5.6-sol
PR-URL: https://github.com/nodejs/node/pull/65032
Fixes: https://github.com/nodejs/node/issues/65031
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
This commit is contained in:
Trivikram Kamat authored and Antoine du Hamel committed 2026-08-26 00:55:58 +02:00
1 parent 3e50a2f1e9
commit 6f9e0846ea
3 files changed
+45 -3

No files matched your search

+11
View File
@@ -24,6 +24,7 @@ const {
getRawPointer,
kFastArguments,
kFastBufferInvoke,
uintptrMax,
} = internalBinding('ffi');
const {
@@ -110,6 +111,14 @@ function needsPointerConversion(type) {
needsNullPointerConversion(type) || needsStringPointerConversion(type);
}
function validateFastPointerArg(type, value, index) {
if (needsPointerConversion(type) && typeof value === 'bigint' &&
(value < 0n || value > uintptrMax)) {
throwFFIArgError(
`Argument ${index} must be a non-negative pointer bigint`);
}
}
function hasStringPointerArg(type, value) {
return typeof value === 'string' && needsStringPointerConversion(type);
}
@@ -159,6 +168,7 @@ function getStringConversionPointer(state, value, index) {
}
function convertPointerArg(type, value, stringState, index) {
validateFastPointerArg(type, value, index);
if (needsNullPointerConversion(type) &&
(value === null || value === undefined)) {
return 0n;
@@ -261,6 +271,7 @@ function wrapWithRawPointerConversions(rawFn, argumentTypes, owner) {
throwFFIArgCountError(1, arguments.length);
}
validateFastIntegerArg(t0, a0, 0);
validateFastPointerArg(t0, a0, 0);
let arg = a0;
if (needsNullPointerConversion(t0) &&
(arg === null || arg === undefined)) {
+3 -3
View File
@@ -1341,9 +1341,9 @@ static void Initialize(Local<Object> target,
Boolean::New(isolate, CHAR_MIN < 0))
.Check();
// The shared-buffer fast path uses `uintptrMax` to reject pointer BigInts
// that would otherwise be silently truncated by `ReadFFIArgFromBuffer`'s
// `memcpy(..., type->size, ...)` on 32-bit platforms. The slow path
// The JavaScript fast paths use `uintptrMax` to reject pointer BigInts that
// would otherwise be silently truncated by V8 or, on 32-bit platforms, by
// `ReadFFIArgFromBuffer`'s `memcpy(..., type->size, ...)`. The slow path
// rejects the same values through `ToFFIArgument`.
target
->Set(context,
@@ -68,3 +68,34 @@ test('fast FFI validates integer argument ranges', () => {
lib.close();
}
});
test('fast FFI validates pointer BigInt ranges', () => {
const lib = new ffi.DynamicLibrary(libraryPath);
try {
for (const type of ['pointer', 'ptr', 'string', 'str',
'buffer', 'arraybuffer']) {
const identityPointer = lib.getFunction('identity_pointer', {
arguments: [type],
return: 'pointer',
});
const sumBuffer = lib.getFunction('sum_buffer', {
arguments: [type, 'u64'],
return: 'u64',
});
function callSingle(value) { return identityPointer(value); }
function callMultiple(value) { return sumBuffer(value, 0n); }
optimize(callSingle, 0n);
optimize(callMultiple, 0n);
const expect = { code: 'ERR_INVALID_ARG_VALUE' };
for (const call of [callSingle, callMultiple]) {
assert.throws(() => call(-1n), expect);
assert.throws(() => call((2n ** 64n) + 5n), expect);
}
}
} finally {
lib.close();
}
});