Skip to content

forward_declare: give slices a Name that no array can have - #3026

Open
dhivadhiva wants to merge 1 commit into
google:mainfrom
dhivadhiva:forward-declare-slice-name
Open

dhivadhiva wants to merge 1 commit into
google:mainfrom
dhivadhiva:forward-declare-slice-name

Conversation

@dhivadhiva

Copy link
Copy Markdown
Contributor

[T] used ([(); usize::MAX], T::Name) as its CppType::Name. That relied on [Incomplete<..>; usize::MAX] not being a valid type, but since Unsized became (), Incomplete is zero-sized and that array type exists. Its Name is the same as the slice's, so &[T] (a fat pointer) and &[Incomplete<..>; usize::MAX] (a thin pointer) ended up in the same transmutability class.

The Vec<&T> -> Vec<&U> CppCoerce impl reinterprets the buffer without checking that &T and &U have the same size, so this is reachable from safe code:

unsafe_define!(symbol!("Position"), Position);
forward_declare!(pub PositionFwd = symbol!("Position"));

let data = [Position { x: 7, y: 8 }; 4];
let fat: Vec<&[Position]> = vec![&data[..]];
let mut thin: Vec<&[PositionFwd; usize::MAX]> = fat.cpp_coerce();
let e = thin[0];
thin.push(e); // buffer now holds [ptr, ptr]
let fat2: Vec<&[Position]> = thin.cpp_coerce();
let s: &[Position] = fat2[0]; // len is a pointer value, e.g. 0x7ffc554b0be0
println!("{:?}", &s[4..6]); // reads out of bounds

This change:

  • gives slices a dedicated internal::SliceName<Name>, so no array shares a slice's Name;
  • checks size_of::<&T>() == size_of::<&U>() in the Vec impl, like the blanket T -> U impl already does;
  • updates the array impl comment, which still said arrays of forward-declared types can't exist;
  • adds a Vec<&[T]> <-> Vec<&[Incomplete]> round trip to forward_declare_macros_test.rs.

Tested outside Bazel with cargo on nightly-2026-11-11, building forward_declare, forward_declare_proc_macros and cref from this tree (with #[gtest] swapped for #[test], since the public googletest crate lacks death_tests). The snippet above now fails to compile with E0271. forward_declare_macros_test.rs passes, including the new case. A hand-written CppType impl that puts a thin-pointer type into a slice's class now panics on the new size check instead of reading memory.

`[T]` used `([(); usize::MAX], T::Name)` as its `CppType::Name`, relying on
`[Incomplete<..>; usize::MAX]` not being a valid type. Since `Unsized` became
`()`, `Incomplete` is zero-sized, so that array type exists and has the same
`Name` as the slice. `&[T]` (a fat pointer) and `&[Incomplete<..>; usize::MAX]`
(a thin pointer) therefore ended up in the same transmutability class.

The `Vec<&T>` -> `Vec<&U>` `CppCoerce` impl reinterprets the buffer without a
size check, so safe code could turn `Vec<&[T]>` into `Vec<&[Fwd; usize::MAX]>`,
push a copy of the thin pointer, and coerce back to get a `&[T]` whose length
is a pointer value. Indexing it reads and writes out of bounds.

Use a dedicated `internal::SliceName<Name>` for slices, and check that `&T` and
`&U` have the same size in the `Vec` impl like the blanket impl already does.
Add a test for `Vec<&[T]>` coercion.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant