Added: Unsafe API for Creating Mmap(s) from Existing Addresses #40
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Added: Ability to create Mmap unsafely from an existing address.
This PR extends the functionality of our memory-mapping library by allowing
Mmap
objects to be created from an existing, raw memory address. This feature has been added through the newMmapOptions::map_from_existing
API, which is unsafe and returns a platform-specificMmapMut
object.Lifetime Management
Memory-mapping objects created from raw addresses become 'owned' by the calling code.
This means that the lifetime of the mapped region is now managed by the
MmapMut
object, and dropping this object will cause the corresponding memory region to be unmapped.This is documented alongside the newly added
map_from_existing
API.Tests
A basic unit test was added. This test creates a new
MmapMut
object from an existing, unmanaged memory address and performs the following checks:Validates that the data at the memory-mapped object's address matches what is expected, ensuring that the raw address mapping is correctly established.
Checks that permission changes performed from one
Mmap
object are reflected in the otherMmap
object, validating that objects point to the same physical page.Use Case
I am in the process of porting a well known C# function hooking library to Rust (Work in Progress), while making it as portable as possible. (This means supporting non-x86 architectures and non-Windows OSes)
mmap-rs
supports more 'edge-case' platforms such as FreeBSD, Android and iOS; so for those platforms where testing is more difficult, I would prefer to leverage existing code, even if I wind up wasting some CPU cycles on Mmap object initialization for some more temporary actions (like temp changing permissions).Notes
Open to making any changes requested before merging; I'm still (relatively) new to Rust, so if I've missed anything here, please let me know.