From fda365a53074f97f3213caefe70ea13297acecb2 Mon Sep 17 00:00:00 2001 From: Rich Felker Date: Mon, 2 Nov 2015 16:37:51 -0500 Subject: fix mremap memory synchronization and use of variadic argument 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 | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) (limited to 'src/mman') diff --git a/src/mman/mremap.c b/src/mman/mremap.c index 1096ace..ce4e8ea 100644 --- a/src/mman/mremap.c +++ b/src/mman/mremap.c @@ -1,3 +1,4 @@ +#define _GNU_SOURCE #include #include #include @@ -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); } -- cgit v1.1