fix mremap memory synchronization and use of variadic argument
authorRich Felker <dalias@aerifal.cx>
Mon, 2 Nov 2015 21:37:51 +0000 (16:37 -0500)
committerRich Felker <dalias@aerifal.cx>
Mon, 2 Nov 2015 21:37:51 +0000 (16:37 -0500)
since mremap with the MREMAP_FIXED flag is an operation that unmaps
existing mappings, it needs to use the vm lock mechanism to ensure
that any in-progress synchronization operations using vm identities
from before the call have finished.

also, the variadic argument was erroneously being read even if the
MREMAP_FIXED flag was not passed. in practice this didn't break
anything, but it's UB and in theory LTO could turn it into a hard
error.

src/mman/mremap.c

index 1096ace..ce4e8ea 100644 (file)
@@ -1,3 +1,4 @@
+#define _GNU_SOURCE
 #include <unistd.h>
 #include <sys/mman.h>
 #include <errno.h>
@@ -6,19 +7,25 @@
 #include "syscall.h"
 #include "libc.h"
 
+static void dummy(void) { }
+weak_alias(dummy, __vm_wait);
+
 void *__mremap(void *old_addr, size_t old_len, size_t new_len, int flags, ...)
 {
        va_list ap;
-       void *new_addr;
+       void *new_addr = 0;
 
        if (new_len >= PTRDIFF_MAX) {
                errno = ENOMEM;
                return MAP_FAILED;
        }
 
-       va_start(ap, flags);
-       new_addr = va_arg(ap, void *);
-       va_end(ap);
+       if (flags & MREMAP_FIXED) {
+               __vm_wait();
+               va_start(ap, flags);
+               new_addr = va_arg(ap, void *);
+               va_end(ap);
+       }
 
        return (void *)syscall(SYS_mremap, old_addr, old_len, new_len, flags, new_addr);
 }