Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion lib/ffi.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
'use strict';

const {
ArrayBufferPrototypeGetDetached,
DataViewPrototypeGetBuffer,
FunctionPrototypeCall,
ObjectDefineProperty,
ObjectFreeze,
ObjectGetOwnPropertyDescriptor,
ObjectKeys,
ObjectPrototypeToString,
SymbolDispose,
TypedArrayPrototypeGetBuffer,
} = primordials;
const { Buffer } = require('buffer');
const { emitExperimentalWarning } = require('internal/util');
const {
isDataView,
isArrayBufferView,
isSharedArrayBuffer,
} = require('internal/util/types');
const {
codes: {
Expand Down Expand Up @@ -284,7 +289,13 @@ function exportArrayBufferView(source, data, len) {

validateInteger(len, 'len', 0);

if (len < source.byteLength) {
// Reading byteLength throws for a detached DataView. Let the native binding
// reject detached views consistently with detached ArrayBuffers.
const buffer = isDataView(source) ?
DataViewPrototypeGetBuffer(source) : TypedArrayPrototypeGetBuffer(source);
if ((isSharedArrayBuffer(buffer) ||
!ArrayBufferPrototypeGetDetached(buffer)) &&
len < source.byteLength) {
throw new ERR_OUT_OF_RANGE('len', `>= ${source.byteLength}`, len);
}

Expand Down
1 change: 1 addition & 0 deletions src/util-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -573,6 +573,7 @@ ArrayBufferViewContents<T, S>::ArrayBufferViewContents(
template <typename T, size_t S>
void ArrayBufferViewContents<T, S>::Read(v8::Local<v8::ArrayBufferView> abv) {
static_assert(sizeof(T) == 1, "Only supports one-byte data at the moment");
was_detached_ = abv->Buffer()->WasDetached();
length_ = abv->ByteLength();
if (length_ > sizeof(stack_storage_) || abv->HasBuffer()) {
auto buf_data = abv->Buffer()->Data();
Expand Down
17 changes: 17 additions & 0 deletions test/ffi/test-ffi-memory.js
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,23 @@ test('ffi validates memory access arguments', () => {
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' });
ffi.exportArrayBufferView(new Uint8Array(new SharedArrayBuffer(1)), ptr, 1);

const detachedArrayBuffer = new ArrayBuffer(1);
detachedArrayBuffer.transfer();
assert.throws(() => ffi.exportArrayBuffer(detachedArrayBuffer, ptr, 1), {
code: 'ERR_INVALID_ARG_VALUE',
});

for (const View of [Uint8Array, DataView]) {
const arrayBuffer = new ArrayBuffer(1);
const view = new View(arrayBuffer);
arrayBuffer.transfer();
assert.throws(() => ffi.exportArrayBufferView(view, ptr, 1), {
code: 'ERR_INVALID_ARG_VALUE',
});
}

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' });
Expand Down