Skip to content

Commit a74740f

Browse files
committed
Provide a way to control SysV shmem attach address in EXEC_BACKEND builds.
In standard non-Windows builds, there's no particular reason to care what address the kernel chooses to map the shared memory segment at. However, when building with EXEC_BACKEND, there's a risk that the chosen address won't be available in all child processes. Linux with ASLR enabled (which it is by default) seems particularly at risk because it puts shmem segments into the same area where it maps shared libraries. We can work around that by specifying a mapping address that's outside the range where shared libraries could get mapped. On x86_64 Linux, 0x7e0000000000 seems to work well. This is only meant for testing/debugging purposes, so it doesn't seem necessary to go as far as providing a GUC (or any user-visible documentation, though we might change that later). Instead, it's just controlled by setting an environment variable PG_SHMEM_ADDR to the desired attach address. Back-patch to all supported branches, since the point here is to remove intermittent buildfarm failures on EXEC_BACKEND animals. Owners of affected animals will need to add a suitable setting of PG_SHMEM_ADDR to their build_env configuration. Discussion: https://postgr.es/m/7036.1492231361@sss.pgh.pa.us
1 parent bfba563 commit a74740f

File tree

1 file changed

+23
-2
lines changed

1 file changed

+23
-2
lines changed

src/backend/port/sysv_shmem.c

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,28 @@ static void *
102102
InternalIpcMemoryCreate(IpcMemoryKey memKey, Size size)
103103
{
104104
IpcMemoryId shmid;
105+
void *requestedAddress = NULL;
105106
void *memAddress;
106107

108+
/*
109+
* Normally we just pass requestedAddress = NULL to shmat(), allowing the
110+
* system to choose where the segment gets mapped. But in an EXEC_BACKEND
111+
* build, it's possible for whatever is chosen in the postmaster to not
112+
* work for backends, due to variations in address space layout. As a
113+
* rather klugy workaround, allow the user to specify the address to use
114+
* via setting the environment variable PG_SHMEM_ADDR. (If this were of
115+
* interest for anything except debugging, we'd probably create a cleaner
116+
* and better-documented way to set it, such as a GUC.)
117+
*/
118+
#ifdef EXEC_BACKEND
119+
{
120+
char *pg_shmem_addr = getenv("PG_SHMEM_ADDR");
121+
122+
if (pg_shmem_addr)
123+
requestedAddress = (void *) strtoul(pg_shmem_addr, NULL, 0);
124+
}
125+
#endif
126+
107127
shmid = shmget(memKey, size, IPC_CREAT | IPC_EXCL | IPCProtection);
108128

109129
if (shmid < 0)
@@ -203,10 +223,11 @@ InternalIpcMemoryCreate(IpcMemoryKey memKey, Size size)
203223
on_shmem_exit(IpcMemoryDelete, Int32GetDatum(shmid));
204224

205225
/* OK, should be able to attach to the segment */
206-
memAddress = shmat(shmid, NULL, PG_SHMAT_FLAGS);
226+
memAddress = shmat(shmid, requestedAddress, PG_SHMAT_FLAGS);
207227

208228
if (memAddress == (void *) -1)
209-
elog(FATAL, "shmat(id=%d) failed: %m", shmid);
229+
elog(FATAL, "shmat(id=%d, addr=%p, flags=0x%x) failed: %m",
230+
shmid, requestedAddress, PG_SHMAT_FLAGS);
210231

211232
/* Register on-exit routine to detach new segment before deleting */
212233
on_shmem_exit(IpcMemoryDetach, PointerGetDatum(memAddress));

0 commit comments

Comments
 (0)