Skip to content

Commit 6540dcc

Browse files
committed
Isolated Namespace frame-pointer reporter validation
1 parent d034415 commit 6540dcc

4 files changed

Lines changed: 208 additions & 0 deletions

File tree

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
name: EFF-1872 isolated Namespace frame validation
2+
on:
3+
push:
4+
branches: [validation/eff-1872-namespace-frames]
5+
permissions:
6+
contents: read
7+
jobs:
8+
probe:
9+
runs-on: namespace-profile-linux-default
10+
timeout-minutes: 15
11+
env:
12+
EFFECT_INTEGRATION_TESTS: "1"
13+
TESTCONTAINERS_RYUK_DISABLED: "true"
14+
steps:
15+
- uses: actions/checkout@d23441a48e516b6c34aea4fa41551a30e30af803
16+
- uses: ./.github/actions/setup
17+
with:
18+
deno-version: v2.9.7
19+
- name: Compile and record core-disabled configuration
20+
run: |
21+
set -euo pipefail
22+
ulimit -c 0
23+
gcc --version
24+
deno --version
25+
gcc -O1 -fPIC -shared -Wall -Wextra -o "$RUNNER_TEMP/reporter.so" probe/reporter.c
26+
gcc -O1 -fPIC -shared -DPROBE_STALL -o "$RUNNER_TEMP/stall.so" probe/reporter.c
27+
mkdir -p "$RUNNER_TEMP/traces" "$RUNNER_TEMP/stall-traces"
28+
test "$(ulimit -c)" = "0"
29+
sysctl kernel.core_pattern
30+
- name: Worker abort
31+
run: |
32+
set -euo pipefail
33+
ulimit -c 0
34+
start=$SECONDS
35+
set +e
36+
ABORT_TRACE_DIR="$RUNNER_TEMP/traces" LD_PRELOAD="$RUNNER_TEMP/reporter.so" timeout 90s deno task test run packages/effect/test/FrameReporterProbe.test.ts > "$RUNNER_TEMP/abort.log" 2>&1
37+
code=$?
38+
set -e
39+
echo "worker probe exit=$code elapsed=$((SECONDS-start))s"
40+
cat "$RUNNER_TEMP/abort.log"
41+
test "$code" = 1
42+
grep -q 'signal SIGABRT during started state' "$RUNNER_TEMP/abort.log"
43+
python3 probe/check.py
44+
- name: Forced handler stall validates watchdog
45+
run: |
46+
set -euo pipefail
47+
ulimit -c 0
48+
start=$SECONDS
49+
set +e
50+
ABORT_TRACE_DIR="$RUNNER_TEMP/stall-traces" LD_PRELOAD="$RUNNER_TEMP/stall.so" timeout 15s deno eval 'process.abort()'
51+
code=$?
52+
set -e
53+
elapsed=$((SECONDS-start))
54+
echo "forced stall exit=$code elapsed=4elapsed"
55+
test "$code" = 134
56+
test "$elapsed" -ge 5
57+
test "$elapsed" -lt 12
58+
- name: Normal MSSQL persistence integration
59+
run: |
60+
set -euo pipefail
61+
ulimit -c 0
62+
mkdir -p "$RUNNER_TEMP/normal-traces"
63+
ABORT_TRACE_DIR="$RUNNER_TEMP/normal-traces" LD_PRELOAD="$RUNNER_TEMP/reporter.so" deno task test run packages/sql/mssql/test/Persistence.integration.test.ts
64+
test -z "$(find "$RUNNER_TEMP/normal-traces" -type f -print -quit)"
65+
test ! -f core
66+
- name: Clean local probe data
67+
if: always()
68+
run: rm -rf "$RUNNER_TEMP/traces" "$RUNNER_TEMP/stall-traces" "$RUNNER_TEMP/normal-traces" "$RUNNER_TEMP/reporter.so" "$RUNNER_TEMP/stall.so" "$RUNNER_TEMP/abort.log"
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { test } from "vitest"
2+
import * as process from "node:process"
3+
test("deliberate worker abort preserves the signal", () => {
4+
const marker = "eff1872-private-stack-sentinel-9c327"
5+
process.env.EFF1872_PRIVATE_PROBE = "eff1872-private-env-sentinel-b381a"
6+
;(globalThis as any).probeMarker = marker
7+
process.abort()
8+
})

