From a2cfc411be2571cf92facdfbe3cd00f0364e5826 Mon Sep 17 00:00:00 2001
From: Thomas Munro <thomas.munro@gmail.com>
Date: Fri, 13 Aug 2021 09:27:57 +1200
Subject: [PATCH] Make EXEC_BACKEND more convenient on macOS.

It's hard to disable ASLR on current macOS releases, when testing with
-DEXEC_BACKEND.  Previously you could set the environment variable
PG_SHMEM_ADDR to something not likely to collide with other segments
that are mapped early in process startup.  Let's also provide a default
value that works on current releases and architectures, for developer
convenience.  It became a lot easier to pick a value when Apple killed
off 32 bit support.

As noted in the pre-existing comment, this is a questionable hack, but
-DEXEC_BACKEND is only used by Unix developers for testing some
otherwise Windows-only code paths, so this seems excusable.

Discussion: https://postgr.es/m/20210806032944.m4tz7j2w47mant26%40alap3.anarazel.de
---
 src/backend/port/sysv_shmem.c | 10 ++++++++++
 1 file changed, 10 insertions(+)

diff --git a/src/backend/port/sysv_shmem.c b/src/backend/port/sysv_shmem.c
index 0cc83ffc16..8ecf9d2ab5 100644
--- a/src/backend/port/sysv_shmem.c
+++ b/src/backend/port/sysv_shmem.c
@@ -143,6 +143,16 @@ InternalIpcMemoryCreate(IpcMemoryKey memKey, Size size)
 
 		if (pg_shmem_addr)
 			requestedAddress = (void *) strtoul(pg_shmem_addr, NULL, 0);
+#if defined(__darwin__) && SIZEOF_VOID_P == 8
+		else
+		{
+			/*
+			 * Provide a default value that is known to work on current macOS
+			 * releases.
+			 */
+			requestedAddress = (void *) 0x80000000000;
+		}
+#endif
 	}
 #endif
 
-- 
2.30.1 (Apple Git-130)

