Skip to content

Make pointer::align_offset exploit allocation alignment #873

Closed
@RalfJung

Description

@RalfJung

Miri's implementation of align_offset currently always says "nope, can't do that". We could do better than that in case the alignment of the allocation of the argument pointer is at least the requested alignment. In that case, we can basically run the real implementation of align_offset from libcore.

Testcase by @shepmaster:

use std::mem;

#[derive(Debug, Default)]
#[repr(align(8))]
struct AlignToU64<T>(T);

const BYTE_LEN: usize = mem::size_of::<[u64; 4]>();
type Data = AlignToU64<[u8; BYTE_LEN]>;

fn example(data: &Data) {
    let (head, u64_arrays, tail) = unsafe { data.0.align_to::<[u64; 4]>() };

    assert!(head.is_empty(), "buffer was not aligned for 64-bit numbers");
    assert_eq!(u64_arrays.len(), 1, "buffer was not long enough");
    assert!(tail.is_empty(), "buffer was too long");

    let u64_array = &u64_arrays[0];
    let _val = u64_array[0]; // make sure we can actually load this
}

fn main() {
    example(&Data::default());
}

There should also be another testcase where the modulo stuff becomes more interesting (align_offset returns an offset in units of size_of::<T>(), not bytes).

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-shimsArea: This affects the external function shimsC-enhancementCategory: a PR with an enhancement or an issue tracking an accepted enhancement

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions