From a641c5075866e6cf8ad121f68b1f5441c28faaa9 Mon Sep 17 00:00:00 2001 From: Christoph Otter Date: Wed, 6 Nov 2024 21:15:04 +0700 Subject: [PATCH 1/2] Fix (cherry picked from commit 4c0d2eab981c13a3cdb2489bca4b806947709012) --- libwasmvm/src/memory.rs | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/libwasmvm/src/memory.rs b/libwasmvm/src/memory.rs index 24d6e91b8..b3d0bad95 100644 --- a/libwasmvm/src/memory.rs +++ b/libwasmvm/src/memory.rs @@ -216,10 +216,14 @@ impl UnmanagedVector { match source { Some(data) => { let (ptr, len, cap) = { - // Can be replaced with Vec::into_raw_parts when stable - // https://doc.rust-lang.org/std/vec/struct.Vec.html#method.into_raw_parts - let mut data = mem::ManuallyDrop::new(data); - (data.as_mut_ptr(), data.len(), data.capacity()) + if data.capacity() == 0 { + (std::ptr::null_mut::(), 0, 0) + } else { + // Can be replaced with Vec::into_raw_parts when stable + // https://doc.rust-lang.org/std/vec/struct.Vec.html#method.into_raw_parts + let mut data = mem::ManuallyDrop::new(data); + (data.as_mut_ptr(), data.len(), data.capacity()) + } }; Self { is_none: false, @@ -260,6 +264,8 @@ impl UnmanagedVector { pub fn consume(self) -> Option> { if self.is_none { None + } else if self.cap == 0 { + Some(Vec::new()) } else { Some(unsafe { Vec::from_raw_parts(self.ptr, self.len, self.cap) }) } @@ -348,7 +354,7 @@ mod test { // Empty data let x = UnmanagedVector::new(Some(vec![])); assert!(!x.is_none); - assert_eq!(x.ptr as usize, 0x01); // We probably don't get any guarantee for this, but good to know where the 0x01 marker pointer can come from + assert_eq!(x.ptr as usize, 0); assert_eq!(x.len, 0); assert_eq!(x.cap, 0); @@ -372,7 +378,7 @@ mod test { // Empty data let x = UnmanagedVector::some(vec![]); assert!(!x.is_none); - assert_eq!(x.ptr as usize, 0x01); // We probably don't get any guarantee for this, but good to know where the 0x01 marker pointer can come from + assert_eq!(x.ptr as usize, 0); assert_eq!(x.len, 0); assert_eq!(x.cap, 0); } From f58292448d138ceba671dc22916ba7e457939a55 Mon Sep 17 00:00:00 2001 From: Christoph Otter Date: Wed, 13 Nov 2024 16:23:18 +0700 Subject: [PATCH 2/2] Add comments (cherry picked from commit ca5f3d35a5543ed5ee83d8574dc5340c59f0413d) --- libwasmvm/src/memory.rs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/libwasmvm/src/memory.rs b/libwasmvm/src/memory.rs index b3d0bad95..cc30c6232 100644 --- a/libwasmvm/src/memory.rs +++ b/libwasmvm/src/memory.rs @@ -217,6 +217,10 @@ impl UnmanagedVector { Some(data) => { let (ptr, len, cap) = { if data.capacity() == 0 { + // we need to explicitly use a null pointer here, since `as_mut_ptr` + // always returns a dangling pointer (e.g. 0x01) on an empty Vec, + // which trips up Go's pointer checks. + // This is safe because the Vec has not allocated, so no memory is leaked. (std::ptr::null_mut::(), 0, 0) } else { // Can be replaced with Vec::into_raw_parts when stable @@ -265,6 +269,10 @@ impl UnmanagedVector { if self.is_none { None } else if self.cap == 0 { + // capacity 0 means the vector was never allocated and + // the ptr field does not point to an actual byte buffer + // (we normalize to `null` in `UnmanagedVector::new`), + // so no memory is leaked by ignoring the ptr field here. Some(Vec::new()) } else { Some(unsafe { Vec::from_raw_parts(self.ptr, self.len, self.cap) })