Skip to content

Alignment of the bytes of Allocation to match align parameter#100467

Closed
emarteca wants to merge 25 commits intorust-lang:masterfrom
emarteca:alloc-align
Closed

Alignment of the bytes of Allocation to match align parameter#100467
emarteca wants to merge 25 commits intorust-lang:masterfrom
emarteca:alloc-align

Conversation

@emarteca
Copy link

Aligning the bytes field of Allocation so that it is aligned matching the align parameter.

Main changes are to:

Instead of just converting the relevant [u8] to a Box<[u8]> directly we now:

  • create a std::alloc::Layout of the alignment the Allocation is specified to have
  • allocate a buffer with this layout
  • get the corresponding [u8] for this buffer

Then we write the relevant bytes to it, check for uninit, etc -- same process as previously.

NOTE
With this current code, when the bytes field is deallocated, even though the size is right, in the layout the alignment might be under-required: for example we might have allocated it with alignment 4 and deallocate it with alignment 2. We know this is undefined behaviour violating the memory fitting requirements of dealloc -- we're going to work around this by adding a wrapper struct for the Box<[u8]> that stores the alignment it was allocated with, and manually implements deallocation with the right alignment.

Why are we doing this?

We want the address of the bytes field of an Allocation to be accessible, and represent the actual address of the [u8]. This is part of an effort to pass pointers in Miri to the C FFI.

Loading
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-inactive Status: Inactive and waiting on the author. This is often applied to closed PRs. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.