forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-ffi-memory.js
More file actions
256 lines (217 loc) · 11.9 KB
/
Copy pathtest-ffi-memory.js
File metadata and controls
256 lines (217 loc) · 11.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
// Flags: --experimental-ffi
'use strict';
const common = require('../common');
common.skipIfFFIMissing();
const { constants: bufferConstants } = require('node:buffer');
const assert = require('node:assert');
const { after, test } = require('node:test');
const ffi = require('node:ffi');
const { fixtureSymbols, libraryPath } = require('./ffi-test-common');
const { lib, functions: symbols } = ffi.dlopen(libraryPath, {
allocate_memory: fixtureSymbols.allocate_memory,
deallocate_memory: fixtureSymbols.deallocate_memory,
pointer_to_usize: fixtureSymbols.pointer_to_usize,
});
after(() => lib.close());
function withAllocations(fn) {
const allocations = new Set();
function alloc(size) {
const ptr = symbols.allocate_memory(BigInt(size));
allocations.add(ptr);
return ptr;
}
try {
fn(alloc);
} finally {
for (const ptr of allocations) {
symbols.deallocate_memory(ptr);
}
}
}
test('ffi reads and writes integer and float values', () => {
withAllocations(common.mustCall((alloc) => {
const ptr = alloc(64);
ffi.setInt8(ptr, 0, -12);
ffi.setUint8(ptr, 1, 250);
ffi.setInt16(ptr, 2, -1234);
ffi.setUint16(ptr, 4, 65000);
ffi.setInt32(ptr, 8, -123456);
ffi.setUint32(ptr, 12, 3000000000);
ffi.setInt64(ptr, 16, -1234567890123n);
ffi.setUint64(ptr, 24, 1234567890123n);
ffi.setFloat32(ptr, 32, 3.5);
ffi.setFloat64(ptr, 40, 7.25);
assert.strictEqual(ffi.getInt8(ptr, 0), -12);
assert.strictEqual(ffi.getUint8(ptr, 1), 250);
assert.strictEqual(ffi.getInt16(ptr, 2), -1234);
assert.strictEqual(ffi.getUint16(ptr, 4), 65000);
assert.strictEqual(ffi.getInt32(ptr, 8), -123456);
assert.strictEqual(ffi.getUint32(ptr, 12), 3000000000);
assert.strictEqual(ffi.getInt64(ptr, 16), -1234567890123n);
assert.strictEqual(ffi.getUint64(ptr, 24), 1234567890123n);
assert.strictEqual(ffi.getFloat32(ptr, 32), 3.5);
assert.strictEqual(ffi.getFloat64(ptr, 40), 7.25);
}));
});
test('ffi supports unaligned memory access', () => {
withAllocations(common.mustCall((alloc) => {
const ptr = alloc(24);
ffi.setInt32(ptr, 1, 0x12345678);
ffi.setUint16(ptr, 7, 0xBEEF);
ffi.setFloat64(ptr, 9, 6.5);
assert.strictEqual(ffi.getInt32(ptr, 1), 0x12345678);
assert.strictEqual(ffi.getUint16(ptr, 7), 0xBEEF);
assert.strictEqual(ffi.getFloat64(ptr, 9), 6.5);
}));
});
test('ffi toBuffer supports copy and zero-copy views', () => {
withAllocations(common.mustCall((alloc) => {
const ptr = alloc(8);
ffi.exportBuffer(Buffer.from([1, 2, 3, 4]), ptr, 4);
const copy = ffi.toBuffer(ptr, 4);
const view = ffi.toBuffer(ptr, 4, false);
assert.deepStrictEqual([...copy], [1, 2, 3, 4]);
assert.deepStrictEqual([...view], [1, 2, 3, 4]);
view[1] = 9;
assert.deepStrictEqual([...ffi.toBuffer(ptr, 4)], [1, 9, 3, 4]);
assert.deepStrictEqual([...copy], [1, 2, 3, 4]);
const copyFromUndefined = ffi.toBuffer(ptr, 4, undefined);
copyFromUndefined[0] = 77;
assert.deepStrictEqual([...copyFromUndefined], [77, 9, 3, 4]);
assert.deepStrictEqual([...ffi.toBuffer(ptr, 4)], [1, 9, 3, 4]);
}));
});
test('ffi toArrayBuffer supports copy and zero-copy views', () => {
withAllocations(common.mustCall((alloc) => {
const ptr = alloc(4);
ffi.exportBuffer(Buffer.from([10, 20, 30, 40]), ptr, 4);
const copied = new Uint8Array(ffi.toArrayBuffer(ptr, 4));
const shared = new Uint8Array(ffi.toArrayBuffer(ptr, 4, false));
assert.deepStrictEqual([...copied], [10, 20, 30, 40]);
assert.deepStrictEqual([...shared], [10, 20, 30, 40]);
shared[2] = 99;
assert.deepStrictEqual([...ffi.toBuffer(ptr, 4)], [10, 20, 99, 40]);
assert.deepStrictEqual([...copied], [10, 20, 30, 40]);
const copiedFromUndefined = new Uint8Array(ffi.toArrayBuffer(ptr, 4, undefined));
copiedFromUndefined[1] = 55;
assert.deepStrictEqual([...copiedFromUndefined], [10, 55, 99, 40]);
assert.deepStrictEqual([...ffi.toBuffer(ptr, 4)], [10, 20, 99, 40]);
}));
});
test('ffi getRawPointer returns raw addresses for byte sources', () => {
const buffer = Buffer.from([1, 2, 3]);
const arrayBuffer = new Uint8Array([4, 5, 6, 7]).buffer;
const view = new Uint8Array(arrayBuffer, 2);
const bufferPointer = ffi.getRawPointer(buffer);
const arrayBufferPointer = ffi.getRawPointer(arrayBuffer);
const viewPointer = ffi.getRawPointer(view);
assert.strictEqual(typeof bufferPointer, 'bigint');
assert.strictEqual(typeof arrayBufferPointer, 'bigint');
assert.strictEqual(typeof viewPointer, 'bigint');
assert.strictEqual(bufferPointer, symbols.pointer_to_usize(buffer));
assert.strictEqual(arrayBufferPointer, symbols.pointer_to_usize(arrayBuffer));
assert.strictEqual(viewPointer, arrayBufferPointer + 2n);
});
test('ffi exportString and exportBuffer copy data into native memory', () => {
withAllocations(common.mustCall((alloc) => {
const stringPtr = alloc(16);
ffi.exportString('hello ffi', stringPtr, 16);
assert.strictEqual(ffi.toString(stringPtr), 'hello ffi');
assert.throws(() => ffi.exportString('truncated value', stringPtr, 5), {
code: 'ERR_OUT_OF_RANGE',
});
ffi.exportBuffer(Buffer.from([0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f]), stringPtr, 6);
ffi.exportString('A', stringPtr, 6, 'utf16le');
assert.deepStrictEqual([...ffi.toBuffer(stringPtr, 6)], [0x41, 0x00, 0x00, 0x00, 0x7f, 0x7f]);
ffi.exportBuffer(Buffer.from([0x7f, 0x7f, 0x7f, 0x7f, 0x7f]), stringPtr, 5);
assert.throws(() => ffi.exportString('AB', stringPtr, 5, 'utf16le'), {
code: 'ERR_OUT_OF_RANGE',
});
ffi.exportString('AB', stringPtr, 6, 'utf16le');
assert.deepStrictEqual([...ffi.toBuffer(stringPtr, 6)], [0x41, 0x00, 0x42, 0x00, 0x00, 0x00]);
const bufferPtr = alloc(7);
ffi.exportBuffer(Buffer.from([1, 2, 3, 4, 5, 6, 7]), bufferPtr, 7);
assert.deepStrictEqual([...ffi.toBuffer(bufferPtr, 7)], [1, 2, 3, 4, 5, 6, 7]);
assert.throws(() => ffi.exportBuffer(Buffer.from([1, 2, 3, 4, 5, 6, 7]), bufferPtr, 6), {
code: 'ERR_OUT_OF_RANGE',
});
const arrayBufferPtr = alloc(8);
const arrayBuffer = new Uint8Array([8, 9, 10, 11]).buffer;
ffi.exportArrayBuffer(arrayBuffer, arrayBufferPtr, 4);
assert.deepStrictEqual([...ffi.toBuffer(arrayBufferPtr, 4)], [8, 9, 10, 11]);
const viewPtr = alloc(8);
const viewSource = new Uint16Array([0x0102, 0x0304, 0x0506]);
const middleBytes = new Uint8Array(viewSource.buffer, 2, 2);
ffi.exportArrayBufferView(middleBytes, viewPtr, 2);
assert.deepStrictEqual([...ffi.toBuffer(viewPtr, 2)], [...middleBytes]);
const bufferViewPtr = alloc(8);
const bufferView = Buffer.from([1, 7, 2, 8, 3]);
ffi.exportArrayBufferView(bufferView.subarray(1, 4), bufferViewPtr, 3);
assert.deepStrictEqual([...ffi.toBuffer(bufferViewPtr, 3)], [7, 2, 8]);
}));
});
test('ffi toString returns null for a null pointer', () => {
assert.strictEqual(ffi.toString(0n), null);
});
test('ffi validates memory access arguments', () => {
withAllocations(common.mustCall((alloc) => {
const ptr = alloc(8);
const maxPointer = process.arch === 'ia32' || process.arch === 'arm' ?
0xfffffffcn :
0xfffffffffffffffcn;
assert.throws(() => ffi.toBuffer('nope', 4), /The first argument must be a bigint/);
assert.throws(() => ffi.toBuffer(-1n, 4), /The first argument must be a non-negative bigint/);
assert.throws(() => ffi.toBuffer(ptr, -1), /The length must be a non-negative integer/);
assert.throws(() => ffi.toBuffer(0n, 1), /Cannot create a buffer from a null pointer/);
assert.throws(() => ffi.toArrayBuffer(ptr, 'bad'), /The length must be a number/);
assert.throws(() => ffi.toArrayBuffer(-1n, 4), /The first argument must be a non-negative bigint/);
assert.throws(() => ffi.toArrayBuffer(0n, 1), /Cannot create an ArrayBuffer from a null pointer/);
assert.throws(() => ffi.getRawPointer('bad'), { code: 'ERR_INVALID_ARG_TYPE' });
assert.throws(() => ffi.getRawPointer(1), { code: 'ERR_INVALID_ARG_TYPE' });
assert.throws(() => ffi.getInt32(0n), /Cannot dereference a null pointer/);
assert.throws(() => ffi.getInt32(-1n), /The pointer must be a non-negative bigint/);
assert.throws(() => ffi.getInt8(maxPointer, 8), /pointer and offset exceed the platform address range/);
assert.throws(() => ffi.getInt32(maxPointer, 2), /accessed range exceeds the platform address range/);
assert.throws(() => ffi.setUint8(ptr), /Expected an offset argument/);
assert.throws(() => ffi.setUint8(-1n, 0, 1), /The pointer must be a non-negative bigint/);
assert.throws(() => ffi.setUint8(maxPointer, 8, 1), /pointer and offset exceed the platform address range/);
assert.throws(() => ffi.setUint32(maxPointer, 2, 1), /accessed range exceeds the platform address range/);
assert.throws(() => ffi.setUint8(ptr, 0), /Expected a value argument/);
assert.throws(() => ffi.setInt8(ptr, 0, 1.5), /Value must be an int8/);
assert.throws(() => ffi.setInt8(ptr, 0, Number.NaN), /Value must be an int8/);
assert.throws(() => ffi.setUint8(ptr, 0, 300), /Value must be a uint8/);
assert.throws(() => ffi.setUint8(ptr, 0, Number.NaN), /Value must be a uint8/);
assert.throws(() => ffi.setInt16(ptr, 'bad', 1), /The offset must be a number/);
assert.throws(() => ffi.setInt16(ptr, 0, 1.5), /Value must be an int16/);
assert.throws(() => ffi.setUint16(ptr, 0, Number.NaN), /Value must be a uint16/);
assert.throws(() => ffi.setInt64(ptr, 0, 1.5), /Value must be an int64/);
assert.throws(() => ffi.setInt64(ptr, 0, Number.NaN), /Value must be an int64/);
assert.throws(() => ffi.setInt64(ptr, 0, 2n ** 63n), /Value must be an int64/);
assert.throws(() => ffi.setInt64(ptr, 0, Number.MAX_SAFE_INTEGER + 1), /Value must be an int64/);
assert.throws(() => ffi.setUint64(ptr, 0, -1), /Value must be a uint64/);
assert.throws(() => ffi.setUint64(ptr, 0, 1.5), /Value must be a uint64/);
assert.throws(() => ffi.setUint64(ptr, 0, -1n), /Value must be a uint64/);
assert.throws(() => ffi.setUint64(ptr, 0, 2n ** 64n), /Value must be a uint64/);
assert.throws(() => ffi.setUint64(ptr, 0, Number.MAX_SAFE_INTEGER + 1), /Value must be a uint64/);
assert.throws(() => ffi.exportString(1, ptr, 4), { code: 'ERR_INVALID_ARG_TYPE' });
assert.throws(() => ffi.exportString('ok', ptr, -1), { code: 'ERR_OUT_OF_RANGE' });
assert.throws(() => ffi.exportString('ok', ptr, 4, 1), { code: 'ERR_INVALID_ARG_TYPE' });
assert.throws(() => ffi.exportString('ok', ptr, 2), { code: 'ERR_OUT_OF_RANGE' });
assert.throws(() => ffi.exportBuffer('bad', ptr, 4), { code: 'ERR_INVALID_ARG_TYPE' });
assert.throws(() => ffi.exportBuffer(Buffer.from([1]), ptr, -1), { code: 'ERR_OUT_OF_RANGE' });
assert.throws(() => ffi.exportBuffer(Buffer.from([1, 2]), ptr, 1), { code: 'ERR_OUT_OF_RANGE' });
assert.throws(() => ffi.exportArrayBuffer('bad', ptr, 4), { code: 'ERR_INVALID_ARG_TYPE' });
assert.throws(() => ffi.exportArrayBuffer(new ArrayBuffer(1), ptr, -1), { code: 'ERR_OUT_OF_RANGE' });
assert.throws(() => ffi.exportArrayBuffer(new ArrayBuffer(2), ptr, 1), { code: 'ERR_OUT_OF_RANGE' });
assert.throws(() => ffi.exportArrayBufferView('bad', ptr, 4), { code: 'ERR_INVALID_ARG_TYPE' });
assert.throws(() => ffi.exportArrayBufferView(new Uint8Array([1]), ptr, -1), { code: 'ERR_OUT_OF_RANGE' });
assert.throws(() => ffi.exportArrayBufferView(new Uint8Array([1, 2]), ptr, 1), { code: 'ERR_OUT_OF_RANGE' });
assert.throws(() => ffi.toBuffer(maxPointer, 8), /pointer and length exceed the platform address range/);
assert.throws(() => ffi.toArrayBuffer(maxPointer, 8), /pointer and length exceed the platform address range/);
assert.throws(() => ffi.toBuffer(1n, bufferConstants.MAX_LENGTH + 1), { code: 'ERR_BUFFER_TOO_LARGE' });
assert.throws(() => ffi.toArrayBuffer(1n, bufferConstants.MAX_LENGTH + 1), { code: 'ERR_BUFFER_TOO_LARGE' });
if (process.arch === 'ia32' || process.arch === 'arm') {
assert.throws(() => ffi.toBuffer(2n ** 32n, 0), /platform pointer range/);
}
}));
});