Skip to content

allocated value is incorrectly aligned #11

@Kogia-sima

Description

@Kogia-sima

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.

https://github.com/fitzgen/bumpalo/blob/a1f663217f93b79b25c9580db33c54e19d022e9e/src/lib.rs#L920-L921

Or, forbit types with more than 8 byte alignement.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions