-/* Return 0 for success. */
-static int
-do_mmap(os_vm_address_t *addr, os_vm_size_t len, int flags)
-{
- /* We *must* have the memory where we expect it. */
- os_vm_address_t old_addr = *addr;
-
- *addr = mmap(*addr, len, OS_VM_PROT_ALL, flags, -1, 0);
- if (*addr == MAP_FAILED ||
- ((old_addr != NULL) && (*addr != old_addr))) {
- FSHOW((stderr,
- "/retryable error in allocating memory from the OS\n"
- "(addr=0x%lx, len=0x%lx, flags=0x%lx)\n",
- (long) addr,
- (long) len,
- (long) flags));
- if (n_do_mmap_ignorable_errors > 0) {
- --n_do_mmap_ignorable_errors;
- } else {
- lose("too many errors in allocating memory from the OS");
- }
- perror("mmap");
- return 1;
+ /* KLUDGE: Disable memory randomization on new Linux kernels
+ * by setting a personality flag and re-executing. (We need
+ * to re-execute, since the memory maps that can conflict with
+ * the SBCL spaces have already been done at this point).
+ *
+ * Since randomization is currently implemented only on x86 kernels,
+ * don't do this trick on other platforms.
+ */
+#ifdef LISP_FEATURE_X86
+ if ((major_version == 2
+ /* Some old kernels will apparently lose unsupported personality flags
+ * on exec() */
+ && ((minor_version == 6 && patch_version >= 11)
+ || (minor_version > 6)))
+ || major_version >= 3)
+ {
+ int pers = personality(0xffffffffUL);
+ /* 0x40000 aka. ADDR_NO_RANDOMIZE */
+ if (!(pers & 0x40000)) {
+ int retval = personality(pers | 0x40000);
+ /* Allegedly some Linux kernels (the reported case was
+ * "hardened Linux 2.6.7") won't set the new personality,
+ * but nor will they return -1 for an error. So as a
+ * workaround query the new personality...
+ */
+ int newpers = personality(0xffffffffUL);
+ /* ... and don't re-execute if either the setting resulted
+ * in an error or if the value didn't change. Otherwise
+ * this might result in an infinite loop.
+ */
+ if (retval != -1 && newpers != pers) {
+ /* Use /proc/self/exe instead of trying to figure out
+ * the executable path from PATH and argv[0], since
+ * that's unreliable. We follow the symlink instead of
+ * executing the file directly in order to prevent top
+ * from displaying the name of the process as "exe". */
+ char runtime[PATH_MAX+1];
+ int i = readlink("/proc/self/exe", runtime, PATH_MAX);
+ if (i != -1) {
+ runtime[i] = '\0';
+ execve(runtime, argv, envp);
+ }
+ }
+ /* Either changing the personality or execve() failed. Either
+ * way we might as well continue, and hope that the random
+ * memory maps are ok this time around.
+ */
+ fprintf(stderr, "WARNING: Couldn't re-execute SBCL with the proper personality flags (maybe /proc isn't mounted?). Trying to continue anyway.\n");
+ }