1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-29 16:30:56 -05:00

fix(ext/ffi): Empty buffers error with index out of bounds on FFI (#14997)

This commit is contained in:
Aapo Alasuutari 2022-06-29 18:00:29 +03:00 committed by David Sherret
parent e3b40ae0ea
commit 825477b3cd
3 changed files with 24 additions and 6 deletions

View file

@ -810,12 +810,16 @@ where
) )
})? })?
.get_backing_store(); .get_backing_store();
let pointer = &backing_store[byte_offset] as *const _ as *const u8; let pointer = if byte_offset > 0 {
&backing_store[byte_offset..] as *const _ as *const u8
} else {
&backing_store[..] as *const _ as *const u8
};
ffi_args.push(NativeValue { pointer }); ffi_args.push(NativeValue { pointer });
} else if let Ok(value) = v8::Local::<v8::ArrayBuffer>::try_from(value) } else if let Ok(value) = v8::Local::<v8::ArrayBuffer>::try_from(value)
{ {
let backing_store = value.get_backing_store(); let backing_store = value.get_backing_store();
let pointer = &backing_store as *const _ as *const u8; let pointer = &backing_store[..] as *const _ as *const u8;
ffi_args.push(NativeValue { pointer }); ffi_args.push(NativeValue { pointer });
} else { } else {
return Err(type_error("Invalid FFI pointer type, expected null, BigInt, ArrayBuffer, or ArrayBufferView")); return Err(type_error("Invalid FFI pointer type, expected null, BigInt, ArrayBuffer, or ArrayBufferView"));
@ -995,13 +999,17 @@ where
) )
})? })?
.get_backing_store(); .get_backing_store();
let pointer = &backing_store[byte_offset] as *const _ as *const u8; let pointer = if byte_offset > 0 {
&backing_store[byte_offset..] as *const _ as *const u8
} else {
&backing_store[..] as *const _ as *const u8
};
ffi_args.push(NativeValue { pointer }); ffi_args.push(NativeValue { pointer });
} else if let Ok(value) = v8::Local::<v8::ArrayBuffer>::try_from(value) } else if let Ok(value) = v8::Local::<v8::ArrayBuffer>::try_from(value)
{ {
let backing_store = value.get_backing_store(); let backing_store = value.get_backing_store();
let pointer = &backing_store as *const _ as *const u8; let pointer = &backing_store[..] as *const _ as *const u8;
ffi_args.push(NativeValue { pointer }); ffi_args.push(NativeValue { pointer });
} else { } else {
@ -1367,11 +1375,15 @@ unsafe fn do_ffi_callback(
.buffer(&mut scope) .buffer(&mut scope)
.expect("Unable to deserialize result parameter.") .expect("Unable to deserialize result parameter.")
.get_backing_store(); .get_backing_store();
let pointer = &backing_store[byte_offset] as *const _ as *const u8; let pointer = if byte_offset > 0 {
&backing_store[byte_offset..] as *const _ as *const u8
} else {
&backing_store[..] as *const _ as *const u8
};
*(result as *mut *const u8) = pointer; *(result as *mut *const u8) = pointer;
} else if let Ok(value) = v8::Local::<v8::ArrayBuffer>::try_from(value) { } else if let Ok(value) = v8::Local::<v8::ArrayBuffer>::try_from(value) {
let backing_store = value.get_backing_store(); let backing_store = value.get_backing_store();
let pointer = &backing_store as *const _ as *const u8; let pointer = &backing_store[..] as *const _ as *const u8;
*(result as *mut *const u8) = pointer; *(result as *mut *const u8) = pointer;
} else if let Ok(value) = v8::Local::<v8::BigInt>::try_from(value) { } else if let Ok(value) = v8::Local::<v8::BigInt>::try_from(value) {
*(result as *mut u64) = value.u64_value().0; *(result as *mut u64) = value.u64_value().0;

View file

@ -56,6 +56,8 @@ fn basic() {
false\n\ false\n\
true\n\ true\n\
false\n\ false\n\
false\n\
false\n\
579\n\ 579\n\
true\n\ true\n\
579\n\ 579\n\

View file

@ -214,6 +214,10 @@ console.log(stringPtrview.getCString(11));
console.log(Boolean(dylib.symbols.is_null_ptr(ptr0))); console.log(Boolean(dylib.symbols.is_null_ptr(ptr0)));
console.log(Boolean(dylib.symbols.is_null_ptr(null))); console.log(Boolean(dylib.symbols.is_null_ptr(null)));
console.log(Boolean(dylib.symbols.is_null_ptr(Deno.UnsafePointer.of(into)))); console.log(Boolean(dylib.symbols.is_null_ptr(Deno.UnsafePointer.of(into))));
const emptyBuffer = new BigUint64Array(0);
console.log(Boolean(dylib.symbols.is_null_ptr(emptyBuffer)));
const emptySlice = into.subarray(6);
console.log(Boolean(dylib.symbols.is_null_ptr(emptySlice)));
const addU32Ptr = dylib.symbols.get_add_u32_ptr(); const addU32Ptr = dylib.symbols.get_add_u32_ptr();
const addU32 = new Deno.UnsafeFnPointer(addU32Ptr, { const addU32 = new Deno.UnsafeFnPointer(addU32Ptr, {