Reject zero-sized element types in ThinVec gecko-ffi mode
Categories
(Core :: XPCOM, task)
Tracking
()
People
(Reporter: mail, Assigned: emilio)
References
Details
Attachments
(3 external files)
Spun off bug 2067654, at emilio's suggestion.
The Happy Eyeballs glue declared the C++ mozilla::net::NetAddr as an opaque handle:
#[repr(C)]
pub union NetAddr {
_private: [u8; 0],
}
and then used it as a collection element type, ThinVec<NetAddr>, which cbindgen renames to nsTArray<NetAddr> on the C++ side. C++ passed an array of real 112-byte elements and Rust walked it with a stride of 0. The length and the base pointer were both right, so nothing crashed and no debug_assert! fired: an N-address DNS answer became N copies of the first address.
Proposal: make ThinVec<T> refuse a zero-sized T when gecko-ffi is on. data_raw() already notes that padding::<T>() holds "~static assertions against types that are incompatible with the current feature flags" and is called before every data pointer.
#[cfg(all(feature = "gecko-ffi", not(test)))]
const {
assert!(
mem::size_of::<T>() != 0,
"ThinVec<T> cannot bridge to nsTArray<T> when T is zero-sized"
)
};
Tried against the vendored 0.2.16 with a crate that only borrows and iterates a ThinVec<NetAddr> and never constructs one, which is the shape bug 2067654 had:
error[E0080]: evaluation panicked: ThinVec<T> cannot bridge to nsTArray<T> when T is zero-sized
note: the above error was encountered while instantiating `fn ThinVec::<NetAddr>::data_raw`
Two caveats:
- It is a post-monomorphization error, so
cargo checkstays quiet and only a real build reports it. That is enough here, since every glue crate gets built. - thin-vec's own tests use
ThinVec<()>(thin_vec![(); 5],ThinVec::<()>::with_capacity), so the assert needs thenot(test)arm, the wayEMPTY_HEADERis already cfg'd, orcargo test --features gecko-ffibreaks.
| Assignee | ||
Comment 1•1 month ago
|
||
| Assignee | ||
Comment 2•1 month ago
|
||
A single variant enum uses a ZST under the hood by default. Prevent it
to be able to forbid ZSTs in ThinVec altogether.
| Assignee | ||
Comment 3•1 month ago
|
||
| Assignee | ||
Updated•1 month ago
|
Comment 6•1 month ago
|
||
| bugherder | ||
Comment 7•1 month ago
|
||
| bugherder | ||
| Assignee | ||
Updated•1 month ago
|
Updated•1 month ago
|
Description
•