Skip to content

Document mmap safety#674

Open
CraftSpider wants to merge 2 commits into
rust-lang:masterfrom
CraftSpider:mmap-docs
Open

Document mmap safety#674
CraftSpider wants to merge 2 commits into
rust-lang:masterfrom
CraftSpider:mmap-docs

Conversation

@CraftSpider

Copy link
Copy Markdown
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.

Comment thread src/symbolize/gimli.rs
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
Copy Markdown
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.


// NOTE: we map the remainder of the entire archive instead of just the library so we don't have to determine its length
// NOTE: mmap will fail if `zip_offset` is not page-aligned
// SAFETY: See `super::mmap` function

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The point of a safety comment for a use is to explain why that use is safe. I'm not sure what this is referring to and why it cannot explain safety directly instead.

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.

3 participants