Description
A follow-up from rust-lang/rust#129090 (comment)
I'm writing a custom memory allocator/object pool. I have a bunch of Box<[Thing]>
chunks, where some of the Thing
s are mutably borrowed as &mut Thing
for a limited lifetime. During that lifetime, I need to access the slice len
, but I don't need to touch its contents. I figured this would be okay to do, since the len
is stored in the fat pointer, not in the slice itself. However naively doing chunk.len()
or (&*chunk).len()
triggers Miri UB warning. The problem seems to be that the dereference invalidates an earlier Unique
tag within the slice. The offset 0x0..0x60
below corresponds to the whole memory span of the slice.
<318767> was later invalidated at offsets [0x0..0x60] by a SharedReadOnly retag
I managed to get it to work with Box::as_ptr(&chunk).len()
(there fortunately is a method <*const [T]>::len()
), but this is not ideal as Box::as_ptr
is still unstable. (&raw const *chunk).len()
doesn't seem to work, as it retags the whole slice like chunk.len()
.
Are there any stable, Miri-friendly workarounds for getting the length, or more generally getting a pointer to a slice behind a box without retagging the contents of the slice?