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

Document mmap safety #674

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

Conversation

CraftSpider
Copy link
Contributor

Supercedes #667

Document safety preconditions and reasoning for why they're followed. I'd like to push the unsafety down another level, or just remove the mmap function (make Mmap a common type with sys impls for the platform-specific bits), but those both are a lot more work.

@@ -186,7 +186,9 @@ impl<'data> Context<'data> {
fn mmap(path: &Path) -> Option<Mmap> {
let file = File::open(path).ok()?;
let len = file.metadata().ok()?.len().try_into().ok()?;
unsafe { Mmap::map(&file, len, 0) }
// SAFETY: All files we mmap are mmaped by the dynamic linker or the kernel already for the
// executable code of the process. Modifying them would cause crashes or UB anyways.
Copy link
Contributor

Choose a reason for hiding this comment

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

This is true for some but not all uses of mmap in this crate. DWARF can be in a separate file that is not loaded by the dynamic linker or kernel (.dSYM on macOS, .debug/.dwz/.dwp on linux, or unpacked object files on both).

That doesn't mean we should use fs::read for those though. We don't need to read all of the DWARF, and doing so would affect performance and memory usage.

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.

2 participants