‎probe/check.py‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import os, pathlib, re, urllib.request
2+
traces = list((pathlib.Path(os.environ['RUNNER_TEMP']) / 'traces').glob('trace.*.6.txt'))
3+
assert len(traces) == 1, len(traces)
4+
s = traces[0].read_text()
5+
assert len(s) < 65536
6+
assert 'eff1872-private-stack-sentinel' not in s
7+
assert 'eff1872-private-env-sentinel' not in s
8+
allowed = r'(signal=|rip=|stack-map |text-map |exec-segment |exe=|--- |fp#|sp\+|/|\[)'
9+
assert all(re.match(allowed, line) for line in s.splitlines())
10+
print(s)
11+
values = [int(v,16)-1 for v in re.findall(r'^fp#.*exe\+(0x[0-9a-f]+)', s, re.M) if int(v,16)>0]
12+
assert len(values) >= 10
13+
alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_'
14+
def encode(v):
15+
n = v << 1
16+
result = ''
17+
while True:
18+
d = n & 31
19+
n >>= 5
20+
result += alphabet[d | (32 if n else 0)]
21+
if not n:
22+
return result
23+
url = 'https://panic.deno.com/v2.9.7/x86_64-unknown-linux-gnu/' + ''.join(map(encode, values))
24+
print('symbolication URL:', url)
25+
req = urllib.request.Request(url, headers={'Accept': 'application/json'})
26+
body = urllib.request.urlopen(req, timeout=60).read().decode()
27+
assert 'op_process_abort' in body, body[:200]
28+
assert 'ext/node/ops/process.rs' in body
29+
print('symbolication verified: op_process_abort, ext/node/ops/process.rs; frames=', len(values), 'trace bytes=',len(s))

‎probe/reporter.c‎

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
// LD_PRELOAD crash reporter probe: on SIGABRT/SIGSEGV/SIGBUS/SIGILL write to
2+
// $ABORT_TRACE_DIR/trace.<pid>.<sig>.txt: siginfo, registers, the executable's text
3+
// mappings (for offset math), libgcc backtrace, a frame-pointer walk, and a scan of
4+
// the stack for return addresses inside the executable. No memory contents beyond
5+
// code addresses are written. Then re-raise with the default action.
6+
#define _GNU_SOURCE
7+
#include <execinfo.h>
8+
#include <fcntl.h>
9+
#include <signal.h>
10+
#include <stdio.h>
11+
#include <stdlib.h>
12+
#include <string.h>
13+
#include <ucontext.h>
14+
#include <unistd.h>
15+
#include <link.h>
16+
17+
static unsigned long exe_base; static unsigned long stack_lo, stack_hi;
18+
static unsigned long seg_lo[16], seg_hi[16]; static int nseg;
19+
static int phdr_cb(struct dl_phdr_info *i, size_t n, void *d) { (void)n; (void)d; exe_base = i->dlpi_addr;
20+
for (int k = 0; k < i->dlpi_phnum && nseg < 16; k++) if (i->dlpi_phdr[k].p_type == PT_LOAD && (i->dlpi_phdr[k].p_flags & PF_X)) {
21+
seg_lo[nseg] = exe_base + i->dlpi_phdr[k].p_vaddr; seg_hi[nseg] = seg_lo[nseg] + i->dlpi_phdr[k].p_memsz; nseg++; }
22+
return 1; }
23+
24+
typedef struct { unsigned long lo, hi, off; } map_t;
25+
static map_t text[16]; static int ntext;
26+
static char exe[512];
27+
28+
static unsigned long cur_rsp;
29+
static void load_maps(int fd) {
30+
int mfd = open("/proc/self/maps", O_RDONLY);
31+
if (mfd < 0) return;
32+
static char buf[1 << 16]; ssize_t n = 0, r;
33+
while (n < (ssize_t)sizeof buf - 1 && (r = read(mfd, buf + n, sizeof buf - 1 - n)) > 0) n += r;
34+
close(mfd); buf[n] = 0;
35+
readlink("/proc/self/exe", exe, sizeof exe - 1);
36+
char *line = buf;
37+
while (line && *line) {
38+
char *nl = strchr(line, '\n'); if (nl) *nl = 0;
39+
unsigned long lo, hi, off; char perms[8];
40+
if (sscanf(line, "%lx-%lx %7s %lx", &lo, &hi, perms, &off) == 4 && cur_rsp >= lo && cur_rsp < hi) { stack_lo = lo; stack_hi = hi; dprintf(fd, "stack-map %s\n", line); }
41+
if (sscanf(line, "%lx-%lx %7s %lx", &lo, &hi, perms, &off) == 4 && perms[2] == 'x' && strstr(line, exe) && ntext < 16) {
42+
text[ntext].lo = lo; text[ntext].hi = hi; text[ntext].off = off; ntext++;
43+
dprintf(fd, "text-map %s\n", line);
44+
}
45+
line = nl ? nl + 1 : NULL;
46+
}
47+
}
48+
static long exe_off(unsigned long a) {
49+
for (int i = 0; i < nseg; i++) if (a >= seg_lo[i] && a < seg_hi[i]) return (long)(a - exe_base);
50+
return -1;
51+
}
52+
53+
// Probe-only watchdog recommended in the design, preserving SIGABRT on expiry.
54+
static void watchdog(int unused) {
55+
(void)unused;
56+
signal(SIGABRT, SIG_DFL);
57+
sigset_t set; sigemptyset(&set); sigaddset(&set, SIGABRT);
58+
sigprocmask(SIG_UNBLOCK, &set, NULL);
59+
raise(SIGABRT);
60+
_exit(134);
61+
}
62+
static void handler(int sig, siginfo_t *info, void *ctx) {
63+
alarm(5);
64+
#ifdef PROBE_STALL
65+
sleep(20);
66+
#endif
67+
const char *dir = getenv("ABORT_TRACE_DIR");
68+
if (dir) {
69+
char path[600]; snprintf(path, sizeof path, "%s/trace.%d.%d.txt", dir, (int)getpid(), sig);
70+
int fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0600);
71+
if (fd >= 0) {
72+
ucontext_t *uc = (ucontext_t *)ctx;
73+
unsigned long rip = uc->uc_mcontext.gregs[REG_RIP], rsp = uc->uc_mcontext.gregs[REG_RSP], rbp = uc->uc_mcontext.gregs[REG_RBP];
74+
dprintf(fd, "signal=%d si_code=%d si_pid=%d pid=%d tid=%ld\n", sig, info->si_code, (int)info->si_pid, (int)getpid(), (long)gettid());
75+
dprintf(fd, "rip=%#lx rsp=%#lx rbp=%#lx\n", rip, rsp, rbp);
76+
cur_rsp = rsp; load_maps(fd);
77+
for (int i = 0; i < nseg; i++) dprintf(fd, "exec-segment %#lx-%#lx (vaddr %#lx)\n", seg_lo[i], seg_hi[i], seg_lo[i] - exe_base);
78+
dprintf(fd, "exe=%s base=%#lx (offsets below are vaddr-relative, same convention as backtrace_symbols)\n", exe, exe_base);
79+
dprintf(fd, "--- libgcc backtrace\n");
80+
void *frames[64]; int cnt = backtrace(frames, 64); backtrace_symbols_fd(frames, cnt, fd);
81+
dprintf(fd, "--- frame-pointer walk\n");
82+
unsigned long *fp = (unsigned long *)rbp;
83+
for (int i = 0; i < 64 && fp && (unsigned long)fp > rsp && (unsigned long)fp < rsp + (64UL << 20); i++) {
84+
unsigned long ret = fp[1]; long off = exe_off(ret);
85+
dprintf(fd, "fp#%d ret=%#lx exe+%#lx\n", i, ret, off < 0 ? 0UL : (unsigned long)off);
86+
unsigned long *next = (unsigned long *)fp[0]; if (next <= fp) break; fp = next;
87+
}
88+
dprintf(fd, "--- stack scan (return addresses inside the executable, rsp upwards, 64 KiB)\n");
89+
unsigned long *sp = (unsigned long *)(rsp & ~7UL);
90+
unsigned long limit = stack_hi ? (stack_hi - (unsigned long)sp) / 8 : 0; if (limit > 8192) limit = 8192;
91+
for (unsigned long i = 0; i < limit; i++) { long off = exe_off(sp[i]); if (off >= 0) dprintf(fd, "sp+%#x exe+%#lx\n", i * 8, (unsigned long)off); }
92+
close(fd);
93+
}
94+
}
95+
signal(sig, SIG_DFL); raise(sig);
96+
}
97+
__attribute__((constructor)) static void init(void) {
98+
struct sigaction sa; memset(&sa, 0, sizeof sa);
99+
sa.sa_sigaction = handler; sa.sa_flags = SA_SIGINFO | SA_ONSTACK | SA_RESETHAND;
100+
signal(SIGALRM, watchdog);
101+
dl_iterate_phdr(phdr_cb, NULL);
102+
sigaction(SIGABRT, &sa, NULL); sigaction(SIGSEGV, &sa, NULL); sigaction(SIGBUS, &sa, NULL); sigaction(SIGILL, &sa, NULL);
103+
}

0 commit comments

Comments
 (0)