sensorium-trace 0.18.2__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (135) hide show
  1. sensorium/__init__.py +0 -0
  2. sensorium/__main__.py +4 -0
  3. sensorium/_c/rt/sensorium_c_rt.c +1446 -0
  4. sensorium/_typescript/package-lock.json +68 -0
  5. sensorium/_typescript/package.json +39 -0
  6. sensorium/_typescript/src/bindings.mjs +792 -0
  7. sensorium/_typescript/src/cap.mjs +227 -0
  8. sensorium/_typescript/src/capabilities.mjs +40 -0
  9. sensorium/_typescript/src/dbg.mjs +348 -0
  10. sensorium/_typescript/src/escape.mjs +351 -0
  11. sensorium/_typescript/src/focus.mjs +135 -0
  12. sensorium/_typescript/src/hook.mjs +127 -0
  13. sensorium/_typescript/src/index.mjs +1 -0
  14. sensorium/_typescript/src/naming.mjs +63 -0
  15. sensorium/_typescript/src/positions.mjs +37 -0
  16. sensorium/_typescript/src/probe.mjs +246 -0
  17. sensorium/_typescript/src/qualname.mjs +236 -0
  18. sensorium/_typescript/src/redact.mjs +711 -0
  19. sensorium/_typescript/src/register.mjs +15 -0
  20. sensorium/_typescript/src/resolve.mjs +260 -0
  21. sensorium/_typescript/src/rt.mjs +798 -0
  22. sensorium/_typescript/src/setup.mjs +11 -0
  23. sensorium/_typescript/src/tally.mjs +164 -0
  24. sensorium/_typescript/src/tasks.mjs +175 -0
  25. sensorium/_typescript/src/transform.mjs +800 -0
  26. sensorium/_typescript/src/vite.mjs +99 -0
  27. sensorium/c/__init__.py +8 -0
  28. sensorium/c/cc.py +612 -0
  29. sensorium/c/cli.py +60 -0
  30. sensorium/c/convert.py +669 -0
  31. sensorium/c/decode.py +300 -0
  32. sensorium/c/driver.py +365 -0
  33. sensorium/c/dwarf.py +706 -0
  34. sensorium/c/format.py +271 -0
  35. sensorium/c/pkg.py +187 -0
  36. sensorium/c/plan.py +277 -0
  37. sensorium/cli.py +140 -0
  38. sensorium/driver.py +47 -0
  39. sensorium/exit.py +33 -0
  40. sensorium/invocations.py +83 -0
  41. sensorium/limits.py +253 -0
  42. sensorium/mcp/__init__.py +0 -0
  43. sensorium/mcp/audit.py +162 -0
  44. sensorium/mcp/child.py +240 -0
  45. sensorium/mcp/cmd.py +101 -0
  46. sensorium/mcp/jsonrpc.py +172 -0
  47. sensorium/mcp/result.py +187 -0
  48. sensorium/mcp/schema.py +390 -0
  49. sensorium/mcp/server.py +493 -0
  50. sensorium/mcp/tools.py +299 -0
  51. sensorium/paths.py +80 -0
  52. sensorium/query/__init__.py +0 -0
  53. sensorium/query/caps.py +150 -0
  54. sensorium/query/dbg_dialects.py +64 -0
  55. sensorium/query/diff_cmd.py +748 -0
  56. sensorium/query/diff_notes.py +117 -0
  57. sensorium/query/exceptions_c.py +263 -0
  58. sensorium/query/exceptions_cmd.py +788 -0
  59. sensorium/query/exceptions_group.py +571 -0
  60. sensorium/query/exceptions_invocation.py +487 -0
  61. sensorium/query/exceptions_rust.py +683 -0
  62. sensorium/query/exceptions_typescript.py +688 -0
  63. sensorium/query/expr.py +606 -0
  64. sensorium/query/failures_cmd.py +306 -0
  65. sensorium/query/flow_cmd.py +778 -0
  66. sensorium/query/flow_report.py +128 -0
  67. sensorium/query/flow_values.py +195 -0
  68. sensorium/query/fmt.py +280 -0
  69. sensorium/query/frame_cmd.py +237 -0
  70. sensorium/query/grep_cmd.py +156 -0
  71. sensorium/query/help.py +18 -0
  72. sensorium/query/info_cmd.py +527 -0
  73. sensorium/query/info_rust.py +214 -0
  74. sensorium/query/info_typescript.py +261 -0
  75. sensorium/query/js_inspect.py +394 -0
  76. sensorium/query/moves.py +254 -0
  77. sensorium/query/redact_cmd.py +304 -0
  78. sensorium/query/refocus_cmd.py +661 -0
  79. sensorium/query/refocus_env.py +493 -0
  80. sensorium/query/refocus_facts.py +269 -0
  81. sensorium/query/refocus_licence.py +187 -0
  82. sensorium/query/refocus_report.py +327 -0
  83. sensorium/query/refocus_rust.py +537 -0
  84. sensorium/query/refocus_threads.py +197 -0
  85. sensorium/query/refocus_typescript.py +756 -0
  86. sensorium/query/refocus_world.py +666 -0
  87. sensorium/query/runs_cmd.py +213 -0
  88. sensorium/query/rust_debug.py +176 -0
  89. sensorium/query/sites.py +171 -0
  90. sensorium/query/tree_cmd.py +511 -0
  91. sensorium/query/tree_threads.py +168 -0
  92. sensorium/query/vocab.py +723 -0
  93. sensorium/query/watch_cmd.py +657 -0
  94. sensorium/record/__init__.py +0 -0
  95. sensorium/record/boot.py +797 -0
  96. sensorium/record/boot_io.py +141 -0
  97. sensorium/record/budget.py +135 -0
  98. sensorium/record/capture.py +433 -0
  99. sensorium/record/children.py +755 -0
  100. sensorium/record/children_merge.py +179 -0
  101. sensorium/record/elide.py +55 -0
  102. sensorium/record/fingerprint.py +26 -0
  103. sensorium/record/thread_starts.py +261 -0
  104. sensorium/record/tracer.py +798 -0
  105. sensorium/record/tracer_exc.py +199 -0
  106. sensorium/record/tracer_frames.py +618 -0
  107. sensorium/redact.py +304 -0
  108. sensorium/redact_content.py +156 -0
  109. sensorium/redact_key.py +317 -0
  110. sensorium/redact_store.py +502 -0
  111. sensorium/redact_values.py +433 -0
  112. sensorium/store/__init__.py +0 -0
  113. sensorium/store/db.py +284 -0
  114. sensorium/store/gc.py +360 -0
  115. sensorium/store/payloads.py +69 -0
  116. sensorium/store/reader.py +438 -0
  117. sensorium/store/writer.py +283 -0
  118. sensorium/ts/__init__.py +10 -0
  119. sensorium/ts/build.py +783 -0
  120. sensorium/ts/cli.py +130 -0
  121. sensorium/ts/driver.py +472 -0
  122. sensorium/ts/focus.py +146 -0
  123. sensorium/ts/harness.py +314 -0
  124. sensorium/ts/ingest.py +380 -0
  125. sensorium/ts/invocation.py +252 -0
  126. sensorium/ts/pkg.py +167 -0
  127. sensorium/ts/redaction.py +64 -0
  128. sensorium/ts/spool.py +168 -0
  129. sensorium/ts/truncation.py +81 -0
  130. sensorium/ts/wrapper.py +255 -0
  131. sensorium_trace-0.18.2.dist-info/METADATA +245 -0
  132. sensorium_trace-0.18.2.dist-info/RECORD +135 -0
  133. sensorium_trace-0.18.2.dist-info/WHEEL +4 -0
  134. sensorium_trace-0.18.2.dist-info/entry_points.txt +3 -0
  135. sensorium_trace-0.18.2.dist-info/licenses/LICENSE +21 -0
