-
Notifications
You must be signed in to change notification settings - Fork 6
Open
Description
Description
According to Rust official reference (https://doc.rust-lang.org/reference/type-layout.html):
A value of alignment n must only be stored at an address that is a multiple of n.
However, if the value with more than 8 bytes is allocated using toolshed, it will be incorrectly aligned. That means toolshed may cause undefined behaviour without unsafe block.
Example
// main.rs
use toolshed::Arena;
#[repr(align(4096))]
#[derive(Clone, Copy, Default)]
struct U64Array {
values: [u64; 16],
}
fn main() {
println!("allocated on stack: {:p}", &U64Array::default() as *const _);
let arena = Arena::new();
let array = arena.alloc(U64Array::default());
println!("allocated using arena: {:p}", array as *mut _);
}
$ cargo run
allocated on stack: 0x7ffde7138000
allocated using arena: 0x564cf4988ca0
Possible solution
bumpalo crate has more heauristic way to correctly align the value.
Or, forbit types with more than 8 byte alignement.
Metadata
Metadata
Assignees
Labels
No labels