public inbox for cygwin@cygwin.com
 help / color / mirror / Atom feed
* fork fails after nmap with hint address in an unmapped memory region
@ 2017-12-09 15:50 Stéphane Mbape via cygwin
  2017-12-09 18:29 ` Andrey Repin
  2017-12-10 16:13 ` Corinna Vinschen
  0 siblings, 2 replies; 14+ messages in thread
From: Stéphane Mbape via cygwin @ 2017-12-09 15:50 UTC (permalink / raw)
  To: cygwin

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

^ permalink raw reply	[flat|nested] 14+ messages in thread

end of thread, other threads:[~2017-12-10 18:04 UTC | newest]

Thread overview: 14+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-12-09 15:50 fork fails after nmap with hint address in an unmapped memory region Stéphane Mbape via cygwin
2017-12-09 18:29 ` Andrey Repin
2017-12-09 20:12   ` Stéphane Mbape via cygwin
2017-12-09 21:57     ` Brian Inglis
2017-12-10  1:23       ` Brian Inglis
2017-12-10  6:41       ` Jon Turney
2017-12-10 13:31         ` Brian Inglis
2017-12-10 15:29           ` Corinna Vinschen
2017-12-10 13:30   ` Stéphane Mbape via cygwin
2017-12-10 18:04   ` Stéphane Mbape via cygwin
2017-12-11 13:37     ` Corinna Vinschen
2017-12-10 16:13 ` Corinna Vinschen
2017-12-10 18:04   ` Houder
2017-12-11 16:49     ` Corinna Vinschen

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).