mirror of
https://github.com/denoland/deno.git
synced 2024-11-21 15:04:11 -05:00
fix(serde_v8): update bytes::Bytes layout assumptions (#15718)
This commit is contained in:
parent
e267ec6ed5
commit
73b4597dec
4 changed files with 40 additions and 3 deletions
4
Cargo.lock
generated
4
Cargo.lock
generated
|
@ -398,9 +398,9 @@ checksum = "14c189c53d098945499cdfa7ecc63567cf3886b3332b312a5b4585d8d3a6a610"
|
|||
|
||||
[[package]]
|
||||
name = "bytes"
|
||||
version = "1.1.0"
|
||||
version = "1.2.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "c4872d67bab6358e59559027aa3b9157c53d9358c51423c17554809a8858e0f8"
|
||||
checksum = "ec8a7b6a70fde80372154c65702f00a0f56f3e1c36abbc6c440484be248856db"
|
||||
|
||||
[[package]]
|
||||
name = "cache_control"
|
||||
|
|
|
@ -13,7 +13,7 @@ description = "Rust to V8 serialization and deserialization"
|
|||
path = "lib.rs"
|
||||
|
||||
[dependencies]
|
||||
bytes = "1"
|
||||
bytes = "=1.2.1"
|
||||
derive_more = "0.99.17"
|
||||
serde = { version = "1.0.136", features = ["derive"] }
|
||||
serde_bytes = "0.11"
|
||||
|
|
|
@ -26,11 +26,25 @@ impl RawBytes {
|
|||
}
|
||||
}
|
||||
|
||||
// Validate some bytes::Bytes layout assumptions at compile time.
|
||||
const _: () = {
|
||||
assert!(
|
||||
core::mem::size_of::<RawBytes>() == core::mem::size_of::<bytes::Bytes>(),
|
||||
);
|
||||
assert!(
|
||||
core::mem::align_of::<RawBytes>() == core::mem::align_of::<bytes::Bytes>(),
|
||||
);
|
||||
};
|
||||
|
||||
#[allow(unused)]
|
||||
pub(crate) struct Vtable {
|
||||
/// fn(data, ptr, len)
|
||||
pub clone: unsafe fn(&AtomicPtr<()>, *const u8, usize) -> bytes::Bytes,
|
||||
/// fn(data, ptr, len)
|
||||
///
|
||||
/// takes `Bytes` to value
|
||||
pub to_vec: unsafe fn(&AtomicPtr<()>, *const u8, usize) -> Vec<u8>,
|
||||
/// fn(data, ptr, len)
|
||||
pub drop: unsafe fn(&mut AtomicPtr<()>, *const u8, usize),
|
||||
}
|
||||
|
||||
|
@ -55,6 +69,7 @@ mod tests {
|
|||
const STATIC_VTABLE: Vtable = Vtable {
|
||||
clone: static_clone,
|
||||
drop: static_drop,
|
||||
to_vec: static_to_vec,
|
||||
};
|
||||
|
||||
unsafe fn static_clone(
|
||||
|
@ -65,6 +80,15 @@ mod tests {
|
|||
from_static(std::slice::from_raw_parts(ptr, len)).into()
|
||||
}
|
||||
|
||||
unsafe fn static_to_vec(
|
||||
_: &AtomicPtr<()>,
|
||||
ptr: *const u8,
|
||||
len: usize,
|
||||
) -> Vec<u8> {
|
||||
let slice = std::slice::from_raw_parts(ptr, len);
|
||||
slice.to_vec()
|
||||
}
|
||||
|
||||
unsafe fn static_drop(_: &mut AtomicPtr<()>, _: *const u8, _: usize) {
|
||||
// nothing to drop for &'static [u8]
|
||||
}
|
||||
|
|
|
@ -146,6 +146,7 @@ impl From<V8Slice> for bytes::Bytes {
|
|||
const V8SLICE_VTABLE: rawbytes::Vtable = rawbytes::Vtable {
|
||||
clone: v8slice_clone,
|
||||
drop: v8slice_drop,
|
||||
to_vec: v8slice_to_vec,
|
||||
};
|
||||
|
||||
unsafe fn v8slice_clone(
|
||||
|
@ -161,6 +162,18 @@ unsafe fn v8slice_clone(
|
|||
rawbytes::RawBytes::new_raw(ptr, len, data.cast(), &V8SLICE_VTABLE)
|
||||
}
|
||||
|
||||
unsafe fn v8slice_to_vec(
|
||||
data: &rawbytes::AtomicPtr<()>,
|
||||
ptr: *const u8,
|
||||
len: usize,
|
||||
) -> Vec<u8> {
|
||||
let rc = Rc::from_raw(*data as *const V8Slice);
|
||||
std::mem::forget(rc);
|
||||
// NOTE: `bytes::Bytes` does bounds checking so we trust its ptr, len inputs
|
||||
// and must use them to allow cloning Bytes it has sliced
|
||||
Vec::from_raw_parts(ptr as _, len, len)
|
||||
}
|
||||
|
||||
unsafe fn v8slice_drop(
|
||||
data: &mut rawbytes::AtomicPtr<()>,
|
||||
_: *const u8,
|
||||
|
|
Loading…
Reference in a new issue