diff --git a/docs/NATIVE-ABI.md b/docs/NATIVE-ABI.md index 6e89b28..28db537 100644 --- a/docs/NATIVE-ABI.md +++ b/docs/NATIVE-ABI.md @@ -43,6 +43,13 @@ anything outside the supported signature set is refused (exit 110, no output). T Names must be C identifiers, a parameter name may not end in `_len` (the binding names a length `o__len`), and each unit name appears once; anything else is refused before any C is written. +All emitted `ombr_*` assembly definitions have local ELF binding, including code, entry stubs and +runtime trampolines. C-defined runtime helpers and state retain `static` linkage; the `extern` +declarations only reference assembler-owned local definitions and do not export them. Only +`omega_` is public. Independently generated C translation units can therefore be linked +together without renaming or weak symbols. Each translation unit owns its runtime table, reentry +flag, thread stack and pthread destructor key; units generated together share that private runtime. + Supported signatures: up to 6 parameters, each `ulong`, `long`, or `edit`/`view` of `ulong` or `uchar` with a fixed length; result `ulong`, `long` or `void`. A unit may hold several functions: the seeded compiler enters function-mode code at the unit's last function (word 0 is a branch to it), so the @@ -65,8 +72,9 @@ embedded code, with any Omega types. Only the last function's signature must be | status 35 | the binding could not map its stack for this thread | Buffers stay owned by the caller and are borrowed for the call only; Omega keeps no address after it -returns. Writes made before a trap stay visible (no rollback). Calls on different threads are independent. The runtime table is one read-only table shared by -every call and thread (the code only reads it); a write to it would fault. +returns. Writes made before a trap stay visible (no rollback). Calls on different threads are independent. +Each generated translation unit has one read-only runtime table shared by its calls and threads (the +code only reads it); a write to it would fault. Stack: on its first call from a thread the binding maps 8 MiB of stack for that thread above an 8 MiB no-access guard, and frees both when the thread exits. The caller's own stack size does not matter (a @@ -111,6 +119,12 @@ A missing toolchain emits `NOT_RUN` and makes the runner exit nonzero, not a qua missing-toolchain controls, followed by positive calls with the real Zig and Go toolchains. The historical `evidence/native-call-v1/run.log` predates this runner correction and is not rewritten. +`python3 tests/native/test_private_symbols.py` (with the same `OMEGA_NATIVE_OMC`) independently +generates `madd` and `smadd` bindings, links their separate objects into one caller, checks local +private symbols and the exact public export names with `nm`, and runs trap recovery and both units +concurrently on separate threads. Restoring `.globl ombr_enter` must make the link fail with a +multiple-definition diagnostic. The native-call runner includes this regression. + Red before green (this change): with the generator of commit e7777ca, the C caller dies with SIGILL under BTI, and the 4,000,000-byte array on a 2 MiB thread dies with SIGSEGV; with this generator both pass. Extra receipts: 5+20+7 = 32 (calc:08302bc4), (7+1)+7 = 15 (calc:e44bce3f), 5+20+300 exceeds 255 diff --git a/evidence/native-call-v1/private-symbols-compat.log b/evidence/native-call-v1/private-symbols-compat.log new file mode 100644 index 0000000..077db31 --- /dev/null +++ b/evidence/native-call-v1/private-symbols-compat.log @@ -0,0 +1,2 @@ +PASS public binding text and embedded code byte-identical; only .globl -> .local differs +PASS consumer omc 2c82480596bbdc50cda9626866209ef662d5834f278f839ff4ce0250c495029f: separate units, trap recovery and concurrent threads diff --git a/evidence/native-call-v1/private-symbols-failures.log b/evidence/native-call-v1/private-symbols-failures.log new file mode 100644 index 0000000..72a8434 --- /dev/null +++ b/evidence/native-call-v1/private-symbols-failures.log @@ -0,0 +1,4 @@ +PASS control zig-build (exit 1; NATIVE-CALL FAIL) +PASS control go-build (exit 1; NATIVE-CALL FAIL) +PASS control missing (exit 1; NATIVE-CALL NOT_RUN) +PASS native callers: Zig and Go built and executed; no missing languages diff --git a/evidence/native-call-v1/private-symbols-native.log b/evidence/native-call-v1/private-symbols-native.log new file mode 100644 index 0000000..1f77ac9 --- /dev/null +++ b/evidence/native-call-v1/private-symbols-native.log @@ -0,0 +1,17 @@ +PASS embedded code == omc output, per unit (85b3add4fe86a894) +PASS c caller (28 lines) +PASS c-bti caller (28 lines) +PASS generator refuses a unit name that is not a C identifier +PASS generator refuses a parameter name ending in _len +PASS generator refuses a repeated unit name +PASS generator accepts the same unit once (control) +PASS generator refuses a last (bound) function with an unsupported parameter type +PASS generator binds only the last function; an earlier helper of any type stays private +PASS cpp caller (10 lines) +PASS python caller (10 lines) +PASS rust caller (11 lines) +PASS zig caller (10 lines) +PASS go caller (10 lines) +PASS mutant madd (add -> sub) caught (7 failing checks) +PASS separate binding translation units, private symbols, traps, threads and global-symbol mutant +NATIVE-CALL PASS (not run: none) diff --git a/evidence/native-call-v1/private-symbols-red.log b/evidence/native-call-v1/private-symbols-red.log new file mode 100644 index 0000000..8943191 --- /dev/null +++ b/evidence/native-call-v1/private-symbols-red.log @@ -0,0 +1,13 @@ +/usr/bin/ld: /tmp/omega-private-symbols-6bphh3il/normal/smadd.o: in function `ombr_enter': +smadd.c:(.text+0x0): multiple definition of `ombr_enter'; /tmp/omega-private-symbols-6bphh3il/normal/madd.o:madd.c:(.text+0x0): first defined here +/usr/bin/ld: /tmp/omega-private-symbols-6bphh3il/normal/smadd.o: in function `ombr_trap': +smadd.c:(.text+0x94): multiple definition of `ombr_trap'; /tmp/omega-private-symbols-6bphh3il/normal/madd.o:madd.c:(.text+0x94): first defined here +/usr/bin/ld: /tmp/omega-private-symbols-6bphh3il/normal/smadd.o: in function `ombr_no_entry': +smadd.c:(.text+0xb0): multiple definition of `ombr_no_entry'; /tmp/omega-private-symbols-6bphh3il/normal/madd.o:madd.c:(.text+0xb0): first defined here +collect2: error: ld returned 1 exit status +Traceback (most recent call last): + File "/tmp/bridge-private-runtime/tests/native/test_private_symbols.py", line 73, in + main() + File "/tmp/bridge-private-runtime/tests/native/test_private_symbols.py", line 53, in main + raise AssertionError("separately generated bindings must link") +AssertionError: separately generated bindings must link diff --git a/src/native.om b/src/native.om index 5fed471..ac16c0f 100644 --- a/src/native.om +++ b/src/native.om @@ -21,8 +21,8 @@ global us ulong 256 global ue ulong 256 text hx "0123456789abcdef" text kw "toeditviewulonglongucharvoidmain" -text fr "__asm__(\".section .text.omega_bridge,\\\"ax\\\",@progbits\\n.balign 4\\n.globl ombr_code_~\\n.hidden ombr_code_~\\nombr_code_~:\\n.byte ~\\n.previous\\n\");\nextern const unsigned char ombr_entry_~[] __attribute__((visibility(\"hidden\")));\n__attribute__((visibility(\"default\"))) uint32_t omega_~(uint64_t *result~, uint64_t o_~, void *o_~, const void *o_~_len~)\n{\n ombr_span sp[7];\n unsigned ns = 0;\n uint64_t a[6] = {0};\n if (ombr_span_add(sp, &ns, result, 1u, 1u, 8u, 1))\n return OMEGA_BAD_ARG;\n~ if (ombr_span_add(sp, &ns, o_~, o_~_len, ~u, ~))\n return OMEGA_BAD_ARG;\n a[~] = (uint64_t)(uintptr_t)o_~;\n~ a[~] = o_~ if (ombr_overlap(sp, ns))\n return OMEGA_BAD_ARG;\n return ombr_run(ombr_entry_~, a, result, ~);\n}\n~\\n.globl ombr_entry_~\\n.hidden ombr_entry_~\\nombr_entry_~:\\nhint #34\\nb ombr_code_" -text pre "/* Generated by omega-bridge src/native.om: native call ABI v1 (docs/NATIVE-ABI.md).\n * The Omega machine code below is embedded byte for byte as the seeded compiler wrote it.\n * This file only adapts the C calling convention to it. Regenerate it; do not edit it. */\n#include \n#include \n#include \n#include \n#define OMEGA_OK 0u\n#define OMEGA_BAD_ARG 32u\n#define OMEGA_REENTRY 33u\n#define OMEGA_BAD_RT 34u\n#define OMEGA_NO_STACK 35u\n#define OMBR_STACK (8u << 20)\n#define OMBR_GUARD (8u << 20)\n/* entry[2] is the trap entry (OSC-1 section 7). */\ntypedef struct ombr_rt { const void *entry[32]; } ombr_rt;\ntypedef struct ombr_span { uintptr_t lo, hi; int w; } ombr_span;\nstatic _Thread_local volatile sig_atomic_t ombr_active;\nstatic _Thread_local unsigned char *ombr_stk;\nstatic pthread_key_t ombr_key;\nstatic int ombr_keyok;\nstatic pthread_once_t ombr_once = PTHREAD_ONCE_INIT;\nuint32_t ombr_enter(const void *code, const uint64_t *args, const ombr_rt *rt, uint64_t *out, void *top)\n __attribute__((visibility(\"hidden\")));\nextern const unsigned char ombr_trap[] __attribute__((visibility(\"hidden\")));\nextern const unsigned char ombr_no_entry[] __attribute__((visibility(\"hidden\")));\n/* ombr_enter saves the callee-saved registers on the caller's stack, keeps that stack pointer in x19 and\n * runs the code on the binding's own stack. A normal return stores x0 through out and\n * returns 0. A runtime entry restores the stack pointer from x19 and returns a status; out is not\n * written. The seeded compiler never writes x19..x28. Runtime entries are reached by blr/br, so they\n * start with bti jc. */\n__asm__(\".text\\n.balign 4\\n.globl ombr_enter\\n.hidden ombr_enter\\n.type ombr_enter,%function\\nombr_enter:\\n\"\n \"hint #34\\nstp x29,x30,[sp,#-176]!\\nmov x29,sp\\nstp x19,x20,[sp,#16]\\nstp x21,x22,[sp,#32]\\n\"\n \"stp x23,x24,[sp,#48]\\nstp x25,x26,[sp,#64]\\nstp x27,x28,[sp,#80]\\nstp d8,d9,[sp,#96]\\n\"\n \"stp d10,d11,[sp,#112]\\nstp d12,d13,[sp,#128]\\nstp d14,d15,[sp,#144]\\nstr x3,[sp,#160]\\n\"\n \"mov x19,sp\\nmov x16,x0\\nmov x17,x1\\nmov x7,x2\\nmov sp,x4\\n\"\n \"ldp x0,x1,[x17]\\nldp x2,x3,[x17,#16]\\nldp x4,x5,[x17,#32]\\nblr x16\\n\"\n \"mov sp,x19\\nldr x9,[sp,#160]\\nstr x0,[x9]\\nmov w0,#0\\n\"\n \"ombr_leave:\\nldp d14,d15,[sp,#144]\\nldp d12,d13,[sp,#128]\\nldp d10,d11,[sp,#112]\\n\"\n \"ldp d8,d9,[sp,#96]\\nldp x27,x28,[sp,#80]\\nldp x25,x26,[sp,#64]\\nldp x23,x24,[sp,#48]\\n\"\n \"ldp x21,x22,[sp,#32]\\nldp x19,x20,[sp,#16]\\nldp x29,x30,[sp],#176\\nret\\n.size ombr_enter,.-ombr_enter\\n\"\n \".globl ombr_trap\\n.hidden ombr_trap\\n.type ombr_trap,%function\\nombr_trap:\\nhint #38\\n\"\n \"mov sp,x19\\nsub x2,x1,#1\\nmov w3,#34\\ncmp x2,#13\\ncsel w0,w1,w3,ls\\nb ombr_leave\\n\"\n \".size ombr_trap,.-ombr_trap\\n\"\n \".globl ombr_no_entry\\n.hidden ombr_no_entry\\n.type ombr_no_entry,%function\\nombr_no_entry:\\nhint #38\\n\"\n \"mov sp,x19\\nmov w0,#34\\nb ombr_leave\\n.size ombr_no_entry,.-ombr_no_entry\\n\");\nstatic void ombr_free(void *p)\n{\n munmap(p, OMBR_STACK + OMBR_GUARD);\n}\nstatic void ombr_init(void)\n{\n ombr_keyok = pthread_key_create(&ombr_key, ombr_free) == 0;\n}\n/* The thread's Omega stack: OMBR_STACK usable bytes above an OMBR_GUARD no-access region. A function-mode\n * unit has no call cycles and at most 4 MiB of local arrays per function (OMEGESE, local arrays), so a frame that\n * would pass the bottom starts inside the guard and faults there; nothing below the guard is touched. */\nstatic unsigned char *ombr_stack(void)\n{\n if (ombr_stk)\n return ombr_stk;\n pthread_once(&ombr_once, ombr_init);\n if (!ombr_keyok)\n return 0;\n unsigned char *p = mmap(0, OMBR_STACK + OMBR_GUARD, PROT_READ | PROT_WRITE,\n MAP_PRIVATE | MAP_ANONYMOUS | MAP_NORESERVE, -1, 0);\n if (p == MAP_FAILED)\n return 0;\n if (mprotect(p, OMBR_GUARD, PROT_NONE) != 0 || pthread_setspecific(ombr_key, p) != 0) {\n munmap(p, OMBR_STACK + OMBR_GUARD);\n return 0;\n }\n ombr_stk = p + OMBR_GUARD + OMBR_STACK;\n return ombr_stk;\n}\nstatic int ombr_span_add(ombr_span *sp, unsigned *ns, const void *p, uint64_t n, uint64_t want, uint64_t es, int w)\n{\n uintptr_t lo = (uintptr_t)p;\n if (!p || n != want || lo % es != 0u || n > (UINTPTR_MAX - lo) / es)\n return 1;\n sp[*ns].lo = lo;\n sp[*ns].hi = lo + n * es;\n sp[*ns].w = w;\n (*ns)++;\n return 0;\n}\nstatic int ombr_overlap(const ombr_span *sp, unsigned ns)\n{\n for (unsigned i = 0; i < ns; i++)\n for (unsigned j = i + 1; j < ns; j++)\n if ((sp[i].w || sp[j].w) && sp[i].lo < sp[j].hi && sp[j].lo < sp[i].hi)\n return 1;\n return 0;\n}\n/* One read-only runtime table for every call and thread: the code only reads it (a write would fault). */\nstatic const ombr_rt ombr_rt_table = {{ [0 ... 1] = ombr_no_entry, [2] = ombr_trap, [3 ... 31] = ombr_no_entry }};\nstatic uint32_t ombr_run(const void *code, const uint64_t *args, uint64_t *result, int has_value)\n{\n if (ombr_active)\n return OMEGA_REENTRY;\n ombr_active = 1;\n unsigned char *top = ombr_stack();\n if (!top) {\n ombr_active = 0;\n return OMEGA_NO_STACK;\n }\n uint64_t v = 0;\n uint32_t s = ombr_enter(code, args, &ombr_rt_table, &v, top);\n ombr_active = 0;\n if (s == OMEGA_OK)\n *result = has_value ? v : 0u;\n return s;\n}\n" +text fr "__asm__(\".section .text.omega_bridge,\\\"ax\\\",@progbits\\n.balign 4\\n.local ombr_code_~\\n.hidden ombr_code_~\\nombr_code_~:\\n.byte ~\\n.previous\\n\");\nextern const unsigned char ombr_entry_~[] __attribute__((visibility(\"hidden\")));\n__attribute__((visibility(\"default\"))) uint32_t omega_~(uint64_t *result~, uint64_t o_~, void *o_~, const void *o_~_len~)\n{\n ombr_span sp[7];\n unsigned ns = 0;\n uint64_t a[6] = {0};\n if (ombr_span_add(sp, &ns, result, 1u, 1u, 8u, 1))\n return OMEGA_BAD_ARG;\n~ if (ombr_span_add(sp, &ns, o_~, o_~_len, ~u, ~))\n return OMEGA_BAD_ARG;\n a[~] = (uint64_t)(uintptr_t)o_~;\n~ a[~] = o_~ if (ombr_overlap(sp, ns))\n return OMEGA_BAD_ARG;\n return ombr_run(ombr_entry_~, a, result, ~);\n}\n~\\n.local ombr_entry_~\\n.hidden ombr_entry_~\\nombr_entry_~:\\nhint #34\\nb ombr_code_" +text pre "/* Generated by omega-bridge src/native.om: native call ABI v1 (docs/NATIVE-ABI.md).\n * The Omega machine code below is embedded byte for byte as the seeded compiler wrote it.\n * This file only adapts the C calling convention to it. Regenerate it; do not edit it. */\n#include \n#include \n#include \n#include \n#define OMEGA_OK 0u\n#define OMEGA_BAD_ARG 32u\n#define OMEGA_REENTRY 33u\n#define OMEGA_BAD_RT 34u\n#define OMEGA_NO_STACK 35u\n#define OMBR_STACK (8u << 20)\n#define OMBR_GUARD (8u << 20)\n/* entry[2] is the trap entry (OSC-1 section 7). */\ntypedef struct ombr_rt { const void *entry[32]; } ombr_rt;\ntypedef struct ombr_span { uintptr_t lo, hi; int w; } ombr_span;\nstatic _Thread_local volatile sig_atomic_t ombr_active;\nstatic _Thread_local unsigned char *ombr_stk;\nstatic pthread_key_t ombr_key;\nstatic int ombr_keyok;\nstatic pthread_once_t ombr_once = PTHREAD_ONCE_INIT;\nuint32_t ombr_enter(const void *code, const uint64_t *args, const ombr_rt *rt, uint64_t *out, void *top)\n __attribute__((visibility(\"hidden\")));\nextern const unsigned char ombr_trap[] __attribute__((visibility(\"hidden\")));\nextern const unsigned char ombr_no_entry[] __attribute__((visibility(\"hidden\")));\n/* ombr_enter saves the callee-saved registers on the caller's stack, keeps that stack pointer in x19 and\n * runs the code on the binding's own stack. A normal return stores x0 through out and\n * returns 0. A runtime entry restores the stack pointer from x19 and returns a status; out is not\n * written. The seeded compiler never writes x19..x28. Runtime entries are reached by blr/br, so they\n * start with bti jc. */\n__asm__(\".text\\n.balign 4\\n.local ombr_enter\\n.hidden ombr_enter\\n.type ombr_enter,%function\\nombr_enter:\\n\"\n \"hint #34\\nstp x29,x30,[sp,#-176]!\\nmov x29,sp\\nstp x19,x20,[sp,#16]\\nstp x21,x22,[sp,#32]\\n\"\n \"stp x23,x24,[sp,#48]\\nstp x25,x26,[sp,#64]\\nstp x27,x28,[sp,#80]\\nstp d8,d9,[sp,#96]\\n\"\n \"stp d10,d11,[sp,#112]\\nstp d12,d13,[sp,#128]\\nstp d14,d15,[sp,#144]\\nstr x3,[sp,#160]\\n\"\n \"mov x19,sp\\nmov x16,x0\\nmov x17,x1\\nmov x7,x2\\nmov sp,x4\\n\"\n \"ldp x0,x1,[x17]\\nldp x2,x3,[x17,#16]\\nldp x4,x5,[x17,#32]\\nblr x16\\n\"\n \"mov sp,x19\\nldr x9,[sp,#160]\\nstr x0,[x9]\\nmov w0,#0\\n\"\n \"ombr_leave:\\nldp d14,d15,[sp,#144]\\nldp d12,d13,[sp,#128]\\nldp d10,d11,[sp,#112]\\n\"\n \"ldp d8,d9,[sp,#96]\\nldp x27,x28,[sp,#80]\\nldp x25,x26,[sp,#64]\\nldp x23,x24,[sp,#48]\\n\"\n \"ldp x21,x22,[sp,#32]\\nldp x19,x20,[sp,#16]\\nldp x29,x30,[sp],#176\\nret\\n.size ombr_enter,.-ombr_enter\\n\"\n \".local ombr_trap\\n.hidden ombr_trap\\n.type ombr_trap,%function\\nombr_trap:\\nhint #38\\n\"\n \"mov sp,x19\\nsub x2,x1,#1\\nmov w3,#34\\ncmp x2,#13\\ncsel w0,w1,w3,ls\\nb ombr_leave\\n\"\n \".size ombr_trap,.-ombr_trap\\n\"\n \".local ombr_no_entry\\n.hidden ombr_no_entry\\n.type ombr_no_entry,%function\\nombr_no_entry:\\nhint #38\\n\"\n \"mov sp,x19\\nmov w0,#34\\nb ombr_leave\\n.size ombr_no_entry,.-ombr_no_entry\\n\");\nstatic void ombr_free(void *p)\n{\n munmap(p, OMBR_STACK + OMBR_GUARD);\n}\nstatic void ombr_init(void)\n{\n ombr_keyok = pthread_key_create(&ombr_key, ombr_free) == 0;\n}\n/* The thread's Omega stack: OMBR_STACK usable bytes above an OMBR_GUARD no-access region. A function-mode\n * unit has no call cycles and at most 4 MiB of local arrays per function (OMEGESE, local arrays), so a frame that\n * would pass the bottom starts inside the guard and faults there; nothing below the guard is touched. */\nstatic unsigned char *ombr_stack(void)\n{\n if (ombr_stk)\n return ombr_stk;\n pthread_once(&ombr_once, ombr_init);\n if (!ombr_keyok)\n return 0;\n unsigned char *p = mmap(0, OMBR_STACK + OMBR_GUARD, PROT_READ | PROT_WRITE,\n MAP_PRIVATE | MAP_ANONYMOUS | MAP_NORESERVE, -1, 0);\n if (p == MAP_FAILED)\n return 0;\n if (mprotect(p, OMBR_GUARD, PROT_NONE) != 0 || pthread_setspecific(ombr_key, p) != 0) {\n munmap(p, OMBR_STACK + OMBR_GUARD);\n return 0;\n }\n ombr_stk = p + OMBR_GUARD + OMBR_STACK;\n return ombr_stk;\n}\nstatic int ombr_span_add(ombr_span *sp, unsigned *ns, const void *p, uint64_t n, uint64_t want, uint64_t es, int w)\n{\n uintptr_t lo = (uintptr_t)p;\n if (!p || n != want || lo % es != 0u || n > (UINTPTR_MAX - lo) / es)\n return 1;\n sp[*ns].lo = lo;\n sp[*ns].hi = lo + n * es;\n sp[*ns].w = w;\n (*ns)++;\n return 0;\n}\nstatic int ombr_overlap(const ombr_span *sp, unsigned ns)\n{\n for (unsigned i = 0; i < ns; i++)\n for (unsigned j = i + 1; j < ns; j++)\n if ((sp[i].w || sp[j].w) && sp[i].lo < sp[j].hi && sp[j].lo < sp[i].hi)\n return 1;\n return 0;\n}\n/* One read-only runtime table for every call and thread: the code only reads it (a write would fault). */\nstatic const ombr_rt ombr_rt_table = {{ [0 ... 1] = ombr_no_entry, [2] = ombr_trap, [3 ... 31] = ombr_no_entry }};\nstatic uint32_t ombr_run(const void *code, const uint64_t *args, uint64_t *result, int has_value)\n{\n if (ombr_active)\n return OMEGA_REENTRY;\n ombr_active = 1;\n unsigned char *top = ombr_stack();\n if (!top) {\n ombr_active = 0;\n return OMEGA_NO_STACK;\n }\n uint64_t v = 0;\n uint32_t s = ombr_enter(code, args, &ombr_rt_table, &v, top);\n ombr_active = 0;\n if (s == OMEGA_OK)\n *result = has_value ? v : 0u;\n return s;\n}\n" note ob: append one byte to the output pool (trap 6 when it is full). fn ob b uchar to void n = get st 1 diff --git a/tests/native/private_symbols.c b/tests/native/private_symbols.c new file mode 100644 index 0000000..db66f8d --- /dev/null +++ b/tests/native/private_symbols.c @@ -0,0 +1,48 @@ +/* Independently generated bindings must retain private runtime and thread state. + * Values and overflow case reuse the qualified vectors in caller.c. */ +#include +#include +#include + +uint32_t omega_madd(uint64_t *, uint64_t, uint64_t, uint64_t); +uint32_t omega_smadd(uint64_t *, uint64_t, uint64_t, uint64_t); +static pthread_barrier_t ready; + +static void *exercise(void *arg) +{ + int *failed = arg; + int rc = pthread_barrier_wait(&ready); + if (rc != 0 && rc != PTHREAD_BARRIER_SERIAL_THREAD) { + *failed = 1; + return NULL; + } + for (unsigned i = 0; i < 10000; ++i) { + uint64_t r = UINT64_C(6148914691236517205); + if (omega_smadd(&r, UINT64_C(4611686018427387904), 4, 0) != 1 || + r != UINT64_C(6148914691236517205)) + *failed = 1; + if (omega_madd(&r, 3, 5, 7) != 0 || r != 22) + *failed = 1; + if (omega_smadd(&r, (uint64_t)(int64_t)-3, 5, 2) != 0 || (int64_t)r != -13) + *failed = 1; + } + return NULL; +} + +int main(void) +{ + pthread_t threads[4]; + int failed[4] = {0}; + if (pthread_barrier_init(&ready, NULL, 4) != 0) + return 1; + for (unsigned i = 0; i < 4; ++i) + if (pthread_create(&threads[i], NULL, exercise, &failed[i]) != 0) + return 1; + for (unsigned i = 0; i < 4; ++i) + if (pthread_join(threads[i], NULL) != 0 || failed[i]) + return 1; + if (pthread_barrier_destroy(&ready) != 0) + return 1; + puts("PRIVATE-SYMBOLS PASS: distinct bindings, trap recovery and concurrent threads"); + return 0; +} diff --git a/tests/native/run.sh b/tests/native/run.sh index b050fc7..e6b147f 100755 --- a/tests/native/run.sh +++ b/tests/native/run.sh @@ -122,6 +122,9 @@ cc -std=gnu11 -O2 -fPIC -shared -Wl,-soname,libomega_native.so -o "$O/mut/libome cc -std=gnu11 -O2 -pthread "$O/caller.c" -L"$O/mut" -lomega_native -Wl,-rpath,"$O/mut" -o "$O/mut/caller_c" if "$O/mut/caller_c" > "$O/mut/c.log" 2>&1; then fail "mutant madd (add -> sub) not caught" else pass "mutant madd (add -> sub) caught ($(grep -c '^FAIL' "$O/mut/c.log") failing checks)"; fi +if python3 "$T/test_private_symbols.py" > "$O/private-symbols.log" 2>&1; then + pass "separate binding translation units, private symbols, traps, threads and global-symbol mutant" +else fail "private binding symbols"; cat "$O/private-symbols.log"; fi for l in $notrun; do echo "NOT_RUN $l caller: toolchain not installed"; done if [ "$fails" -eq 0 ]; then for l in c c-bti cpp python rust zig go; do diff --git a/tests/native/test_private_symbols.py b/tests/native/test_private_symbols.py new file mode 100755 index 0000000..0188a58 --- /dev/null +++ b/tests/native/test_private_symbols.py @@ -0,0 +1,73 @@ +#!/usr/bin/env python3 +"""Host-only linkage regression; no device or application is opened.""" +import os +from pathlib import Path +import subprocess +import tempfile + +ROOT = Path(__file__).resolve().parents[2] +OMC = os.environ["OMEGA_NATIVE_OMC"] +CC = os.environ.get("CC", "cc") +PIN = "734f0a39319fbfa930bcc62cb30dbf06d47d78e126372b74f5baf478e6126b61" + + +def run(args, **kwargs): + return subprocess.run(args, check=True, capture_output=True, **kwargs) + + +def build(generator, out): + out.mkdir() + objects = [] + for name in ("madd", "smadd"): + source = (ROOT / "tests/native" / (name + ".om")).read_bytes() + code = run([OMC], input=source).stdout + frame = source + b"\0" + f"{len(code):08d}".encode() + code + cfile = out / (name + ".c") + cfile.write_bytes(run([str(generator)], input=frame).stdout) + obj = out / (name + ".o") + run([CC, "-std=gnu11", "-O2", "-Wall", "-Wextra", "-Werror", "-c", str(cfile), "-o", str(obj)]) + objects.append(obj) + result = subprocess.run([CC, "-pthread", str(ROOT / "tests/native/private_symbols.c"), + *map(str, objects), "-o", str(out / "caller")], capture_output=True) + (out / "link.log").write_bytes(result.stdout + result.stderr) + return result, objects + + +def generator(source, out): + out.write_bytes(run([OMC], input=source).stdout) + out.chmod(0o755) + return out + + +def main(): + import hashlib + assert hashlib.sha256(Path(OMC).read_bytes()).hexdigest() == PIN, "compiler pin mismatch" + # Optional source override also permits replaying the pre-fix generator. + source = Path(os.environ.get("OMEGA_NATIVE_SOURCE", ROOT / "src/native.om")).read_bytes() + with tempfile.TemporaryDirectory(prefix="omega-private-symbols-") as tmp: + tmp = Path(tmp) + gen = generator(source, tmp / "native") + linked, objects = build(gen, tmp / "normal") + if linked.returncode: + print(linked.stderr.decode(), end="") + raise AssertionError("separately generated bindings must link") + for name, obj in zip(("madd", "smadd"), objects): + symbols = run(["nm", "--defined-only", str(obj)], text=True).stdout.splitlines() + public = {line.split()[-1] for line in symbols if line.split()[-2].isupper()} + assert public == {"omega_" + name}, (obj, public) + private = {line.split()[-1]: line.split()[-2] for line in symbols + if line.split()[-1].startswith("ombr_")} + for helper in ("ombr_enter", "ombr_trap", "ombr_no_entry", "ombr_code_" + name, + "ombr_entry_" + name, "ombr_active", "ombr_stk", "ombr_rt_table"): + assert helper in private and private[helper].islower(), (helper, private) + print(run([str(tmp / "normal/caller")], text=True).stdout, end="") + # Restore just the original defect, not arbitrary code or public wrappers. + mutant = source.replace(b".local ombr_enter", b".globl ombr_enter") + assert mutant != source, "missing local helper directive" + linked, _ = build(generator(mutant, tmp / "mutant"), tmp / "global") + assert linked.returncode != 0 and b"multiple definition" in linked.stderr and b"ombr_enter" in linked.stderr, linked.stderr + print("PRIVATE-SYMBOLS PASS: nm public ABI unchanged, private locals; .globl mutant rejected") + + +if __name__ == "__main__": + main()