@@ -0,0 +1,1446 @@
1
+ /* Linux x86-64 recorder runtime. Wire format: sensorium.c.format (v1).
2
+ * Linked into programs built with -g -fno-omit-frame-pointer -fno-inline
3
+ * -finstrument-functions. Inactive unless SENSORIUM_C_ROOT is set.
4
+ *
5
+ * cfa = callee rbp + 16. At -O0, DW_OP_fbreg is relative to that CFA.
6
+ * glibc 2.41 mangles jmp_buf rbp/rsp/pc as ROL(value ^ %fs:0x30, 17).
7
+ */
8
+ #define _GNU_SOURCE
9
+ /* Ubuntu's gcc defines _FORTIFY_SOURCE when optimizing. glibc then
10
+ * renames longjmp, _longjmp, and siglongjmp onto __longjmp_chk (an asm
11
+ * label, or a macro when the compiler has no asm labels). The four
12
+ * interposers below have to stay four symbols: fortified callers enter
13
+ * at __longjmp_chk, everyone else at longjmp. This file does not need
14
+ * fortify of its own, and the level is latched by the first include. */
15
+ #undef _FORTIFY_SOURCE
16
+
17
+ #include <dlfcn.h>
18
+ #include <elf.h>
19
+ #include <errno.h>
20
+ #include <fcntl.h>
21
+ #include <link.h>
22
+ #include <pthread.h>
23
+ #include <setjmp.h>
24
+ #include <signal.h>
25
+ #include <stdatomic.h>
26
+ #include <stdint.h>
27
+ #include <stdio.h>
28
+ #include <stdlib.h>
29
+ #include <string.h>
30
+ #include <time.h>
31
+ #include <ucontext.h>
32
+ #include <sys/stat.h>
33
+ #include <sys/syscall.h>
34
+ #include <sys/uio.h>
35
+ #include <unistd.h>
36
+
37
+ #define NOI __attribute__((no_instrument_function))
38
+
39
+ #define NO_SLOT ((int32_t)(-2147483647 - 1))
40
+ #define FLAG_CSTR 1
41
+ #define FLAG_TRUNC 2
42
+ #define RET_MEM 3
43
+ #define STR_CAP 64
44
+ #define PARAM_CAP 256
45
+ #define STACK_BYTES 512
46
+ #define ASSERT_CLIP 200
47
+ #define SHADOW_MAX 4096
48
+ #define MAX_THREADS 256
49
+ #define MAX_OBJS 128
50
+ #define MAX_LOADS 8
51
+ #define MAX_FUNCS 16384
52
+ #define MAX_PARAMS 65536
53
+ #define BUF_SIZE (1 << 16)
54
+ #define BLOB_MAX (1 << 14)
55
+ #define ARCH_SHSTK_STATUS_ 0x5005
56
+ #define PAGE 4096u
57
+
58
+ struct param_plan {
59
+ int32_t off;
60
+ uint16_t size;
61
+ uint16_t flags;
62
+ };
63
+
64
+ struct func_plan {
65
+ uint64_t addr; /* unrelocated low_pc */
66
+ uint32_t param_index;
67
+ uint16_t nparams;
68
+ uint16_t ret_size;
69
+ uint8_t ret_class;
70
+ uint8_t ret_nreg;
71
+ };
72
+
73
+ struct load_range {
74
+ uint64_t lo, hi;
75
+ };
76
+
77
+ struct obj_mod {
78
+ uint64_t bias;
79
+ struct load_range loads[MAX_LOADS];
80
+ int nloads;
81
+ uint32_t func_begin;
82
+ uint32_t nfuncs;
83
+ };
84
+
85
+ struct shadow_ent {
86
+ uint64_t *slot;
87
+ uint64_t orig;
88
+ uint64_t rbp;
89
+ uint16_t ret_size;
90
+ uint8_t ret_class;
91
+ };
92
+
93
+ struct tstate {
94
+ uint32_t tid;
95
+ int fd;
96
+ atomic_int dead;
97
+ atomic_size_t pub_len; /* bytes of buf[] visible to a crash */
98
+ atomic_size_t disk_len; /* prefix of buf[] already write()'d */
99
+ size_t len; /* owner-only logical length */
100
+ int depth;
101
+ unsigned char buf[BUF_SIZE];
102
+ unsigned char scratch[BLOB_MAX];
103
+ struct shadow_ent shadow[SHADOW_MAX];
104
+ };
105
+
106
+ struct hit {
107
+ int found;
108
+ uint8_t ret_class;
109
+ uint16_t ret_size;
110
+ uint16_t nparams;
111
+ uint32_t param_index;
112
+ };
113
+
114
+ typedef void (*lj_fn)(struct __jmp_buf_tag *, int);
115
+ typedef void (*assert_fn)(const char *, const char *, unsigned, const char *);
116
+ typedef pid_t (*fork_fn)(void);
117
+ typedef int (*thr_fn)(pthread_t *, const pthread_attr_t *, void *(*)(void *), void *);
118
+
119
+ extern void sensorium_ret_tramp(void);
120
+
121
+ static struct param_plan g_params[MAX_PARAMS];
122
+ static struct func_plan g_funcs[MAX_FUNCS];
123
+ static struct obj_mod g_objs[MAX_OBJS];
124
+ static unsigned g_nparams;
125
+ static unsigned g_nfuncs;
126
+ static atomic_int g_nobjs;
127
+ static atomic_int g_scan_lock;
128
+ static __thread int g_scanning;
129
+
130
+ static atomic_int g_on;
131
+ static atomic_int g_cet;
132
+ static int g_no_retval;
133
+ static atomic_int g_crashing;
134
+ static atomic_uint_fast64_t g_seq = 1;
135
+ static atomic_uint g_next_tid = 1;
136
+ static atomic_uint g_started;
137
+ static atomic_int g_resolved;
138
+ static pid_t g_proc_pid;
139
+ static char g_root[4096];
140
+ static char g_proc[4096];
141
+ static char g_exe[4096];
142
+ static char g_objs_txt[65536];
143
+ static size_t g_objs_len;
144
+ static char *g_argv_json;
145
+ static size_t g_argv_len;
146
+ static pid_t g_kids[256];
147
+ static int g_nkids;
148
+
149
+ static _Atomic uintptr_t g_reg[MAX_THREADS];
150
+ static atomic_uint g_nreg;
151
+
152
+ static __thread struct tstate *ts_;
153
+ static pthread_key_t g_key;
154
+ static int g_key_ok;
155
+
156
+ static lj_fn real_longjmp, real__longjmp, real_siglongjmp, real___longjmp_chk;
157
+ static assert_fn real_assert_fail;
158
+ static fork_fn real_fork;
159
+ static thr_fn real_pthread_create;
160
+
161
+ NOI static void resolve_syms(void);
162
+ NOI static void flush_claim(struct tstate *t);
163
+ NOI static void emit(struct tstate *t, uint8_t kind, uint64_t a, uint64_t b,
164
+ uint64_t c, uint64_t d, const void *extra, uint32_t elen);
165
+ NOI static struct tstate *me(void);
166
+ NOI static void child_switch(void);
167
+ NOI static void write_threads_json(void);
168
+ NOI static void repair_longjmp(struct __jmp_buf_tag *env);
169
+
170
+ NOI static void pause_once(void) {
171
+ __asm__ volatile("pause");
172
+ }
173
+
174
+ NOI static int write_full(int fd, const void *buf, size_t n) {
175
+ const unsigned char *p = buf;
176
+ size_t off = 0;
177
+ while (off < n) {
178
+ ssize_t w = write(fd, p + off, n - off);
179
+ if (w < 0) {
180
+ if (errno == EINTR) continue;
181
+ return -1;
182
+ }
183
+ if (w == 0) return -1;
184
+ off += (size_t)w;
185
+ }
186
+ return 0;
187
+ }
188
+
189
+ NOI static char *ap_str(char *p, char *end, const char *s) {
190
+ while (*s && p < end) *p++ = *s++;
191
+ return p;
192
+ }
193
+
194
+ NOI static char *ap_u(char *p, char *end, unsigned long v) {
195
+ char rev[32];
196
+ int r = 0;
197
+ if (v == 0) rev[r++] = '0';
198
+ while (v) {
199
+ rev[r++] = (char)('0' + (v % 10));
200
+ v /= 10;
201
+ }
202
+ while (r && p < end) *p++ = rev[--r];
203
+ return p;
204
+ }
205
+
206
+ NOI static int join2(char *dst, size_t cap, const char *a, const char *b) {
207
+ size_t na = strlen(a), nb = strlen(b);
208
+ if (na + 1 + nb + 1 > cap) {
209
+ if (cap) dst[0] = 0;
210
+ return -1;
211
+ }
212
+ memcpy(dst, a, na);
213
+ dst[na] = '/';
214
+ memcpy(dst + na + 1, b, nb + 1);
215
+ return 0;
216
+ }
217
+
218
+ NOI static uint64_t now_ns(void) {
219
+ struct timespec ts;
220
+ clock_gettime(CLOCK_REALTIME, &ts);
221
+ return (uint64_t)ts.tv_sec * 1000000000ull + (uint64_t)ts.tv_nsec;
222
+ }
223
+
224
+ NOI static size_t copy_mem(void *dst, uintptr_t src, size_t n) {
225
+ size_t got = 0;
226
+ unsigned char *d = dst;
227
+ if (n == 0 || src == 0) return 0;
228
+ while (got < n) {
229
+ size_t left = PAGE - (size_t)(src & (PAGE - 1));
230
+ size_t chunk = n - got;
231
+ struct iovec li, ri;
232
+ ssize_t r;
233
+ if (chunk > left) chunk = left;
234
+ li.iov_base = d + got;
235
+ li.iov_len = chunk;
236
+ ri.iov_base = (void *)src;
237
+ ri.iov_len = chunk;
238
+ do {
239
+ r = process_vm_readv(getpid(), &li, 1, &ri, 1, 0);
240
+ } while (r < 0 && errno == EINTR);
241
+ if (r < 0) break;
242
+ got += (size_t)r;
243
+ src += (uintptr_t)r;
244
+ if ((size_t)r != chunk) break;
245
+ }
246
+ return got;
247
+ }
248
+
249
+ /* ---- thread buffer -------------------------------------------------- */
250
+
251
+ NOI static void flush_claim(struct tstate *t) {
252
+ size_t disk, pub;
253
+ if (!t || t->fd < 0) return;
254
+ disk = atomic_load_explicit(&t->disk_len, memory_order_relaxed);
255
+ pub = atomic_load_explicit(&t->pub_len, memory_order_acquire);
256
+ if (pub <= disk) return;
257
+ if (!atomic_compare_exchange_strong_explicit(
258
+ &t->disk_len, &disk, pub,
259
+ memory_order_acq_rel, memory_order_relaxed))
260
+ return;
261
+ (void)write_full(t->fd, t->buf + disk, pub - disk);
262
+ }
263
+
264
+ NOI static void emit(struct tstate *t, uint8_t kind, uint64_t a, uint64_t b,
265
+ uint64_t c, uint64_t d, const void *extra, uint32_t elen) {
266
+ unsigned char *p;
267
+ uint64_t seq, ts;
268
+ uint32_t total;
269
+ if (!t || t->fd < 0) return;
270
+ if (elen > BUF_SIZE - 56) elen = BUF_SIZE - 56;
271
+ total = 56u + elen;
272
+ if (t->len + total > BUF_SIZE) {
273
+ flush_claim(t);
274
+ if (!atomic_load_explicit(&g_crashing, memory_order_relaxed) &&
275
+ atomic_load_explicit(&t->disk_len, memory_order_relaxed) == t->len &&
276
+ atomic_load_explicit(&t->pub_len, memory_order_relaxed) == t->len) {
277
+ t->len = 0;
278
+ atomic_store_explicit(&t->pub_len, 0, memory_order_relaxed);
279
+ atomic_store_explicit(&t->disk_len, 0, memory_order_relaxed);
280
+ }
281
+ }
282
+ if (t->len + total > BUF_SIZE) return;
283
+ p = t->buf + t->len;
284
+ memset(p, 0, 56);
285
+ p[0] = kind;
286
+ seq = atomic_fetch_add_explicit(&g_seq, 1, memory_order_relaxed);
287
+ ts = now_ns();
288
+ memcpy(p + 4, &seq, 8);
289
+ memcpy(p + 12, &ts, 8);
290
+ memcpy(p + 20, &a, 8);
291
+ memcpy(p + 28, &b, 8);
292
+ memcpy(p + 36, &c, 8);
293
+ memcpy(p + 44, &d, 8);
294
+ memcpy(p + 52, &elen, 4);
295
+ if (elen && extra) memcpy(p + 56, extra, elen);
296
+ t->len += total;
297
+ /* Publish only after the bytes are in place. A crash mid-record
298
+ * keeps the previous length, so at most this record is lost. */
299
+ atomic_store_explicit(&t->pub_len, t->len, memory_order_release);
300
+ }
301
+
302
+ /* ---- plans ---------------------------------------------------------- */
303
+
304
+ NOI static int obj_contains(const struct obj_mod *o, uint64_t fn) {
305
+ int i;
306
+ for (i = 0; i < o->nloads; i++) {
307
+ if (fn >= o->loads[i].lo && fn < o->loads[i].hi) return 1;
308
+ }
309
+ return 0;
310
+ }
311
+
312
+ NOI static int find_hit(uint64_t fn, struct hit *h) {
313
+ int n, i;
314
+ memset(h, 0, sizeof *h);
315
+ n = atomic_load_explicit(&g_nobjs, memory_order_acquire);
316
+ for (i = 0; i < n; i++) {
317
+ const struct obj_mod *o = &g_objs[i];
318
+ uint64_t rel;
319
+ uint32_t lo, hi;
320
+ if (!obj_contains(o, fn)) continue;
321
+ rel = fn - o->bias;
322
+ lo = 0;
323
+ hi = o->nfuncs;
324
+ while (lo < hi) {
325
+ uint32_t mid = lo + (hi - lo) / 2;
326
+ uint64_t a = g_funcs[o->func_begin + mid].addr;
327
+ if (a == rel) {
328
+ const struct func_plan *fp = &g_funcs[o->func_begin + mid];
329
+ h->found = 1;
330
+ h->ret_class = fp->ret_class;
331
+ h->ret_size = fp->ret_size;
332
+ h->nparams = fp->nparams;
333
+ h->param_index = fp->param_index;
334
+ return 1;
335
+ }
336
+ if (a < rel) lo = mid + 1;
337
+ else hi = mid;
338
+ }
339
+ return 0; /* in this image, but not in its plan */
340
+ }
341
+ return 0;
342
+ }
343
+
344
+ NOI static int cmp_func(const void *a, const void *b) {
345
+ const struct func_plan *fa = a, *fb = b;
346
+ if (fa->addr < fb->addr) return -1;
347
+ if (fa->addr > fb->addr) return 1;
348
+ return 0;
349
+ }
350
+
351
+ NOI static void load_plan_file(const char *path, struct obj_mod *o) {
352
+ int fd;
353
+ struct stat st;
354
+ unsigned char *buf = NULL;
355
+ size_t nread = 0;
356
+ uint32_t ver = 0, count = 0;
357
+ size_t pos, i;
358
+ uint32_t begin;
359
+ fd = open(path, O_RDONLY | O_CLOEXEC);
360
+ if (fd < 0) return;
361
+ if (fstat(fd, &st) < 0 || st.st_size < 12 || st.st_size > 64 * 1024 * 1024) {
362
+ close(fd);
363
+ return;
364
+ }
365
+ buf = malloc((size_t)st.st_size);
366
+ if (!buf) {
367
+ close(fd);
368
+ return;
369
+ }
370
+ while (nread < (size_t)st.st_size) {
371
+ ssize_t r = read(fd, buf + nread, (size_t)st.st_size - nread);
372
+ if (r < 0) {
373
+ if (errno == EINTR) continue;
374
+ break;
375
+ }
376
+ if (r == 0) break;
377
+ nread += (size_t)r;
378
+ }
379
+ close(fd);
380
+ if (nread != (size_t)st.st_size || memcmp(buf, "SCPL", 4) != 0) {
381
+ free(buf);
382
+ return;
383
+ }
384
+ memcpy(&ver, buf + 4, 4);
385
+ memcpy(&count, buf + 8, 4);
386
+ if (ver != 1) {
387
+ free(buf);
388
+ return;
389
+ }
390
+ begin = g_nfuncs;
391
+ pos = 12;
392
+ for (i = 0; i < count; i++) {
393
+ struct func_plan fp;
394
+ uint16_t np, p;
395
+ if (pos + 14 > nread) break;
396
+ if (g_nfuncs >= MAX_FUNCS) break;
397
+ memset(&fp, 0, sizeof fp);
398
+ memcpy(&fp.addr, buf + pos, 8);
399
+ fp.ret_class = buf[pos + 8];
400
+ fp.ret_nreg = buf[pos + 9];
401
+ memcpy(&fp.ret_size, buf + pos + 10, 2);
402
+ memcpy(&np, buf + pos + 12, 2);
403
+ pos += 14;
404
+ fp.param_index = g_nparams;
405
+ fp.nparams = 0;
406
+ for (p = 0; p < np; p++) {
407
+ struct param_plan pp;
408
+ if (pos + 8 > nread) {
409
+ pos = nread + 1;
410
+ break;
411
+ }
412
+ if (g_nparams >= MAX_PARAMS) {
413
+ pos += 8;
414
+ continue;
415
+ }
416
+ memcpy(&pp.off, buf + pos, 4);
417
+ memcpy(&pp.size, buf + pos + 4, 2);
418
+ memcpy(&pp.flags, buf + pos + 6, 2);
419
+ pos += 8;
420
+ g_params[g_nparams++] = pp;
421
+ fp.nparams++;
422
+ }
423
+ if (pos > nread) break;
424
+ g_funcs[g_nfuncs++] = fp;
425
+ }
426
+ o->func_begin = begin;
427
+ o->nfuncs = g_nfuncs - begin;
428
+ if (o->nfuncs > 1)
429
+ qsort(g_funcs + begin, o->nfuncs, sizeof(struct func_plan), cmp_func);
430
+ free(buf);
431
+ }
432
+
433
+ NOI static int known_bias(uint64_t bias) {
434
+ int n = atomic_load_explicit(&g_nobjs, memory_order_relaxed);
435
+ int i;
436
+ for (i = 0; i < n; i++)
437
+ if (g_objs[i].bias == bias) return 1;
438
+ return 0;
439
+ }
440
+
441
+ NOI static void objs_add(uint64_t bias, const char *name) {
442
+ char line[4200];
443
+ int n;
444
+ size_t len;
445
+ n = snprintf(line, sizeof line, "%lx %s\n", (unsigned long)bias, name);
446
+ if (n < 0) return;
447
+ len = (size_t)n;
448
+ if (len >= sizeof line) return;
449
+ if (g_objs_len + len >= sizeof g_objs_txt) return;
450
+ memcpy(g_objs_txt + g_objs_len, line, len);
451
+ g_objs_len += len;
452
+ }
453
+
454
+ NOI static void write_objs_file(void) {
455
+ char path[4200];
456
+ int fd;
457
+ if (join2(path, sizeof path, g_proc, "objs.txt") != 0) return;
458
+ fd = open(path, O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, 0600);
459
+ if (fd < 0) return;
460
+ (void)write_full(fd, g_objs_txt, g_objs_len);
461
+ close(fd);
462
+ }
463
+
464
+ NOI static int phdr_cb(struct dl_phdr_info *info, size_t sz, void *data) {
465
+ struct obj_mod o;
466
+ const char *name;
467
+ int j, slot;
468
+ char *plan = NULL;
469
+ (void)sz;
470
+ (void)data;
471
+ name = (info->dlpi_name && info->dlpi_name[0]) ? info->dlpi_name : "<exe>";
472
+ objs_add((uint64_t)info->dlpi_addr, name);
473
+ if (known_bias((uint64_t)info->dlpi_addr)) return 0;
474
+ slot = atomic_load_explicit(&g_nobjs, memory_order_relaxed);
475
+ if (slot >= MAX_OBJS) return 0;
476
+ memset(&o, 0, sizeof o);
477
+ o.bias = (uint64_t)info->dlpi_addr;
478
+ for (j = 0; j < (int)info->dlpi_phnum && o.nloads < MAX_LOADS; j++) {
479
+ const ElfW(Phdr) *ph = &info->dlpi_phdr[j];
480
+ if (ph->p_type != PT_LOAD || ph->p_memsz == 0) continue;
481
+ o.loads[o.nloads].lo = o.bias + (uint64_t)ph->p_vaddr;
482
+ o.loads[o.nloads].hi = o.loads[o.nloads].lo + (uint64_t)ph->p_memsz;
483
+ o.nloads++;
484
+ }
485
+ if (name[0] == '<' ) {
486
+ size_t el = strlen(g_exe);
487
+ plan = malloc(el + 8);
488
+ if (plan) {
489
+ memcpy(plan, g_exe, el);
490
+ memcpy(plan + el, ".scplan", 8);
491
+ }
492
+ } else {
493
+ size_t el = strlen(info->dlpi_name);
494
+ plan = malloc(el + 8);
495
+ if (plan) {
496
+ memcpy(plan, info->dlpi_name, el);
497
+ memcpy(plan + el, ".scplan", 8);
498
+ }
499
+ }
500
+ if (plan) {
501
+ load_plan_file(plan, &o);
502
+ free(plan);
503
+ }
504
+ g_objs[slot] = o;
505
+ atomic_store_explicit(&g_nobjs, slot + 1, memory_order_release);
506
+ return 0;
507
+ }
508
+
509
+ NOI static void scan_phdrs(void) {
510
+ int exp;
511
+ if (g_scanning) return;
512
+ g_scanning = 1;
513
+ exp = 0;
514
+ while (!atomic_compare_exchange_weak_explicit(
515
+ &g_scan_lock, &exp, 1, memory_order_acquire, memory_order_relaxed)) {
516
+ exp = 0;
517
+ pause_once();
518
+ }
519
+ g_objs_len = 0;
520
+ dl_iterate_phdr(phdr_cb, NULL);
521
+ write_objs_file();
522
+ atomic_store_explicit(&g_scan_lock, 0, memory_order_release);
523
+ g_scanning = 0;
524
+ }
525
+
526
+ /* ---- argument blob -------------------------------------------------- */
527
+
528
+ NOI static int read_cstr(uintptr_t ptr, unsigned char *dst, int *slen) {
529
+ unsigned char tmp[STR_CAP];
530
+ size_t got, i;
531
+ if (ptr == 0) {
532
+ *slen = -2;
533
+ return 0;
534
+ }
535
+ got = copy_mem(tmp, ptr, STR_CAP);
536
+ if (got == 0) {
537
+ *slen = -1;
538
+ return 0;
539
+ }
540
+ for (i = 0; i < got; i++) {
541
+ if (tmp[i] == 0) {
542
+ *slen = (int)i;
543
+ if (i) memcpy(dst, tmp, i);
544
+ return 0;
545
+ }
546
+ }
547
+ *slen = (int)got;
548
+ memcpy(dst, tmp, got);
549
+ return 0;
550
+ }
551
+
552
+ NOI static uint32_t build_blob(const struct hit *h, int have_fp, uint64_t cfa,
553
+ unsigned char *dst, size_t cap) {
554
+ unsigned char *p = dst;
555
+ unsigned char *end = dst + cap;
556
+ uint16_t n = 0, i;
557
+ uint16_t zero = 0;
558
+ if (!h->found || !have_fp) {
559
+ if (cap < 4) return 0;
560
+ memcpy(dst, &zero, 2);
561
+ memcpy(dst + 2, &zero, 2);
562
+ return 4;
563
+ }
564
+ if (cap < 4) return 0;
565
+ p += 4; /* patch nparams at the end */
566
+ for (i = 0; i < h->nparams; i++) {
567
+ const struct param_plan *pp = &g_params[h->param_index + i];
568
+ uint16_t flags = pp->flags;
569
+ uint16_t copy = pp->size;
570
+ unsigned char raw[PARAM_CAP];
571
+ unsigned char sbuf[STR_CAP];
572
+ int slen = -1;
573
+ int want_str = (flags & FLAG_CSTR) != 0;
574
+ size_t need;
575
+ if (pp->off == NO_SLOT || pp->size == 0) copy = 0;
576
+ else if (copy > PARAM_CAP) {
577
+ copy = PARAM_CAP;
578
+ flags = (uint16_t)(flags | FLAG_TRUNC);
579
+ }
580
+ if (copy) {
581
+ uint64_t addr = cfa + (int64_t)pp->off;
582
+ size_t got = copy_mem(raw, (uintptr_t)addr, copy);
583
+ /* A bad slot is a missing argument, not a crash in the hook. */
584
+ if (got != copy) {
585
+ copy = 0;
586
+ flags = (uint16_t)(flags & (uint16_t)~(FLAG_CSTR | FLAG_TRUNC));
587
+ want_str = 0;
588
+ }
589
+ }
590
+ if (want_str) {
591
+ if (pp->off == NO_SLOT || pp->size < 8 || !copy) slen = -1;
592
+ else {
593
+ uint64_t ptr = 0;
594
+ memcpy(&ptr, raw, 8);
595
+ read_cstr((uintptr_t)ptr, sbuf, &slen);
596
+ }
597
+ }
598
+ need = 8u + copy;
599
+ if (want_str) {
600
+ need += 4;
601
+ if (slen > 0) need += (size_t)slen;
602
+ }
603
+ if (p + need > end) break;
604
+ memcpy(p, &pp->off, 4);
605
+ p += 4;
606
+ memcpy(p, &copy, 2);
607
+ p += 2;
608
+ memcpy(p, &flags, 2);
609
+ p += 2;
610
+ if (copy) {
611
+ memcpy(p, raw, copy);
612
+ p += copy;
613
+ }
614
+ if (want_str) {
615
+ int32_t sl = slen;
616
+ memcpy(p, &sl, 4);
617
+ p += 4;
618
+ if (slen > 0) {
619
+ memcpy(p, sbuf, (size_t)slen);
620
+ p += slen;
621
+ }
622
+ }
623
+ n++;
624
+ }
625
+ memcpy(dst, &n, 2);
626
+ memcpy(dst + 2, &zero, 2);
627
+ return (uint32_t)(p - dst);
628
+ }
629
+
630
+ /* ---- meta files ----------------------------------------------------- */
631
+
632
+ NOI static int mkdir_0700(const char *path) {
633
+ if (mkdir(path, 0700) == 0 || errno == EEXIST) {
634
+ (void)chmod(path, 0700);
635
+ return 0;
636
+ }
637
+ return -1;
638
+ }
639
+
640
+ NOI static int ensure_dirs(pid_t pid) {
641
+ char tmp[32];
642
+ int tn = 0;
643
+ unsigned long v;
644
+ size_t rl;
645
+ g_proc_pid = pid;
646
+ if (mkdir(g_root, 0700) == 0) (void)chmod(g_root, 0700);
647
+ v = (unsigned long)pid;
648
+ if (v == 0) tmp[tn++] = '0';
649
+ else {
650
+ char rev[32];
651
+ int r = 0;
652
+ while (v) {
653
+ rev[r++] = (char)('0' + (v % 10));
654
+ v /= 10;
655
+ }
656
+ while (r) tmp[tn++] = rev[--r];
657
+ }
658
+ tmp[tn] = 0;
659
+ rl = strlen(g_root);
660
+ if (rl + 1 + (size_t)tn + 1 > sizeof g_proc) return -1;
661
+ memcpy(g_proc, g_root, rl);
662
+ g_proc[rl] = '/';
663
+ memcpy(g_proc + rl + 1, tmp, (size_t)tn + 1);
664
+ if (mkdir_0700(g_proc) != 0) return -1;
665
+ {
666
+ char th[4200];
667
+ if (join2(th, sizeof th, g_proc, "threads") != 0) return -1;
668
+ if (mkdir_0700(th) != 0) return -1;
669
+ }
670
+ return 0;
671
+ }
672
+
673
+ NOI static void write_boot(void) {
674
+ char path[4200];
675
+ char body[8192];
676
+ char *p, *end;
677
+ struct timespec ts;
678
+ int fd;
679
+ const char *s;
680
+ if (join2(path, sizeof path, g_proc, "boot.json") != 0) return;
681
+ clock_gettime(CLOCK_REALTIME, &ts);
682
+ p = body;
683
+ end = body + sizeof body - 4;
684
+ p = ap_str(p, end, "{\"start_ts\":");
685
+ p = ap_u(p, end, (unsigned long)ts.tv_sec);
686
+ if (p < end) *p++ = '.';
687
+ {
688
+ unsigned long nsec = (unsigned long)ts.tv_nsec;
689
+ char rev[9];
690
+ int i;
691
+ for (i = 0; i < 9; i++) {
692
+ rev[i] = (char)('0' + (nsec % 10));
693
+ nsec /= 10;
694
+ }
695
+ for (i = 8; i >= 0 && p < end; i--) *p++ = rev[i];
696
+ }
697
+ p = ap_str(p, end, ",\"exe\":\"");
698
+ for (s = g_exe; *s && p + 2 < end; s++) {
699
+ if (*s == '"' || *s == '\\') *p++ = '\\';
700
+ *p++ = *s;
701
+ }
702
+ p = ap_str(p, end, "\"}\n");
703
+ fd = open(path, O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, 0600);
704
+ if (fd < 0) return;
705
+ (void)write_full(fd, body, (size_t)(p - body));
706
+ close(fd);
707
+ }
708
+
709
+ NOI static void json_escape_write(int fd, const char *s, size_t n) {
710
+ size_t i;
711
+ (void)write_full(fd, "\"", 1);
712
+ for (i = 0; i < n; i++) {
713
+ unsigned char c = (unsigned char)s[i];
714
+ char buf[8];
715
+ if (c == '"' || c == '\\') {
716
+ buf[0] = '\\';
717
+ buf[1] = (char)c;
718
+ (void)write_full(fd, buf, 2);
719
+ } else if (c == '\n') {
720
+ (void)write_full(fd, "\\n", 2);
721
+ } else if (c == '\r') {
722
+ (void)write_full(fd, "\\r", 2);
723
+ } else if (c == '\t') {
724
+ (void)write_full(fd, "\\t", 2);
725
+ } else if (c < 0x20) {
726
+ static const char hex[] = "0123456789abcdef";
727
+ buf[0] = '\\';
728
+ buf[1] = 'u';
729
+ buf[2] = '0';
730
+ buf[3] = '0';
731
+ buf[4] = hex[c >> 4];
732
+ buf[5] = hex[c & 0xf];
733
+ (void)write_full(fd, buf, 6);
734
+ } else {
735
+ (void)write_full(fd, &c, 1);
736
+ }
737
+ }
738
+ (void)write_full(fd, "\"", 1);
739
+ }
740
+
741
+ NOI static void write_argv(void) {
742
+ char path[4200];
743
+ char *raw;
744
+ int inf, out;
745
+ ssize_t n;
746
+ size_t i, start;
747
+ int first = 1;
748
+ raw = malloc(1 << 16);
749
+ if (!raw) return;
750
+ inf = open("/proc/self/cmdline", O_RDONLY | O_CLOEXEC);
751
+ if (inf < 0) {
752
+ free(raw);
753
+ return;
754
+ }
755
+ n = read(inf, raw, (1 << 16) - 1);
756
+ close(inf);
757
+ if (n < 0) n = 0;
758
+ raw[n] = 0;
759
+ if (join2(path, sizeof path, g_proc, "argv.json") != 0) {
760
+ free(raw);
761
+ return;
762
+ }
763
+ out = open(path, O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, 0600);
764
+ if (out < 0) {
765
+ free(raw);
766
+ return;
767
+ }
768
+ (void)write_full(out, "[", 1);
769
+ start = 0;
770
+ for (i = 0; i <= (size_t)n; i++) {
771
+ if (i == (size_t)n || raw[i] == 0) {
772
+ /* i == start is an empty argument (two NULs) or the end. */
773
+ if (i == start && i == (size_t)n) break;
774
+ if (i == start && !(i < (size_t)n)) break;
775
+ if (!first) (void)write_full(out, ",", 1);
776
+ first = 0;
777
+ json_escape_write(out, raw + start, i - start);
778
+ if (i == (size_t)n) break;
779
+ start = i + 1;
780
+ if (start >= (size_t)n) break;
781
+ }
782
+ }
783
+ (void)write_full(out, "]\n", 2);
784
+ close(out);
785
+ /* Replay buffer so a forked child can rewrite argv.json with write()
786
+ * only, without taking the malloc lock. */
787
+ free(raw);
788
+ /* Read the file we just wrote into the child-replay buffer. */
789
+ {
790
+ int fd = open(path, O_RDONLY | O_CLOEXEC);
791
+ struct stat st;
792
+ if (fd >= 0 && fstat(fd, &st) == 0 && st.st_size > 0 &&
793
+ st.st_size < (1 << 20)) {
794
+ char *copy = malloc((size_t)st.st_size);
795
+ if (copy) {
796
+ size_t got = 0;
797
+ while (got < (size_t)st.st_size) {
798
+ ssize_t r = read(fd, copy + got, (size_t)st.st_size - got);
799
+ if (r < 0) {
800
+ if (errno == EINTR) continue;
801
+ break;
802
+ }
803
+ if (r == 0) break;
804
+ got += (size_t)r;
805
+ }
806
+ if (got == (size_t)st.st_size) {
807
+ free(g_argv_json);
808
+ g_argv_json = copy;
809
+ g_argv_len = got;
810
+ } else {
811
+ free(copy);
812
+ }
813
+ }
814
+ }
815
+ if (fd >= 0) close(fd);
816
+ }
817
+ }
818
+
819
+ NOI static void write_warn_cet(void) {
820
+ char path[4200];
821
+ int fd;
822
+ static const char msg[] =
823
+ "cet: shadow stack enabled; return values not recorded\n";
824
+ if (join2(path, sizeof path, g_proc, "warn.txt") != 0) return;
825
+ fd = open(path, O_WRONLY | O_CREAT | O_APPEND | O_CLOEXEC, 0600);
826
+ if (fd < 0) return;
827
+ (void)write_full(fd, msg, sizeof msg - 1);
828
+ close(fd);
829
+ }
830
+
831
+ NOI static void cet_check(void) {
832
+ unsigned long status = 0;
833
+ long rc = syscall(SYS_arch_prctl, ARCH_SHSTK_STATUS_, &status);
834
+ /* EINVAL means the kernel has no shadow-stack prctl. CET is off. */
835
+ if (rc == 0 && (status & 1ul)) {
836
+ atomic_store_explicit(&g_cet, 1, memory_order_relaxed);
837
+ write_warn_cet();
838
+ }
839
+ }
840
+
841
+ NOI static void write_exit_status(int code) {
842
+ char path[4200];
843
+ char body[64];
844
+ char *p, *end;
845
+ int fd;
846
+ if (join2(path, sizeof path, g_proc, "exit.json") != 0) return;
847
+ fd = open(path, O_WRONLY | O_CREAT | O_EXCL | O_CLOEXEC, 0600);
848
+ if (fd < 0) return;
849
+ p = body;
850
+ end = body + sizeof body;
851
+ p = ap_str(p, end, "{\"status\": ");
852
+ if (code < 0) {
853
+ if (p < end) *p++ = '-';
854
+ p = ap_u(p, end, (unsigned long)(-(long)code));
855
+ } else {
856
+ p = ap_u(p, end, (unsigned long)code);
857
+ }
858
+ p = ap_str(p, end, "}\n");
859
+ (void)write_full(fd, body, (size_t)(p - body));
860
+ close(fd);
861
+ }
862
+
863
+ NOI static void write_exit_signal(int sig) {
864
+ char path[4200];
865
+ char body[64];
866
+ char *p, *end;
867
+ int fd;
868
+ if (join2(path, sizeof path, g_proc, "exit.json") != 0) return;
869
+ fd = open(path, O_WRONLY | O_CREAT | O_EXCL | O_CLOEXEC, 0600);
870
+ if (fd < 0) return;
871
+ p = body;
872
+ end = body + sizeof body;
873
+ p = ap_str(p, end, "{\"signal\": ");
874
+ p = ap_u(p, end, (unsigned long)sig);
875
+ p = ap_str(p, end, "}\n");
876
+ (void)write_full(fd, body, (size_t)(p - body));
877
+ close(fd);
878
+ }
879
+
880
+ NOI static void write_threads_json(void) {
881
+ char path[4200];
882
+ char body[8192];
883
+ char *p, *end;
884
+ unsigned n, i;
885
+ int first = 1;
886
+ int fd;
887
+ if (join2(path, sizeof path, g_proc, "threads.json") != 0) return;
888
+ p = body;
889
+ end = body + sizeof body - 8;
890
+ p = ap_str(p, end, "{\"started\": ");
891
+ p = ap_u(p, end, (unsigned long)atomic_load_explicit(&g_started, memory_order_relaxed));
892
+ p = ap_str(p, end, ", \"live\": [");
893
+ n = atomic_load_explicit(&g_nreg, memory_order_acquire);
894
+ if (n > MAX_THREADS) n = MAX_THREADS;
895
+ for (i = 0; i < n; i++) {
896
+ uintptr_t bits = atomic_load_explicit(&g_reg[i], memory_order_acquire);
897
+ struct tstate *t;
898
+ if (!bits) continue;
899
+ t = (struct tstate *)bits;
900
+ if (atomic_load_explicit(&t->dead, memory_order_acquire)) continue;
901
+ if (!first) p = ap_str(p, end, ", ");
902
+ if (p >= end - 16) break;
903
+ first = 0;
904
+ p = ap_u(p, end, t->tid);
905
+ }
906
+ p = ap_str(p, end, "]}\n");
907
+ fd = open(path, O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, 0600);
908
+ if (fd < 0) return;
909
+ (void)write_full(fd, body, (size_t)(p - body));
910
+ close(fd);
911
+ }
912
+
913
+ NOI static void write_forks_json(void) {
914
+ char path[4200];
915
+ char body[4096];
916
+ char *p, *end;
917
+ int i, fd;
918
+ if (join2(path, sizeof path, g_proc, "forks.json") != 0) return;
919
+ p = body;
920
+ end = body + sizeof body - 4;
921
+ p = ap_str(p, end, "[");
922
+ for (i = 0; i < g_nkids; i++) {
923
+ if (i) p = ap_str(p, end, ", ");
924
+ if (p >= end - 24) break;
925
+ p = ap_u(p, end, (unsigned long)g_kids[i]);
926
+ }
927
+ p = ap_str(p, end, "]\n");
928
+ fd = open(path, O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, 0600);
929
+ if (fd < 0) return;
930
+ (void)write_full(fd, body, (size_t)(p - body));
931
+ close(fd);
932
+ }
933
+
934
+ /* ---- threads -------------------------------------------------------- */
935
+
936
+ NOI static void open_thread_file(struct tstate *t) {
937
+ char path[4200];
938
+ char tail[64];
939
+ char *q, *end;
940
+ unsigned char hdr[8];
941
+ int fd;
942
+ q = tail;
943
+ end = tail + sizeof tail;
944
+ q = ap_str(q, end, "threads/t");
945
+ q = ap_u(q, end, t->tid);
946
+ q = ap_str(q, end, ".bin");
947
+ if (q >= end) return;
948
+ *q = 0;
949
+ if (join2(path, sizeof path, g_proc, tail) != 0) return;
950
+ fd = open(path, O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, 0600);
951
+ if (fd < 0) return;
952
+ memcpy(hdr, "SCR1", 4);
953
+ memcpy(hdr + 4, &t->tid, 4);
954
+ if (write_full(fd, hdr, 8) != 0) {
955
+ close(fd);
956
+ return;
957
+ }
958
+ t->fd = fd;
959
+ }
960
+
961
+ NOI static void thread_dead(void *p) {
962
+ struct tstate *t = p;
963
+ if (!t) return;
964
+ flush_claim(t);
965
+ atomic_store_explicit(&t->dead, 1, memory_order_release);
966
+ }
967
+
968
+ NOI static struct tstate *me(void) {
969
+ struct tstate *t;
970
+ unsigned slot;
971
+ uint32_t tid;
972
+ if (ts_) return ts_;
973
+ t = calloc(1, sizeof *t);
974
+ if (!t) return NULL;
975
+ tid = atomic_fetch_add_explicit(&g_next_tid, 1, memory_order_relaxed);
976
+ t->tid = tid;
977
+ t->fd = -1;
978
+ if (tid != 1)
979
+ atomic_fetch_add_explicit(&g_started, 1, memory_order_relaxed);
980
+ open_thread_file(t);
981
+ ts_ = t;
982
+ slot = atomic_fetch_add_explicit(&g_nreg, 1, memory_order_relaxed);
983
+ if (slot < MAX_THREADS)
984
+ atomic_store_explicit(&g_reg[slot], (uintptr_t)t, memory_order_release);
985
+ if (g_key_ok) pthread_setspecific(g_key, t);
986
+ return t;
987
+ }
988
+
989
+ NOI static void flush_all(void) {
990
+ unsigned n, i;
991
+ n = atomic_load_explicit(&g_nreg, memory_order_acquire);
992
+ if (n > MAX_THREADS) n = MAX_THREADS;
993
+ for (i = 0; i < n; i++) {
994
+ uintptr_t bits = atomic_load_explicit(&g_reg[i], memory_order_acquire);
995
+ if (!bits) continue;
996
+ flush_claim((struct tstate *)bits);
997
+ }
998
+ }
999
+
1000
+ /* ---- signals -------------------------------------------------------- */
1001
+
1002
+ NOI static void on_fatal(int sig, siginfo_t *si, void *uc_) {
1003
+ static atomic_int in_handler;
1004
+ ucontext_t *uc = uc_;
1005
+ unsigned char stackbuf[STACK_BYTES];
1006
+ uint64_t rip = 0, rsp = 0, addr = 0;
1007
+ uint64_t code = 0;
1008
+ uint32_t elen = 0;
1009
+ size_t got;
1010
+ if (atomic_exchange_explicit(&in_handler, 1, memory_order_acq_rel)) {
1011
+ signal(sig, SIG_DFL);
1012
+ raise(sig);
1013
+ return;
1014
+ }
1015
+ atomic_store_explicit(&g_crashing, 1, memory_order_release);
1016
+ if (si) {
1017
+ addr = (uint64_t)(uintptr_t)si->si_addr;
1018
+ code = (uint64_t)(int64_t)si->si_code;
1019
+ }
1020
+ if (uc) {
1021
+ rip = (uint64_t)uc->uc_mcontext.gregs[REG_RIP];
1022
+ rsp = (uint64_t)uc->uc_mcontext.gregs[REG_RSP];
1023
+ }
1024
+ if (rsp) {
1025
+ got = copy_mem(stackbuf, (uintptr_t)rsp, STACK_BYTES);
1026
+ if (got == STACK_BYTES) elen = STACK_BYTES;
1027
+ }
1028
+ if (ts_)
1029
+ emit(ts_, (uint8_t)'S', (uint64_t)sig, addr, rip, code,
1030
+ elen ? stackbuf : NULL, elen);
1031
+ flush_all();
1032
+ write_exit_signal(sig);
1033
+ write_threads_json();
1034
+ signal(sig, SIG_DFL);
1035
+ raise(sig);
1036
+ }
1037
+
1038
+ /* ---- hooks ---------------------------------------------------------- */
1039
+
1040
+ NOI void __cyg_profile_func_enter(void *fn, void *site) {
1041
+ struct tstate *t;
1042
+ struct hit h;
1043
+ void *fpv;
1044
+ uint64_t *fp = NULL;
1045
+ uint64_t cfa = 0;
1046
+ uint32_t blen;
1047
+ volatile uint64_t *slot;
1048
+ uint64_t cur;
1049
+ if (!atomic_load_explicit(&g_on, memory_order_acquire)) return;
1050
+ t = me();
1051
+ if (!t) return;
1052
+ fpv = __builtin_frame_address(1);
1053
+ if (fpv) {
1054
+ fp = fpv;
1055
+ cfa = (uint64_t)(uintptr_t)fp + 16;
1056
+ }
1057
+ if (!find_hit((uint64_t)(uintptr_t)fn, &h)) {
1058
+ scan_phdrs();
1059
+ (void)find_hit((uint64_t)(uintptr_t)fn, &h);
1060
+ }
1061
+ blen = build_blob(&h, fp != NULL, cfa, t->scratch, sizeof t->scratch);
1062
+ emit(t, (uint8_t)'E', (uint64_t)(uintptr_t)fn, (uint64_t)(uintptr_t)site,
1063
+ cfa, 0, t->scratch, blen);
1064
+ if (!fp) return;
1065
+ if (atomic_load_explicit(&g_cet, memory_order_relaxed)) return;
1066
+ if (g_no_retval) return;
1067
+ if (t->depth >= SHADOW_MAX) return;
1068
+ slot = (volatile uint64_t *)(fp + 1);
1069
+ cur = *slot;
1070
+ if (cur == (uint64_t)(uintptr_t)sensorium_ret_tramp) return;
1071
+ t->shadow[t->depth].slot = (uint64_t *)(uintptr_t)(fp + 1);
1072
+ t->shadow[t->depth].orig = cur;
1073
+ t->shadow[t->depth].rbp = (uint64_t)(uintptr_t)fp;
1074
+ t->shadow[t->depth].ret_class = h.found ? h.ret_class : 0;
1075
+ t->shadow[t->depth].ret_size = h.found ? h.ret_size : 0;
1076
+ t->depth++;
1077
+ *slot = (uint64_t)(uintptr_t)sensorium_ret_tramp;
1078
+ }
1079
+
1080
+ NOI void __cyg_profile_func_exit(void *fn, void *site) {
1081
+ struct tstate *t;
1082
+ if (!atomic_load_explicit(&g_on, memory_order_acquire)) return;
1083
+ t = me();
1084
+ if (!t) return;
1085
+ emit(t, (uint8_t)'X', (uint64_t)(uintptr_t)fn, (uint64_t)(uintptr_t)site,
1086
+ 0, 0, NULL, 0);
1087
+ }
1088
+
1089
+ NOI __attribute__((used, noinline)) uint64_t
1090
+ sensorium_ret_hook(uint64_t rax, uint64_t rdx, uint64_t xmm0, uint64_t xmm1) {
1091
+ struct tstate *t = ts_;
1092
+ uint64_t orig = 0;
1093
+ uint8_t cls = 0;
1094
+ uint16_t rsz = 0;
1095
+ unsigned char mem[256];
1096
+ uint32_t elen = 0;
1097
+ if (t && t->depth > 0) {
1098
+ struct shadow_ent *e = &t->shadow[--t->depth];
1099
+ orig = e->orig;
1100
+ cls = e->ret_class;
1101
+ rsz = e->ret_size;
1102
+ }
1103
+ if (cls == RET_MEM && rsz > 0 && rax != 0) {
1104
+ uint16_t n = rsz > 256 ? 256 : rsz;
1105
+ size_t got = copy_mem(mem, (uintptr_t)rax, n);
1106
+ if (got) elen = (uint32_t)got;
1107
+ }
1108
+ if (t && atomic_load_explicit(&g_on, memory_order_relaxed))
1109
+ emit(t, (uint8_t)'V', rax, rdx, xmm0, xmm1, elen ? mem : NULL, elen);
1110
+ return orig;
1111
+ }
1112
+
1113
+ __asm__(
1114
+ ".text\n"
1115
+ ".globl sensorium_ret_tramp\n"
1116
+ ".type sensorium_ret_tramp,@function\n"
1117
+ "sensorium_ret_tramp:\n"
1118
+ " endbr64\n"
1119
+ " sub $48, %rsp\n"
1120
+ " mov %rax, 0(%rsp)\n"
1121
+ " mov %rdx, 8(%rsp)\n"
1122
+ " movq %xmm0, 16(%rsp)\n"
1123
+ " movq %xmm1, 24(%rsp)\n"
1124
+ " mov %rax, %rdi\n"
1125
+ " mov %rdx, %rsi\n"
1126
+ " movq %xmm0, %rdx\n"
1127
+ " movq %xmm1, %rcx\n"
1128
+ " call sensorium_ret_hook\n"
1129
+ " mov %rax, %r11\n"
1130
+ " mov 0(%rsp), %rax\n"
1131
+ " mov 8(%rsp), %rdx\n"
1132
+ " movq 16(%rsp), %xmm0\n"
1133
+ " movq 24(%rsp), %xmm1\n"
1134
+ " add $48, %rsp\n"
1135
+ " jmp *%r11\n"
1136
+ ".size sensorium_ret_tramp, .-sensorium_ret_tramp\n"
1137
+ ".pushsection .note.GNU-stack,\"\",@progbits\n"
1138
+ ".popsection\n");
1139
+
1140
+ /* ---- longjmp -------------------------------------------------------- */
1141
+
1142
+ NOI static uint64_t demangle_ptr(uint64_t v) {
1143
+ uint64_t guard;
1144
+ __asm__ volatile("movq %%fs:0x30, %0" : "=r"(guard));
1145
+ v = (v >> 17) | (v << (64 - 17));
1146
+ return v ^ guard;
1147
+ }
1148
+
1149
+ NOI static void repair_longjmp(struct __jmp_buf_tag *env) {
1150
+ struct tstate *t;
1151
+ uint64_t rbp, rsp, pc;
1152
+ int abandoned = 0;
1153
+ uint64_t *jb;
1154
+ if (!env) return;
1155
+ jb = (uint64_t *)env->__jmpbuf;
1156
+ rbp = demangle_ptr(jb[1]);
1157
+ rsp = demangle_ptr(jb[6]);
1158
+ pc = demangle_ptr(jb[7]);
1159
+ t = ts_;
1160
+ if (t) {
1161
+ /* Stack grows down: abandoned callees have a smaller rbp than
1162
+ * the setjmp frame. Leave the setjmp frame itself on the shadow
1163
+ * so its later normal return still hits the trampoline once. */
1164
+ while (t->depth > 0 && t->shadow[t->depth - 1].rbp < rbp) {
1165
+ struct shadow_ent *e = &t->shadow[t->depth - 1];
1166
+ if (e->slot) *e->slot = e->orig;
1167
+ t->depth--;
1168
+ abandoned++;
1169
+ }
1170
+ emit(t, (uint8_t)'J', rbp, rsp, pc, (uint64_t)abandoned, NULL, 0);
1171
+ }
1172
+ }
1173
+
1174
+ /* A fortify header may still have macro-substituted these names. */
1175
+ #undef longjmp
1176
+ #undef _longjmp
1177
+ #undef siglongjmp
1178
+
1179
+ NOI __attribute__((noreturn)) void longjmp(struct __jmp_buf_tag env[1], int val) {
1180
+ resolve_syms();
1181
+ if (atomic_load_explicit(&g_on, memory_order_acquire)) repair_longjmp(env);
1182
+ if (real_longjmp) real_longjmp(env, val);
1183
+ abort();
1184
+ }
1185
+
1186
+ NOI __attribute__((noreturn)) void _longjmp(struct __jmp_buf_tag env[1], int val) {
1187
+ resolve_syms();
1188
+ if (atomic_load_explicit(&g_on, memory_order_acquire)) repair_longjmp(env);
1189
+ if (real__longjmp) real__longjmp(env, val);
1190
+ else if (real_longjmp) real_longjmp(env, val);
1191
+ abort();
1192
+ }
1193
+
1194
+ NOI __attribute__((noreturn)) void siglongjmp(sigjmp_buf env, int val) {
1195
+ resolve_syms();
1196
+ if (atomic_load_explicit(&g_on, memory_order_acquire))
1197
+ repair_longjmp((struct __jmp_buf_tag *)env);
1198
+ if (real_siglongjmp) real_siglongjmp((struct __jmp_buf_tag *)env, val);
1199
+ else if (real_longjmp) real_longjmp((struct __jmp_buf_tag *)env, val);
1200
+ abort();
1201
+ }
1202
+
1203
+ NOI __attribute__((noreturn)) void __longjmp_chk(struct __jmp_buf_tag env[1], int val) {
1204
+ resolve_syms();
1205
+ if (atomic_load_explicit(&g_on, memory_order_acquire)) repair_longjmp(env);
1206
+ if (real___longjmp_chk) real___longjmp_chk(env, val);
1207
+ else if (real_longjmp) real_longjmp(env, val);
1208
+ abort();
1209
+ }
1210
+
1211
+ /* ---- assert --------------------------------------------------------- */
1212
+
1213
+ NOI static size_t clip_str(unsigned char *dst, const char *src, size_t cap) {
1214
+ size_t i = 0;
1215
+ if (!src) return 0;
1216
+ while (i < cap && src[i]) {
1217
+ dst[i] = (unsigned char)src[i];
1218
+ i++;
1219
+ }
1220
+ return i;
1221
+ }
1222
+
1223
+ NOI __attribute__((noreturn)) void
1224
+ __assert_fail(const char *assertion, const char *file, unsigned line,
1225
+ const char *function) {
1226
+ struct tstate *t;
1227
+ unsigned char extra[ASSERT_CLIP * 3 + 8];
1228
+ size_t n = 0, c;
1229
+ resolve_syms();
1230
+ if (atomic_load_explicit(&g_on, memory_order_acquire)) {
1231
+ t = ts_ ? ts_ : me();
1232
+ c = clip_str(extra, assertion, ASSERT_CLIP);
1233
+ n = c;
1234
+ extra[n++] = 0;
1235
+ c = clip_str(extra + n, file, ASSERT_CLIP);
1236
+ n += c;
1237
+ extra[n++] = 0;
1238
+ c = clip_str(extra + n, function, ASSERT_CLIP);
1239
+ n += c;
1240
+ if (t) {
1241
+ emit(t, (uint8_t)'A', (uint64_t)line, 0, 0, 0, extra, (uint32_t)n);
1242
+ flush_claim(t);
1243
+ }
1244
+ }
1245
+ if (real_assert_fail) real_assert_fail(assertion, file, line, function);
1246
+ abort();
1247
+ }
1248
+
1249
+ /* ---- fork ----------------------------------------------------------- */
1250
+
1251
+ NOI static void replay_file(const char *name, const void *buf, size_t n) {
1252
+ char path[4200];
1253
+ int fd;
1254
+ if (!buf || !n) return;
1255
+ if (join2(path, sizeof path, g_proc, name) != 0) return;
1256
+ fd = open(path, O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, 0600);
1257
+ if (fd < 0) return;
1258
+ (void)write_full(fd, buf, n);
1259
+ close(fd);
1260
+ }
1261
+
1262
+ NOI static void child_switch(void) {
1263
+ pid_t pid = getpid();
1264
+ struct tstate *self;
1265
+ unsigned n, i;
1266
+ if (!atomic_load_explicit(&g_on, memory_order_relaxed)) return;
1267
+ if (pid == g_proc_pid) return;
1268
+ self = ts_;
1269
+ n = atomic_load_explicit(&g_nreg, memory_order_relaxed);
1270
+ if (n > MAX_THREADS) n = MAX_THREADS;
1271
+ for (i = 0; i < n; i++) {
1272
+ uintptr_t bits = atomic_load_explicit(&g_reg[i], memory_order_relaxed);
1273
+ struct tstate *t;
1274
+ if (!bits) continue;
1275
+ t = (struct tstate *)bits;
1276
+ if (t->fd >= 0) {
1277
+ close(t->fd);
1278
+ t->fd = -1;
1279
+ }
1280
+ if (t != self) {
1281
+ atomic_store_explicit(&t->dead, 1, memory_order_relaxed);
1282
+ atomic_store_explicit(&g_reg[i], 0, memory_order_relaxed);
1283
+ }
1284
+ }
1285
+ /* Drop the copied buffer. Those bytes were flushed to the parent. */
1286
+ if (self) {
1287
+ self->len = 0;
1288
+ atomic_store_explicit(&self->pub_len, 0, memory_order_relaxed);
1289
+ atomic_store_explicit(&self->disk_len, 0, memory_order_relaxed);
1290
+ }
1291
+ atomic_store_explicit(&g_started, 0, memory_order_relaxed);
1292
+ g_nkids = 0;
1293
+ if (ensure_dirs(pid) != 0) {
1294
+ atomic_store_explicit(&g_on, 0, memory_order_relaxed);
1295
+ return;
1296
+ }
1297
+ write_boot();
1298
+ replay_file("argv.json", g_argv_json, g_argv_len);
1299
+ replay_file("objs.txt", g_objs_txt, g_objs_len);
1300
+ if (self) open_thread_file(self);
1301
+ }
1302
+
1303
+ NOI static void atfork_prepare(void) {
1304
+ if (ts_) flush_claim(ts_);
1305
+ }
1306
+
1307
+ NOI static void atfork_parent(void) {}
1308
+
1309
+ NOI static void atfork_child(void) { child_switch(); }
1310
+
1311
+ NOI pid_t fork(void) {
1312
+ pid_t pid;
1313
+ struct tstate *t;
1314
+ resolve_syms();
1315
+ if (!atomic_load_explicit(&g_on, memory_order_acquire) || !real_fork) {
1316
+ if (!real_fork) {
1317
+ errno = ENOSYS;
1318
+ return -1;
1319
+ }
1320
+ return real_fork();
1321
+ }
1322
+ t = ts_ ? ts_ : me();
1323
+ if (t) flush_claim(t);
1324
+ pid = real_fork();
1325
+ if (pid == 0) {
1326
+ child_switch(); /* idempotent if the atfork child handler ran */
1327
+ return 0;
1328
+ }
1329
+ if (pid > 0) {
1330
+ if (g_nkids < (int)(sizeof g_kids / sizeof g_kids[0]))
1331
+ g_kids[g_nkids++] = pid;
1332
+ write_forks_json();
1333
+ if (t) emit(t, (uint8_t)'F', (uint64_t)pid, 0, 0, 0, NULL, 0);
1334
+ }
1335
+ return pid;
1336
+ }
1337
+
1338
+ /* ---- pthread_create ------------------------------------------------- */
1339
+
1340
+ struct start_pack {
1341
+ void *(*fn)(void *);
1342
+ void *arg;
1343
+ };
1344
+
1345
+ NOI static void *thread_main(void *p) {
1346
+ struct start_pack pack = *(struct start_pack *)p;
1347
+ free(p);
1348
+ if (atomic_load_explicit(&g_on, memory_order_acquire)) (void)me();
1349
+ return pack.fn(pack.arg);
1350
+ }
1351
+
1352
+ NOI int pthread_create(pthread_t *thread, const pthread_attr_t *attr,
1353
+ void *(*start)(void *), void *arg) {
1354
+ struct start_pack *pack;
1355
+ int rc;
1356
+ resolve_syms();
1357
+ if (!atomic_load_explicit(&g_on, memory_order_acquire) || !real_pthread_create) {
1358
+ if (!real_pthread_create) return EAGAIN;
1359
+ return real_pthread_create(thread, attr, start, arg);
1360
+ }
1361
+ pack = malloc(sizeof *pack);
1362
+ if (!pack) return EAGAIN;
1363
+ pack->fn = start;
1364
+ pack->arg = arg;
1365
+ rc = real_pthread_create(thread, attr, thread_main, pack);
1366
+ if (rc != 0) free(pack);
1367
+ return rc;
1368
+ }
1369
+
1370
+ /* ---- startup / shutdown --------------------------------------------- */
1371
+
1372
+ NOI static void resolve_syms(void) {
1373
+ int exp = 0;
1374
+ int st = atomic_load_explicit(&g_resolved, memory_order_acquire);
1375
+ if (st == 2) return;
1376
+ if (!atomic_compare_exchange_strong_explicit(
1377
+ &g_resolved, &exp, 1, memory_order_acq_rel, memory_order_acquire)) {
1378
+ while (atomic_load_explicit(&g_resolved, memory_order_acquire) != 2)
1379
+ pause_once();
1380
+ return;
1381
+ }
1382
+ real_longjmp = (lj_fn)dlsym(RTLD_NEXT, "longjmp");
1383
+ real__longjmp = (lj_fn)dlsym(RTLD_NEXT, "_longjmp");
1384
+ real_siglongjmp = (lj_fn)dlsym(RTLD_NEXT, "siglongjmp");
1385
+ real___longjmp_chk = (lj_fn)dlsym(RTLD_NEXT, "__longjmp_chk");
1386
+ real_assert_fail = (assert_fn)dlsym(RTLD_NEXT, "__assert_fail");
1387
+ real_fork = (fork_fn)dlsym(RTLD_NEXT, "fork");
1388
+ real_pthread_create = (thr_fn)dlsym(RTLD_NEXT, "pthread_create");
1389
+ atomic_store_explicit(&g_resolved, 2, memory_order_release);
1390
+ }
1391
+
1392
+ NOI static void on_exit_fn(int code, void *unused) {
1393
+ (void)unused;
1394
+ if (!atomic_load_explicit(&g_on, memory_order_relaxed)) return;
1395
+ if (atomic_load_explicit(&g_crashing, memory_order_relaxed)) return;
1396
+ write_exit_status(code);
1397
+ }
1398
+
1399
+ NOI static void at_exit_fn(void) {
1400
+ if (!atomic_load_explicit(&g_on, memory_order_relaxed)) return;
1401
+ if (ts_) flush_claim(ts_);
1402
+ flush_all();
1403
+ write_threads_json();
1404
+ }
1405
+
1406
+ NOI __attribute__((constructor, no_instrument_function)) static void boot(void) {
1407
+ const char *root;
1408
+ ssize_t n;
1409
+ struct sigaction sa;
1410
+ int sigs[5];
1411
+ unsigned i;
1412
+ resolve_syms();
1413
+ root = getenv("SENSORIUM_C_ROOT");
1414
+ if (!root || !root[0]) return;
1415
+ if (strlen(root) >= sizeof g_root) return;
1416
+ memcpy(g_root, root, strlen(root) + 1);
1417
+ {
1418
+ const char *nr = getenv("SENSORIUM_C_NO_RETVAL");
1419
+ const char *rv = getenv("SENSORIUM_C_RETVAL");
1420
+ if ((nr && nr[0] && strcmp(nr, "0") != 0) || (rv && strcmp(rv, "0") == 0))
1421
+ g_no_retval = 1;
1422
+ }
1423
+ n = readlink("/proc/self/exe", g_exe, sizeof g_exe - 1);
1424
+ if (n < 0) n = 0;
1425
+ g_exe[n] = 0;
1426
+ if (ensure_dirs(getpid()) != 0) return;
1427
+ write_argv();
1428
+ write_boot();
1429
+ cet_check();
1430
+ scan_phdrs();
1431
+ memset(&sa, 0, sizeof sa);
1432
+ sa.sa_sigaction = on_fatal;
1433
+ sa.sa_flags = SA_SIGINFO | SA_NODEFER;
1434
+ sigs[0] = SIGSEGV;
1435
+ sigs[1] = SIGABRT;
1436
+ sigs[2] = SIGFPE;
1437
+ sigs[3] = SIGBUS;
1438
+ sigs[4] = SIGILL;
1439
+ for (i = 0; i < 5; i++) sigaction(sigs[i], &sa, NULL);
1440
+ if (pthread_key_create(&g_key, thread_dead) == 0) g_key_ok = 1;
1441
+ pthread_atfork(atfork_prepare, atfork_parent, atfork_child);
1442
+ atexit(at_exit_fn);
1443
+ on_exit(on_exit_fn, NULL);
1444
+ atomic_store_explicit(&g_on, 1, memory_order_release);
1445
+ (void)me();
1446
+ }