Skip to content

Commit f3d4109

Browse files
committed
fixup! doc,test: mem protection must be observed in ffi
1 parent b448d7f commit f3d4109

File tree

4 files changed

+15
-8
lines changed

4 files changed

+15
-8
lines changed

doc/api/ffi.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -533,7 +533,8 @@ native memory directly. The caller must guarantee that:
533533
* `length` stays within the allocated native region.
534534
* no native code frees or repurposes that memory while JavaScript still uses
535535
the `Buffer`.
536-
* Memory protection is observed.
536+
* Memory protection is observed. For example, read-only memory pages must not
537+
be written to.
537538

538539
If these guarantees are not met, reading or writing the `Buffer` can corrupt
539540
memory or crash the process.

test/ffi/ffi-test-common.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,10 @@ const fixtureSymbols = {
8080
readonly_memory: { parameters: [], result: 'pointer' },
8181
};
8282

83+
if (!common.isWindows) {
84+
fixtureSymbols.readonly_memory = { parameters: [], result: 'pointer' };
85+
}
86+
8387
function cString(value) {
8488
return Buffer.from(`${value}\0`);
8589
}

test/ffi/fixture_library/ffi_test_library.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,10 @@
22
#include <stdint.h>
33
#include <stdlib.h>
44
#include <string.h>
5-
#include <sys/mman.h>
6-
75
#ifdef _WIN32
86
#define FFI_EXPORT __declspec(dllexport)
97
#else
8+
#include <sys/mman.h>
109
#define FFI_EXPORT
1110
#endif
1211

@@ -380,9 +379,11 @@ FFI_EXPORT void array_set_f64(double* arr, size_t index, double value) {
380379
arr[index] = value;
381380
}
382381

383-
FFI_EXPORT void * readonly_memory() {
382+
#ifndef _WIN32
383+
FFI_EXPORT void* readonly_memory() {
384384
// TODO(bengl) Add a Windows version of this.
385385

386-
void * p = mmap(0, 4096, PROT_READ, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
386+
void* p = mmap(0, 4096, PROT_READ, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
387387
return p;
388388
}
389+
#endif

test/ffi/test-ffi-readonly-write.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,15 @@ if (isWindows) {
1111
skip('This test currently relies on POSIX APIs');
1212
}
1313

14-
test('writing to readonly memory via buffer results in SIGBUS', () => {
14+
test('writing to readonly memory via buffer fails', () => {
1515
const symbols = JSON.stringify(fixtureSymbols);
16-
const { stdout, status, signal } = spawnSync(process.execPath, [
16+
const libPath = JSON.stringify(libraryPath);
17+
const { stdout, status } = spawnSync(process.execPath, [
1718
'--experimental-ffi',
1819
'-p',
1920
`
2021
const ffi = require('node:ffi');
21-
const { functions } = ffi.dlopen('${libraryPath}', ${symbols})
22+
const { functions } = ffi.dlopen(${libPath}, ${symbols})
2223
const p = functions.readonly_memory();
2324
const b = ffi.toBuffer(p, 4096, false);
2425
b[0] = 42;

0 commit comments

Comments
 (0)