mirror of
https://github.com/vercel-labs/scriptc.git
synced 2026-10-20 05:40:17 +08:00
- tests/harness/library-int.test.ts: all 16 reference-corpus cases as library fixtures on both emissions — send/sendU64 are profile-mapped exports with i64/u64 params, PROVE cases pin every crossed value against the Node oracle (the test process runs the same case source), the counter loop crosses 0..9 in order, REFUSE cases name obligation, slot path, and evidence - int-returns fixture: i64/u64 returns cross as real int64_t/uint64_t (9007199254740991, -0 as 0, -7 % 3 as -1, ToUint32 max on a u64 slot) - int-trap fixture: inbound integers past 2^53-1 deliver the structured SC4012 host-contract trap with profile teaching/remediation; in-range extremes convert exactly - Sidecar slots: integer_slots attestation in declaration order, declared slots spelled i64 (V10 bijection validated), unproven writes refused by slot path (Msg.count, helpers.clampIdx.return), unresolvable and non-number declarations refused SC4009, teachings ride SC4022 as attributed notes - Layer-1 differential: no Layer-1 representation exists (trivially unobservable); pinned consequence — f64-declared vs i64-declared builds cross byte-identical values - Integer-declared helpers join libRoots so their attestation covers a compiled body, never a dead-stripped vacuity
87 lines
2.9 KiB
C
87 lines
2.9 KiB
C
/* The inbound declared-integer host-contract trap, mode-selected by
|
||
* argv[1]:
|
||
* ok — in-range int64_t/uint64_t values convert exactly (the
|
||
* edges: ±(2^53 − 1) for i64, 2^53 − 1 for u64)
|
||
* trap-i64 — 2^53 + 1 through the i64 parameter: the wrapper's
|
||
* range-check delivers the structured SC4012 message (the
|
||
* profile's teaching text and remediation riding it) and
|
||
* the library poisons — silent rounding never happens
|
||
* trap-u64 — 2^60 through the u64 parameter, same story
|
||
* The parse in show() is the spec's rule: split the bytes after the 0x01
|
||
* marker on 0x1F into text/code/symbol/remediation. */
|
||
#include <inttypes.h>
|
||
#include <setjmp.h>
|
||
#include <stddef.h>
|
||
#include <stdint.h>
|
||
#include <stdio.h>
|
||
#include <string.h>
|
||
|
||
extern void kt_init(void);
|
||
extern void kt_set_panic_sink(void (*fn)(void *, const uint8_t *, size_t, uint64_t), void *ctx);
|
||
extern void kt_take(int64_t x);
|
||
extern void kt_take_u(uint64_t x);
|
||
extern double kt_last(void);
|
||
|
||
static jmp_buf trap_jmp;
|
||
static int sink_calls = 0;
|
||
|
||
static void show(const uint8_t *msg, size_t len) {
|
||
if (len == 0 || msg[0] != 0x01) {
|
||
printf("baseline text=%.*s", (int)len, (const char *)msg);
|
||
return;
|
||
}
|
||
static const char *names[4] = {"text", "code", "symbol", "remediation"};
|
||
const uint8_t *p = msg + 1, *end = msg + len;
|
||
int fields = 0;
|
||
for (;;) {
|
||
const uint8_t *sep = memchr(p, 0x1f, (size_t)(end - p));
|
||
const uint8_t *stop = sep != NULL ? sep : end;
|
||
if (fields < 4) printf("%s=[%.*s]\n", names[fields], (int)(stop - p), (const char *)p);
|
||
fields++;
|
||
if (sep == NULL) break;
|
||
p = sep + 1;
|
||
}
|
||
printf("fields=%d\n", fields);
|
||
}
|
||
|
||
static void sink(void *ctx, const uint8_t *msg, size_t len, uint64_t addr) {
|
||
(void)ctx;
|
||
(void)addr;
|
||
sink_calls++;
|
||
printf("sink[%d]:\n", sink_calls);
|
||
show(msg, len);
|
||
longjmp(trap_jmp, 1);
|
||
}
|
||
|
||
int main(int argc, char **argv) {
|
||
const char *mode = argc > 1 ? argv[1] : "ok";
|
||
kt_set_panic_sink(sink, NULL);
|
||
kt_init();
|
||
|
||
if (setjmp(trap_jmp) == 0) {
|
||
if (strcmp(mode, "ok") == 0) {
|
||
kt_take(INT64_C(9007199254740991));
|
||
printf("i64 max: %.17g\n", kt_last());
|
||
kt_take(INT64_C(-9007199254740991));
|
||
printf("i64 min: %.17g\n", kt_last());
|
||
kt_take_u(UINT64_C(9007199254740991));
|
||
printf("u64 max: %.17g\n", kt_last());
|
||
printf("ok, sink_calls=%d\n", sink_calls);
|
||
return 0;
|
||
} else if (strcmp(mode, "trap-i64") == 0) {
|
||
kt_take(INT64_C(9007199254740993)); /* 2^53 + 1: cannot ride f64 */
|
||
} else if (strcmp(mode, "trap-u64") == 0) {
|
||
kt_take_u(UINT64_C(1) << 60);
|
||
} else {
|
||
fprintf(stderr, "unknown mode %s\n", mode);
|
||
return 2;
|
||
}
|
||
printf("UNREACHABLE\n");
|
||
return 1;
|
||
}
|
||
/* Poisoned now: no further entries (kt_last would abort by design) —
|
||
* the message arrived exactly once and the host survived. */
|
||
printf("survived, sink_calls=%d\n", sink_calls);
|
||
return 0;
|
||
}
|