-
Notifications
You must be signed in to change notification settings - Fork 73
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
Safe API for RO memmap construction #73
Comments
I'm not sure I understood your question. |
Yep, the code I wrote works, however it requires that |
Can you provide a code example? |
Sure thing :) // this is some (abridged) code in my project
pub struct FileContent(MmapRaw);
impl FileContent {
pub fn from_file(file: &std::fs::File) -> std::io::Result<Self> {
// I would like this code to be forbid_unsafe, but currently need an unsafe block to call `Mmap::map`
Ok(Self(unsafe { memmap2::Mmap::map(file) }?.into()))
// Instead, I'd rather write:
Ok(Self(memmap2::MmapRaw::map_raw_read_only(file)?))
}
} |
memmap is inherently unsafe. Even in a read-only mode, because other process can modify the file content, which violates Rust safety rules. It cannot be made safe. |
If you believe that, you're going to need to fix the https://docs.rs/memmap2/latest/memmap2/struct.MmapRaw.html#method.map_raw API 😉 However I think that API is perfectly valid, because it doesn't allow any UB and is perfectly sound to call within Rust. At least, I'd be incapable of triggering UB with it. |
But what would you do with the raw pointer access enabled via |
You would need unsafe to access
Fix what? Everything works as expected. |
You have an API That's all I came to ask :) If you feel the additional API in memmap2 would be superfluous, you can close the issue |
To maybe clarify the original issue, I would like an extra parameter in the Line 130 in 9970820
...without changing the safety properties of the API |
I think the aim is understandable, but sadly and especially with For example, let's you safely create a read-only So in this case, the safety requirements are not just not localized in the same process as the unsafe call to So in summary, while technically the API you envision can be added and would be sound as far I can see, I don't think it will really benefit your initial aim for introducing it. |
I'm specifically working with shared memory and pointer reads are reasoned about in the presence of it, so I definitely don't agree there 😄 I still think you might as well be talking about the API that's already present in the crate (do you think it should be deprecated?) but I can't add any more to this discussion. |
Yes, the existing EDIT: Add a missing "not" before "add". |
@RazrFalcon We do have pub fn map_raw_read_only<T: MmapAsRawDesc>(&self, file: T) -> Result<MmapRaw> {
let desc = file.as_raw_desc();
MmapInner::map(self.get_len(&file)?, desc.0, self.offset, self.populate)
.map(|inner| MmapRaw { inner })
} and some documentation. Meaning the implementation is basically free. So maybe we just add it? |
@adamreichold But I honestly do not have any input here. I personally use this crate just to read files faster. This is the only thing I care about. I have zero knowledge and experience with any other use cases. @Plecra I'm sorry, but I'm genuinely have no idea what problem are you having. If this all just about a slightly simpler API, then I think it can be ignored, since memmap is inherently unsafe. There is no point in pretending it is. It would just confuse the user. |
Nope, that's it :) It's about extending the safe I want to use |
Note that this is not unsound: It is common for unsafe code to temporarily lift invariants so that even calling safe methods of the involved types would be UB (the |
@Plecra Published. Hopefully this is what you wanted. |
I'd like to be able to call a safe version of
MmapRaw::from(unsafe { Mmap::map(&file) })
, as a sibling API formap_raw
.Maybe it should be possible to pass flags for read/write/exec to MmapOptions::map_raw?
The text was updated successfully, but these errors were encountered: