StephanvanSchaik / mmap-rs

A cross-platform and safe Rust API to create and manage memory mappings in the virtual address space of the calling process.
Apache License 2.0
61 stars 17 forks source link

implement Reserved to represent reserved memory mappings #27

Closed StephanvanSchaik closed 1 year ago

StephanvanSchaik commented 1 year ago

This introduces Reserved, ReservedNone and ReservedMut to represent reserved memory mappings that can later on be committed. Similar to Mmap objects, these can be split and the permissions of the memory mappings can be changed. In addition, these implement TryInto such that they can be converted into their corresponding Mmap object, committing the memory mapping on Microsoft Windows.

Since these represent memory mappings without being backed by physical pages, Reserved objects do not implement Deref or DerefMut such that they are inaccessible until committed.

See also PR #12 and PR #13.

s1341 commented 1 year ago

Looks good. Did you cut a release with this included?

StephanvanSchaik commented 1 year ago

Looks good. Did you cut a release with this included?

I haven't released a new version of mmap-rs yet that includes this PR, but I do think that mmap-rs is getting in good shape for a 0.6 release with the current changes. So let me see if I can get around to that by the end of this week.

s1341 commented 1 year ago

Thanks. That'd be awesome.

StephanvanSchaik commented 1 year ago

@s1341 mmap-rs 0.6 is now available.

s1341 commented 1 year ago

I finally got a chance to try this and ran into a roadblock.

I need to be able to reserve a very large region, and then commit pages in that region one at a time.

I can do it with split_off, but then I am required to manage a bunch of Reserved and Mmap objects....

Can you think of a better implementation?

StephanvanSchaik commented 1 year ago

Is it possible for you to commit pages from the beginning to the end? If so, you could maintain just one Reserved object and one Mmap object by calling split_off() on the first page of Reserved, committing it, and merging it with the Mmap object. I think that would simplify it a lot.

s1341 commented 1 year ago

Nope. In the end I just keep a HashMap of Reserved that are split off of the large region. (Implementing ASAN shadow memory).

StephanvanSchaik commented 1 year ago

I don't see a cleaner way of doing it other than managing the objects into their own respective HashMap objects. At least not without giving up on modelling it with Rust ownership and using the type system to restrict access depending on the type.

s1341 commented 1 year ago

Yeah. Ok. That's what I figured, but I thought I'd pick your brain...