简体   繁体   中英

Mmap allocating more than one page

So I'm on x86-64 linux, and when I try to mmap one byte, I get way more than one page. Here's my thinking: When I allocate one byte, I should be able to access PAGE_SIZE bytes after that. That's how paging works, right? I confirmed that PAGE_SIZE on my system is 4096. But still, the following code doesn't segfault:

#include <sys/mman.h>
#include <stdio.h>

int main()
{
        char *p = mmap(0, 1, PROT_READ|PROT_WRITE, MAP_PRIVATE
                                                    |MAP_ANONYMOUS, -1, 0);
        p[5000] = 3;
}

5000 there is some arbitrary value greater than PAGE_SIZE . My understanding is, the line p[5000] = 3 should generate a page fault, and the page fault handler should realize that the page doesn't belong to me. But it doesn't happen. The code works. So is mmap giving me more than one page?

My understanding is, the line p[5000] = 3 should generate a page fault

No, dereferencing p to a memory that was not allocated is undefined behavior : it does not imply that your program must segfault.

For example, p[5000] may still access some memory your process owns and therefore no segfault occurs.

The code works. So is mmap giving me more than one page?

No, the fact that you don't get a segfault does not mean either that the code is correct.

From mmap 's manual page:

The length argument specifies the length of the mapping.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM