@@ -429,7 +429,7 @@ esac
AC_CHECK_HEADERS(sys/mman.h syscall.h sys/epoll.h sys/ptrace.h sys/syscall.h sys/user.h sys/utsname.h)
AM_CONDITIONAL(HAVE_SYS_MMAN_H, test "$ac_cv_header_sys_mman_h" = yes)
-AC_CHECK_FUNCS(srandom random strerror_r strsignal wait4)
+AC_CHECK_FUNCS(srandom random strerror_r strsignal wait4 mincore)
AM_CONDITIONAL(HAVE_STRERROR_R, test "$ac_cv_func_strerror_r" = yes)
AM_CONDITIONAL(HAVE_WAIT4, test "$ac_cv_func_wait4" = yes)
@@ -1,4 +1,5 @@
#include <errno.h>
+#include <unistd.h>
#include "runtime.h"
#include "malloc.h"
@@ -16,6 +17,23 @@
static int dev_zero = -1;
#endif
+static _Bool
+addrspace_free(void *v __attribute__ ((unused)), uintptr n __attribute__ ((unused)))
+{
+#ifdef HAVE_MINCORE
+ size_t page_size = getpagesize();
+ size_t off;
+ char one_byte;
+
+ errno = 0;
+ for(off = 0; off < n; off += page_size)
+ if(mincore((char *)v + off, page_size, (char *)&one_byte) != -1
+ || errno != ENOMEM)
+ return 0;
+#endif
+ return 1;
+}
+
void*
runtime_SysAlloc(uintptr n)
{
@@ -109,7 +127,12 @@ runtime_SysMap(void *v, uintptr n)
// On 64-bit, we don't actually have v reserved, so tread carefully.
if(sizeof(void*) == 8) {
p = runtime_mmap(v, n, PROT_READ|PROT_WRITE|PROT_EXEC, MAP_ANON|MAP_PRIVATE, fd, 0);
- if(p != v) {
+ if(p != v && addrspace_free(v, n)) {
+ // On some systems, mmap ignores v without
+ // MAP_FIXED, so retry if the address space is free.
+ p = runtime_mmap(v, n, PROT_READ|PROT_WRITE|PROT_EXEC, MAP_ANON|MAP_FIXED|MAP_PRIVATE, fd, 0);
+ }
+ if (p != v) {
runtime_printf("runtime: address space conflict: map(%p) = %p\n", v, p);
runtime_throw("runtime: address space conflict");
}