I learned that pages allocated by mmap()
can be unusable, even if the call returned success. For example if a file is being mapped and the len
passed to mmap()
is sufficiently larger than the file. In that case, any pages beyond the pages used by the file, even if within the requested size, will yield a SIGBUS
if they are attempted to be used.
Is there any way to directly determine if all the bytes from the beginning of the mapping up to len
will be accessible safely or not? Can I do this besides manually measuring the length of the file, or setting up a SIGBUS
handler, or otherwise (exhaustively) checking if any of the reason(s) a page would be inaccessible have been met?
// Assuming PAGE_SIZE == 4096
int fd = open("file", O_RDONLY);
void *buffer = mmap(NULL, 8192, PROT_READ, MAP_SHARED, fd, 0);
// Is there any way to determine if reading bytes 4093-8192 will cause a bus error?
// Besides measuring if the size of the file is greater than 4092?
Rather than checking the file size to determine if any pages would be unavailable because the file is too small, can I directly determine if any pages are unavailable for any reason?