Files
node/test/ffi/test-ffi-fast-integer-validation.js
T
Matteo Collina 9f04fcd70d ffi: enable module by default
Enable node:ffi by default in builds with FFI support. Keep
--experimental-ffi as a compatibility no-op and --no-experimental-ffi as
an opt-out. Loading the module still emits an experimental warning.

Signed-off-by: Matteo Collina <hello@matteocollina.com>
PR-URL: https://github.com/nodejs/node/pull/65475
Reviewed-By: Paolo Insogna <paolo@cowtech.it>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Filip Skokan <panva.ip@gmail.com>
Reviewed-By: Chemi Atlow <chemi@atlow.co.il>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Gürgün Dayıoğlu <hey@gurgun.day>
2026-08-28 00:23:11 +00:00

104 lines
3.1 KiB
JavaScript

// Flags: --allow-natives-syntax
'use strict';
const common = require('../common');
common.skipIfFFIMissing();
const assert = require('node:assert');
const { test } = require('node:test');
const ffi = require('node:ffi');
const { fixtureSymbols, libraryPath } = require('./ffi-test-common');
function optimize(fn, value) {
eval('%PrepareFunctionForOptimization(fn)');
fn(value);
fn(value);
eval('%OptimizeFunctionOnNextCall(fn)');
fn(value);
}
test('fast FFI validates integer argument ranges', () => {
const { lib, functions } = ffi.dlopen(libraryPath, fixtureSymbols);
try {
function callI8(value) { return functions.add_i8(value, 0); }
function callU8(value) { return functions.add_u8(value, 0); }
function callI16(value) { return functions.add_i16(value, 0); }
function callU16(value) { return functions.add_u16(value, 0); }
function callI32(value) { return functions.add_i32(value, 0); }
function callU32(value) { return functions.add_u32(value, 0); }
function callI64(value) { return functions.add_i64(value, 0n); }
function callU64(value) { return functions.add_u64(value, 0n); }
for (const [fn, value] of [
[callI8, 0],
[callU8, 0],
[callI16, 0],
[callU16, 0],
[callI32, 0],
[callU32, 0],
[callI64, 0n],
[callU64, 0n],
]) {
optimize(fn, value);
}
const expect = { code: 'ERR_INVALID_ARG_VALUE' };
assert.throws(() => callI8(128), expect);
assert.throws(() => callU8(256), expect);
assert.throws(() => callI16(32768), expect);
assert.throws(() => callU16(65536), expect);
assert.throws(() => callI32(2147483648), expect);
assert.throws(() => callI32(-2147483649), expect);
assert.throws(() => callI32(1.5), expect);
assert.throws(() => callI32('1'), expect);
assert.throws(() => callU32(4294967296), expect);
assert.throws(() => callU32(-1), expect);
assert.throws(() => callU32(1.5), expect);
assert.throws(() => callU32('1'), expect);
assert.throws(() => callI64(2n ** 63n), expect);
assert.throws(() => callU64(2n ** 64n), expect);
} finally {
eval('%WaitForBackgroundOptimization()');
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 {
eval('%WaitForBackgroundOptimization()');
lib.close();
}
});