Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adjust Allocation Bytes used by Miri to custom MiriAllocBytes #3526

Open
wants to merge 9 commits into
base: master
Choose a base branch
from

Conversation

Strophox
Copy link

@Strophox Strophox commented Apr 28, 2024

Previously, the MiriMachine used type Bytes = Box<[u8]> for its allocations.
This PR swaps this out for a custom MiriAllocBytes type implemented in alloc_bytes.rs.
This is in anticipation of an extension to Miri's FFI, which will require its allocations to take care of alignment (the methods in impl AllocBytes for Box<[u8]> ignore this _align: Align argument).

Needs rust-lang/rust#124492

@Strophox Strophox marked this pull request as ready for review April 29, 2024 13:40
Copy link
Member

@RalfJung RalfJung left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, this is a good start!

src/alloc_bytes.rs Outdated Show resolved Hide resolved
src/alloc_bytes.rs Show resolved Hide resolved
src/alloc_bytes.rs Outdated Show resolved Hide resolved
src/alloc_bytes.rs Outdated Show resolved Hide resolved
src/alloc_bytes.rs Show resolved Hide resolved
} else {
alloc_fn(layout)
};
if ptr.is_null() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're now checking this even in the size == 0 case, which is unnecessary. You can move this if inside the else { ... } above.

}

impl Clone for MiriAllocBytes {
fn clone(&self) -> Self {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you implement this using MiriAllocBytes:: from_bytes(Cow::Borrowed(&*self), ...)? Then you don't need unsafe here. :)

let alloc_bytes = MiriAllocBytes::alloc_with(size, align, alloc_fn)
.unwrap_or_else(|layout| alloc::handle_alloc_error(layout));
// SAFETY: `alloc_bytes.ptr` and `slice.as_ptr()` are non-null, properly aligned
// and valid for the `slice.len()`-many bytes to be copied.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// and valid for the `slice.len()`-many bytes to be copied.
// and valid for the `size`-many bytes to be copied.

fn as_mut_ptr(&mut self) -> *mut u8 {
self.ptr
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a newline at the end. (./miri fmt should do that automatically, and also otherwise will make sure you use standard Rust formatting.)

@@ -122,7 +122,7 @@ impl VisitProvenance for OpTy<'_, Provenance> {
}
}

impl VisitProvenance for Allocation<Provenance, AllocExtra<'_>> {
impl<Bytes: AllocBytes> VisitProvenance for Allocation<Provenance, AllocExtra<'_>, Bytes> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
impl<Bytes: AllocBytes> VisitProvenance for Allocation<Provenance, AllocExtra<'_>, Bytes> {
impl VisitProvenance for Allocation<Provenance, AllocExtra<'_>, MiriAllocBytes> {

fn from_bytes<'a>(slice: impl Into<Cow<'a, [u8]>>, align: Align) -> Self {
let slice = slice.into();
let size = slice.len();
let align = align.bytes().try_into().unwrap();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After rebasing over the latest Miri master, you should be able to use align.bytes_usize() here.

@RalfJung

This comment was marked as outdated.

Strophox and others added 3 commits May 3, 2024 13:01
Co-authored-by: Ralf Jung <post@ralfj.de>
Co-authored-by: Ralf Jung <post@ralfj.de>
Co-authored-by: Ralf Jung <post@ralfj.de>
@RalfJung RalfJung mentioned this pull request May 3, 2024
bors added a commit that referenced this pull request May 3, 2024
Preparing for merge from rustc

Unblocks #3526.
@RalfJung
Copy link
Member

RalfJung commented May 4, 2024

If you rebase over the latest Miri master, you should get the updated rustc that includes your PR, so that this one can finally build. :)

RalfJung pushed a commit to RalfJung/rust that referenced this pull request May 4, 2024
RalfJung pushed a commit to RalfJung/rust that referenced this pull request May 4, 2024
@RalfJung RalfJung added the S-waiting-on-author Status: Waiting for the PR author to address review comments label May 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-author Status: Waiting for the PR author to address review comments
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants