fork fails after nmap with hint address in an unmapped memory region

Stéphane Mbape via cygwin cygwin@cygwin.com
Sat Dec 9 15:50:00 GMT 2017


Hello,

While embeding luajit in a c  program, I found myself unable to fork 
processes.
Investigations prove that it was related to nmap.
To be accurate, calling nmap with hint address in a unmapped memory 
region will cause all forks to fail with
"fixup_mmaps_after_fork: ReadProcessMemory failed for MAP_PRIVATE 
address 0x6FFFFFE0000, Win32 error 299"

There is a sample code below.

Thank you for reading.


#include <stdio.h>
#include <stdlib.h>
#include <sys/mman.h>
#include <sys/wait.h>
#include <unistd.h>
#include <stdint.h>

#define MMAP_PROT           (PROT_READ|PROT_WRITE)
#define MMAP_FLAGS_PROBE    (MAP_PRIVATE|MAP_ANONYMOUS)

int main() {
     printf("I am master %d\n", (int) getpid());

     size_t size = ((size_t)128U * (size_t)1024U);
     uintptr_t hint_addr = 0;

     void *p = mmap((void *)hint_addr, size, MMAP_PROT, 
MMAP_FLAGS_PROBE, -1, 0);
     printf ("nmap() = %p, hint_addr = %p\n", p, (void *) hint_addr);

     uintptr_t addr = (uintptr_t) p;

     munmap(p, size); // make sure there is an unmapped memory

     // hint_addr = addr; // produces no error
     hint_addr = addr + 1;
     p = mmap((void *)hint_addr, size, MMAP_PROT, MMAP_FLAGS_PROBE, -1, 0);
     printf ("nmap() = %p, hint_addr = %p\n", p, (void *) hint_addr);

     pid_t child_pid = fork();

     if (child_pid < 0) {
         perror("fork failed");
     } else if (child_pid == 0) {
         printf("I am worker %d\n", (int) getpid());
         sleep(2);
         printf("worker exiting\n");
         exit(0);
     }

     wait(NULL);

     printf("master exiting\n");
     return 0;
}


--
Problem reports:       http://cygwin.com/problems.html
FAQ:                   http://cygwin.com/faq/
Documentation:         http://cygwin.com/docs.html
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple



More information about the Cygwin mailing list