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

mmap/munmap/mremamp shims #2520

Merged
merged 3 commits into from
Jun 20, 2023
Merged

mmap/munmap/mremamp shims #2520

merged 3 commits into from
Jun 20, 2023

Conversation

saethlin
Copy link
Member

@saethlin saethlin commented Aug 30, 2022

This adds basic support for mmap/mremap/munmap, with the specific goal of testing allocators targeting Linux under Miri.

This supports mmap with MAP_PRIVATE|MAP_ANONYMOUS, and PROT_READ|PROT_WRITE, and explicitly does not support MAP_SHARED (because that's asking for MMIO) as well as any kind of file mapping (because it seems like nobody does MAP_PRIVATE on files even though that would be very sensible). And (officially) we don't support MAP_FIXED, so we always ignore the addr argument.

This supports mremap only when the implementation is allowed to move the mapping (so no MREMAP_FIXED, no MREMAP_DONTUNMAP, and required MREMAP_MAYMOVE), and also when the entirety of a region previously mapped by mmap is being remapped.

This supports munmap but only when the entirety of a region previously mapped by mmap is unmapped.

src/shims/unix/mem.rs Outdated Show resolved Hide resolved
src/shims/unix/mem.rs Outdated Show resolved Hide resolved
src/shims/unix/mem.rs Outdated Show resolved Hide resolved
@saethlin saethlin added the S-waiting-on-author Status: Waiting for the PR author to address review comments label Sep 4, 2022
@saethlin saethlin force-pushed the mmap-shim branch 2 times, most recently from 19c0419 to a0a1679 Compare September 22, 2022 04:09
src/machine.rs Outdated Show resolved Hide resolved
@bors
Copy link
Collaborator

bors commented Oct 19, 2022

☔ The latest upstream changes (presumably #2322) made this pull request unmergeable. Please resolve the merge conflicts.

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! I left some comments. I think my main concern is that this duplicates book-keeping between the intptrcast module and these new 'mappings', and that seems error-prone. I'd prefer to have only on source of information for mapping between integers and AllocId.

src/intptrcast.rs Outdated Show resolved Hide resolved
src/intptrcast.rs Outdated Show resolved Hide resolved
src/shims/unix/foreign_items.rs Outdated Show resolved Hide resolved
src/shims/unix/mem.rs Outdated Show resolved Hide resolved
src/shims/unix/mem.rs Outdated Show resolved Hide resolved
src/shims/unix/mem.rs Show resolved Hide resolved
@saethlin
Copy link
Member Author

saethlin commented Jan 15, 2023

I'm picking this up again. I saw someone looking for this kind of support so I'm probably going to scope this down a bit to avoid some of the annoying questions it raises.

@saethlin saethlin force-pushed the mmap-shim branch 2 times, most recently from b585884 to c6f389f Compare January 16, 2023 04:21
@saethlin saethlin force-pushed the mmap-shim branch 3 times, most recently from d0d5d53 to 0e5835c Compare January 27, 2023 01:12
@saethlin
Copy link
Member Author

I think the only real thing left to do here is to figure out how to raise an error instead of reporting UB when a user tries to munmap part of a region from mmap. But I have no idea how to do that other than exposing the intptrcast internals. Help?

@saethlin saethlin marked this pull request as ready for review January 27, 2023 01:13
Comment on lines 190 to 191
// FIXME: The man page says this returns MAP_FAILED but that is type void*, and this
// function returns int.
Copy link
Member

Choose a reason for hiding this comment

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

I assume it's (int)MAP_FAILED then?

Copy link
Member Author

Choose a reason for hiding this comment

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

🤷 I just re-read the Linux man page for this and it specifies not only that munmap returns -1 on error, but in the docs for mmap:

the value MAP_FAILED (that is, (void *) -1)

@RalfJung
Copy link
Member

I think the only real thing left to do here is to figure out how to raise an error instead of reporting UB when a user tries to munmap part of a region from mmap. But I have no idea how to do that other than exposing the intptrcast internals. Help?

What exactly is it that needs to be accessed/checked here? I'm seeing a FIXME near a deallocate_ptr call, is that the one you mean? deallocate_ptr is not part of intptrcast though so I am confused?

src/shims/unix/mem.rs Outdated Show resolved Hide resolved
@RalfJung
Copy link
Member

That sounds like a ton of restrictions that basically make mmap be the same as a miri_allocate. No fundamental objection but I am surprised that that is even useful?

@saethlin
Copy link
Member Author

saethlin commented Feb 1, 2023

I am surprised that that is even useful?

This is enough to run the test suite for dlmalloc, unmodified. I'm a bit surprised too.

What exactly is it that needs to be accessed/checked here? I'm seeing a FIXME near a deallocate_ptr call, is that the one you mean? deallocate_ptr is not part of intptrcast though so I am confused?

The commits I just pushed addresses this munmap situation. I just wasn't sure how to build the check that the call to munmap is a whole previous allocation, but I guess I can get the information I need about existing allocations based on the access that was added for priroda?

@saethlin
Copy link
Member Author

@rustbot ready
I think?

@rustbot rustbot added S-waiting-on-review Status: Waiting for a review to complete and removed S-waiting-on-author Status: Waiting for the PR author to address review comments labels Feb 11, 2023
tests/pass-dep/shims/mmap.rs Outdated Show resolved Hide resolved
src/shims/unix/mem.rs Show resolved Hide resolved

pub(crate) fn round_up_to_multiple_of_page_size(&self, length: u64) -> Option<u64> {
#[allow(clippy::integer_arithmetic)] // page size is nonzero
(length.checked_add(self.page_size - 1)? / self.page_size).checked_mul(self.page_size)
Copy link
Member

Choose a reason for hiding this comment

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

The standard library should really have a method for rounding integers up and down...

src/shims/unix/mem.rs Outdated Show resolved Hide resolved
@saethlin saethlin force-pushed the mmap-shim branch 3 times, most recently from 29165b9 to a2f99ce Compare June 12, 2023 20:51
@saethlin
Copy link
Member Author

That was weird. I think the windows job timed out or something. It was cancelled, so I restarted it, and it passed this time.

@saethlin saethlin added S-waiting-on-review Status: Waiting for a review to complete and removed S-waiting-on-author Status: Waiting for the PR author to address review comments labels Jun 13, 2023
src/shims/unix/mem.rs Outdated Show resolved Hide resolved
src/shims/unix/mem.rs Outdated Show resolved Hide resolved
src/shims/unix/mem.rs Outdated Show resolved Hide resolved
src/shims/unix/mem.rs Outdated Show resolved Hide resolved
src/shims/unix/mem.rs Outdated Show resolved Hide resolved
src/shims/unix/mem.rs Outdated Show resolved Hide resolved
src/shims/unix/mem.rs Outdated Show resolved Hide resolved
@RalfJung
Copy link
Member

I'm still trying to understand what exactly you want to achieve for munmap.

Clearly this is not a full implementation of mmap. It says in the comments that it only supports the part that behaves like malloc/realloc/free. Why can't we just say that this applies to munmap as well, and that we require the given range to exactly match a previous mmap?

@saethlin
Copy link
Member Author

🤦 Yeah I can do that. I previously hadn't figured out how to use the prioroda access to the AllocMap to pull out the information I need to do that.

@RalfJung
Copy link
Member

RalfJung commented Jun 16, 2023 via email

@saethlin
Copy link
Member Author

But then we will get a false-positive UB report for a munmap call that doesn't perfectly correspond to an mmap call.

@saethlin saethlin removed the S-waiting-on-review Status: Waiting for a review to complete label Jun 16, 2023
@RalfJung
Copy link
Member

Yeah isn't that fine if we say we only support allocator-style logic?

Or are you saying we should try our hardest to throw "unsupported" instead of UB?

@saethlin
Copy link
Member Author

Yes, I think we should try our hardest to not say that a program is UB when it's just using something we don't support.

I think it's important that people can trust that when Miri says it has encountered Undefined Behavior that there is a problem in the program.

src/shims/unix/mem.rs Outdated Show resolved Hide resolved
src/shims/unix/mem.rs Outdated Show resolved Hide resolved
src/shims/unix/mem.rs Outdated Show resolved Hide resolved
tests/fail/mmap_invalid_dealloc.rs Outdated Show resolved Hide resolved
tests/pass-dep/shims/mmap.rs Show resolved Hide resolved
@RalfJung
Copy link
Member

Looking good. :) Please squash the history a bit, then r=me.

@saethlin
Copy link
Member Author

@bors r=RalfJung

@bors
Copy link
Collaborator

bors commented Jun 20, 2023

📌 Commit 4916a77 has been approved by RalfJung

It is now in the queue for this repository.

@bors
Copy link
Collaborator

bors commented Jun 20, 2023

⌛ Testing commit 4916a77 with merge 4b90db3...

@bors
Copy link
Collaborator

bors commented Jun 20, 2023

☀️ Test successful - checks-actions
Approved by: RalfJung
Pushing 4b90db3 to master...

@bors bors merged commit 4b90db3 into rust-lang:master Jun 20, 2023
7 checks passed
@saethlin saethlin deleted the mmap-shim branch June 20, 2023 16:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants