serde_v8: fix pointer size assumptions (#15613)

This commit is contained in:
Will Glozer 2022-08-27 21:35:32 +09:00 committed by GitHub
parent ec98d86d21
commit 09c256d5eb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 8 additions and 5 deletions

View file

@ -5,6 +5,8 @@ use crate::Error;
use smallvec::SmallVec; use smallvec::SmallVec;
use std::mem::size_of; use std::mem::size_of;
const USIZE2X: usize = size_of::<usize>() * 2;
#[derive( #[derive(
PartialEq, PartialEq,
Eq, Eq,
@ -18,14 +20,15 @@ use std::mem::size_of;
)] )]
#[as_mut(forward)] #[as_mut(forward)]
#[as_ref(forward)] #[as_ref(forward)]
pub struct ByteString(SmallVec<[u8; 16]>); pub struct ByteString(SmallVec<[u8; USIZE2X]>);
impl_magic!(ByteString); impl_magic!(ByteString);
// const-assert that Vec<u8> and SmallVec<[u8; 16]> have a same size. // const-assert that Vec<u8> and SmallVec<[u8; size_of::<usize>() * 2]> have a same size.
// Note from https://docs.rs/smallvec/latest/smallvec/#union - // Note from https://docs.rs/smallvec/latest/smallvec/#union -
// smallvec can still be larger than Vec if the inline buffer is // smallvec can still be larger than Vec if the inline buffer is
// larger than two machine words. // larger than two machine words.
const _: () = assert!(size_of::<Vec<u8>>() == size_of::<SmallVec<[u8; 16]>>()); const _: () =
assert!(size_of::<Vec<u8>>() == size_of::<SmallVec<[u8; USIZE2X]>>());
impl ToV8 for ByteString { impl ToV8 for ByteString {
fn to_v8<'a>( fn to_v8<'a>(

View file

@ -105,13 +105,13 @@ pub(crate) unsafe fn opaque_recv<T: ?Sized>(ptr: &T) -> u64 {
/// Transmutes an "opaque" ptr back into a reference /// Transmutes an "opaque" ptr back into a reference
pub(crate) unsafe fn opaque_deref<'a, T>(ptr: u64) -> &'a T { pub(crate) unsafe fn opaque_deref<'a, T>(ptr: u64) -> &'a T {
std::mem::transmute(ptr) std::mem::transmute(ptr as usize)
} }
/// Transmutes & copies the value from the "opaque" ptr /// Transmutes & copies the value from the "opaque" ptr
/// NOTE: takes ownership & requires other end to forget its ownership /// NOTE: takes ownership & requires other end to forget its ownership
pub(crate) unsafe fn opaque_take<T>(ptr: u64) -> T { pub(crate) unsafe fn opaque_take<T>(ptr: u64) -> T {
std::mem::transmute_copy::<T, T>(std::mem::transmute(ptr)) std::mem::transmute_copy::<T, T>(std::mem::transmute(ptr as usize))
} }
macro_rules! impl_magic { macro_rules! impl_magic {