mmap successed but SIGBUS generated on access
Hi, Jamie!
Thanks for an answer!
Quoted 1 line
Quoted 1 line
I have bad CD-R with a some number of unreadable
files.
Quoted 2 lines
Quoted 2 lines
Then user-space program use mmap system it returns ok
but any
Quoted 1 line
Quoted 1 line
attempt to access a memory pointed by this system
call finishes
Quoted 1 line
Quoted 1 line
with SIGBUS. So Midnight Commander internal file
viewer faults.
Quoted 2 lines
You can get the same problem even without read errors
with some
Quoted 1 line
configurations of NFS. (root mmaps a file owned by
someone else, but
Quoted 3 lines
cannot read the file due to `root_squash' on server).
It's not an error, it's standard behaviour.
Well.
Quoted 1 line
Quoted 1 line
Is there any way to detect such problem in user-space
without signal
Quoted 3 lines
Quoted 1 line
handlers ?
I don't think there is any way without a signal
handler.
Quoted 2 lines
It is possible to do something useful with a signal
handler sometimes.
Quoted 1 line
For example, you can
mmap()a zero page into the
offending page once
Quoted 1 line
you've got the fault address, or
read()a zero page if
you did
Quoted 1 line
MAP_PRIVATE(this produces fewer VMAs), set a flag, and
let the program
Quoted 1 line
continue until it checks the flag and aborts the
parsing or whatever
Quoted 1 line
operation it's doing.
So, I must read all of the mapped area and even this does not saves me of faulting next time if somebody change file permission. Does I understand this situation right?
Quoted 1 line
Unfortunately I don't think the signal handler's
si_errno is set
Quoted 1 line
properly to indicate the error. So another thing to
try is read() of
Quoted 1 line
the offending page, to get a useful error code. (And
if the read
Quoted 1 line
succeeds, that's ok because you did it at the correct
address so the
Quoted 3 lines
program can proceed anyway).
Fwiw, unfortunately not all versions of the kernel, or
all
Quoted 1 line
architectures, set
si_addrproperly for SIGBUS.
It's pity. But thanks again.
--
Regards,
Andrew.
____________________________________________
Играй в шахматы в Интернет на InstantChess.com!
Play chess on InstantChess.com !
www.instantchess.com
Quoted 11 lines · Andrew V. Samoilov
Quoted 6 lines
It is possible to do something useful with a signal handler sometimes. For example, you can
mmap()a zero page into the offending page once you've got the fault address, orread()a zero page if you didMAP_PRIVATE(this produces fewer VMAs), set a flag, and let the program continue until it checks the flag and aborts the parsing or whatever operation it's doing.So, I must read all of the mapped area and even this does not saves me of faulting next time if somebody change file permission. Does I understand this situation right?
If you use MAP_PRIVATE, then after your process modifies the mapped page, you have the data for sure. This includes calling read() over a page that raises a SIGBUS -- the page is "modified" by this operation, although the contents should hopefully be the correct file contents if read() succeeds. Any subsequent change to the file, including permission changes and data changes, won't affect the data you have in memory.
If you do not modify the pages (and usually, for efficiency, you wouldn't), then yes a change in file permissions can mean you can read data one second and will get a SIGBUS later. You're not guaranteed to get a SIGBUS, but you might get one -- it depends on whether the OS decides to reclaim the page's memory temporarily in between.
If you want to do something like an Editor's "Load File" operation, then you need to read the whole file. Either call read(), or call mmap() and then modify every page by reading one byte from each page and writing the same value back to the same place. read() is probably quicker, but I've never checked.
-- Jamie