r/rust•
insanitybit

How do I satisfy miri here/ change alignment?

I'm just messing around with the idea of reusing a Vec<T> when you need a Vec<U>.

https://play.rust-lang.org/?version=nightly&mode=debug&edition=2021&gist=2adde1b64b936d8dcdc93f6ed8852120

Miri complains that the ptr alignment is supposed to be 4 but the writes are done with an alignment of 1. Given that the pointer was created for u8 and now it's u32 I guess that makes sense. But... what to do?

Posted Mar 13, 2023, 5:54 PM UTC

614
Shnatsel•

It's not possible to cast from a Vec<u8> to a Vec<u32> without copying, because the allocation must be freed with the same size and alignment with which it was created. This is a requirement of allocators, not Rust, and this applies to C as well.

You can try casting a slice at runtime and see if the alignment matches. The crate bytemuck provides this as a safe function, try_cast_slice, to do just that. It will check if the alignment is correct for the larger type, and if yes, cast it; if it fails, you'll have to create a new allocation with the correct alignment and copy the data. This should be rare in practice because most malloc() implementations return allocations with rather high alignment.

19
insanitybitOP•

Thanks :)

because the allocation must be freed with the same size and alignment with which it was created

Interesting, why is that?

5
SkiFire13•

AFAIK it's mostly for future-proofing e.g. an allocator may use the knowledge about the alignment to yield allocations from different pools.

10
insanitybitOP•

Huh, interesting. I would have thought that an allocator would just get "you want N bytes? OK, here's a pointer to N bytes. When you want to free it give me that pointer and N back" and if I want to treat that pointer as N/8 or N/16 or N/32 or whatever it wouldn't care.

4
ssokolow•

These days, it's to reduce memory fragmentation in long-lived processes in the absence of a system that allows compaction (i.e. moving allocations).

If an allocator allocates from a bunch of pools for different block sizes, it minimizes the rate at which you wind up with tiny allocations dividing the blocks the kernel gives you up into "too small to satisfy the request" regions. (eg. jemalloc does this)

It's called "external fragmentation" and you can read about it [on Wikipedia](https://en.wikipedia.org/wiki/Fragmentation_(computing\)). Allocators like jemalloc choose instead to prefer "internal fragmentation". (i.e. If the allocation would consume 1022 bytes, leave the following two unallocated because the long-term cost of having an allocation that's not a nice even multiple of a block size is higher than wasting a couple of bytes.)

Even something like jemalloc can't fix it entirely. I have an actix-web server where I noticed that while using ab to hammer away on it for a while. (Something that'll nerd-snipe me into spending time optimizing that rather than working on more pressing things if I allow it.)

It'll be interesting to see what becomes of the allocator from "Compacting the Uncompactable" by Bobby Powers.

I'm sure there's also an element of "some more esoteric ISAs have really particular requirements about what offsets a given type must be at" though that's not as bad as it used to be as was touched on in passing in The Lost Art of Structure Packing.

10
minno•
3
insanitybitOP•

Interesting. I wonder if you could keep the initial allocation alignment around so that you can free the memory using that.

3
minno•

The easiest way to do that is to keep the original Vec around and only do type casts when you take a slice of it.

1
Tastaturtaste•

It's not possible to cast from a Vec<u8> to a Vec<u32> without copying, because the allocation must be freed with the same size and alignment with which it was created.

Wouldn't it theoretically be possible if the Vec does not get modified in a way that causes an allocation and gets cast back to its original size before it gets dropped?

1
Shnatsel•

Yes, and that's pretty much what casting a slice of it does.

5
Tastaturtaste•

Yeah, and there is another issue with my comment. Vecs with different generic types can have a different and incompatible layout. This makes the simple act of casting them UB.

1