GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — ffi: fix handle leak in fast buffer arguments · nodejs/node@1103d3f

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • src/ffi
    • fast.cc
  • test/ffi
    • test-ffi-fast-buffer.js
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎src/ffi/fast.cc‎

Copy file name to clipboardExpand all lines: src/ffi/fast.cc
+16-25Lines changed: 16 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,13 @@ extern "C" uintptr_t node_ffi_fast_buffer_data(v8::Local<v8::Value> value,
245245
// returns zero after throwing, preventing the native target from seeing an
246246
// invalid pointer value.
247247
constexpr uintptr_t kInvalidBuffer = std::numeric_limits<uintptr_t>::max();
248-
v8::Isolate* isolate = options != nullptr ? options->isolate : nullptr;
248+
// The trampoline always passes the FastApiCallbackOptions declared in the
249+
// CFunctionInfo. No HandleScope is active during a Fast API call, and both
250+
// ArrayBufferView::Buffer() and the error paths below create handles, so
251+
// open one for the whole helper. Otherwise every call leaks handles into
252+
// the caller's scope.
253+
v8::Isolate* isolate = options->isolate;
254+
v8::HandleScope scope(isolate);
249255

250256
// Accept only the memory-backed JS values supported by ToFFIArgument in the
251257
// native helper. Other pointer conversions, including strings and direct
@@ -254,41 +260,26 @@ extern "C" uintptr_t node_ffi_fast_buffer_data(v8::Local<v8::Value> value,
254260
if (value->IsArrayBufferView()) {
255261
v8::Local<v8::ArrayBufferView> view = value.As<v8::ArrayBufferView>();
256262
if (view->Buffer()->WasDetached()) {
257-
if (isolate != nullptr) {
258-
// No HandleScope is active during a Fast API call, so open one before
259-
// creating the error object.
260-
v8::HandleScope scope(isolate);
261-
THROW_ERR_INVALID_ARG_VALUE(
262-
isolate,
263-
"Argument %u is an ArrayBufferView backed by a detached "
264-
"ArrayBuffer",
265-
index);
266-
}
263+
THROW_ERR_INVALID_ARG_VALUE(
264+
isolate,
265+
"Argument %u is an ArrayBufferView backed by a detached "
266+
"ArrayBuffer",
267+
index);
267268
return kInvalidBuffer;
268269
}
269270
return PointerFromValue(value);
270271
}
271272
if (value->IsArrayBuffer()) {
272273
if (value.As<v8::ArrayBuffer>()->WasDetached()) {
273-
if (isolate != nullptr) {
274-
// No HandleScope is active during a Fast API call, so open one before
275-
// creating the error object.
276-
v8::HandleScope scope(isolate);
277-
THROW_ERR_INVALID_ARG_VALUE(
278-
isolate, "Argument %u is a detached ArrayBuffer", index);
279-
}
274+
THROW_ERR_INVALID_ARG_VALUE(
275+
isolate, "Argument %u is a detached ArrayBuffer", index);
280276
return kInvalidBuffer;
281277
}
282278
return PointerFromValue(value);
283279
}
284280

285-
if (isolate != nullptr) {
286-
// No HandleScope is active during a Fast API call, so open one before
287-
// creating the error object.
288-
v8::HandleScope scope(isolate);
289-
THROW_ERR_INVALID_ARG_VALUE(
290-
isolate, "Argument %u must be a buffer or an ArrayBuffer", index);
291-
}
281+
THROW_ERR_INVALID_ARG_VALUE(
282+
isolate, "Argument %u must be a buffer or an ArrayBuffer", index);
292283
return kInvalidBuffer;
293284
}
294285

Collapse file

‎test/ffi/test-ffi-fast-buffer.js‎

Copy file name to clipboardExpand all lines: test/ffi/test-ffi-fast-buffer.js
+31Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ common.skipIfFFIMissing();
66

77
const assert = require('node:assert');
88
const { test } = require('node:test');
9+
const { queryObjects } = require('node:v8');
910
const { internalBinding } = require('internal/test/binding');
1011
const {
1112
kSbSharedBuffer,
@@ -100,6 +101,36 @@ test('optimized pointer arguments reject direct SharedArrayBuffers', () => {
100101
}
101102
});
102103

104+
test('optimized buffer calls do not retain handles', () => {
105+
const { lib, functions } = ffi.dlopen(libraryPath, {
106+
first_byte: { arguments: ['buffer'], return: 'u8' },
107+
});
108+
// A leaked handle to the backing store keeps it alive until the current
109+
// synchronous run ends, so count live backing stores after the calls.
110+
class TrackedArrayBuffer extends ArrayBuffer {}
111+
112+
function callFirstByte(view) {
113+
return functions.first_byte(view);
114+
}
115+
116+
try {
117+
const warm = new Uint8Array(1);
118+
eval('%PrepareFunctionForOptimization(callFirstByte)');
119+
for (let i = 0; i < 1000; i++) callFirstByte(warm);
120+
eval('%OptimizeFunctionOnNextCall(callFirstByte)');
121+
callFirstByte(warm);
122+
123+
for (let i = 0; i < 100; i++) {
124+
callFirstByte(new Uint8Array(new TrackedArrayBuffer(1)));
125+
}
126+
assert.strictEqual(
127+
queryObjects(TrackedArrayBuffer, { format: 'count' }), 0);
128+
} finally {
129+
eval('%WaitForBackgroundOptimization()');
130+
lib.close();
131+
}
132+
});
133+
103134
test('fast FFI string buffers survive reentrant callbacks', {
104135
// Bundled libffi callbacks crash on SmartOS.
105136
skip: common.isSunOS,

0 commit comments

Comments
0 (0)
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.