From 8e1f3cf19988b754489262b4b399d65861454ad1 Mon Sep 17 00:00:00 2001 From: Drake Stapleton Date: Sat, 24 Oct 2026 18:25:26 -0500 Subject: [PATCH 1/4] native call v1: one generator writes the bindings for C, C++, Python, Rust, Zig and Go src/native.om takes an output word: c (the library source, unchanged bytes) or h, python, rust, zig, go (that library's declarations for the language), all written from the same parsed Omega signatures. tools/native compiles the units with the pinned omc, writes the six files and builds libomega_native.so with its own header included, so a declaration that disagrees with its definition fails the build. The six test callers now take every Omega declaration from their generated file; the run fails if any caller declares one by hand. tests/native/run.sh: tools/native for the main and the BTI build, the no-hand-declaration check, refusals of a missing and of an unknown output word, and five generator mutants (one scalar type word per binding), each of which must be caught by that language's build or run. Suite at this tree: NATIVE-CALL PASS (22 checks, not run: none) with gcc, g++, python3, rustc, Zig 0.13.0, Go 1.27.3 on omc 734f0a39. Co-authored-by: Drake Stapleton Co-Authored-By: Claude Fable 5.1 --- README.md | 10 +- docs/CAPABILITIES.md | 2 +- docs/NATIVE-ABI.md | 56 +++++++++- src/native.om | 242 ++++++++++++++++++++++++++++++++++++++-- tests/native/caller.c | 8 +- tests/native/caller.cpp | 6 +- tests/native/caller.go | 41 +++---- tests/native/caller.py | 24 ++-- tests/native/caller.rs | 9 +- tests/native/caller.zig | 23 ++-- tests/native/run.sh | 112 +++++++++++++------ tools/native | 42 +++++++ 12 files changed, 457 insertions(+), 118 deletions(-) create mode 100755 tools/native diff --git a/README.md b/README.md index 8112f79..0258f97 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # Omega Bridge -An Omega-written compiler that emits executable C and Python from a checked, bounded Omega subset. Existing C and Python applications can also call generated library functions. Apache-2.0 WITH LLVM-exception. +An Omega-written compiler that emits executable C and Python from a checked, bounded Omega subset. Existing C and Python applications can also call generated library functions, and compiled Omega functions can be called in place from C, C++, Python, Rust, Zig and Go through the native call adapter. Apache-2.0 WITH LLVM-exception. The source-only compiler bootstrap currently requires Linux AArch64. Generated C uses GNU C11 and POSIX; generated Python uses Python 3. This branch has local differential tests and caller demos; independent review remains required before release. No universal conversion or performance improvement is claimed. @@ -34,6 +34,14 @@ quiet-build python3 tests/attachments.py See [supported features and limits](docs/CAPABILITIES.md), [caller ABI and ownership](docs/ABI.md), [design and study decisions](docs/DESIGN.md), and [provenance](docs/PROVENANCE.md). Text constants, floats, heap operations and other target languages are explicitly unsupported by the C/Python converters. GPU interoperability is a separate, opt-in adapter boundary (it converts no host program): see [GPU adapter](docs/GPU-ADAPTER.md) and the [support matrix](docs/GPU-SUPPORT-MATRIX.md). Python/C input conversion is not implemented. The Python test harness is infrastructure; all conversion logic and both runtime emitters are in [src/toc.om](src/toc.om). +Call compiled Omega in place from six languages (the native call adapter, a separate claim from conversion: it runs Omega's own machine code, it does not turn Omega into another language): + +```sh +OMEGA_NATIVE_OMC= tools/native build/native tests/native/madd.om +``` + +That writes `libomega_native.so` and, from the same Omega signatures, `omega_native.h` (C and C++), `omega_native.py`, `omega_native.rs`, `omega_native.zig` and `omega_native.go`; a program includes or imports its file and calls `omega_madd`, nothing is declared by hand. One generator, [src/native.om](src/native.om), writes all six. Contract, limits and the qualification run: [native call ABI](docs/NATIVE-ABI.md). + ## License First-party material is licensed under the Apache License, Version 2.0 with LLVM diff --git a/docs/CAPABILITIES.md b/docs/CAPABILITIES.md index 5e31688..9fbf0c2 100644 --- a/docs/CAPABILITIES.md +++ b/docs/CAPABILITIES.md @@ -28,6 +28,6 @@ For loops evaluate a compound count once; a variable count is read each iteratio Full-program results are compared byte-for-byte on stdout/stderr and process status. Trap exits are 100 + code; explicit exit and main return use the low eight bits. I/O tests use ordinary pipes/files with no asynchronous signals. Neither backend optimizes away checks. -Other targets need a real emitter, capability checks, reference executor, differential corpus and an attachment gate. Rust, JavaScript, Go, Java, C#, Fortran and other languages have no implemented or tested adapter here. Their possible use of the C ABI is not a support claim. +Other conversion targets need a real emitter, capability checks, reference executor, differential corpus and an attachment gate; no language other than C and Python has a converter. Calling a compiled Omega function in place from C, C++, Python, Rust, Zig or Go is the native call adapter (docs/NATIVE-ABI.md): `tools/native` builds the library and writes each language's declarations from the Omega signatures. That is a call into Omega's machine code, a separate claim from conversion. JavaScript, Java, C#, Fortran and other languages have no implemented or tested adapter here. The seed's optional grouped-expression syntax and explicit `view A` argument prefix are not lowered by this bridge; use ordinary prefix expressions and view-typed parameters. Source constructs beyond the table may be accepted by the seed and then refused by the target compiler. Error 24 includes an unsupported-capability diagnostic; errors 13/14/23 identify name, table/output or storage limits. No target output is published on these failures. diff --git a/docs/NATIVE-ABI.md b/docs/NATIVE-ABI.md index 4b91be3..02abc87 100644 --- a/docs/NATIVE-ABI.md +++ b/docs/NATIVE-ABI.md @@ -24,8 +24,11 @@ end ## The binding `src/native.om` is an Omega program. It reads units (the unit's source, a 0 byte, the code length as 8 -decimal digits, the code) and writes one C file. Output appears only after every unit is accepted; -anything outside the supported signature set is refused (exit 110, no output). The C file: +decimal digits, the code) and one argument naming the output: `c` is the library source, one C file; +`h`, `python`, `rust`, `zig` and `go` are that library's declarations for those languages (next +section), written from the same parsed signatures. Output appears only after every unit is accepted; +anything outside the supported signature set, and a missing or unknown output word, is refused (exit +110, no output). The C file: - embeds each unit's code byte for byte (tests/native/run.sh compares the bytes inside the built library with the compiler's output); @@ -46,6 +49,36 @@ Names must be C identifiers, a parameter name may not end in `_len` (the binding 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`; one function per unit. +## The declarations for each language (`tools/native`) + +`OMEGA_NATIVE_OMC= tools/native OUTDIR unit.om...` compiles the units with the pinned +compiler, runs the generator once per output word and writes, into OUTDIR: + +| file | for | how a program uses it | +|---|---|---| +| `omega_native.c` | the library source | built into `libomega_native.so` by the same command | +| `omega_native.h` | C and C++ | `#include "omega_native.h"` (`extern "C"` inside for C++) | +| `omega_native.py` | Python 3 (ctypes) | `import omega_native`; loads `libomega_native.so` from beside itself | +| `omega_native.rs` | Rust | `mod omega_native;` beside the crate root; link with `-L OUTDIR` | +| `omega_native.zig` | Zig 0.13 | `@import("omega_native.zig")`; build with `-L OUTDIR -lomega_native -lc` | +| `omega_native.go` | Go (cgo) | package `omeganative`: a folder of that name holding the file and the library | + +Every file carries the unit's Omega signature line as a comment above its declaration, the five status +constants (0, 32, 33, 34, 35) and the parameter names prefixed `o_` (so no language's reserved words +collide). The scalar type is the language's unsigned 64-bit integer, an array is a pointer then a +`uint64_t` count, the status is unsigned 32-bit: C `uint64_t` / `void *` / `const void *`; Python +`ctypes.c_uint64` / `ctypes.c_void_p` (any ctypes array or address); Rust `u64` / `*mut u64`, `*mut u8`, +`*const u64`, `*const u8`; Zig `u64` / `?[*]u64`, `?[*]u8`, `?[*]const u64`, `?[*]const u8`; Go +`uint64` / `unsafe.Pointer` to memory holding no Go pointers. The result pointer is `uint64_t *`, +`ctypes.POINTER(c_uint64)` (pass `ctypes.byref`), `*mut u64`, `?*u64`, `*uint64`. The Go file wraps +each C function as `Omega_` so the caller needs no cgo of its own. + +All six files are generated before any is written, so a refused unit leaves OUTDIR without a half set. +The library is compiled with its own header included (`-include omega_native.h`): a declaration that +disagrees with a definition fails that build, so the C file and the header cannot drift apart. The +other four files are checked by the qualification below, where each language builds its caller against +its generated file only and a one-word type change in the generator must make that build or run fail. + ## Contract | | | @@ -87,7 +120,9 @@ omega-mv PR #696). Expected values come from receipts, not from Omega: mod 2^64 = 3637949782307626511 (calc:9bf5b67e, Wolfram agrees); -13 as 64-bit bits = 18446744073709551603 (calc:cbb0735b); 0+1+...+999999 = 499999500000 (calc:3f197279). -The C, C++, Python and Rust callers check values, wrap-around, signed bits, a signed overflow trap with +The six callers (C, C++, Python, Rust, Zig, Go) take every Omega declaration from their generated file +(the run fails if any caller source declares an `omega_` function, sets ctypes `argtypes`, loads the +library itself or carries its own cgo). They check values, wrap-around, signed bits, a signed overflow trap with the result left unchanged, a trap after two buffer writes (writes visible, result unchanged), a successful buffer call and a refused null result. The C caller also checks a null buffer, a wrong length, misalignment, result/buffer overlap, a view overlapping an edit array, a call after traps, reentry from a @@ -113,4 +148,17 @@ Zig and Go: run with Zig 0.13.0 and Go 1.27.3 (official aarch64 archives, checks ziglang.org/download/index.json and go.dev/dl, unpacked in the user's ~/.local/opt). Both callers pass 10 checks. The library is linked with `-soname libomega_native.so`: without that name, Zig records the library as `./libomega_native.so` and the caller fails to load outside the build folder (red: dd8b9a9; -green: this change). The embedded code is unchanged by the name. +green: 1e40844). The embedded code is unchanged by the name. + +Generated declarations (this change): before it, each caller typed its own prototypes and a wrong +type there was the caller's mistake to find. Now `tools/native` builds everything and the generator +writes the six files above from the parsed signatures; the C output is byte for byte what the previous +generator wrote. The run adds: the no-hand-declaration check; refusals of a missing and of an unknown +output word; and five generator mutants, one per binding, each changing one scalar type word in the +generator's text (`uint64_t` to `uint32_t`, `c_uint64` to `c_uint32`, `u64` to `u32` twice, `uint64` +to `uint32`). Each is caught: the header mutant by the library build (`conflicting types for +omega_madd`), the Python mutant by wrong results (two of the ten checks fail: the argument is +truncated to 32 bits), the Rust, Zig and Go mutants by their compilers (a 64-bit value passed to a +32-bit parameter). Found by reading the output before the run, not by a test: the first Go wrapper +called `C.omega_(` with no name; the test that would have caught it is the Go build, which this +change runs. diff --git a/src/native.om b/src/native.om index ef9d32d..ecbf3ca 100644 --- a/src/native.om +++ b/src/native.om @@ -1,13 +1,17 @@ note native.om: omega-bridge native call ABI v1 binding generator (docs/NATIVE-ABI.md). note Input on stdin, one or more units: the unit's .om source, a 0 byte, its code length as 8 decimal note digits, then the function-mode machine code the seeded compiler wrote for that source. -note Output on stdout, only after every unit is accepted: one C file that embeds each code unchanged -note and exports uint32_t omega_(uint64_t *result, ...) per unit. A unit holds one function; -note parameters are ulong or long scalars, or edit/view arrays of ulong or uchar; the result is ulong, -note long or void. Anything else is refused: trap 10 (exit 110) and no output. +note One argument names the output: c is the library source, one C file that embeds each code +note unchanged and exports uint32_t omega_(uint64_t *result, ...) per unit; h, python, rust, +note zig and go are that library's declarations for those languages, written from the same +note signatures (tools/native runs all six). Output on stdout, only after every unit is accepted. +note A unit holds one function; parameters are ulong or long scalars, or edit/view arrays of ulong +note or uchar; the result is ulong, long or void. Anything else, and a missing or unknown argument, +note is refused: trap 10 (exit 110) and no output. global inb uchar 1048576 global outb uchar 4194304 -global st ulong 8 +global st ulong 16 +global md uchar 8 global tks ulong 40 global tke ulong 40 global pk ulong 6 @@ -21,6 +25,10 @@ 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, 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}\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 ombr_rt rt;\n for (int i = 0; i < 32; i++)\n rt.entry[i] = ombr_no_entry;\n rt.entry[2] = ombr_trap;\n uint64_t v = 0;\n uint32_t s = ombr_enter(code, args, &rt, &v, top);\n ombr_active = 0;\n if (s == OMEGA_OK)\n *result = has_value ? v : 0u;\n return s;\n}\n" +note mo: the output names; tb: the binding texts, fragments separated by ~ (byte 126): 0-11 h, +note 12-21 python, 22-34 rust, 35-46 zig, 47-67 go (48-49 with the h types 3-10 for the C preamble). +text mo "chpythonrustziggo" +text tb "/* Generated by omega-bridge src/native.om: native call ABI v1 (docs/NATIVE-ABI.md).\n * C and C++ declarations for libomega_native. Regenerate it; do not edit it.\n * Status: 0 done; 1..14 the Omega trap code; 32 argument refused (null, wrong length, misaligned,\n * overlap); 33 reentry; 34 bad runtime entry; 35 no stack. The result is written only on status 0.\n * A long is its two's-complement bits; an array is its address then its element count.\n * Parameter names carry o_ so no language's reserved words collide. */\n#ifndef OMEGA_NATIVE_H\n#define OMEGA_NATIVE_H\n#include \n#ifdef __cplusplus\nextern \"C\" {\n#endif\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~/* ~ */\nuint32_t omega_~(uint64_t *result~, uint64_t o_~, void *o_~, const void *o_~, void *o_~, const void *o_~_len~);\n~#ifdef __cplusplus\n}\n#endif\n#endif\n~# Generated by omega-bridge src/native.om: native call ABI v1 (docs/NATIVE-ABI.md).\n# ctypes declarations for libomega_native.so beside this file. Regenerate it; do not edit it.\n# Status: 0 done; 1..14 the Omega trap code; 32 argument refused (None, wrong length, misaligned,\n# overlap); 33 reentry; 34 bad runtime entry; 35 no stack. The result (a ctypes.c_uint64 passed with\n# ctypes.byref) is written only on status 0. A long is its two's-complement bits; an array is a\n# ctypes array (or any address) then its element count.\nimport ctypes, os\nOMEGA_OK, OMEGA_BAD_ARG, OMEGA_REENTRY, OMEGA_BAD_RT, OMEGA_NO_STACK = 0, 32, 33, 34, 35\nlib = ctypes.CDLL(os.path.join(os.path.dirname(os.path.abspath(__file__)), \"libomega_native.so\"))\n_u64 = ctypes.c_uint64\n_res = ctypes.POINTER(ctypes.c_uint64)\n_arr = ctypes.c_void_p\n~# ~\nomega_~ = lib.omega_~\nomega_~.argtypes = [_res~, _u64~, _arr~]\nomega_~.restype = ctypes.c_uint32\n~// Generated by omega-bridge src/native.om: native call ABI v1 (docs/NATIVE-ABI.md).\n// Declarations for libomega_native (build with -L ). Regenerate it; do not edit it.\n// Status: 0 done; 1..14 the Omega trap code; 32 argument refused (null, wrong length, misaligned,\n// overlap); 33 reentry; 34 bad runtime entry; 35 no stack. The result is written only on status 0.\n// A long is its two's-complement bits (an i64 as u64); an array is its pointer then its element count.\n#[allow(dead_code)]\npub const OMEGA_OK: u32 = 0;\n#[allow(dead_code)]\npub const OMEGA_BAD_ARG: u32 = 32;\n#[allow(dead_code)]\npub const OMEGA_REENTRY: u32 = 33;\n#[allow(dead_code)]\npub const OMEGA_BAD_RT: u32 = 34;\n#[allow(dead_code)]\npub const OMEGA_NO_STACK: u32 = 35;\n#[allow(dead_code)]\n#[link(name = \"omega_native\")]\nextern \"C\" {\n~ // ~\n pub fn omega_~(result: *mut u64~, o_~: u64~: *mut u64~: *const u64~: *mut u8~: *const u8~_len: u64~) -> u32;\n~}\n~// Generated by omega-bridge src/native.om: native call ABI v1 (docs/NATIVE-ABI.md).\n// Declarations for libomega_native (build with -L -lomega_native -lc). Regenerate it; do not edit it.\n// Status: 0 done; 1..14 the Omega trap code; 32 argument refused (null, wrong length, misaligned,\n// overlap); 33 reentry; 34 bad runtime entry; 35 no stack. The result is written only on status 0.\n// A long is its two's-complement bits (@bitCast); an array is its pointer then its element count.\npub const OMEGA_OK: u32 = 0;\npub const OMEGA_BAD_ARG: u32 = 32;\npub const OMEGA_REENTRY: u32 = 33;\npub const OMEGA_BAD_RT: u32 = 34;\npub const OMEGA_NO_STACK: u32 = 35;\n~// ~\npub extern fn omega_~(result: ?*u64~, o_~: u64~: ?[*]u64~: ?[*]const u64~: ?[*]u8~: ?[*]const u8~_len: u64~) u32;\n~// Generated by omega-bridge src/native.om: native call ABI v1 (docs/NATIVE-ABI.md).\n// cgo bindings for libomega_native.so beside this file. Regenerate it; do not edit it.\n// Status: 0 done; 1..14 the Omega trap code; 32 argument refused (nil, wrong length, misaligned,\n// overlap); 33 reentry; 34 bad runtime entry; 35 no stack. The result is written only on status 0.\n// A long is its two's-complement bits (an int64 as uint64); an array is an unsafe.Pointer to memory\n// holding no Go pointers, then its element count.\npackage omeganative\n\n/*\n#cgo LDFLAGS: -L${SRCDIR} -lomega_native -Wl,-rpath,${SRCDIR}\n#include \n~// ~\nuint32_t omega_~*/\nimport \"C\"\n\nimport \"unsafe\"\n\nconst (\n\tOmegaOK uint32 = 0\n\tOmegaBadArg uint32 = 32\n\tOmegaReentry uint32 = 33\n\tOmegaBadRt uint32 = 34\n\tOmegaNoStack uint32 = 35\n)\n~\n// ~\nfunc Omega_~(result *uint64~, o_~ uint64~ unsafe.Pointer~ unsafe.Pointer~ unsafe.Pointer~ unsafe.Pointer~_len uint64~) uint32 {\n\treturn uint32(C.omega_~((*C.uint64_t)(unsafe.Pointer(result))~, C.uint64_t(o_~)~, o_~_len)~))\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 @@ -82,6 +90,66 @@ end end end end +note ofb: fragment k of tb. +fn ofb k ulong to void +c = ulong 0 +for i ulong len tb +b = get tb i +if eq b 126 +c = add c 1 +else +if eq c k +ob b +end +end +end +end +note mdis: 1 when the argument in md (its length in st 8) is mo[o .. o+n-1], else 0. +fn mdis o ulong n ulong to ulong +if ne get st 8 n +ret 0 +end +for j ulong n +if ne get md j get mo add o j +ret 0 +end +end +ret 1 +end +note rmode: the one argument as an output number: 1 c, 2 h, 3 python, 4 rust, 5 zig, 6 go. +fn rmode to ulong +if ne argcnt 2 +trap 10 +end +k = argread 1 md 0 8 +if lt k 0 +trap 10 +end +put st 8 ulong k +m = ulong 0 +if eq mdis 0 1 1 +m = 1 +end +if eq mdis 1 1 1 +m = 2 +end +if eq mdis 2 6 1 +m = 3 +end +if eq mdis 8 4 1 +m = 4 +end +if eq mdis 12 3 1 +m = 5 +end +if eq mdis 15 2 1 +m = 6 +end +if eq m 0 +trap 10 +end +ret m +end note tkis: 1 when token k is the keyword kw[o .. o+n-1], else 0. fn tkis k ulong o ulong n ulong to ulong s = get tks k @@ -119,6 +187,7 @@ e = p while and lt e q ne get inb e 10 e = add e 1 end +put st 7 e n = ulong 0 s = p i = p @@ -148,6 +217,10 @@ note opn: the name of parameter j. fn opn j ulong to void orng get ps j get pe j end +note osig: the unit's signature line (st 6 .. st 7), the comment each binding carries. +fn osig to void +orng get st 6 get st 7 +end note sepat: where the 0 byte after the source starting at p is. fn sepat p ulong to ulong t = get st 0 @@ -340,6 +413,7 @@ end note parse: read the signature line at fl; parameters into ps pe pk pl, their count into st 2, note 1 or 0 into st 3 for a function with or without a value. fn parse fl ulong q ulong to void +put st 6 fl nt = tokln fl q if lt nt 4 trap 10 @@ -494,16 +568,131 @@ ofr 22 odec get st 3 ofr 23 end -note unit: accept the unit at p, emit its C, return where the next unit starts. +note edeclC: a declaration with the type before the name (h; the C preamble of go): the comment +note fragments at cb (open, close + head) and the type fragments at tb (result, scalar, the four +note array kinds, the length suffix, close). +fn edeclC cb ulong tp ulong to void +ofb cb +osig +ofb add cb 1 +oname 1 +ofb tp +for j ulong get st 2 +x = get pk j +if le x 2 +ofb add tp 1 +opn j +else +ofb add tp sub x 1 +opn j +ofb add tp 1 +opn j +ofb add tp 6 +end +end +ofb add tp 7 +end +note edeclR: a declaration with the name before the type (rust, zig, the go wrapper head): the +note type fragments at tp are result, the name prefix, scalar, the four array kinds, length, close. +fn edeclR cb ulong tp ulong to void +ofb cb +osig +ofb add cb 1 +oname 1 +ofb tp +for j ulong get st 2 +x = get pk j +ofb add tp 1 +opn j +if le x 2 +ofb add tp 2 +else +ofb add tp x +ofb add tp 1 +opn j +ofb add tp 7 +end +end +ofb add tp 8 +end +note epy: the python declaration: the function, its argument types, its result type. +fn epy to void +ofb 13 +osig +ofb 14 +oname 1 +ofb 15 +oname 1 +ofb 16 +oname 1 +ofb 17 +for j ulong get st 2 +if le get pk j 2 +ofb 18 +else +ofb 19 +ofb 18 +end +end +ofb 20 +oname 1 +ofb 21 +end +note ecall: the go wrapper's call of the C function and its end. +fn ecall to void +oname 1 +ofb 62 +for j ulong get st 2 +if le get pk j 2 +ofb 63 +opn j +ofb 64 +else +ofb 65 +opn j +ofb 63 +opn j +ofb 66 +end +end +ofb 67 +end +note emit: the parsed unit in the output named by st 5 (7 is the second pass of go). +fn emit c0 ulong cl ulong to void +m = get st 5 +if eq m 1 +ecode c0 cl +esig +ebody +end +if eq m 2 +edeclC 1 3 +end +if eq m 3 +epy +end +if eq m 4 +edeclR 23 25 +end +if eq m 5 +edeclR 36 38 +end +if eq m 6 +edeclC 48 3 +end +if eq m 7 +edeclR 51 53 +ecall +end +end +note unit: accept the unit at p, emit it, return where the next unit starts. fn unit p ulong to ulong q = sepat p fl = sigln p q parse fl q cl = codelen q c0 = add q 9 -ecode c0 cl -esig -ebody +emit c0 cl ret add c0 cl end note slurp: all of stdin into inb; refuse input that fills the pool. @@ -526,11 +715,31 @@ end ret t end fn main to int +m = rmode t = slurp put st 0 t put st 1 0 put st 4 0 +put st 5 m +if eq m 1 opre +else +if eq m 2 +ofb 0 +end +if eq m 3 +ofb 12 +end +if eq m 4 +ofb 22 +end +if eq m 5 +ofb 35 +end +if eq m 6 +ofb 47 +end +end p = ulong 0 nu = ulong 0 while lt p t @@ -540,6 +749,21 @@ end if eq nu 0 trap 10 end +if eq m 2 +ofb 11 +end +if eq m 4 +ofb 34 +end +if eq m 6 +ofb 50 +put st 5 7 +put st 4 0 +p = 0 +while lt p t +p = unit p +end +end o = ulong 0 m = get st 1 while lt o m diff --git a/tests/native/caller.c b/tests/native/caller.c index 15e3247..fc0bd43 100644 --- a/tests/native/caller.c +++ b/tests/native/caller.c @@ -7,13 +7,7 @@ #include #include -uint32_t omega_madd(uint64_t *result, uint64_t a, uint64_t b, uint64_t c); -uint32_t omega_smadd(uint64_t *result, uint64_t a, uint64_t b, uint64_t c); -uint32_t omega_fill(uint64_t *result, void *buf, uint64_t buf_len, uint64_t k); -uint32_t omega_spin(uint64_t *result, uint64_t n); -uint32_t omega_mix(uint64_t *result, uint64_t a, uint64_t b, const void *c, uint64_t c_len, void *d, uint64_t d_len, - const void *e, uint64_t e_len, uint64_t f); -uint32_t omega_big(uint64_t *result, uint64_t n); +#include "omega_native.h" /* every Omega declaration: generated by tools/native, nothing typed by hand */ #define SENTINEL UINT64_C(6148914691236517205) static int fails; diff --git a/tests/native/caller.cpp b/tests/native/caller.cpp index fe7b59b..688f72a 100644 --- a/tests/native/caller.cpp +++ b/tests/native/caller.cpp @@ -3,11 +3,7 @@ #include #include -extern "C" { -std::uint32_t omega_madd(std::uint64_t *result, std::uint64_t a, std::uint64_t b, std::uint64_t c); -std::uint32_t omega_smadd(std::uint64_t *result, std::uint64_t a, std::uint64_t b, std::uint64_t c); -std::uint32_t omega_fill(std::uint64_t *result, void *buf, std::uint64_t buf_len, std::uint64_t k); -} +#include "omega_native.h" // every Omega declaration: generated by tools/native, nothing typed by hand static int fails; static void check(const char *name, bool ok) diff --git a/tests/native/caller.go b/tests/native/caller.go index 61f5732..6218214 100644 --- a/tests/native/caller.go +++ b/tests/native/caller.go @@ -1,21 +1,14 @@ -// Go caller (cgo) for the omega-bridge native call ABI v1 test library. -// Run with Go 1.27.3 (cgo) by tests/native/run.sh (10 checks). +// Go caller for the omega-bridge native call ABI v1 test library. +// Run with Go 1.27.3 by tests/native/run.sh (10 checks). Every Omega declaration comes from the +// omeganative package (omega_native.go, generated by tools/native); this file has no cgo of its own. package main -/* -#cgo LDFLAGS: -L${SRCDIR} -lomega_native -Wl,-rpath,${SRCDIR} -#include -#include -uint32_t omega_madd(uint64_t *result, uint64_t a, uint64_t b, uint64_t c); -uint32_t omega_smadd(uint64_t *result, uint64_t a, uint64_t b, uint64_t c); -uint32_t omega_fill(uint64_t *result, void *buf, uint64_t buf_len, uint64_t k); -*/ -import "C" - import ( "fmt" "os" "unsafe" + + on "omegacaller/omeganative" ) var fails int @@ -31,21 +24,19 @@ func check(name string, ok bool) { func main() { const sentinel = uint64(6148914691236517205) - var r C.uint64_t - check("madd 3*5+7 = 22", C.omega_madd(&r, 3, 5, 7) == 0 && r == 22) - check("madd wraps = 3", C.omega_madd(&r, C.uint64_t(^uint64(0)), 2, 5) == 0 && r == 3) - check("madd large", C.omega_madd(&r, 1311768467463790320, 18364758544493064720, 1085102592571150095) == 0 && r == 3637949782307626511) + var r uint64 + check("madd 3*5+7 = 22", on.Omega_madd(&r, 3, 5, 7) == 0 && r == 22) + check("madd wraps = 3", on.Omega_madd(&r, ^uint64(0), 2, 5) == 0 && r == 3) + check("madd large", on.Omega_madd(&r, 1311768467463790320, 18364758544493064720, 1085102592571150095) == 0 && r == 3637949782307626511) m3 := int64(-3) - check("madd signed bits = -13", C.omega_madd(&r, C.uint64_t(uint64(m3)), 5, 2) == 0 && int64(r) == -13) - r = C.uint64_t(sentinel) - check("smadd overflow traps 1, result unchanged", C.omega_smadd(&r, 1<<62, 4, 0) == 1 && uint64(r) == sentinel) - buf := (*[4]C.uint64_t)(C.malloc(32)) // C memory: the call may not hold Go pointers past its return - defer C.free(unsafe.Pointer(buf)) - *buf = [4]C.uint64_t{} - check("fill k=1 traps 3, result unchanged", C.omega_fill(&r, unsafe.Pointer(buf), 4, 1) == 3 && uint64(r) == sentinel) + check("madd signed bits = -13", on.Omega_madd(&r, uint64(m3), 5, 2) == 0 && int64(r) == -13) + r = sentinel + check("smadd overflow traps 1, result unchanged", on.Omega_smadd(&r, 1<<62, 4, 0) == 1 && r == sentinel) + var buf [4]uint64 // Go memory holding no Go pointers may be handed to C (cgo pointer rules) + check("fill k=1 traps 3, result unchanged", on.Omega_fill(&r, unsafe.Pointer(&buf[0]), 4, 1) == 3 && r == sentinel) check("writes before the trap stay visible", buf[0] == 11 && buf[1] == 22 && buf[2] == 0) - check("fill k=0 = 3", C.omega_fill(&r, unsafe.Pointer(buf), 4, 0) == 0 && r == 3 && buf[2] == 33) - check("null result refused 32", C.omega_madd(nil, 3, 5, 7) == 32) + check("fill k=0 = 3", on.Omega_fill(&r, unsafe.Pointer(&buf[0]), 4, 0) == 0 && r == 3 && buf[2] == 33) + check("null result refused 32", on.Omega_madd(nil, 3, 5, 7) == 32) if fails > 0 { fmt.Println("FAIL", fails, "failure(s)") os.Exit(1) diff --git a/tests/native/caller.py b/tests/native/caller.py index a36caf5..3a67756 100644 --- a/tests/native/caller.py +++ b/tests/native/caller.py @@ -1,10 +1,8 @@ # Python caller for the omega-bridge native call ABI v1 test library (ctypes, standard library only). import ctypes, os, sys -lib = ctypes.CDLL(os.path.join(os.path.dirname(os.path.abspath(__file__)), "libomega_native.so")) -u64, u32, P = ctypes.c_uint64, ctypes.c_uint32, ctypes.POINTER(ctypes.c_uint64) -for name in ("omega_madd", "omega_smadd"): - getattr(lib, name).argtypes, getattr(lib, name).restype = [P, u64, u64, u64], u32 -lib.omega_fill.argtypes, lib.omega_fill.restype = [P, ctypes.c_void_p, u64, u64], u32 +sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) # python3 -I drops this folder; the binding is beside us +import omega_native as om # every Omega declaration: generated by tools/native, nothing typed by hand +u64 = ctypes.c_uint64 SENTINEL = 6148914691236517205 fails = 0 def check(name, ok): @@ -16,17 +14,17 @@ def bits(v): # signed Python int -> canonical 64-bit two's-complement bits def signed(b): return b - 2**64 if b >= 2**63 else b r = u64(0) -check("madd 3*5+7 = 22", lib.omega_madd(ctypes.byref(r), 3, 5, 7) == 0 and r.value == 22) -check("madd wraps = 3", lib.omega_madd(ctypes.byref(r), 2**64 - 1, 2, 5) == 0 and r.value == 3) -check("madd large", lib.omega_madd(ctypes.byref(r), 1311768467463790320, 18364758544493064720, 1085102592571150095) == 0 +check("madd 3*5+7 = 22", om.omega_madd(ctypes.byref(r), 3, 5, 7) == 0 and r.value == 22) +check("madd wraps = 3", om.omega_madd(ctypes.byref(r), 2**64 - 1, 2, 5) == 0 and r.value == 3) +check("madd large", om.omega_madd(ctypes.byref(r), 1311768467463790320, 18364758544493064720, 1085102592571150095) == 0 and r.value == 3637949782307626511) -check("madd signed bits = -13", lib.omega_madd(ctypes.byref(r), bits(-3), 5, 2) == 0 and signed(r.value) == -13) +check("madd signed bits = -13", om.omega_madd(ctypes.byref(r), bits(-3), 5, 2) == 0 and signed(r.value) == -13) r = u64(SENTINEL) -check("smadd overflow traps 1, result unchanged", lib.omega_smadd(ctypes.byref(r), 2**62, 4, 0) == 1 and r.value == SENTINEL) +check("smadd overflow traps 1, result unchanged", om.omega_smadd(ctypes.byref(r), 2**62, 4, 0) == 1 and r.value == SENTINEL) buf = (u64 * 4)() -check("fill k=1 traps 3, result unchanged", lib.omega_fill(ctypes.byref(r), buf, 4, 1) == 3 and r.value == SENTINEL) +check("fill k=1 traps 3, result unchanged", om.omega_fill(ctypes.byref(r), buf, 4, 1) == 3 and r.value == SENTINEL) check("writes before the trap stay visible", list(buf) == [11, 22, 0, 0]) -check("fill k=0 = 3", lib.omega_fill(ctypes.byref(r), buf, 4, 0) == 0 and r.value == 3 and list(buf) == [11, 22, 33, 0]) -check("null result refused 32", lib.omega_madd(None, 3, 5, 7) == 32) +check("fill k=0 = 3", om.omega_fill(ctypes.byref(r), buf, 4, 0) == 0 and r.value == 3 and list(buf) == [11, 22, 33, 0]) +check("null result refused 32", om.omega_madd(None, 3, 5, 7) == 32) print(("FAIL " if fails else "PASS ") + "%d failure(s)" % fails) sys.exit(fails != 0) diff --git a/tests/native/caller.rs b/tests/native/caller.rs index ccb58da..4945d0b 100644 --- a/tests/native/caller.rs +++ b/tests/native/caller.rs @@ -1,11 +1,6 @@ // Rust caller for the omega-bridge native call ABI v1 test library (no crates). -#[link(name = "omega_native")] -extern "C" { - fn omega_madd(result: *mut u64, a: u64, b: u64, c: u64) -> u32; - fn omega_smadd(result: *mut u64, a: u64, b: u64, c: u64) -> u32; - fn omega_fill(result: *mut u64, buf: *mut u64, buf_len: u64, k: u64) -> u32; - fn omega_big(result: *mut u64, n: u64) -> u32; -} +mod omega_native; // every Omega declaration: generated by tools/native, nothing typed by hand +use omega_native::*; fn main() { const SENTINEL: u64 = 6148914691236517205; diff --git a/tests/native/caller.zig b/tests/native/caller.zig index 7006762..e82332d 100644 --- a/tests/native/caller.zig +++ b/tests/native/caller.zig @@ -1,9 +1,8 @@ // Zig caller for the omega-bridge native call ABI v1 test library. Written against Zig 0.13. -// Run with Zig 0.13.0 by tests/native/run.sh (10 checks). +// Run with Zig 0.13.0 by tests/native/run.sh (10 checks). Every Omega declaration comes from +// omega_native.zig, generated by tools/native beside this file; nothing is typed by hand here. const std = @import("std"); -extern fn omega_madd(result: ?*u64, a: u64, b: u64, c: u64) u32; -extern fn omega_smadd(result: ?*u64, a: u64, b: u64, c: u64) u32; -extern fn omega_fill(result: ?*u64, buf: ?*anyopaque, buf_len: u64, k: u64) u32; +const om = @import("omega_native.zig"); var fails: u32 = 0; fn check(name: []const u8, ok: bool) void { @@ -14,17 +13,17 @@ fn check(name: []const u8, ok: bool) void { pub fn main() void { const sentinel: u64 = 6148914691236517205; var r: u64 = 0; - check("madd 3*5+7 = 22", omega_madd(&r, 3, 5, 7) == 0 and r == 22); - check("madd wraps = 3", omega_madd(&r, std.math.maxInt(u64), 2, 5) == 0 and r == 3); - check("madd large", omega_madd(&r, 1311768467463790320, 18364758544493064720, 1085102592571150095) == 0 and r == 3637949782307626511); - check("madd signed bits = -13", omega_madd(&r, @bitCast(@as(i64, -3)), 5, 2) == 0 and @as(i64, @bitCast(r)) == -13); + check("madd 3*5+7 = 22", om.omega_madd(&r, 3, 5, 7) == 0 and r == 22); + check("madd wraps = 3", om.omega_madd(&r, std.math.maxInt(u64), 2, 5) == 0 and r == 3); + check("madd large", om.omega_madd(&r, 1311768467463790320, 18364758544493064720, 1085102592571150095) == 0 and r == 3637949782307626511); + check("madd signed bits = -13", om.omega_madd(&r, @bitCast(@as(i64, -3)), 5, 2) == 0 and @as(i64, @bitCast(r)) == -13); r = sentinel; - check("smadd overflow traps 1, result unchanged", omega_smadd(&r, 1 << 62, 4, 0) == 1 and r == sentinel); + check("smadd overflow traps 1, result unchanged", om.omega_smadd(&r, 1 << 62, 4, 0) == 1 and r == sentinel); var buf = [_]u64{ 0, 0, 0, 0 }; - check("fill k=1 traps 3, result unchanged", omega_fill(&r, &buf, 4, 1) == 3 and r == sentinel); + check("fill k=1 traps 3, result unchanged", om.omega_fill(&r, &buf, 4, 1) == 3 and r == sentinel); check("writes before the trap stay visible", buf[0] == 11 and buf[1] == 22 and buf[2] == 0); - check("fill k=0 = 3", omega_fill(&r, &buf, 4, 0) == 0 and r == 3 and buf[2] == 33); - check("null result refused 32", omega_madd(null, 3, 5, 7) == 32); + check("fill k=0 = 3", om.omega_fill(&r, &buf, 4, 0) == 0 and r == 3 and buf[2] == 33); + check("null result refused 32", om.omega_madd(null, 3, 5, 7) == 32); std.debug.print("{s} {d} failure(s)\n", .{ if (fails > 0) "FAIL" else "PASS", fails }); std.process.exit(if (fails > 0) 1 else 0); } diff --git a/tests/native/run.sh b/tests/native/run.sh index c5859b5..be34589 100755 --- a/tests/native/run.sh +++ b/tests/native/run.sh @@ -1,8 +1,10 @@ #!/bin/sh # Native call ABI v1 qualification (docs/NATIVE-ABI.md): one compiled Omega implementation of -# (a*b+c) modulo 2^64, called from C, C++, Python, Rust, Zig and Go through one generated C binding. -# OMEGA_NATIVE_OMC names the seeded compiler; its sha256 must be the pinned one below. -# A missing language toolchain is reported NOT_RUN, never PASS. Exit 0 only when every run check passed. +# (a*b+c) modulo 2^64, called from C, C++, Python, Rust, Zig and Go. tools/native builds the library +# and writes every language's declarations from the Omega signatures (one generator, src/native.om); +# no caller declares an Omega function by hand. OMEGA_NATIVE_OMC names the seeded compiler; its sha256 +# must be the pinned one below. A missing language toolchain is reported NOT_RUN, never PASS. +# Exit 0 only when every run check passed. set -eu PIN=734f0a39319fbfa930bcc62cb30dbf06d47d78e126372b74f5baf478e6126b61 ROOT=$(cd "$(dirname "$0")/../.." && pwd) @@ -15,15 +17,19 @@ rm -rf "$O"; mkdir -p "$O" "$O/mut" fails=0; notrun="" pass() { echo "PASS $1"; } fail() { echo "FAIL $1"; fails=$((fails + 1)); } -"$OMC" < "$ROOT/src/native.om" > "$O/native"; chmod +x "$O/native" U="madd smadd fill spin mix big" -for u in $U; do "$OMC" < "$T/$u.om" > "$O/$u.bin"; done -frame() { cat "$T/$1.om"; printf '\0'; printf '%08d' "$(wc -c < "$2/$1.bin")"; cat "$2/$1.bin"; } -gen() { for u in $U; do frame $u "$1"; done | "$O/native" > "$1/omega_native.c"; } -gen "$O" -# The library names itself (soname), so every caller records "libomega_native.so" and finds it via rpath; -# without it Zig records "./libomega_native.so", which only loads from the build folder. -cc -std=gnu11 -O2 -Wall -Wextra -Werror -fPIC -shared -Wl,-soname,libomega_native.so -o "$O/libomega_native.so" "$O/omega_native.c" +units=""; for u in $U; do units="$units $T/$u.om"; done +# tools/native: the generator, the units' code, the library source, the header, the python, rust, zig +# and go bindings, then the library built with the header included (a declaration that disagrees with +# its definition fails that build). +if OMEGA_NATIVE_OMC="$OMC" "$ROOT/tools/native" "$O" $units > "$O/tools-native.log" 2>&1; then + pass "tools/native: library + six bindings ($(cd "$O" && cat omega_native.h omega_native.py omega_native.rs omega_native.zig omega_native.go | sha256sum | cut -c1-16))" +else fail "tools/native"; cat "$O/tools-native.log"; exit 1; fi +GEN=$O/native-gen +# Every caller takes its declarations from the generated file; none types an Omega function itself. +if grep -n -E 'uint32_t omega_|fn omega_|argtypes|CDLL|import "C"' "$T"/caller.* > "$O/hand.log"; then + fail "a caller declares an Omega function by hand"; cat "$O/hand.log" +else pass "no caller declares an Omega function by hand"; fi # The embedded code must be the compiler's bytes, unchanged. # Each unit is read at its symbol, mapped from load address to file offset through its section. emb() { @@ -46,27 +52,25 @@ done if [ $ok = 1 ]; then pass "embedded code == omc output, per unit ($(sha256sum < "$O/code.bin" | cut -c1-16))" else fail "embedded code differs from omc output"; fi run() { lang=$1; shift; if "$@" > "$O/$lang.log" 2>&1; then pass "$lang caller ($(grep -c '^PASS' "$O/$lang.log") lines)"; else fail "$lang caller"; cat "$O/$lang.log"; fi; } +bld() { lang=$1; shift; if "$@" > "$O/$lang-build.log" 2>&1; then return 0; fi; fail "$lang build"; cat "$O/$lang-build.log"; return 1; } cp "$T"/caller.* "$O/" -cc -std=gnu11 -O2 -Wall -Wextra -Werror -pthread "$O/caller.c" -L"$O" -lomega_native -Wl,-rpath,"$O" -o "$O/caller_c" -run c "$O/caller_c" +bld c cc -std=gnu11 -O2 -Wall -Wextra -Werror -pthread "$O/caller.c" -L"$O" -lomega_native -Wl,-rpath,"$O" -o "$O/caller_c" \ + && run c "$O/caller_c" # Branch protection: the library built with BTI enforced, so every indirect branch into it (PLT calls, the # per-unit entry stubs, the runtime entries) must land on a landing pad. -nostartfiles because this # system's crti.o has no landing pad in _init; that is the C library's start code, not the binding's. # Without the entry stubs (commit e7777ca) this caller dies with SIGILL on its first call. -mkdir -p "$O/bti" -# The library names itself (soname), so every caller records "libomega_native.so" and finds it via rpath; -# without it Zig records "./libomega_native.so", which only loads from the build folder. -cc -std=gnu11 -O2 -Wall -Wextra -Werror -fPIC -shared -Wl,-soname,libomega_native.so -nostartfiles -mbranch-protection=standard \ - -Wl,-z,force-bti -o "$O/bti/libomega_native.so" "$O/omega_native.c" 2> "$O/bti/cc.log" -cc -std=gnu11 -O2 -Wall -Wextra -Werror -pthread "$O/caller.c" -L"$O/bti" -lomega_native -Wl,-rpath,"$O/bti" \ - -o "$O/bti/caller_c" -if readelf -n "$O/bti/libomega_native.so" | grep -q "feature: BTI"; then - run c-bti "$O/bti/caller_c" -else fail "c-bti build carries no BTI property"; fi +if OMEGA_NATIVE_OMC="$OMC" OMEGA_NATIVE_CFLAGS="-nostartfiles -mbranch-protection=standard -Wl,-z,force-bti" \ + "$ROOT/tools/native" "$O/bti" $units > "$O/bti-native.log" 2>&1 \ + && cmp -s "$O/bti/omega_native.c" "$O/omega_native.c" \ + && bld c-bti cc -std=gnu11 -O2 -Wall -Wextra -Werror -pthread "$O/caller.c" -L"$O/bti" -lomega_native -Wl,-rpath,"$O/bti" -o "$O/bti/caller_c"; then + if readelf -n "$O/bti/libomega_native.so" | grep -q "feature: BTI"; then run c-bti "$O/bti/caller_c" + else fail "c-bti build carries no BTI property"; fi +else fail "c-bti library build"; cat "$O/bti-native.log" 2> /dev/null || true; fi # Generator refusals: names the C side cannot carry give exit 110 and no output, never broken C. unitin() { printf '%s' "$1"; printf '\0'; printf '%08d' "$(wc -c < "$O/madd.bin")"; cat "$O/madd.bin"; } refuse() { - rc=0; "$O/native" < "$O/ref.in" > "$O/ref.out" 2> /dev/null || rc=$? + rc=0; "$GEN" c < "$O/ref.in" > "$O/ref.out" 2> /dev/null || rc=$? if [ "$rc" = 110 ] && [ ! -s "$O/ref.out" ]; then pass "generator refuses $1"; else fail "generator accepted $1 (exit $rc)"; fi } S='a ulong b ulong c ulong to ulong @@ -79,32 +83,72 @@ ret 0 end " > "$O/ref.in"; refuse "a parameter name ending in _len" { unitin "fn m $S"; unitin "fn m $S"; } > "$O/ref.in"; refuse "a repeated unit name" -unitin "fn m $S" > "$O/ref.in"; rc=0; "$O/native" < "$O/ref.in" > "$O/ref.out" 2> /dev/null || rc=$? +unitin "fn m $S" > "$O/ref.in"; rc=0; "$GEN" c < "$O/ref.in" > "$O/ref.out" 2> /dev/null || rc=$? if [ "$rc" = 0 ] && [ -s "$O/ref.out" ]; then pass "generator accepts the same unit once (control)" else fail "generator refused the control unit (exit $rc)"; fi +rc=0; "$GEN" < "$O/units.in" > "$O/ref.out" 2> /dev/null || rc=$? +if [ "$rc" = 110 ] && [ ! -s "$O/ref.out" ]; then pass "generator refuses a missing output word"; else fail "generator ran without an output word (exit $rc)"; fi +rc=0; "$GEN" java < "$O/units.in" > "$O/ref.out" 2> /dev/null || rc=$? +if [ "$rc" = 110 ] && [ ! -s "$O/ref.out" ]; then pass "generator refuses an unknown output word"; else fail "generator accepted the word java (exit $rc)"; fi +# The other five languages, each built against its generated binding only. if command -v c++ > /dev/null; then - c++ -std=c++17 -O2 -Wall -Wextra -Werror "$O/caller.cpp" -L"$O" -lomega_native -Wl,-rpath,"$O" -o "$O/caller_cpp" - run cpp "$O/caller_cpp" + bld cpp c++ -std=c++17 -O2 -Wall -Wextra -Werror "$O/caller.cpp" -L"$O" -lomega_native -Wl,-rpath,"$O" -o "$O/caller_cpp" \ + && run cpp "$O/caller_cpp" else notrun="$notrun c++"; fi if command -v python3 > /dev/null; then run python python3 -I "$O/caller.py"; else notrun="$notrun python"; fi if command -v rustc > /dev/null; then - rustc -O --edition 2021 "$O/caller.rs" -L "$O" -C link-arg=-Wl,-rpath,"$O" -o "$O/caller_rs" - run rust "$O/caller_rs" + bld rust rustc -O --edition 2021 "$O/caller.rs" -L "$O" -C link-arg=-Wl,-rpath,"$O" -o "$O/caller_rs" \ + && run rust "$O/caller_rs" else notrun="$notrun rust"; fi if command -v zig > /dev/null; then - (cd "$O" && zig build-exe caller.zig -L. -lomega_native -lc -rpath "$O" -femit-bin=caller_zig) && run zig "$O/caller_zig" + bld zig sh -c 'cd "$1" && zig build-exe caller.zig -L. -lomega_native -lc -rpath "$1" -femit-bin=caller_zig' _ "$O" \ + && run zig "$O/caller_zig" else notrun="$notrun zig"; fi if command -v go > /dev/null; then - (cd "$O" && go mod init omegacaller > /dev/null 2>&1 && go build -o caller_go caller.go) && run go "$O/caller_go" + # Module omegacaller: caller.go (package main) and omeganative/ holding the generated package and the library. + mkdir -p "$O/go/omeganative"; cp "$O/omega_native.go" "$O/libomega_native.so" "$O/go/omeganative/"; cp "$O/caller.go" "$O/go/" + bld go sh -c 'cd "$1" && go mod init omegacaller > /dev/null 2>&1 && go build -o caller_go .' _ "$O/go" \ + && run go "$O/go/caller_go" else notrun="$notrun go"; fi -# Negative control: madd's add turned into sub must be caught. +# Negative control 1: madd's add turned into sub in the embedded code must be caught by the C caller. +frame() { cat "$T/$1.om"; printf '\0'; printf '%08d' "$(wc -c < "$2/$1.bin")"; cat "$2/$1.bin"; } cp "$O/smadd.bin" "$O/fill.bin" "$O/spin.bin" "$O/mix.bin" "$O/big.bin" "$O/mut/" printf '\003\174\001\233\140\000\002\313\300\003\137\326' > "$O/mut/madd.bin" -gen "$O/mut" -cc -std=gnu11 -O2 -fPIC -shared -Wl,-soname,libomega_native.so -o "$O/mut/libomega_native.so" "$O/mut/omega_native.c" +for u in $U; do frame $u "$O/mut"; done | "$GEN" c > "$O/mut/omega_native.c" +cc -std=gnu11 -O2 -fPIC -shared -Wl,-soname,libomega_native.so -include "$O/omega_native.h" -o "$O/mut/libomega_native.so" "$O/mut/omega_native.c" 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 +# Negative control 2: one wrong scalar type in each generated binding (a one-byte change to the +# generator's text) must be caught by that language's build or run. The real library is used. +gmut() { + name=$1; ex=$2; w=$3; ext=$4; shift 4; M=$O/mut/$w; mkdir -p "$M" + sed "$ex" "$ROOT/src/native.om" > "$M/native.om" + if cmp -s "$M/native.om" "$ROOT/src/native.om"; then fail "mutant $name did not change the generator"; return 0; fi + if ! "$OMC" < "$M/native.om" > "$M/gen" 2> /dev/null; then fail "mutant $name: generator did not build"; return 0; fi + chmod +x "$M/gen" + if ! "$M/gen" "$w" < "$O/units.in" > "$M/omega_native.$ext" 2> /dev/null; then fail "mutant $name: generator refused"; return 0; fi + if cmp -s "$M/omega_native.$ext" "$O/omega_native.$ext"; then fail "mutant $name: binding unchanged"; return 0; fi + if "$@" > "$M/check.log" 2>&1; then fail "mutant $name not caught"; else pass "mutant $name caught"; fi +} +gmut "h scalar uint64_t -> uint32_t" 's/, uint64_t o_~/, uint32_t o_~/' h h \ + cc -std=gnu11 -O2 -fPIC -shared -include "$O/mut/h/omega_native.h" -o "$O/mut/h/lib.so" "$O/omega_native.c" +if command -v python3 > /dev/null; then + gmut "python scalar c_uint64 -> c_uint32" 's/_u64 = ctypes.c_uint64/_u64 = ctypes.c_uint32/' python py \ + sh -c 'cp "$1/caller.py" "$1/libomega_native.so" "$1/mut/python/" && python3 -I "$1/mut/python/caller.py"' _ "$O" +fi +if command -v rustc > /dev/null; then + gmut "rust scalar u64 -> u32" 's/: u64~: \*mut u64/: u32~: *mut u64/' rust rs \ + sh -c 'cp "$1/caller.rs" "$1/mut/rust/" && rustc -O --edition 2021 "$1/mut/rust/caller.rs" -L "$1" -C link-arg=-Wl,-rpath,"$1" -o "$1/mut/rust/caller_rs" && "$1/mut/rust/caller_rs"' _ "$O" +fi +if command -v zig > /dev/null; then + gmut "zig scalar u64 -> u32" 's/: u64~: ?\[\*\]u64/: u32~: ?[*]u64/' zig zig \ + sh -c 'cp "$1/caller.zig" "$1/mut/zig/" && cd "$1/mut/zig" && zig build-exe caller.zig -L "$1" -lomega_native -lc -rpath "$1" -femit-bin=caller_zig && ./caller_zig' _ "$O" +fi +if command -v go > /dev/null; then + gmut "go scalar uint64 -> uint32" 's/ uint64~ unsafe.Pointer/ uint32~ unsafe.Pointer/' go go \ + sh -c 'mkdir -p "$1/mut/go/omeganative" && mv "$1/mut/go/omega_native.go" "$1/mut/go/omeganative/" && cp "$1/libomega_native.so" "$1/mut/go/omeganative/" && cp "$1/caller.go" "$1/mut/go/" && cd "$1/mut/go" && go mod init omegacaller && go build -o caller_go . && ./caller_go' _ "$O" +fi for l in $notrun; do echo "NOT_RUN $l caller: toolchain not installed"; done if [ "$fails" -ne 0 ]; then echo "NATIVE-CALL FAIL $fails"; exit 1; fi echo "NATIVE-CALL PASS (not run:${notrun:- none})" diff --git a/tools/native b/tools/native new file mode 100755 index 0000000..f07cc88 --- /dev/null +++ b/tools/native @@ -0,0 +1,42 @@ +#!/bin/sh +# tools/native: one Omega library and its declarations for six languages, from Omega function units. +# Usage: OMEGA_NATIVE_OMC= tools/native OUTDIR unit.om [unit.om...] +# Each unit is one .om function in the native call subset (docs/NATIVE-ABI.md). OUTDIR receives +# omega_native.c the library source: every unit's compiled code, embedded unchanged, behind +# uint32_t omega_(uint64_t *result, ...) +# omega_native.h its C and C++ declarations +# omega_native.py ctypes bindings (import it from beside libomega_native.so) +# omega_native.rs a Rust module (mod omega_native;) +# omega_native.zig a Zig file (@import("omega_native.zig")) +# omega_native.go a cgo package omeganative (a folder of that name holding it and the library) +# libomega_native.so the library, built with omega_native.h included, so a declaration that +# disagrees with its definition fails this build +# all written by one generator, src/native.om, from the units' signatures. A unit outside the subset +# is refused (exit 110) and no binding is written. Extra C flags: OMEGA_NATIVE_CFLAGS. +set -eu +PIN=734f0a39319fbfa930bcc62cb30dbf06d47d78e126372b74f5baf478e6126b61 +ROOT=$(cd "$(dirname "$0")/.." && pwd) +OMC=${OMEGA_NATIVE_OMC:?set OMEGA_NATIVE_OMC to the seeded omc (omega seed/out/omc, sha256 $PIN)} +got=$(sha256sum < "$OMC" | cut -d' ' -f1) +[ "$got" = "$PIN" ] || { echo "tools/native: omc sha256 $got, pinned $PIN" >&2; exit 1; } +[ $# -ge 2 ] || { echo "usage: OMEGA_NATIVE_OMC= tools/native OUTDIR unit.om [unit.om...]" >&2; exit 2; } +OUT=$1; shift +mkdir -p "$OUT" +OUT=$(cd "$OUT" && pwd) +"$OMC" < "$ROOT/src/native.om" > "$OUT/native-gen"; chmod +x "$OUT/native-gen" +# The generator's input: each unit's source, a 0 byte, the code length in 8 decimal digits, the code. +: > "$OUT/units.in" +for u in "$@"; do + b=$(basename "$u" .om) + "$OMC" < "$u" > "$OUT/$b.bin" + { cat "$u"; printf '\0'; printf '%08d' "$(wc -c < "$OUT/$b.bin")"; cat "$OUT/$b.bin"; } >> "$OUT/units.in" +done +# Every output is generated before any is installed, so a refusal leaves OUTDIR without a half set. +for w in c h python rust zig go; do "$OUT/native-gen" $w < "$OUT/units.in" > "$OUT/.$w.new"; done +mv "$OUT/.c.new" "$OUT/omega_native.c"; mv "$OUT/.h.new" "$OUT/omega_native.h" +mv "$OUT/.python.new" "$OUT/omega_native.py"; mv "$OUT/.rust.new" "$OUT/omega_native.rs" +mv "$OUT/.zig.new" "$OUT/omega_native.zig"; mv "$OUT/.go.new" "$OUT/omega_native.go" +# The library names itself (soname), so every caller records libomega_native.so and finds it via rpath. +${CC:-cc} -std=gnu11 -O2 -Wall -Wextra -Werror -fPIC -shared -Wl,-soname,libomega_native.so \ + -include "$OUT/omega_native.h" ${OMEGA_NATIVE_CFLAGS:-} -o "$OUT/libomega_native.so" "$OUT/omega_native.c" +echo "tools/native: $# unit(s) -> $OUT/libomega_native.so + omega_native.{c,h,py,rs,zig,go}" From d18fa91881ac353429941976ccd217acfa24b00e Mon Sep 17 00:00:00 2001 From: Drake Stapleton Date: Sat, 24 Oct 2026 18:26:42 -0500 Subject: [PATCH 2/4] native call v1: evidence for 8e1f3cf (generated bindings, six languages, five type mutants caught) evidence/native-bindings: the suite log at 8e1f3cf (NATIVE-CALL PASS, 22 checks, not run: none), the six callers' output, the code mutant and the five generator-mutant logs, the five generated declaration files, and identity.txt (commit, omc, generator and binding sha256s, toolchain versions). Co-authored-by: Drake Stapleton Co-Authored-By: Claude Fable 5.1 --- evidence/native-bindings/c-bti.log | 26 +++++++++ evidence/native-bindings/c.log | 26 +++++++++ evidence/native-bindings/cpp.log | 10 ++++ evidence/native-bindings/go.log | 10 ++++ evidence/native-bindings/identity.txt | 19 +++++++ evidence/native-bindings/mutant-code-c.log | 26 +++++++++ evidence/native-bindings/mutant-go.log | 10 ++++ evidence/native-bindings/mutant-h.log | 37 ++++++++++++ evidence/native-bindings/mutant-python.log | 10 ++++ evidence/native-bindings/mutant-rust.log | 57 +++++++++++++++++++ evidence/native-bindings/mutant-zig.log | 1 + evidence/native-bindings/omega_native.go | 65 ++++++++++++++++++++++ evidence/native-bindings/omega_native.h | 33 +++++++++++ evidence/native-bindings/omega_native.py | 36 ++++++++++++ evidence/native-bindings/omega_native.rs | 31 +++++++++++ evidence/native-bindings/omega_native.zig | 22 ++++++++ evidence/native-bindings/python.log | 10 ++++ evidence/native-bindings/run.log | 23 ++++++++ evidence/native-bindings/rust.log | 11 ++++ evidence/native-bindings/tools-native.log | 1 + evidence/native-bindings/zig.log | 10 ++++ 21 files changed, 474 insertions(+) create mode 100644 evidence/native-bindings/c-bti.log create mode 100644 evidence/native-bindings/c.log create mode 100644 evidence/native-bindings/cpp.log create mode 100644 evidence/native-bindings/go.log create mode 100644 evidence/native-bindings/identity.txt create mode 100644 evidence/native-bindings/mutant-code-c.log create mode 100644 evidence/native-bindings/mutant-go.log create mode 100644 evidence/native-bindings/mutant-h.log create mode 100644 evidence/native-bindings/mutant-python.log create mode 100644 evidence/native-bindings/mutant-rust.log create mode 100644 evidence/native-bindings/mutant-zig.log create mode 100644 evidence/native-bindings/omega_native.go create mode 100644 evidence/native-bindings/omega_native.h create mode 100644 evidence/native-bindings/omega_native.py create mode 100644 evidence/native-bindings/omega_native.rs create mode 100644 evidence/native-bindings/omega_native.zig create mode 100644 evidence/native-bindings/python.log create mode 100644 evidence/native-bindings/run.log create mode 100644 evidence/native-bindings/rust.log create mode 100644 evidence/native-bindings/tools-native.log create mode 100644 evidence/native-bindings/zig.log diff --git a/evidence/native-bindings/c-bti.log b/evidence/native-bindings/c-bti.log new file mode 100644 index 0000000..4526c03 --- /dev/null +++ b/evidence/native-bindings/c-bti.log @@ -0,0 +1,26 @@ +PASS madd 3*5+7 = 22 +PASS madd wraps: (2^64-1)*2+5 = 3 +PASS madd large = 3637949782307626511 +PASS madd signed bits: -3*5+2 = -13 +PASS smadd -3*5+2 = -13 +PASS smadd 2^62*4 traps 1, result unchanged +PASS fill k=1 traps 3, result unchanged +PASS fill k=1 writes before the trap stay visible +PASS fill k=0 returns 3, writes 11 22 33 +PASS null result refused 32 +PASS null buffer refused 32, result unchanged +PASS buffer length 3 refused 32 +PASS misaligned buffer refused 32 +PASS result inside buffer refused 32, buffer untouched +PASS call after traps still works +PASS mix six parameters, void result writes 0 +PASS mix negative long traps 9 after both writes, result unchanged +PASS mix uchar conversion 325 traps 5, first write visible +PASS mix view overlapping the edit array refused 32, nothing written +PASS big local array on the main thread = 15 +PASS big traps 3 from a frame holding a local array, result unchanged +PASS big on a 256 KiB thread runs on the binding's stack = 15 +PASS x19..x28 and d8..d15 kept across a normal return +PASS x19..x28 and d8..d15 kept across a trap +PASS reentry from a signal handler during a call refused 33 +PASS 0 failure(s) diff --git a/evidence/native-bindings/c.log b/evidence/native-bindings/c.log new file mode 100644 index 0000000..4526c03 --- /dev/null +++ b/evidence/native-bindings/c.log @@ -0,0 +1,26 @@ +PASS madd 3*5+7 = 22 +PASS madd wraps: (2^64-1)*2+5 = 3 +PASS madd large = 3637949782307626511 +PASS madd signed bits: -3*5+2 = -13 +PASS smadd -3*5+2 = -13 +PASS smadd 2^62*4 traps 1, result unchanged +PASS fill k=1 traps 3, result unchanged +PASS fill k=1 writes before the trap stay visible +PASS fill k=0 returns 3, writes 11 22 33 +PASS null result refused 32 +PASS null buffer refused 32, result unchanged +PASS buffer length 3 refused 32 +PASS misaligned buffer refused 32 +PASS result inside buffer refused 32, buffer untouched +PASS call after traps still works +PASS mix six parameters, void result writes 0 +PASS mix negative long traps 9 after both writes, result unchanged +PASS mix uchar conversion 325 traps 5, first write visible +PASS mix view overlapping the edit array refused 32, nothing written +PASS big local array on the main thread = 15 +PASS big traps 3 from a frame holding a local array, result unchanged +PASS big on a 256 KiB thread runs on the binding's stack = 15 +PASS x19..x28 and d8..d15 kept across a normal return +PASS x19..x28 and d8..d15 kept across a trap +PASS reentry from a signal handler during a call refused 33 +PASS 0 failure(s) diff --git a/evidence/native-bindings/cpp.log b/evidence/native-bindings/cpp.log new file mode 100644 index 0000000..a11ebc5 --- /dev/null +++ b/evidence/native-bindings/cpp.log @@ -0,0 +1,10 @@ +PASS madd 3*5+7 = 22 +PASS madd wraps = 3 +PASS madd large +PASS madd signed bits = -13 +PASS smadd overflow traps 1, result unchanged +PASS fill k=1 traps 3, result unchanged +PASS writes before the trap stay visible +PASS fill k=0 = 3 +PASS wrong length refused 32 +PASS 0 failure(s) diff --git a/evidence/native-bindings/go.log b/evidence/native-bindings/go.log new file mode 100644 index 0000000..80e6776 --- /dev/null +++ b/evidence/native-bindings/go.log @@ -0,0 +1,10 @@ +PASS madd 3*5+7 = 22 +PASS madd wraps = 3 +PASS madd large +PASS madd signed bits = -13 +PASS smadd overflow traps 1, result unchanged +PASS fill k=1 traps 3, result unchanged +PASS writes before the trap stay visible +PASS fill k=0 = 3 +PASS null result refused 32 +PASS 0 failure(s) diff --git a/evidence/native-bindings/identity.txt b/evidence/native-bindings/identity.txt new file mode 100644 index 0000000..a68cfb0 --- /dev/null +++ b/evidence/native-bindings/identity.txt @@ -0,0 +1,19 @@ +date 2026-10-24T23:25:56Z +source commit 8e1f3cf19988b754489262b4b399d65861454ad1 (branch feat/native-bindings, on feat/native-call-v1 9d5f768) +omc sha256 734f0a39319fbfa930bcc62cb30dbf06d47d78e126372b74f5baf478e6126b61 +generator src/native.om sha256 cbe58616329b1f07f374927867b7a0a94fc1794c4abe4ebb5d2ee8280b202524 +embedded code (six units) sha256 38c294080111990298ac3ce881986d1ba1869b2966b498c36f40485c226c3ee9 +omega_native.c sha256 ce64c4b50d5d54904398d657a1977abe2fe6bad6497503ad55668d247ee77fef +omega_native.h sha256 fe3a520a5fdd62897ff4f00d0e3c8a78a33ba79e93cbe42f93b7819692d430f0 +omega_native.py sha256 155f8bef31051027d5bd911a8583d6fcd1dc91e8af9af5175cb4088560ecdc04 +omega_native.rs sha256 291de48c14b0f3a332fb0d717c477f193e6f707ab1d203b59c9eaa4eae7c0131 +omega_native.zig sha256 196447e328340385b814d0a787dbd871726465a8e33e88aabdf184a55103ca09 +omega_native.go sha256 50be29a9b907ab52aa37af075e7ad107580a0ed0602ee2ce37264136e5d22f2d +cc cc (Ubuntu 13.3.0-6ubuntu2~24.04.1) 13.3.0 +c++ c++ (Ubuntu 13.3.0-6ubuntu2~24.04.1) 13.3.0 +python3 Python 3.12.3 +rustc rustc 1.98.1 (48a229cea 2026-09-01) +zig 0.13.0 (archive sha256 041ac42323837eb5624068acd8b00cd5777dac4cf91179e8dad7a7e90dd0c556) +go go version go1.27.3 linux/arm64 (archive sha256 a06e99309dbcbdca1a8ccd2282c3ee146e55930886e26b4f1193269587165f49) +machine Linux 7.0.0-1019-nvidia aarch64 +verdict NATIVE-CALL PASS (not run: none) diff --git a/evidence/native-bindings/mutant-code-c.log b/evidence/native-bindings/mutant-code-c.log new file mode 100644 index 0000000..d5ecefb --- /dev/null +++ b/evidence/native-bindings/mutant-code-c.log @@ -0,0 +1,26 @@ +FAIL madd 3*5+7 = 22 +FAIL madd wraps: (2^64-1)*2+5 = 3 +FAIL madd large = 3637949782307626511 +FAIL madd signed bits: -3*5+2 = -13 +PASS smadd -3*5+2 = -13 +PASS smadd 2^62*4 traps 1, result unchanged +PASS fill k=1 traps 3, result unchanged +PASS fill k=1 writes before the trap stay visible +PASS fill k=0 returns 3, writes 11 22 33 +PASS null result refused 32 +PASS null buffer refused 32, result unchanged +PASS buffer length 3 refused 32 +PASS misaligned buffer refused 32 +PASS result inside buffer refused 32, buffer untouched +FAIL call after traps still works +PASS mix six parameters, void result writes 0 +PASS mix negative long traps 9 after both writes, result unchanged +PASS mix uchar conversion 325 traps 5, first write visible +PASS mix view overlapping the edit array refused 32, nothing written +PASS big local array on the main thread = 15 +PASS big traps 3 from a frame holding a local array, result unchanged +PASS big on a 256 KiB thread runs on the binding's stack = 15 +FAIL x19..x28 and d8..d15 kept across a normal return +PASS x19..x28 and d8..d15 kept across a trap +PASS reentry from a signal handler during a call refused 33 +FAIL 6 failure(s) diff --git a/evidence/native-bindings/mutant-go.log b/evidence/native-bindings/mutant-go.log new file mode 100644 index 0000000..c60989e --- /dev/null +++ b/evidence/native-bindings/mutant-go.log @@ -0,0 +1,10 @@ +go: creating new go.mod: module omegacaller +go: to add module requirements and sums: + go mod tidy +# omegacaller +./caller.go:29:44: cannot use ^uint64(0) (constant 18446744073709551615 of type uint64) as uint32 value in argument to on.Omega_madd +./caller.go:30:40: cannot use 1311768467463790320 (untyped int constant) as uint32 value in argument to on.Omega_madd (overflows) +./caller.go:30:61: cannot use 18364758544493064720 (untyped int constant) as uint32 value in argument to on.Omega_madd (overflows) +./caller.go:30:83: cannot use 1085102592571150095 (untyped int constant) as uint32 value in argument to on.Omega_madd (overflows) +./caller.go:32:52: cannot use uint64(m3) (value of type uint64) as uint32 value in argument to on.Omega_madd +./caller.go:34:71: cannot use 1 << 62 (untyped int constant 4611686018427387904) as uint32 value in argument to on.Omega_smadd (overflows) diff --git a/evidence/native-bindings/mutant-h.log b/evidence/native-bindings/mutant-h.log new file mode 100644 index 0000000..a560a61 --- /dev/null +++ b/evidence/native-bindings/mutant-h.log @@ -0,0 +1,37 @@ +/home/drakestapleton/workspace/bridge-native-call/build/native/omega_native.c:118:49: error: conflicting types for ‘omega_madd’; have ‘uint32_t(uint64_t *, uint64_t, uint64_t, uint64_t)’ {aka ‘unsigned int(long unsigned int *, long unsigned int, long unsigned int, long unsigned int)’} + 118 | __attribute__((visibility("default"))) uint32_t omega_madd(uint64_t *result, uint64_t o_a, uint64_t o_b, uint64_t o_c) + | ^~~~~~~~~~ +In file included from : +/home/drakestapleton/workspace/bridge-native-call/build/native/mut/h/omega_native.h:19:10: note: previous declaration of ‘omega_madd’ with type ‘uint32_t(uint64_t *, uint32_t, uint32_t, uint32_t)’ {aka ‘unsigned int(long unsigned int *, unsigned int, unsigned int, unsigned int)’} + 19 | uint32_t omega_madd(uint64_t *result, uint32_t o_a, uint32_t o_b, uint32_t o_c); + | ^~~~~~~~~~ +/home/drakestapleton/workspace/bridge-native-call/build/native/omega_native.c:134:49: error: conflicting types for ‘omega_smadd’; have ‘uint32_t(uint64_t *, uint64_t, uint64_t, uint64_t)’ {aka ‘unsigned int(long unsigned int *, long unsigned int, long unsigned int, long unsigned int)’} + 134 | __attribute__((visibility("default"))) uint32_t omega_smadd(uint64_t *result, uint64_t o_a, uint64_t o_b, uint64_t o_c) + | ^~~~~~~~~~~ +/home/drakestapleton/workspace/bridge-native-call/build/native/mut/h/omega_native.h:21:10: note: previous declaration of ‘omega_smadd’ with type ‘uint32_t(uint64_t *, uint32_t, uint32_t, uint32_t)’ {aka ‘unsigned int(long unsigned int *, unsigned int, unsigned int, unsigned int)’} + 21 | uint32_t omega_smadd(uint64_t *result, uint32_t o_a, uint32_t o_b, uint32_t o_c); + | ^~~~~~~~~~~ +/home/drakestapleton/workspace/bridge-native-call/build/native/omega_native.c:150:49: error: conflicting types for ‘omega_fill’; have ‘uint32_t(uint64_t *, void *, uint64_t, uint64_t)’ {aka ‘unsigned int(long unsigned int *, void *, long unsigned int, long unsigned int)’} + 150 | __attribute__((visibility("default"))) uint32_t omega_fill(uint64_t *result, void *o_buf, uint64_t o_buf_len, uint64_t o_k) + | ^~~~~~~~~~ +/home/drakestapleton/workspace/bridge-native-call/build/native/mut/h/omega_native.h:23:10: note: previous declaration of ‘omega_fill’ with type ‘uint32_t(uint64_t *, void *, uint32_t, uint32_t)’ {aka ‘unsigned int(long unsigned int *, void *, unsigned int, unsigned int)’} + 23 | uint32_t omega_fill(uint64_t *result, void *o_buf, uint32_t o_buf_len, uint32_t o_k); + | ^~~~~~~~~~ +/home/drakestapleton/workspace/bridge-native-call/build/native/omega_native.c:167:49: error: conflicting types for ‘omega_spin’; have ‘uint32_t(uint64_t *, uint64_t)’ {aka ‘unsigned int(long unsigned int *, long unsigned int)’} + 167 | __attribute__((visibility("default"))) uint32_t omega_spin(uint64_t *result, uint64_t o_n) + | ^~~~~~~~~~ +/home/drakestapleton/workspace/bridge-native-call/build/native/mut/h/omega_native.h:25:10: note: previous declaration of ‘omega_spin’ with type ‘uint32_t(uint64_t *, uint32_t)’ {aka ‘unsigned int(long unsigned int *, unsigned int)’} + 25 | uint32_t omega_spin(uint64_t *result, uint32_t o_n); + | ^~~~~~~~~~ +/home/drakestapleton/workspace/bridge-native-call/build/native/omega_native.c:181:49: error: conflicting types for ‘omega_mix’; have ‘uint32_t(uint64_t *, uint64_t, uint64_t, const void *, uint64_t, void *, uint64_t, const void *, uint64_t, uint64_t)’ {aka ‘unsigned int(long unsigned int *, long unsigned int, long unsigned int, const void *, long unsigned int, void *, long unsigned int, const void *, long unsigned int, long unsigned int)’} + 181 | __attribute__((visibility("default"))) uint32_t omega_mix(uint64_t *result, uint64_t o_a, uint64_t o_b, const void *o_c, uint64_t o_c_len, void *o_d, uint64_t o_d_len, const void *o_e, uint64_t o_e_len, uint64_t o_f) + | ^~~~~~~~~ +/home/drakestapleton/workspace/bridge-native-call/build/native/mut/h/omega_native.h:27:10: note: previous declaration of ‘omega_mix’ with type ‘uint32_t(uint64_t *, uint32_t, uint32_t, const void *, uint32_t, void *, uint32_t, const void *, uint32_t, uint32_t)’ {aka ‘unsigned int(long unsigned int *, unsigned int, unsigned int, const void *, unsigned int, void *, unsigned int, const void *, unsigned int, unsigned int)’} + 27 | uint32_t omega_mix(uint64_t *result, uint32_t o_a, uint32_t o_b, const void *o_c, uint32_t o_c_len, void *o_d, uint32_t o_d_len, const void *o_e, uint32_t o_e_len, uint32_t o_f); + | ^~~~~~~~~ +/home/drakestapleton/workspace/bridge-native-call/build/native/omega_native.c:206:49: error: conflicting types for ‘omega_big’; have ‘uint32_t(uint64_t *, uint64_t)’ {aka ‘unsigned int(long unsigned int *, long unsigned int)’} + 206 | __attribute__((visibility("default"))) uint32_t omega_big(uint64_t *result, uint64_t o_n) + | ^~~~~~~~~ +/home/drakestapleton/workspace/bridge-native-call/build/native/mut/h/omega_native.h:29:10: note: previous declaration of ‘omega_big’ with type ‘uint32_t(uint64_t *, uint32_t)’ {aka ‘unsigned int(long unsigned int *, unsigned int)’} + 29 | uint32_t omega_big(uint64_t *result, uint32_t o_n); + | ^~~~~~~~~ diff --git a/evidence/native-bindings/mutant-python.log b/evidence/native-bindings/mutant-python.log new file mode 100644 index 0000000..135614b --- /dev/null +++ b/evidence/native-bindings/mutant-python.log @@ -0,0 +1,10 @@ +PASS madd 3*5+7 = 22 +FAIL madd wraps = 3 +FAIL madd large +FAIL madd signed bits = -13 +FAIL smadd overflow traps 1, result unchanged +FAIL fill k=1 traps 3, result unchanged +PASS writes before the trap stay visible +PASS fill k=0 = 3 +PASS null result refused 32 +FAIL 5 failure(s) diff --git a/evidence/native-bindings/mutant-rust.log b/evidence/native-bindings/mutant-rust.log new file mode 100644 index 0000000..07bed59 --- /dev/null +++ b/evidence/native-bindings/mutant-rust.log @@ -0,0 +1,57 @@ +error[E0308]: mismatched types + --> /home/drakestapleton/workspace/bridge-native-call/build/native/mut/rust/caller.rs:17:52 + | +17 | check("madd wraps = 3", omega_madd(&mut r, u64::MAX, 2, 5) == 0 && r == 3); + | ---------- ^^^^^^^^ expected `u32`, found `u64` + | | + | arguments to this function are incorrect + | +note: function defined here + --> /home/drakestapleton/workspace/bridge-native-call/build/native/mut/rust/omega_native.rs:20:12 + | +20 | pub fn omega_madd(result: *mut u64, o_a: u32, o_b: u32, o_c: u32) -> u32; + | ^^^^^^^^^^ --- +help: you can convert a `u64` to a `u32` and panic if the converted value doesn't fit + | +17 | check("madd wraps = 3", omega_madd(&mut r, u64::MAX.try_into().unwrap(), 2, 5) == 0 && r == 3); + | ++++++++++++++++++++ + +error[E0308]: mismatched types + --> /home/drakestapleton/workspace/bridge-native-call/build/native/mut/rust/caller.rs:20:60 + | +20 | check("madd signed bits = -13", omega_madd(&mut r, (-3i64) as u64, 5, 2) == 0 && r as i64 == -13); + | ---------- ^^^^^^^^^^^^^^ expected `u32`, found `u64` + | | + | arguments to this function are incorrect + | +note: function defined here + --> /home/drakestapleton/workspace/bridge-native-call/build/native/mut/rust/omega_native.rs:20:12 + | +20 | pub fn omega_madd(result: *mut u64, o_a: u32, o_b: u32, o_c: u32) -> u32; + | ^^^^^^^^^^ --- +help: you can convert a `u64` to a `u32` and panic if the converted value doesn't fit + | +20 | check("madd signed bits = -13", omega_madd(&mut r, ((-3i64) as u64).try_into().unwrap(), 5, 2) == 0 && r as i64 == -13); + | + +++++++++++++++++++++ + +error[E0308]: mismatched types + --> /home/drakestapleton/workspace/bridge-native-call/build/native/mut/rust/caller.rs:22:79 + | +22 | check("smadd overflow traps 1, result unchanged", omega_smadd(&mut r, 1u64 << 62, 4, 0) == 1 && r == SENTINEL); + | ----------- ^^^^^^^^^^ expected `u32`, found `u64` + | | + | arguments to this function are incorrect + | +note: function defined here + --> /home/drakestapleton/workspace/bridge-native-call/build/native/mut/rust/omega_native.rs:22:12 + | +22 | pub fn omega_smadd(result: *mut u64, o_a: u32, o_b: u32, o_c: u32) -> u32; + | ^^^^^^^^^^^ --- +help: you can convert a `u64` to a `u32` and panic if the converted value doesn't fit + | +22 | check("smadd overflow traps 1, result unchanged", omega_smadd(&mut r, (1u64 << 62).try_into().unwrap(), 4, 0) == 1 && r == SENTINEL); + | + +++++++++++++++++++++ + +error: aborting due to 3 previous errors + +For more information about this error, try `rustc --explain E0308`. diff --git a/evidence/native-bindings/mutant-zig.log b/evidence/native-bindings/mutant-zig.log new file mode 100644 index 0000000..12448c7 --- /dev/null +++ b/evidence/native-bindings/mutant-zig.log @@ -0,0 +1 @@ +caller.zig:17:62: error: type 'u32' cannot represent integer value '18446744073709551615' diff --git a/evidence/native-bindings/omega_native.go b/evidence/native-bindings/omega_native.go new file mode 100644 index 0000000..d9b2814 --- /dev/null +++ b/evidence/native-bindings/omega_native.go @@ -0,0 +1,65 @@ +// Generated by omega-bridge src/native.om: native call ABI v1 (docs/NATIVE-ABI.md). +// cgo bindings for libomega_native.so beside this file. Regenerate it; do not edit it. +// Status: 0 done; 1..14 the Omega trap code; 32 argument refused (nil, wrong length, misaligned, +// overlap); 33 reentry; 34 bad runtime entry; 35 no stack. The result is written only on status 0. +// A long is its two's-complement bits (an int64 as uint64); an array is an unsafe.Pointer to memory +// holding no Go pointers, then its element count. +package omeganative + +/* +#cgo LDFLAGS: -L${SRCDIR} -lomega_native -Wl,-rpath,${SRCDIR} +#include +// fn madd a ulong b ulong c ulong to ulong +uint32_t omega_madd(uint64_t *result, uint64_t o_a, uint64_t o_b, uint64_t o_c); +// fn smadd a long b long c long to long +uint32_t omega_smadd(uint64_t *result, uint64_t o_a, uint64_t o_b, uint64_t o_c); +// fn fill buf edit ulong 4 k ulong to ulong +uint32_t omega_fill(uint64_t *result, void *o_buf, uint64_t o_buf_len, uint64_t o_k); +// fn spin n ulong to ulong +uint32_t omega_spin(uint64_t *result, uint64_t o_n); +// fn mix a ulong b long c view uchar 3 d edit uchar 2 e view ulong 2 f ulong to void +uint32_t omega_mix(uint64_t *result, uint64_t o_a, uint64_t o_b, const void *o_c, uint64_t o_c_len, void *o_d, uint64_t o_d_len, const void *o_e, uint64_t o_e_len, uint64_t o_f); +// fn big n ulong to ulong +uint32_t omega_big(uint64_t *result, uint64_t o_n); +*/ +import "C" + +import "unsafe" + +const ( + OmegaOK uint32 = 0 + OmegaBadArg uint32 = 32 + OmegaReentry uint32 = 33 + OmegaBadRt uint32 = 34 + OmegaNoStack uint32 = 35 +) + +// fn madd a ulong b ulong c ulong to ulong +func Omega_madd(result *uint64, o_a uint64, o_b uint64, o_c uint64) uint32 { + return uint32(C.omega_madd((*C.uint64_t)(unsafe.Pointer(result)), C.uint64_t(o_a), C.uint64_t(o_b), C.uint64_t(o_c))) +} + +// fn smadd a long b long c long to long +func Omega_smadd(result *uint64, o_a uint64, o_b uint64, o_c uint64) uint32 { + return uint32(C.omega_smadd((*C.uint64_t)(unsafe.Pointer(result)), C.uint64_t(o_a), C.uint64_t(o_b), C.uint64_t(o_c))) +} + +// fn fill buf edit ulong 4 k ulong to ulong +func Omega_fill(result *uint64, o_buf unsafe.Pointer, o_buf_len uint64, o_k uint64) uint32 { + return uint32(C.omega_fill((*C.uint64_t)(unsafe.Pointer(result)), o_buf, C.uint64_t(o_buf_len), C.uint64_t(o_k))) +} + +// fn spin n ulong to ulong +func Omega_spin(result *uint64, o_n uint64) uint32 { + return uint32(C.omega_spin((*C.uint64_t)(unsafe.Pointer(result)), C.uint64_t(o_n))) +} + +// fn mix a ulong b long c view uchar 3 d edit uchar 2 e view ulong 2 f ulong to void +func Omega_mix(result *uint64, o_a uint64, o_b uint64, o_c unsafe.Pointer, o_c_len uint64, o_d unsafe.Pointer, o_d_len uint64, o_e unsafe.Pointer, o_e_len uint64, o_f uint64) uint32 { + return uint32(C.omega_mix((*C.uint64_t)(unsafe.Pointer(result)), C.uint64_t(o_a), C.uint64_t(o_b), o_c, C.uint64_t(o_c_len), o_d, C.uint64_t(o_d_len), o_e, C.uint64_t(o_e_len), C.uint64_t(o_f))) +} + +// fn big n ulong to ulong +func Omega_big(result *uint64, o_n uint64) uint32 { + return uint32(C.omega_big((*C.uint64_t)(unsafe.Pointer(result)), C.uint64_t(o_n))) +} diff --git a/evidence/native-bindings/omega_native.h b/evidence/native-bindings/omega_native.h new file mode 100644 index 0000000..ef8b854 --- /dev/null +++ b/evidence/native-bindings/omega_native.h @@ -0,0 +1,33 @@ +/* Generated by omega-bridge src/native.om: native call ABI v1 (docs/NATIVE-ABI.md). + * C and C++ declarations for libomega_native. Regenerate it; do not edit it. + * Status: 0 done; 1..14 the Omega trap code; 32 argument refused (null, wrong length, misaligned, + * overlap); 33 reentry; 34 bad runtime entry; 35 no stack. The result is written only on status 0. + * A long is its two's-complement bits; an array is its address then its element count. + * Parameter names carry o_ so no language's reserved words collide. */ +#ifndef OMEGA_NATIVE_H +#define OMEGA_NATIVE_H +#include +#ifdef __cplusplus +extern "C" { +#endif +#define OMEGA_OK 0u +#define OMEGA_BAD_ARG 32u +#define OMEGA_REENTRY 33u +#define OMEGA_BAD_RT 34u +#define OMEGA_NO_STACK 35u +/* fn madd a ulong b ulong c ulong to ulong */ +uint32_t omega_madd(uint64_t *result, uint64_t o_a, uint64_t o_b, uint64_t o_c); +/* fn smadd a long b long c long to long */ +uint32_t omega_smadd(uint64_t *result, uint64_t o_a, uint64_t o_b, uint64_t o_c); +/* fn fill buf edit ulong 4 k ulong to ulong */ +uint32_t omega_fill(uint64_t *result, void *o_buf, uint64_t o_buf_len, uint64_t o_k); +/* fn spin n ulong to ulong */ +uint32_t omega_spin(uint64_t *result, uint64_t o_n); +/* fn mix a ulong b long c view uchar 3 d edit uchar 2 e view ulong 2 f ulong to void */ +uint32_t omega_mix(uint64_t *result, uint64_t o_a, uint64_t o_b, const void *o_c, uint64_t o_c_len, void *o_d, uint64_t o_d_len, const void *o_e, uint64_t o_e_len, uint64_t o_f); +/* fn big n ulong to ulong */ +uint32_t omega_big(uint64_t *result, uint64_t o_n); +#ifdef __cplusplus +} +#endif +#endif diff --git a/evidence/native-bindings/omega_native.py b/evidence/native-bindings/omega_native.py new file mode 100644 index 0000000..6b811ca --- /dev/null +++ b/evidence/native-bindings/omega_native.py @@ -0,0 +1,36 @@ +# Generated by omega-bridge src/native.om: native call ABI v1 (docs/NATIVE-ABI.md). +# ctypes declarations for libomega_native.so beside this file. Regenerate it; do not edit it. +# Status: 0 done; 1..14 the Omega trap code; 32 argument refused (None, wrong length, misaligned, +# overlap); 33 reentry; 34 bad runtime entry; 35 no stack. The result (a ctypes.c_uint64 passed with +# ctypes.byref) is written only on status 0. A long is its two's-complement bits; an array is a +# ctypes array (or any address) then its element count. +import ctypes, os +OMEGA_OK, OMEGA_BAD_ARG, OMEGA_REENTRY, OMEGA_BAD_RT, OMEGA_NO_STACK = 0, 32, 33, 34, 35 +lib = ctypes.CDLL(os.path.join(os.path.dirname(os.path.abspath(__file__)), "libomega_native.so")) +_u64 = ctypes.c_uint64 +_res = ctypes.POINTER(ctypes.c_uint64) +_arr = ctypes.c_void_p +# fn madd a ulong b ulong c ulong to ulong +omega_madd = lib.omega_madd +omega_madd.argtypes = [_res, _u64, _u64, _u64] +omega_madd.restype = ctypes.c_uint32 +# fn smadd a long b long c long to long +omega_smadd = lib.omega_smadd +omega_smadd.argtypes = [_res, _u64, _u64, _u64] +omega_smadd.restype = ctypes.c_uint32 +# fn fill buf edit ulong 4 k ulong to ulong +omega_fill = lib.omega_fill +omega_fill.argtypes = [_res, _arr, _u64, _u64] +omega_fill.restype = ctypes.c_uint32 +# fn spin n ulong to ulong +omega_spin = lib.omega_spin +omega_spin.argtypes = [_res, _u64] +omega_spin.restype = ctypes.c_uint32 +# fn mix a ulong b long c view uchar 3 d edit uchar 2 e view ulong 2 f ulong to void +omega_mix = lib.omega_mix +omega_mix.argtypes = [_res, _u64, _u64, _arr, _u64, _arr, _u64, _arr, _u64, _u64] +omega_mix.restype = ctypes.c_uint32 +# fn big n ulong to ulong +omega_big = lib.omega_big +omega_big.argtypes = [_res, _u64] +omega_big.restype = ctypes.c_uint32 diff --git a/evidence/native-bindings/omega_native.rs b/evidence/native-bindings/omega_native.rs new file mode 100644 index 0000000..0547b8f --- /dev/null +++ b/evidence/native-bindings/omega_native.rs @@ -0,0 +1,31 @@ +// Generated by omega-bridge src/native.om: native call ABI v1 (docs/NATIVE-ABI.md). +// Declarations for libomega_native (build with -L ). Regenerate it; do not edit it. +// Status: 0 done; 1..14 the Omega trap code; 32 argument refused (null, wrong length, misaligned, +// overlap); 33 reentry; 34 bad runtime entry; 35 no stack. The result is written only on status 0. +// A long is its two's-complement bits (an i64 as u64); an array is its pointer then its element count. +#[allow(dead_code)] +pub const OMEGA_OK: u32 = 0; +#[allow(dead_code)] +pub const OMEGA_BAD_ARG: u32 = 32; +#[allow(dead_code)] +pub const OMEGA_REENTRY: u32 = 33; +#[allow(dead_code)] +pub const OMEGA_BAD_RT: u32 = 34; +#[allow(dead_code)] +pub const OMEGA_NO_STACK: u32 = 35; +#[allow(dead_code)] +#[link(name = "omega_native")] +extern "C" { + // fn madd a ulong b ulong c ulong to ulong + pub fn omega_madd(result: *mut u64, o_a: u64, o_b: u64, o_c: u64) -> u32; + // fn smadd a long b long c long to long + pub fn omega_smadd(result: *mut u64, o_a: u64, o_b: u64, o_c: u64) -> u32; + // fn fill buf edit ulong 4 k ulong to ulong + pub fn omega_fill(result: *mut u64, o_buf: *mut u64, o_buf_len: u64, o_k: u64) -> u32; + // fn spin n ulong to ulong + pub fn omega_spin(result: *mut u64, o_n: u64) -> u32; + // fn mix a ulong b long c view uchar 3 d edit uchar 2 e view ulong 2 f ulong to void + pub fn omega_mix(result: *mut u64, o_a: u64, o_b: u64, o_c: *const u8, o_c_len: u64, o_d: *mut u8, o_d_len: u64, o_e: *const u64, o_e_len: u64, o_f: u64) -> u32; + // fn big n ulong to ulong + pub fn omega_big(result: *mut u64, o_n: u64) -> u32; +} diff --git a/evidence/native-bindings/omega_native.zig b/evidence/native-bindings/omega_native.zig new file mode 100644 index 0000000..f1efafd --- /dev/null +++ b/evidence/native-bindings/omega_native.zig @@ -0,0 +1,22 @@ +// Generated by omega-bridge src/native.om: native call ABI v1 (docs/NATIVE-ABI.md). +// Declarations for libomega_native (build with -L -lomega_native -lc). Regenerate it; do not edit it. +// Status: 0 done; 1..14 the Omega trap code; 32 argument refused (null, wrong length, misaligned, +// overlap); 33 reentry; 34 bad runtime entry; 35 no stack. The result is written only on status 0. +// A long is its two's-complement bits (@bitCast); an array is its pointer then its element count. +pub const OMEGA_OK: u32 = 0; +pub const OMEGA_BAD_ARG: u32 = 32; +pub const OMEGA_REENTRY: u32 = 33; +pub const OMEGA_BAD_RT: u32 = 34; +pub const OMEGA_NO_STACK: u32 = 35; +// fn madd a ulong b ulong c ulong to ulong +pub extern fn omega_madd(result: ?*u64, o_a: u64, o_b: u64, o_c: u64) u32; +// fn smadd a long b long c long to long +pub extern fn omega_smadd(result: ?*u64, o_a: u64, o_b: u64, o_c: u64) u32; +// fn fill buf edit ulong 4 k ulong to ulong +pub extern fn omega_fill(result: ?*u64, o_buf: ?[*]u64, o_buf_len: u64, o_k: u64) u32; +// fn spin n ulong to ulong +pub extern fn omega_spin(result: ?*u64, o_n: u64) u32; +// fn mix a ulong b long c view uchar 3 d edit uchar 2 e view ulong 2 f ulong to void +pub extern fn omega_mix(result: ?*u64, o_a: u64, o_b: u64, o_c: ?[*]const u8, o_c_len: u64, o_d: ?[*]u8, o_d_len: u64, o_e: ?[*]const u64, o_e_len: u64, o_f: u64) u32; +// fn big n ulong to ulong +pub extern fn omega_big(result: ?*u64, o_n: u64) u32; diff --git a/evidence/native-bindings/python.log b/evidence/native-bindings/python.log new file mode 100644 index 0000000..80e6776 --- /dev/null +++ b/evidence/native-bindings/python.log @@ -0,0 +1,10 @@ +PASS madd 3*5+7 = 22 +PASS madd wraps = 3 +PASS madd large +PASS madd signed bits = -13 +PASS smadd overflow traps 1, result unchanged +PASS fill k=1 traps 3, result unchanged +PASS writes before the trap stay visible +PASS fill k=0 = 3 +PASS null result refused 32 +PASS 0 failure(s) diff --git a/evidence/native-bindings/run.log b/evidence/native-bindings/run.log new file mode 100644 index 0000000..6308bcf --- /dev/null +++ b/evidence/native-bindings/run.log @@ -0,0 +1,23 @@ +PASS tools/native: library + six bindings (2beb3d30a6f46b74) +PASS no caller declares an Omega function by hand +PASS embedded code == omc output, per unit (38c2940801119902) +PASS c caller (26 lines) +PASS c-bti caller (26 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 missing output word +PASS generator refuses an unknown output word +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 mutant h scalar uint64_t -> uint32_t caught +PASS mutant python scalar c_uint64 -> c_uint32 caught +PASS mutant rust scalar u64 -> u32 caught +PASS mutant zig scalar u64 -> u32 caught +PASS mutant go scalar uint64 -> uint32 caught +NATIVE-CALL PASS (not run: none) diff --git a/evidence/native-bindings/rust.log b/evidence/native-bindings/rust.log new file mode 100644 index 0000000..c28138e --- /dev/null +++ b/evidence/native-bindings/rust.log @@ -0,0 +1,11 @@ +PASS madd 3*5+7 = 22 +PASS madd wraps = 3 +PASS madd large +PASS madd signed bits = -13 +PASS smadd overflow traps 1, result unchanged +PASS fill k=1 traps 3, result unchanged +PASS writes before the trap stay visible +PASS fill k=0 = 3 +PASS null result refused 32 +PASS big local array on a default std::thread = 15 +PASS 0 failure(s) diff --git a/evidence/native-bindings/tools-native.log b/evidence/native-bindings/tools-native.log new file mode 100644 index 0000000..fafe8a9 --- /dev/null +++ b/evidence/native-bindings/tools-native.log @@ -0,0 +1 @@ +tools/native: 6 unit(s) -> /home/drakestapleton/workspace/bridge-native-call/build/native/libomega_native.so + omega_native.{c,h,py,rs,zig,go} diff --git a/evidence/native-bindings/zig.log b/evidence/native-bindings/zig.log new file mode 100644 index 0000000..80e6776 --- /dev/null +++ b/evidence/native-bindings/zig.log @@ -0,0 +1,10 @@ +PASS madd 3*5+7 = 22 +PASS madd wraps = 3 +PASS madd large +PASS madd signed bits = -13 +PASS smadd overflow traps 1, result unchanged +PASS fill k=1 traps 3, result unchanged +PASS writes before the trap stay visible +PASS fill k=0 = 3 +PASS null result refused 32 +PASS 0 failure(s) From 350e82bf70c7120be3e429141d28ae23eb6af381 Mon Sep 17 00:00:00 2001 From: Drake Stapleton Date: Sat, 24 Oct 2026 18:51:14 -0500 Subject: [PATCH 3/4] native bindings: review fixes (Rust unsafe extern, Python range checks, Go header preamble, atomic tools/native) Fixes from the review of PR #9, all qualified by tests/native/run.sh (25 checks, nothing NOT_RUN): - src/native.om: the Rust block is `unsafe extern "C"` (edition 2024 rejects a plain extern block); the Python binding range-checks every scalar (an int in 0..2^64-1, else ctypes.ArgumentError, never truncated) and types arrays per kind (POINTER(c_uint64), POINTER(c_ubyte), c_char_p) so bytes are refused for a writable array; the Go preamble includes omega_native.h instead of repeating the prototypes, bakes in no run-time path, and is written in one pass. - tools/native: everything is produced in a scratch folder and installed together after the library compiles; a refused unit is named (exit 110) and a previous run's outputs stay; intermediates in work/. - tests: mix (six parameters, void result, trap after writes, conversion trap) in all six callers; Python refusals (2^64+3, -1, 2.0, bytes for edit arrays) and a call afterwards; reference.c (hand-written prototypes compiled against the generated header); Rust built under editions 2021 and 2024 with -D warnings; the Go program names the library folder through CGO_LDFLAGS; each generator mutant must change exactly one line and be caught for its stated reason; PIN read from tools/native. - docs/NATIVE-ABI.md: Python/Rust/Go mapping, tools/native behaviour, qualification list, mutant counts from the evidence (the Python mutant fails seven of eighteen checks, not two of ten). Co-authored-by: Drake Stapleton Co-Authored-By: Claude Fable 5.1 --- docs/NATIVE-ABI.md | 93 +++++++++++++++++++++++++--------------- src/native.om | 37 ++++++---------- tests/native/caller.cpp | 16 +++++++ tests/native/caller.go | 20 ++++++++- tests/native/caller.py | 28 ++++++++++++ tests/native/caller.rs | 16 +++++++ tests/native/caller.zig | 16 ++++++- tests/native/reference.c | 19 ++++++++ tests/native/run.sh | 78 +++++++++++++++++++++------------ tools/native | 53 ++++++++++++++++------- 10 files changed, 270 insertions(+), 106 deletions(-) create mode 100644 tests/native/reference.c diff --git a/docs/NATIVE-ABI.md b/docs/NATIVE-ABI.md index 02abc87..48f673f 100644 --- a/docs/NATIVE-ABI.md +++ b/docs/NATIVE-ABI.md @@ -52,32 +52,42 @@ with a fixed length; result `ulong`, `long` or `void`; one function per unit. ## The declarations for each language (`tools/native`) `OMEGA_NATIVE_OMC= tools/native OUTDIR unit.om...` compiles the units with the pinned -compiler, runs the generator once per output word and writes, into OUTDIR: +compiler, runs the generator once per output word and installs, into OUTDIR: | file | for | how a program uses it | |---|---|---| | `omega_native.c` | the library source | built into `libomega_native.so` by the same command | | `omega_native.h` | C and C++ | `#include "omega_native.h"` (`extern "C"` inside for C++) | | `omega_native.py` | Python 3 (ctypes) | `import omega_native`; loads `libomega_native.so` from beside itself | -| `omega_native.rs` | Rust | `mod omega_native;` beside the crate root; link with `-L OUTDIR` | +| `omega_native.rs` | Rust 1.82 or later, edition 2021 or 2024 | `mod omega_native;` beside the crate root; link with `-L OUTDIR` | | `omega_native.zig` | Zig 0.13 | `@import("omega_native.zig")`; build with `-L OUTDIR -lomega_native -lc` | -| `omega_native.go` | Go (cgo) | package `omeganative`: a folder of that name holding the file and the library | +| `omega_native.go` | Go (cgo) | package `omeganative`: a folder of that name holding the file, `omega_native.h` and the library | +| `work/` | the test driver | the generator and each unit's compiled code; not an interface | Every file carries the unit's Omega signature line as a comment above its declaration, the five status constants (0, 32, 33, 34, 35) and the parameter names prefixed `o_` (so no language's reserved words collide). The scalar type is the language's unsigned 64-bit integer, an array is a pointer then a -`uint64_t` count, the status is unsigned 32-bit: C `uint64_t` / `void *` / `const void *`; Python -`ctypes.c_uint64` / `ctypes.c_void_p` (any ctypes array or address); Rust `u64` / `*mut u64`, `*mut u8`, -`*const u64`, `*const u8`; Zig `u64` / `?[*]u64`, `?[*]u8`, `?[*]const u64`, `?[*]const u8`; Go -`uint64` / `unsafe.Pointer` to memory holding no Go pointers. The result pointer is `uint64_t *`, -`ctypes.POINTER(c_uint64)` (pass `ctypes.byref`), `*mut u64`, `?*u64`, `*uint64`. The Go file wraps -each C function as `Omega_` so the caller needs no cgo of its own. - -All six files are generated before any is written, so a refused unit leaves OUTDIR without a half set. -The library is compiled with its own header included (`-include omega_native.h`): a declaration that -disagrees with a definition fails that build, so the C file and the header cannot drift apart. The -other four files are checked by the qualification below, where each language builds its caller against -its generated file only and a one-word type change in the generator must make that build or run fail. +`uint64_t` count, the status is unsigned 32-bit: C `uint64_t` / `void *` / `const void *`; Rust `u64` / +`*mut u64`, `*mut u8`, `*const u64`, `*const u8` (declared in an `unsafe extern "C"` block); Zig `u64` / +`?[*]u64`, `?[*]u8`, `?[*]const u64`, `?[*]const u8`; Go `uint64` / `unsafe.Pointer` to memory holding +no Go pointers. Python checks instead of trusting: a scalar is an `int` in 0 .. 2^64-1 (a `long` is +passed as its two's-complement bits) and anything else, including a negative or an out-of-range value, +raises `ctypes.ArgumentError` rather than being truncated; an array is a ctypes array of its element type +(`POINTER(c_uint64)` for `edit`/`view ulong`, `POINTER(c_ubyte)` for `edit uchar`, `c_char_p` for `view +uchar`, which also takes `bytes`), and an `edit` array must be writable, so `bytes` are refused there. The +result pointer is `uint64_t *`, `ctypes.POINTER(c_uint64)` (pass `ctypes.byref`), `*mut u64`, `?*u64`, +`*uint64`. The Go file wraps each C function as `Omega_` so the caller needs no cgo of its own; its +preamble includes the generated header (so the Go side and the C side cannot disagree) and bakes in no +run-time path: the program's build names the folder (`CGO_LDFLAGS=-Wl,-rpath,`) or +`LD_LIBRARY_PATH` does. + +Everything is produced in a scratch folder and installed together after the library compiles. A unit +outside the subset, or a name the binding cannot carry, is refused by name (exit 110): nothing is +installed and a previous run's outputs in OUTDIR stay as they were. The library is compiled with its own +header included (`-include omega_native.h`): a declaration that disagrees with a definition fails that +build, so the C file and the header cannot drift apart. The other four files are checked by the +qualification below, where each language builds its caller against its generated file only and a +one-word type change in the generator must make that build or run fail for the stated reason. ## Contract @@ -122,18 +132,24 @@ mod 2^64 = 3637949782307626511 (calc:9bf5b67e, Wolfram agrees); -13 as 64-bit bi The six callers (C, C++, Python, Rust, Zig, Go) take every Omega declaration from their generated file (the run fails if any caller source declares an `omega_` function, sets ctypes `argtypes`, loads the -library itself or carries its own cgo). They check values, wrap-around, signed bits, a signed overflow trap with -the result left unchanged, a trap after two buffer writes (writes visible, result unchanged), a -successful buffer call and a refused null result. The C caller also checks a null buffer, a wrong length, +library itself or carries its own cgo). All six check values, wrap-around, signed bits, a signed overflow +trap with the result left unchanged, a trap after two buffer writes (writes visible, result unchanged), a +successful buffer call, a refused null result (C++: a wrong length) and the six-parameter unit `mix` +(`view`/`edit` `uchar` and `ulong` arrays, no result: success with the void result written as 0, a `trap +9` after two writes with the result unchanged, a conversion trap 5 after one write). The Python caller +also checks that the binding refuses 2^64+3, -1 and 2.0 as scalars and `bytes` for an `edit` array, and +that a call works after those refusals. The C caller also checks a null buffer, a wrong length, misalignment, result/buffer overlap, a view overlapping an edit array, a call after traps, reentry from a -signal handler (status 33), a six-parameter unit with `view`/`edit` `uchar` and `ulong` arrays and no -result (success, a `trap 9` after two writes, a conversion trap 5 after one write), a trap from a frame -holding a 4,000,000-byte local array, that array on a 256 KiB thread, and that x19..x28 and d8..d15 keep -the caller's values across a normal return and a trap. The Rust caller runs the array on a default -thread. The C caller runs a second time against a library built with BTI enforced. The generator is -shown to refuse a non-C unit name, a parameter ending in `_len` and a repeated unit name, and to accept -the same unit once. Not exercised: status 34 and 35 (no supported unit reaches another runtime entry; -mapping failure is not induced). +signal handler (status 33), a trap from a frame holding a 4,000,000-byte local array, that array on a +256 KiB thread, and that x19..x28 and d8..d15 keep the caller's values across a normal return and a trap. +The Rust caller runs the array on a default thread and is built twice, under edition 2021 and edition +2024, with warnings as errors. The C caller runs a second time against a library built with BTI enforced. +`tests/native/reference.c`, prototypes for the six test units written by hand from this contract, is +compiled with the generated header included: a disagreement is a conflicting-types error. The generator +is shown to refuse a non-C unit name, a parameter ending in `_len` and a repeated unit name, and to accept +the same unit once; `tools/native` is shown to refuse a unit with a `uchar` result by name, with exit 110, +leaving a previous run's outputs untouched. Not exercised: status 34 and 35 (no supported unit reaches +another runtime entry; mapping failure is not induced). 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 @@ -146,19 +162,26 @@ own. Only AArch64 Linux is supported. Zig and Go: run with Zig 0.13.0 and Go 1.27.3 (official aarch64 archives, checksums from ziglang.org/download/index.json and go.dev/dl, unpacked in the user's ~/.local/opt). Both callers pass -10 checks. The library is linked with `-soname libomega_native.so`: without that name, Zig records the +their checks (12 each in this change). The library is linked with `-soname libomega_native.so`: without that name, Zig records the library as `./libomega_native.so` and the caller fails to load outside the build folder (red: dd8b9a9; green: 1e40844). The embedded code is unchanged by the name. Generated declarations (this change): before it, each caller typed its own prototypes and a wrong type there was the caller's mistake to find. Now `tools/native` builds everything and the generator writes the six files above from the parsed signatures; the C output is byte for byte what the previous -generator wrote. The run adds: the no-hand-declaration check; refusals of a missing and of an unknown -output word; and five generator mutants, one per binding, each changing one scalar type word in the -generator's text (`uint64_t` to `uint32_t`, `c_uint64` to `c_uint32`, `u64` to `u32` twice, `uint64` -to `uint32`). Each is caught: the header mutant by the library build (`conflicting types for -omega_madd`), the Python mutant by wrong results (two of the ten checks fail: the argument is -truncated to 32 bits), the Rust, Zig and Go mutants by their compilers (a 64-bit value passed to a -32-bit parameter). Found by reading the output before the run, not by a test: the first Go wrapper +generator wrote. The run adds: the no-hand-declaration check; the hand-written reference prototypes; +the `tools/native` refusal; refusals of a missing and of an unknown output word; and five generator +mutants, one per binding, each changing one scalar type word on the generator's text line (`uint64_t` +to `uint32_t`, `c_uint64` to `c_uint32`, `u64` to `u32` twice, `uint64` to `uint32`). The run checks +that each mutant changes exactly one generator line and is caught for the stated reason: the header +mutant by the library build (`conflicting types for omega_madd`), the Python mutant by wrong results +(seven of the eighteen checks fail: the scalar is truncated to 32 bits), the Rust mutant by `mismatched +types`, the Zig mutant by `type 'u32' cannot represent integer value`, the Go mutant by `cannot use ... +as uint32 value`. Found by reading the output before the run, not by a test: the first Go wrapper called `C.omega_(` with no name; the test that would have caught it is the Go build, which this -change runs. +change runs. Review of the first version of this change led to: the Rust block declared `unsafe +extern` (edition 2024 rejects a plain `extern` block); Python scalars range-checked and arrays typed +per kind instead of `c_void_p` (which took `bytes` for a writable array and truncated scalars +silently); the Go preamble including the header instead of repeating prototypes, with no baked +run-time path; `tools/native` producing in a scratch folder, installing together, and naming the +refused unit. diff --git a/src/native.om b/src/native.om index ecbf3ca..b24cdb0 100644 --- a/src/native.om +++ b/src/native.om @@ -26,9 +26,9 @@ 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, 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}\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 ombr_rt rt;\n for (int i = 0; i < 32; i++)\n rt.entry[i] = ombr_no_entry;\n rt.entry[2] = ombr_trap;\n uint64_t v = 0;\n uint32_t s = ombr_enter(code, args, &rt, &v, top);\n ombr_active = 0;\n if (s == OMEGA_OK)\n *result = has_value ? v : 0u;\n return s;\n}\n" note mo: the output names; tb: the binding texts, fragments separated by ~ (byte 126): 0-11 h, -note 12-21 python, 22-34 rust, 35-46 zig, 47-67 go (48-49 with the h types 3-10 for the C preamble). +note 12-21 python, 22-34 rust, 35-46 zig, 47-64 go (its C preamble includes the generated header). text mo "chpythonrustziggo" -text tb "/* Generated by omega-bridge src/native.om: native call ABI v1 (docs/NATIVE-ABI.md).\n * C and C++ declarations for libomega_native. Regenerate it; do not edit it.\n * Status: 0 done; 1..14 the Omega trap code; 32 argument refused (null, wrong length, misaligned,\n * overlap); 33 reentry; 34 bad runtime entry; 35 no stack. The result is written only on status 0.\n * A long is its two's-complement bits; an array is its address then its element count.\n * Parameter names carry o_ so no language's reserved words collide. */\n#ifndef OMEGA_NATIVE_H\n#define OMEGA_NATIVE_H\n#include \n#ifdef __cplusplus\nextern \"C\" {\n#endif\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~/* ~ */\nuint32_t omega_~(uint64_t *result~, uint64_t o_~, void *o_~, const void *o_~, void *o_~, const void *o_~_len~);\n~#ifdef __cplusplus\n}\n#endif\n#endif\n~# Generated by omega-bridge src/native.om: native call ABI v1 (docs/NATIVE-ABI.md).\n# ctypes declarations for libomega_native.so beside this file. Regenerate it; do not edit it.\n# Status: 0 done; 1..14 the Omega trap code; 32 argument refused (None, wrong length, misaligned,\n# overlap); 33 reentry; 34 bad runtime entry; 35 no stack. The result (a ctypes.c_uint64 passed with\n# ctypes.byref) is written only on status 0. A long is its two's-complement bits; an array is a\n# ctypes array (or any address) then its element count.\nimport ctypes, os\nOMEGA_OK, OMEGA_BAD_ARG, OMEGA_REENTRY, OMEGA_BAD_RT, OMEGA_NO_STACK = 0, 32, 33, 34, 35\nlib = ctypes.CDLL(os.path.join(os.path.dirname(os.path.abspath(__file__)), \"libomega_native.so\"))\n_u64 = ctypes.c_uint64\n_res = ctypes.POINTER(ctypes.c_uint64)\n_arr = ctypes.c_void_p\n~# ~\nomega_~ = lib.omega_~\nomega_~.argtypes = [_res~, _u64~, _arr~]\nomega_~.restype = ctypes.c_uint32\n~// Generated by omega-bridge src/native.om: native call ABI v1 (docs/NATIVE-ABI.md).\n// Declarations for libomega_native (build with -L ). Regenerate it; do not edit it.\n// Status: 0 done; 1..14 the Omega trap code; 32 argument refused (null, wrong length, misaligned,\n// overlap); 33 reentry; 34 bad runtime entry; 35 no stack. The result is written only on status 0.\n// A long is its two's-complement bits (an i64 as u64); an array is its pointer then its element count.\n#[allow(dead_code)]\npub const OMEGA_OK: u32 = 0;\n#[allow(dead_code)]\npub const OMEGA_BAD_ARG: u32 = 32;\n#[allow(dead_code)]\npub const OMEGA_REENTRY: u32 = 33;\n#[allow(dead_code)]\npub const OMEGA_BAD_RT: u32 = 34;\n#[allow(dead_code)]\npub const OMEGA_NO_STACK: u32 = 35;\n#[allow(dead_code)]\n#[link(name = \"omega_native\")]\nextern \"C\" {\n~ // ~\n pub fn omega_~(result: *mut u64~, o_~: u64~: *mut u64~: *const u64~: *mut u8~: *const u8~_len: u64~) -> u32;\n~}\n~// Generated by omega-bridge src/native.om: native call ABI v1 (docs/NATIVE-ABI.md).\n// Declarations for libomega_native (build with -L -lomega_native -lc). Regenerate it; do not edit it.\n// Status: 0 done; 1..14 the Omega trap code; 32 argument refused (null, wrong length, misaligned,\n// overlap); 33 reentry; 34 bad runtime entry; 35 no stack. The result is written only on status 0.\n// A long is its two's-complement bits (@bitCast); an array is its pointer then its element count.\npub const OMEGA_OK: u32 = 0;\npub const OMEGA_BAD_ARG: u32 = 32;\npub const OMEGA_REENTRY: u32 = 33;\npub const OMEGA_BAD_RT: u32 = 34;\npub const OMEGA_NO_STACK: u32 = 35;\n~// ~\npub extern fn omega_~(result: ?*u64~, o_~: u64~: ?[*]u64~: ?[*]const u64~: ?[*]u8~: ?[*]const u8~_len: u64~) u32;\n~// Generated by omega-bridge src/native.om: native call ABI v1 (docs/NATIVE-ABI.md).\n// cgo bindings for libomega_native.so beside this file. Regenerate it; do not edit it.\n// Status: 0 done; 1..14 the Omega trap code; 32 argument refused (nil, wrong length, misaligned,\n// overlap); 33 reentry; 34 bad runtime entry; 35 no stack. The result is written only on status 0.\n// A long is its two's-complement bits (an int64 as uint64); an array is an unsafe.Pointer to memory\n// holding no Go pointers, then its element count.\npackage omeganative\n\n/*\n#cgo LDFLAGS: -L${SRCDIR} -lomega_native -Wl,-rpath,${SRCDIR}\n#include \n~// ~\nuint32_t omega_~*/\nimport \"C\"\n\nimport \"unsafe\"\n\nconst (\n\tOmegaOK uint32 = 0\n\tOmegaBadArg uint32 = 32\n\tOmegaReentry uint32 = 33\n\tOmegaBadRt uint32 = 34\n\tOmegaNoStack uint32 = 35\n)\n~\n// ~\nfunc Omega_~(result *uint64~, o_~ uint64~ unsafe.Pointer~ unsafe.Pointer~ unsafe.Pointer~ unsafe.Pointer~_len uint64~) uint32 {\n\treturn uint32(C.omega_~((*C.uint64_t)(unsafe.Pointer(result))~, C.uint64_t(o_~)~, o_~_len)~))\n}\n" +text tb "/* Generated by omega-bridge src/native.om: native call ABI v1 (docs/NATIVE-ABI.md).\n * C and C++ declarations for libomega_native. Regenerate it; do not edit it.\n * Status: 0 done; 1..14 the Omega trap code; 32 argument refused (null, wrong length, misaligned,\n * overlap); 33 reentry; 34 bad runtime entry; 35 no stack. The result is written only on status 0.\n * A long is its two's-complement bits; an array is its address then its element count.\n * Parameter names carry o_ so no language's reserved words collide. */\n#ifndef OMEGA_NATIVE_H\n#define OMEGA_NATIVE_H\n#include \n#ifdef __cplusplus\nextern \"C\" {\n#endif\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~/* ~ */\nuint32_t omega_~(uint64_t *result~, uint64_t o_~, void *o_~, const void *o_~, void *o_~, const void *o_~_len~);\n~#ifdef __cplusplus\n}\n#endif\n#endif\n~# Generated by omega-bridge src/native.om: native call ABI v1 (docs/NATIVE-ABI.md).\n# ctypes declarations for libomega_native.so beside this file. Regenerate it; do not edit it.\n# Status: 0 done; 1..14 the Omega trap code; 32 argument refused (None, wrong length, misaligned,\n# overlap); 33 reentry; 34 bad runtime entry; 35 no stack. The result (a ctypes.c_uint64 passed with\n# ctypes.byref) is written only on status 0. A scalar is an int in 0 .. 2**64-1 (a long is passed as its\n# two's-complement bits); anything else raises ctypes.ArgumentError, nothing is truncated. An array is a\n# ctypes array of its element type (bytes for a view uchar) then its element count; an edit array must\n# be writable, so bytes are refused there.\nimport ctypes, os\nOMEGA_OK, OMEGA_BAD_ARG, OMEGA_REENTRY, OMEGA_BAD_RT, OMEGA_NO_STACK = 0, 32, 33, 34, 35\nlib = ctypes.CDLL(os.path.join(os.path.dirname(os.path.abspath(__file__)), \"libomega_native.so\"))\nclass _u64:\n @classmethod\n def from_param(cls, v):\n if type(v) is not int or v < 0 or v > 18446744073709551615:\n raise ValueError(\"omega scalar: an int in 0 .. 2**64-1 expected, got %r\" % (v,))\n return ctypes.c_uint64(v)\n_res = ctypes.POINTER(ctypes.c_uint64)\n_a3 = ctypes.POINTER(ctypes.c_uint64) # edit ulong: a writable ctypes array of c_uint64\n_a4 = ctypes.POINTER(ctypes.c_uint64) # view ulong: a ctypes array of c_uint64\n_a5 = ctypes.POINTER(ctypes.c_ubyte) # edit uchar: a writable ctypes array of c_ubyte\n_a6 = ctypes.c_char_p # view uchar: bytes, or a ctypes array of c_char\n~# ~\nomega_~ = lib.omega_~\nomega_~.argtypes = [_res~, _u64~, _a~]\nomega_~.restype = ctypes.c_uint32\n~// Generated by omega-bridge src/native.om: native call ABI v1 (docs/NATIVE-ABI.md).\n// Declarations for libomega_native (build with -L ; Rust 1.82 or later). Regenerate it; do not edit it.\n// Status: 0 done; 1..14 the Omega trap code; 32 argument refused (null, wrong length, misaligned,\n// overlap); 33 reentry; 34 bad runtime entry; 35 no stack. The result is written only on status 0.\n// A long is its two's-complement bits (an i64 as u64); an array is its pointer then its element count.\n#[allow(dead_code)]\npub const OMEGA_OK: u32 = 0;\n#[allow(dead_code)]\npub const OMEGA_BAD_ARG: u32 = 32;\n#[allow(dead_code)]\npub const OMEGA_REENTRY: u32 = 33;\n#[allow(dead_code)]\npub const OMEGA_BAD_RT: u32 = 34;\n#[allow(dead_code)]\npub const OMEGA_NO_STACK: u32 = 35;\n#[allow(dead_code)]\n#[link(name = \"omega_native\")]\nunsafe extern \"C\" {\n~ // ~\n pub fn omega_~(result: *mut u64~, o_~: u64~: *mut u64~: *const u64~: *mut u8~: *const u8~_len: u64~) -> u32;\n~}\n~// Generated by omega-bridge src/native.om: native call ABI v1 (docs/NATIVE-ABI.md).\n// Declarations for libomega_native (build with -L -lomega_native -lc). Regenerate it; do not edit it.\n// Status: 0 done; 1..14 the Omega trap code; 32 argument refused (null, wrong length, misaligned,\n// overlap); 33 reentry; 34 bad runtime entry; 35 no stack. The result is written only on status 0.\n// A long is its two's-complement bits (@bitCast); an array is its pointer then its element count.\npub const OMEGA_OK: u32 = 0;\npub const OMEGA_BAD_ARG: u32 = 32;\npub const OMEGA_REENTRY: u32 = 33;\npub const OMEGA_BAD_RT: u32 = 34;\npub const OMEGA_NO_STACK: u32 = 35;\n~// ~\npub extern fn omega_~(result: ?*u64~, o_~: u64~: ?[*]u64~: ?[*]const u64~: ?[*]u8~: ?[*]const u8~_len: u64~) u32;\n~// Generated by omega-bridge src/native.om: native call ABI v1 (docs/NATIVE-ABI.md).\n// cgo package omeganative: this file, omega_native.h and libomega_native.so in one folder. Regenerate it;\n// do not edit it. No run-time path is baked in: the program's build names the folder (CGO_LDFLAGS set to\n// -Wl,-rpath,), or LD_LIBRARY_PATH does.\n// Status: 0 done; 1..14 the Omega trap code; 32 argument refused (nil, wrong length, misaligned,\n// overlap); 33 reentry; 34 bad runtime entry; 35 no stack. The result is written only on status 0.\n// A long is its two's-complement bits (an int64 as uint64); an array is an unsafe.Pointer to memory\n// holding no Go pointers, then its element count.\npackage omeganative\n\n/*\n#cgo CFLAGS: -I${SRCDIR}\n#cgo LDFLAGS: -L${SRCDIR} -lomega_native\n#include \"omega_native.h\"\n*/\nimport \"C\"\n\nimport \"unsafe\"\n\nconst (\n\tOmegaOK uint32 = 0\n\tOmegaBadArg uint32 = 32\n\tOmegaReentry uint32 = 33\n\tOmegaBadRt uint32 = 34\n\tOmegaNoStack uint32 = 35\n)\n~\n// ~\nfunc Omega_~(result *uint64~, o_~ uint64~ unsafe.Pointer~ unsafe.Pointer~ unsafe.Pointer~ unsafe.Pointer~_len uint64~) uint32 {\n\treturn uint32(C.omega_~((*C.uint64_t)(unsafe.Pointer(result))~, C.uint64_t(o_~)~, o_~_len)~))\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 @@ -568,7 +568,7 @@ ofr 22 odec get st 3 ofr 23 end -note edeclC: a declaration with the type before the name (h; the C preamble of go): the comment +note edeclC: a declaration with the type before the name (h): the comment note fragments at cb (open, close + head) and the type fragments at tb (result, scalar, the four note array kinds, the length suffix, close). fn edeclC cb ulong tp ulong to void @@ -631,6 +631,7 @@ if le get pk j 2 ofb 18 else ofb 19 +odec get pk j ofb 18 end end @@ -641,23 +642,23 @@ end note ecall: the go wrapper's call of the C function and its end. fn ecall to void oname 1 -ofb 62 +ofb 59 for j ulong get st 2 if le get pk j 2 -ofb 63 +ofb 60 opn j -ofb 64 +ofb 61 else -ofb 65 +ofb 62 opn j -ofb 63 +ofb 60 opn j -ofb 66 +ofb 63 end end -ofb 67 +ofb 64 end -note emit: the parsed unit in the output named by st 5 (7 is the second pass of go). +note emit: the parsed unit in the output named by st 5. fn emit c0 ulong cl ulong to void m = get st 5 if eq m 1 @@ -678,10 +679,7 @@ if eq m 5 edeclR 36 38 end if eq m 6 -edeclC 48 3 -end -if eq m 7 -edeclR 51 53 +edeclR 48 50 ecall end end @@ -755,15 +753,6 @@ end if eq m 4 ofb 34 end -if eq m 6 -ofb 50 -put st 5 7 -put st 4 0 -p = 0 -while lt p t -p = unit p -end -end o = ulong 0 m = get st 1 while lt o m diff --git a/tests/native/caller.cpp b/tests/native/caller.cpp index 688f72a..1cca955 100644 --- a/tests/native/caller.cpp +++ b/tests/native/caller.cpp @@ -29,6 +29,22 @@ int main() check("writes before the trap stay visible", buf[0] == 11 && buf[1] == 22 && buf[2] == 0); check("fill k=0 = 3", omega_fill(&r, buf.data(), buf.size(), 0) == 0 && r == 3 && buf[2] == 33); check("wrong length refused 32", omega_fill(&r, buf.data(), 3, 0) == 32); + // mix: six parameters (ulong, long, view uchar 3, edit uchar 2, view ulong 2, ulong), no result. + // 5 + 20 + 7 = 32 (calc:08302bc4); 5 + 20 + 300 is 70 over 255 (calc:a6a8bb76), so the conversion traps 5. + const unsigned char cb[3] = {1, 2, 3}; + unsigned char db[2] = {0, 0}; + const std::uint64_t eb[2] = {10, 20}; + r = sentinel; + check("mix six parameters, void result writes 0", + omega_mix(&r, 5, 1, cb, 3, db, 2, eb, 2, 7) == 0 && r == 0 && db[0] == 3 && db[1] == 32); + db[0] = db[1] = 0; + r = sentinel; + check("mix negative long traps 9 after both writes, result unchanged", + omega_mix(&r, 5, static_cast(std::int64_t{-1}), cb, 3, db, 2, eb, 2, 7) == 9 && r == sentinel && + db[0] == 3 && db[1] == 32); + db[0] = db[1] = 0; + check("mix uchar conversion 325 traps 5, first write visible", + omega_mix(&r, 5, 1, cb, 3, db, 2, eb, 2, 300) == 5 && r == sentinel && db[0] == 3 && db[1] == 0); std::printf("%s %d failure(s)\n", fails ? "FAIL" : "PASS", fails); return fails != 0; } diff --git a/tests/native/caller.go b/tests/native/caller.go index 6218214..1006965 100644 --- a/tests/native/caller.go +++ b/tests/native/caller.go @@ -1,6 +1,6 @@ // Go caller for the omega-bridge native call ABI v1 test library. -// Run with Go 1.27.3 by tests/native/run.sh (10 checks). Every Omega declaration comes from the -// omeganative package (omega_native.go, generated by tools/native); this file has no cgo of its own. +// Run with Go 1.27.3 by tests/native/run.sh. Every Omega declaration comes from the omeganative +// package (omega_native.go, generated by tools/native); this file has no cgo of its own. package main import ( @@ -37,6 +37,22 @@ func main() { check("writes before the trap stay visible", buf[0] == 11 && buf[1] == 22 && buf[2] == 0) check("fill k=0 = 3", on.Omega_fill(&r, unsafe.Pointer(&buf[0]), 4, 0) == 0 && r == 3 && buf[2] == 33) check("null result refused 32", on.Omega_madd(nil, 3, 5, 7) == 32) + // mix: six parameters (ulong, long, view uchar 3, edit uchar 2, view ulong 2, ulong), no result. + // 5 + 20 + 7 = 32 (calc:08302bc4); 5 + 20 + 300 is 70 over 255 (calc:a6a8bb76), so the conversion traps 5. + cb := [3]byte{1, 2, 3} + var db [2]byte + eb := [2]uint64{10, 20} + r = sentinel + check("mix six parameters, void result writes 0", + on.Omega_mix(&r, 5, 1, unsafe.Pointer(&cb[0]), 3, unsafe.Pointer(&db[0]), 2, unsafe.Pointer(&eb[0]), 2, 7) == 0 && r == 0 && db == [2]byte{3, 32}) + db = [2]byte{} + r = sentinel + m1 := int64(-1) + check("mix negative long traps 9 after both writes, result unchanged", + on.Omega_mix(&r, 5, uint64(m1), unsafe.Pointer(&cb[0]), 3, unsafe.Pointer(&db[0]), 2, unsafe.Pointer(&eb[0]), 2, 7) == 9 && r == sentinel && db == [2]byte{3, 32}) + db = [2]byte{} + check("mix uchar conversion 325 traps 5, first write visible", + on.Omega_mix(&r, 5, 1, unsafe.Pointer(&cb[0]), 3, unsafe.Pointer(&db[0]), 2, unsafe.Pointer(&eb[0]), 2, 300) == 5 && r == sentinel && db == [2]byte{3, 0}) if fails > 0 { fmt.Println("FAIL", fails, "failure(s)") os.Exit(1) diff --git a/tests/native/caller.py b/tests/native/caller.py index 3a67756..da4b5bb 100644 --- a/tests/native/caller.py +++ b/tests/native/caller.py @@ -26,5 +26,33 @@ check("fill k=1 traps 3, result unchanged", om.omega_fill(ctypes.byref(r), buf, check("writes before the trap stay visible", list(buf) == [11, 22, 0, 0]) check("fill k=0 = 3", om.omega_fill(ctypes.byref(r), buf, 4, 0) == 0 and r.value == 3 and list(buf) == [11, 22, 33, 0]) check("null result refused 32", om.omega_madd(None, 3, 5, 7) == 32) +# mix: six parameters (ulong, long, view uchar 3, edit uchar 2, view ulong 2, ulong), no result. +# 5 + 20 + 7 = 32 (calc:08302bc4); 5 + 20 + 300 is 70 over 255 (calc:a6a8bb76), so the conversion traps 5. +cb = bytes([1, 2, 3]) # view uchar: bytes +db = (ctypes.c_ubyte * 2)() # edit uchar: a writable ctypes array +eb = (u64 * 2)(10, 20) # view ulong: a ctypes array +r = u64(SENTINEL) +check("mix six parameters, void result writes 0", + om.omega_mix(ctypes.byref(r), 5, 1, cb, 3, db, 2, eb, 2, 7) == 0 and r.value == 0 and list(db) == [3, 32]) +db[0] = db[1] = 0 +r = u64(SENTINEL) +check("mix negative long traps 9 after both writes, result unchanged", + om.omega_mix(ctypes.byref(r), 5, bits(-1), cb, 3, db, 2, eb, 2, 7) == 9 and r.value == SENTINEL and list(db) == [3, 32]) +db[0] = db[1] = 0 +check("mix uchar conversion 325 traps 5, first write visible", + om.omega_mix(ctypes.byref(r), 5, 1, cb, 3, db, 2, eb, 2, 300) == 5 and r.value == SENTINEL and list(db) == [3, 0]) +# The binding refuses what the ABI cannot carry instead of truncating or writing into immutable bytes. +def refused(f): + try: + f() + except ctypes.ArgumentError: + return True + return False +check("scalar 2**64+3 refused, not truncated", refused(lambda: om.omega_madd(ctypes.byref(r), 2**64 + 3, 5, 7))) +check("scalar -1 refused (pass its bits)", refused(lambda: om.omega_madd(ctypes.byref(r), -1, 5, 7))) +check("scalar 2.0 refused", refused(lambda: om.omega_madd(ctypes.byref(r), 2.0, 5, 7))) +check("bytes for an edit uchar array refused", refused(lambda: om.omega_mix(ctypes.byref(r), 5, 1, cb, 3, bytes(2), 2, eb, 2, 7))) +check("bytes for an edit ulong array refused", refused(lambda: om.omega_fill(ctypes.byref(r), bytes(32), 4, 0))) +check("call after refusals still works", om.omega_madd(ctypes.byref(r), 3, 5, 7) == 0 and r.value == 22) print(("FAIL " if fails else "PASS ") + "%d failure(s)" % fails) sys.exit(fails != 0) diff --git a/tests/native/caller.rs b/tests/native/caller.rs index 4945d0b..35b280b 100644 --- a/tests/native/caller.rs +++ b/tests/native/caller.rs @@ -25,6 +25,22 @@ fn main() { check("writes before the trap stay visible", buf == [11, 22, 0, 0]); check("fill k=0 = 3", omega_fill(&mut r, buf.as_mut_ptr(), 4, 0) == 0 && r == 3 && buf == [11, 22, 33, 0]); check("null result refused 32", omega_madd(std::ptr::null_mut(), 3, 5, 7) == 32); + // mix: six parameters (ulong, long, view uchar 3, edit uchar 2, view ulong 2, ulong), no result. + // 5 + 20 + 7 = 32 (calc:08302bc4); 5 + 20 + 300 is 70 over 255 (calc:a6a8bb76), so the conversion traps 5. + let cb = [1u8, 2, 3]; + let mut db = [0u8; 2]; + let eb = [10u64, 20]; + r = SENTINEL; + check("mix six parameters, void result writes 0", + omega_mix(&mut r, 5, 1, cb.as_ptr(), 3, db.as_mut_ptr(), 2, eb.as_ptr(), 2, 7) == 0 && r == 0 && db == [3, 32]); + db = [0, 0]; + r = SENTINEL; + check("mix negative long traps 9 after both writes, result unchanged", + omega_mix(&mut r, 5, (-1i64) as u64, cb.as_ptr(), 3, db.as_mut_ptr(), 2, eb.as_ptr(), 2, 7) == 9 + && r == SENTINEL && db == [3, 32]); + db = [0, 0]; + check("mix uchar conversion 325 traps 5, first write visible", + omega_mix(&mut r, 5, 1, cb.as_ptr(), 3, db.as_mut_ptr(), 2, eb.as_ptr(), 2, 300) == 5 && r == SENTINEL && db == [3, 0]); } // A std::thread has a 2 MiB stack by default; the 4,000,000-byte local array runs on the binding's own stack. // (7 + 1) + 7 = 15 (calc:e44bce3f). diff --git a/tests/native/caller.zig b/tests/native/caller.zig index e82332d..41da74c 100644 --- a/tests/native/caller.zig +++ b/tests/native/caller.zig @@ -1,6 +1,6 @@ // Zig caller for the omega-bridge native call ABI v1 test library. Written against Zig 0.13. -// Run with Zig 0.13.0 by tests/native/run.sh (10 checks). Every Omega declaration comes from -// omega_native.zig, generated by tools/native beside this file; nothing is typed by hand here. +// Run with Zig 0.13.0 by tests/native/run.sh. Every Omega declaration comes from omega_native.zig, +// generated by tools/native beside this file; nothing is typed by hand here. const std = @import("std"); const om = @import("omega_native.zig"); @@ -24,6 +24,18 @@ pub fn main() void { check("writes before the trap stay visible", buf[0] == 11 and buf[1] == 22 and buf[2] == 0); check("fill k=0 = 3", om.omega_fill(&r, &buf, 4, 0) == 0 and r == 3 and buf[2] == 33); check("null result refused 32", om.omega_madd(null, 3, 5, 7) == 32); + // mix: six parameters (ulong, long, view uchar 3, edit uchar 2, view ulong 2, ulong), no result. + // 5 + 20 + 7 = 32 (calc:08302bc4); 5 + 20 + 300 is 70 over 255 (calc:a6a8bb76), so the conversion traps 5. + const cb = [_]u8{ 1, 2, 3 }; + var db = [_]u8{ 0, 0 }; + const eb = [_]u64{ 10, 20 }; + r = sentinel; + check("mix six parameters, void result writes 0", om.omega_mix(&r, 5, 1, &cb, 3, &db, 2, &eb, 2, 7) == 0 and r == 0 and db[0] == 3 and db[1] == 32); + db = .{ 0, 0 }; + r = sentinel; + check("mix negative long traps 9 after both writes, result unchanged", om.omega_mix(&r, 5, @bitCast(@as(i64, -1)), &cb, 3, &db, 2, &eb, 2, 7) == 9 and r == sentinel and db[0] == 3 and db[1] == 32); + db = .{ 0, 0 }; + check("mix uchar conversion 325 traps 5, first write visible", om.omega_mix(&r, 5, 1, &cb, 3, &db, 2, &eb, 2, 300) == 5 and r == sentinel and db[0] == 3 and db[1] == 0); std.debug.print("{s} {d} failure(s)\n", .{ if (fails > 0) "FAIL" else "PASS", fails }); std.process.exit(if (fails > 0) 1 else 0); } diff --git a/tests/native/reference.c b/tests/native/reference.c new file mode 100644 index 0000000..6b5f5a8 --- /dev/null +++ b/tests/native/reference.c @@ -0,0 +1,19 @@ +/* A hand-written statement of the six test units' exported C signatures, from the contract in + * docs/NATIVE-ABI.md and the units' Omega signature lines, independent of the generator. + * tests/native/run.sh compiles this file with the generated omega_native.h included first: if the + * generator's declarations disagree with these, the compiler reports conflicting types. */ +#include + +/* fn madd a ulong b ulong c ulong to ulong */ +uint32_t omega_madd(uint64_t *result, uint64_t a, uint64_t b, uint64_t c); +/* fn smadd a long b long c long to long */ +uint32_t omega_smadd(uint64_t *result, uint64_t a, uint64_t b, uint64_t c); +/* fn fill buf edit ulong 4 k ulong to ulong */ +uint32_t omega_fill(uint64_t *result, void *buf, uint64_t buf_len, uint64_t k); +/* fn spin n ulong to ulong */ +uint32_t omega_spin(uint64_t *result, uint64_t n); +/* fn mix a ulong b long c view uchar 3 d edit uchar 2 e view ulong 2 f ulong to void */ +uint32_t omega_mix(uint64_t *result, uint64_t a, uint64_t b, const void *c, uint64_t c_len, void *d, uint64_t d_len, + const void *e, uint64_t e_len, uint64_t f); +/* fn big n ulong to ulong */ +uint32_t omega_big(uint64_t *result, uint64_t n); diff --git a/tests/native/run.sh b/tests/native/run.sh index be34589..77ceaa6 100755 --- a/tests/native/run.sh +++ b/tests/native/run.sh @@ -3,13 +3,13 @@ # (a*b+c) modulo 2^64, called from C, C++, Python, Rust, Zig and Go. tools/native builds the library # and writes every language's declarations from the Omega signatures (one generator, src/native.om); # no caller declares an Omega function by hand. OMEGA_NATIVE_OMC names the seeded compiler; its sha256 -# must be the pinned one below. A missing language toolchain is reported NOT_RUN, never PASS. +# must be the one tools/native pins. A missing language toolchain is reported NOT_RUN, never PASS. # Exit 0 only when every run check passed. set -eu -PIN=734f0a39319fbfa930bcc62cb30dbf06d47d78e126372b74f5baf478e6126b61 ROOT=$(cd "$(dirname "$0")/../.." && pwd) T=$ROOT/tests/native O=$ROOT/build/native +PIN=$(sed -n 's/^PIN=//p' "$ROOT/tools/native") OMC=${OMEGA_NATIVE_OMC:?set OMEGA_NATIVE_OMC to the seeded omc (omega seed/out/omc, sha256 $PIN)} got=$(sha256sum < "$OMC" | cut -d' ' -f1) [ "$got" = "$PIN" ] || { echo "FAIL omc sha256 $got, pinned $PIN"; exit 1; } @@ -21,15 +21,31 @@ U="madd smadd fill spin mix big" units=""; for u in $U; do units="$units $T/$u.om"; done # tools/native: the generator, the units' code, the library source, the header, the python, rust, zig # and go bindings, then the library built with the header included (a declaration that disagrees with -# its definition fails that build). +# its definition fails that build). Its work/ folder holds the generator and the units' code. if OMEGA_NATIVE_OMC="$OMC" "$ROOT/tools/native" "$O" $units > "$O/tools-native.log" 2>&1; then pass "tools/native: library + six bindings ($(cd "$O" && cat omega_native.h omega_native.py omega_native.rs omega_native.zig omega_native.go | sha256sum | cut -c1-16))" else fail "tools/native"; cat "$O/tools-native.log"; exit 1; fi -GEN=$O/native-gen +W=$O/work +GEN=$W/native-gen +# A unit outside the subset (here: a uchar result) is refused by name with exit 110, and a previous run's +# outputs in that folder stay as they were: nothing half-written, no scratch left behind. +printf 'fn m a ulong to uchar\nret uchar a\nend\n' > "$O/bad.om" +OMEGA_NATIVE_OMC="$OMC" "$ROOT/tools/native" "$O/keep" "$T/madd.om" > /dev/null 2>&1 +h0=$(sha256sum < "$O/keep/omega_native.h") +rc=0; OMEGA_NATIVE_OMC="$OMC" "$ROOT/tools/native" "$O/keep" "$T/madd.om" "$O/bad.om" > "$O/refused.log" 2>&1 || rc=$? +if [ "$rc" = 110 ] && grep -q "refused $O/bad.om" "$O/refused.log" && [ "$(sha256sum < "$O/keep/omega_native.h")" = "$h0" ] \ + && [ -z "$(ls -A "$O/keep" | grep -v -E '^(omega_native\.(c|h|py|rs|zig|go)|libomega_native\.so|work)$')" ]; then + pass "tools/native refuses a unit outside the subset by name (exit 110), previous outputs untouched" +else fail "tools/native refusal (exit $rc)"; cat "$O/refused.log"; ls -A "$O/keep"; fi # Every caller takes its declarations from the generated file; none types an Omega function itself. if grep -n -E 'uint32_t omega_|fn omega_|argtypes|CDLL|import "C"' "$T"/caller.* > "$O/hand.log"; then fail "a caller declares an Omega function by hand"; cat "$O/hand.log" else pass "no caller declares an Omega function by hand"; fi +# The generated header against prototypes written by hand from the contract (tests/native/reference.c), +# independent of the generator: a disagreement is a conflicting-types error. +if cc -std=gnu11 -fsyntax-only -Wall -Wextra -Werror -include "$O/omega_native.h" "$T/reference.c" > "$O/reference.log" 2>&1; then + pass "generated header agrees with the hand-written reference prototypes" +else fail "generated header disagrees with tests/native/reference.c"; cat "$O/reference.log"; fi # The embedded code must be the compiler's bytes, unchanged. # Each unit is read at its symbol, mapped from load address to file offset through its section. emb() { @@ -45,9 +61,9 @@ ok=1; : > "$O/code.bin" for u in $U; do off=$(emb "$O/libomega_native.so" $u) if [ -z "$off" ]; then ok=0; continue; fi - dd if="$O/libomega_native.so" bs=1 skip="$off" count="$(wc -c < "$O/$u.bin")" 2>/dev/null > "$O/$u.emb" - cmp -s "$O/$u.bin" "$O/$u.emb" || ok=0 - cat "$O/$u.bin" >> "$O/code.bin" + dd if="$O/libomega_native.so" bs=1 skip="$off" count="$(wc -c < "$W/$u.bin")" 2>/dev/null > "$O/$u.emb" + cmp -s "$W/$u.bin" "$O/$u.emb" || ok=0 + cat "$W/$u.bin" >> "$O/code.bin" done if [ $ok = 1 ]; then pass "embedded code == omc output, per unit ($(sha256sum < "$O/code.bin" | cut -c1-16))" else fail "embedded code differs from omc output"; fi @@ -68,7 +84,7 @@ if OMEGA_NATIVE_OMC="$OMC" OMEGA_NATIVE_CFLAGS="-nostartfiles -mbranch-protectio else fail "c-bti build carries no BTI property"; fi else fail "c-bti library build"; cat "$O/bti-native.log" 2> /dev/null || true; fi # Generator refusals: names the C side cannot carry give exit 110 and no output, never broken C. -unitin() { printf '%s' "$1"; printf '\0'; printf '%08d' "$(wc -c < "$O/madd.bin")"; cat "$O/madd.bin"; } +unitin() { printf '%s' "$1"; printf '\0'; printf '%08d' "$(wc -c < "$W/madd.bin")"; cat "$W/madd.bin"; } refuse() { rc=0; "$GEN" c < "$O/ref.in" > "$O/ref.out" 2> /dev/null || rc=$? if [ "$rc" = 110 ] && [ ! -s "$O/ref.out" ]; then pass "generator refuses $1"; else fail "generator accepted $1 (exit $rc)"; fi @@ -86,9 +102,9 @@ end unitin "fn m $S" > "$O/ref.in"; rc=0; "$GEN" c < "$O/ref.in" > "$O/ref.out" 2> /dev/null || rc=$? if [ "$rc" = 0 ] && [ -s "$O/ref.out" ]; then pass "generator accepts the same unit once (control)" else fail "generator refused the control unit (exit $rc)"; fi -rc=0; "$GEN" < "$O/units.in" > "$O/ref.out" 2> /dev/null || rc=$? +rc=0; "$GEN" < "$W/units.in" > "$O/ref.out" 2> /dev/null || rc=$? if [ "$rc" = 110 ] && [ ! -s "$O/ref.out" ]; then pass "generator refuses a missing output word"; else fail "generator ran without an output word (exit $rc)"; fi -rc=0; "$GEN" java < "$O/units.in" > "$O/ref.out" 2> /dev/null || rc=$? +rc=0; "$GEN" java < "$W/units.in" > "$O/ref.out" 2> /dev/null || rc=$? if [ "$rc" = 110 ] && [ ! -s "$O/ref.out" ]; then pass "generator refuses an unknown output word"; else fail "generator accepted the word java (exit $rc)"; fi # The other five languages, each built against its generated binding only. if command -v c++ > /dev/null; then @@ -97,57 +113,65 @@ if command -v c++ > /dev/null; then else notrun="$notrun c++"; fi if command -v python3 > /dev/null; then run python python3 -I "$O/caller.py"; else notrun="$notrun python"; fi if command -v rustc > /dev/null; then - bld rust rustc -O --edition 2021 "$O/caller.rs" -L "$O" -C link-arg=-Wl,-rpath,"$O" -o "$O/caller_rs" \ + # The binding declares `unsafe extern "C"` (Rust 1.82 or later), so it reads under edition 2021 and 2024 alike. + bld rust rustc -O --edition 2021 -D warnings "$O/caller.rs" -L "$O" -C link-arg=-Wl,-rpath,"$O" -o "$O/caller_rs" \ && run rust "$O/caller_rs" + bld rust-2024 rustc -O --edition 2024 -D warnings "$O/caller.rs" -L "$O" -C link-arg=-Wl,-rpath,"$O" -o "$O/caller_rs24" \ + && run rust-2024 "$O/caller_rs24" else notrun="$notrun rust"; fi if command -v zig > /dev/null; then bld zig sh -c 'cd "$1" && zig build-exe caller.zig -L. -lomega_native -lc -rpath "$1" -femit-bin=caller_zig' _ "$O" \ && run zig "$O/caller_zig" else notrun="$notrun zig"; fi if command -v go > /dev/null; then - # Module omegacaller: caller.go (package main) and omeganative/ holding the generated package and the library. - mkdir -p "$O/go/omeganative"; cp "$O/omega_native.go" "$O/libomega_native.so" "$O/go/omeganative/"; cp "$O/caller.go" "$O/go/" - bld go sh -c 'cd "$1" && go mod init omegacaller > /dev/null 2>&1 && go build -o caller_go .' _ "$O/go" \ + # Module omegacaller: caller.go (package main) and omeganative/ holding the generated package, the header + # and the library. The binding bakes in no run-time path; this build names the folder through CGO_LDFLAGS. + mkdir -p "$O/go/omeganative"; cp "$O/omega_native.go" "$O/omega_native.h" "$O/libomega_native.so" "$O/go/omeganative/"; cp "$O/caller.go" "$O/go/" + bld go sh -c 'cd "$1" && go mod init omegacaller > /dev/null 2>&1 && CGO_LDFLAGS="-Wl,-rpath,$1/omeganative" go build -o caller_go .' _ "$O/go" \ && run go "$O/go/caller_go" else notrun="$notrun go"; fi # Negative control 1: madd's add turned into sub in the embedded code must be caught by the C caller. frame() { cat "$T/$1.om"; printf '\0'; printf '%08d' "$(wc -c < "$2/$1.bin")"; cat "$2/$1.bin"; } -cp "$O/smadd.bin" "$O/fill.bin" "$O/spin.bin" "$O/mix.bin" "$O/big.bin" "$O/mut/" +cp "$W/smadd.bin" "$W/fill.bin" "$W/spin.bin" "$W/mix.bin" "$W/big.bin" "$O/mut/" printf '\003\174\001\233\140\000\002\313\300\003\137\326' > "$O/mut/madd.bin" for u in $U; do frame $u "$O/mut"; done | "$GEN" c > "$O/mut/omega_native.c" cc -std=gnu11 -O2 -fPIC -shared -Wl,-soname,libomega_native.so -include "$O/omega_native.h" -o "$O/mut/libomega_native.so" "$O/mut/omega_native.c" 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 -# Negative control 2: one wrong scalar type in each generated binding (a one-byte change to the -# generator's text) must be caught by that language's build or run. The real library is used. +# Negative control 2: one wrong scalar type in each generated binding (one word changed on the generator's +# text line) must be caught by that language's build or run, for the stated reason. The real library is used. gmut() { - name=$1; ex=$2; w=$3; ext=$4; shift 4; M=$O/mut/$w; mkdir -p "$M" + name=$1; ex=$2; w=$3; ext=$4; pat=$5; shift 5; M=$O/mut/$w; mkdir -p "$M" sed "$ex" "$ROOT/src/native.om" > "$M/native.om" - if cmp -s "$M/native.om" "$ROOT/src/native.om"; then fail "mutant $name did not change the generator"; return 0; fi + n=$(diff "$ROOT/src/native.om" "$M/native.om" | grep -c '^<' || true) + if [ "$n" != 1 ]; then fail "mutant $name changed $n generator lines, not 1"; return 0; fi if ! "$OMC" < "$M/native.om" > "$M/gen" 2> /dev/null; then fail "mutant $name: generator did not build"; return 0; fi chmod +x "$M/gen" - if ! "$M/gen" "$w" < "$O/units.in" > "$M/omega_native.$ext" 2> /dev/null; then fail "mutant $name: generator refused"; return 0; fi + if ! "$M/gen" "$w" < "$W/units.in" > "$M/omega_native.$ext" 2> /dev/null; then fail "mutant $name: generator refused"; return 0; fi if cmp -s "$M/omega_native.$ext" "$O/omega_native.$ext"; then fail "mutant $name: binding unchanged"; return 0; fi - if "$@" > "$M/check.log" 2>&1; then fail "mutant $name not caught"; else pass "mutant $name caught"; fi + if "$@" > "$M/check.log" 2>&1; then fail "mutant $name not caught" + elif grep -q -E "$pat" "$M/check.log"; then + pass "mutant $name caught: $(grep -m1 -E "$pat" "$M/check.log" | sed 's/^[[:space:]]*//' | cut -c1-80)" + else fail "mutant $name failed for another reason (expected /$pat/)"; cat "$M/check.log"; fi } -gmut "h scalar uint64_t -> uint32_t" 's/, uint64_t o_~/, uint32_t o_~/' h h \ +gmut "h scalar uint64_t -> uint32_t" '/^text tb /s/, uint64_t o_~/, uint32_t o_~/' h h 'conflicting types for' \ cc -std=gnu11 -O2 -fPIC -shared -include "$O/mut/h/omega_native.h" -o "$O/mut/h/lib.so" "$O/omega_native.c" if command -v python3 > /dev/null; then - gmut "python scalar c_uint64 -> c_uint32" 's/_u64 = ctypes.c_uint64/_u64 = ctypes.c_uint32/' python py \ + gmut "python scalar c_uint64 -> c_uint32" '/^text tb /s/return ctypes.c_uint64(v)/return ctypes.c_uint32(v)/' python py '^FAIL madd' \ sh -c 'cp "$1/caller.py" "$1/libomega_native.so" "$1/mut/python/" && python3 -I "$1/mut/python/caller.py"' _ "$O" fi if command -v rustc > /dev/null; then - gmut "rust scalar u64 -> u32" 's/: u64~: \*mut u64/: u32~: *mut u64/' rust rs \ + gmut "rust scalar u64 -> u32" '/^text tb /s/: u64~: \*mut u64/: u32~: *mut u64/' rust rs 'mismatched types' \ sh -c 'cp "$1/caller.rs" "$1/mut/rust/" && rustc -O --edition 2021 "$1/mut/rust/caller.rs" -L "$1" -C link-arg=-Wl,-rpath,"$1" -o "$1/mut/rust/caller_rs" && "$1/mut/rust/caller_rs"' _ "$O" fi if command -v zig > /dev/null; then - gmut "zig scalar u64 -> u32" 's/: u64~: ?\[\*\]u64/: u32~: ?[*]u64/' zig zig \ + gmut "zig scalar u64 -> u32" '/^text tb /s/: u64~: ?\[\*\]u64/: u32~: ?[*]u64/' zig zig 'cannot represent integer value|expected type' \ sh -c 'cp "$1/caller.zig" "$1/mut/zig/" && cd "$1/mut/zig" && zig build-exe caller.zig -L "$1" -lomega_native -lc -rpath "$1" -femit-bin=caller_zig && ./caller_zig' _ "$O" fi if command -v go > /dev/null; then - gmut "go scalar uint64 -> uint32" 's/ uint64~ unsafe.Pointer/ uint32~ unsafe.Pointer/' go go \ - sh -c 'mkdir -p "$1/mut/go/omeganative" && mv "$1/mut/go/omega_native.go" "$1/mut/go/omeganative/" && cp "$1/libomega_native.so" "$1/mut/go/omeganative/" && cp "$1/caller.go" "$1/mut/go/" && cd "$1/mut/go" && go mod init omegacaller && go build -o caller_go . && ./caller_go' _ "$O" + gmut "go scalar uint64 -> uint32" '/^text tb /s/ uint64~ unsafe.Pointer/ uint32~ unsafe.Pointer/' go go 'cannot use' \ + sh -c 'mkdir -p "$1/mut/go/omeganative" && mv "$1/mut/go/omega_native.go" "$1/mut/go/omeganative/" && cp "$1/omega_native.h" "$1/libomega_native.so" "$1/mut/go/omeganative/" && cp "$1/caller.go" "$1/mut/go/" && cd "$1/mut/go" && go mod init omegacaller && CGO_LDFLAGS="-Wl,-rpath,$1/mut/go/omeganative" go build -o caller_go . && ./caller_go' _ "$O" fi for l in $notrun; do echo "NOT_RUN $l caller: toolchain not installed"; done if [ "$fails" -ne 0 ]; then echo "NATIVE-CALL FAIL $fails"; exit 1; fi diff --git a/tools/native b/tools/native index f07cc88..1b46e2c 100755 --- a/tools/native +++ b/tools/native @@ -6,13 +6,18 @@ # uint32_t omega_(uint64_t *result, ...) # omega_native.h its C and C++ declarations # omega_native.py ctypes bindings (import it from beside libomega_native.so) -# omega_native.rs a Rust module (mod omega_native;) -# omega_native.zig a Zig file (@import("omega_native.zig")) -# omega_native.go a cgo package omeganative (a folder of that name holding it and the library) +# omega_native.rs a Rust module (mod omega_native;), Rust 1.82 or later +# omega_native.zig a Zig 0.13 file (@import("omega_native.zig")) +# omega_native.go a cgo package omeganative: a folder of that name holding it, omega_native.h +# and the library # libomega_native.so the library, built with omega_native.h included, so a declaration that # disagrees with its definition fails this build -# all written by one generator, src/native.om, from the units' signatures. A unit outside the subset -# is refused (exit 110) and no binding is written. Extra C flags: OMEGA_NATIVE_CFLAGS. +# work/ the generator (native-gen), each unit's code (.bin) and the framed +# generator input (units.in); tests/native/run.sh reads them. Not an interface. +# Everything is produced in a scratch folder and installed together after the library compiles. A +# refusal (a unit outside the subset, or a name the binding cannot carry) names the unit, exits 110 +# and installs nothing; a previous run's outputs are replaced only on success. Extra C flags: +# OMEGA_NATIVE_CFLAGS. set -eu PIN=734f0a39319fbfa930bcc62cb30dbf06d47d78e126372b74f5baf478e6126b61 ROOT=$(cd "$(dirname "$0")/.." && pwd) @@ -23,20 +28,36 @@ got=$(sha256sum < "$OMC" | cut -d' ' -f1) OUT=$1; shift mkdir -p "$OUT" OUT=$(cd "$OUT" && pwd) -"$OMC" < "$ROOT/src/native.om" > "$OUT/native-gen"; chmod +x "$OUT/native-gen" +W=$OUT/.native-work.$$ +rm -rf "$W"; mkdir -p "$W/work" +trap 'rm -rf "$W"' EXIT +"$OMC" < "$ROOT/src/native.om" > "$W/work/native-gen"; chmod +x "$W/work/native-gen" # The generator's input: each unit's source, a 0 byte, the code length in 8 decimal digits, the code. -: > "$OUT/units.in" +# Each unit is tried alone first, so a refusal names the unit. +: > "$W/work/units.in" for u in "$@"; do b=$(basename "$u" .om) - "$OMC" < "$u" > "$OUT/$b.bin" - { cat "$u"; printf '\0'; printf '%08d' "$(wc -c < "$OUT/$b.bin")"; cat "$OUT/$b.bin"; } >> "$OUT/units.in" + "$OMC" < "$u" > "$W/work/$b.bin" + { cat "$u"; printf '\0'; printf '%08d' "$(wc -c < "$W/work/$b.bin")"; cat "$W/work/$b.bin"; } > "$W/one.in" + if ! "$W/work/native-gen" c < "$W/one.in" > /dev/null 2>&1; then + echo "tools/native: refused $u: outside the native call subset, or a name the binding cannot carry (docs/NATIVE-ABI.md); nothing installed" >&2 + exit 110 + fi + cat "$W/one.in" >> "$W/work/units.in" done -# Every output is generated before any is installed, so a refusal leaves OUTDIR without a half set. -for w in c h python rust zig go; do "$OUT/native-gen" $w < "$OUT/units.in" > "$OUT/.$w.new"; done -mv "$OUT/.c.new" "$OUT/omega_native.c"; mv "$OUT/.h.new" "$OUT/omega_native.h" -mv "$OUT/.python.new" "$OUT/omega_native.py"; mv "$OUT/.rust.new" "$OUT/omega_native.rs" -mv "$OUT/.zig.new" "$OUT/omega_native.zig"; mv "$OUT/.go.new" "$OUT/omega_native.go" +for w in c h python rust zig go; do + if ! "$W/work/native-gen" $w < "$W/work/units.in" > "$W/$w.out" 2> /dev/null; then + echo "tools/native: refused the set of units (a unit name repeats); nothing installed" >&2 + exit 110 + fi +done +mv "$W/c.out" "$W/omega_native.c"; mv "$W/h.out" "$W/omega_native.h"; mv "$W/python.out" "$W/omega_native.py" +mv "$W/rust.out" "$W/omega_native.rs"; mv "$W/zig.out" "$W/omega_native.zig"; mv "$W/go.out" "$W/omega_native.go" # The library names itself (soname), so every caller records libomega_native.so and finds it via rpath. ${CC:-cc} -std=gnu11 -O2 -Wall -Wextra -Werror -fPIC -shared -Wl,-soname,libomega_native.so \ - -include "$OUT/omega_native.h" ${OMEGA_NATIVE_CFLAGS:-} -o "$OUT/libomega_native.so" "$OUT/omega_native.c" -echo "tools/native: $# unit(s) -> $OUT/libomega_native.so + omega_native.{c,h,py,rs,zig,go}" + -include "$W/omega_native.h" ${OMEGA_NATIVE_CFLAGS:-} -o "$W/libomega_native.so" "$W/omega_native.c" +# Install together: the previous run's outputs go, the new set comes in. +for f in omega_native.c omega_native.h omega_native.py omega_native.rs omega_native.zig omega_native.go libomega_native.so work; do + rm -rf "$OUT/$f"; mv "$W/$f" "$OUT/$f" +done +echo "tools/native: $# unit(s) -> $OUT/libomega_native.so + omega_native.{c,h,py,rs,zig,go} (+ work/)" From 247bcb154c9fca0451de47124402a858d07d20f3 Mon Sep 17 00:00:00 2001 From: Drake Stapleton Date: Sat, 24 Oct 2026 18:52:14 -0500 Subject: [PATCH 4/4] native call v1: evidence for 350e82b (review fixes; 25 checks PASS, nothing NOT_RUN) tests/native/run.sh at 350e82b with the pinned omc 734f0a39: NATIVE-CALL PASS (not run: none). Logs have the repository path replaced by . New since d18fa91: rust-2024.log (edition 2024 build), refused.log (tools/native naming the refused unit), the mutant logs show the stated reasons. Co-authored-by: Drake Stapleton Co-Authored-By: Claude Fable 5.1 --- evidence/native-bindings/cpp.log | 3 ++ evidence/native-bindings/go.log | 3 ++ evidence/native-bindings/identity.txt | 14 ++++++---- evidence/native-bindings/mutant-go.log | 1 + evidence/native-bindings/mutant-h.log | 24 ++++++++-------- evidence/native-bindings/mutant-python.log | 11 +++++++- evidence/native-bindings/mutant-rust.log | 32 +++++++++++++++++----- evidence/native-bindings/omega_native.go | 21 ++++---------- evidence/native-bindings/omega_native.py | 22 +++++++++++---- evidence/native-bindings/omega_native.rs | 4 +-- evidence/native-bindings/python.log | 9 ++++++ evidence/native-bindings/refused.log | 1 + evidence/native-bindings/run.log | 25 +++++++++-------- evidence/native-bindings/rust-2024.log | 14 ++++++++++ evidence/native-bindings/rust.log | 3 ++ evidence/native-bindings/tools-native.log | 2 +- evidence/native-bindings/zig.log | 3 ++ 17 files changed, 131 insertions(+), 61 deletions(-) create mode 100644 evidence/native-bindings/refused.log create mode 100644 evidence/native-bindings/rust-2024.log diff --git a/evidence/native-bindings/cpp.log b/evidence/native-bindings/cpp.log index a11ebc5..d46b46f 100644 --- a/evidence/native-bindings/cpp.log +++ b/evidence/native-bindings/cpp.log @@ -7,4 +7,7 @@ PASS fill k=1 traps 3, result unchanged PASS writes before the trap stay visible PASS fill k=0 = 3 PASS wrong length refused 32 +PASS mix six parameters, void result writes 0 +PASS mix negative long traps 9 after both writes, result unchanged +PASS mix uchar conversion 325 traps 5, first write visible PASS 0 failure(s) diff --git a/evidence/native-bindings/go.log b/evidence/native-bindings/go.log index 80e6776..42de241 100644 --- a/evidence/native-bindings/go.log +++ b/evidence/native-bindings/go.log @@ -7,4 +7,7 @@ PASS fill k=1 traps 3, result unchanged PASS writes before the trap stay visible PASS fill k=0 = 3 PASS null result refused 32 +PASS mix six parameters, void result writes 0 +PASS mix negative long traps 9 after both writes, result unchanged +PASS mix uchar conversion 325 traps 5, first write visible PASS 0 failure(s) diff --git a/evidence/native-bindings/identity.txt b/evidence/native-bindings/identity.txt index a68cfb0..e2c2c7b 100644 --- a/evidence/native-bindings/identity.txt +++ b/evidence/native-bindings/identity.txt @@ -1,14 +1,15 @@ -date 2026-10-24T23:25:56Z -source commit 8e1f3cf19988b754489262b4b399d65861454ad1 (branch feat/native-bindings, on feat/native-call-v1 9d5f768) +date 2026-10-24T23:52:14Z +source commit 350e82bf70c7120be3e429141d28ae23eb6af381 (branch feat/native-bindings, on feat/native-call-v1 9d5f768) omc sha256 734f0a39319fbfa930bcc62cb30dbf06d47d78e126372b74f5baf478e6126b61 -generator src/native.om sha256 cbe58616329b1f07f374927867b7a0a94fc1794c4abe4ebb5d2ee8280b202524 +generator src/native.om sha256 e1d0c562b691e5930567d16a2688ed910b476c741d9cca73403d1b7f6565c066 +tools/native sha256 aa0c978c92cb7d5be2743f6be6097c6ec2455c16026a7141c83b3c97b1460534 embedded code (six units) sha256 38c294080111990298ac3ce881986d1ba1869b2966b498c36f40485c226c3ee9 omega_native.c sha256 ce64c4b50d5d54904398d657a1977abe2fe6bad6497503ad55668d247ee77fef omega_native.h sha256 fe3a520a5fdd62897ff4f00d0e3c8a78a33ba79e93cbe42f93b7819692d430f0 -omega_native.py sha256 155f8bef31051027d5bd911a8583d6fcd1dc91e8af9af5175cb4088560ecdc04 -omega_native.rs sha256 291de48c14b0f3a332fb0d717c477f193e6f707ab1d203b59c9eaa4eae7c0131 +omega_native.py sha256 b87c4c0509cae5158116baf99ebe4d09f1495f2414fd9e2083a7646f7c9cc4e8 +omega_native.rs sha256 01ba5d3953356ffa968524c0eb096b69ac4b2161c2fdd5ce93e72ea6468d74b5 omega_native.zig sha256 196447e328340385b814d0a787dbd871726465a8e33e88aabdf184a55103ca09 -omega_native.go sha256 50be29a9b907ab52aa37af075e7ad107580a0ed0602ee2ce37264136e5d22f2d +omega_native.go sha256 ee1ae689e82f99b1738736c074b5815d894f964a11cebdff3168741cd9c3284b cc cc (Ubuntu 13.3.0-6ubuntu2~24.04.1) 13.3.0 c++ c++ (Ubuntu 13.3.0-6ubuntu2~24.04.1) 13.3.0 python3 Python 3.12.3 @@ -16,4 +17,5 @@ rustc rustc 1.98.1 (48a229cea 2026-09-01) zig 0.13.0 (archive sha256 041ac42323837eb5624068acd8b00cd5777dac4cf91179e8dad7a7e90dd0c556) go go version go1.27.3 linux/arm64 (archive sha256 a06e99309dbcbdca1a8ccd2282c3ee146e55930886e26b4f1193269587165f49) machine Linux 7.0.0-1019-nvidia aarch64 +checks 25 PASS, 0 FAIL, 0 NOT_RUN verdict NATIVE-CALL PASS (not run: none) diff --git a/evidence/native-bindings/mutant-go.log b/evidence/native-bindings/mutant-go.log index c60989e..774a599 100644 --- a/evidence/native-bindings/mutant-go.log +++ b/evidence/native-bindings/mutant-go.log @@ -8,3 +8,4 @@ go: to add module requirements and sums: ./caller.go:30:83: cannot use 1085102592571150095 (untyped int constant) as uint32 value in argument to on.Omega_madd (overflows) ./caller.go:32:52: cannot use uint64(m3) (value of type uint64) as uint32 value in argument to on.Omega_madd ./caller.go:34:71: cannot use 1 << 62 (untyped int constant 4611686018427387904) as uint32 value in argument to on.Omega_smadd (overflows) +./caller.go:52:23: cannot use uint64(m1) (value of type uint64) as uint32 value in argument to on.Omega_mix diff --git a/evidence/native-bindings/mutant-h.log b/evidence/native-bindings/mutant-h.log index a560a61..95d2d2e 100644 --- a/evidence/native-bindings/mutant-h.log +++ b/evidence/native-bindings/mutant-h.log @@ -1,37 +1,37 @@ -/home/drakestapleton/workspace/bridge-native-call/build/native/omega_native.c:118:49: error: conflicting types for ‘omega_madd’; have ‘uint32_t(uint64_t *, uint64_t, uint64_t, uint64_t)’ {aka ‘unsigned int(long unsigned int *, long unsigned int, long unsigned int, long unsigned int)’} +/build/native/omega_native.c:118:49: error: conflicting types for ‘omega_madd’; have ‘uint32_t(uint64_t *, uint64_t, uint64_t, uint64_t)’ {aka ‘unsigned int(long unsigned int *, long unsigned int, long unsigned int, long unsigned int)’} 118 | __attribute__((visibility("default"))) uint32_t omega_madd(uint64_t *result, uint64_t o_a, uint64_t o_b, uint64_t o_c) | ^~~~~~~~~~ In file included from : -/home/drakestapleton/workspace/bridge-native-call/build/native/mut/h/omega_native.h:19:10: note: previous declaration of ‘omega_madd’ with type ‘uint32_t(uint64_t *, uint32_t, uint32_t, uint32_t)’ {aka ‘unsigned int(long unsigned int *, unsigned int, unsigned int, unsigned int)’} +/build/native/mut/h/omega_native.h:19:10: note: previous declaration of ‘omega_madd’ with type ‘uint32_t(uint64_t *, uint32_t, uint32_t, uint32_t)’ {aka ‘unsigned int(long unsigned int *, unsigned int, unsigned int, unsigned int)’} 19 | uint32_t omega_madd(uint64_t *result, uint32_t o_a, uint32_t o_b, uint32_t o_c); | ^~~~~~~~~~ -/home/drakestapleton/workspace/bridge-native-call/build/native/omega_native.c:134:49: error: conflicting types for ‘omega_smadd’; have ‘uint32_t(uint64_t *, uint64_t, uint64_t, uint64_t)’ {aka ‘unsigned int(long unsigned int *, long unsigned int, long unsigned int, long unsigned int)’} +/build/native/omega_native.c:134:49: error: conflicting types for ‘omega_smadd’; have ‘uint32_t(uint64_t *, uint64_t, uint64_t, uint64_t)’ {aka ‘unsigned int(long unsigned int *, long unsigned int, long unsigned int, long unsigned int)’} 134 | __attribute__((visibility("default"))) uint32_t omega_smadd(uint64_t *result, uint64_t o_a, uint64_t o_b, uint64_t o_c) | ^~~~~~~~~~~ -/home/drakestapleton/workspace/bridge-native-call/build/native/mut/h/omega_native.h:21:10: note: previous declaration of ‘omega_smadd’ with type ‘uint32_t(uint64_t *, uint32_t, uint32_t, uint32_t)’ {aka ‘unsigned int(long unsigned int *, unsigned int, unsigned int, unsigned int)’} +/build/native/mut/h/omega_native.h:21:10: note: previous declaration of ‘omega_smadd’ with type ‘uint32_t(uint64_t *, uint32_t, uint32_t, uint32_t)’ {aka ‘unsigned int(long unsigned int *, unsigned int, unsigned int, unsigned int)’} 21 | uint32_t omega_smadd(uint64_t *result, uint32_t o_a, uint32_t o_b, uint32_t o_c); | ^~~~~~~~~~~ -/home/drakestapleton/workspace/bridge-native-call/build/native/omega_native.c:150:49: error: conflicting types for ‘omega_fill’; have ‘uint32_t(uint64_t *, void *, uint64_t, uint64_t)’ {aka ‘unsigned int(long unsigned int *, void *, long unsigned int, long unsigned int)’} +/build/native/omega_native.c:150:49: error: conflicting types for ‘omega_fill’; have ‘uint32_t(uint64_t *, void *, uint64_t, uint64_t)’ {aka ‘unsigned int(long unsigned int *, void *, long unsigned int, long unsigned int)’} 150 | __attribute__((visibility("default"))) uint32_t omega_fill(uint64_t *result, void *o_buf, uint64_t o_buf_len, uint64_t o_k) | ^~~~~~~~~~ -/home/drakestapleton/workspace/bridge-native-call/build/native/mut/h/omega_native.h:23:10: note: previous declaration of ‘omega_fill’ with type ‘uint32_t(uint64_t *, void *, uint32_t, uint32_t)’ {aka ‘unsigned int(long unsigned int *, void *, unsigned int, unsigned int)’} +/build/native/mut/h/omega_native.h:23:10: note: previous declaration of ‘omega_fill’ with type ‘uint32_t(uint64_t *, void *, uint32_t, uint32_t)’ {aka ‘unsigned int(long unsigned int *, void *, unsigned int, unsigned int)’} 23 | uint32_t omega_fill(uint64_t *result, void *o_buf, uint32_t o_buf_len, uint32_t o_k); | ^~~~~~~~~~ -/home/drakestapleton/workspace/bridge-native-call/build/native/omega_native.c:167:49: error: conflicting types for ‘omega_spin’; have ‘uint32_t(uint64_t *, uint64_t)’ {aka ‘unsigned int(long unsigned int *, long unsigned int)’} +/build/native/omega_native.c:167:49: error: conflicting types for ‘omega_spin’; have ‘uint32_t(uint64_t *, uint64_t)’ {aka ‘unsigned int(long unsigned int *, long unsigned int)’} 167 | __attribute__((visibility("default"))) uint32_t omega_spin(uint64_t *result, uint64_t o_n) | ^~~~~~~~~~ -/home/drakestapleton/workspace/bridge-native-call/build/native/mut/h/omega_native.h:25:10: note: previous declaration of ‘omega_spin’ with type ‘uint32_t(uint64_t *, uint32_t)’ {aka ‘unsigned int(long unsigned int *, unsigned int)’} +/build/native/mut/h/omega_native.h:25:10: note: previous declaration of ‘omega_spin’ with type ‘uint32_t(uint64_t *, uint32_t)’ {aka ‘unsigned int(long unsigned int *, unsigned int)’} 25 | uint32_t omega_spin(uint64_t *result, uint32_t o_n); | ^~~~~~~~~~ -/home/drakestapleton/workspace/bridge-native-call/build/native/omega_native.c:181:49: error: conflicting types for ‘omega_mix’; have ‘uint32_t(uint64_t *, uint64_t, uint64_t, const void *, uint64_t, void *, uint64_t, const void *, uint64_t, uint64_t)’ {aka ‘unsigned int(long unsigned int *, long unsigned int, long unsigned int, const void *, long unsigned int, void *, long unsigned int, const void *, long unsigned int, long unsigned int)’} +/build/native/omega_native.c:181:49: error: conflicting types for ‘omega_mix’; have ‘uint32_t(uint64_t *, uint64_t, uint64_t, const void *, uint64_t, void *, uint64_t, const void *, uint64_t, uint64_t)’ {aka ‘unsigned int(long unsigned int *, long unsigned int, long unsigned int, const void *, long unsigned int, void *, long unsigned int, const void *, long unsigned int, long unsigned int)’} 181 | __attribute__((visibility("default"))) uint32_t omega_mix(uint64_t *result, uint64_t o_a, uint64_t o_b, const void *o_c, uint64_t o_c_len, void *o_d, uint64_t o_d_len, const void *o_e, uint64_t o_e_len, uint64_t o_f) | ^~~~~~~~~ -/home/drakestapleton/workspace/bridge-native-call/build/native/mut/h/omega_native.h:27:10: note: previous declaration of ‘omega_mix’ with type ‘uint32_t(uint64_t *, uint32_t, uint32_t, const void *, uint32_t, void *, uint32_t, const void *, uint32_t, uint32_t)’ {aka ‘unsigned int(long unsigned int *, unsigned int, unsigned int, const void *, unsigned int, void *, unsigned int, const void *, unsigned int, unsigned int)’} +/build/native/mut/h/omega_native.h:27:10: note: previous declaration of ‘omega_mix’ with type ‘uint32_t(uint64_t *, uint32_t, uint32_t, const void *, uint32_t, void *, uint32_t, const void *, uint32_t, uint32_t)’ {aka ‘unsigned int(long unsigned int *, unsigned int, unsigned int, const void *, unsigned int, void *, unsigned int, const void *, unsigned int, unsigned int)’} 27 | uint32_t omega_mix(uint64_t *result, uint32_t o_a, uint32_t o_b, const void *o_c, uint32_t o_c_len, void *o_d, uint32_t o_d_len, const void *o_e, uint32_t o_e_len, uint32_t o_f); | ^~~~~~~~~ -/home/drakestapleton/workspace/bridge-native-call/build/native/omega_native.c:206:49: error: conflicting types for ‘omega_big’; have ‘uint32_t(uint64_t *, uint64_t)’ {aka ‘unsigned int(long unsigned int *, long unsigned int)’} +/build/native/omega_native.c:206:49: error: conflicting types for ‘omega_big’; have ‘uint32_t(uint64_t *, uint64_t)’ {aka ‘unsigned int(long unsigned int *, long unsigned int)’} 206 | __attribute__((visibility("default"))) uint32_t omega_big(uint64_t *result, uint64_t o_n) | ^~~~~~~~~ -/home/drakestapleton/workspace/bridge-native-call/build/native/mut/h/omega_native.h:29:10: note: previous declaration of ‘omega_big’ with type ‘uint32_t(uint64_t *, uint32_t)’ {aka ‘unsigned int(long unsigned int *, unsigned int)’} +/build/native/mut/h/omega_native.h:29:10: note: previous declaration of ‘omega_big’ with type ‘uint32_t(uint64_t *, uint32_t)’ {aka ‘unsigned int(long unsigned int *, unsigned int)’} 29 | uint32_t omega_big(uint64_t *result, uint32_t o_n); | ^~~~~~~~~ diff --git a/evidence/native-bindings/mutant-python.log b/evidence/native-bindings/mutant-python.log index 135614b..45993f5 100644 --- a/evidence/native-bindings/mutant-python.log +++ b/evidence/native-bindings/mutant-python.log @@ -7,4 +7,13 @@ FAIL fill k=1 traps 3, result unchanged PASS writes before the trap stay visible PASS fill k=0 = 3 PASS null result refused 32 -FAIL 5 failure(s) +PASS mix six parameters, void result writes 0 +FAIL mix negative long traps 9 after both writes, result unchanged +FAIL mix uchar conversion 325 traps 5, first write visible +PASS scalar 2**64+3 refused, not truncated +PASS scalar -1 refused (pass its bits) +PASS scalar 2.0 refused +PASS bytes for an edit uchar array refused +PASS bytes for an edit ulong array refused +PASS call after refusals still works +FAIL 7 failure(s) diff --git a/evidence/native-bindings/mutant-rust.log b/evidence/native-bindings/mutant-rust.log index 07bed59..8b4625b 100644 --- a/evidence/native-bindings/mutant-rust.log +++ b/evidence/native-bindings/mutant-rust.log @@ -1,5 +1,5 @@ error[E0308]: mismatched types - --> /home/drakestapleton/workspace/bridge-native-call/build/native/mut/rust/caller.rs:17:52 + --> /build/native/mut/rust/caller.rs:17:52 | 17 | check("madd wraps = 3", omega_madd(&mut r, u64::MAX, 2, 5) == 0 && r == 3); | ---------- ^^^^^^^^ expected `u32`, found `u64` @@ -7,7 +7,7 @@ error[E0308]: mismatched types | arguments to this function are incorrect | note: function defined here - --> /home/drakestapleton/workspace/bridge-native-call/build/native/mut/rust/omega_native.rs:20:12 + --> /build/native/mut/rust/omega_native.rs:20:12 | 20 | pub fn omega_madd(result: *mut u64, o_a: u32, o_b: u32, o_c: u32) -> u32; | ^^^^^^^^^^ --- @@ -17,7 +17,7 @@ help: you can convert a `u64` to a `u32` and panic if the converted value doesn' | ++++++++++++++++++++ error[E0308]: mismatched types - --> /home/drakestapleton/workspace/bridge-native-call/build/native/mut/rust/caller.rs:20:60 + --> /build/native/mut/rust/caller.rs:20:60 | 20 | check("madd signed bits = -13", omega_madd(&mut r, (-3i64) as u64, 5, 2) == 0 && r as i64 == -13); | ---------- ^^^^^^^^^^^^^^ expected `u32`, found `u64` @@ -25,7 +25,7 @@ error[E0308]: mismatched types | arguments to this function are incorrect | note: function defined here - --> /home/drakestapleton/workspace/bridge-native-call/build/native/mut/rust/omega_native.rs:20:12 + --> /build/native/mut/rust/omega_native.rs:20:12 | 20 | pub fn omega_madd(result: *mut u64, o_a: u32, o_b: u32, o_c: u32) -> u32; | ^^^^^^^^^^ --- @@ -35,7 +35,7 @@ help: you can convert a `u64` to a `u32` and panic if the converted value doesn' | + +++++++++++++++++++++ error[E0308]: mismatched types - --> /home/drakestapleton/workspace/bridge-native-call/build/native/mut/rust/caller.rs:22:79 + --> /build/native/mut/rust/caller.rs:22:79 | 22 | check("smadd overflow traps 1, result unchanged", omega_smadd(&mut r, 1u64 << 62, 4, 0) == 1 && r == SENTINEL); | ----------- ^^^^^^^^^^ expected `u32`, found `u64` @@ -43,7 +43,7 @@ error[E0308]: mismatched types | arguments to this function are incorrect | note: function defined here - --> /home/drakestapleton/workspace/bridge-native-call/build/native/mut/rust/omega_native.rs:22:12 + --> /build/native/mut/rust/omega_native.rs:22:12 | 22 | pub fn omega_smadd(result: *mut u64, o_a: u32, o_b: u32, o_c: u32) -> u32; | ^^^^^^^^^^^ --- @@ -52,6 +52,24 @@ help: you can convert a `u64` to a `u32` and panic if the converted value doesn' 22 | check("smadd overflow traps 1, result unchanged", omega_smadd(&mut r, (1u64 << 62).try_into().unwrap(), 4, 0) == 1 && r == SENTINEL); | + +++++++++++++++++++++ -error: aborting due to 3 previous errors +error[E0308]: mismatched types + --> /build/native/mut/rust/caller.rs:39:34 + | +39 | omega_mix(&mut r, 5, (-1i64) as u64, cb.as_ptr(), 3, db.as_mut_ptr(), 2, eb.as_ptr(), 2, 7) == 9 + | --------- ^^^^^^^^^^^^^^ expected `u32`, found `u64` + | | + | arguments to this function are incorrect + | +note: function defined here + --> /build/native/mut/rust/omega_native.rs:28:12 + | +28 | pub fn omega_mix(result: *mut u64, o_a: u32, o_b: u32, o_c: *const u8, o_c_len: u64, o_d: *mut u8, o_d_len: u64, o_e: *const u64... + | ^^^^^^^^^ --- +help: you can convert a `u64` to a `u32` and panic if the converted value doesn't fit + | +39 | omega_mix(&mut r, 5, ((-1i64) as u64).try_into().unwrap(), cb.as_ptr(), 3, db.as_mut_ptr(), 2, eb.as_ptr(), 2, 7) == 9 + | + +++++++++++++++++++++ + +error: aborting due to 4 previous errors For more information about this error, try `rustc --explain E0308`. diff --git a/evidence/native-bindings/omega_native.go b/evidence/native-bindings/omega_native.go index d9b2814..1d77b86 100644 --- a/evidence/native-bindings/omega_native.go +++ b/evidence/native-bindings/omega_native.go @@ -1,5 +1,7 @@ // Generated by omega-bridge src/native.om: native call ABI v1 (docs/NATIVE-ABI.md). -// cgo bindings for libomega_native.so beside this file. Regenerate it; do not edit it. +// cgo package omeganative: this file, omega_native.h and libomega_native.so in one folder. Regenerate it; +// do not edit it. No run-time path is baked in: the program's build names the folder (CGO_LDFLAGS set to +// -Wl,-rpath,), or LD_LIBRARY_PATH does. // Status: 0 done; 1..14 the Omega trap code; 32 argument refused (nil, wrong length, misaligned, // overlap); 33 reentry; 34 bad runtime entry; 35 no stack. The result is written only on status 0. // A long is its two's-complement bits (an int64 as uint64); an array is an unsafe.Pointer to memory @@ -7,20 +9,9 @@ package omeganative /* -#cgo LDFLAGS: -L${SRCDIR} -lomega_native -Wl,-rpath,${SRCDIR} -#include -// fn madd a ulong b ulong c ulong to ulong -uint32_t omega_madd(uint64_t *result, uint64_t o_a, uint64_t o_b, uint64_t o_c); -// fn smadd a long b long c long to long -uint32_t omega_smadd(uint64_t *result, uint64_t o_a, uint64_t o_b, uint64_t o_c); -// fn fill buf edit ulong 4 k ulong to ulong -uint32_t omega_fill(uint64_t *result, void *o_buf, uint64_t o_buf_len, uint64_t o_k); -// fn spin n ulong to ulong -uint32_t omega_spin(uint64_t *result, uint64_t o_n); -// fn mix a ulong b long c view uchar 3 d edit uchar 2 e view ulong 2 f ulong to void -uint32_t omega_mix(uint64_t *result, uint64_t o_a, uint64_t o_b, const void *o_c, uint64_t o_c_len, void *o_d, uint64_t o_d_len, const void *o_e, uint64_t o_e_len, uint64_t o_f); -// fn big n ulong to ulong -uint32_t omega_big(uint64_t *result, uint64_t o_n); +#cgo CFLAGS: -I${SRCDIR} +#cgo LDFLAGS: -L${SRCDIR} -lomega_native +#include "omega_native.h" */ import "C" diff --git a/evidence/native-bindings/omega_native.py b/evidence/native-bindings/omega_native.py index 6b811ca..532f2bd 100644 --- a/evidence/native-bindings/omega_native.py +++ b/evidence/native-bindings/omega_native.py @@ -2,14 +2,24 @@ # ctypes declarations for libomega_native.so beside this file. Regenerate it; do not edit it. # Status: 0 done; 1..14 the Omega trap code; 32 argument refused (None, wrong length, misaligned, # overlap); 33 reentry; 34 bad runtime entry; 35 no stack. The result (a ctypes.c_uint64 passed with -# ctypes.byref) is written only on status 0. A long is its two's-complement bits; an array is a -# ctypes array (or any address) then its element count. +# ctypes.byref) is written only on status 0. A scalar is an int in 0 .. 2**64-1 (a long is passed as its +# two's-complement bits); anything else raises ctypes.ArgumentError, nothing is truncated. An array is a +# ctypes array of its element type (bytes for a view uchar) then its element count; an edit array must +# be writable, so bytes are refused there. import ctypes, os OMEGA_OK, OMEGA_BAD_ARG, OMEGA_REENTRY, OMEGA_BAD_RT, OMEGA_NO_STACK = 0, 32, 33, 34, 35 lib = ctypes.CDLL(os.path.join(os.path.dirname(os.path.abspath(__file__)), "libomega_native.so")) -_u64 = ctypes.c_uint64 +class _u64: + @classmethod + def from_param(cls, v): + if type(v) is not int or v < 0 or v > 18446744073709551615: + raise ValueError("omega scalar: an int in 0 .. 2**64-1 expected, got %r" % (v,)) + return ctypes.c_uint64(v) _res = ctypes.POINTER(ctypes.c_uint64) -_arr = ctypes.c_void_p +_a3 = ctypes.POINTER(ctypes.c_uint64) # edit ulong: a writable ctypes array of c_uint64 +_a4 = ctypes.POINTER(ctypes.c_uint64) # view ulong: a ctypes array of c_uint64 +_a5 = ctypes.POINTER(ctypes.c_ubyte) # edit uchar: a writable ctypes array of c_ubyte +_a6 = ctypes.c_char_p # view uchar: bytes, or a ctypes array of c_char # fn madd a ulong b ulong c ulong to ulong omega_madd = lib.omega_madd omega_madd.argtypes = [_res, _u64, _u64, _u64] @@ -20,7 +30,7 @@ omega_smadd.argtypes = [_res, _u64, _u64, _u64] omega_smadd.restype = ctypes.c_uint32 # fn fill buf edit ulong 4 k ulong to ulong omega_fill = lib.omega_fill -omega_fill.argtypes = [_res, _arr, _u64, _u64] +omega_fill.argtypes = [_res, _a3, _u64, _u64] omega_fill.restype = ctypes.c_uint32 # fn spin n ulong to ulong omega_spin = lib.omega_spin @@ -28,7 +38,7 @@ omega_spin.argtypes = [_res, _u64] omega_spin.restype = ctypes.c_uint32 # fn mix a ulong b long c view uchar 3 d edit uchar 2 e view ulong 2 f ulong to void omega_mix = lib.omega_mix -omega_mix.argtypes = [_res, _u64, _u64, _arr, _u64, _arr, _u64, _arr, _u64, _u64] +omega_mix.argtypes = [_res, _u64, _u64, _a6, _u64, _a5, _u64, _a4, _u64, _u64] omega_mix.restype = ctypes.c_uint32 # fn big n ulong to ulong omega_big = lib.omega_big diff --git a/evidence/native-bindings/omega_native.rs b/evidence/native-bindings/omega_native.rs index 0547b8f..a719a88 100644 --- a/evidence/native-bindings/omega_native.rs +++ b/evidence/native-bindings/omega_native.rs @@ -1,5 +1,5 @@ // Generated by omega-bridge src/native.om: native call ABI v1 (docs/NATIVE-ABI.md). -// Declarations for libomega_native (build with -L ). Regenerate it; do not edit it. +// Declarations for libomega_native (build with -L ; Rust 1.82 or later). Regenerate it; do not edit it. // Status: 0 done; 1..14 the Omega trap code; 32 argument refused (null, wrong length, misaligned, // overlap); 33 reentry; 34 bad runtime entry; 35 no stack. The result is written only on status 0. // A long is its two's-complement bits (an i64 as u64); an array is its pointer then its element count. @@ -15,7 +15,7 @@ pub const OMEGA_BAD_RT: u32 = 34; pub const OMEGA_NO_STACK: u32 = 35; #[allow(dead_code)] #[link(name = "omega_native")] -extern "C" { +unsafe extern "C" { // fn madd a ulong b ulong c ulong to ulong pub fn omega_madd(result: *mut u64, o_a: u64, o_b: u64, o_c: u64) -> u32; // fn smadd a long b long c long to long diff --git a/evidence/native-bindings/python.log b/evidence/native-bindings/python.log index 80e6776..6bb56fc 100644 --- a/evidence/native-bindings/python.log +++ b/evidence/native-bindings/python.log @@ -7,4 +7,13 @@ PASS fill k=1 traps 3, result unchanged PASS writes before the trap stay visible PASS fill k=0 = 3 PASS null result refused 32 +PASS mix six parameters, void result writes 0 +PASS mix negative long traps 9 after both writes, result unchanged +PASS mix uchar conversion 325 traps 5, first write visible +PASS scalar 2**64+3 refused, not truncated +PASS scalar -1 refused (pass its bits) +PASS scalar 2.0 refused +PASS bytes for an edit uchar array refused +PASS bytes for an edit ulong array refused +PASS call after refusals still works PASS 0 failure(s) diff --git a/evidence/native-bindings/refused.log b/evidence/native-bindings/refused.log new file mode 100644 index 0000000..4db0fa0 --- /dev/null +++ b/evidence/native-bindings/refused.log @@ -0,0 +1 @@ +tools/native: refused /build/native/bad.om: outside the native call subset, or a name the binding cannot carry (docs/NATIVE-ABI.md); nothing installed diff --git a/evidence/native-bindings/run.log b/evidence/native-bindings/run.log index 6308bcf..797964d 100644 --- a/evidence/native-bindings/run.log +++ b/evidence/native-bindings/run.log @@ -1,5 +1,7 @@ -PASS tools/native: library + six bindings (2beb3d30a6f46b74) +PASS tools/native: library + six bindings (3c426dace42fde5d) +PASS tools/native refuses a unit outside the subset by name (exit 110), previous outputs untouched PASS no caller declares an Omega function by hand +PASS generated header agrees with the hand-written reference prototypes PASS embedded code == omc output, per unit (38c2940801119902) PASS c caller (26 lines) PASS c-bti caller (26 lines) @@ -9,15 +11,16 @@ PASS generator refuses a repeated unit name PASS generator accepts the same unit once (control) PASS generator refuses a missing output word PASS generator refuses an unknown output word -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 cpp caller (13 lines) +PASS python caller (19 lines) +PASS rust caller (14 lines) +PASS rust-2024 caller (14 lines) +PASS zig caller (13 lines) +PASS go caller (13 lines) PASS mutant madd (add -> sub) caught (7 failing checks) -PASS mutant h scalar uint64_t -> uint32_t caught -PASS mutant python scalar c_uint64 -> c_uint32 caught -PASS mutant rust scalar u64 -> u32 caught -PASS mutant zig scalar u64 -> u32 caught -PASS mutant go scalar uint64 -> uint32 caught +PASS mutant h scalar uint64_t -> uint32_t caught: /build/native/omega_native.c:11 +PASS mutant python scalar c_uint64 -> c_uint32 caught: FAIL madd wraps = 3 +PASS mutant rust scalar u64 -> u32 caught: error[E0308]: mismatched types +PASS mutant zig scalar u64 -> u32 caught: caller.zig:17:62: error: type 'u32' cannot represent integer value '184467440737 +PASS mutant go scalar uint64 -> uint32 caught: ./caller.go:29:44: cannot use ^uint64(0) (constant 18446744073709551615 of type NATIVE-CALL PASS (not run: none) diff --git a/evidence/native-bindings/rust-2024.log b/evidence/native-bindings/rust-2024.log new file mode 100644 index 0000000..d40ca82 --- /dev/null +++ b/evidence/native-bindings/rust-2024.log @@ -0,0 +1,14 @@ +PASS madd 3*5+7 = 22 +PASS madd wraps = 3 +PASS madd large +PASS madd signed bits = -13 +PASS smadd overflow traps 1, result unchanged +PASS fill k=1 traps 3, result unchanged +PASS writes before the trap stay visible +PASS fill k=0 = 3 +PASS null result refused 32 +PASS mix six parameters, void result writes 0 +PASS mix negative long traps 9 after both writes, result unchanged +PASS mix uchar conversion 325 traps 5, first write visible +PASS big local array on a default std::thread = 15 +PASS 0 failure(s) diff --git a/evidence/native-bindings/rust.log b/evidence/native-bindings/rust.log index c28138e..d40ca82 100644 --- a/evidence/native-bindings/rust.log +++ b/evidence/native-bindings/rust.log @@ -7,5 +7,8 @@ PASS fill k=1 traps 3, result unchanged PASS writes before the trap stay visible PASS fill k=0 = 3 PASS null result refused 32 +PASS mix six parameters, void result writes 0 +PASS mix negative long traps 9 after both writes, result unchanged +PASS mix uchar conversion 325 traps 5, first write visible PASS big local array on a default std::thread = 15 PASS 0 failure(s) diff --git a/evidence/native-bindings/tools-native.log b/evidence/native-bindings/tools-native.log index fafe8a9..49dd697 100644 --- a/evidence/native-bindings/tools-native.log +++ b/evidence/native-bindings/tools-native.log @@ -1 +1 @@ -tools/native: 6 unit(s) -> /home/drakestapleton/workspace/bridge-native-call/build/native/libomega_native.so + omega_native.{c,h,py,rs,zig,go} +tools/native: 6 unit(s) -> /build/native/libomega_native.so + omega_native.{c,h,py,rs,zig,go} (+ work/) diff --git a/evidence/native-bindings/zig.log b/evidence/native-bindings/zig.log index 80e6776..42de241 100644 --- a/evidence/native-bindings/zig.log +++ b/evidence/native-bindings/zig.log @@ -7,4 +7,7 @@ PASS fill k=1 traps 3, result unchanged PASS writes before the trap stay visible PASS fill k=0 = 3 PASS null result refused 32 +PASS mix six parameters, void result writes 0 +PASS mix negative long traps 9 after both writes, result unchanged +PASS mix uchar conversion 325 traps 5, first write visible PASS 0 failure(s)