| 1 |
|
| /* |
| 2 |
|
| * pool.h -- Fixed-size shared-memory object pool for Linux |
| 3 |
|
| * |
| 4 |
|
| * Bitmap-based slot allocation with CAS (lock-free). |
| 5 |
|
| * Futex-based blocking when pool is exhausted. |
| 6 |
|
| * PID-based stale slot recovery. |
| 7 |
|
| * |
| 8 |
|
| * Variants: |
| 9 |
|
| * Raw -- opaque byte slots of arbitrary elem_size |
| 10 |
|
| * I64 -- int64_t slots with atomic CAS/add |
| 11 |
|
| * F64 -- double slots |
| 12 |
|
| * I32 -- int32_t slots with atomic CAS/add |
| 13 |
|
| * Str -- fixed-length string slots (4-byte length prefix + data) |
| 14 |
|
| */ |
| 15 |
|
| |
| 16 |
|
| #ifndef POOL_H |
| 17 |
|
| #define POOL_H |
| 18 |
|
| |
| 19 |
|
| #include <stddef.h> |
| 20 |
|
| #include <stdint.h> |
| 21 |
|
| #include <stdbool.h> |
| 22 |
|
| #include <stdlib.h> |
| 23 |
|
| #include <string.h> |
| 24 |
|
| #include <unistd.h> |
| 25 |
|
| #include <fcntl.h> |
| 26 |
|
| #include <errno.h> |
| 27 |
|
| #include <time.h> |
| 28 |
|
| #include <limits.h> |
| 29 |
|
| #include <signal.h> |
| 30 |
|
| #include <sys/mman.h> |
| 31 |
|
| #include <sys/stat.h> |
| 32 |
|
| #include <sys/file.h> |
| 33 |
|
| #include <sys/syscall.h> |
| 34 |
|
| #include <linux/futex.h> |
| 35 |
|
| #include <sys/eventfd.h> |
| 36 |
|
| |
| 37 |
|
| #ifndef POOL_WAIT_INTERRUPTED |
| 38 |
|
| #define POOL_WAIT_INTERRUPTED() 0 |
| 39 |
|
| #endif |
| 40 |
|
| |
| 41 |
|
| /* ================================================================ |
| 42 |
|
| * Constants |
| 43 |
|
| * ================================================================ */ |
| 44 |
|
| |
| 45 |
|
| #define POOL_MAGIC 0x504F4C31U /* "POL1" */ |
| 46 |
|
| #define POOL_VERSION 2 /* 2: slots are claimed through the owner word, not the bitmap */ |
| 47 |
|
| |
| 48 |
|
| /* Set in an owner word while recover_stale frees the slot of a dead owner. */ |
| 49 |
|
| #define POOL_OWNER_RECOVERING 0x80000000U |
| 50 |
|
| #define POOL_ERR_BUFLEN (PATH_MAX + 256) |
| 51 |
|
| |
| 52 |
|
| #define POOL_VAR_RAW 0 |
| 53 |
|
| #define POOL_VAR_I64 1 |
| 54 |
|
| #define POOL_VAR_F64 2 |
| 55 |
|
| #define POOL_VAR_I32 3 |
| 56 |
|
| #define POOL_VAR_STR 4 |
| 57 |
|
| |
| 58 |
|
| #define POOL_ALIGN8(x) (((x) + 7) & ~(uint64_t)7) |
| 59 |
|
| |
| 60 |
|
| /* ================================================================ |
| 61 |
|
| * Header (128 bytes = 2 cache lines) |
| 62 |
|
| * ================================================================ */ |
| 63 |
|
| |
| 64 |
|
| typedef struct { |
| 65 |
|
| /* ---- Cache line 0 (0-63): immutable after create ---- */ |
| 66 |
|
| uint32_t magic; /* 0 */ |
| 67 |
|
| uint32_t version; /* 4 */ |
| 68 |
|
| uint32_t elem_size; /* 8 */ |
| 69 |
|
| uint32_t variant_id; /* 12 */ |
| 70 |
|
| uint64_t capacity; /* 16: number of slots */ |
| 71 |
|
| uint64_t total_size; /* 24: total mmap size */ |
| 72 |
|
| uint64_t data_off; /* 32: offset to slot data */ |
| 73 |
|
| uint64_t bitmap_off; /* 40: offset to allocation bitmap */ |
| 74 |
|
| uint64_t owners_off; /* 48: offset to per-slot owner PIDs */ |
| 75 |
|
| uint8_t _pad0[8]; /* 56-63 */ |
| 76 |
|
| |
| 77 |
|
| /* ---- Cache line 1 (64-127): mutable state ---- */ |
| 78 |
|
| uint32_t used; /* 64: allocated count (futex word) */ |
| 79 |
|
| uint32_t waiters; /* 68: blocked on alloc */ |
| 80 |
|
| uint8_t _pad1[8]; /* 72-79 */ |
| 81 |
|
| uint64_t stat_allocs; /* 80 */ |
| 82 |
|
| uint64_t stat_frees; /* 88 */ |
| 83 |
|
| uint64_t stat_waits; /* 96 */ |
| 84 |
|
| uint64_t stat_timeouts; /* 104 */ |
| 85 |
|
| uint64_t stat_recoveries;/* 112 */ |
| 86 |
|
| uint8_t _pad2[8]; /* 120-127 */ |
| 87 |
|
| } PoolHeader; |
| 88 |
|
| |
| 89 |
|
| #if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L |
| 90 |
|
| _Static_assert(sizeof(PoolHeader) == 128, "PoolHeader must be 128 bytes"); |
| 91 |
|
| #endif |
| 92 |
|
| |
| 93 |
|
| /* ================================================================ |
| 94 |
|
| * Process-local handle |
| 95 |
|
| * ================================================================ */ |
| 96 |
|
| |
| 97 |
|
| typedef struct { |
| 98 |
|
| PoolHeader *hdr; |
| 99 |
|
| uint64_t *bitmap; |
| 100 |
|
| uint32_t *owners; |
| 101 |
|
| uint8_t *data; |
| 102 |
|
| size_t mmap_size; |
| 103 |
|
| uint32_t bitmap_words; |
| 104 |
|
| uint64_t capacity; /* cached-at-attach geometry (peer may corrupt hdr->capacity) */ |
| 105 |
|
| uint32_t elem_size; /* cached-at-attach geometry (peer may corrupt hdr->elem_size) */ |
| 106 |
|
| char *path; |
| 107 |
|
| int notify_fd; |
| 108 |
|
| int backing_fd; |
| 109 |
|
| uint32_t scan_hint; |
| 110 |
|
| } PoolHandle; |
| 111 |
|
| |
| 112 |
|
| /* ================================================================ |
| 113 |
|
| * Utility |
| 114 |
|
| * ================================================================ */ |
| 115 |
|
| |
| 116 |
|
| /* A zombie (dead but not yet reaped) still answers kill(pid,0) as alive, so a |
| 117 |
|
| * process that crashed while holding the lock and lingers unreaped would never |
| 118 |
|
| * be recovered. Treat /proc/<pid>/stat state 'Z' as dead. Linux-only (as is |
| 119 |
|
| * this module); if /proc is unreadable we fall back to "alive" (safe: we never |
| 120 |
|
| * force-recover a possibly-live holder). */ |
| 121 |
1 |
| static inline int pool_pid_is_zombie(uint32_t pid) { |
| 122 |
|
| char path[32], buf[256]; |
| 123 |
1 |
| snprintf(path, sizeof(path), "/proc/%u/stat", (unsigned)pid); |
| 124 |
1 |
| int fd = open(path, O_RDONLY | O_CLOEXEC); |
| 125 |
1 |
▶ | if (fd < 0) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (fd < 0) return 0; |
0 |
1 |
1 |
|
| 126 |
1 |
| ssize_t n = read(fd, buf, sizeof(buf) - 1); |
| 127 |
1 |
| close(fd); |
| 128 |
1 |
▶ | if (n <= 0) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (n <= 0) return 0; |
0 |
1 |
1 |
|
| 129 |
1 |
| buf[n] = '\0'; |
| 130 |
|
| /* "pid (comm) state ..."; comm may contain ')', so scan to the last one. */ |
| 131 |
1 |
| char *rp = strrchr(buf, ')'); |
| 132 |
1 |
▶ | if (!rp || rp + 2 >= buf + n) return 0; /* need ") X" within the bytes read */ |
Branch: 2 branches
| Branch | True | False | Total |
| if (!rp || rp + 2 >= buf + n) return 0; /* need ") X" within the bytes read */ |
1 |
0 |
1 |
| if (!rp || rp + 2 >= buf + n) return 0; /* need ") X" within the bytes read */ |
0 |
1 |
1 |
|
| 133 |
1 |
▶ | return rp[1] == ' ' && rp[2] == 'Z'; |
Branch: 2 branches
| Branch | True | False | Total |
| return rp[1] == ' ' && rp[2] == 'Z'; |
1 |
0 |
1 |
| return rp[1] == ' ' && rp[2] == 'Z'; |
0 |
1 |
1 |
|
| 134 |
|
| } |
| 135 |
22 |
| static inline int pool_pid_alive(uint32_t pid) { |
| 136 |
22 |
▶ | if (pid == 0) return 0; /* no owner recorded, treat as dead */ |
Branch: 1 branch
| Branch | True | False | Total |
| if (pid == 0) return 0; /* no owner recorded, treat as dead */ |
0 |
22 |
22 |
|
| 137 |
22 |
▶ | if (kill((pid_t)pid, 0) == -1 && errno == ESRCH) return 0; /* definitely dead */ |
Branch: 2 branches
| Branch | True | False | Total |
| if (kill((pid_t)pid, 0) == -1 && errno == ESRCH) return 0; /* definitely dead */ |
21 |
1 |
22 |
| if (kill((pid_t)pid, 0) == -1 && errno == ESRCH) return 0; /* definitely dead */ |
21 |
0 |
21 |
|
| 138 |
1 |
| return !pool_pid_is_zombie(pid); /* kill() also succeeds for a zombie -> treat as dead */ |
| 139 |
|
| } |
| 140 |
|
| |
| 141 |
6 |
| static inline void pool_make_deadline(double timeout, struct timespec *deadline) { |
| 142 |
6 |
| clock_gettime(CLOCK_MONOTONIC, deadline); |
| 143 |
6 |
▶ | if (!(timeout < 1e9)) timeout = 1e9; /* clamp Inf/NaN/huge: avoid UB (time_t) cast -> instant spurious timeout */ |
Branch: 1 branch
| Branch | True | False | Total |
| if (!(timeout < 1e9)) timeout = 1e9; /* clamp Inf/NaN/huge: avoid UB (time_t) cast -> instant spurious timeout */ |
0 |
6 |
6 |
|
| 144 |
6 |
| deadline->tv_sec += (time_t)timeout; |
| 145 |
6 |
| deadline->tv_nsec += (long)((timeout - (double)(time_t)timeout) * 1e9); |
| 146 |
6 |
▶ | if (deadline->tv_nsec >= 1000000000L) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (deadline->tv_nsec >= 1000000000L) { |
2 |
4 |
6 |
|
| 147 |
2 |
| deadline->tv_sec++; |
| 148 |
2 |
| deadline->tv_nsec -= 1000000000L; |
| 149 |
|
| } |
| 150 |
6 |
| } |
| 151 |
|
| |
| 152 |
5 |
| static inline int pool_remaining_time(const struct timespec *deadline, |
| 153 |
|
| struct timespec *remaining) { |
| 154 |
|
| struct timespec now; |
| 155 |
5 |
| clock_gettime(CLOCK_MONOTONIC, &now); |
| 156 |
5 |
| remaining->tv_sec = deadline->tv_sec - now.tv_sec; |
| 157 |
5 |
| remaining->tv_nsec = deadline->tv_nsec - now.tv_nsec; |
| 158 |
5 |
▶ | if (remaining->tv_nsec < 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (remaining->tv_nsec < 0) { |
3 |
2 |
5 |
|
| 159 |
3 |
| remaining->tv_sec--; |
| 160 |
3 |
| remaining->tv_nsec += 1000000000L; |
| 161 |
|
| } |
| 162 |
5 |
| return remaining->tv_sec >= 0; |
| 163 |
|
| } |
| 164 |
|
| |
| 165 |
|
| /* ================================================================ |
| 166 |
|
| * Slot access |
| 167 |
|
| * ================================================================ */ |
| 168 |
|
| |
| 169 |
1808 |
| static inline uint8_t *pool_slot_ptr(PoolHandle *h, uint64_t slot) { |
| 170 |
1808 |
| return h->data + slot * h->elem_size; |
| 171 |
|
| } |
| 172 |
|
| |
| 173 |
829 |
| static inline int pool_is_allocated(PoolHandle *h, uint64_t slot) { |
| 174 |
829 |
| uint32_t widx = (uint32_t)(slot / 64); |
| 175 |
829 |
| int bit = (int)(slot % 64); |
| 176 |
829 |
| uint64_t word = __atomic_load_n(&h->bitmap[widx], __ATOMIC_RELAXED); |
| 177 |
829 |
| return (word >> bit) & 1; |
| 178 |
|
| } |
| 179 |
|
| |
| 180 |
|
| /* ================================================================ |
| 181 |
|
| * Allocation (lock-free bitmap scan + CAS) |
| 182 |
|
| * ================================================================ */ |
| 183 |
|
| |
| 184 |
1017 |
| static inline int64_t pool_try_alloc(PoolHandle *h) { |
| 185 |
1017 |
| uint32_t nwords = h->bitmap_words; |
| 186 |
1017 |
| uint64_t cap = h->capacity; |
| 187 |
1017 |
| uint32_t start = h->scan_hint; |
| 188 |
1017 |
| uint32_t mypid = (uint32_t)getpid(); |
| 189 |
|
| |
| 190 |
1047 |
▶ | for (uint32_t i = 0; i < nwords; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (uint32_t i = 0; i < nwords; i++) { |
1028 |
19 |
1047 |
|
| 191 |
1028 |
| uint32_t widx = (start + i) % nwords; |
| 192 |
1028 |
| uint64_t word = __atomic_load_n(&h->bitmap[widx], __ATOMIC_RELAXED); |
| 193 |
|
| |
| 194 |
|
| /* The owner word is the claim (0 -> pid); the bitmap bit follows it, so a |
| 195 |
|
| * claimer killed in between leaves a dead owner that recover_stale frees. |
| 196 |
|
| * `used` counts the claim before the bit: a kill can leave it high, never low. */ |
| 197 |
1028 |
▶ | while (word != ~(uint64_t)0) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (word != ~(uint64_t)0) { |
1017 |
11 |
1028 |
|
| 198 |
1017 |
| int bit = __builtin_ctzll(~word); |
| 199 |
1017 |
| uint64_t slot = (uint64_t)widx * 64 + bit; |
| 200 |
1017 |
▶ | if (slot >= cap) break; |
Branch: 1 branch
| Branch | True | False | Total |
| if (slot >= cap) break; |
19 |
998 |
1017 |
|
| 201 |
|
| |
| 202 |
998 |
| uint32_t unowned = 0; |
| 203 |
998 |
▶ | if (__atomic_compare_exchange_n(&h->owners[slot], &unowned, mypid, |
Branch: 1 branch
| Branch | True | False | Total |
| if (__atomic_compare_exchange_n(&h->owners[slot], &unowned, mypid, |
998 |
0 |
998 |
|
| 204 |
|
| 0, __ATOMIC_ACQUIRE, __ATOMIC_RELAXED)) { |
| 205 |
998 |
| __atomic_add_fetch(&h->hdr->used, 1, __ATOMIC_RELEASE); |
| 206 |
998 |
| memset(pool_slot_ptr(h, slot), 0, h->elem_size); |
| 207 |
998 |
| uint64_t new_word = __atomic_or_fetch(&h->bitmap[widx], |
| 208 |
|
| (uint64_t)1 << bit, __ATOMIC_RELEASE); |
| 209 |
998 |
| __atomic_add_fetch(&h->hdr->stat_allocs, 1, __ATOMIC_RELAXED); |
| 210 |
|
| /* Advance hint past full word to reduce next scan */ |
| 211 |
11 |
▶ | h->scan_hint = (new_word == ~(uint64_t)0 && nwords > 1) |
Branch: 1 branch
| Branch | True | False | Total |
| h->scan_hint = (new_word == ~(uint64_t)0 && nwords > 1) |
10 |
1 |
11 |
|
| 212 |
1009 |
▶ | ? (widx + 1) % nwords : widx; |
Branch: 1 branch
| Branch | True | False | Total |
| ? (widx + 1) % nwords : widx; |
11 |
987 |
998 |
|
| 213 |
998 |
| return (int64_t)slot; |
| 214 |
|
| } |
| 215 |
|
| /* Owned but not yet (or no longer) in the bitmap: skip it this scan. */ |
| 216 |
0 |
| word |= (uint64_t)1 << bit; |
| 217 |
|
| } |
| 218 |
|
| } |
| 219 |
19 |
| return -1; |
| 220 |
|
| } |
| 221 |
|
| |
| 222 |
|
| /* Pin the per-handle allocation scan to start at the given slot's word. Pass 0 for |
| 223 |
|
| * deterministic, sequential (low-to-high) allocation order, overriding the |
| 224 |
|
| * getpid()-derived spread start. The spread only reduces CAS contention when many |
| 225 |
|
| * processes allocate concurrently; a single allocator wants reproducible order, and |
| 226 |
|
| * the bitmap stays the source of truth either way. An out-of-range slot wraps. */ |
| 227 |
8 |
| static inline void pool_scan_from(PoolHandle *h, uint64_t slot) { |
| 228 |
8 |
| h->scan_hint = (uint32_t)((slot / 64) % h->bitmap_words); |
| 229 |
8 |
| } |
| 230 |
|
| |
| 231 |
|
| /* Blocking alloc. timeout<0 = infinite, 0 = non-blocking, >0 = seconds. */ |
| 232 |
969 |
| static inline int64_t pool_alloc(PoolHandle *h, double timeout) { |
| 233 |
969 |
| int64_t slot = pool_try_alloc(h); |
| 234 |
969 |
▶ | if (slot >= 0) return slot; |
Branch: 1 branch
| Branch | True | False | Total |
| if (slot >= 0) return slot; |
964 |
5 |
969 |
|
| 235 |
5 |
▶ | if (timeout == 0) return -1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (timeout == 0) return -1; |
2 |
3 |
5 |
|
| 236 |
|
| |
| 237 |
3 |
| PoolHeader *hdr = h->hdr; |
| 238 |
|
| struct timespec deadline, remaining; |
| 239 |
3 |
| int has_deadline = (timeout > 0); |
| 240 |
3 |
▶ | if (has_deadline) pool_make_deadline(timeout, &deadline); |
Branch: 1 branch
| Branch | True | False | Total |
| if (has_deadline) pool_make_deadline(timeout, &deadline); |
3 |
0 |
3 |
|
| 241 |
|
| |
| 242 |
3 |
| __atomic_add_fetch(&hdr->stat_waits, 1, __ATOMIC_RELAXED); |
| 243 |
|
| |
| 244 |
0 |
| for (;;) { |
| 245 |
3 |
▶ | if (POOL_WAIT_INTERRUPTED()) { errno = EINTR; return -2; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (POOL_WAIT_INTERRUPTED()) { errno = EINTR; return -2; } |
0 |
3 |
3 |
|
| 246 |
3 |
| uint32_t cur_used = __atomic_load_n(&hdr->used, __ATOMIC_RELAXED); |
| 247 |
3 |
▶ | if ((uint64_t)cur_used < hdr->capacity) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ((uint64_t)cur_used < hdr->capacity) { |
0 |
3 |
3 |
|
| 248 |
0 |
| slot = pool_try_alloc(h); |
| 249 |
0 |
▶ | if (slot >= 0) return slot; |
Branch: 1 branch
| Branch | True | False | Total |
| if (slot >= 0) return slot; |
0 |
0 |
0 |
|
| 250 |
|
| } |
| 251 |
|
| |
| 252 |
3 |
| __atomic_add_fetch(&hdr->waiters, 1, __ATOMIC_RELEASE); |
| 253 |
|
| /* StoreLoad barrier: we must publish waiters++ before re-reading |
| 254 |
|
| * `used`, so a concurrent releaser either sees our waiters++ (and |
| 255 |
|
| * wakes us) or we see its used-- here (and don't sleep). Release/ |
| 256 |
|
| * acquire does not order a store before a later load to a different |
| 257 |
|
| * address; without this fence both sides can miss each other on a |
| 258 |
|
| * weakly-ordered CPU -> lost wakeup -> hang. */ |
| 259 |
3 |
| __atomic_thread_fence(__ATOMIC_SEQ_CST); |
| 260 |
|
| |
| 261 |
3 |
| cur_used = __atomic_load_n(&hdr->used, __ATOMIC_ACQUIRE); |
| 262 |
3 |
▶ | if ((uint64_t)cur_used >= hdr->capacity) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ((uint64_t)cur_used >= hdr->capacity) { |
3 |
0 |
3 |
|
| 263 |
|
| /* Rescan at least every second: a recover_stale killed mid-slot can leave |
| 264 |
|
| * `used` one high, so a full-looking pool may hold a free slot. */ |
| 265 |
3 |
| struct timespec recheck = { 1, 0 }, *pts = &recheck; |
| 266 |
3 |
▶ | if (has_deadline) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (has_deadline) { |
3 |
0 |
3 |
|
| 267 |
3 |
▶ | if (!pool_remaining_time(&deadline, &remaining)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!pool_remaining_time(&deadline, &remaining)) { |
0 |
3 |
3 |
|
| 268 |
0 |
| __atomic_sub_fetch(&hdr->waiters, 1, __ATOMIC_RELAXED); |
| 269 |
0 |
| __atomic_add_fetch(&hdr->stat_timeouts, 1, __ATOMIC_RELAXED); |
| 270 |
0 |
| return -1; |
| 271 |
|
| } |
| 272 |
3 |
▶ | if (remaining.tv_sec < 1) pts = &remaining; |
Branch: 1 branch
| Branch | True | False | Total |
| if (remaining.tv_sec < 1) pts = &remaining; |
2 |
1 |
3 |
|
| 273 |
|
| } |
| 274 |
3 |
| syscall(SYS_futex, &hdr->used, FUTEX_WAIT, cur_used, pts, NULL, 0); |
| 275 |
|
| } |
| 276 |
|
| |
| 277 |
3 |
| __atomic_sub_fetch(&hdr->waiters, 1, __ATOMIC_RELAXED); |
| 278 |
3 |
▶ | if (POOL_WAIT_INTERRUPTED()) { errno = EINTR; return -2; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (POOL_WAIT_INTERRUPTED()) { errno = EINTR; return -2; } |
1 |
2 |
3 |
|
| 279 |
|
| |
| 280 |
2 |
| slot = pool_try_alloc(h); |
| 281 |
2 |
▶ | if (slot >= 0) return slot; |
Branch: 1 branch
| Branch | True | False | Total |
| if (slot >= 0) return slot; |
1 |
1 |
2 |
|
| 282 |
|
| |
| 283 |
1 |
▶ | if (has_deadline) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (has_deadline) { |
1 |
0 |
1 |
|
| 284 |
1 |
▶ | if (!pool_remaining_time(&deadline, &remaining)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!pool_remaining_time(&deadline, &remaining)) { |
1 |
0 |
1 |
|
| 285 |
1 |
| __atomic_add_fetch(&hdr->stat_timeouts, 1, __ATOMIC_RELAXED); |
| 286 |
1 |
| return -1; |
| 287 |
|
| } |
| 288 |
|
| } |
| 289 |
|
| } |
| 290 |
|
| } |
| 291 |
|
| |
| 292 |
|
| /* ================================================================ |
| 293 |
|
| * Free |
| 294 |
|
| * ================================================================ */ |
| 295 |
|
| |
| 296 |
|
| /* Clear an allocated slot's bitmap bit, then give up its owner word; a freer |
| 297 |
|
| * killed in between leaves an owner that recover_stale reclaims once it is |
| 298 |
|
| * dead. An owner mid-recovery is left to recover_stale. Returns 1 if this |
| 299 |
|
| * call cleared the bit, 0 if the slot was not allocated. */ |
| 300 |
767 |
| static inline int pool_release_slot(PoolHandle *h, uint64_t slot) { |
| 301 |
767 |
| uint32_t widx = (uint32_t)(slot / 64); |
| 302 |
767 |
| uint64_t mask = (uint64_t)1 << (slot % 64); |
| 303 |
767 |
| uint32_t owner = __atomic_load_n(&h->owners[slot], __ATOMIC_ACQUIRE); |
| 304 |
767 |
| uint64_t prev = __atomic_fetch_and(&h->bitmap[widx], ~mask, __ATOMIC_RELEASE); |
| 305 |
767 |
▶ | if (!(prev & mask)) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!(prev & mask)) return 0; |
2 |
765 |
767 |
|
| 306 |
765 |
▶ | if (!(owner & POOL_OWNER_RECOVERING)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!(owner & POOL_OWNER_RECOVERING)) |
765 |
0 |
765 |
|
| 307 |
765 |
| __atomic_compare_exchange_n(&h->owners[slot], &owner, 0, |
| 308 |
|
| 0, __ATOMIC_RELEASE, __ATOMIC_RELAXED); |
| 309 |
765 |
| return 1; |
| 310 |
|
| } |
| 311 |
|
| |
| 312 |
754 |
| static inline int pool_free_slot(PoolHandle *h, uint64_t slot) { |
| 313 |
754 |
| PoolHeader *hdr = h->hdr; |
| 314 |
754 |
▶ | if (slot >= h->capacity) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (slot >= h->capacity) return 0; |
0 |
754 |
754 |
|
| 315 |
754 |
▶ | if (!pool_release_slot(h, slot)) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!pool_release_slot(h, slot)) return 0; |
2 |
752 |
754 |
|
| 316 |
752 |
| __atomic_sub_fetch(&hdr->used, 1, __ATOMIC_RELEASE); |
| 317 |
752 |
| __atomic_add_fetch(&hdr->stat_frees, 1, __ATOMIC_RELAXED); |
| 318 |
|
| /* StoreLoad barrier: publish used-- before reading waiters, so we |
| 319 |
|
| * observe a waiter that registered just before our free (else the |
| 320 |
|
| * wake is lost and it sleeps until timeout). Pairs with the fence |
| 321 |
|
| * in the alloc-wait path. */ |
| 322 |
752 |
| __atomic_thread_fence(__ATOMIC_SEQ_CST); |
| 323 |
752 |
▶ | if (__atomic_load_n(&hdr->waiters, __ATOMIC_RELAXED) > 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (__atomic_load_n(&hdr->waiters, __ATOMIC_RELAXED) > 0) |
0 |
752 |
752 |
|
| 324 |
0 |
| syscall(SYS_futex, &hdr->used, FUTEX_WAKE, 1, NULL, NULL, 0); |
| 325 |
752 |
| return 1; |
| 326 |
|
| } |
| 327 |
|
| |
| 328 |
|
| /* ================================================================ |
| 329 |
|
| * Batch free -- single used decrement + single futex wake |
| 330 |
|
| * ================================================================ */ |
| 331 |
|
| |
| 332 |
7 |
| static inline uint32_t pool_free_n(PoolHandle *h, uint64_t *slots, uint32_t count) { |
| 333 |
7 |
| uint32_t freed = 0; |
| 334 |
7 |
| PoolHeader *hdr = h->hdr; |
| 335 |
|
| |
| 336 |
20 |
▶ | for (uint32_t i = 0; i < count; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (uint32_t i = 0; i < count; i++) { |
13 |
7 |
20 |
|
| 337 |
13 |
| uint64_t slot = slots[i]; |
| 338 |
13 |
▶ | if (slot >= h->capacity) continue; |
Branch: 1 branch
| Branch | True | False | Total |
| if (slot >= h->capacity) continue; |
0 |
13 |
13 |
|
| 339 |
13 |
▶ | if (pool_release_slot(h, slot)) freed++; |
Branch: 1 branch
| Branch | True | False | Total |
| if (pool_release_slot(h, slot)) freed++; |
13 |
0 |
13 |
|
| 340 |
|
| } |
| 341 |
|
| |
| 342 |
7 |
▶ | if (freed > 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (freed > 0) { |
7 |
0 |
7 |
|
| 343 |
7 |
| __atomic_sub_fetch(&hdr->used, freed, __ATOMIC_RELEASE); |
| 344 |
7 |
| __atomic_add_fetch(&hdr->stat_frees, freed, __ATOMIC_RELAXED); |
| 345 |
|
| /* StoreLoad barrier: see pool_free_slot. */ |
| 346 |
7 |
| __atomic_thread_fence(__ATOMIC_SEQ_CST); |
| 347 |
7 |
▶ | if (__atomic_load_n(&hdr->waiters, __ATOMIC_RELAXED) > 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (__atomic_load_n(&hdr->waiters, __ATOMIC_RELAXED) > 0) { |
0 |
7 |
7 |
|
| 348 |
0 |
▶ | int wake = freed < (uint32_t)INT_MAX ? (int)freed : INT_MAX; |
Branch: 1 branch
| Branch | True | False | Total |
| int wake = freed < (uint32_t)INT_MAX ? (int)freed : INT_MAX; |
0 |
0 |
0 |
|
| 349 |
0 |
| syscall(SYS_futex, &hdr->used, FUTEX_WAKE, wake, NULL, NULL, 0); |
| 350 |
|
| } |
| 351 |
|
| } |
| 352 |
7 |
| return freed; |
| 353 |
|
| } |
| 354 |
|
| |
| 355 |
|
| /* ================================================================ |
| 356 |
|
| * Batch alloc -- all-or-nothing, shared deadline |
| 357 |
|
| * ================================================================ */ |
| 358 |
|
| |
| 359 |
6 |
| static inline int pool_alloc_n(PoolHandle *h, uint64_t *out, uint32_t count, |
| 360 |
|
| double timeout) { |
| 361 |
6 |
▶ | if (count == 0) return 1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (count == 0) return 1; |
0 |
6 |
6 |
|
| 362 |
|
| |
| 363 |
6 |
▶ | if (timeout == 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (timeout == 0) { |
1 |
5 |
6 |
|
| 364 |
2 |
▶ | for (uint32_t i = 0; i < count; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (uint32_t i = 0; i < count; i++) { |
2 |
0 |
2 |
|
| 365 |
2 |
| int64_t slot = pool_try_alloc(h); |
| 366 |
2 |
▶ | if (slot < 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (slot < 0) { |
1 |
1 |
2 |
|
| 367 |
1 |
▶ | if (i > 0) pool_free_n(h, out, i); |
Branch: 1 branch
| Branch | True | False | Total |
| if (i > 0) pool_free_n(h, out, i); |
1 |
0 |
1 |
|
| 368 |
1 |
| return 0; |
| 369 |
|
| } |
| 370 |
1 |
| out[i] = (uint64_t)slot; |
| 371 |
|
| } |
| 372 |
0 |
| return 1; |
| 373 |
|
| } |
| 374 |
|
| |
| 375 |
|
| /* count > capacity can never be satisfied: fail fast rather than grab the |
| 376 |
|
| * whole pool and block forever. */ |
| 377 |
5 |
▶ | if ((uint64_t)count > h->hdr->capacity) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if ((uint64_t)count > h->hdr->capacity) return 0; |
0 |
5 |
5 |
|
| 378 |
|
| |
| 379 |
5 |
| PoolHeader *hdr = h->hdr; |
| 380 |
|
| struct timespec deadline, remaining; |
| 381 |
5 |
| int has_deadline = (timeout > 0); |
| 382 |
5 |
▶ | if (has_deadline) pool_make_deadline(timeout, &deadline); |
Branch: 1 branch
| Branch | True | False | Total |
| if (has_deadline) pool_make_deadline(timeout, &deadline); |
0 |
5 |
5 |
|
| 383 |
|
| |
| 384 |
|
| /* All-or-nothing with retry, releasing any partial claim BEFORE waiting: |
| 385 |
|
| * holding it across the wait let two over-demanding alloc_n callers deadlock. */ |
| 386 |
5 |
| uint32_t backoff = 0; |
| 387 |
0 |
| for (;;) { |
| 388 |
5 |
| uint32_t got = 0; |
| 389 |
14 |
▶ | for (; got < count; got++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (; got < count; got++) { |
9 |
5 |
14 |
|
| 390 |
9 |
| int64_t s = pool_try_alloc(h); |
| 391 |
9 |
▶ | if (s < 0) break; |
Branch: 1 branch
| Branch | True | False | Total |
| if (s < 0) break; |
0 |
9 |
9 |
|
| 392 |
9 |
| out[got] = (uint64_t)s; |
| 393 |
|
| } |
| 394 |
5 |
▶ | if (got == count) return 1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (got == count) return 1; |
5 |
0 |
5 |
|
| 395 |
0 |
▶ | if (got > 0) pool_free_n(h, out, got); /* don't hold across the wait */ |
Branch: 1 branch
| Branch | True | False | Total |
| if (got > 0) pool_free_n(h, out, got); /* don't hold across the wait */ |
0 |
0 |
0 |
|
| 396 |
0 |
▶ | if (POOL_WAIT_INTERRUPTED()) { errno = EINTR; return -1; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (POOL_WAIT_INTERRUPTED()) { errno = EINTR; return -1; } |
0 |
0 |
0 |
|
| 397 |
|
| |
| 398 |
0 |
▶ | if (has_deadline && !pool_remaining_time(&deadline, &remaining)) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (has_deadline && !pool_remaining_time(&deadline, &remaining)) { |
0 |
0 |
0 |
| if (has_deadline && !pool_remaining_time(&deadline, &remaining)) { |
0 |
0 |
0 |
|
| 399 |
0 |
| __atomic_add_fetch(&hdr->stat_timeouts, 1, __ATOMIC_RELAXED); |
| 400 |
0 |
| return 0; |
| 401 |
|
| } |
| 402 |
|
| /* Jittered (pid-seeded) backoff desyncs competing alloc_n callers, |
| 403 |
|
| * breaking the symmetric grab/release livelock. */ |
| 404 |
|
| { |
| 405 |
0 |
| long us = (long)(((getpid() & 0x7u) + backoff) % 40u) * 25 + 25; /* 25..1000 us */ |
| 406 |
0 |
▶ | if (backoff < 40) backoff++; |
Branch: 1 branch
| Branch | True | False | Total |
| if (backoff < 40) backoff++; |
0 |
0 |
0 |
|
| 407 |
0 |
| struct timespec bt = { 0, us * 1000L }; |
| 408 |
0 |
| nanosleep(&bt, NULL); |
| 409 |
|
| } |
| 410 |
|
| } |
| 411 |
|
| } |
| 412 |
|
| |
| 413 |
|
| /* capacity - used, never wrapping: `used` can run one high (see pool_alloc) */ |
| 414 |
9 |
| static inline uint64_t pool_available(PoolHandle *h) { |
| 415 |
9 |
| uint64_t used = __atomic_load_n(&h->hdr->used, __ATOMIC_RELAXED); |
| 416 |
9 |
▶ | return used >= h->hdr->capacity ? 0 : h->hdr->capacity - used; |
Branch: 1 branch
| Branch | True | False | Total |
| return used >= h->hdr->capacity ? 0 : h->hdr->capacity - used; |
5 |
4 |
9 |
|
| 417 |
|
| } |
| 418 |
|
| |
| 419 |
|
| /* ================================================================ |
| 420 |
|
| * Stale recovery |
| 421 |
|
| * ================================================================ */ |
| 422 |
|
| |
| 423 |
|
| /* Free every slot whose owner is dead, whatever its bitmap bit says: a dead |
| 424 |
|
| * claimer may have died before setting the bit, a dead freer after clearing |
| 425 |
|
| * it. A live owner with a clear bit is a claim in progress and is skipped. |
| 426 |
|
| * The owner word is taken over (dead pid -> our pid|RECOVERING) before the bit |
| 427 |
|
| * is cleared, so no claimer can reuse the slot until we give it up. */ |
| 428 |
3 |
| static inline uint32_t pool_recover_stale(PoolHandle *h) { |
| 429 |
3 |
| uint32_t recovered = 0; |
| 430 |
3 |
| uint64_t cap = h->capacity; |
| 431 |
3 |
| uint32_t me = (uint32_t)getpid() | POOL_OWNER_RECOVERING; |
| 432 |
|
| |
| 433 |
267 |
▶ | for (uint64_t slot = 0; slot < cap; slot++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (uint64_t slot = 0; slot < cap; slot++) { |
264 |
3 |
267 |
|
| 434 |
264 |
| uint32_t owner = __atomic_load_n(&h->owners[slot], __ATOMIC_ACQUIRE); |
| 435 |
264 |
▶ | if (owner == 0 || pool_pid_alive(owner & ~POOL_OWNER_RECOVERING)) continue; |
Branch: 2 branches
| Branch | True | False | Total |
| if (owner == 0 || pool_pid_alive(owner & ~POOL_OWNER_RECOVERING)) continue; |
22 |
242 |
264 |
| if (owner == 0 || pool_pid_alive(owner & ~POOL_OWNER_RECOVERING)) continue; |
1 |
21 |
22 |
|
| 436 |
21 |
▶ | if (!__atomic_compare_exchange_n(&h->owners[slot], &owner, me, |
Branch: 1 branch
| Branch | True | False | Total |
| if (!__atomic_compare_exchange_n(&h->owners[slot], &owner, me, |
0 |
21 |
21 |
|
| 437 |
|
| 0, __ATOMIC_ACQ_REL, __ATOMIC_RELAXED)) |
| 438 |
0 |
| continue; |
| 439 |
21 |
| uint32_t widx = (uint32_t)(slot / 64); |
| 440 |
21 |
| uint64_t mask = (uint64_t)1 << (slot % 64); |
| 441 |
21 |
| uint64_t prev = __atomic_fetch_and(&h->bitmap[widx], ~mask, __ATOMIC_ACQ_REL); |
| 442 |
21 |
▶ | if (prev & mask) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (prev & mask) { |
21 |
0 |
21 |
|
| 443 |
|
| /* Saturating: a corrupted count must not wrap. */ |
| 444 |
21 |
| uint32_t cur = __atomic_load_n(&h->hdr->used, __ATOMIC_RELAXED); |
| 445 |
21 |
▶ | while (cur > 0 && !__atomic_compare_exchange_n(&h->hdr->used, |
Branch: 1 branch
| Branch | True | False | Total |
| while (cur > 0 && !__atomic_compare_exchange_n(&h->hdr->used, |
21 |
0 |
21 |
|
| 446 |
21 |
▶ | &cur, cur - 1, 1, __ATOMIC_RELEASE, __ATOMIC_RELAXED)) |
Branch: 1 branch
| Branch | True | False | Total |
| &cur, cur - 1, 1, __ATOMIC_RELEASE, __ATOMIC_RELAXED)) |
0 |
21 |
21 |
|
| 447 |
|
| ; |
| 448 |
21 |
| __atomic_add_fetch(&h->hdr->stat_frees, 1, __ATOMIC_RELAXED); |
| 449 |
|
| } |
| 450 |
21 |
| __atomic_store_n(&h->owners[slot], 0, __ATOMIC_RELEASE); |
| 451 |
|
| /* StoreLoad barrier: see pool_free_slot. */ |
| 452 |
21 |
| __atomic_thread_fence(__ATOMIC_SEQ_CST); |
| 453 |
21 |
▶ | if (__atomic_load_n(&h->hdr->waiters, __ATOMIC_RELAXED) > 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (__atomic_load_n(&h->hdr->waiters, __ATOMIC_RELAXED) > 0) |
0 |
21 |
21 |
|
| 454 |
0 |
| syscall(SYS_futex, &h->hdr->used, FUTEX_WAKE, 1, NULL, NULL, 0); |
| 455 |
21 |
| recovered++; |
| 456 |
|
| } |
| 457 |
|
| |
| 458 |
3 |
▶ | if (recovered > 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (recovered > 0) |
2 |
1 |
3 |
|
| 459 |
2 |
| __atomic_add_fetch(&h->hdr->stat_recoveries, recovered, __ATOMIC_RELAXED); |
| 460 |
|
| |
| 461 |
3 |
| return recovered; |
| 462 |
|
| } |
| 463 |
|
| |
| 464 |
|
| /* ================================================================ |
| 465 |
|
| * Layout calculation |
| 466 |
|
| * ================================================================ */ |
| 467 |
|
| |
| 468 |
86 |
| static inline void pool_calc_layout(uint64_t capacity, uint32_t elem_size, |
| 469 |
|
| uint64_t *bitmap_off, uint64_t *owners_off, |
| 470 |
|
| uint64_t *data_off, uint64_t *total_size) { |
| 471 |
86 |
| uint64_t bwords = (capacity + 63) / 64; |
| 472 |
86 |
| uint64_t bitmap_sz = bwords * 8; |
| 473 |
86 |
| uint64_t owners_sz = POOL_ALIGN8(capacity * 4); |
| 474 |
86 |
| uint64_t data_sz = (uint64_t)capacity * elem_size; |
| 475 |
|
| |
| 476 |
86 |
| *bitmap_off = sizeof(PoolHeader); |
| 477 |
86 |
| *owners_off = *bitmap_off + bitmap_sz; |
| 478 |
86 |
| *data_off = *owners_off + owners_sz; |
| 479 |
86 |
| *total_size = *data_off + data_sz; |
| 480 |
86 |
| } |
| 481 |
|
| |
| 482 |
|
| /* ================================================================ |
| 483 |
|
| * Header initialization (shared by pool_create and pool_create_memfd) |
| 484 |
|
| * ================================================================ */ |
| 485 |
|
| |
| 486 |
72 |
| static inline void pool_init_header(void *base, uint64_t total, |
| 487 |
|
| uint32_t elem_size, uint32_t variant_id, |
| 488 |
|
| uint64_t capacity, uint64_t bm_off, |
| 489 |
|
| uint64_t own_off, uint64_t dat_off) { |
| 490 |
72 |
| PoolHeader *hdr = (PoolHeader *)base; |
| 491 |
72 |
| memset(base, 0, (size_t)total); |
| 492 |
72 |
| hdr->version = POOL_VERSION; |
| 493 |
72 |
| hdr->elem_size = elem_size; |
| 494 |
72 |
| hdr->variant_id = variant_id; |
| 495 |
72 |
| hdr->capacity = capacity; |
| 496 |
72 |
| hdr->total_size = total; |
| 497 |
72 |
| hdr->data_off = dat_off; |
| 498 |
72 |
| hdr->bitmap_off = bm_off; |
| 499 |
72 |
| hdr->owners_off = own_off; |
| 500 |
72 |
| __atomic_thread_fence(__ATOMIC_SEQ_CST); |
| 501 |
|
| /* Publish magic LAST, as a release store: it is the commit point, so a |
| 502 |
|
| creator killed before it leaves magic==0 and never a file mistaken for |
| 503 |
|
| a valid one. A kill during the field stores leaves one to remove by |
| 504 |
|
| hand. */ |
| 505 |
72 |
| __atomic_store_n(&hdr->magic, POOL_MAGIC, __ATOMIC_RELEASE); |
| 506 |
|
| |
| 507 |
72 |
| } |
| 508 |
|
| |
| 509 |
|
| /* ================================================================ |
| 510 |
|
| * Create / Open / Close |
| 511 |
|
| * ================================================================ */ |
| 512 |
|
| |
| 513 |
|
| #define POOL_ERR(fmt, ...) do { if (errbuf) snprintf(errbuf, POOL_ERR_BUFLEN, fmt, ##__VA_ARGS__); } while(0) |
| 514 |
|
| |
| 515 |
|
| /* Max capacity to prevent bitmap_words (uint32_t) truncation */ |
| 516 |
|
| #define POOL_MAX_CAPACITY ((uint64_t)UINT32_MAX * 64) |
| 517 |
|
| |
| 518 |
|
| /* Fixed element width for the typed variants; 0 for RAW/STR (caller-chosen). |
| 519 |
|
| * A crafted file could pair a typed variant_id with a wrong elem_size and drive |
| 520 |
|
| * the typed accessors past a slot or into a misaligned atomic; pin it here. */ |
| 521 |
8 |
| static inline uint32_t pool_variant_elem_size(uint32_t variant_id) { |
| 522 |
8 |
| switch (variant_id) { |
| 523 |
3 |
| case POOL_VAR_I64: case POOL_VAR_F64: return 8; |
| 524 |
1 |
| case POOL_VAR_I32: return 4; |
| 525 |
4 |
| default: return 0; |
| 526 |
|
| } |
| 527 |
|
| } |
| 528 |
|
| |
| 529 |
|
| /* Validate header: magic, version, variant, sizes, AND layout offsets. */ |
| 530 |
12 |
| static inline int pool_validate_header(const PoolHeader *hdr, uint64_t file_size, |
| 531 |
|
| uint32_t expected_variant) { |
| 532 |
12 |
▶ | if (hdr->magic != POOL_MAGIC) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (hdr->magic != POOL_MAGIC) return 0; |
4 |
8 |
12 |
|
| 533 |
8 |
▶ | if (hdr->version != POOL_VERSION) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (hdr->version != POOL_VERSION) return 0; |
0 |
8 |
8 |
|
| 534 |
8 |
▶ | if (hdr->variant_id != expected_variant) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (hdr->variant_id != expected_variant) return 0; |
0 |
8 |
8 |
|
| 535 |
8 |
| { uint32_t w = pool_variant_elem_size(hdr->variant_id); |
| 536 |
8 |
▶ | if (w && hdr->elem_size != w) return 0; } /* typed variant: elem_size must match its fixed width */ |
Branch: 2 branches
| Branch | True | False | Total |
| if (w && hdr->elem_size != w) return 0; } /* typed variant: elem_size must match its fixed width */ |
4 |
4 |
8 |
| if (w && hdr->elem_size != w) return 0; } /* typed variant: elem_size must match its fixed width */ |
0 |
4 |
4 |
|
| 537 |
8 |
▶ | if (hdr->capacity == 0 || hdr->capacity > POOL_MAX_CAPACITY) return 0; |
Branch: 2 branches
| Branch | True | False | Total |
| if (hdr->capacity == 0 || hdr->capacity > POOL_MAX_CAPACITY) return 0; |
8 |
0 |
8 |
| if (hdr->capacity == 0 || hdr->capacity > POOL_MAX_CAPACITY) return 0; |
0 |
8 |
8 |
|
| 538 |
8 |
▶ | if (hdr->elem_size == 0) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (hdr->elem_size == 0) return 0; |
0 |
8 |
8 |
|
| 539 |
|
| /* Full-layout overflow check, identical to the one the create paths apply: |
| 540 |
|
| * total = header + bitmap + owners + data. Counting only sizeof(PoolHeader) |
| 541 |
|
| * here was weaker than at creation, so a crafted header could wrap |
| 542 |
|
| * total_size mod 2^64, satisfy the equality checks below, and leave the |
| 543 |
|
| * derived slot pointers far outside the mapping. |
| 544 |
|
| * capacity <= POOL_MAX_CAPACITY (checked above) bounds the bitmap/owners |
| 545 |
|
| * products, so computing the overhead cannot itself overflow. */ |
| 546 |
8 |
| { uint64_t overhead = sizeof(PoolHeader) |
| 547 |
8 |
| + ((hdr->capacity + 63) / 64) * 8 |
| 548 |
8 |
| + POOL_ALIGN8(hdr->capacity * 4); |
| 549 |
8 |
▶ | if (hdr->capacity > (UINT64_MAX - overhead) / hdr->elem_size) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (hdr->capacity > (UINT64_MAX - overhead) / hdr->elem_size) return 0; |
0 |
8 |
8 |
|
| 550 |
|
| } |
| 551 |
8 |
▶ | if (hdr->total_size != file_size) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (hdr->total_size != file_size) return 0; |
0 |
8 |
8 |
|
| 552 |
|
| |
| 553 |
|
| uint64_t bm_off, own_off, dat_off, total; |
| 554 |
8 |
| pool_calc_layout(hdr->capacity, hdr->elem_size, &bm_off, &own_off, &dat_off, &total); |
| 555 |
|
| |
| 556 |
8 |
▶ | if (hdr->bitmap_off != bm_off) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (hdr->bitmap_off != bm_off) return 0; |
0 |
8 |
8 |
|
| 557 |
8 |
▶ | if (hdr->owners_off != own_off) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (hdr->owners_off != own_off) return 0; |
0 |
8 |
8 |
|
| 558 |
8 |
▶ | if (hdr->data_off != dat_off) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (hdr->data_off != dat_off) return 0; |
0 |
8 |
8 |
|
| 559 |
8 |
▶ | if (hdr->total_size != total) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (hdr->total_size != total) return 0; |
0 |
8 |
8 |
|
| 560 |
8 |
| return 1; |
| 561 |
|
| } |
| 562 |
|
| |
| 563 |
80 |
| static inline PoolHandle *pool_setup_handle(void *base, size_t map_size, |
| 564 |
|
| const char *path, int backing_fd) { |
| 565 |
80 |
| PoolHeader *hdr = (PoolHeader *)base; |
| 566 |
80 |
| PoolHandle *h = (PoolHandle *)calloc(1, sizeof(PoolHandle)); |
| 567 |
80 |
▶ | if (!h) { munmap(base, map_size); if (backing_fd >= 0) close(backing_fd); return NULL; } |
Branch: 2 branches
| Branch | True | False | Total |
| if (!h) { munmap(base, map_size); if (backing_fd >= 0) close(backing_fd); return NULL; } |
0 |
80 |
80 |
| if (!h) { munmap(base, map_size); if (backing_fd >= 0) close(backing_fd); return NULL; } |
0 |
0 |
0 |
|
| 568 |
|
| |
| 569 |
80 |
| h->hdr = hdr; |
| 570 |
80 |
| h->bitmap = (uint64_t *)((uint8_t *)base + hdr->bitmap_off); |
| 571 |
80 |
| h->owners = (uint32_t *)((uint8_t *)base + hdr->owners_off); |
| 572 |
80 |
| h->data = (uint8_t *)base + hdr->data_off; |
| 573 |
80 |
| h->mmap_size = map_size; |
| 574 |
80 |
| h->bitmap_words = (uint32_t)((hdr->capacity + 63) / 64); |
| 575 |
80 |
| h->capacity = hdr->capacity; /* validated at attach; use cached copy for all indexing/bounds */ |
| 576 |
80 |
| h->elem_size = hdr->elem_size; /* validated at attach; use cached copy for all slot addressing */ |
| 577 |
80 |
▶ | h->path = path ? strdup(path) : NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| h->path = path ? strdup(path) : NULL; |
16 |
64 |
80 |
|
| 578 |
80 |
| h->notify_fd = -1; |
| 579 |
80 |
| h->backing_fd = backing_fd; |
| 580 |
80 |
| h->scan_hint = (uint32_t)getpid() % h->bitmap_words; |
| 581 |
|
| |
| 582 |
80 |
| return h; |
| 583 |
|
| } |
| 584 |
|
| |
| 585 |
|
| /* Securely obtain a fd: create exclusively (O_CREAT|O_EXCL|O_NOFOLLOW at mode, |
| 586 |
|
| * default 0600), or attach an existing file (O_RDWR|O_NOFOLLOW, no O_CREAT). */ |
| 587 |
19 |
| static int pool_secure_open(const char *path, mode_t mode, char *errbuf) { |
| 588 |
19 |
▶ | for (int attempt = 0; attempt < 100; attempt++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (int attempt = 0; attempt < 100; attempt++) { |
19 |
0 |
19 |
|
| 589 |
19 |
| int fd = open(path, O_RDWR|O_CREAT|O_EXCL|O_NOFOLLOW|O_CLOEXEC, mode); |
| 590 |
19 |
▶ | if (fd >= 0) { (void)fchmod(fd, mode); return fd; } /* exact mode: umask narrowed the O_EXCL create */ |
Branch: 1 branch
| Branch | True | False | Total |
| if (fd >= 0) { (void)fchmod(fd, mode); return fd; } /* exact mode: umask narrowed the O_EXCL create */ |
12 |
7 |
19 |
|
| 591 |
7 |
▶ | if (errno != EEXIST) { POOL_ERR("create %s: %s", path, strerror(errno)); return -1; } |
Branch: 2 branches
| Branch | True | False | Total |
| if (errno != EEXIST) { POOL_ERR("create %s: %s", path, strerror(errno)); return -1; } |
0 |
7 |
7 |
| if (errno != EEXIST) { POOL_ERR("create %s: %s", path, strerror(errno)); return -1; } |
0 |
0 |
0 |
|
| 592 |
7 |
| fd = open(path, O_RDWR|O_NOFOLLOW|O_CLOEXEC); |
| 593 |
7 |
▶ | if (fd >= 0) return fd; |
Branch: 1 branch
| Branch | True | False | Total |
| if (fd >= 0) return fd; |
7 |
0 |
7 |
|
| 594 |
0 |
▶ | if (errno == ENOENT) continue; /* creator unlinked between our two opens; retry */ |
Branch: 1 branch
| Branch | True | False | Total |
| if (errno == ENOENT) continue; /* creator unlinked between our two opens; retry */ |
0 |
0 |
0 |
|
| 595 |
0 |
▶ | POOL_ERR("open %s: %s", path, strerror(errno)); /* ELOOP => symlink rejected */ |
Branch: 1 branch
| Branch | True | False | Total |
| POOL_ERR("open %s: %s", path, strerror(errno)); /* ELOOP => symlink rejected */ |
0 |
0 |
0 |
|
| 596 |
0 |
| return -1; |
| 597 |
|
| } |
| 598 |
0 |
▶ | POOL_ERR("open %s: create/attach kept racing", path); |
Branch: 1 branch
| Branch | True | False | Total |
| POOL_ERR("open %s: create/attach kept racing", path); |
0 |
0 |
0 |
|
| 599 |
0 |
| return -1; |
| 600 |
|
| } |
| 601 |
|
| |
| 602 |
|
| /* True iff the whole file is zero -- what an abandoned mid-init |
| 603 |
|
| creator leaves. Lets recovery re-init only a provably-empty file, never |
| 604 |
|
| one that merely starts with a zero word. Cold path, so a byte scan is |
| 605 |
|
| fine. Read, not mapped: a read fault on a tmpfs hole allocates the page. */ |
| 606 |
2 |
| static int pool_file_is_zero(int fd, uint64_t size) { |
| 607 |
|
| char buf[16384]; |
| 608 |
3 |
▶ | for (uint64_t off = 0; off < size; ) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (uint64_t off = 0; off < size; ) { |
2 |
1 |
3 |
|
| 609 |
2 |
| ssize_t n = pread(fd, buf, size - off < sizeof buf ? (size_t)(size - off) : sizeof buf, (off_t)off); |
| 610 |
2 |
▶ | if (n <= 0) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (n <= 0) return 0; |
0 |
2 |
2 |
|
| 611 |
993 |
▶ | for (ssize_t i = 0; i < n; i++) if (buf[i]) return 0; |
Branch: 2 branches
| Branch | True | False | Total |
| for (ssize_t i = 0; i < n; i++) if (buf[i]) return 0; |
1 |
991 |
992 |
| for (ssize_t i = 0; i < n; i++) if (buf[i]) return 0; |
992 |
1 |
993 |
|
| 612 |
1 |
| off += (uint64_t)n; |
| 613 |
|
| } |
| 614 |
1 |
| return 1; |
| 615 |
|
| } |
| 616 |
|
| |
| 617 |
|
| /* A sparse segment would SIGBUS at the first write the filesystem cannot back. */ |
| 618 |
19 |
| static int pool_reserve(int fd, uint64_t size) { |
| 619 |
19 |
| const char *sp = getenv("DATA_POOL_SHARED_SPARSE"); |
| 620 |
19 |
▶ | if (sp && *sp && strcmp(sp, "0")) return 0; |
Branch: 3 branches
| Branch | True | False | Total |
| if (sp && *sp && strcmp(sp, "0")) return 0; |
0 |
19 |
19 |
| if (sp && *sp && strcmp(sp, "0")) return 0; |
0 |
0 |
0 |
| if (sp && *sp && strcmp(sp, "0")) return 0; |
0 |
0 |
0 |
|
| 621 |
|
| /* Before Linux 6.11 tmpfs gives up at any pending signal and undoes the allocation, so a |
| 622 |
|
| * periodic one could keep it from ever completing; SIGSTOP cannot be blocked. */ |
| 623 |
|
| sigset_t all, old; |
| 624 |
19 |
| sigfillset(&all); |
| 625 |
19 |
| sigprocmask(SIG_BLOCK, &all, &old); |
| 626 |
|
| int e; |
| 627 |
19 |
▶ | do e = posix_fallocate(fd, 0, (off_t)size); while (e == EINTR); |
Branch: 1 branch
| Branch | True | False | Total |
| do e = posix_fallocate(fd, 0, (off_t)size); while (e == EINTR); |
0 |
19 |
19 |
|
| 628 |
19 |
| sigprocmask(SIG_SETMASK, &old, NULL); |
| 629 |
19 |
▶ | if (e == 0 || e == EOPNOTSUPP || e == EINVAL) return 0; |
Branch: 3 branches
| Branch | True | False | Total |
| if (e == 0 || e == EOPNOTSUPP || e == EINVAL) return 0; |
0 |
19 |
19 |
| if (e == 0 || e == EOPNOTSUPP || e == EINVAL) return 0; |
0 |
0 |
0 |
| if (e == 0 || e == EOPNOTSUPP || e == EINVAL) return 0; |
0 |
0 |
0 |
|
| 630 |
0 |
| errno = e; |
| 631 |
0 |
| return -1; |
| 632 |
|
| } |
| 633 |
|
| |
| 634 |
72 |
| static PoolHandle *pool_create(const char *path, uint64_t capacity, |
| 635 |
|
| uint32_t elem_size, uint32_t variant_id, |
| 636 |
|
| mode_t mode, char *errbuf) { |
| 637 |
72 |
▶ | if (errbuf) errbuf[0] = '\0'; |
Branch: 1 branch
| Branch | True | False | Total |
| if (errbuf) errbuf[0] = '\0'; |
72 |
0 |
72 |
|
| 638 |
|
| |
| 639 |
72 |
▶ | if (capacity == 0) { POOL_ERR("capacity must be > 0"); return NULL; } |
Branch: 2 branches
| Branch | True | False | Total |
| if (capacity == 0) { POOL_ERR("capacity must be > 0"); return NULL; } |
0 |
72 |
72 |
| if (capacity == 0) { POOL_ERR("capacity must be > 0"); return NULL; } |
0 |
0 |
0 |
|
| 640 |
72 |
▶ | if (elem_size == 0) { POOL_ERR("elem_size must be > 0"); return NULL; } |
Branch: 2 branches
| Branch | True | False | Total |
| if (elem_size == 0) { POOL_ERR("elem_size must be > 0"); return NULL; } |
0 |
72 |
72 |
| if (elem_size == 0) { POOL_ERR("elem_size must be > 0"); return NULL; } |
0 |
0 |
0 |
|
| 641 |
72 |
▶ | if (capacity > POOL_MAX_CAPACITY) { POOL_ERR("capacity too large"); return NULL; } |
Branch: 2 branches
| Branch | True | False | Total |
| if (capacity > POOL_MAX_CAPACITY) { POOL_ERR("capacity too large"); return NULL; } |
0 |
72 |
72 |
| if (capacity > POOL_MAX_CAPACITY) { POOL_ERR("capacity too large"); return NULL; } |
0 |
0 |
0 |
|
| 642 |
|
| { /* full-layout overflow check: total = header + bitmap + owners + data. |
| 643 |
|
| capacity <= POOL_MAX_CAPACITY bounds the bitmap/owners products. */ |
| 644 |
72 |
| uint64_t overhead = sizeof(PoolHeader) + ((capacity + 63) / 64) * 8 + POOL_ALIGN8(capacity * 4); |
| 645 |
72 |
▶ | if (capacity > (UINT64_MAX - overhead) / elem_size) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (capacity > (UINT64_MAX - overhead) / elem_size) { |
0 |
72 |
72 |
|
| 646 |
0 |
▶ | POOL_ERR("capacity * elem_size overflow"); return NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| POOL_ERR("capacity * elem_size overflow"); return NULL; |
0 |
0 |
0 |
|
| 647 |
|
| } |
| 648 |
|
| } |
| 649 |
|
| |
| 650 |
|
| uint64_t bm_off, own_off, dat_off, total; |
| 651 |
72 |
| pool_calc_layout(capacity, elem_size, &bm_off, &own_off, &dat_off, &total); |
| 652 |
|
| |
| 653 |
72 |
| int fd = -1; |
| 654 |
72 |
| int anonymous = (path == NULL); |
| 655 |
|
| size_t map_size; |
| 656 |
|
| void *base; |
| 657 |
|
| |
| 658 |
72 |
▶ | if (anonymous) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (anonymous) { |
53 |
19 |
72 |
|
| 659 |
53 |
| map_size = (size_t)total; |
| 660 |
53 |
| base = mmap(NULL, map_size, PROT_READ | PROT_WRITE, |
| 661 |
|
| MAP_SHARED | MAP_ANONYMOUS, -1, 0); |
| 662 |
53 |
▶ | if (base == MAP_FAILED) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (base == MAP_FAILED) { |
0 |
53 |
53 |
|
| 663 |
0 |
▶ | POOL_ERR("mmap(anonymous): %s", strerror(errno)); |
Branch: 1 branch
| Branch | True | False | Total |
| POOL_ERR("mmap(anonymous): %s", strerror(errno)); |
0 |
0 |
0 |
|
| 664 |
0 |
| return NULL; |
| 665 |
|
| } |
| 666 |
|
| } else { |
| 667 |
19 |
| fd = pool_secure_open(path, mode, errbuf); |
| 668 |
26 |
▶ | if (fd < 0) return NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (fd < 0) return NULL; |
0 |
19 |
19 |
|
| 669 |
|
| |
| 670 |
19 |
▶ | if (flock(fd, LOCK_EX) < 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (flock(fd, LOCK_EX) < 0) { |
0 |
19 |
19 |
|
| 671 |
0 |
▶ | POOL_ERR("flock(%s): %s", path, strerror(errno)); |
Branch: 1 branch
| Branch | True | False | Total |
| POOL_ERR("flock(%s): %s", path, strerror(errno)); |
0 |
0 |
0 |
|
| 672 |
0 |
| close(fd); return NULL; |
| 673 |
|
| } |
| 674 |
|
| |
| 675 |
|
| struct stat st; |
| 676 |
19 |
▶ | if (fstat(fd, &st) < 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (fstat(fd, &st) < 0) { |
0 |
19 |
19 |
|
| 677 |
0 |
▶ | POOL_ERR("fstat(%s): %s", path, strerror(errno)); |
Branch: 1 branch
| Branch | True | False | Total |
| POOL_ERR("fstat(%s): %s", path, strerror(errno)); |
0 |
0 |
0 |
|
| 678 |
0 |
| flock(fd, LOCK_UN); close(fd); return NULL; |
| 679 |
|
| } |
| 680 |
|
| |
| 681 |
19 |
| int is_new = (st.st_size == 0); |
| 682 |
|
| |
| 683 |
19 |
▶ | if (!is_new && (uint64_t)st.st_size < sizeof(PoolHeader)) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (!is_new && (uint64_t)st.st_size < sizeof(PoolHeader)) { |
7 |
12 |
19 |
| if (!is_new && (uint64_t)st.st_size < sizeof(PoolHeader)) { |
0 |
7 |
7 |
|
| 684 |
0 |
▶ | POOL_ERR("%s: file too small (%lld)", path, (long long)st.st_size); |
Branch: 1 branch
| Branch | True | False | Total |
| POOL_ERR("%s: file too small (%lld)", path, (long long)st.st_size); |
0 |
0 |
0 |
|
| 685 |
0 |
| flock(fd, LOCK_UN); close(fd); return NULL; |
| 686 |
|
| } |
| 687 |
|
| |
| 688 |
19 |
▶ | if (is_new && (st.st_uid != geteuid() || fchmod(fd, mode) < 0)) { |
Branch: 3 branches
| Branch | True | False | Total |
| if (is_new && (st.st_uid != geteuid() || fchmod(fd, mode) < 0)) { |
12 |
7 |
19 |
| if (is_new && (st.st_uid != geteuid() || fchmod(fd, mode) < 0)) { |
12 |
0 |
12 |
| if (is_new && (st.st_uid != geteuid() || fchmod(fd, mode) < 0)) { |
0 |
12 |
12 |
|
| 689 |
0 |
▶ | POOL_ERR("%s: refusing to initialize file not owned by us", path); |
Branch: 1 branch
| Branch | True | False | Total |
| POOL_ERR("%s: refusing to initialize file not owned by us", path); |
0 |
0 |
0 |
|
| 690 |
0 |
| flock(fd, LOCK_UN); close(fd); return NULL; |
| 691 |
|
| } |
| 692 |
19 |
▶ | if (is_new) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (is_new) { |
12 |
7 |
19 |
|
| 693 |
12 |
▶ | if (ftruncate(fd, (off_t)total) < 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (ftruncate(fd, (off_t)total) < 0) { |
0 |
12 |
12 |
|
| 694 |
0 |
▶ | POOL_ERR("ftruncate(%s): %s", path, strerror(errno)); |
Branch: 1 branch
| Branch | True | False | Total |
| POOL_ERR("ftruncate(%s): %s", path, strerror(errno)); |
0 |
0 |
0 |
|
| 695 |
0 |
| flock(fd, LOCK_UN); close(fd); return NULL; |
| 696 |
|
| } |
| 697 |
12 |
▶ | if (pool_reserve(fd, total) < 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (pool_reserve(fd, total) < 0) { |
0 |
12 |
12 |
|
| 698 |
0 |
▶ | POOL_ERR("%s: cannot reserve %llu bytes: %s", path, (unsigned long long)total, strerror(errno)); |
Branch: 1 branch
| Branch | True | False | Total |
| POOL_ERR("%s: cannot reserve %llu bytes: %s", path, (unsigned long long)total, strerror(errno)); |
0 |
0 |
0 |
|
| 699 |
0 |
| if (ftruncate(fd, 0) < 0) { /* best effort */ } |
| 700 |
0 |
| flock(fd, LOCK_UN); close(fd); return NULL; |
| 701 |
|
| } |
| 702 |
|
| } |
| 703 |
|
| |
| 704 |
19 |
▶ | map_size = is_new ? (size_t)total : (size_t)st.st_size; |
Branch: 1 branch
| Branch | True | False | Total |
| map_size = is_new ? (size_t)total : (size_t)st.st_size; |
7 |
12 |
19 |
|
| 705 |
19 |
| base = mmap(NULL, map_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); |
| 706 |
19 |
▶ | if (base == MAP_FAILED) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (base == MAP_FAILED) { |
0 |
19 |
19 |
|
| 707 |
0 |
▶ | POOL_ERR("mmap(%s): %s", path, strerror(errno)); |
Branch: 1 branch
| Branch | True | False | Total |
| POOL_ERR("mmap(%s): %s", path, strerror(errno)); |
0 |
0 |
0 |
|
| 708 |
0 |
▶ | if (is_new && ftruncate(fd, 0) < 0) { /* best effort */ } |
Branch: 1 branch
| Branch | True | False | Total |
| if (is_new && ftruncate(fd, 0) < 0) { /* best effort */ } |
0 |
0 |
0 |
|
| 709 |
0 |
| flock(fd, LOCK_UN); close(fd); return NULL; |
| 710 |
|
| } |
| 711 |
|
| |
| 712 |
19 |
▶ | if (!is_new) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!is_new) { |
7 |
12 |
19 |
|
| 713 |
7 |
▶ | if (!pool_validate_header((PoolHeader *)base, (uint64_t)st.st_size, variant_id)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!pool_validate_header((PoolHeader *)base, (uint64_t)st.st_size, variant_id)) { |
4 |
3 |
7 |
|
| 714 |
|
| /* Recover an abandoned mid-init file: a creator killed |
| 715 |
|
| * between the ftruncate and the header init leaves a |
| 716 |
|
| * full-size, all-zero (magic==0) file that would brick every |
| 717 |
|
| * future open of this path. Re-initialize ONLY when it is |
| 718 |
|
| * exactly our size, still uninitialized, and owned by us; |
| 719 |
|
| * anything else still errors. */ |
| 720 |
4 |
▶ | if (((PoolHeader *)base)->magic == 0 && (uint64_t)st.st_size == total |
Branch: 2 branches
| Branch | True | False | Total |
| if (((PoolHeader *)base)->magic == 0 && (uint64_t)st.st_size == total |
3 |
1 |
4 |
| if (((PoolHeader *)base)->magic == 0 && (uint64_t)st.st_size == total |
2 |
1 |
3 |
|
| 721 |
2 |
▶ | && st.st_uid == geteuid() && pool_file_is_zero(fd, map_size)) { |
Branch: 2 branches
| Branch | True | False | Total |
| && st.st_uid == geteuid() && pool_file_is_zero(fd, map_size)) { |
2 |
0 |
2 |
| && st.st_uid == geteuid() && pool_file_is_zero(fd, map_size)) { |
1 |
1 |
2 |
|
| 722 |
1 |
▶ | if (fchmod(fd, mode) < 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (fchmod(fd, mode) < 0) { |
0 |
1 |
1 |
|
| 723 |
0 |
▶ | POOL_ERR("%s: fchmod: %s", path, strerror(errno)); |
Branch: 1 branch
| Branch | True | False | Total |
| POOL_ERR("%s: fchmod: %s", path, strerror(errno)); |
0 |
0 |
0 |
|
| 724 |
0 |
| munmap(base, map_size); flock(fd, LOCK_UN); close(fd); return NULL; |
| 725 |
|
| } |
| 726 |
1 |
▶ | if (pool_reserve(fd, total) < 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (pool_reserve(fd, total) < 0) { |
0 |
1 |
1 |
|
| 727 |
0 |
▶ | POOL_ERR("%s: cannot reserve %llu bytes: %s", path, (unsigned long long)total, strerror(errno)); |
Branch: 1 branch
| Branch | True | False | Total |
| POOL_ERR("%s: cannot reserve %llu bytes: %s", path, (unsigned long long)total, strerror(errno)); |
0 |
0 |
0 |
|
| 728 |
0 |
| munmap(base, map_size); flock(fd, LOCK_UN); close(fd); return NULL; |
| 729 |
|
| } |
| 730 |
1 |
| pool_init_header(base, total, elem_size, variant_id, capacity, |
| 731 |
|
| bm_off, own_off, dat_off); |
| 732 |
1 |
| flock(fd, LOCK_UN); close(fd); |
| 733 |
1 |
| return pool_setup_handle(base, map_size, path, -1); |
| 734 |
|
| } |
| 735 |
3 |
▶ | if (((PoolHeader *)base)->magic == 0 && (uint64_t)st.st_size == total |
Branch: 2 branches
| Branch | True | False | Total |
| if (((PoolHeader *)base)->magic == 0 && (uint64_t)st.st_size == total |
2 |
1 |
3 |
| if (((PoolHeader *)base)->magic == 0 && (uint64_t)st.st_size == total |
1 |
1 |
2 |
|
| 736 |
1 |
▶ | && st.st_uid == geteuid()) |
Branch: 1 branch
| Branch | True | False | Total |
| && st.st_uid == geteuid()) |
1 |
0 |
1 |
|
| 737 |
1 |
▶ | POOL_ERR("%s: incomplete pool file left by an interrupted create; remove it and retry", path); |
Branch: 1 branch
| Branch | True | False | Total |
| POOL_ERR("%s: incomplete pool file left by an interrupted create; remove it and retry", path); |
1 |
0 |
1 |
|
| 738 |
2 |
▶ | else if (((PoolHeader *)base)->magic == POOL_MAGIC && ((PoolHeader *)base)->version != POOL_VERSION) |
Branch: 2 branches
| Branch | True | False | Total |
| else if (((PoolHeader *)base)->magic == POOL_MAGIC && ((PoolHeader *)base)->version != POOL_VERSION) |
0 |
2 |
2 |
| else if (((PoolHeader *)base)->magic == POOL_MAGIC && ((PoolHeader *)base)->version != POOL_VERSION) |
0 |
0 |
0 |
|
| 739 |
0 |
▶ | POOL_ERR("%s: pool file format version %u is not supported by this build (format %u); recreate it with this version", |
Branch: 1 branch
| Branch | True | False | Total |
| POOL_ERR("%s: pool file format version %u is not supported by this build (format %u); recreate it with this version", |
0 |
0 |
0 |
|
| 740 |
|
| path, ((PoolHeader *)base)->version, POOL_VERSION); |
| 741 |
|
| else |
| 742 |
2 |
▶ | POOL_ERR("%s: invalid or incompatible pool file", path); |
Branch: 1 branch
| Branch | True | False | Total |
| POOL_ERR("%s: invalid or incompatible pool file", path); |
2 |
0 |
2 |
|
| 743 |
3 |
| munmap(base, map_size); flock(fd, LOCK_UN); close(fd); return NULL; |
| 744 |
|
| } |
| 745 |
|
| /* Attaching an existing file must match the requested geometry, else |
| 746 |
|
| * a caller that grew capacity/elem_size in code but kept the old file |
| 747 |
|
| * would silently get the old (smaller) pool (shmget-style EINVAL). */ |
| 748 |
3 |
| PoolHeader *ehdr = (PoolHeader *)base; |
| 749 |
3 |
▶ | if (ehdr->capacity != capacity || ehdr->elem_size != elem_size) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (ehdr->capacity != capacity || ehdr->elem_size != elem_size) { |
3 |
0 |
3 |
| if (ehdr->capacity != capacity || ehdr->elem_size != elem_size) { |
0 |
3 |
3 |
|
| 750 |
0 |
▶ | POOL_ERR("%s: geometry mismatch (file capacity=%llu elem_size=%u; requested capacity=%llu elem_size=%u)", |
Branch: 1 branch
| Branch | True | False | Total |
| POOL_ERR("%s: geometry mismatch (file capacity=%llu elem_size=%u; requested capacity=%llu elem_size=%u)", |
0 |
0 |
0 |
|
| 751 |
|
| path, (unsigned long long)ehdr->capacity, ehdr->elem_size, |
| 752 |
|
| (unsigned long long)capacity, elem_size); |
| 753 |
0 |
| munmap(base, map_size); flock(fd, LOCK_UN); close(fd); return NULL; |
| 754 |
|
| } |
| 755 |
3 |
| flock(fd, LOCK_UN); |
| 756 |
3 |
| close(fd); |
| 757 |
3 |
| return pool_setup_handle(base, map_size, path, -1); |
| 758 |
|
| } |
| 759 |
|
| } |
| 760 |
|
| |
| 761 |
|
| /* Initialize header -- flock still held for file-backed new files */ |
| 762 |
65 |
| pool_init_header(base, total, elem_size, variant_id, capacity, |
| 763 |
|
| bm_off, own_off, dat_off); |
| 764 |
|
| |
| 765 |
65 |
▶ | if (fd >= 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (fd >= 0) { |
12 |
53 |
65 |
|
| 766 |
12 |
| flock(fd, LOCK_UN); |
| 767 |
12 |
| close(fd); |
| 768 |
|
| } |
| 769 |
|
| |
| 770 |
65 |
| return pool_setup_handle(base, map_size, path, -1); |
| 771 |
|
| } |
| 772 |
|
| |
| 773 |
6 |
| static PoolHandle *pool_create_memfd(const char *name, uint64_t capacity, |
| 774 |
|
| uint32_t elem_size, uint32_t variant_id, |
| 775 |
|
| char *errbuf) { |
| 776 |
6 |
▶ | if (errbuf) errbuf[0] = '\0'; |
Branch: 1 branch
| Branch | True | False | Total |
| if (errbuf) errbuf[0] = '\0'; |
6 |
0 |
6 |
|
| 777 |
|
| |
| 778 |
6 |
▶ | if (capacity == 0) { POOL_ERR("capacity must be > 0"); return NULL; } |
Branch: 2 branches
| Branch | True | False | Total |
| if (capacity == 0) { POOL_ERR("capacity must be > 0"); return NULL; } |
0 |
6 |
6 |
| if (capacity == 0) { POOL_ERR("capacity must be > 0"); return NULL; } |
0 |
0 |
0 |
|
| 779 |
6 |
▶ | if (elem_size == 0) { POOL_ERR("elem_size must be > 0"); return NULL; } |
Branch: 2 branches
| Branch | True | False | Total |
| if (elem_size == 0) { POOL_ERR("elem_size must be > 0"); return NULL; } |
0 |
6 |
6 |
| if (elem_size == 0) { POOL_ERR("elem_size must be > 0"); return NULL; } |
0 |
0 |
0 |
|
| 780 |
6 |
▶ | if (capacity > POOL_MAX_CAPACITY) { POOL_ERR("capacity too large"); return NULL; } |
Branch: 2 branches
| Branch | True | False | Total |
| if (capacity > POOL_MAX_CAPACITY) { POOL_ERR("capacity too large"); return NULL; } |
0 |
6 |
6 |
| if (capacity > POOL_MAX_CAPACITY) { POOL_ERR("capacity too large"); return NULL; } |
0 |
0 |
0 |
|
| 781 |
|
| { /* full-layout overflow check: total = header + bitmap + owners + data. |
| 782 |
|
| capacity <= POOL_MAX_CAPACITY bounds the bitmap/owners products. */ |
| 783 |
6 |
| uint64_t overhead = sizeof(PoolHeader) + ((capacity + 63) / 64) * 8 + POOL_ALIGN8(capacity * 4); |
| 784 |
6 |
▶ | if (capacity > (UINT64_MAX - overhead) / elem_size) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (capacity > (UINT64_MAX - overhead) / elem_size) { |
0 |
6 |
6 |
|
| 785 |
0 |
▶ | POOL_ERR("capacity * elem_size overflow"); return NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| POOL_ERR("capacity * elem_size overflow"); return NULL; |
0 |
0 |
0 |
|
| 786 |
|
| } |
| 787 |
|
| } |
| 788 |
|
| |
| 789 |
|
| uint64_t bm_off, own_off, dat_off, total; |
| 790 |
6 |
| pool_calc_layout(capacity, elem_size, &bm_off, &own_off, &dat_off, &total); |
| 791 |
|
| |
| 792 |
6 |
▶ | int fd = memfd_create(name ? name : "pool", MFD_CLOEXEC | MFD_ALLOW_SEALING); |
Branch: 1 branch
| Branch | True | False | Total |
| int fd = memfd_create(name ? name : "pool", MFD_CLOEXEC | MFD_ALLOW_SEALING); |
6 |
0 |
6 |
|
| 793 |
6 |
▶ | if (fd < 0) { POOL_ERR("memfd_create: %s", strerror(errno)); return NULL; } |
Branch: 2 branches
| Branch | True | False | Total |
| if (fd < 0) { POOL_ERR("memfd_create: %s", strerror(errno)); return NULL; } |
0 |
6 |
6 |
| if (fd < 0) { POOL_ERR("memfd_create: %s", strerror(errno)); return NULL; } |
0 |
0 |
0 |
|
| 794 |
|
| |
| 795 |
6 |
▶ | if (ftruncate(fd, (off_t)total) < 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (ftruncate(fd, (off_t)total) < 0) { |
0 |
6 |
6 |
|
| 796 |
0 |
▶ | POOL_ERR("ftruncate(memfd): %s", strerror(errno)); |
Branch: 1 branch
| Branch | True | False | Total |
| POOL_ERR("ftruncate(memfd): %s", strerror(errno)); |
0 |
0 |
0 |
|
| 797 |
0 |
| close(fd); return NULL; |
| 798 |
|
| } |
| 799 |
6 |
▶ | if (pool_reserve(fd, total) < 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (pool_reserve(fd, total) < 0) { |
0 |
6 |
6 |
|
| 800 |
0 |
▶ | POOL_ERR("memfd: cannot reserve %llu bytes: %s", (unsigned long long)total, strerror(errno)); |
Branch: 1 branch
| Branch | True | False | Total |
| POOL_ERR("memfd: cannot reserve %llu bytes: %s", (unsigned long long)total, strerror(errno)); |
0 |
0 |
0 |
|
| 801 |
0 |
| close(fd); return NULL; |
| 802 |
|
| } |
| 803 |
|
| |
| 804 |
|
| /* Seal against shrink/grow to block ftruncate-based SIGBUS attacks via |
| 805 |
|
| * SCM_RIGHTS-shared fds. Peers can still write; only size is immutable. */ |
| 806 |
6 |
| (void)fcntl(fd, F_ADD_SEALS, F_SEAL_SHRINK | F_SEAL_GROW); |
| 807 |
|
| |
| 808 |
6 |
| void *base = mmap(NULL, (size_t)total, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); |
| 809 |
6 |
▶ | if (base == MAP_FAILED) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (base == MAP_FAILED) { |
0 |
6 |
6 |
|
| 810 |
0 |
▶ | POOL_ERR("mmap(memfd): %s", strerror(errno)); |
Branch: 1 branch
| Branch | True | False | Total |
| POOL_ERR("mmap(memfd): %s", strerror(errno)); |
0 |
0 |
0 |
|
| 811 |
0 |
| close(fd); return NULL; |
| 812 |
|
| } |
| 813 |
|
| |
| 814 |
6 |
| pool_init_header(base, total, elem_size, variant_id, capacity, |
| 815 |
|
| bm_off, own_off, dat_off); |
| 816 |
|
| |
| 817 |
6 |
| return pool_setup_handle(base, (size_t)total, NULL, fd); |
| 818 |
|
| } |
| 819 |
|
| |
| 820 |
5 |
| static PoolHandle *pool_open_fd(int fd, uint32_t variant_id, char *errbuf) { |
| 821 |
5 |
▶ | if (errbuf) errbuf[0] = '\0'; |
Branch: 1 branch
| Branch | True | False | Total |
| if (errbuf) errbuf[0] = '\0'; |
5 |
0 |
5 |
|
| 822 |
|
| |
| 823 |
|
| struct stat st; |
| 824 |
5 |
▶ | if (fstat(fd, &st) < 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (fstat(fd, &st) < 0) { |
0 |
5 |
5 |
|
| 825 |
0 |
▶ | POOL_ERR("fstat(fd=%d): %s", fd, strerror(errno)); |
Branch: 1 branch
| Branch | True | False | Total |
| POOL_ERR("fstat(fd=%d): %s", fd, strerror(errno)); |
0 |
0 |
0 |
|
| 826 |
0 |
| return NULL; |
| 827 |
|
| } |
| 828 |
|
| |
| 829 |
5 |
▶ | if ((uint64_t)st.st_size < sizeof(PoolHeader)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ((uint64_t)st.st_size < sizeof(PoolHeader)) { |
0 |
5 |
5 |
|
| 830 |
0 |
▶ | POOL_ERR("fd %d: too small (%lld)", fd, (long long)st.st_size); |
Branch: 1 branch
| Branch | True | False | Total |
| POOL_ERR("fd %d: too small (%lld)", fd, (long long)st.st_size); |
0 |
0 |
0 |
|
| 831 |
0 |
| return NULL; |
| 832 |
|
| } |
| 833 |
|
| |
| 834 |
|
| /* before mmap: a creator may truncate the file until it has set magic */ |
| 835 |
|
| uint32_t magic; |
| 836 |
5 |
▶ | if (pread(fd, &magic, sizeof magic, offsetof(PoolHeader, magic)) != (ssize_t)sizeof magic || magic != POOL_MAGIC) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (pread(fd, &magic, sizeof magic, offsetof(PoolHeader, magic)) != (ssize_t)sizeof magic || magic != POOL_MAGIC) { |
5 |
0 |
5 |
| if (pread(fd, &magic, sizeof magic, offsetof(PoolHeader, magic)) != (ssize_t)sizeof magic || magic != POOL_MAGIC) { |
0 |
5 |
5 |
|
| 837 |
0 |
▶ | POOL_ERR("fd %d: invalid or incompatible pool", fd); |
Branch: 1 branch
| Branch | True | False | Total |
| POOL_ERR("fd %d: invalid or incompatible pool", fd); |
0 |
0 |
0 |
|
| 838 |
0 |
| return NULL; |
| 839 |
|
| } |
| 840 |
|
| |
| 841 |
5 |
| size_t map_size = (size_t)st.st_size; |
| 842 |
5 |
| void *base = mmap(NULL, map_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); |
| 843 |
5 |
▶ | if (base == MAP_FAILED) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (base == MAP_FAILED) { |
0 |
5 |
5 |
|
| 844 |
0 |
▶ | POOL_ERR("mmap(fd=%d): %s", fd, strerror(errno)); |
Branch: 1 branch
| Branch | True | False | Total |
| POOL_ERR("mmap(fd=%d): %s", fd, strerror(errno)); |
0 |
0 |
0 |
|
| 845 |
0 |
| return NULL; |
| 846 |
|
| } |
| 847 |
|
| |
| 848 |
5 |
▶ | if (!pool_validate_header((PoolHeader *)base, (uint64_t)st.st_size, variant_id)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!pool_validate_header((PoolHeader *)base, (uint64_t)st.st_size, variant_id)) { |
0 |
5 |
5 |
|
| 849 |
0 |
▶ | if (((PoolHeader *)base)->magic == POOL_MAGIC && ((PoolHeader *)base)->version != POOL_VERSION) |
Branch: 2 branches
| Branch | True | False | Total |
| if (((PoolHeader *)base)->magic == POOL_MAGIC && ((PoolHeader *)base)->version != POOL_VERSION) |
0 |
0 |
0 |
| if (((PoolHeader *)base)->magic == POOL_MAGIC && ((PoolHeader *)base)->version != POOL_VERSION) |
0 |
0 |
0 |
|
| 850 |
0 |
▶ | POOL_ERR("fd %d: pool file format version %u is not supported by this build (format %u)", |
Branch: 1 branch
| Branch | True | False | Total |
| POOL_ERR("fd %d: pool file format version %u is not supported by this build (format %u)", |
0 |
0 |
0 |
|
| 851 |
|
| fd, ((PoolHeader *)base)->version, POOL_VERSION); |
| 852 |
|
| else |
| 853 |
0 |
▶ | POOL_ERR("fd %d: invalid or incompatible pool", fd); |
Branch: 1 branch
| Branch | True | False | Total |
| POOL_ERR("fd %d: invalid or incompatible pool", fd); |
0 |
0 |
0 |
|
| 854 |
0 |
| munmap(base, map_size); |
| 855 |
0 |
| return NULL; |
| 856 |
|
| } |
| 857 |
|
| |
| 858 |
5 |
| int myfd = fcntl(fd, F_DUPFD_CLOEXEC, 0); |
| 859 |
5 |
▶ | if (myfd < 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (myfd < 0) { |
0 |
5 |
5 |
|
| 860 |
0 |
▶ | POOL_ERR("fcntl(F_DUPFD_CLOEXEC): %s", strerror(errno)); |
Branch: 1 branch
| Branch | True | False | Total |
| POOL_ERR("fcntl(F_DUPFD_CLOEXEC): %s", strerror(errno)); |
0 |
0 |
0 |
|
| 861 |
0 |
| munmap(base, map_size); |
| 862 |
0 |
| return NULL; |
| 863 |
|
| } |
| 864 |
|
| |
| 865 |
5 |
| return pool_setup_handle(base, map_size, NULL, myfd); |
| 866 |
|
| } |
| 867 |
|
| |
| 868 |
80 |
| static void pool_destroy(PoolHandle *h) { |
| 869 |
80 |
▶ | if (!h) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!h) return; |
0 |
80 |
80 |
|
| 870 |
80 |
▶ | if (h->notify_fd >= 0) close(h->notify_fd); |
Branch: 1 branch
| Branch | True | False | Total |
| if (h->notify_fd >= 0) close(h->notify_fd); |
2 |
78 |
80 |
|
| 871 |
80 |
▶ | if (h->backing_fd >= 0) close(h->backing_fd); |
Branch: 1 branch
| Branch | True | False | Total |
| if (h->backing_fd >= 0) close(h->backing_fd); |
11 |
69 |
80 |
|
| 872 |
80 |
▶ | if (h->hdr) munmap(h->hdr, h->mmap_size); |
Branch: 1 branch
| Branch | True | False | Total |
| if (h->hdr) munmap(h->hdr, h->mmap_size); |
80 |
0 |
80 |
|
| 873 |
80 |
| free(h->path); |
| 874 |
80 |
| free(h); |
| 875 |
|
| } |
| 876 |
|
| |
| 877 |
|
| /* ================================================================ |
| 878 |
|
| * Eventfd integration |
| 879 |
|
| * ================================================================ */ |
| 880 |
|
| |
| 881 |
3 |
| static int pool_create_eventfd(PoolHandle *h) { |
| 882 |
3 |
▶ | if (h->notify_fd >= 0) return h->notify_fd; |
Branch: 1 branch
| Branch | True | False | Total |
| if (h->notify_fd >= 0) return h->notify_fd; |
1 |
2 |
3 |
|
| 883 |
2 |
| int efd = eventfd(0, EFD_NONBLOCK | EFD_CLOEXEC); |
| 884 |
2 |
▶ | if (efd < 0) return -1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (efd < 0) return -1; |
0 |
2 |
2 |
|
| 885 |
2 |
| h->notify_fd = efd; |
| 886 |
2 |
| return efd; |
| 887 |
|
| } |
| 888 |
|
| |
| 889 |
4 |
| static int pool_notify(PoolHandle *h) { |
| 890 |
4 |
▶ | if (h->notify_fd < 0) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (h->notify_fd < 0) return 0; |
0 |
4 |
4 |
|
| 891 |
4 |
| uint64_t val = 1; |
| 892 |
4 |
| return write(h->notify_fd, &val, sizeof(val)) == sizeof(val); |
| 893 |
|
| } |
| 894 |
|
| |
| 895 |
4 |
| static int64_t pool_eventfd_consume(PoolHandle *h) { |
| 896 |
4 |
▶ | if (h->notify_fd < 0) return -1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (h->notify_fd < 0) return -1; |
0 |
4 |
4 |
|
| 897 |
4 |
| uint64_t val = 0; |
| 898 |
4 |
▶ | if (read(h->notify_fd, &val, sizeof(val)) != sizeof(val)) return -1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (read(h->notify_fd, &val, sizeof(val)) != sizeof(val)) return -1; |
1 |
3 |
4 |
|
| 899 |
3 |
| return (int64_t)val; |
| 900 |
|
| } |
| 901 |
|
| |
| 902 |
1 |
| static int pool_msync(PoolHandle *h) { |
| 903 |
1 |
| return msync(h->hdr, h->mmap_size, MS_SYNC); |
| 904 |
|
| } |
| 905 |
|
| |
| 906 |
|
| /* ================================================================ |
| 907 |
|
| * Typed accessors -- integers (atomic) |
| 908 |
|
| * ================================================================ */ |
| 909 |
|
| |
| 910 |
65 |
| static inline int64_t pool_get_i64(PoolHandle *h, uint64_t slot) { |
| 911 |
65 |
| return __atomic_load_n((int64_t *)pool_slot_ptr(h, slot), __ATOMIC_RELAXED); |
| 912 |
|
| } |
| 913 |
|
| |
| 914 |
620 |
| static inline void pool_set_i64(PoolHandle *h, uint64_t slot, int64_t val) { |
| 915 |
620 |
| __atomic_store_n((int64_t *)pool_slot_ptr(h, slot), val, __ATOMIC_RELAXED); |
| 916 |
620 |
| } |
| 917 |
|
| |
| 918 |
3 |
| static inline int pool_cas_i64(PoolHandle *h, uint64_t slot, |
| 919 |
|
| int64_t expected, int64_t desired) { |
| 920 |
3 |
| return __atomic_compare_exchange_n((int64_t *)pool_slot_ptr(h, slot), |
| 921 |
|
| &expected, desired, 0, __ATOMIC_ACQ_REL, __ATOMIC_RELAXED); |
| 922 |
|
| } |
| 923 |
|
| |
| 924 |
2 |
| static inline int64_t pool_cmpxchg_i64(PoolHandle *h, uint64_t slot, |
| 925 |
|
| int64_t expected, int64_t desired) { |
| 926 |
2 |
| __atomic_compare_exchange_n((int64_t *)pool_slot_ptr(h, slot), |
| 927 |
|
| &expected, desired, 0, __ATOMIC_ACQ_REL, __ATOMIC_RELAXED); |
| 928 |
2 |
| return expected; |
| 929 |
|
| } |
| 930 |
|
| |
| 931 |
1 |
| static inline int64_t pool_xchg_i64(PoolHandle *h, uint64_t slot, int64_t val) { |
| 932 |
1 |
| return __atomic_exchange_n((int64_t *)pool_slot_ptr(h, slot), val, __ATOMIC_ACQ_REL); |
| 933 |
|
| } |
| 934 |
|
| |
| 935 |
7 |
| static inline int64_t pool_add_i64(PoolHandle *h, uint64_t slot, int64_t delta) { |
| 936 |
7 |
| return __atomic_add_fetch((int64_t *)pool_slot_ptr(h, slot), delta, __ATOMIC_ACQ_REL); |
| 937 |
|
| } |
| 938 |
|
| |
| 939 |
5 |
| static inline int32_t pool_get_i32(PoolHandle *h, uint64_t slot) { |
| 940 |
5 |
| return __atomic_load_n((int32_t *)pool_slot_ptr(h, slot), __ATOMIC_RELAXED); |
| 941 |
|
| } |
| 942 |
|
| |
| 943 |
10 |
| static inline void pool_set_i32(PoolHandle *h, uint64_t slot, int32_t val) { |
| 944 |
10 |
| __atomic_store_n((int32_t *)pool_slot_ptr(h, slot), val, __ATOMIC_RELAXED); |
| 945 |
10 |
| } |
| 946 |
|
| |
| 947 |
1 |
| static inline int pool_cas_i32(PoolHandle *h, uint64_t slot, |
| 948 |
|
| int32_t expected, int32_t desired) { |
| 949 |
1 |
| return __atomic_compare_exchange_n((int32_t *)pool_slot_ptr(h, slot), |
| 950 |
|
| &expected, desired, 0, __ATOMIC_ACQ_REL, __ATOMIC_RELAXED); |
| 951 |
|
| } |
| 952 |
|
| |
| 953 |
2 |
| static inline int32_t pool_cmpxchg_i32(PoolHandle *h, uint64_t slot, |
| 954 |
|
| int32_t expected, int32_t desired) { |
| 955 |
2 |
| __atomic_compare_exchange_n((int32_t *)pool_slot_ptr(h, slot), |
| 956 |
|
| &expected, desired, 0, __ATOMIC_ACQ_REL, __ATOMIC_RELAXED); |
| 957 |
2 |
| return expected; |
| 958 |
|
| } |
| 959 |
|
| |
| 960 |
1 |
| static inline int32_t pool_xchg_i32(PoolHandle *h, uint64_t slot, int32_t val) { |
| 961 |
1 |
| return __atomic_exchange_n((int32_t *)pool_slot_ptr(h, slot), val, __ATOMIC_ACQ_REL); |
| 962 |
|
| } |
| 963 |
|
| |
| 964 |
2 |
| static inline int32_t pool_add_i32(PoolHandle *h, uint64_t slot, int32_t delta) { |
| 965 |
2 |
| return __atomic_add_fetch((int32_t *)pool_slot_ptr(h, slot), delta, __ATOMIC_ACQ_REL); |
| 966 |
|
| } |
| 967 |
|
| |
| 968 |
|
| /* ================================================================ |
| 969 |
|
| * Typed accessors -- float (non-atomic) |
| 970 |
|
| * ================================================================ */ |
| 971 |
|
| |
| 972 |
7 |
| static inline double pool_get_f64(PoolHandle *h, uint64_t slot) { |
| 973 |
|
| double v; |
| 974 |
7 |
| memcpy(&v, pool_slot_ptr(h, slot), sizeof(double)); |
| 975 |
7 |
| return v; |
| 976 |
|
| } |
| 977 |
|
| |
| 978 |
11 |
| static inline void pool_set_f64(PoolHandle *h, uint64_t slot, double val) { |
| 979 |
11 |
| memcpy(pool_slot_ptr(h, slot), &val, sizeof(double)); |
| 980 |
11 |
| } |
| 981 |
|
| |
| 982 |
|
| /* ================================================================ |
| 983 |
|
| * Typed accessors -- string (4-byte length prefix + data) |
| 984 |
|
| * ================================================================ */ |
| 985 |
|
| |
| 986 |
11 |
| static inline uint32_t pool_get_str_len(PoolHandle *h, uint64_t slot) { |
| 987 |
|
| /* elem_size is attacker-controlled (a local peer can craft the backing file); |
| 988 |
|
| * a Str slot must be large enough to hold the 4-byte length prefix. If it is |
| 989 |
|
| * not, the file is corrupt: skip the prefix read and return 0 rather than let |
| 990 |
|
| * max_len underflow and defeat the clamp below (which would otherwise return a |
| 991 |
|
| * ~4 GiB length -> OOB read). Legit Str pools have elem_size >= 5, so this is a |
| 992 |
|
| * never-taken branch for valid data. */ |
| 993 |
11 |
| uint32_t elem = h->elem_size; |
| 994 |
11 |
▶ | if (elem <= sizeof(uint32_t)) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (elem <= sizeof(uint32_t)) return 0; |
0 |
11 |
11 |
|
| 995 |
|
| uint32_t len; |
| 996 |
11 |
| memcpy(&len, pool_slot_ptr(h, slot), sizeof(uint32_t)); |
| 997 |
11 |
| uint32_t max_len = elem - (uint32_t)sizeof(uint32_t); |
| 998 |
11 |
▶ | if (len > max_len) len = max_len; |
Branch: 1 branch
| Branch | True | False | Total |
| if (len > max_len) len = max_len; |
0 |
11 |
11 |
|
| 999 |
11 |
| return len; |
| 1000 |
|
| } |
| 1001 |
|
| |
| 1002 |
11 |
| static inline const char *pool_get_str_ptr(PoolHandle *h, uint64_t slot) { |
| 1003 |
11 |
| return (const char *)(pool_slot_ptr(h, slot) + sizeof(uint32_t)); |
| 1004 |
|
| } |
| 1005 |
|
| |
| 1006 |
12 |
| static inline void pool_set_str(PoolHandle *h, uint64_t slot, |
| 1007 |
|
| const char *str, uint32_t len) { |
| 1008 |
|
| /* See pool_get_str_len: reject slots too small to hold the length prefix so |
| 1009 |
|
| * the max_len computation cannot underflow (which would defeat the clamp and |
| 1010 |
|
| * let the data memcpy run off the end of the data region -> OOB write). */ |
| 1011 |
12 |
| uint32_t elem = h->elem_size; |
| 1012 |
12 |
▶ | if (elem <= sizeof(uint32_t)) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (elem <= sizeof(uint32_t)) return; |
0 |
12 |
12 |
|
| 1013 |
12 |
| uint32_t max_len = elem - (uint32_t)sizeof(uint32_t); |
| 1014 |
12 |
▶ | if (len > max_len) len = max_len; |
Branch: 1 branch
| Branch | True | False | Total |
| if (len > max_len) len = max_len; |
2 |
10 |
12 |
|
| 1015 |
12 |
| memcpy(pool_slot_ptr(h, slot), &len, sizeof(uint32_t)); |
| 1016 |
12 |
| memcpy(pool_slot_ptr(h, slot) + sizeof(uint32_t), str, len); |
| 1017 |
|
| } |
| 1018 |
|
| |
| 1019 |
|
| /* ================================================================ |
| 1020 |
|
| * Reset -- free all slots (NOT concurrency-safe, caller must |
| 1021 |
|
| * ensure no other process is accessing the pool) |
| 1022 |
|
| * ================================================================ */ |
| 1023 |
|
| |
| 1024 |
10 |
| static inline void pool_reset(PoolHandle *h) { |
| 1025 |
10 |
| PoolHeader *hdr = h->hdr; |
| 1026 |
10 |
| memset(h->bitmap, 0, (size_t)h->bitmap_words * 8); |
| 1027 |
10 |
| memset(h->owners, 0, (size_t)h->capacity * 4); |
| 1028 |
10 |
| __atomic_store_n(&hdr->used, 0, __ATOMIC_RELEASE); |
| 1029 |
|
| /* StoreLoad barrier: see pool_free_slot (reset is documented single-writer, |
| 1030 |
|
| * but keep the handshake correct if a waiter is nonetheless parked). */ |
| 1031 |
10 |
| __atomic_thread_fence(__ATOMIC_SEQ_CST); |
| 1032 |
10 |
▶ | if (__atomic_load_n(&hdr->waiters, __ATOMIC_RELAXED) > 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (__atomic_load_n(&hdr->waiters, __ATOMIC_RELAXED) > 0) |
0 |
10 |
10 |
|
| 1033 |
0 |
| syscall(SYS_futex, &hdr->used, FUTEX_WAKE, INT_MAX, NULL, NULL, 0); |
| 1034 |
10 |
| } |
| 1035 |
|
| |
| 1036 |
|
| #endif /* POOL_H */ |