POSIX Shared Memory and the Cross-Process Pointer Pitfall

How shm_open and mmap map physical RAM across isolated address spaces, why pointers break across processes, and how to design position-independent shared structures in C.

Mapping Physical RAM Across Isolated Processes

When two processes communicate over a pipe or socket, every message traverses the kernel twice. POSIX shared memory bypasses the kernel on the data path entirely: once two processes map the same shared memory object into their respective virtual address spaces, they read and write the exact same physical RAM frames directly from user space.

Yet shared memory introduces a subtle trap that catches almost every systems programmer at least once: code that works flawlessly with pointers in a multi-threaded program will immediately segfault when moved to shared memory. Let’s examine how the POSIX shared memory bridge works under the hood, why virtual addresses break across process boundaries, and how to design position-independent shared data structures in modern C.


Setting Up the Shared Memory Bridge

Establishing a shared memory region in POSIX requires three system calls: shm_open, ftruncate, and mmap.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#define _POSIX_C_SOURCE 200809L

#include <fcntl.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/mman.h>
#include <unistd.h>

constexpr size_t RING_CAPACITY = 16;

typedef struct {
    uint64_t write_idx;
    uint64_t read_idx;
    int32_t  slots[RING_CAPACITY];
} SharedRing;

SharedRing *shm_create(const char *name) {
    int fd = shm_open(name, O_CREAT | O_RDWR, 0600);
    if (fd == -1) {
        perror("shm_open");
        return nullptr;
    }

    if (ftruncate(fd, sizeof(SharedRing)) == -1) {
        perror("ftruncate");
        close(fd);
        return nullptr;
    }

    void *addr = mmap(nullptr, sizeof(SharedRing),
                      PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
    close(fd); // Descriptor is no longer needed once mmap succeeds

    if (addr == MAP_FAILED) {
        perror("mmap");
        return nullptr;
    }
    return (SharedRing *)addr;
}

Each step fulfills a distinct contract with the operating system kernel:

  1. shm_open: Creates or opens a POSIX shared memory object backed by a RAM-resident filesystem (tmpfs at /dev/shm on Linux) and returns a file descriptor fd.
  2. ftruncate: A newly created shared memory object has a length of zero bytes. Mapping a zero-length object and dereferencing the resulting pointer raises a hardware bus error (SIGBUS) because no backing physical pages exist. ftruncate resizes the object to sizeof(SharedRing).
  3. mmap with MAP_SHARED: Instructs the kernel to wire the object’s physical frames into the calling process’s page tables. Note that we can immediately close(fd) after mmap returnsβ€”the virtual memory mapping holds its own kernel reference to the underlying backing object.

Why Pointers Break Across Process Boundaries

Look at SharedRing above: int32_t slots[RING_CAPACITY] is embedded directly inside the struct as a fixed-size array. Suppose instead that a developer wanted a dynamically sized buffer and wrote:

1
2
3
4
5
// BROKEN DESIGN: Do not store raw pointers inside shared memory!
typedef struct {
    size_t   capacity;
    int32_t *slots; // Raw virtual address!
} BrokenSharedRing;

If Process A creates the shared segment and initializes the buffer with ring->slots = malloc(cap * sizeof(int32_t)), Process A can read and write ring->slots[i] without issue. The instant Process B attaches to the shared memory and evaluates ring->slots[0], Process B crashes with SIGSEGV.

Two Separate Mechanisms Cause This Failure

1. Heap Allocations Are Process-Private

When Process A calls malloc(), the memory allocator (brk or anonymous mmap) allocates pages inside Process A’s private heap, not inside /dev/shm. Writing the pointer into ring->slots only copies an 8-byte integerβ€”the virtual address 0x55a100402010β€”into shared memory. In Process B’s page tables, virtual address 0x55a100402010 either is unmapped (SIGSEGV) or points to unrelated private data inside Process B.

2. Even Pointers Inside the Shared Region Change Address (ASLR)

What if Process A doesn’t use malloc(), and instead points ring->slots to another address inside the shared memory region itself?

It still breaks! When Process A and Process B call mmap(nullptr, ...), the kernel is free to place the shared mapping at different virtual base addresses in each process (especially under Address Space Layout Randomization, ASLR):

1
2
3
4
5
Process A Virtual Space          Physical RAM           Process B Virtual Space
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”      β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”      β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ Base: 0x7f8e42c00000 β”œβ”€β”€β”€β”€β”€β–Ίβ”‚  Shared Header │◄────── Base: 0x7f1100a00000 β”‚
β”‚ Offset +0x10: Data   β”œβ”€β”€β”€β”€β”€β–Ίβ”‚  Shared Array  │◄────── Offset +0x10: Data   β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜      β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜      β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

In Process A, the array inside the shared region lives at 0x7f8e42c00010. In Process B, that exact same physical memory lives at 0x7f1100a00010. If Process A writes the absolute pointer 0x7f8e42c00010 into the shared header, Process B will dereference 0x7f8e42c00010β€”an unmapped address in Process B!


Designing Position-Independent Shared Structures

To make a data structure safe across processes, every reference inside the shared region must be position-independent: its meaning cannot depend on the virtual base address returned by mmap. Systems programmers use three idiomatic patterns in C to achieve this.

Pattern 1: Embedded Fixed Arrays (Compile-Time Sizing)

When the maximum capacity is known at compile time, embed the array directly inside the struct (as in SharedRing above). When either process accesses ring->slots[i], the compiler emits an address calculation relative to that process’s own local ring pointer:

$$\text{Address} = \text{local_base} + \texttt{offsetof(SharedRing, slots)} + i \times \texttt{sizeof(int32_t)}.$$

Pattern 2: C99 Flexible Array Members (Runtime Sizing)

What if the capacity $N$ is only known at runtime when the producer starts? Use a Flexible Array Member (int32_t slots[];) as the final field of the struct, and size the shm_open segment dynamically:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
typedef struct {
    size_t   capacity;
    uint64_t write_idx;
    uint64_t read_idx;
    int32_t  slots[]; // C99 Flexible Array Member (offset is constant)
} DynamicSharedRing;

DynamicSharedRing *shm_create_dynamic(const char *name, size_t capacity) {
    size_t total_bytes = sizeof(DynamicSharedRing) + capacity * sizeof(int32_t);

    int fd = shm_open(name, O_CREAT | O_RDWR, 0600);
    if (fd == -1) return nullptr;

    if (ftruncate(fd, (off_t)total_bytes) == -1) {
        close(fd);
        return nullptr;
    }

    DynamicSharedRing *ring = mmap(nullptr, total_bytes,
                                   PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
    close(fd);
    if (ring == MAP_FAILED) return nullptr;

    ring->capacity  = capacity;
    ring->write_idx = 0;
    ring->read_idx  = 0;
    return ring;
}

A consumer attaching later simply maps the first sizeof(DynamicSharedRing) bytes to read ring->capacity (or checks fstat(fd, &st) to get st.st_size), and maps the full regionβ€”with zero pointers stored in shared memory.

Pattern 3: Relative Offset Pointers (Linked Structures & Arenas)

What if you are building a dynamic linked list, B-tree, or slab allocator inside shared memory? Instead of storing an absolute virtual address (Node *next), store a byte offset (ptrdiff_t or uint64_t) relative to either the base of the shared memory segment or the address of the pointer field itself (“self-relative pointer”):

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
#include <stddef.h>
#include <stdint.h>

typedef struct {
    ptrdiff_t offset; // 0 represents nullptr; otherwise relative to &offset
} RelPtr;

static inline void relptr_set(RelPtr *rp, const void *target) {
    rp->offset = target ? ((const char *)target - (const char *)rp) : 0;
}

static inline void *relptr_get(const RelPtr *rp) {
    return rp->offset ? (void *)((const char *)rp + rp->offset) : nullptr;
}

Because the distance in bytes between two locations inside the same contiguous mmap region is identical in every process, (const char *)rp + rp->offset always resolves to the exact local virtual address in whichever process evaluates it.