omri-c
omri-c

Reputation: 81

Process virtual memory

Can process have a few virtual pages pointing to the same physical address in his same address space ?

I would like that virt_page1---> physical-X and also that virt_page2 ---> physical-X

How can it be done ? Should it be done from the kernel space ? what routines involves ?

if I would like to map shared library like this:

7ff2a90d8000-7ff2a928d000 r-xp 00000000 08:02 4980747 /lib/x86_64-linux-gnu/libc-2.15.so 7ff2a928d000-7ff2a948d000 ---p 001b5000 08:02 4980747 /lib/x86_64-linux-gnu/libc-2.15.so 7ff2a948d000-7ff2a9491000 r--p 001b5000 08:02 4980747 /lib/x86_64-linux-gnu/libc-2.15.so 7ff2a9491000-7ff2a9493000 rw-p 001b9000 08:02 4980747 /lib/x86_64-linux-gnu/libc-2.15.so

I see the mapping are private, does it mean that I can't map them again to other virtual address ? should I change the linker for that ?

Update:

While disabling ASLR I did the following:

int main(void)
{
  int fd = open("/lib/x86_64-linux-gnu/libc-2.15.so", O_RDONLY);
  void* f1 = mmap(0, 1748*1024, PROT_READ|PROT_EXEC, MAP_PRIVATE, fd, 0);
  void *f2 = (void*)0x00007ffff7a1a000;

  if (memcmp(f1, f2, 1748*1024) != 0) {
      printf("DIFFER\n");
  }
  while(1);
  return 0;
}

This is the .so mapping when there is no ASLR 00007ffff7a1a000 1748K r-x-- /lib/x86_64-linux-gnu/libc-2.15.so

So I mmap the regions of the above to other page & I got this:

00007ffff7e26000 1748K r-x-- /lib/x86_64-linux-gnu/libc-2.15.so

While I compare f1 & f2 I see the same data, is it to say that I have now to virtual regions mapped to the same physical address which is the shared library portion of 1748K ?

Upvotes: 0

Views: 375

Answers (1)

n. m. could be an AI
n. m. could be an AI

Reputation: 120021

Yes it's possible from the user space. The simplest method is to mmap the same file twice.

char templ[] = "XXXXXXXX";
int fd = mkstemp(templ);
ftruncate(fd, 1024);
void* f1 = mmap(0, 1024, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
void* f2 = mmap(0, 1024, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
strcpy (f1, "foo bar baz weep quup");
printf ("%p %s\n", f1, (char*)f1);
printf ("%p %s\n", f2, (char*)f2);

Upvotes: 0

Related Questions