I guess I must goof somewhere but I can't see where, so multiple eyes may help.
I intended to use linux mremap() to grow an area in my VAS. The mremap() call seems to do the job, i.e new mapping, but odly enough the extended area is not accessible.
Here is my test program
#define _GNU_SOURCE
#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/mman.h>
int main(int c, char **v)
{ char *p;
int i;
setbuf(stdout, NULL);
printf("pid=%d\n",getpid());
p=mmap(0, 4096,PROT_READ|PROT_WRITE,MAP_ANONYMOUS|MAP_SHARED, -1, 0);
if((void*)p == MAP_FAILED)
{ printf("mmap failed\n");
}
p[0]='a';
printf("p=%#lx p[0]=%c\n",(long)p,p[0]);
printf("Paused [Ret]:"); read(0,&i,4);
p=mremap(p,4096,8192,MREMAP_MAYMOVE);
p[4095]='b';
printf("p=%#lx p[0]=%c p[4095]=%c\n",(long)p,p[0],p[4095]);
printf("Paused [Ret]:"); read(0,&i,4);
p[4096]='c';
printf("p=%#lx p[0]=%c p[4096]=%c\n",(long)p,p[0],p[4096]);
exit(0);
}
Running it I get
PW$ cc -o e e.c
PW$ ./e
pid=7178
p=0x7ffa912b9000 p[0]=a
Paused [Ret]:
At this point I can check the maps
PW$ grep zero /proc/7178/maps
7ffa912b9000-7ffa912ba000 rw-s 00000000 00:01 209101 /dev/zero (deleted)
We can see the mapping match p=0x7ffa912b9000, continuing the program we got after mremap()
p=0x7ffa9128b000 p[0]=a p[4095]=b
Paused [Ret]:
PW$ grep zero /proc/7178/maps
7ffa9128b000-7ffa9128d000 rw-s 00000000 00:01 209101 /dev/zero (deleted)
Here we can see the re-mapping was done, we obtained a new address, and the old data 'a' is still there. We can see also that this new mapping is 8192 in size 7ffa9128d000-7ffa9128b000-0x2000=8192. But then trying to write in there in the extended area bring chaos.
Paused [Ret]:
Bus error (core dumped)
I do this on Ubuntu 20.04 kernel is 5.4.0-29-generic
For a minute I thought that may be the extended area had no PROT_WRITE, though the original has it, so I plugged an mprotect() PROT_WRITE|PROT_READ on the new area (new addr, new size), yet no joy.
If someone can spot the goof, and provide a new pointer, it would be greatly appreciated :)
Cheers, Phi