Files
scriptc/tests/library-mode/thread-instances/probe_inspect.c
Chris Tate 6f7a1a08fe feat: support thread-instanced library state (#137)
* feat: support thread-instanced library state

- Add the abi.instance_per_thread profile field: the archive's TUs compile
  with -DSCR_THREAD_INSTANCES, and the SCR_TL qualifier in scr_runtime.h
  moves every runtime unit's mutable statics into thread-local storage;
  both backends emit the program TU's module globals, run-once guards, and
  lazily-compiled regex literal caches thread-local to match
- ONE linked archive then serves one independent instance per embedder
  thread through the unchanged entry family: a thread registers its sink
  and calls the init entry, and owns its own collector, result arena,
  poison flag, and program state — a trap poisons only the instance it
  fired in while sibling threads' instances keep answering
- Document the contract beside the profile spec: one instance per thread,
  selected implicitly by the calling thread; instance lifetime is the
  thread's lifetime; the never-entered-from-two-threads rule is unchanged;
  independent of and composable with abi.localize_runtime
- Immutable interned data (string literals, unit arms, template arrays,
  vtables) stays shared — Darwin ASan's image-registration common
  included, keeping the one-registration-per-image discipline; the
  exception cell's current pointer resolves a NULL sentinel in this mode
  because a thread-local address is not a constant initializer
- Add the four-thread acceptance probe (distinct per-thread workloads,
  concurrent instance-local inits, per-instance collects, a trap delivered
  to its own thread's sink exactly once), the composition probe pairing a
  thread-instanced localized archive with a second different-prefix
  localized archive, an explicit ASan rerun beside the suite-flavor
  sanitized builds, and profile-shape coverage; schedule the new lane
  contracts beside M1/M2
- Non-opted builds are byte-for-byte unchanged: the qualifier expands to
  nothing outside -DSCR_LIB -DSCR_THREAD_INSTANCES (verified object-level
  over every touched runtime TU in both default and library flavors)

* fix: keep sanitized runtime-localized archives linkable on ELF

ASan's instrumented globals ride ELF section groups; archives built from
shared runtime objects carry groups with REPEATED signatures, so a process
linking two runtime-localized sanitized archives kept one archive's group
and discarded the other's — whose now-local references then dangled at the
embedder's link. Resolving the groups into the combined relocatable member
(ld -r --force-group-allocation) keeps every archive's copies; the
localization step then demotes them per archive exactly like unsanitized
state. Plain builds carry no section groups, so the flag is inert there.

With the groups resolved, ELF surfaces the same deliberate exception
Mach-O already documented: the image-wide registration guard COMMON stays
shared so the final image registers its ASan globals exactly once — M1/M7
now pin that spelling on both platforms. The explicit ASan pairing (M8)
additionally points Linux LSan away from contractually thread-lifetime
instance state, exactly as the sanitized suite lanes do.

* fix: isolate inspect state per thread

* fix(runtime): share uptime anchor across threads
2026-08-12 09:49:43 -05:00

68 lines
1.9 KiB
C

/* Deterministic regression for util.inspect's thread-instanced circular
* target map. The inspect stack and target count were already TLS; the
* target pointer array must be TLS too. Thread A records its first target,
* thread B then records a different target in its own slot zero, and A
* must still resolve its target as circular id 1. A shared backing array
* makes A lose that entry and assign id 2 instead. */
#include <pthread.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
extern void scr_insp_seen_push(const void *v);
extern double scr_insp_circ_check(const void *v);
/* The retained inspect sections only need the OOM trap path. */
_Noreturn void scr_trap(const char *msg) {
fputs(msg, stderr);
abort();
}
static pthread_mutex_t mu = PTHREAD_MUTEX_INITIALIZER;
static pthread_cond_t cv = PTHREAD_COND_INITIALIZER;
static int stage;
static int targets[2];
static double ids[3];
static void advance(int next) {
pthread_mutex_lock(&mu);
stage = next;
pthread_cond_broadcast(&cv);
pthread_mutex_unlock(&mu);
}
static void await(int expected) {
pthread_mutex_lock(&mu);
while (stage < expected) pthread_cond_wait(&cv, &mu);
pthread_mutex_unlock(&mu);
}
static void *worker_a(void *unused) {
(void)unused;
scr_insp_seen_push(&targets[0]);
ids[0] = scr_insp_circ_check(&targets[0]);
advance(1);
await(2);
ids[2] = scr_insp_circ_check(&targets[0]);
return NULL;
}
static void *worker_b(void *unused) {
(void)unused;
await(1);
scr_insp_seen_push(&targets[1]);
ids[1] = scr_insp_circ_check(&targets[1]);
advance(2);
return NULL;
}
int main(void) {
pthread_t a, b;
pthread_create(&a, NULL, worker_a, NULL);
pthread_create(&b, NULL, worker_b, NULL);
pthread_join(a, NULL);
pthread_join(b, NULL);
printf("%.0f %.0f %.0f\n", ids[0], ids[1], ids[2]);
return ids[0] == 1 && ids[1] == 1 && ids[2] == 1 ? 0 : 1;
}