Whilst reading the posix munmap specification I got some questions that I cannot answer using that specification.
For the munmap call it is specified:
[quote=The open group base specifications issue 7]
The munmap() function shall remove any mappings for those entire pages containing any part of the address space of the process starting at addr and continuing for len bytes. Further references to these pages shall result in the generation of a SIGSEGV signal to the process. If there are no mappings in the specified address range, then munmap() has no effect.
[/quote]
Now suppose we have a system with a page size of 4K and an application which has 4K of memory mapped at address 0
What (according to the specs) should be the result of the following munmap call:
munmap(0,1);
Should it do absolutely nothing aside from registering that a small part of that first page is unmapped so that when a call munmap(1,4K-1) happens it can unmap the entire page
or should it unmap the entire 4K page at address 0?
POSIX munmap
Re: POSIX munmap
Well, the key is in "shall remove any mappings for those entire pages containing any part of the address space"
So it will unmap the entire 4K page. Here's a test:
Attempting to use the mapping at word 8 faults, as expected.
So it will unmap the entire 4K page. Here's a test:
Code: Select all
bigcheese:~ chris$ uname -a
Darwin bigcheese.lan 10.6.0 Darwin Kernel Version 10.6.0: Wed Nov 10 18:13:17 PST 2010; root:xnu-1504.9.26~3/RELEASE_I386 i386
bigcheese:~ chris$ cat mmaptest.c
#include <sys/mman.h>
#include <stdio.h>
int main()
{
unsigned int *ptr = mmap(0, 4096, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, 0, 0);
if(ptr == (void *)-1)
{
printf("[-] mmap() failed\n");
return 1;
}
printf("[+] created mapping from %p\n", ptr);
*ptr = 0xdefaced;
munmap(ptr, 1);
printf("[+] destroyed mapping and attempting to write into it\n");
ptr[8] = 0;
printf("[+] are we still here?\n");
}
bigcheese:~ chris$ gcc -o testmmap mmaptest.c
bigcheese:~ chris$ ./testmmap
[+] created mapping from 0x100035000
[+] destroyed mapping and attempting to write into it
Segmentation fault