commit b0699233ebb6aa3221e92d5c93b52d19ad2cded1
parent bac5e9ae8b2c6067de9e768591129852d7503d3b
Author: finwo <finwo@pm.me>
Date: Sat, 10 Oct 2026 20:51:45 +0200
threading
Diffstat:
7 files changed, 446 insertions(+), 49 deletions(-)
diff --git a/src/sfinx.c b/src/sfinx.c
@@ -5,6 +5,10 @@
#include <string.h>
#include <unistd.h>
+#ifdef _REENTRANT
+#include <pthread.h>
+#endif
+
// E4M4 {{{
//
// Log-ish number format, only positive
@@ -191,6 +195,227 @@ static inline int _sfinx_wots_verify(sfinx_hash hash, const uint8_t *pubkey, con
}
// }}}
+// Pool {{{
+//
+// Compile-time worker pool for one top-level operation
+// _REENTRANT: a round pulls indices from a shared counter, one scratch per worker
+// otherwise, and for counts under two, the loop runs on the caller
+
+typedef void (*_sfinx_job_fn)(void *ctx, size_t index, void *scratch);
+
+#ifdef _REENTRANT
+typedef struct {
+ struct _sfinx_pool *pool;
+ size_t index;
+} _sfinx_worker;
+#endif
+
+typedef struct _sfinx_pool {
+ size_t count;
+#ifdef _REENTRANT
+ pthread_t *threads;
+ _sfinx_worker *workers;
+ pthread_mutex_t mutex;
+ pthread_cond_t work;
+ pthread_cond_t done;
+ _sfinx_job_fn fn;
+ void *ctx;
+ size_t total;
+ size_t next;
+ size_t completed;
+ unsigned long round;
+ size_t scratch;
+ uint8_t *scratch_mem;
+ int stop;
+#endif
+} _sfinx_pool;
+
+static inline size_t _sfinx_pool_scratch(size_t key_len) {
+ return key_len > SFINX_BATCH_KEY_BUDGET ? key_len : SFINX_BATCH_KEY_BUDGET;
+}
+
+#ifdef _REENTRANT
+static void *_sfinx_pool_worker(void *arg) {
+ _sfinx_worker *worker = arg;
+ _sfinx_pool *pool = worker->pool;
+ uint8_t *scratch = pool->scratch_mem + (worker->index + 1) * pool->scratch;
+ unsigned long seen = 0;
+
+ for (;;) {
+ pthread_mutex_lock(&pool->mutex);
+ while (!pool->stop && pool->round == seen) {
+ pthread_cond_wait(&pool->work, &pool->mutex);
+ }
+ if (pool->stop) {
+ pthread_mutex_unlock(&pool->mutex);
+ return NULL;
+ }
+ seen = pool->round;
+ pthread_mutex_unlock(&pool->mutex);
+ for (;;) {
+ size_t i;
+ pthread_mutex_lock(&pool->mutex);
+ if (pool->next >= pool->total) {
+ pthread_mutex_unlock(&pool->mutex);
+ break;
+ }
+ i = pool->next++;
+ pthread_mutex_unlock(&pool->mutex);
+ pool->fn(pool->ctx, i, scratch);
+ pthread_mutex_lock(&pool->mutex);
+ pool->completed++;
+ if (pool->completed == pool->total) {
+ pthread_cond_broadcast(&pool->done);
+ }
+ pthread_mutex_unlock(&pool->mutex);
+ }
+ }
+}
+#endif
+
+static void _sfinx_pool_destroy(_sfinx_pool *pool) {
+#ifdef _REENTRANT
+ if (!pool) {
+ return;
+ }
+ pthread_mutex_lock(&pool->mutex);
+ pool->stop = 1;
+ pthread_cond_broadcast(&pool->work);
+ pthread_mutex_unlock(&pool->mutex);
+ for (size_t i = 0; i < pool->count; i++) {
+ pthread_join(pool->threads[i], NULL);
+ }
+ pthread_mutex_destroy(&pool->mutex);
+ pthread_cond_destroy(&pool->work);
+ pthread_cond_destroy(&pool->done);
+ free(pool->scratch_mem);
+ free(pool->threads);
+ free(pool->workers);
+ free(pool);
+#else
+ (void)pool;
+#endif
+}
+
+static _sfinx_pool *_sfinx_pool_create(size_t scratch) {
+#ifdef _REENTRANT
+ _sfinx_pool *pool;
+ long n;
+
+ if (scratch == 0) {
+ scratch = 1;
+ }
+ n = sysconf(_SC_NPROCESSORS_ONLN);
+ if (n <= 1) {
+ return NULL;
+ }
+ pool = calloc(1, sizeof(*pool));
+ if (!pool) {
+ return NULL;
+ }
+ pool->count = (size_t)n;
+ pool->scratch = scratch;
+ pool->scratch_mem = malloc((pool->count + 1) * scratch);
+ pool->threads = calloc(pool->count, sizeof(*pool->threads));
+ pool->workers = calloc(pool->count, sizeof(*pool->workers));
+ if (!pool->scratch_mem || !pool->threads || !pool->workers) {
+ free(pool->scratch_mem);
+ free(pool->threads);
+ free(pool->workers);
+ free(pool);
+ return NULL;
+ }
+ if (pthread_mutex_init(&pool->mutex, NULL) != 0) {
+ free(pool->scratch_mem);
+ free(pool->threads);
+ free(pool->workers);
+ free(pool);
+ return NULL;
+ }
+ if (pthread_cond_init(&pool->work, NULL) != 0) {
+ pthread_mutex_destroy(&pool->mutex);
+ free(pool->scratch_mem);
+ free(pool->threads);
+ free(pool->workers);
+ free(pool);
+ return NULL;
+ }
+ if (pthread_cond_init(&pool->done, NULL) != 0) {
+ pthread_cond_destroy(&pool->work);
+ pthread_mutex_destroy(&pool->mutex);
+ free(pool->scratch_mem);
+ free(pool->threads);
+ free(pool->workers);
+ free(pool);
+ return NULL;
+ }
+ for (size_t i = 0; i < pool->count; i++) {
+ pool->workers[i].pool = pool;
+ pool->workers[i].index = i;
+ }
+ for (size_t i = 0; i < pool->count; i++) {
+ if (pthread_create(&pool->threads[i], NULL, _sfinx_pool_worker, &pool->workers[i]) != 0) {
+ pool->count = i;
+ _sfinx_pool_destroy(pool);
+ return NULL;
+ }
+ }
+ return pool;
+#else
+ (void)scratch;
+ return NULL;
+#endif
+}
+
+static void _sfinx_parallel_for(_sfinx_pool *pool, size_t count, _sfinx_job_fn fn, void *ctx) {
+ uint8_t *scratch;
+#ifdef _REENTRANT
+ if (pool && count >= 2) {
+ scratch = pool->scratch_mem;
+ pthread_mutex_lock(&pool->mutex);
+ pool->fn = fn;
+ pool->ctx = ctx;
+ pool->total = count;
+ pool->next = 0;
+ pool->completed = 0;
+ pool->round++;
+ pthread_cond_broadcast(&pool->work);
+ pthread_mutex_unlock(&pool->mutex);
+ for (;;) {
+ size_t i;
+ pthread_mutex_lock(&pool->mutex);
+ if (pool->next >= pool->total) {
+ pthread_mutex_unlock(&pool->mutex);
+ break;
+ }
+ i = pool->next++;
+ pthread_mutex_unlock(&pool->mutex);
+ fn(ctx, i, scratch);
+ pthread_mutex_lock(&pool->mutex);
+ pool->completed++;
+ if (pool->completed == pool->total) {
+ pthread_cond_broadcast(&pool->done);
+ }
+ pthread_mutex_unlock(&pool->mutex);
+ }
+ pthread_mutex_lock(&pool->mutex);
+ while (pool->completed < pool->total) {
+ pthread_cond_wait(&pool->done, &pool->mutex);
+ }
+ pthread_mutex_unlock(&pool->mutex);
+ return;
+ }
+ scratch = pool ? pool->scratch_mem : NULL;
+#else
+ (void)pool;
+ scratch = NULL;
+#endif
+ for (size_t i = 0; i < count; i++) {
+ fn(ctx, i, scratch);
+ }
+}
+// }}}
+
// Subtree {{{
//
// One binary subtree of height 8, 256 WOTS leaves folded into a root
@@ -221,52 +446,113 @@ static inline void _sfinx_merkle_parent(sfinx_hash hash, uint8_t bitlen, uint8_t
_sfinx_hash(hash, buf, at, out);
}
-static inline int _sfinx_subtree_build(sfinx_hash hash, const uint8_t *seed, size_t seed_len, const uint8_t *prefix,
- size_t prefix_len, uint8_t leaf_index, uint8_t *root_out, uint8_t *auth_out) {
- size_t len = sfinx_hash_len(hash);
- size_t c = len + 2;
- size_t key_len = seed_len + prefix_len + 1;
- size_t chunk = SFINX_BATCH_KEY_BUDGET / key_len;
- size_t count = 256;
- size_t pos = leaf_index;
- uint8_t nodes[256 * SFINX_HASH_LEN_MAX];
- uint8_t *ends;
- uint8_t *keybuf;
+static inline void _sfinx_subtree_leaves(sfinx_hash hash, const uint8_t *seed, size_t seed_len, const uint8_t *prefix,
+ size_t prefix_len, size_t begin, size_t end, uint8_t *ends, uint8_t *nodes,
+ uint8_t *keybuf) {
+ size_t len = sfinx_hash_len(hash);
+ size_t c = len + 2;
+ size_t key_len = seed_len + prefix_len + 1;
+ size_t chunk = SFINX_BATCH_KEY_BUDGET / key_len;
+ size_t n = end - begin;
if (chunk == 0) {
chunk = 1;
}
- if (chunk > count) {
- chunk = count;
- }
- ends = malloc(count * c * len);
- keybuf = malloc(chunk * key_len);
- if (!ends || !keybuf) {
- free(ends);
- free(keybuf);
- return 0;
- }
- for (size_t base = 0; base < count; base += chunk) {
- size_t n = count - base;
- if (n > chunk) {
- n = chunk;
+ for (size_t base = 0; base < n; base += chunk) {
+ size_t m = n - base;
+ if (m > chunk) {
+ m = chunk;
}
- for (size_t i = 0; i < n; i++) {
+ for (size_t i = 0; i < m; i++) {
uint8_t *key = keybuf + i * key_len;
memcpy(key, seed, seed_len);
if (prefix_len > 0) {
memcpy(key + seed_len, prefix, prefix_len);
}
- key[key_len - 1] = (uint8_t)(base + i);
+ key[key_len - 1] = (uint8_t)(begin + base + i);
}
- _sfinx_shake256_batch(n, keybuf, key_len, ends + base * c * len, c * len);
+ _sfinx_shake256_batch(m, keybuf, key_len, ends + (begin + base) * c * len, c * len);
}
- free(keybuf);
for (uint32_t step = 0; step < 256u; step++) {
- _sfinx_hash_batch(hash, count * c, ends, len, ends, len);
+ _sfinx_hash_batch(hash, n * c, ends + begin * c * len, len, ends + begin * c * len, len);
+ }
+ _sfinx_hash_batch(hash, n, ends + begin * c * len, c * len, nodes + begin * len, len);
+}
+
+typedef struct {
+ sfinx_hash hash;
+ const uint8_t *seed;
+ size_t seed_len;
+ const uint8_t *prefix;
+ size_t prefix_len;
+ size_t chunk;
+ size_t count;
+ uint8_t *ends;
+ uint8_t *nodes;
+} _sfinx_subtree_ctx;
+
+static void _sfinx_subtree_job(void *vctx, size_t index, void *scratch) {
+ _sfinx_subtree_ctx *ctx = vctx;
+ size_t begin = index * ctx->chunk;
+ size_t end = begin + ctx->chunk;
+
+ if (end > ctx->count) {
+ end = ctx->count;
+ }
+ _sfinx_subtree_leaves(ctx->hash, ctx->seed, ctx->seed_len, ctx->prefix, ctx->prefix_len, begin, end, ctx->ends,
+ ctx->nodes, scratch);
+}
+
+static inline int _sfinx_subtree_build(_sfinx_pool *pool, sfinx_hash hash, const uint8_t *seed, size_t seed_len,
+ const uint8_t *prefix, size_t prefix_len, uint8_t leaf_index, uint8_t *root_out,
+ uint8_t *auth_out) {
+ size_t len = sfinx_hash_len(hash);
+ size_t c = len + 2;
+ size_t key_len = seed_len + prefix_len + 1;
+ size_t count = 256;
+ size_t pos = leaf_index;
+ uint8_t nodes[256 * SFINX_HASH_LEN_MAX];
+ uint8_t *ends;
+
+ ends = malloc(count * c * len);
+ if (!ends) {
+ return 0;
+ }
+ if (pool) {
+ _sfinx_subtree_ctx ctx = {
+ .hash = hash,
+ .seed = seed,
+ .seed_len = seed_len,
+ .prefix = prefix,
+ .prefix_len = prefix_len,
+ .chunk = (count + pool->count - 1) / pool->count,
+ .count = count,
+ .ends = ends,
+ .nodes = nodes,
+ };
+ size_t jobs;
+ if (ctx.chunk == 0) {
+ ctx.chunk = 1;
+ }
+ jobs = (count + ctx.chunk - 1) / ctx.chunk;
+ _sfinx_parallel_for(pool, jobs, _sfinx_subtree_job, &ctx);
+ } else {
+ size_t chunk = SFINX_BATCH_KEY_BUDGET / key_len;
+ uint8_t *keybuf;
+ if (chunk == 0) {
+ chunk = 1;
+ }
+ if (chunk > count) {
+ chunk = count;
+ }
+ keybuf = malloc(chunk * key_len);
+ if (!keybuf) {
+ free(ends);
+ return 0;
+ }
+ _sfinx_subtree_leaves(hash, seed, seed_len, prefix, prefix_len, 0, count, ends, nodes, keybuf);
+ free(keybuf);
}
- _sfinx_hash_batch(hash, count, ends, c * len, nodes, len);
- free(ends);
for (size_t bitlen = 8; bitlen-- > 0;) {
if (auth_out) {
memcpy(auth_out + (7 - bitlen) * len, nodes + (pos ^ 1) * len, len);
@@ -279,6 +565,7 @@ static inline int _sfinx_subtree_build(sfinx_hash hash, const uint8_t *seed, siz
pos >>= 1;
}
memcpy(root_out, nodes, len);
+ free(ends);
return 1;
}
@@ -351,14 +638,19 @@ sfinx_status sfinx_single_sign(sfinx_hash hash, const uint8_t *seed, size_t seed
sfinx_status sfinx_tree_public_key(sfinx_hash hash, const uint8_t *seed, size_t seed_len, uint8_t *pubkey_out,
size_t pubkey_cap, size_t *pubkey_len_out) {
- size_t len = sfinx_hash_len(hash);
+ size_t len = sfinx_hash_len(hash);
+ _sfinx_pool *pool;
+ int ok;
if (len == 0 || !seed || seed_len > SFINX_SEED_LEN_MAX) {
return SFINX_ERR_ARGS;
}
if (!pubkey_out || pubkey_cap < len) {
return SFINX_ERR_NOSPACE;
}
- if (!_sfinx_subtree_build(hash, seed, seed_len, NULL, 0, 0, pubkey_out, NULL)) {
+ pool = _sfinx_pool_create(_sfinx_pool_scratch(seed_len + 1));
+ ok = _sfinx_subtree_build(pool, hash, seed, seed_len, NULL, 0, 0, pubkey_out, NULL);
+ _sfinx_pool_destroy(pool);
+ if (!ok) {
return SFINX_ERR_NOSPACE;
}
if (pubkey_len_out) {
@@ -370,16 +662,17 @@ sfinx_status sfinx_tree_public_key(sfinx_hash hash, const uint8_t *seed, size_t
sfinx_status sfinx_tree_sign(sfinx_hash hash, const uint8_t *seed, size_t seed_len, const uint8_t *path,
size_t path_len, const uint8_t *msg, size_t msg_len, uint8_t *sig, size_t sig_cap,
size_t *sig_len_out) {
- size_t len = sfinx_hash_len(hash);
- size_t c = len + 2;
- size_t need = sfinx_signature_len(hash, path_len);
- uint8_t org[SFINX_HASH_LEN_MAX];
- uint8_t value[SFINX_HASH_LEN_MAX];
- uint8_t root[SFINX_HASH_LEN_MAX];
- uint8_t auth[8 * SFINX_HASH_LEN_MAX];
- uint8_t *key;
- uint8_t *joined;
- size_t at;
+ size_t len = sfinx_hash_len(hash);
+ size_t c = len + 2;
+ size_t need = sfinx_signature_len(hash, path_len);
+ uint8_t org[SFINX_HASH_LEN_MAX];
+ uint8_t value[SFINX_HASH_LEN_MAX];
+ uint8_t root[SFINX_HASH_LEN_MAX];
+ uint8_t auth[8 * SFINX_HASH_LEN_MAX];
+ uint8_t *key;
+ uint8_t *joined;
+ size_t at;
+ _sfinx_pool *pool;
if (len == 0 || !seed || !msg || seed_len > SFINX_SEED_LEN_MAX || !path || path_len == 0 ||
!sfinx_path_len_valid(path_len)) {
@@ -395,6 +688,7 @@ sfinx_status sfinx_tree_sign(sfinx_hash hash, const uint8_t *seed, size_t seed_l
free(key);
return SFINX_ERR_NOSPACE;
}
+ pool = _sfinx_pool_create(_sfinx_pool_scratch(seed_len + path_len));
// Sign (path||message)
memcpy(joined, path, path_len);
memcpy(joined + path_len, msg, msg_len);
@@ -414,11 +708,13 @@ sfinx_status sfinx_tree_sign(sfinx_hash hash, const uint8_t *seed, size_t seed_l
// WOTS-sign up the tree
memcpy(value, org, len);
for (size_t k = path_len; k >= 1; k--) {
- if (!_sfinx_subtree_build(hash, seed, seed_len, path, k - 1, path[k - 1], root, auth)) {
+ if (!_sfinx_subtree_build(pool, hash, seed, seed_len, path, k - 1, path[k - 1], root, auth)) {
+ _sfinx_pool_destroy(pool);
free(key);
return SFINX_ERR_NOSPACE;
}
if (_sfinx_wots_sign(hash, key, seed_len + k, value, len, sig + at) != SFINX_OK) {
+ _sfinx_pool_destroy(pool);
free(key);
return SFINX_ERR_ARGS;
}
@@ -428,6 +724,7 @@ sfinx_status sfinx_tree_sign(sfinx_hash hash, const uint8_t *seed, size_t seed_l
memcpy(value, root, len);
}
+ _sfinx_pool_destroy(pool);
free(key);
if (sig_len_out) {
*sig_len_out = need;
diff --git a/target/common/Makefile b/target/common/Makefile
@@ -14,6 +14,10 @@ LDFLAGS?=
CFLAGS +=-ffunction-sections -fdata-sections
LDFLAGS+=-Wl,--gc-sections
+# pthread backs the optional worker pool in sfinx.c; the library manifest leaves it to the consumer
+CFLAGS +=-pthread
+LDFLAGS+=-pthread
+
INCLUDES:=
INCLUDES+=-I src
diff --git a/test/04-subtree.c b/test/04-subtree.c
@@ -22,7 +22,7 @@ int main(void) {
tap_begin("sfinx subtree");
- tap(_sfinx_subtree_build(SFINX_HASH_256, seed, seed_len, prefix, 3, idx, root, auth), "subtree builds",
+ tap(_sfinx_subtree_build(NULL, SFINX_HASH_256, seed, seed_len, prefix, 3, idx, root, auth), "subtree builds",
"build failed");
memcpy(key, seed, seed_len);
@@ -42,7 +42,7 @@ int main(void) {
_sfinx_subtree_verify(SFINX_HASH_256, prefix, 3, leaf2, idx, auth, built);
tap(memcmp(built, root, len) != 0, "tampered leaf fails", "root matched");
- tap(_sfinx_subtree_build(SFINX_HASH_256, seed, seed_len, prefix2, 3, idx, root2, NULL), "second subtree builds",
+ tap(_sfinx_subtree_build(NULL, SFINX_HASH_256, seed, seed_len, prefix2, 3, idx, root2, NULL), "second subtree builds",
"build failed");
tap(memcmp(root, root2, len) != 0, "prefix changes the subtree root", "roots match");
@@ -73,7 +73,7 @@ int main(void) {
uint8_t mkey[32];
uint8_t mnodes[256 * SFINX_HASH_LEN_MAX];
- tap(_sfinx_subtree_build(h, seed, seed_len, prefix, 3, idx, broot, NULL) != 0, "subtree builds at every size",
+ tap(_sfinx_subtree_build(NULL, h, seed, seed_len, prefix, 3, idx, broot, NULL) != 0, "subtree builds at every size",
"build failed");
for (size_t i = 0; i < 256; i++) {
memcpy(mkey, seed, seed_len);
diff --git a/test/10-batch.c b/test/10-batch.c
@@ -26,7 +26,7 @@ int main(void) {
uint8_t ref_auth[8 * SFINX_HASH_LEN_MAX];
char label[64];
- tap(_sfinx_subtree_build(hash, seed, seed_len, prefix, sizeof(prefix), 0x5a, lib_root, lib_auth) != 0,
+ tap(_sfinx_subtree_build(NULL, hash, seed, seed_len, prefix, sizeof(prefix), 0x5a, lib_root, lib_auth) != 0,
"chunked subtree builds", "build failed");
ref_subtree(bit, seed, seed_len, prefix, sizeof(prefix), 0x5a, ref_root, ref_auth);
diff --git a/test/Makefile b/test/Makefile
@@ -1,6 +1,7 @@
CC ?= cc
CFLAGS ?= -Wall -Wextra -O2
CFLAGS += -I.. -I../src -I../lib/.dep/include
+CFLAGS += -pthread
OUT := ../build/test
SRCS := $(filter-out _tap.c,$(wildcard *.c))
diff --git a/test/ref/thread_dump.c b/test/ref/thread_dump.c
@@ -0,0 +1,36 @@
+// Prints signatures for a fixed seed, path, and message, so a -pthread build
+// and a plain build can be diffed byte for byte
+
+#include <stdint.h>
+#include <stdio.h>
+#include <string.h>
+
+#include "src/sfinx.c"
+
+int main(void) {
+ static const uint8_t seed[32] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a,
+ 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15,
+ 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f};
+ static const uint8_t path[3] = {0x0a, 0x1b, 0x2c};
+ static const char msg[] = "sfinx thread dump";
+ static const int bits[] = {224, 256, 384, 512};
+ size_t msg_len = sizeof(msg) - 1;
+
+ for (size_t s = 0; s < sizeof(bits) / sizeof(bits[0]); s++) {
+ for (size_t n = 1; n <= 3; n += 2) {
+ uint8_t sig[32768];
+ size_t sig_len = 0;
+ if (sfinx_tree_sign((sfinx_hash)bits[s], seed, sizeof(seed), path, n, (const uint8_t *)msg, msg_len, sig,
+ sizeof(sig), &sig_len) != SFINX_OK) {
+ fprintf(stderr, "sign failed for L%d N%zu\n", bits[s], n);
+ return 1;
+ }
+ printf("L=%d N=%zu ", bits[s], n);
+ for (size_t i = 0; i < sig_len; i++) {
+ printf("%02x", sig[i]);
+ }
+ printf("\n");
+ }
+ }
+ return 0;
+}
diff --git a/test/test_threading.sh b/test/test_threading.sh
@@ -0,0 +1,59 @@
+#!/bin/sh
+# Builds the thread dump with and without -pthread and diffs the signatures.
+# A ThreadSanitizer build runs too, and is skipped when it does not build.
+set -eu
+HERE=$(cd "$(dirname "$0")" && pwd)
+ROOT=$(cd "${HERE}/.." && pwd)
+. "${HERE}/_tap.sh"
+
+CC=${CC:-cc}
+
+tap_begin "sfinx threading"
+
+HASH_FIRST="${ROOT}/lib/finwo/keccak-fast/src/keccak-fast.c"
+HASH="${HASH_FIRST} ${ROOT}/lib/finwo/keccak-fast/src/backend/scalar.c ${ROOT}/lib/finwo/keccak-fast/src/backend/scalar_bmi.c ${ROOT}/lib/finwo/keccak-fast/src/backend/avx2.c ${ROOT}/lib/finwo/keccak-fast/src/backend/avx512.c"
+if [ ! -f "${HASH_FIRST}" ]; then
+ (cd "${ROOT}" && dep install) >/dev/null 2>&1 || true
+fi
+
+TMP=$(mktemp -d)
+trap 'rm -rf "${TMP}"' EXIT
+
+build() {
+ out=$1
+ shift
+ "${CC}" -Wall -Wextra -O2 "$@" -I"${ROOT}" -I"${ROOT}/src" -I"${ROOT}/lib/.dep/include" \
+ "${HERE}/ref/thread_dump.c" ${HASH} -o "${out}"
+}
+
+if build "${TMP}/threaded" -pthread >"${TMP}/b1.log" 2>&1; then
+ tap 1 "threaded dump builds" ""
+else
+ tap 0 "threaded dump builds" "$(cat "${TMP}/b1.log")"
+fi
+if build "${TMP}/plain" >"${TMP}/b2.log" 2>&1; then
+ tap 1 "plain dump builds" ""
+else
+ tap 0 "plain dump builds" "$(cat "${TMP}/b2.log")"
+fi
+
+"${TMP}/threaded" >"${TMP}/a" 2>/dev/null || true
+"${TMP}/plain" >"${TMP}/b" 2>/dev/null || true
+if [ -s "${TMP}/a" ] && cmp -s "${TMP}/a" "${TMP}/b"; then
+ tap 1 "threaded and plain signatures match" ""
+else
+ tap 0 "threaded and plain signatures match" "dump outputs differ or are empty"
+fi
+
+if build "${TMP}/tsan" -pthread -fsanitize=thread >"${TMP}/b3.log" 2>&1; then
+ if TSAN_OPTIONS="halt_on_error=1" "${TMP}/tsan" >"${TMP}/t.log" 2>&1; then
+ tap 1 "thread sanitizer reports no races" ""
+ else
+ tap 0 "thread sanitizer reports no races" "$(tail -n 5 "${TMP}/t.log" 2>/dev/null || true)"
+ fi
+else
+ TAP_NO=$((TAP_NO + 1))
+ printf 'ok %s - thread sanitizer available # SKIP TSan build failed\n' "${TAP_NO}"
+fi
+
+tap_plan