#include <sys/mman.h>(executable NetRun link)
int foo(void) {
int len=1024*1024;
void *addr=mmap((void *)0,len, /* address and data size (bytes) */
PROT_READ+PROT_WRITE,MAP_ANONYMOUS+MAP_SHARED, /* access flags */
-1,0); /* <- file descriptor (none needed) and file offset */
if (addr==MAP_FAILED) {perror("mmap"); exit(1);}
int *buf=(int *)addr; /* <- make mmap'd region into an int pointer */
buf[3]=7;
buf[2]=buf[3];
printf("mmap returned %p, which seems readable and writable\n",addr);
munmap(addr,len);
return 0;
}
Purpose |
Code |
You just want some memory from the OS. | void *mem=mmap(0,length, PROT_READ+PROT_WRITE, MAP_ANONYMOUS+MAP_SHARED, -1,0); |
You want to put some memory at a given location, for example to service a page fault, or operate with old code, so you pass in an address. | mmap((void *)0xabcde000,length, PROT_READ+PROT_WRITE, MAP_ANONYMOUS+MAP_SHARED, -1,0); |
You want to mark a given location as unreadable, for example to cause pagefaults when people try to access there. |
mmap((void *)0xabcde000,length, PROT_NONE, MAP_ANONYMOUS+MAP_SHARED, -1,0); |
You want to create some executable memory, for example to write some machine code there. |
void *mem=mmap(0,length, PROT_READ+PROT_WRITE+PROT_EXEC, MAP_ANONYMOUS+MAP_SHARED, -1,0); |
You want to bring in the file "fd" for reading. |
void *mem=mmap(0,length, PROT_READ, MAP_ANONYMOUS+MAP_SHARED, fd,0); |
You want to bring in the file "fd" for reading and writing. |
void *mem=mmap(0,length, PROT_READ+PROT_WRITE, MAP_ANONYMOUS+MAP_SHARED, fd,0); |
Flags |
What |
Why |
Weirdness |
PROT_NONE |
Disable all access to the memory. | Basically requesting a page fault when accessed. Used by "electric fence" to find memory access errors. |
! |
PROT_READ |
Read only area. | Useful for input files, or big read-only tables. |
no |
PROT_WRITE |
Write only area. | Can't be read, though. Secure shared drop box? |
!! |
PROT_EXEC |
Execute only area. | Secure code? |
!! |
PROT_READ+PROT_WRITE |
Read-write access. | Most ordinary memory from "new" is allocated like this. You can't execute code here, as a security feature. |
no |
PROT_READ+PROT_EXEC |
Readable (for constants) and executable (for code). |
Most programs are mapped this way. |
no |
PROT_WRITE+PROT_EXEC |
Write and execute, but not read? | Maybe for a dynamically generated program, plus security? |
!!! |
PROT_READ+PROT_WRITE+PROT_EXEC |
Allow all access: do what thou wilt. |
Once used for everything. Good for dynamically created code. |
! |