Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/omega-mv/patches/6449b620c847983a # omega-mv Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance. Brought by drakestapleton Sign in to star 1 stars 0 watching About Files History Releases Proposed changes Issues For developers ## GitHub aien-dev/omega-mv#435 (code only) closed derived `6449b620c847983a` #435 Imported from GitHub aien-dev/omega-mv#435 (code only) GitHub's metadata for this pull request is unavailable: its state is DERIVED from the commits (merged: the head is reachable from this project's main; open: GitHub kept a merge ref; else closed). Base `116292526e2922c5e3c254d9930d23681534efbd` · Head `773337320893471870d8e20751c483de1f3b9b14` ### Fetch it A git bundle of the pull request's head, 22239 bytes. ``` curl -fsSL https://interplane.io/drakestapleton/omega-mv/patches/6449b620c847983a.bundle -o 6449b620c847983a.bundle git fetch 6449b620c847983a.bundle refs/pull/435/head ``` Raw bundle · Diff ### Provenance This proposal is imported from GitHub; it is not signed by its author. Its import statement and source record: ``` github-import 1 aien-dev/omega-mv 435 773337320893471870d8e20751c483de1f3b9b14 ``` ``` {"kind":"github-import","owner":"aien-dev","repo":"omega-mv","number":435,"url":"https://github.com/aien-dev/omega-mv/pull/435","export_sha256":"967c79e26a893657b5cdaa45efd442be5dbd2ecfe68e25f9a8c1b5da4d60d452","imported_utc":"2026-11-18T16:37:52Z","metadata":"unavailable","state_derived":true} ``` ### Changes ``` diff --git a/.crumb b/.crumb index c5c8930af..ad0ef9642 100644 --- a/.crumb +++ b/.crumb @@ -51,8 +51,8 @@ "purpose": "Directory calibration. Purpose not yet described by a human." }, { - "digest": "cb284f3f45c223e3a3f5e70f7eeafbf5059a6f0b0f65fc2d0549ab8fdc9b60ef", - "last_commit": "9b557902", + "digest": "665c0c470a53a6cc653d0f74b30a63f9998971caaf8acf2e93944b62b0c7ee92", + "last_commit": "52f89512", "name": "docs", "purpose": "Directory docs. Purpose not yet described by a human." }, @@ -63,8 +63,8 @@ "purpose": "Directory doctrine. Purpose not yet described by a human." }, { - "digest": "bb7a612a73a0204d9c380ebc40c84bb2695823835c9e5c9f7afbd1610de13f73", - "last_commit": "95829f2a", + "digest": "f6d1a9bb8d18f58cddfbea2f9ecce90f27f5b5f75b584b3a0f1ce1cea509044c", + "last_commit": "c5268ac0", "name": "mk", "purpose": "Directory mk. Purpose not yet described by a human." }, @@ -87,14 +87,14 @@ "purpose": "Directory spec. Purpose not yet described by a human." }, { - "digest": "4e740ad2caa887e1b91103311126fc0224984aa9e05e9485bee82f34718f5350", - "last_commit": "057414c5", + "digest": "920fbb0917af3267da46e5c70a88374831dd92637b0e7a825672a0dea956dfad", + "last_commit": "8139dbd1", "name": "src", "purpose": "Directory src. Purpose not yet described by a human." }, { - "digest": "036c34e6022ae19b81e105135457d02186fa8597ec00ebf4a150af6c237e0e61", - "last_commit": "25ee84a2", + "digest": "55eb38cd39a0240da6b66975594c053d9e21bcdf2dbd8ebf9760f5dd26313a09", + "last_commit": "c5268ac0", "name": "tests", "purpose": "Directory tests. Purpose not yet described by a human." }, @@ -105,15 +105,15 @@ "purpose": "Directory tools. Purpose not yet described by a human." } ], - "children_root": "sha256:a75ef8844b8c65e5ecf407f4f978597a66e4b208bb332413059f89ce26c0f345", - "compiled_at": "2026-10-13T04:53:34Z", + "children_root": "sha256:245d85deb7a8aba3883e95ce31788789ab3f3e7cb73bbbd0cd8f454aec2ab7bc", + "compiled_at": "2026-10-13T05:08:22Z", "compiler": "crumb-compile/1", - "digest": "sha256:df238d3251d9f411d9daf83a275532a3ba98dfe49d173835281a5dbb04b306a6", + "digest": "sha256:5d6e37fe94d468e51a47d11f22d6b44c98ce510d92895efb369e4756cf2f7b9e", "evidence_files": 7245, "evidence_root": "sha256:035beef5cdb99afaefd1743640f5e7eeaaed13c4a5ec6c8b4b6a4cb0c528407a", - "files": 9748, - "generated_at_commit": "c432ce62a2143198e214075fdfe5803072f62358", - "source_tree": "sha256:83e92f8935da59c24b4d2c38b420b88abc1feb16f10b7be8ca74143a6823a908" + "files": 9751, + "generated_at_commit": "52f8951208b8bcdbcf3280a251fbb1d3fd6200d5", + "source_tree": "sha256:0c91225d9e83123412ca9e4d68f5ce4b2bd91fb379a5347d31f3eb264798dfc9" }, "owner_lane": "", "provenance": { diff --git a/docs/.crumb b/docs/.crumb index 0ad030a24..f165a9ec5 100644 --- a/docs/.crumb +++ b/docs/.crumb @@ -87,8 +87,8 @@ "purpose": "Directory docs/numeric. Purpose not yet described by a human." }, { - "digest": "c80de8d01f2a06b28bc9fdaf5918fceca16f2fe7dda1eb3dee6519b640ee18aa", - "last_commit": "fb13b3bb", + "digest": "39dc2cde3376d909cb4182944f31e87f2148e5bf2d83d6459c91140ce622b8a7", + "last_commit": "c5268ac0", "name": "osc", "purpose": "Directory docs/osc. Purpose not yet described by a human." }, @@ -117,15 +117,15 @@ "purpose": "Directory docs/turing. Purpose not yet described by a human." } ], - "children_root": "sha256:23a7a5496b7b55a1e61db01b1ca8c6256f0ac798ec4cbc6d32c8c62936a84618", - "compiled_at": "2026-10-13T04:36:58Z", + "children_root": "sha256:ea319a0528149bd19ab10f7242d73ab457189ae61c9325ffb11db026993dfa73", + "compiled_at": "2026-10-13T04:58:02Z", "compiler": "crumb-compile/1", - "digest": "sha256:cb284f3f45c223e3a3f5e70f7eeafbf5059a6f0b0f65fc2d0549ab8fdc9b60ef", + "digest": "sha256:665c0c470a53a6cc653d0f74b30a63f9998971caaf8acf2e93944b62b0c7ee92", "evidence_files": 34, "evidence_root": "sha256:589d8b3706e6d7a202ab20b278dac5fc5bafc973f1c2a867b2668aa7210b65cc", "files": 121, - "generated_at_commit": "43d44f84f5df257c4f0d132f3a69f22881be8b0a", - "source_tree": "sha256:7fbfe25009350c5afb1ac4e5937701304ebdce6b2aad5b5ec401e07e429f1b3c" + "generated_at_commit": "8139dbd1d470652ac40a9e70edb37a5321b03dea", + "source_tree": "sha256:e00996aaac44b18404d6a3e518355059a389272687cacfe1ecec455bb06a5675" }, "owner_lane": "", "provenance": { diff --git a/docs/osc/.crumb b/docs/osc/.crumb index 22382d0fe..89dc31d25 100644 --- a/docs/osc/.crumb +++ b/docs/osc/.crumb @@ -20,14 +20,14 @@ } ], "children_root": "sha256:33192ccd879c26130c2aba2fb02d7d8f03b093e64bb571d14403152ecbfeb3ae", - "compiled_at": "2026-10-13T04:21:28Z", + "compiled_at": "2026-10-13T04:58:02Z", "compiler": "crumb-compile/1", - "digest": "sha256:c80de8d01f2a06b28bc9fdaf5918fceca16f2fe7dda1eb3dee6519b640ee18aa", + "digest": "sha256:39dc2cde3376d909cb4182944f31e87f2148e5bf2d83d6459c91140ce622b8a7", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", "files": 14, - "generated_at_commit": "e0ff720120101e252748dbb47b134d4435bb91a6", - "source_tree": "sha256:5a2cd5fcce884da9e8729ec150a77062f5b6e71390e6f2ece6ddc4b621eb1b7e" + "generated_at_commit": "8139dbd1d470652ac40a9e70edb37a5321b03dea", + "source_tree": "sha256:e99b84ebf7e139c5af2b3a43e66b88777220525cb72147e0a0a0e754ad188db4" }, "owner_lane": "", "provenance": { diff --git a/docs/osc/OSC-1-DESIGN.md b/docs/osc/OSC-1-DESIGN.md index e93b500d4..85327403b 100644 --- a/docs/osc/OSC-1-DESIGN.md +++ b/docs/osc/OSC-1-DESIGN.md @@ -253,3 +253,26 @@ self-hosting claim. arithmetic computed exactly in 128 bits and range-checked), not a copy of the codegen's flag logic. It is a differential oracle; agreement between two implementations is evidence, not proof. + +## 11. Explicit allocation + +Every heap byte the compiler takes comes from one `OscAlloc` (`src/compiler/osc_alloc.h`) the +caller passes in: `alloc(ctx, n)`, `realloc(ctx, p, old_n, new_n)`, `free(ctx, p, n)`. Sizes travel +with every call (Zig style), so an allocator needs no per-block header and a test allocator can +check them. Memory is never assumed zeroed; the checker and lowerer clear what they need. + +- Entry points: `osc_compile_a`, `osc_compile_imports_a`, `osc_cg_compile_a`, `osc_ir_validate_a` + take the allocator first (NULL means libc). `osc_compile`, `osc_compile_imports`, + `osc_cg_compile`, `osc_ir_validate` are the same over `osc_alloc_libc()`, so no caller changes. + `OscCode.a` records the allocator that owns `.code`; `osc_cg_free` frees with it (NULL = libc). +- A refusal is an ordinary failure: `OSC_DIAG_CAPACITY` from the front end; `-1` with "out of memory" + in `err` from the back end; `osc_ir_validate_a` returns `-2` with the same text. All scratch is + freed on every failure path. +- Output is a pure function of the source, whatever the allocator. +- `OscAllocMeter` wraps any allocator: it counts calls, tracks live and peak bytes, refuses the k-th + call (`fail_at`) or any call past a byte `budget`, and flags a free or realloc with a wrong size. +- `tests/compiler/test_osc_alloc.c` refuses every allocation call of every golden program in turn + (plus a generated unit that grows the back end buffers), requires a clean failure and zero live + bytes each time, and checks that a budget of the measured peak P succeeds and P-1 fails. +- Not covered: `osc_ir_encode`, `osc_ir_digest` and the interpreter entry validate over libc; the + `oscc` driver keeps its own file buffers on libc. diff --git a/mk/.crumb b/mk/.crumb index 534b33d69..3aac582c5 100644 --- a/mk/.crumb +++ b/mk/.crumb @@ -7,14 +7,14 @@ "generated": { "children": [], "children_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - "compiled_at": "2026-10-12T16:47:44Z", + "compiled_at": "2026-10-13T04:37:52Z", "compiler": "crumb-compile/1", - "digest": "sha256:bb7a612a73a0204d9c380ebc40c84bb2695823835c9e5c9f7afbd1610de13f73", + "digest": "sha256:f6d1a9bb8d18f58cddfbea2f9ecce90f27f5b5f75b584b3a0f1ce1cea509044c", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", "files": 67, - "generated_at_commit": "a073db70e22eec13128c00554683dd8c59b6f456", - "source_tree": "sha256:99298a209933d9311ece77629b6d00f2e022985259a641142229aa65578b8b80" + "generated_at_commit": "cc3f8584d83571c4d53336742af0ebdda254a0ab", + "source_tree": "sha256:702fc6a621e0ca7a96ac452f23cd9102d8ca7c8ff636d8b816d3f60b82138f7a" }, "owner_lane": "", "provenance": { diff --git a/mk/compiler.mk b/mk/compiler.mk index 8b3ae55d6..c78095a6d 100644 --- a/mk/compiler.mk +++ b/mk/compiler.mk @@ -40,6 +40,14 @@ $(OSC_DIR)/test_osc_backend_asan: tests/compiler/test_osc_backend.c $(OSC_LIB) $ @mkdir -p $(OSC_DIR) $(CC) $(OSC_CFLAGS) $(OSC_ASAN) -o $@ tests/compiler/test_osc_backend.c $(OSC_LIB) +$(OSC_DIR)/test_osc_alloc: tests/compiler/test_osc_alloc.c $(OSC_LIB) $(OSC_HDRS) + @mkdir -p $(OSC_DIR) + $(CC) $(OSC_CFLAGS) -o $@ tests/compiler/test_osc_alloc.c $(OSC_LIB) + +$(OSC_DIR)/test_osc_alloc_asan: tests/compiler/test_osc_alloc.c $(OSC_LIB) $(OSC_HDRS) + @mkdir -p $(OSC_DIR) + $(CC) $(OSC_CFLAGS) $(OSC_ASAN) -o $@ tests/compiler/test_osc_alloc.c $(OSC_LIB) + $(OSC_DIR)/test_osc_compiler: tests/compiler/test_osc_compiler.c $(OSC_LIB) $(OSC_MODEL) $(OSC_HDRS) @mkdir -p $(OSC_DIR) $(CC) $(OSC_CFLAGS) -o $@ tests/compiler/test_osc_compiler.c $(OSC_LIB) $(OSC_MODEL) @@ -71,6 +79,7 @@ $(OSC_DIR)/test_legacy_a64_asan: tests/compiler/test_legacy_a64.c $(OSC_LEGACY_A # the 3-minute single-core budget; the plain legs run the full counts. test-compiler: $(OSC_DIR)/test_osc_model $(OSC_DIR)/test_osc_model_asan \ $(OSC_DIR)/test_osc_backend $(OSC_DIR)/test_osc_backend_asan \ + $(OSC_DIR)/test_osc_alloc $(OSC_DIR)/test_osc_alloc_asan \ $(OSC_DIR)/test_osc_compiler $(OSC_DIR)/test_osc_compiler_asan $(OSC_DIR)/oscc \ $(OSC_DIR)/test_legacy_a64 $(OSC_DIR)/test_legacy_a64_asan $(abspath $(OSC_DIR)/test_osc_model) > $(OSC_DIR)/model.out @@ -81,6 +90,12 @@ test-compiler: $(OSC_DIR)/test_osc_model $(OSC_DIR)/test_osc_model_asan \ @tail -1 $(OSC_DIR)/backend.out; grep -q '^OSC1_BACKEND_PASS$$' $(OSC_DIR)/backend.out $(abspath $(OSC_DIR)/test_osc_backend_asan) > $(OSC_DIR)/backend_asan.out @tail -1 $(OSC_DIR)/backend_asan.out; grep -q '^OSC1_BACKEND_PASS$$' $(OSC_DIR)/backend_asan.out + $(abspath $(OSC_DIR)/test_osc_alloc) > $(OSC_DIR)/alloc.out + @tail -1 $(OSC_DIR)/alloc.out; grep -Eq '^PASS [0-9]+ [0-9]+$$' $(OSC_DIR)/alloc.out + @grep "^alloc sweep: " $(OSC_DIR)/alloc.out + $(abspath $(OSC_DIR)/test_osc_alloc_asan) > $(OSC_DIR)/alloc_asan.out + @tail -1 $(OSC_DIR)/alloc_asan.out; grep -Eq '^PASS [0-9]+ [0-9]+$$' $(OSC_DIR)/alloc_asan.out + @echo "test-osc-alloc: PASS" $(abspath $(OSC_DIR)/test_osc_compiler) > $(OSC_DIR)/compiler.out @tail -1 $(OSC_DIR)/compiler.out; grep -Eq '^OSC1_COMPILER_PASS( |$$)' $(OSC_DIR)/compiler.out $(abspath $(OSC_DIR)/test_osc_compiler_asan) $(OSC_COMPILER_ASAN_ARGS) > $(OSC_DIR)/compiler_asan.out @@ -118,6 +133,7 @@ OSC_QUICK_ASAN_FUZZ ?= 120 test-compiler-full: test-compiler test-compiler-quick: $(OSC_DIR)/test_osc_model $(OSC_DIR)/test_osc_backend $(OSC_DIR)/test_osc_backend_asan \ + $(OSC_DIR)/test_osc_alloc $(OSC_DIR)/test_osc_alloc_asan \ $(OSC_DIR)/test_osc_compiler $(OSC_DIR)/test_osc_compiler_asan $(OSC_DIR)/oscc \ $(OSC_DIR)/test_legacy_a64 $(OSC_DIR)/test_legacy_a64_asan @mkdir -p $(OSC_QDIR) @@ -127,6 +143,12 @@ test-compiler-quick: $(OSC_DIR)/test_osc_model $(OSC_DIR)/test_osc_backend $(OSC @tail -1 $(OSC_QDIR)/backend.out; grep -q '^OSC1_BACKEND_PASS$$' $(OSC_QDIR)/backend.out $(abspath $(OSC_DIR)/test_osc_backend_asan) > $(OSC_QDIR)/backend_asan.out @tail -1 $(OSC_QDIR)/backend_asan.out; grep -q '^OSC1_BACKEND_PASS$$' $(OSC_QDIR)/backend_asan.out + $(abspath $(OSC_DIR)/test_osc_alloc) > $(OSC_QDIR)/alloc.out + @tail -1 $(OSC_QDIR)/alloc.out; grep -Eq '^PASS [0-9]+ [0-9]+$$' $(OSC_QDIR)/alloc.out + @grep "^alloc sweep: " $(OSC_QDIR)/alloc.out + $(abspath $(OSC_DIR)/test_osc_alloc_asan) > $(OSC_QDIR)/alloc_asan.out + @tail -1 $(OSC_QDIR)/alloc_asan.out; grep -Eq '^PASS [0-9]+ [0-9]+$$' $(OSC_QDIR)/alloc_asan.out + @echo "test-osc-alloc: PASS" $(abspath $(OSC_DIR)/test_osc_compiler) $(OSC_QUICK_FUZZ) > $(OSC_QDIR)/compiler.out @tail -1 $(OSC_QDIR)/compiler.out; grep -Eq '^OSC1_COMPILER_PASS( |$$)' $(OSC_QDIR)/compiler.out $(abspath $(OSC_DIR)/test_osc_compiler_asan) $(OSC_QUICK_ASAN_FUZZ) > $(OSC_QDIR)/compiler_asan.out diff --git a/src/.crumb b/src/.crumb index 9be6117b8..b83d49c2a 100644 --- a/src/.crumb +++ b/src/.crumb @@ -79,8 +79,8 @@ "purpose": "M21 OMEGA_AUTODIFF: reverse-mode tape over M20 tensor CPU ops, caller-owned nodes and scratch, p" }, { - "digest": "18e933f213d7958215bb0ecf09116e5c21d3b335cfdabc1ffbf7a2a4e98b5250", - "last_commit": "f7b43890", + "digest": "436bd3a2666c7573fe572fc3078a75748d06255b4e64168877dad1adb6de3420", + "last_commit": "c5268ac0", "name": "compiler", "purpose": "Directory src/compiler. Purpose not yet described by a human." }, @@ -175,15 +175,15 @@ "purpose": "Directory src/visor. Purpose not yet described by a human." } ], - "children_root": "sha256:1f75b375dd95df74b4dfa3d9a62633f693f3597a6d5a2571655e54d5d8466b4c", - "compiled_at": "2026-10-13T04:05:40Z", + "children_root": "sha256:46b008ff99a65f5bf20ffaf5feb3c89b8bbd6997aeeaa1cb68260f422bec0f61", + "compiled_at": "2026-10-13T04:53:55Z", "compiler": "crumb-compile/1", - "digest": "sha256:4e740ad2caa887e1b91103311126fc0224984aa9e05e9485bee82f34718f5350", + "digest": "sha256:920fbb0917af3267da46e5c70a88374831dd92637b0e7a825672a0dea956dfad", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - "files": 457, - "generated_at_commit": "057414c53f08a2c612038dc9812ac65532e7683e", - "source_tree": "sha256:06ba076df344ab2f81b6d3100c3fa4a9a3ff38175d1554be79b12e2c8a984116" + "files": 459, + "generated_at_commit": "3490958276cb23111f7ef17eef30ee4f3a7cfe10", + "source_tree": "sha256:a0f7901925bb647cb9413272967121be9620fe7dc31f754e053373983a6bdf62" }, "owner_lane": "", "provenance": { diff --git a/src/compiler/.crumb b/src/compiler/.crumb index ccef77068..13adbf1a4 100644 --- a/src/compiler/.crumb +++ b/src/compiler/.crumb @@ -14,20 +14,20 @@ "children": [ { "digest": "4958bd790f408a33ee369dbadc3c8ab6c9eca52effaec2aa65f9f0938b293262", - "last_commit": "b81f850", + "last_commit": "b81f8500", "name": "model", "purpose": "Directory src/compiler/model. Purpose not yet described by a human." } ], "children_root": "sha256:4067665ff5aaacc482c82c65d5074409c044890a046868d11fdb4faa38bbb95c", - "compiled_at": "2026-10-08T07:51:40Z", + "compiled_at": "2026-10-13T04:53:55Z", "compiler": "crumb-compile/1", - "digest": "sha256:18e933f213d7958215bb0ecf09116e5c21d3b335cfdabc1ffbf7a2a4e98b5250", + "digest": "sha256:436bd3a2666c7573fe572fc3078a75748d06255b4e64168877dad1adb6de3420", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - "files": 29, - "generated_at_commit": "bcb9751a17f9e31115154278b636c8d0950948e3", - "source_tree": "sha256:95ca3c705f80d57feba703dda927c2503a7993d12a82f24604f4bd95426f0424" + "files": 31, + "generated_at_commit": "3490958276cb23111f7ef17eef30ee4f3a7cfe10", + "source_tree": "sha256:03a4ac220661d07e3145a19d8aaad375d2d86f62cec231cf13f90b056402987f" }, "owner_lane": "", "provenance": { diff --git a/src/compiler/osc_alloc.c b/src/compiler/osc_alloc.c new file mode 100644 index 000000000..318f3d05c --- /dev/null +++ b/src/compiler/osc_alloc.c @@ -0,0 +1,106 @@ +/* + * osc_alloc.c -- see osc_alloc.h. + * OSC-1 slice; not a general Omega compiler; no self-hosting. + */ +#include "osc_alloc.h" + +#include +#include +#include + +static void *libc_alloc(void *ctx, size_t n) +{ + (void)ctx; + return malloc(n ? n : 1); +} +static void *libc_realloc(void *ctx, void *p, size_t old_n, size_t new_n) +{ + (void)ctx; + (void)old_n; + return realloc(p, new_n ? new_n : 1); +} +static void libc_free(void *ctx, void *p, size_t n) +{ + (void)ctx; + (void)n; + free(p); +} +static const OscAlloc k_libc = {libc_alloc, libc_realloc, libc_free, NULL}; + +const OscAlloc *osc_alloc_libc(void) { return &k_libc; } + +void *osc_alloc_zero(const OscAlloc *a, size_t n) +{ + void *p = a->alloc(a->ctx, n); + if (p) memset(p, 0, n); + return p; +} + +/* ------------------------------------------------------------ meter */ +#define MHDR 16 /* header bytes before each block: the recorded size, padded to keep 16-byte alignment */ + +void osc_alloc_meter_init(OscAllocMeter *m, const OscAlloc *base, size_t budget, size_t fail_at) +{ + memset(m, 0, sizeof *m); + m->base = base ? base : osc_alloc_libc(); + m->budget = budget; + m->fail_at = fail_at; +} + +static size_t *hdr_of(void *p) { return (size_t *)((uint8_t *)p - MHDR); } + +/* would taking `grow` more bytes pass the budget or the fail_at-th counted call? */ +static int meter_refuses(OscAllocMeter *m, size_t grow) +{ + m->calls++; + if (m->fail_at && m->calls == m->fail_at) return 1; + if (grow > SIZE_MAX - MHDR) return 1; + if (m->budget && (grow > m->budget || m->live > m->budget - grow)) return 1; + return 0; +} + +static void *meter_alloc(void *ctx, size_t n) +{ + OscAllocMeter *m = ctx; + m->total_calls++; + if (meter_refuses(m, n)) { m->refused++; return NULL; } + uint8_t *raw = m->base->alloc(m->base->ctx, n + MHDR); + if (!raw) { m->refused++; return NULL; } + memcpy(raw, &n, sizeof n); + m->live += n; + if (m->live > m->peak) m->peak = m->live; + return raw + MHDR; +} + +static void *meter_realloc(void *ctx, void *p, size_t old_n, size_t new_n) +{ + OscAllocMeter *m = ctx; + if (!p) return meter_alloc(ctx, new_n); + m->total_calls++; + size_t rec = *hdr_of(p); + if (rec != old_n) m->bad++; + if (new_n > rec && meter_refuses(m, new_n - rec)) { m->refused++; return NULL; } + uint8_t *raw = m->base->realloc(m->base->ctx, hdr_of(p), rec + MHDR, new_n + MHDR); + if (!raw) { m->refused++; return NULL; } + memcpy(raw, &new_n, sizeof new_n); + m->live = m->live - rec + new_n; + if (m->live > m->peak) m->peak = m->live; + return raw + MHDR; +} + +static void meter_free(void *ctx, void *p, size_t n) +{ + OscAllocMeter *m = ctx; + m->total_calls++; + if (!p) return; + size_t rec = *hdr_of(p); + if (rec != n) m->bad++; + m->live -= rec; + m->base->free(m->base->ctx, hdr_of(p), rec + MHDR); +} + +OscAlloc osc_alloc_meter(OscAllocMeter *m) +{ + OscAlloc a = {meter_alloc, meter_realloc, meter_free, m}; + return a; +} diff --git a/src/compiler/osc_alloc.h b/src/compiler/osc_alloc.h new file mode 100644 index 000000000..650b418d5 --- /dev/null +++ b/src/compiler/osc_alloc.h @@ -0,0 +1,55 @@ +/* + * osc_alloc.h -- explicit allocation context for the OSC compiler (docs/osc/OSC-1-DESIGN.md, + * "Explicit allocation"). Every byte the compiler takes from the heap goes through one OscAlloc + * the caller hands in, so out-of-memory behaviour is testable and peak memory is bounded. + * Zig-style: sizes travel with the call (free and realloc get the size the block was allocated + * with), so an allocator needs no per-block header and a test allocator can check the sizes. + * Contract: + * alloc(ctx, n) n bytes, uninitialised, 16-byte aligned; NULL on refusal. + * realloc(ctx, p, old_n, new_n) + * p == NULL means alloc. On NULL the old block is untouched and still owned + * by the caller (libc semantics). Contents up to min(old_n, new_n) are kept. + * free(ctx, p, n) n = the size p was last allocated or reallocated with; p == NULL is a no-op. + * The compiler never assumes zeroed memory (it clears what it needs, see osc_alloc_zero), never + * keeps a block after the call that took it returns (except OscCode.code, released by osc_cg_free), + * and answers a refusal with a clean failure: OSC_DIAG_CAPACITY from the front end, -1 with + * "out of memory" in err from the validator and back end. Output never depends on the allocator. + * An OscAlloc passed to a compiler entry point must outlive every block taken from it. + * OSC-1 slice; not a general Omega compiler; no self-hosting. + */ +#ifndef OSC_ALLOC_H +#define OSC_ALLOC_H + +#include + +typedef struct OscAlloc { + void *(*alloc)(void *ctx, size_t n); + void *(*realloc)(void *ctx, void *p, size_t old_n, size_t new_n); + void (*free)(void *ctx, void *p, size_t n); + void *ctx; +} OscAlloc; + +/* malloc / realloc / free. What every non-_a entry point uses; a NULL OscAlloc* means this. */ +const OscAlloc *osc_alloc_libc(void); + +/* alloc + memset 0 (the calloc the checker and lowerer used to call). */ +void *osc_alloc_zero(const OscAlloc *a, size_t n); + +/* Test / budget wrapper, usable by any caller. Sits over `base`, keeps a 16-byte size header in + * front of every block so a free or realloc with the wrong size is caught (bad). Counting: + * calls counted attempts: every alloc and every growing realloc (the fail_at clock) + * total_calls every call through the meter, free and shrinking realloc included + * live/peak user bytes held now / at the high-water mark (logical sizes, headers excluded) + * Refusal (NULL, refused++): the fail_at-th counted call (fail_at 0 = never), or a call that would + * take live above budget (budget 0 = none), or base refusing. A compile that leaves live != 0 + * leaked; one with bad != 0 passed a wrong size. */ +typedef struct { + const OscAlloc *base; + size_t live, peak, total_calls, budget, fail_at; + size_t calls, refused, bad; +} OscAllocMeter; + +void osc_alloc_meter_init(OscAllocMeter *m, const OscAlloc *base, size_t budget, size_t fail_at); +OscAlloc osc_alloc_meter(OscAllocMeter *m); /* the allocator view; m must outlive it */ + +#endif /* OSC_ALLOC_H */ diff --git a/src/compiler/osc_cg.c b/src/compiler/osc_cg.c index 8e54b4a68..5d6145122 100644 --- a/src/compiler/osc_cg.c +++ b/src/compiler/osc_cg.c @@ -30,6 +30,7 @@ typedef struct { } Fix; typedef struct { + const OscAlloc *a; uint32_t *w; size_t n, cap; int bad; @@ -59,9 +60,10 @@ static void cg_fail(Cg *g, const char *fmt, ...) { static size_t here(Cg *g) { return g->n; } static void put(Cg *g, uint32_t word) { + if (g->bad) return; /* a failed unit is discarded; do not ask the allocator again */ if (g->n == g->cap) { size_t nc = g->cap ? g->cap * 2 : 4096; - uint32_t *p = realloc(g->w, nc * sizeof *p); + uint32_t *p = g->a->realloc(g->a->ctx, g->w, g->cap * sizeof *p, nc * sizeof *p); if (!p) { cg_fail(g, "out of memory"); return; } g->w = p; g->cap = nc; @@ -77,9 +79,10 @@ static void E(Cg *g, OscA64Insn i) { } static void fix(Cg *g, OscA64Insn i, int kind, int target) { + if (g->bad) return; if (g->nfx == g->capfx) { size_t nc = g->capfx ? g->capfx * 2 : 256; - Fix *p = realloc(g->fx, nc * sizeof *p); + Fix *p = g->a->realloc(g->a->ctx, g->fx, g->capfx * sizeof *p, nc * sizeof *p); if (!p) { cg_fail(g, "out of memory"); return; } g->fx = p; g->capfx = nc; @@ -545,31 +548,38 @@ int osc_cg_verify(const OscCode *c, char *err, size_t n) { return 0; } -int osc_cg_compile(const OscUnit *u, OscCode *out, char *err, size_t n) { +/* the word and fixup buffers, released with the sizes they were last grown to */ +static void cg_drop(Cg *g) { + g->a->free(g->a->ctx, g->w, g->cap * sizeof *g->w); + g->a->free(g->a->ctx, g->fx, g->capfx * sizeof *g->fx); +} + +int osc_cg_compile_a(const OscAlloc *a, const OscUnit *u, OscCode *out, char *err, size_t n) { if (!out) return -1; memset(out, 0, sizeof *out); + if (!a) a = osc_alloc_libc(); + out->a = a; if (!u) return -1; - if (osc_ir_validate(u, err, n)) return -1; + if (osc_ir_validate_a(a, u, err, n)) return -1; Cg g; memset(&g, 0, sizeof g); + g.a = a; g.err = err; g.errn = n; for (int fi = 0; fi < u->nfuncs && !g.bad; fi++) gen_func(&g, u, fi); if (!g.bad && g.n == 0) cg_fail(&g, "empty unit"); if (g.bad) { - free(g.w); - free(g.fx); + cg_drop(&g); return -1; } - out->code = malloc(g.n * 4); - if (!out->code) { free(g.w); free(g.fx); return -1; } + out->code = a->alloc(a->ctx, g.n * 4); + if (!out->code) { cg_fail(&g, "out of memory"); cg_drop(&g); return -1; } for (size_t i = 0; i < g.n; i++) for (int b = 0; b < 4; b++) out->code[4 * i + b] = (uint8_t)(g.w[i] >> (8 * b)); out->len = g.n * 4; out->nfuncs = u->nfuncs; for (int fi = 0; fi < u->nfuncs; fi++) out->entry[fi] = g.entry_word[fi] * 4; - free(g.w); - free(g.fx); + cg_drop(&g); if (osc_cg_verify(out, err, n)) { osc_cg_free(out); return -1; @@ -577,8 +587,13 @@ int osc_cg_compile(const OscUnit *u, OscCode *out, char *err, size_t n) { return 0; } +int osc_cg_compile(const OscUnit *u, OscCode *out, char *err, size_t n) { + return osc_cg_compile_a(osc_alloc_libc(), u, out, err, n); +} + void osc_cg_free(OscCode *c) { if (!c) return; - free(c->code); + const OscAlloc *a = c->a ? c->a : osc_alloc_libc(); + if (c->code) a->free(a->ctx, c->code, c->len); memset(c, 0, sizeof *c); } diff --git a/src/compiler/osc_cg.h b/src/compiler/osc_cg.h index c28aa18e1..550183faf 100644 --- a/src/compiler/osc_cg.h +++ b/src/compiler/osc_cg.h @@ -9,6 +9,7 @@ #include #include +#include "osc_alloc.h" #include "osc_ir.h" typedef struct { @@ -16,12 +17,19 @@ typedef struct { size_t len; /* bytes (multiple of 4) */ uint32_t entry[OSC_MAX_FUNCS]; /* byte offset of each function's entry */ uint16_t nfuncs; + const OscAlloc *a; /* allocator that owns .code (set by compile; NULL = libc, so a zeroed OscCode frees) */ } OscCode; /* Compile a unit. Refuses (-1, message in err) a unit that fails - * osc_ir_validate, any encoder refusal, or a failed self-check (every emitted - * word must decode through osc_a64_decode and re-encode to itself). - * On success out->code is malloc'd; release with osc_cg_free. */ + * osc_ir_validate, any encoder refusal, an allocator refusal ("out of memory"), + * or a failed self-check (every emitted word must decode through osc_a64_decode + * and re-encode to itself). + * On success out->code is allocated from the allocator; release with osc_cg_free. + * osc_cg_compile_a takes all its memory (word and fixup buffers, the validator's + * scratch, the code) from `a` (NULL = libc) and frees with out->a, so `a` must + * outlive the OscCode; osc_cg_compile is the same over libc. The bytes emitted + * never depend on the allocator. */ +int osc_cg_compile_a(const OscAlloc *a, const OscUnit *u, OscCode *out, char *err, size_t n); int osc_cg_compile(const OscUnit *u, OscCode *out, char *err, size_t n); void osc_cg_free(OscCode *c); diff --git a/src/compiler/osc_check.c b/src/compiler/osc_check.c index 41caed578..fbbf9d806 100644 --- a/src/compiler/osc_check.c +++ b/src/compiler/osc_check.c @@ -4,6 +4,7 @@ * See osc_check.h and docs/osc/OSC-1-DESIGN.md section 3. * OSC-1 slice; not a general Omega compiler; no self-hosting. */ +#include "osc_alloc.h" #include "osc_check.h" #include @@ -82,6 +83,7 @@ typedef struct { #define MAX_SCOPES (OSC_AST_MAX_DEPTH * 2 + 8) typedef struct { + const OscAlloc *a; /* every heap byte of the checker (state snapshots, this struct) comes from here */ OscAst *ast; OscDiag *d; OscTrace *tr; @@ -503,7 +505,7 @@ static void pop_scope(C *c) /* ------------------------------------------------------------ state snapshots */ static Snap *snap_take(C *c) { - Snap *s = malloc(sizeof *s); + Snap *s = c->a->alloc(c->a->ctx, sizeof *s); if (!s) return NULL; memcpy(s->obj, c->obj, sizeof(Obj) * (size_t)c->nobj); memcpy(s->bor, c->bor, sizeof(Bor) * (size_t)c->nbor); @@ -513,10 +515,11 @@ static Snap *snap_take(C *c) s->nsym = c->nsym; return s; } +static void snap_free(C *c, Snap *s) { c->a->free(c->a->ctx, s, sizeof *s); } /* OSC-3 item 2: handle state (freed provenances, provenance per handle) */ static HSave *hs_take(C *c) { - HSave *s = malloc(sizeof *s); + HSave *s = c->a->alloc(c->a->ctx, sizeof *s); if (!s) return NULL; memcpy(s->pfreed, c->pfreed, (size_t)c->nprov); for (int i = 0; i < c->nsym; i++) s->hprov[i] = c->sym[i].prov; @@ -524,6 +527,7 @@ static HSave *hs_take(C *c) s->nsym = c->nsym; return s; } +static void hs_free(C *c, HSave *s) { c->a->free(c->a->ctx, s, sizeof *s); } static void hs_restore(C *c, const HSave *s) { memcpy(c->pfreed, s->pfreed, (size_t)s->nprov); @@ -1814,10 +1818,10 @@ static int chk_if(C *c, int i) rc = 0; out: c->cond_depth--; - free(s0); - free(s1); - free(h0); - free(h1); + snap_free(c, s0); + snap_free(c, s1); + hs_free(c, h0); + hs_free(c, h1); return rc; } @@ -1826,7 +1830,7 @@ static int chk_loop_body(C *c, OscNode *n, int is_for) Snap *s0 = snap_take(c); if (!s0) return cap_fail(c, n->line, "checker memory"); HSave *h0 = hs_take(c); /* OSC-3 item 2 */ - if (!h0) { free(s0); return cap_fail(c, n->line, "checker memory"); } + if (!h0) { snap_free(c, s0); return cap_fail(c, n->line, "checker memory"); } int rc = -1; tr_save(c, n->line); c->loop_depth++; @@ -1858,8 +1862,8 @@ static int chk_loop_body(C *c, OscNode *n, int is_for) c->term = 0; rc = 0; out: - free(s0); - free(h0); + snap_free(c, s0); + hs_free(c, h0); return rc; } @@ -2327,13 +2331,14 @@ static int chk_fn(C *c, int fi) return 0; } -int osc_check(OscAst *ast, OscDiag *d, OscTrace *trace) +int osc_check(const OscAlloc *a, OscAst *ast, OscDiag *d, OscTrace *trace) { - C *c = calloc(1, sizeof *c); + C *c = osc_alloc_zero(a, sizeof *c); if (!c) { osc_diag_set(d, OSC_DIAG_CAPACITY, 0, 0, "unit", 0, NULL, "checker memory", "out of memory"); return -1; } + c->a = a; c->ast = ast; c->d = d; c->tr = trace; @@ -2345,6 +2350,6 @@ int osc_check(OscAst *ast, OscDiag *d, OscTrace *trace) osc_diag_set(d, OSC_DIAG_CAPACITY, 0, 0, "unit", 0, NULL, "release list capacity", "too many scope-exit releases"); rc = -1; } - free(c); + a->free(a->ctx, c, sizeof *c); return rc; } diff --git a/src/compiler/osc_check.h b/src/compiler/osc_check.h index 165917aca..549830853 100644 --- a/src/compiler/osc_check.h +++ b/src/compiler/osc_check.h @@ -29,6 +29,7 @@ #include #include "model/osc_model.h" +#include "osc_alloc.h" #include "osc_diag.h" #include "osc_parse.h" @@ -55,7 +56,9 @@ typedef struct OscTrace { OscTraceEntry e[OSC_TRACE_MAX]; } OscTrace; -/* Check ast (after osc_parse). 0 ok; -1 with *d filled. trace may be NULL. */ -int osc_check(OscAst *ast, OscDiag *d, OscTrace *trace); +/* Check ast (after osc_parse). 0 ok; -1 with *d filled (CAPACITY when `a` refuses the checker state + * or one of its branch/loop snapshots). trace may be NULL. + * `a` is not NULL here: the front end resolves NULL to libc. */ +int osc_check(const OscAlloc *a, OscAst *ast, OscDiag *d, OscTrace *trace); #endif /* OSC_CHECK_H */ diff --git a/src/compiler/osc_front.c b/src/compiler/osc_front.c index f793f9117..2b51cc789 100644 --- a/src/compiler/osc_front.c +++ b/src/compiler/osc_front.c @@ -10,13 +10,15 @@ #include #include -int osc_compile_imports(const char *src, size_t len, OscUnit *out, OscDiag *diag, OscTrace *trace, - OscImportResolver resolver, void *resolver_ctx) +int osc_compile_imports_a(const OscAlloc *a, const char *src, size_t len, OscUnit *out, OscDiag *diag, OscTrace *trace, + OscImportResolver resolver, void *resolver_ctx) { + if (!a) a = osc_alloc_libc(); memset(diag, 0, sizeof *diag); if (trace) { trace->n = 0; trace->overflow = 0; trace->refused = 0; trace->nfuncs = 0; } - OscToken *toks = malloc(sizeof(OscToken) * OSC_LEX_MAX_TOKENS); - OscAst *ast = malloc(sizeof *ast); + const size_t toks_sz = sizeof(OscToken) * OSC_LEX_MAX_TOKENS; + OscToken *toks = a->alloc(a->ctx, toks_sz); + OscAst *ast = a->alloc(a->ctx, sizeof *ast); int rc = -1; if (!toks || !ast) { osc_diag_set(diag, OSC_DIAG_CAPACITY, 0, 0, "unit", 0, NULL, "compiler memory", "out of memory"); @@ -54,22 +56,38 @@ int osc_compile_imports(const char *src, size_t len, OscUnit *out, OscDiag *diag "a unit must define at least one function"); goto out; } - if (osc_check(ast, diag, trace)) goto out; - if (osc_lower(ast, out, diag)) goto out; + if (osc_check(a, ast, diag, trace)) goto out; + if (osc_lower(a, ast, out, diag)) goto out; char err[160]; - if (osc_ir_validate(out, err, sizeof err)) { + int vr = osc_ir_validate_a(a, out, err, sizeof err); + if (vr == -2) { + osc_diag_set(diag, OSC_DIAG_CAPACITY, 0, 0, "unit", 0, NULL, "validator memory", "out of memory"); + goto out; + } + if (vr) { osc_diag_set(diag, OSC_DIAG_UNSUPPORTED, 0, 0, "internal", 0, NULL, "IR validation", "internal error: lowered IR refused by the validator: %s", err); goto out; } rc = 0; out: - free(toks); - free(ast); + a->free(a->ctx, toks, toks_sz); + a->free(a->ctx, ast, sizeof *ast); return rc; } +int osc_compile_imports(const char *src, size_t len, OscUnit *out, OscDiag *diag, OscTrace *trace, + OscImportResolver resolver, void *resolver_ctx) +{ + return osc_compile_imports_a(osc_alloc_libc(), src, len, out, diag, trace, resolver, resolver_ctx); +} + +int osc_compile_a(const OscAlloc *a, const char *src, size_t len, OscUnit *out, OscDiag *diag, OscTrace *trace) +{ + return osc_compile_imports_a(a, src, len, out, diag, trace, NULL, NULL); +} + int osc_compile(const char *src, size_t len, OscUnit *out, OscDiag *diag, OscTrace *trace) { - return osc_compile_imports(src, len, out, diag, trace, NULL, NULL); + return osc_compile_imports_a(osc_alloc_libc(), src, len, out, diag, trace, NULL, NULL); } diff --git a/src/compiler/osc_front.h b/src/compiler/osc_front.h index 05b5ea70d..2754d7028 100644 --- a/src/compiler/osc_front.h +++ b/src/compiler/osc_front.h @@ -9,6 +9,7 @@ #define OSC_FRONT_H #include +#include "osc_alloc.h" #include "osc_check.h" #include "osc_diag.h" #include "osc_ir.h" @@ -30,4 +31,14 @@ int osc_compile_imports(const char *src, size_t len, OscUnit *out, OscDiag *diag * refusal: the refused event is the last one, flagged). */ int osc_compile(const char *src, size_t len, OscUnit *out, OscDiag *diag, OscTrace *trace_or_NULL); +/* Explicit allocation (osc_alloc.h): the same entry points with every heap byte the front end + * takes (token array, AST, checker state and snapshots, lowering state, validator scratch) drawn + * from `a` (NULL = libc) and returned to it before the call returns. An allocator refusal is an + * ordinary failure: -1 with diag->kind == OSC_DIAG_CAPACITY. out and diag are the caller's. The + * IR produced is identical for every allocator; osc_compile and osc_compile_imports are these + * two over osc_alloc_libc(). */ +int osc_compile_imports_a(const OscAlloc *a, const char *src, size_t len, OscUnit *out, OscDiag *diag, + OscTrace *trace_or_NULL, OscImportResolver resolver, void *resolver_ctx); +int osc_compile_a(const OscAlloc *a, const char *src, size_t len, OscUnit *out, OscDiag *diag, OscTrace *trace_or_NULL); + #endif /* OSC_FRONT_H */ diff --git a/src/compiler/osc_ir.c b/src/compiler/osc_ir.c index c6bbc7c44..c565a4654 100644 --- a/src/compiler/osc_ir.c +++ b/src/compiler/osc_ir.c @@ -321,7 +321,7 @@ static int validate_insn(const OscUnit *u, int fi, const OscFunc *f, uint32_t ii static bool str_ok(const char *s, size_t cap) { return memchr(s, 0, cap) != NULL; } -static int validate_func(const OscUnit *u, int fi, char *err, size_t n) { +static int validate_func(const OscAlloc *a, const OscUnit *u, int fi, char *err, size_t n) { const OscFunc *f = &u->funcs[fi]; if (!str_ok(f->name, sizeof f->name)) return vfail(err, n, "func %d: name not terminated", fi); if (!str_ok(f->requires_text, sizeof f->requires_text) || !str_ok(f->ensures_text, sizeof f->ensures_text)) @@ -446,8 +446,9 @@ static int validate_func(const OscUnit *u, int fi, char *err, size_t n) { /* definite assignment: every use reads a vreg defined on every path */ int nb = f->nblocks; - uint64_t *in = malloc(sizeof(uint64_t) * BSW * nb), *out = malloc(sizeof(uint64_t) * BSW * nb); - if (!in || !out) { free(in); free(out); return vfail(err, n, "out of memory"); } + size_t bsz = sizeof(uint64_t) * BSW * (size_t)nb; + uint64_t *in = a->alloc(a->ctx, bsz), *out = a->alloc(a->ctx, bsz); + if (!in || !out) { a->free(a->ctx, in, bsz); a->free(a->ctx, out, bsz); vfail(err, n, "out of memory"); return -2; } uint64_t params[BSW] = {0}; for (int v = 0; v < f->nparams; v++) params[v / 64] |= 1ULL << (v % 64); for (int b = 0; b < nb; b++) for (int k = 0; k < BSW; k++) { in[b * BSW + k] = ~0ULL; out[b * BSW + k] = ~0ULL; } @@ -487,8 +488,8 @@ static int validate_func(const OscUnit *u, int fi, char *err, size_t n) { if (d >= 0) cur[d / 64] |= 1ULL << (d % 64); } } - free(in); - free(out); + a->free(a->ctx, in, bsz); + a->free(a->ctx, out, bsz); return rc; } @@ -518,17 +519,22 @@ static int validate_structs(const OscUnit *u, char *err, size_t n) { return 0; } -int osc_ir_validate(const OscUnit *u, char *err, size_t n) { +int osc_ir_validate_a(const OscAlloc *a, const OscUnit *u, char *err, size_t n) { + if (!a) a = osc_alloc_libc(); if (!u) return vfail(err, n, "null unit"); if (u->nfuncs > OSC_MAX_FUNCS) return vfail(err, n, "too many functions"); g_unit = u; if (validate_structs(u, err, n)) return -1; - for (int fi = 0; fi < u->nfuncs; fi++) - if (validate_func(u, fi, err, n)) return -1; + for (int fi = 0; fi < u->nfuncs; fi++) { + int rc = validate_func(a, u, fi, err, n); + if (rc) return rc; + } if (err && n) err[0] = 0; return 0; } +int osc_ir_validate(const OscUnit *u, char *err, size_t n) { return osc_ir_validate_a(osc_alloc_libc(), u, err, n); } + /* ---- canonical encoding ------------------------------------------------ */ typedef struct { uint8_t *buf; diff --git a/src/compiler/osc_ir.h b/src/compiler/osc_ir.h index 34a1d5aba..66ea97226 100644 --- a/src/compiler/osc_ir.h +++ b/src/compiler/osc_ir.h @@ -27,6 +27,7 @@ #include #include #include +#include "osc_alloc.h" #define OSC_MAX_FUNCS 32 #define OSC_MAX_PARAMS 6 /* x0..x5; x7 carries the hidden runtime ctx */ @@ -236,7 +237,11 @@ typedef struct { * terminator and has no terminator before its end, branch targets valid, * calls only to earlier functions with matching arity/types, REF kinds legal * for LOAD/STORE/RELEASE). 0 ok, -1 invalid with message in err. Both the - * interpreter and the back end refuse a unit that does not validate. */ + * interpreter and the back end refuse a unit that does not validate. + * osc_ir_validate_a takes its scratch (one definite-assignment bitset pair per + * function) from `a` (NULL = libc) and returns -2 instead of -1, with "out of + * memory" in err, when `a` refuses; osc_ir_validate is the same over libc. */ +int osc_ir_validate_a(const OscAlloc *a, const OscUnit *u, char *err, size_t n); int osc_ir_validate(const OscUnit *u, char *err, size_t n); /* Canonical encoding of the IR (WireLayout, declared little-endian, no diff --git a/src/compiler/osc_lower.c b/src/compiler/osc_lower.c index 16965a5c9..8ce948772 100644 --- a/src/compiler/osc_lower.c +++ b/src/compiler/osc_lower.c @@ -707,9 +707,9 @@ static int lower_fn(L *l, int fi) return 0; } -int osc_lower(const OscAst *ast, OscUnit *out, OscDiag *d) +int osc_lower(const OscAlloc *a, const OscAst *ast, OscUnit *out, OscDiag *d) { - L *l = calloc(1, sizeof *l); + L *l = osc_alloc_zero(a, sizeof *l); if (!l) { osc_diag_set(d, OSC_DIAG_CAPACITY, 0, 0, "unit", 0, NULL, "lowering memory", "out of memory"); return -1; @@ -725,6 +725,6 @@ int osc_lower(const OscAst *ast, OscUnit *out, OscDiag *d) out->nfuncs = (uint16_t)ast->nfns; memcpy(out->structs, ast->structs, sizeof out->structs); out->nstructs = ast->nstructs; - free(l); + a->free(a->ctx, l, sizeof *l); return rc; } diff --git a/src/compiler/osc_lower.h b/src/compiler/osc_lower.h index a751e8d3e..f005218a5 100644 --- a/src/compiler/osc_lower.h +++ b/src/compiler/osc_lower.h @@ -9,11 +9,13 @@ #ifndef OSC_LOWER_H #define OSC_LOWER_H +#include "osc_alloc.h" #include "osc_diag.h" #include "osc_ir.h" #include "osc_parse.h" -/* 0 ok; -1 with *d filled (CAPACITY when a function exceeds the IR limits). */ -int osc_lower(const OscAst *ast, OscUnit *out, OscDiag *d); +/* 0 ok; -1 with *d filled (CAPACITY when a function exceeds the IR limits, or `a` refuses the + * lowering state). `a` is not NULL here. */ +int osc_lower(const OscAlloc *a, const OscAst *ast, OscUnit *out, OscDiag *d); #endif /* OSC_LOWER_H */ diff --git a/src/compiler/oscc_main.c b/src/compiler/oscc_main.c index 0d52d067b..14e938fd4 100644 --- a/src/compiler/oscc_main.c +++ b/src/compiler/oscc_main.c @@ -4,6 +4,8 @@ * ir_sha256= code_sha256= funcs= * With OUT, also write OUT.ir, OUT.code and OUT.entries (see emit_files). * On a refusal print the diagnostic fields and exit 1. + * Allocation: oscc is a one-shot CLI, so its own buffers (source text, OscUnit, the .ir staging + * buffer) stay on libc; the compiler calls it makes run over osc_alloc_libc() (osc_alloc.h). * OSC-1 slice; not a general Omega compiler; no self-hosting. */ #include diff --git a/tests/.crumb b/tests/.crumb index f00f1c9d4..439127fb9 100644 --- a/tests/.crumb +++ b/tests/.crumb @@ -110,194 +110,194 @@ "children": [ { "digest": "e7498eca87fe5ca8dc27d92f2d99be0de3e4c804711f9d8cba5bb1e47ca95f74", - "last_commit": "28911be", + "last_commit": "28911be7", "name": "algebra", "purpose": "Directory tests/algebra. Purpose not yet described by a human." }, { "digest": "d484ca176e9932b098df5664d01bdd679c78f5b89f4d15a77eb30f43d235f570", - "last_commit": "834ee93", + "last_commit": "834ee93e", "name": "ans", "purpose": "Tests 1-10 for ANS (docs/ans/INERTIAL_ALIGNMENT_SPEC_V1.md): freeze/digest, hostile reference mu" }, { "digest": "42a7efc25b16d1ea7bd7a5cbc31be7e499c9bc64c9e7df179c7627121d7df637", - "last_commit": "1b0e296", + "last_commit": "1b0e2968", "name": "brownian", "purpose": "Directory tests/brownian. Purpose not yet described by a human." }, { "digest": "1cbe9561b87b394e200f67996ce1407c377025d07ad033b27cdd542625e277d4", - "last_commit": "30bb116", + "last_commit": "30bb116c", "name": "canonicalization", "purpose": "Directory tests/canonicalization. Purpose not yet described by a human." }, { - "digest": "d0d9e7040af5d739682fe07e15cb59598f0a981ab1a766a4c2ad2f26e4e1e458", - "last_commit": "eb8dedf", + "digest": "5446c693d59f25f1871d39f66c5f1fac33cf9e7476101a9b303e8e59079a4516", + "last_commit": "c5268ac0", "name": "compiler", "purpose": "Directory tests/compiler. Purpose not yet described by a human." }, { "digest": "47683e6a56ca8e14ee526004b2a90dd08498507e8d4b0b42c3d85d22afb07d28", - "last_commit": "28b8793", + "last_commit": "28b87934", "name": "constraints", "purpose": "Directory tests/constraints. Purpose not yet described by a human." }, { "digest": "95b2041562face1ba7baa6c57456bf07d77a0f44e093b989ec18f559064e22d9", - "last_commit": "49abb02", + "last_commit": "49abb024", "name": "crumbline", "purpose": "Directory tests/crumbline. Purpose not yet described by a human." }, { "digest": "d57833fb3f63ef6475cf25e2e16319d818a0a407b825d5b4c5cbdd3cc0e06492", - "last_commit": "00f54b6", + "last_commit": "00f54b6b", "name": "dual_experiment", "purpose": "Tests for the DUAL applicability experiment: determinism (same seed, byte-identical results dige" }, { "digest": "fb0a9296a93d065d5e016ad0a950fc6b9691f8ddca12ad0111cee2e35791cdb4", - "last_commit": "c819b37", + "last_commit": "c819b376", "name": "effect", "purpose": "Directory tests/effect. Purpose not yet described by a human." }, { "digest": "bc2473da101ba5a74c32021a9d27ecb2007897f07a9689ba50ccc99b9030bf0c", - "last_commit": "1a147e4", + "last_commit": "1a147e40", "name": "estimation", "purpose": "Directory tests/estimation. Purpose not yet described by a human." }, { "digest": "de2bc4e310a257dbd21cc5c9624641b137b1b4487d8876cb52469aea2af5b904", - "last_commit": "3108fc2", + "last_commit": "3108fc2f", "name": "fabric", "purpose": "Directory tests/fabric. Purpose not yet described by a human." }, { "digest": "90418926706f827aafd72938aee1dea89222cb5e465fba06a8df5a83253e0412", - "last_commit": "28b8793", + "last_commit": "28b87934", "name": "identity", "purpose": "Directory tests/identity. Purpose not yet described by a human." }, { "digest": "c653c851c8934ce9ac4eabaa32a9df65d858c9ac7a556e76b265b1ee81494443", - "last_commit": "4f55f45", + "last_commit": "4f55f45e", "name": "language", "purpose": "Directory tests/language. Purpose not yet described by a human." }, { "digest": "e485aa0a00f0a588e22d55caa704db8581a5da1b53080b311086e6edd38f6044", - "last_commit": "30bb116", + "last_commit": "30bb116c", "name": "malformed", "purpose": "Directory tests/malformed. Purpose not yet described by a human." }, { "digest": "adba9b5ea1c000db36078fca7d1bfef0b2769b73c8fb474024d9409bd48bdbc1", - "last_commit": "40d1ea3", + "last_commit": "40d1ea37", "name": "numeric_native", "purpose": "Simulated M16 driver for numeric resource-lifecycle tests. No device execution or numeric parity" }, { "digest": "f3085ce1facdcdb22bfedc85de659dd5e512aa7b59cd8103666c24a4456681c3", - "last_commit": "6d8d92a", + "last_commit": "6d8d92ad", "name": "osh", "purpose": "osh lexer host driver, vendored aien-protocols PR#16 draft vectors (5ae5292)" }, { "digest": "a4d320b3203650a28c43ea954957d8902d1cc25cbdc8aecd15628c01a92f68f8", - "last_commit": "d550b70", + "last_commit": "d550b705", "name": "path", "purpose": "Directory tests/path. Purpose not yet described by a human." }, { "digest": "d5153d318a11642a5e13824353222fc392ead92108540bc8ffd747fcfd40b813", - "last_commit": "e59633e", + "last_commit": "e59633e5", "name": "physics0", "purpose": "PD-0 gates: world self-test, oracle calibration, learner L0/L1/NULL, mutations, OSC differential" }, { "digest": "4619a90a957e77cd3d5680132ab4d1a5ef65f43419ddcc089743ca5b7a7719ee", - "last_commit": "8f8babe", + "last_commit": "8f8babe9", "name": "polyglot", "purpose": "Directory tests/polyglot. Purpose not yet described by a human." }, { "digest": "16bea65aa50f0b3ab8b90cb286de048639fa8e635ff84e517e138c6f9f26a059", - "last_commit": "3c82389", + "last_commit": "3c82389f", "name": "program", "purpose": "Directory tests/program. Purpose not yet described by a human." }, { "digest": "ba0c09f67d12b5d7de906129bdbe2f6e0a228e1a34236ecc4b779c523cb2061e", - "last_commit": "6fdc4c3", + "last_commit": "6fdc4c33", "name": "r16_inventory", "purpose": "Directory tests/r16_inventory. Purpose not yet described by a human." }, { "digest": "000c487afa8f600842eff25a22a671afb5e29b26845105405de9542a21b70746", - "last_commit": "79a805d", + "last_commit": "79a805d1", "name": "r16_negative", "purpose": "Directory tests/r16_negative. Purpose not yet described by a human." }, { "digest": "25cabf47d18353539db28146434d246a2f81bf9c323471566451aca56806f279", - "last_commit": "44d8c06", + "last_commit": "44d8c06c", "name": "r16_surface", "purpose": "Directory tests/r16_surface. Purpose not yet described by a human." }, { "digest": "9106f2ea57e8f453322f0a988b61cfcbd06631573c9ff32a777903d27d7741e1", - "last_commit": "40d1ea3", + "last_commit": "40d1ea37", "name": "realize", "purpose": "Directory tests/realize. Purpose not yet described by a human." }, { "digest": "381184e79b26920b256dd4a774fb11f17a92afa8ceda4813eada241bac4163a9", - "last_commit": "30bb116", + "last_commit": "30bb116c", "name": "relations", "purpose": "Directory tests/relations. Purpose not yet described by a human." }, { "digest": "bc2b514054f69604a155900fb1884948a78e12b88a15852296cbea1f751055b2", - "last_commit": "362c9ab", + "last_commit": "501d555c", "name": "runtime", "purpose": "Directory tests/runtime. Purpose not yet described by a human." }, { "digest": "b1311025a3bfd8dd7b5e07b1adf1d7ece502a15200f7207f515334f7083b8315", - "last_commit": "5ae192a", + "last_commit": "5ae192a4", "name": "searchtrace", "purpose": "Directory tests/searchtrace. Purpose not yet described by a human." }, { "digest": "3ecf636e2923038708cafa062895b8d8f2664dc1865cdff38575911eaa9f1b2d", - "last_commit": "54826a3", + "last_commit": "54826a37", "name": "train", "purpose": "Directory tests/train. Purpose not yet described by a human." }, { "digest": "75d5e576c38a0497a55e9f5ee79550cb94f9b75008f3e4e126312802704b3f86", - "last_commit": "2564f57", + "last_commit": "2564f57a", "name": "turing", "purpose": "Directory tests/turing. Purpose not yet described by a human." }, { "digest": "3f19e3034ff999211cb13ab8995ca6c38d0d8e1f5bfbbbd621caaa08412d3f1f", - "last_commit": "fed4f07", + "last_commit": "fed4f077", "name": "visor", "purpose": "Directory tests/visor. Purpose not yet described by a human." } ], - "children_root": "sha256:1cc27c84d27f640e8c60e4d261fb875c96805684d1dab580b1cf456f56aa2495", - "compiled_at": "2026-10-13T03:32:59Z", + "children_root": "sha256:e0bff164a6974e32193d41c0b9a7e94c39c27dd442f6d656fa8c47997854191b", + "compiled_at": "2026-10-13T04:37:52Z", "compiler": "crumb-compile/1", - "digest": "sha256:036c34e6022ae19b81e105135457d02186fa8597ec00ebf4a150af6c237e0e61", + "digest": "sha256:55eb38cd39a0240da6b66975594c053d9e21bcdf2dbd8ebf9760f5dd26313a09", "evidence_files": 16, "evidence_root": "sha256:96d08097822ad95f4e562c19e0ba5df5a035774c48632f1832912c40a27bd27a", - "files": 1028, - "generated_at_commit": "8c3a234288f9c0dd1b3ede1322a95513187c4373", - "source_tree": "sha256:d57dd4c04d26027c6e23f73d62065234f73f85cdb458b2be07b1bd954089d40e" + "files": 1029, + "generated_at_commit": "cc3f8584d83571c4d53336742af0ebdda254a0ab", + "source_tree": "sha256:e163f8b2313e0a2ff06efc7deccc8cbc8756095f644ef3f1a6162d4c649d1719" }, "owner_lane": "", "provenance": { diff --git a/tests/compiler/.crumb b/tests/compiler/.crumb index 04db7fa81..57a9088a5 100644 --- a/tests/compiler/.crumb +++ b/tests/compiler/.crumb @@ -18,26 +18,26 @@ "children": [ { "digest": "52f60db8cad897bd1ad36a41cff28e90ad3ccd0e734555a8325701f1a9e9ba7b", - "last_commit": "eb8dedf", + "last_commit": "eb8dedf7", "name": "neg", "purpose": "Directory tests/compiler/neg. Purpose not yet described by a human." }, { "digest": "44128e1034bd4f570667a4a57f5d058b09b127f6e203e2173e2b07c533c45b57", - "last_commit": "334117d", + "last_commit": "334117d1", "name": "progs", "purpose": "Directory tests/compiler/progs. Purpose not yet described by a human." } ], "children_root": "sha256:8e71869afa3e9efae551dc468dfd9fae972bd9a404fc7f2cbdabc1684e98fb8e", - "compiled_at": "2026-10-08T00:52:33Z", + "compiled_at": "2026-10-13T04:37:52Z", "compiler": "crumb-compile/1", - "digest": "sha256:d0d9e7040af5d739682fe07e15cb59598f0a981ab1a766a4c2ad2f26e4e1e458", + "digest": "sha256:5446c693d59f25f1871d39f66c5f1fac33cf9e7476101a9b303e8e59079a4516", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - "files": 204, - "generated_at_commit": "eb8dedf75dfdf6cdeaee122ac8c7db90539c28e4", - "source_tree": "sha256:dda89ad854c58187acd329fa6991a61038101638f6e52caafadd73c43e25aec2" + "files": 205, + "generated_at_commit": "cc3f8584d83571c4d53336742af0ebdda254a0ab", + "source_tree": "sha256:2b0bf6b04b517e2e9d9758878b98212b2491a33c3bc5f880cc7ccb86502f0db6" }, "owner_lane": "", "provenance": { diff --git a/tests/compiler/test_osc_alloc.c b/tests/compiler/test_osc_alloc.c new file mode 100644 index 000000000..81f9c881f --- /dev/null +++ b/tests/compiler/test_osc_alloc.c @@ -0,0 +1,365 @@ +/* + * test_osc_alloc.c -- OSC explicit-allocation gate (src/compiler/osc_alloc.h, + * docs/osc/OSC-1-DESIGN.md "Explicit allocation"). + * OSC-1 slice; not a general Omega compiler; no self-hosting. + * + * For every golden program (tests/compiler/progs/NAME.osc) plus one generated unit big enough to + * grow the back end's word and fixup buffers (the only realloc sites), compile + emit through + * osc_compile_a / osc_cg_compile_a and check: + * 1. metered run over libc: succeeds, live == 0 after osc_cg_free (no leak), no wrong-size + * free or realloc (bad == 0), nothing refused; N = counted allocation calls, P = peak bytes. + * 2. determinism: the IR (osc_ir_encode) and code bytes are identical to the unwrapped libc entry + * points, to osc_alloc_libc() passed explicitly, to a NULL allocator, and to an allocator that + * fills new blocks with 0xA5 and freed blocks with 0xDD (so nothing may rely on zeroed or + * stale memory; the checker and lowerer used to calloc). + * 3. failure sweep: for every k in 1..N the k-th allocation call is refused. Compile + emit must + * fail cleanly (front end: OSC_DIAG_CAPACITY; validator / back end: -1 with "out of memory" + * in err), exactly one refusal, live == 0 afterwards (no leak on any failure path), bad == 0. + * 4. budget: a budget of P-1 bytes fails cleanly, a budget of P succeeds with the same bytes and + * a peak of exactly P; the same pair for the back end alone (peak Pe, unit already built). + * Also: osc_ir_validate_a swept the same way (-2 + "out of memory"), and osc_cg_free on a zeroed + * OscCode (a == NULL) releases a libc-compiled unit. + * + * Usage: test_osc_alloc [tests dir (default tests/compiler)] + * Final line: `PASS `, or `FAIL ...` and exit 1. + */ +#include +#include +#include +#include +#include + +#include "osc_alloc.h" +#include "osc_cg.h" +#include "osc_front.h" +#include "osc_ir.h" + +static unsigned long checks, failures; + +#define CHECK(cond, ...) \ + do { \ + checks++; \ + if (!(cond)) { \ + failures++; \ + if (failures <= 40) { \ + printf("FAIL %s:%d: ", __FILE__, __LINE__); \ + printf(__VA_ARGS__); \ + printf("\n"); \ + } \ + } \ + } while (0) + +/* ------------------------------------------------------------ files */ +static char *slurp(const char *path, size_t *len) +{ + FILE *f = fopen(path, "rb"); + if (!f) return NULL; + size_t cap = 1 << 16, n = 0; + char *b = malloc(cap + 1); + while (b) { + n += fread(b + n, 1, cap - n, f); + if (n < cap) break; + cap *= 2; + char *nb = realloc(b, cap + 1); + if (!nb) { free(b); b = NULL; break; } + b = nb; + } + fclose(f); + if (b) { b[n] = 0; *len = n; } + return b; +} + +static int cmpstr(const void *a, const void *b) { return strcmp(*(char *const *)a, *(char *const *)b); } + +static int list_osc(const char *dir, char ***out) +{ + DIR *d = opendir(dir); + if (!d) return -1; + int n = 0, cap = 64; + char **v = malloc(sizeof(char *) * (size_t)cap); + struct dirent *e; + while ((e = readdir(d))) { + size_t l = strlen(e->d_name); + if (l < 5 || strcmp(e->d_name + l - 4, ".osc")) continue; + if (n == cap) { cap *= 2; v = realloc(v, sizeof(char *) * (size_t)cap); } + v[n++] = strdup(e->d_name); + } + closedir(d); + qsort(v, (size_t)n, sizeof(char *), cmpstr); + *out = v; + return n; +} + +/* Twelve functions of 200 `s = s + b;` statements: about 17000 code words (the word buffer starts + * at 4096 and doubles) and 2400 trap fixups (starts at 256), so the growing-realloc paths of the + * back end run. Under the 480-vreg and 16384-node limits of one function / one unit. */ +static char *gen_big(size_t *len) +{ + size_t cap = 12 * 200 * 16 + 4096, n = 0; + char *b = malloc(cap); + if (!b) return NULL; + for (int f = 0; f < 12; f++) { + n += (size_t)snprintf(b + n, cap - n, "fn big%d(a: u64, b: u64) -> u64 {\n let mut s: u64 = a;\n", f); + for (int i = 0; i < 200; i++) n += (size_t)snprintf(b + n, cap - n, " s = s + b;\n"); + n += (size_t)snprintf(b + n, cap - n, " return s;\n}\n"); + } + *len = n; + return b; +} + +/* ------------------------------------------------------------ allocators */ +/* fills what it hands out with 0xA5 and what it takes back with 0xDD */ +static void *scr_alloc(void *ctx, size_t n) +{ + (void)ctx; + void *p = malloc(n ? n : 1); + if (p) memset(p, 0xA5, n); + return p; +} +static void *scr_realloc(void *ctx, void *p, size_t old_n, size_t new_n) +{ + (void)ctx; + void *q = realloc(p, new_n ? new_n : 1); + if (q && new_n > old_n) memset((uint8_t *)q + old_n, 0xA5, new_n - old_n); + return q; +} +static void scr_free(void *ctx, void *p, size_t n) +{ + (void)ctx; + if (p) memset(p, 0xDD, n); + free(p); +} +static const OscAlloc k_scribble = {scr_alloc, scr_realloc, scr_free, NULL}; + +/* ------------------------------------------------------------ one compile + emit */ +typedef struct { + int stage; /* 0 ok, 1 front end refused, 2 validator / back end refused */ + OscDiag d; + char err[160]; +} Res; + +typedef struct { + uint8_t *ir, *code; + size_t irn, coden; +} Out; + +static int run(const OscAlloc *a, const char *src, size_t len, OscUnit *u, OscCode *c, Res *r) +{ + memset(r, 0, sizeof *r); + memset(c, 0, sizeof *c); + if (osc_compile_a(a, src, len, u, &r->d, NULL)) { r->stage = 1; return -1; } + if (osc_cg_compile_a(a, u, c, r->err, sizeof r->err)) { r->stage = 2; return -1; } + return 0; +} + +static int snapshot(const OscUnit *u, const OscCode *c, Out *o) +{ + memset(o, 0, sizeof *o); + if (osc_ir_encode(u, NULL, 0, &o->irn)) return -1; + o->ir = malloc(o->irn); + o->code = malloc(c->len ? c->len : 1); + if (!o->ir || !o->code || osc_ir_encode(u, o->ir, o->irn, &o->irn)) return -1; + memcpy(o->code, c->code, c->len); + o->coden = c->len; + return 0; +} + +static void out_free(Out *o) { free(o->ir); free(o->code); memset(o, 0, sizeof *o); } + +static int same(const Out *a, const Out *b) +{ + return a->irn == b->irn && a->coden == b->coden && !memcmp(a->ir, b->ir, a->irn) && !memcmp(a->code, b->code, a->coden); +} + +/* A refusal is reported the documented way: front end CAPACITY, validator / back end "out of memory". */ +static int clean_oom(const Res *r) +{ + if (r->stage == 1) return r->d.kind == OSC_DIAG_CAPACITY; + if (r->stage == 2) return strstr(r->err, "out of memory") != NULL; + return 0; +} + +static unsigned long n_programs, n_calls, n_failpoints, n_front, n_emit, n_pairs, n_validate_points; +static size_t peak_max, emit_peak_max; + +static void program(const char *label, const char *src, size_t len, OscUnit *u) +{ + OscCode c; + Res r; + Out ref, o; + unsigned long f0 = failures; + + /* reference bytes: the unwrapped libc entry points (what every existing caller uses) */ + OscDiag d; + char err[160]; + int rc = osc_compile(src, len, u, &d, NULL) || osc_cg_compile(u, &c, err, sizeof err); + CHECK(rc == 0, "%s: reference compile failed (%s)", label, d.message); + if (rc) return; + CHECK(snapshot(u, &c, &ref) == 0, "%s: snapshot", label); + osc_cg_free(&c); + + /* 1. metered run over libc */ + OscAllocMeter m; + osc_alloc_meter_init(&m, NULL, 0, 0); + OscAlloc ma = osc_alloc_meter(&m); + rc = run(&ma, src, len, u, &c, &r); + CHECK(rc == 0, "%s: metered compile failed: %s%s", label, r.d.message, r.err); + CHECK(rc != 0 || (snapshot(u, &c, &o) == 0 && same(&o, &ref)), "%s: metered output differs from libc", label); + if (rc == 0) out_free(&o); + CHECK(c.a == &ma || rc, "%s: OscCode.a not set to the allocator", label); + osc_cg_free(&c); + CHECK(m.live == 0, "%s: leak: live=%zu after free", label, m.live); + CHECK(m.bad == 0, "%s: %zu wrong-size free/realloc", label, m.bad); + CHECK(m.refused == 0, "%s: %zu refusals with no limit set", label, m.refused); + size_t N = m.calls, P = m.peak; + CHECK(N >= 6 && P > 0, "%s: implausible meter N=%zu P=%zu", label, N, P); + if (rc || failures != f0) { out_free(&ref); return; } + + /* 2. determinism across allocators: libc passed explicitly, NULL (= libc), and the scribbler under a meter */ + OscAllocMeter sm; + osc_alloc_meter_init(&sm, &k_scribble, 0, 0); + OscAlloc sa = osc_alloc_meter(&sm); + const OscAlloc *variants[3] = {osc_alloc_libc(), NULL, &sa}; + const char *vname[3] = {"libc", "NULL", "scribble"}; + for (int v = 0; v < 3; v++) { + rc = run(variants[v], src, len, u, &c, &r); + CHECK(rc == 0, "%s: %s allocator: compile failed", label, vname[v]); + if (rc == 0) { + CHECK(snapshot(u, &c, &o) == 0 && same(&o, &ref), "%s: %s allocator: output differs", label, vname[v]); + out_free(&o); + } + osc_cg_free(&c); + } + CHECK(sm.live == 0 && sm.bad == 0 && sm.calls == N, "%s: scribble run: live=%zu bad=%zu calls=%zu (want %zu)", label, + sm.live, sm.bad, sm.calls, N); + /* a zeroed OscCode (a == NULL) frees a libc-compiled unit */ + rc = run(osc_alloc_libc(), src, len, u, &c, &r); + if (rc == 0) { c.a = NULL; osc_cg_free(&c); CHECK(c.code == NULL && c.len == 0, "%s: osc_cg_free left the code", label); } + + /* 3. failure sweep: the k-th allocation call is refused */ + for (size_t k = 1; k <= N; k++) { + OscAllocMeter fm; + osc_alloc_meter_init(&fm, NULL, 0, k); + OscAlloc fa = osc_alloc_meter(&fm); + rc = run(&fa, src, len, u, &c, &r); + osc_cg_free(&c); + CHECK(rc != 0, "%s: fail_at=%zu: compile + emit succeeded", label, k); + CHECK(rc == 0 || clean_oom(&r), "%s: fail_at=%zu: stage %d refusal is not a clean OOM (kind %d, err '%s')", label, k, + r.stage, (int)r.d.kind, r.err); + CHECK(fm.refused == 1, "%s: fail_at=%zu: %zu refusals", label, k, fm.refused); + CHECK(fm.live == 0, "%s: fail_at=%zu: leak: live=%zu", label, k, fm.live); + CHECK(fm.bad == 0, "%s: fail_at=%zu: %zu wrong-size free/realloc", label, k, fm.bad); + n_failpoints++; + if (r.stage == 1) n_front++; + if (r.stage == 2) n_emit++; + } + + /* 4. budget: P-1 fails, P succeeds with the same bytes and a peak of exactly P */ + OscAllocMeter bm; + osc_alloc_meter_init(&bm, NULL, P - 1, 0); + OscAlloc ba = osc_alloc_meter(&bm); + rc = run(&ba, src, len, u, &c, &r); + osc_cg_free(&c); + CHECK(rc != 0 && clean_oom(&r), "%s: budget %zu (P-1) did not fail cleanly", label, P - 1); + CHECK(bm.live == 0 && bm.bad == 0 && bm.peak <= P - 1, "%s: budget P-1: live=%zu bad=%zu peak=%zu", label, bm.live, bm.bad, bm.peak); + osc_alloc_meter_init(&bm, NULL, P, 0); + ba = osc_alloc_meter(&bm); + rc = run(&ba, src, len, u, &c, &r); + CHECK(rc == 0, "%s: budget %zu (P) failed: %s%s", label, P, r.d.message, r.err); + if (rc == 0) { + CHECK(snapshot(u, &c, &o) == 0 && same(&o, &ref), "%s: budget P: output differs", label); + out_free(&o); + } + osc_cg_free(&c); + CHECK(bm.live == 0 && bm.bad == 0 && bm.peak == P && bm.refused == 0, "%s: budget P: live=%zu bad=%zu peak=%zu (want %zu) refused=%zu", label, + bm.live, bm.bad, bm.peak, P, bm.refused); + n_pairs++; + + /* 4b. the back end alone (the unit already exists): its own peak Pe, Pe-1 fails, Pe succeeds with the same code */ + OscAllocMeter em; + osc_alloc_meter_init(&em, NULL, 0, 0); + OscAlloc ea = osc_alloc_meter(&em); + rc = osc_cg_compile_a(&ea, u, &c, err, sizeof err); + CHECK(rc == 0, "%s: emit-only compile failed: %s", label, err); + osc_cg_free(&c); + size_t Pe = em.peak; + CHECK(em.live == 0 && Pe > 0, "%s: emit-only: live=%zu peak=%zu", label, em.live, Pe); + osc_alloc_meter_init(&em, NULL, Pe - 1, 0); + ea = osc_alloc_meter(&em); + rc = osc_cg_compile_a(&ea, u, &c, err, sizeof err); + osc_cg_free(&c); + CHECK(rc != 0 && strstr(err, "out of memory") && em.live == 0 && em.bad == 0, "%s: emit budget %zu (Pe-1): rc=%d err='%s' live=%zu", label, + Pe - 1, rc, err, em.live); + osc_alloc_meter_init(&em, NULL, Pe, 0); + ea = osc_alloc_meter(&em); + rc = osc_cg_compile_a(&ea, u, &c, err, sizeof err); + CHECK(rc == 0 && c.len == ref.coden && !memcmp(c.code, ref.code, c.len), "%s: emit budget %zu (Pe): rc=%d err='%s'", label, Pe, rc, err); + osc_cg_free(&c); + CHECK(em.live == 0 && em.bad == 0 && em.peak == Pe, "%s: emit budget Pe: live=%zu peak=%zu (want %zu)", label, em.live, em.peak, Pe); + if (Pe > emit_peak_max) emit_peak_max = Pe; + + /* osc_ir_validate_a swept on its own: -2 and "out of memory", no leak */ + OscAllocMeter vm; + osc_alloc_meter_init(&vm, NULL, 0, 0); + OscAlloc va = osc_alloc_meter(&vm); + CHECK(osc_ir_validate_a(&va, u, err, sizeof err) == 0 && vm.live == 0, "%s: validate_a over the meter", label); + size_t vn = vm.calls; + for (size_t k = 1; k <= vn; k++) { + osc_alloc_meter_init(&vm, NULL, 0, k); + va = osc_alloc_meter(&vm); + int vr = osc_ir_validate_a(&va, u, err, sizeof err); + CHECK(vr == -2 && strstr(err, "out of memory") && vm.live == 0 && vm.bad == 0 && vm.refused == 1, + "%s: validate_a fail_at=%zu: rc=%d err='%s' live=%zu", label, k, vr, err, vm.live); + n_validate_points++; + } + + n_programs++; + n_calls += N; + if (P > peak_max) peak_max = P; + printf("%-28s calls=%-4zu peak=%-8zu fail-points=%zu\n", label, N, P, N); + out_free(&ref); +} + +int main(int argc, char **argv) +{ + const char *root = argc > 1 ? argv[1] : "tests/compiler"; + char pdir[512]; + snprintf(pdir, sizeof pdir, "%s/progs", root); + OscUnit *u = malloc(sizeof *u); + if (!u) { printf("FAIL out of memory\n"); return 1; } + + char **pv = NULL; + int np = list_osc(pdir, &pv); + if (np < 0) { printf("FAIL cannot list %s\n", pdir); free(u); return 1; } + CHECK(np >= 20, "need >= 20 golden programs, found %d", np); + for (int i = 0; i < np; i++) { + char path[1024]; + snprintf(path, sizeof path, "%s/%s", pdir, pv[i]); + size_t len = 0; + char *src = slurp(path, &len); + CHECK(src != NULL, "cannot read %s", path); + if (!src) continue; + program(pv[i], src, len, u); + free(src); + } + size_t blen = 0; + char *big = gen_big(&blen); + CHECK(big != NULL, "cannot build the generated unit"); + if (big) { + program("(generated: 12 x 200 adds)", big, blen, u); + free(big); + } + free(u); + for (int i = 0; i < np; i++) free(pv[i]); + free(pv); + + printf("alloc sweep: programs=%lu (golden files %d + 1 generated) calls=%lu peak_max=%zu emit_peak_max=%zu fail_points=%lu (front=%lu emit=%lu) " + "budget_pairs=%lu validate_points=%lu checks=%lu failed=%lu\n", + n_programs, np, n_calls, peak_max, emit_peak_max, n_failpoints, n_front, n_emit, n_pairs, n_validate_points, checks, failures); + if (failures || n_programs != (unsigned long)np + 1 || n_failpoints != n_calls) { + printf("FAIL %lu checks failed (programs run %lu of %d)\n", failures, n_programs, np + 1); + return 1; + } + printf("PASS %lu %lu\n", n_programs, n_failpoints); + return 0; +} ```