Mmap not using the given address
-
27-06-2021 - |
문제
Although the requested mapping address is a page start, it will use an address shifted with a few pages.
I'm trying to do something like this:
char *mapped = mmap(base, page_size, PROT_NONE, MAP_SHARED,
file_handle, 0);
printf("Base : %p\n", base);
printf("Mapped: %p\n", mapped);
Sample output (page_size
= 4096
= 0x1000
):
Base : 0x7f22a1047000
Mapped: 0x7f22a1045000
Offset is 2 pages. This also seems to vary with length
. For example, if instead of one page I try to map 4 pages, output becomes:
Base : 0x7fd24d994000
Mapped: 0x7fd24d98f000
which is 5 pages offset.
Why is it behaving like this?
해결책
Because the OS is, when you don't specifically ask for a mapping at a fixed address, free to choose an address convenient for itself; from the mmap(2) man page:
MAP_FIXED
Don't interpret addr as a hint: place the mapping at exactly that
address. addr must be a multiple of the page size. If the memory
region specified by addr and len overlaps pages of any existing
mapping(s), then the overlapped part of the existing mapping(s) will be
discarded. If the specified address cannot be used, mmap() will fail.
Because requiring a fixed address for a mapping is less portable, the
use of this option is discouraged.
If you don't need that exact address, you're better off letting the system choose it (which, honestly, should be in the majority of cases).
제휴하지 않습니다 StackOverflow