Suppressing SIGBUS signals
Suppressing SIGBUS signals
Posted Jun 27, 2021 4:12 UTC (Sun) by roc (subscriber, #30627)In reply to: Suppressing SIGBUS signals by mathstuf
Parent article: Suppressing SIGBUS signals
You can have a safe mmap function that returns &[u8]. Then you could use something like https://docs.rs/safe-transmute/0.11.2/safe_transmute to transmute to a different kind of reference if that's safe.
You're right that in extremes, safety gets a bit fuzzy. It's nice to be able to push the boundaries pretty far out though.
Posted Jun 27, 2021 17:17 UTC (Sun)
by NYKevin (subscriber, #129325)
[Link] (3 responses)
Making a copy also resolves issues of the form "What if someone decides to scribble all over the file without changing its length, while I'm halfway through processing it?" More generally, this falls into the "validate, then process" model of doing things - you can't validate something if it can change out from under you!
Posted Jun 28, 2021 2:50 UTC (Mon)
by ilammy (subscriber, #145312)
[Link] (2 responses)
Posted Jun 30, 2021 11:57 UTC (Wed)
by hmh (subscriber, #3838)
[Link] (1 responses)
Mmap snapshot (maybe with a read-only result if that would be much easier or cheaper to implement and still cover the use cases).
But until someone offers to do that work...
Posted Jul 1, 2021 2:16 UTC (Thu)
by ilammy (subscriber, #145312)
[Link]
While thinking of how this could be implemented, I realized that it could be quite expensive, complicated, and full of “spooky action at a distance”. If some process grabs or released a MAP_COPY mapping of a file, then all existing processes must be made aware of it (e.g., by turning all mappings for everyone RO and catching page faults). Any change by the other process forces said process to expend some kernel time doing the copy of the page for the benefit of some other process, which is not particularly fair.
Turns out, adding MAP_COPY into Linux was discussed several times [2][3], but it's still considered a pretty stupid idea.
[1]: https://www.gnu.org/software/hurd/glibc/mmap.html
Suppressing SIGBUS signals
I wonder if some sort of copy-on-write private file mappings could help with that by avoiding copying the entire range (since mapped files tend to be huge). Like, you map a file a get a snapshot of its contents. Your process writing to that memory copies a page just for you and never syncs that page with the actual file. If any other process touches the file in any way via non-cow mapping or normal file ops, then the original data is copied and other process cow-mappings are dissociated from the file.
Suppressing SIGBUS signals
Suppressing SIGBUS signals
Suppressing SIGBUS signals
[2]: https://yarchive.net/comp/linux/map_copy.html
[3]: https://www.spinics.net/lists/linux-mm/msg119339.html
