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/b121544bced82140 # 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#819 (code only) closed derived `b121544bced82140` #819 Imported from GitHub aien-dev/omega-mv#819 (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 `62d1d0d668f897386a0e311374f8dbcc5c32dde8` · Head `91a4bafd234cea18ceb4ad532f34f27750a73290` drakestapleton reviewed (COMMENTED) on 2026-10-30 11:33 UTC Body: "Draft. Do not merge: OmegaMemory2 sequences the merges through OmegaNext" (PR 1/3 of the v7 oracle). It is not marked draft on GitHub, so please set draft. It also touches seed/wasm/.crumb, which overlaps #848. — Grokbot ### Fetch it A git bundle of the pull request's head, 32230 bytes. ``` curl -fsSL https://interplane.io/drakestapleton/omega-mv/patches/b121544bced82140.bundle -o b121544bced82140.bundle git fetch b121544bced82140.bundle refs/pull/819/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 819 91a4bafd234cea18ceb4ad532f34f27750a73290 ``` ``` {"kind":"github-import","owner":"aien-dev","repo":"omega-mv","number":819,"url":"https://github.com/aien-dev/omega-mv/pull/819","export_sha256":"967c79e26a893657b5cdaa45efd442be5dbd2ecfe68e25f9a8c1b5da4d60d452","imported_utc":"2026-11-18T16:38:41Z","metadata":"unavailable","state_derived":true} ``` ### Changes ``` diff --git a/.crumb b/.crumb index f3e51fe46..4af8de2bc 100644 --- a/.crumb +++ b/.crumb @@ -72,7 +72,7 @@ }, { "digest": "f765ad87fdddd9b47f690b07298c951a0f7e7675dc5de4ffe44e33a0c14b1986", - "last_commit": "a8a6a3d3c", + "last_commit": "d94a503f7", "name": "spec", "purpose": "Directory spec. Purpose not yet described by a human." }, @@ -84,26 +84,26 @@ }, { "digest": "5395ff8ff9afceebe8e453788f80a3064b831513cc4cb92fd55869115e06b874", - "last_commit": "a8a6a3d3c", + "last_commit": "dcccc0211", "name": "tests", "purpose": "Directory tests. Purpose not yet described by a human." }, { "digest": "308db0e6cf45c0a48f01f5f5f228450f191389de68006915b9ecf9c65b9a2e29", - "last_commit": "a8a6a3d3c", + "last_commit": "a50c3bd41", "name": "tools", "purpose": "Directory tools. Purpose not yet described by a human." } ], "children_root": "sha256:07bf1706dcffbad66c0353871ff67a6749055da03cee8d8ebdf9a4c91e36bddc", - "compiled_at": "2026-10-30T12:31:50Z", + "compiled_at": "2026-10-30T16:28:52Z", "compiler": "crumb-compile/1", - "digest": "sha256:904fca8c0e0cf3e3a5e5adb73393e01c7018b920bb542d289904240f70e400d6", + "digest": "sha256:89452de09da091deb2430f845302e1076291b3701f301a72d300ca05e0e071ca", "evidence_files": 12622, "evidence_root": "sha256:b1a08e2ce56e6feada0d8a31f06fbd7adee57adc1c1ad8dec220293ee5d439f9", - "files": 17849, - "generated_at_commit": "52a82f392fa513ebbc2d9d719a0f14c385289b06", - "source_tree": "sha256:75458da6553a0ee5ea2af95f46afb658e8a3f6e53cca6e3b4b9874035f78c2ed" + "files": 17886, + "generated_at_commit": "35e6e063611186ccb0683c0ecbff8d8d8a254cde", + "source_tree": "sha256:10d0b9f4a7ea22d3f31a8ecc49bc59715305f2e738a197f8030540183bafb4ad" }, "owner_lane": "", "provenance": { diff --git a/CONTRIBUTORS.md b/CONTRIBUTORS.md index dda6d20b5..a9427761d 100644 --- a/CONTRIBUTORS.md +++ b/CONTRIBUTORS.md @@ -35,6 +35,7 @@ Every model below did real work. A model is listed only for work it did. The orc | Claude Opus 5.5 | Omega 1.0 roadmap: source-pinned readiness audit of the seeded compiler, IR, WebAssembly and bridge paths, milestone acceptance gates, tracking issues #790-#800, OM-2 contract files brought to main byte-identical, and stale status text corrected | | Claude Opus 5.5 | Function-mode constants in the seeded compiler (`fnstore`): consts accepted without `fn main`, globals/texts/host words still error 9, gate16 byte-identity fixtures and old-compiler/mutant negative controls | | Codex (GPT-6.1 Sol) | Repaired qty float-domain classification and checked ingress, diagnostic categories and query/output streaming; permanent adversarial regression fixtures for the #909/#911 audit | +| Claude Opus 5.5 | OSC IR v7 memory oracle: independent decoder, validator and interpreter (`seed/wasm/v7/oracle.js`), separate hand-written fixture writer, hand-written expectations and a mutant-checked self-test | This table starts with the numeric work on this branch. Earlier work in this repository is not yet attributed here; add rows only for work that can be traced to a model. diff --git a/seed/wasm/.crumb b/seed/wasm/.crumb index 1eb21eb57..cbd465995 100644 --- a/seed/wasm/.crumb +++ b/seed/wasm/.crumb @@ -3,14 +3,14 @@ "generated": { "children": [], "children_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - "compiled_at": "2026-10-29T01:41:23Z", + "compiled_at": "2026-10-30T02:45:50Z", "compiler": "crumb-compile/1", - "digest": "sha256:13374eb3a158540a8788cad5749bcca0632f26488365f69f0bd6259d5747f83f", + "digest": "sha256:e75c51ee1a1f93c093532067a285fde4dd6bdc7e357fa5300a3324456df6b9bd", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - "files": 7, - "generated_at_commit": "28a7b749a6da68407af60a7cccaf34dc2e5eab48", - "source_tree": "sha256:5b588dfda2c6e3185ea027fe0b73d0b2ce8e87a3930058be12d6517bda1a3b81" + "files": 44, + "generated_at_commit": "dc3885bd32c7b1a2d1afe99e568bb216e39cdc35", + "source_tree": "sha256:57e541cb5101007e9ff8279f2398579fa60ce956d4d5fb8b8f55c88494951d10" } }, "layer": "seed", diff --git a/seed/wasm/v7/expect/check.txt b/seed/wasm/v7/expect/check.txt new file mode 100644 index 000000000..4db0b5d8f --- /dev/null +++ b/seed/wasm/v7/expect/check.txt @@ -0,0 +1,115 @@ +# fixtureexpected verdict (first two words of `oracle.js check`) +# Hand-written from spec/one-layer-format.md sections 3 and 10, not generated. +bad_magic REFUSE MAGIC +bad_version2 REFUSE VERSION +bad_version6 REFUSE VERSION +bad_version8 REFUSE VERSION +bitcast ACCEPT v7 +cap_fname REFUSE CAPACITY +cap_nfuncs REFUSE CAPACITY +cap_nstructs REFUSE CAPACITY +cap_v1_input REFUSE CAPACITY +cap_v7_input REFUSE CAPACITY +convert ACCEPT v7 +float_ops ACCEPT v7 +gratuitous_v7 REFUSE VERSION +gratuitous_v7_empty REFUSE VERSION +init_diamond ACCEPT v7 +inv_bitcast_bool REFUSE INVALID +inv_bitcast_cross REFUSE INVALID +inv_bitcast_none REFUSE INVALID +inv_bitcast_width REFUSE INVALID +inv_cast_bool REFUSE INVALID +inv_cast_to_bool REFUSE INVALID +inv_class4 REFUSE INVALID +inv_count0 REFUSE INVALID +inv_count_big REFUSE INVALID +inv_dup_func REFUSE INVALID +inv_dup_local REFUSE INVALID +inv_dup_module REFUSE INVALID +inv_export_memory REFUSE INVALID +inv_export_region REFUSE INVALID +inv_f32_const_upper REFUSE INVALID +inv_float_and REFUSE INVALID +inv_float_bnot REFUSE INVALID +inv_float_lnot REFUSE INVALID +inv_float_rem REFUSE INVALID +inv_float_shl REFUSE INVALID +inv_fname_8bit REFUSE INVALID +inv_foreign_init REFUSE INVALID +inv_foreign_load REFUSE INVALID +inv_foreign_store REFUSE INVALID +inv_index_bool REFUSE INVALID +inv_index_float REFUSE INVALID +inv_init_after_use REFUSE INVALID +inv_init_one_branch REFUSE INVALID +inv_loop_carried REFUSE INVALID +inv_missing_init REFUSE INVALID +inv_mod_init REFUSE INVALID +inv_nregions REFUSE INVALID +inv_owner_local_module REFUSE INVALID +inv_owner_missing REFUSE INVALID +inv_owner_module0 REFUSE INVALID +inv_reg_undef REFUSE INVALID +inv_rinit_module REFUSE INVALID +inv_rinit_none REFUSE INVALID +inv_rinit_region_none REFUSE INVALID +inv_rinit_type REFUSE INVALID +inv_rload_dst_none REFUSE INVALID +inv_rload_index_8000 REFUSE INVALID +inv_rload_index_none REFUSE INVALID +inv_rload_region_none REFUSE INVALID +inv_rload_region_oob REFUSE INVALID +inv_rload_type REFUSE INVALID +inv_rname_64 REFUSE INVALID +inv_rname_8bit REFUSE INVALID +inv_rname_dot REFUSE INVALID +inv_rname_empty REFUSE INVALID +inv_rname_nul REFUSE INVALID +inv_rstore_src_fffe REFUSE INVALID +inv_rstore_src_oob REFUSE INVALID +inv_rstore_type REFUSE INVALID +inv_rtag_bool REFUSE INVALID +inv_rtag_cells REFUSE INVALID +inv_shadow REFUSE INVALID +inv_store_before_init REFUSE INVALID +inv_text_init REFUSE INVALID +inv_text_len REFUSE INVALID +inv_text_store REFUSE INVALID +inv_text_u32 REFUSE INVALID +inv_unreachable_use REFUSE INVALID +inv_v1_dup_func REFUSE INVALID +local_fresh ACCEPT v7 +ok_count_max ACCEPT v7 +ok_fname_empty ACCEPT v7 +ok_loop_init_before ACCEPT v7 +ok_own_local ACCEPT v7 +persist ACCEPT v7 +rt_f32 ACCEPT v7 +rt_f64 ACCEPT v7 +rt_i32 ACCEPT v7 +rt_u64 ACCEPT v7 +rt_u8 ACCEPT v7 +text_read ACCEPT v7 +trailing REFUSE TRAILING +trunc_empty REFUSE TRUNCATED +trunc_header REFUSE TRUNCATED +trunc_initdata REFUSE TRUNCATED +trunc_last REFUSE TRUNCATED +trunc_magic REFUSE TRUNCATED +unsup_class3 REFUSE UNSUPPORTED_OP +unsup_op0 REFUSE UNSUPPORTED_OP +unsup_op20 REFUSE UNSUPPORTED_OP +unsup_op34 REFUSE UNSUPPORTED_OP +unsup_op7 REFUSE UNSUPPORTED_OP +unsup_op9 REFUSE UNSUPPORTED_OP +unsup_structs REFUSE UNSUPPORTED_TAG +unsup_tag10 REFUSE UNSUPPORTED_TAG +unsup_tag15 REFUSE UNSUPPORTED_TAG +v1_arith ACCEPT v1 +v1_loop ACCEPT v1 +v1_op30 REFUSE UNSUPPORTED_OP +v1_tag13 REFUSE UNSUPPORTED_TAG +v1_tag14_ret REFUSE UNSUPPORTED_TAG +v1_unsup_op10 REFUSE UNSUPPORTED_OP +v7_input_at_v1_cap REFUSE TRAILING diff --git a/seed/wasm/v7/expect/run/bitcast.out b/seed/wasm/v7/expect/run/bitcast.out new file mode 100644 index 000000000..d37ab94f2 --- /dev/null +++ b/seed/wasm/v7/expect/run/bitcast.out @@ -0,0 +1,10 @@ +f32_u32 = 3212836864 +u32_f32 = 0x3f800000 +i32_f32 = 0xffffffff +f64_i64 = -9223372036854775808 +u64_f64 = 0x7ff8000000000001 +u8_i8 = -56 +i8_u8 = 255 +i16_u16 = 32768 +u16_i16 = -1 +f32_f32 = 0x7f800001 diff --git a/seed/wasm/v7/expect/run/bitcast.script b/seed/wasm/v7/expect/run/bitcast.script new file mode 100644 index 000000000..96e23be7a --- /dev/null +++ b/seed/wasm/v7/expect/run/bitcast.script @@ -0,0 +1,10 @@ +call f32_u32 0xbf800000 +call u32_f32 1065353216 +call i32_f32 -1 +call f64_i64 0x8000000000000000 +call u64_f64 9221120237041090561 +call u8_i8 200 +call i8_u8 -1 +call i16_u16 -32768 +call u16_i16 65535 +call f32_f32 0x7f800001 diff --git a/seed/wasm/v7/expect/run/convert.out b/seed/wasm/v7/expect/run/convert.out new file mode 100644 index 000000000..f3cf7c6b9 --- /dev/null +++ b/seed/wasm/v7/expect/run/convert.out @@ -0,0 +1,48 @@ +f64_i32 = 0 +f64_i32 = 2147483647 +f64_i32 = -2147483648 +f64_i32 = 0 +f64_i32 = 2147483647 +f64_i32 = -2 +f64_i32 = 2147483647 +f64_i32 = -2147483648 +f64_u8 = 0 +f64_u8 = 255 +f64_u8 = 255 +f64_u8 = 0 +f32_i8 = 127 +f32_i8 = -128 +f32_i8 = -128 +f32_i8 = 0 +f64_u64 = 18446744073709551615 +f64_u64 = 0 +f64_u64 = 9223372036854775808 +f64_i64 = 9223372036854775807 +f64_i64 = -9223372036854775808 +f32_u32 = 4294967295 +f32_u32 = 4294967040 +f32_i16 = 0 +u64_f32 = 0x5f000001 +u64_f32 = 0x5f800000 +u64_f32 = 0x4b800000 +u64_f32 = 0x4b800002 +i32_f32 = 0x4b800000 +i32_f32 = 0xcf000000 +i32_f32 = 0xcb800002 +u64_f64 = 0x4340000000000000 +u64_f64 = 0x43f0000000000000 +i64_f64 = 0xc3e0000000000000 +i64_f64 = 0x4340000000000002 +f64_f32 = 0x3f800000 +f64_f32 = 0x3f800002 +f64_f32 = 0x7f800000 +f64_f32 = 0x7f800000 +f64_f32 = 0x80000000 +f32_f64 = 0x3ff0000020000000 +f32_f64 = 0xfff0000000000000 +f64_f64 = 0x8000000000000000 +i32_u8 TRAP code=5 +i32_u8 = 255 +i32_u8 TRAP code=5 +u64_i64 TRAP code=5 +u64_i64 = 9223372036854775807 diff --git a/seed/wasm/v7/expect/run/convert.script b/seed/wasm/v7/expect/run/convert.script new file mode 100644 index 000000000..c8d42f790 --- /dev/null +++ b/seed/wasm/v7/expect/run/convert.script @@ -0,0 +1,48 @@ +call f64_i32 0x7ff8000000000000 +call f64_i32 0x7ff0000000000000 +call f64_i32 0xfff0000000000000 +call f64_i32 0x8000000000000000 +call f64_i32 0x41e65a0bc0000000 +call f64_i32 0xc007333333333333 +call f64_i32 0x41dffffffff9999a +call f64_i32 0xc1e0000000200000 +call f64_u8 0xbff8000000000000 +call f64_u8 0x406ffccccccccccd +call f64_u8 0x4070000000000000 +call f64_u8 0xfff8000000000000 +call f32_i8 0x42ff0000 +call f32_i8 0xc300e666 +call f32_i8 0xc3010000 +call f32_i8 0x7fc00000 +call f64_u64 0x43f0000000000000 +call f64_u64 0xfff0000000000000 +call f64_u64 0x43e0000000000000 +call f64_i64 0x43e0000000000000 +call f64_i64 0xc3e0000000000000 +call f32_u32 0x4f800000 +call f32_u32 0x4f7fffff +call f32_i16 0x80000000 +call u64_f32 9223372586610589697 +call u64_f32 18446744073709551615 +call u64_f32 16777217 +call u64_f32 16777219 +call i32_f32 16777217 +call i32_f32 -2147483648 +call i32_f32 -16777219 +call u64_f64 9007199254740993 +call u64_f64 18446744073709551615 +call i64_f64 -9223372036854775808 +call i64_f64 9007199254740995 +call f64_f32 0x3ff0000010000000 +call f64_f32 0x3ff0000030000000 +call f64_f32 0x7ff0000000000000 +call f64_f32 0x48078287f49c4a1d +call f64_f32 0x8000000000000000 +call f32_f64 0x3f800001 +call f32_f64 0xff800000 +call f64_f64 0x8000000000000000 +call i32_u8 300 +call i32_u8 255 +call i32_u8 -1 +call u64_i64 9223372036854775808 +call u64_i64 9223372036854775807 diff --git a/seed/wasm/v7/expect/run/float_ops.out b/seed/wasm/v7/expect/run/float_ops.out new file mode 100644 index 000000000..9a40ea2e3 --- /dev/null +++ b/seed/wasm/v7/expect/run/float_ops.out @@ -0,0 +1,19 @@ +fadd = 0x3f800000 +fadd = 0x7f800000 +fmul = 0x40c00000 +dsub = 0x0000000000000000 +ddiv = 0x7ff0000000000000 +ddiv = 0xfff0000000000000 +ddiv = 0x3fd5555555555555 +fneg = 0xffc00001 +fneg = 0x80000000 +dneg = 0x0000000000000000 +feq = 0 +fne = 1 +flt = 0 +feq = 1 +flt = 0 +dge = 1 +kf = 0x7fa00001 +kd = 0xfff0000000000001 +fneg = 0xffa00001 diff --git a/seed/wasm/v7/expect/run/float_ops.script b/seed/wasm/v7/expect/run/float_ops.script new file mode 100644 index 000000000..85775c1bf --- /dev/null +++ b/seed/wasm/v7/expect/run/float_ops.script @@ -0,0 +1,19 @@ +call fadd 0x3f800000 0x33800000 +call fadd 0x7f7fffff 0x7f7fffff +call fmul 0x40000000 0x40400000 +call dsub 0x3ff0000000000000 0x3ff0000000000000 +call ddiv 0x3ff0000000000000 0x0000000000000000 +call ddiv 0xbff0000000000000 0x0000000000000000 +call ddiv 0x3ff0000000000000 0x4008000000000000 +call fneg 0x7fc00001 +call fneg 0x00000000 +call dneg 0x8000000000000000 +call feq 0x7fc00000 0x7fc00000 +call fne 0x7fc00000 0x7fc00000 +call flt 0x7fc00000 0x3f800000 +call feq 0x00000000 0x80000000 +call flt 0x80000000 0x00000000 +call dge 0x4000000000000000 0x3ff0000000000000 +call kf +call kd +call fneg 0x7fa00001 diff --git a/seed/wasm/v7/expect/run/init_diamond.out b/seed/wasm/v7/expect/run/init_diamond.out new file mode 100644 index 000000000..87edde9fd --- /dev/null +++ b/seed/wasm/v7/expect/run/init_diamond.out @@ -0,0 +1,2 @@ +pick = -3 +pick = 4 diff --git a/seed/wasm/v7/expect/run/init_diamond.script b/seed/wasm/v7/expect/run/init_diamond.script new file mode 100644 index 000000000..00284831f --- /dev/null +++ b/seed/wasm/v7/expect/run/init_diamond.script @@ -0,0 +1,2 @@ +call pick 1 +call pick 0 diff --git a/seed/wasm/v7/expect/run/local_fresh.out b/seed/wasm/v7/expect/run/local_fresh.out new file mode 100644 index 000000000..a1ac3c5fd --- /dev/null +++ b/seed/wasm/v7/expect/run/local_fresh.out @@ -0,0 +1,5 @@ +swap = 5 +swap = 5 +swap = 5 +swap TRAP code=3 +reinit = 7 diff --git a/seed/wasm/v7/expect/run/local_fresh.script b/seed/wasm/v7/expect/run/local_fresh.script new file mode 100644 index 000000000..941d76ddc --- /dev/null +++ b/seed/wasm/v7/expect/run/local_fresh.script @@ -0,0 +1,5 @@ +call swap 0 11 +call swap 0 12 +call swap 3 1 +call swap 4 1 +call reinit 0 diff --git a/seed/wasm/v7/expect/run/ok_loop_init_before.out b/seed/wasm/v7/expect/run/ok_loop_init_before.out new file mode 100644 index 000000000..dc5f63fce --- /dev/null +++ b/seed/wasm/v7/expect/run/ok_loop_init_before.out @@ -0,0 +1,2 @@ +f = 0 +f = 5 diff --git a/seed/wasm/v7/expect/run/ok_loop_init_before.script b/seed/wasm/v7/expect/run/ok_loop_init_before.script new file mode 100644 index 000000000..314790397 --- /dev/null +++ b/seed/wasm/v7/expect/run/ok_loop_init_before.script @@ -0,0 +1,2 @@ +call f 0 +call f 5 diff --git a/seed/wasm/v7/expect/run/ok_own_local.out b/seed/wasm/v7/expect/run/ok_own_local.out new file mode 100644 index 000000000..de6da9d4d --- /dev/null +++ b/seed/wasm/v7/expect/run/ok_own_local.out @@ -0,0 +1,3 @@ +a = 9 +b = 0 +b TRAP code=3 diff --git a/seed/wasm/v7/expect/run/ok_own_local.script b/seed/wasm/v7/expect/run/ok_own_local.script new file mode 100644 index 000000000..f82c2c033 --- /dev/null +++ b/seed/wasm/v7/expect/run/ok_own_local.script @@ -0,0 +1,3 @@ +call a 9 +call b 0 +call b 2 diff --git a/seed/wasm/v7/expect/run/persist.out b/seed/wasm/v7/expect/run/persist.out new file mode 100644 index 000000000..2ab31dd45 --- /dev/null +++ b/seed/wasm/v7/expect/run/persist.out @@ -0,0 +1,10 @@ +bump = 1 +bump = 2 +partial TRAP code=3 +bump = 4 +region c 04000000 +ovf TRAP code=1 +region c 04000000 +ovf = 77 +region c 4d000000 +bump = 78 diff --git a/seed/wasm/v7/expect/run/persist.plain b/seed/wasm/v7/expect/run/persist.plain new file mode 100644 index 000000000..0132d5e56 --- /dev/null +++ b/seed/wasm/v7/expect/run/persist.plain @@ -0,0 +1,10 @@ +bump = 1 +bump = 2 +partial TRAP +bump = 4 +region c 04000000 +ovf TRAP +region c 04000000 +ovf = 77 +region c 4d000000 +bump = 78 diff --git a/seed/wasm/v7/expect/run/persist.script b/seed/wasm/v7/expect/run/persist.script new file mode 100644 index 000000000..2532bab39 --- /dev/null +++ b/seed/wasm/v7/expect/run/persist.script @@ -0,0 +1,12 @@ +call bump +call bump +# writes 3 then traps reading index 1: the write persists +call partial +call bump +region c +# overflow trap before the store: nothing written +call ovf 2147483647 +region c +call ovf 1 +region c +call bump diff --git a/seed/wasm/v7/expect/run/rt_f32.out b/seed/wasm/v7/expect/run/rt_f32.out new file mode 100644 index 000000000..7f88e464e --- /dev/null +++ b/seed/wasm/v7/expect/run/rt_f32.out @@ -0,0 +1,6 @@ +set = void +set = void +get = 0x7fa00001 +get = 0x80000000 +get TRAP code=3 +region g 0100a07f00000080 diff --git a/seed/wasm/v7/expect/run/rt_f32.script b/seed/wasm/v7/expect/run/rt_f32.script new file mode 100644 index 000000000..b8f70c3e8 --- /dev/null +++ b/seed/wasm/v7/expect/run/rt_f32.script @@ -0,0 +1,7 @@ +# signaling-NaN payload and -0 survive RSTORE/RLOAD bit-exactly +call set 0 0x7fa00001 +call set 1 0x80000000 +call get 0 +call get 1 +call get 2 +region g diff --git a/seed/wasm/v7/expect/run/rt_f64.out b/seed/wasm/v7/expect/run/rt_f64.out new file mode 100644 index 000000000..64e8867a0 --- /dev/null +++ b/seed/wasm/v7/expect/run/rt_f64.out @@ -0,0 +1,6 @@ +set = void +set = void +get = 0xfff8000000000123 +get = 0x3ff0000000000000 +get TRAP code=3 +region g 000000000000f03f230100000000f8ff diff --git a/seed/wasm/v7/expect/run/rt_f64.script b/seed/wasm/v7/expect/run/rt_f64.script new file mode 100644 index 000000000..8fea223da --- /dev/null +++ b/seed/wasm/v7/expect/run/rt_f64.script @@ -0,0 +1,6 @@ +call set 1 0xfff8000000000123 +call set 0 0x3ff0000000000000 +call get 1 +call get 0 +call get -1 +region g diff --git a/seed/wasm/v7/expect/run/rt_i32.out b/seed/wasm/v7/expect/run/rt_i32.out new file mode 100644 index 000000000..66fac4df0 --- /dev/null +++ b/seed/wasm/v7/expect/run/rt_i32.out @@ -0,0 +1,6 @@ +set = void +set = void +get = -2 +get = 2147483647 +set TRAP code=3 +region g feffffff00000000ffffff7f diff --git a/seed/wasm/v7/expect/run/rt_i32.script b/seed/wasm/v7/expect/run/rt_i32.script new file mode 100644 index 000000000..92a074378 --- /dev/null +++ b/seed/wasm/v7/expect/run/rt_i32.script @@ -0,0 +1,6 @@ +call set 0 -2 +call set 2 2147483647 +call get 0 +call get 2 +call set 3 1 +region g diff --git a/seed/wasm/v7/expect/run/rt_u64.out b/seed/wasm/v7/expect/run/rt_u64.out new file mode 100644 index 000000000..a662cfbfe --- /dev/null +++ b/seed/wasm/v7/expect/run/rt_u64.out @@ -0,0 +1,6 @@ +set = void +get = 18446744073709551615 +geti TRAP code=3 +geti = 18446744073709551615 +geti TRAP code=3 +region g 0000000000000000ffffffffffffffff diff --git a/seed/wasm/v7/expect/run/rt_u64.script b/seed/wasm/v7/expect/run/rt_u64.script new file mode 100644 index 000000000..15cf53267 --- /dev/null +++ b/seed/wasm/v7/expect/run/rt_u64.script @@ -0,0 +1,6 @@ +call set 1 18446744073709551615 +call get 1 +call geti -1 +call geti 1 +call geti 2 +region g diff --git a/seed/wasm/v7/expect/run/rt_u8.out b/seed/wasm/v7/expect/run/rt_u8.out new file mode 100644 index 000000000..77ca86829 --- /dev/null +++ b/seed/wasm/v7/expect/run/rt_u8.out @@ -0,0 +1,8 @@ +set = void +set = void +get = 255 +get = 7 +get TRAP code=3 +set TRAP code=3 +get = 0 +region g ff000007 diff --git a/seed/wasm/v7/expect/run/rt_u8.script b/seed/wasm/v7/expect/run/rt_u8.script new file mode 100644 index 000000000..9c7a039a6 --- /dev/null +++ b/seed/wasm/v7/expect/run/rt_u8.script @@ -0,0 +1,8 @@ +call set 0 255 +call set 3 7 +call get 0 +call get 3 +call get 4 +call set -1 1 +call get 1 +region g diff --git a/seed/wasm/v7/expect/run/text_read.out b/seed/wasm/v7/expect/run/text_read.out new file mode 100644 index 000000000..7002c5fe5 --- /dev/null +++ b/seed/wasm/v7/expect/run/text_read.out @@ -0,0 +1,6 @@ +tget = 82 +tget = 88 +tget TRAP code=3 +eget TRAP code=3 +region t 52474258 +region e diff --git a/seed/wasm/v7/expect/run/text_read.script b/seed/wasm/v7/expect/run/text_read.script new file mode 100644 index 000000000..2ae214957 --- /dev/null +++ b/seed/wasm/v7/expect/run/text_read.script @@ -0,0 +1,6 @@ +call tget 0 +call tget 3 +call tget 4 +call eget 0 +region t +region e diff --git a/seed/wasm/v7/expect/run/v1_arith.out b/seed/wasm/v7/expect/run/v1_arith.out new file mode 100644 index 000000000..6ca88881e --- /dev/null +++ b/seed/wasm/v7/expect/run/v1_arith.out @@ -0,0 +1,14 @@ +addi TRAP code=1 +addi = -2 +addu = 44 +divi TRAP code=1 +remi = 0 +divi TRAP code=2 +remi = -1 +shl8 TRAP code=1 +shl8 = -128 +shl8 TRAP code=7 +shr8 = -1 +negu = 65535 +memory = 1 +stop TRAP code=9 diff --git a/seed/wasm/v7/expect/run/v1_arith.script b/seed/wasm/v7/expect/run/v1_arith.script new file mode 100644 index 000000000..d012343dc --- /dev/null +++ b/seed/wasm/v7/expect/run/v1_arith.script @@ -0,0 +1,14 @@ +call addi 2147483647 1 +call addi -5 3 +call addu 200 100 +call divi -9223372036854775808 -1 +call remi -9223372036854775808 -1 +call divi 7 0 +call remi -7 2 +call shl8 64 1 +call shl8 -64 1 +call shl8 1 8 +call shr8 -128 7 +call negu 1 +call memory 0 +call stop diff --git a/seed/wasm/v7/expect/run/v1_loop.out b/seed/wasm/v7/expect/run/v1_loop.out new file mode 100644 index 000000000..9dd93b81e --- /dev/null +++ b/seed/wasm/v7/expect/run/v1_loop.out @@ -0,0 +1,4 @@ +sum = 45 +twice = 90 +sum = 0 +sum = 704982704 diff --git a/seed/wasm/v7/expect/run/v1_loop.script b/seed/wasm/v7/expect/run/v1_loop.script new file mode 100644 index 000000000..7eafbc376 --- /dev/null +++ b/seed/wasm/v7/expect/run/v1_loop.script @@ -0,0 +1,4 @@ +call sum 10 +call twice 10 +call sum 0 +call sum 100000 diff --git a/seed/wasm/v7/oracle.js b/seed/wasm/v7/oracle.js new file mode 100755 index 000000000..6ef554f9e --- /dev/null +++ b/seed/wasm/v7/oracle.js @@ -0,0 +1,773 @@ +#!/usr/bin/env node +// SPDX-License-Identifier: Apache-2.0 +// Independent read-only oracle for OSC IR wire versions 1 and 7. +// +// Written from spec/one-layer-format.md sections 3..7 and 10 and +// docs/one_layer/OMEGESE.md 11.1/11.2 only. It shares no code with +// seed/wasm/irwasm.om, seed/wasm/oracle.js, seed/omc/omc.om, the C IR tools, +// or seed/wasm/v7/writer.js (the fixture writer). It is a referee: it never +// produces IR or wasm. +// +// node oracle.js check UNIT -> ACCEPT v1|v7 / REFUSE CLASS detail +// node oracle.js run [--codes] UNIT SCRIPT +// +// Exit: check 0 accept, 1 refuse, 2 usage. run 0 done, 1 unit refused, +// 2 usage/script error, 3 oracle resource limit (not an Omega result). +'use strict'; + +const fs = require('fs'); + +const MAGIC = [0x4f, 0x53, 0x43, 0x31, 0x49, 0x52, 0x00]; +const V1_INPUT_MAX = 2620251; +const V7_INPUT_MAX = 8388608; +const MAX_FUNCS = 32; +const MAX_PARAMS = 6; +const MAX_VREGS = 480; +const MAX_BLOCKS = 512; +const MAX_INSNS = 4096; +const MAX_STRUCTS = 16; +const MAX_REGIONS = 1024; +const MAX_COUNT = 16777216; +const NAME_MAX = 63; +const CLAUSE_MAX = 255; +const NONE = 0xffff; +const STEP_LIMIT = 200000000; + +// Scalar wire tags. +const T_VOID = 0, T_BOOL = 1, T_F32 = 13, T_F64 = 14; +const isInt = (t) => t >= 2 && t <= 9; +const isFloat = (t) => t === T_F32 || t === T_F64; +const isValue = (t) => t === T_BOOL || isInt(t) || isFloat(t); +const isSigned = (t) => t >= 6 && t <= 9; +const intWidth = (t) => [0, 1, 8, 16, 32, 64, 8, 16, 32, 64][t]; +const elemBytes = (t) => (t === T_F32 ? 4 : t === T_F64 ? 8 : intWidth(t) / 8); +const BITCAST_SET = { 2: 0, 6: 0, 3: 1, 7: 1, 4: 2, 8: 2, 13: 2, 5: 3, 9: 3, 14: 3 }; + +// Opcodes. +const OP = { + CONST: 1, MOV: 2, BIN: 3, UN: 4, CMP: 5, CAST: 6, LOAD: 9, STORE: 10, + CALL: 11, TRAP: 12, BR: 13, CBR: 14, RET: 15, + RLOAD: 30, RSTORE: 31, BITCAST: 32, RINIT: 33, +}; +const isTerm = (op) => op === OP.BR || op === OP.CBR || op === OP.RET; + +class Refusal extends Error { + constructor(cls, detail) { + super(cls + ' ' + detail); + this.cls = cls; + this.detail = detail; + } +} +const refuse = (cls, detail) => { throw new Refusal(cls, detail); }; + +// ---------------------------------------------------------------- reader +class Reader { + constructor(buf) { this.buf = buf; this.pos = 0; } + need(n, what) { + if (this.pos + n > this.buf.length) refuse('TRUNCATED', what + ' at byte ' + this.pos); + } + u8(what) { this.need(1, what); return this.buf[this.pos++]; } + u16(what) { this.need(2, what); const v = this.buf[this.pos] | (this.buf[this.pos + 1] << 8); this.pos += 2; return v; } + u32(what) { this.need(4, what); const v = this.buf.readUInt32LE(this.pos); this.pos += 4; return v; } + u64(what) { this.need(8, what); const v = this.buf.readBigUInt64LE(this.pos); this.pos += 8; return v; } + bytes(n, what) { this.need(n, what); const b = this.buf.subarray(this.pos, this.pos + n); this.pos += n; return b; } +} + +function readTag(r, v7, what) { + const t = r.u8(what); + if (t <= 9) return t; + if ((t === T_F32 || t === T_F64) && v7) return t; + refuse('UNSUPPORTED_TAG', what + ' tag ' + t); +} + +// Register-like u16 field. 0x8000..0xFFFE never designates anything (spec 3.5 +// RANGE; 10.1 INVALID). 0xFFFF is the "none" sentinel, checked per use. +function readReg(r, what) { + const v = r.u16(what); + if (v >= 0x8000 && v !== NONE) refuse('INVALID', what + ' operand 0x' + v.toString(16)); + return v; +} + +function readFuncText(r, lenBytes, max, what) { + const n = lenBytes === 1 ? r.u8(what + ' length') : r.u16(what + ' length'); + if (n > max) refuse('CAPACITY', what + ' length ' + n); + const b = r.bytes(n, what); + for (const c of b) if (c === 0) refuse('INVALID', what + ' contains NUL'); + return b; +} + +function readRegion(r, i) { + const what = 'region ' + i; + const n = r.u8(what + ' name length'); + if (n === 0 || n > NAME_MAX) refuse('INVALID', what + ' name length ' + n); + const nb = r.bytes(n, what + ' name'); + for (const c of nb) { + if (c === 0 || c >= 0x80 || c === 0x2e) refuse('INVALID', what + ' name byte 0x' + c.toString(16)); + } + const name = nb.toString('latin1'); + const tag = r.u8(what + ' element'); + if (!(isInt(tag) || isFloat(tag))) refuse('INVALID', what + ' element tag ' + tag); + const cls = r.u8(what + ' class'); + if (cls === 3) refuse('UNSUPPORTED_OP', what + ' class 3 (workgroup)'); + if (cls > 3) refuse('INVALID', what + ' class ' + cls); + const owner = r.u16(what + ' owner'); + const count = r.u32(what + ' count'); + if (count > MAX_COUNT) refuse('INVALID', what + ' count ' + count); + if (cls !== 2 && count === 0) refuse('INVALID', what + ' mutable count 0'); + const initlen = r.u32(what + ' initial length'); + if (cls !== 2 && initlen !== 0) refuse('INVALID', what + ' initial data on class ' + cls); + if (cls === 2 && tag !== 2) refuse('INVALID', what + ' text element tag ' + tag); + if (cls === 2 && initlen !== count) refuse('INVALID', what + ' text length ' + initlen + ' != count ' + count); + const init = Buffer.from(r.bytes(initlen, what + ' initial data')); + return { name, tag, cls, owner, count, init }; +} + +function readInsn(r, v7, what) { + const op = r.u8(what + ' opcode'); + const ins = { op, sub: 0, dst: NONE, a: NONE, b: NONE, c: NONE, imm: 0n, args: [] }; + const reg = (k) => readReg(r, what + ' ' + k); + switch (op) { + case OP.CONST: ins.dst = reg('dst'); ins.imm = r.u64(what + ' imm'); break; + case OP.MOV: case OP.CAST: ins.dst = reg('dst'); ins.a = reg('a'); break; + case OP.BIN: case OP.CMP: + ins.sub = r.u8(what + ' sub'); ins.dst = reg('dst'); ins.a = reg('a'); ins.b = reg('b'); break; + case OP.UN: ins.sub = r.u8(what + ' sub'); ins.dst = reg('dst'); ins.a = reg('a'); break; + // LOAD/STORE (9/10) need a reference register, which this profile has not. + case OP.CALL: { + ins.dst = reg('dst'); ins.callee = reg('callee'); + const n = r.u8(what + ' nargs'); + if (n > MAX_PARAMS) refuse('CAPACITY', what + ' nargs ' + n); + for (let k = 0; k < n; k++) ins.args.push(reg('arg' + k)); + break; + } + case OP.TRAP: ins.imm = BigInt(r.u8(what + ' trap code')); break; + case OP.BR: ins.blkT = reg('target'); break; + case OP.CBR: ins.a = reg('cond'); ins.blkT = reg('true target'); ins.blkF = reg('false target'); break; + case OP.RET: ins.a = reg('a'); break; + default: + if (op >= 30 && op <= 33 && v7) { + if (op === OP.RLOAD) { ins.dst = reg('dst'); ins.rg = reg('region'); ins.b = reg('index'); } + else if (op === OP.RSTORE) { ins.rg = reg('region'); ins.b = reg('index'); ins.c = reg('src'); } + else if (op === OP.BITCAST) { ins.dst = reg('dst'); ins.a = reg('src'); } + else { ins.rg = reg('region'); ins.a = reg('init'); } + break; + } + refuse('UNSUPPORTED_OP', what + ' opcode ' + op); + } + return ins; +} + +function readFunc(r, v7, fi) { + const what = 'function ' + fi; + const nameBytes = readFuncText(r, 1, NAME_MAX, what + ' name'); + for (const c of nameBytes) if (c >= 0x80) refuse('INVALID', what + ' name byte 0x' + c.toString(16)); + const name = nameBytes.toString('latin1'); + const nparams = r.u8(what + ' nparams'); + const ret = readTag(r, v7, what + ' return'); + const nvregs = r.u16(what + ' nvregs'); + if (nvregs > MAX_VREGS) refuse('CAPACITY', what + ' nvregs ' + nvregs); + const vtype = []; + for (let k = 0; k < nvregs; k++) vtype.push(readTag(r, v7, what + ' register ' + k)); + const nblocks = r.u16(what + ' nblocks'); + if (nblocks > MAX_BLOCKS) refuse('CAPACITY', what + ' nblocks ' + nblocks); + const blocks = []; + for (let k = 0; k < nblocks; k++) blocks.push({ first: r.u32(what + ' block first'), count: r.u32(what + ' block count') }); + const ninsns = r.u32(what + ' ninsns'); + if (ninsns > MAX_INSNS) refuse('CAPACITY', what + ' ninsns ' + ninsns); + const insns = []; + for (let k = 0; k < ninsns; k++) insns.push(readInsn(r, v7, what + ' insn ' + k)); + readFuncText(r, 2, CLAUSE_MAX, what + ' requires'); + readFuncText(r, 2, CLAUSE_MAX, what + ' ensures'); + return { name, nameBytes, nparams, ret, nvregs, vtype, blocks, insns }; +} + +// ------------------------------------------------------------ validation +function canonicalConst(t, imm) { + if (t === T_BOOL) return imm === 0n || imm === 1n; + if (t === T_F64) return true; + if (t === T_F32) return imm >> 32n === 0n; // M:f32upper + const w = BigInt(intWidth(t)); + if (isSigned(t)) return BigInt.asUintN(64, BigInt.asIntN(Number(w), imm)) === imm; + return imm >> w === 0n; +} + +function validateFunc(u, fi) { + const f = u.funcs[fi]; + const bad = (m) => refuse('INVALID', 'function ' + fi + ' (' + f.name + '): ' + m); + if (f.nparams > MAX_PARAMS) bad('nparams ' + f.nparams); + if (f.nparams > f.nvregs) bad('nparams exceeds nvregs'); + if (!(f.ret === T_VOID || isValue(f.ret))) bad('return tag ' + f.ret); + f.vtype.forEach((t, k) => { if (!isValue(t)) bad('register ' + k + ' tag ' + t); }); + if (f.blocks.length === 0 || f.insns.length === 0) bad('empty body'); + let next = 0; + f.blocks.forEach((b, k) => { + if (b.first !== next || b.count === 0) bad('block ' + k + ' does not partition'); + next = b.first + b.count; + if (next > f.insns.length) bad('block ' + k + ' past end'); + }); + if (next !== f.insns.length) bad('blocks do not cover instructions'); + f.blocks.forEach((b, k) => { + for (let j = b.first; j < b.first + b.count; j++) { + const last = j === b.first + b.count - 1; + if (isTerm(f.insns[j].op) !== last) bad('block ' + k + ' terminator placement'); + } + }); + + const ty = (r, k) => { + if (r === NONE || r >= f.nvregs) bad(k + ' register ' + r + ' outside table'); + return f.vtype[r]; + }; + const region = (g, k) => { + if (g === NONE || g >= u.regions.length) bad(k + 'region ' + g + ' outside table'); + const rg = u.regions[g]; + if (rg.cls === 1 && rg.owner !== fi) bad(k + 'region ' + rg.name + ' owned by function ' + rg.owner); // M:owner + return rg; + }; + const blk = (b) => { if (b === NONE || b >= f.blocks.length) bad('branch target ' + b); }; + + f.insns.forEach((ins, j) => { + const at = 'insn ' + j + ' '; + switch (ins.op) { + case OP.CONST: if (!canonicalConst(ty(ins.dst, at + 'dst'), ins.imm)) bad(at + 'non-canonical constant'); break; + case OP.MOV: if (ty(ins.dst, at + 'dst') !== ty(ins.a, at + 'a')) bad(at + 'MOV type'); break; + case OP.BIN: { + const t = ty(ins.dst, at + 'dst'); + if (ty(ins.a, at + 'a') !== t) bad(at + 'BIN a type'); + const tb = ty(ins.b, at + 'b'); + const s = ins.sub; + if (s < 1 || s > 10) bad(at + 'BIN sub ' + s); + if (isFloat(t)) { if (s > 4 || tb !== t) bad(at + 'float BIN sub ' + s); } + else if (t === T_BOOL) { if (s < 6 || s > 8 || tb !== t) bad(at + 'bool BIN sub ' + s); } + else if (s >= 9) { if (!isInt(tb)) bad(at + 'shift amount type'); } + else if (tb !== t) bad(at + 'BIN b type'); + break; + } + case OP.UN: { + const t = ty(ins.dst, at + 'dst'); + if (ty(ins.a, at + 'a') !== t) bad(at + 'UN type'); + const ok = ins.sub === 1 ? isInt(t) || isFloat(t) : ins.sub === 2 ? isInt(t) : ins.sub === 3 ? t === T_BOOL : false; + if (!ok) bad(at + 'UN sub ' + ins.sub + ' on tag ' + t); + break; + } + case OP.CMP: + if (ins.sub < 1 || ins.sub > 6) bad(at + 'CMP sub ' + ins.sub); + if (ty(ins.dst, at + 'dst') !== T_BOOL) bad(at + 'CMP dst not bool'); + if (ty(ins.a, at + 'a') !== ty(ins.b, at + 'b')) bad(at + 'CMP operand types'); + break; + case OP.CAST: { + const d = ty(ins.dst, at + 'dst'), s = ty(ins.a, at + 'a'); + if (!(isInt(d) || isFloat(d)) || !(isInt(s) || isFloat(s))) bad(at + 'CAST types'); + break; + } + case OP.CALL: { + if (ins.callee === NONE || ins.callee >= fi) bad(at + 'callee ' + ins.callee + ' is not an earlier function'); + const g = u.funcs[ins.callee]; + if (ins.args.length !== g.nparams) bad(at + 'CALL arity'); + ins.args.forEach((a, k) => { if (ty(a, at + 'arg') !== g.vtype[k]) bad(at + 'CALL arg ' + k + ' type'); }); + if (g.ret === T_VOID) { if (ins.dst !== NONE) bad(at + 'void call with destination'); } + else if (ty(ins.dst, at + 'dst') !== g.ret) bad(at + 'CALL result type'); + break; + } + case OP.TRAP: if (ins.imm < 1n || ins.imm > 14n) bad(at + 'trap code ' + ins.imm); break; + case OP.BR: blk(ins.blkT); break; + case OP.CBR: + if (ty(ins.a, at + 'cond') !== T_BOOL) bad(at + 'CBR condition not bool'); + blk(ins.blkT); blk(ins.blkF); + break; + case OP.RET: + if (f.ret === T_VOID) { if (ins.a !== NONE) bad(at + 'value returned from void'); } + else if (ty(ins.a, at + 'a') !== f.ret) bad(at + 'RET type'); + break; + case OP.RLOAD: { + const t = ty(ins.dst, at + 'dst'); + const rg = region(ins.rg, at); + if (!isInt(ty(ins.b, at + 'index'))) bad(at + 'index type'); + if (t !== rg.tag) bad(at + 'RLOAD element type'); + break; + } + case OP.RSTORE: { + const rg = region(ins.rg, at); + if (rg.cls === 2) bad(at + 'RSTORE into read-only text ' + rg.name); // M:textro + if (!isInt(ty(ins.b, at + 'index'))) bad(at + 'index type'); + if (ty(ins.c, at + 'src') !== rg.tag) bad(at + 'RSTORE element type'); + break; + } + case OP.BITCAST: { + const d = ty(ins.dst, at + 'dst'), s = ty(ins.a, at + 'src'); + if (BITCAST_SET[d] === undefined || BITCAST_SET[d] !== BITCAST_SET[s]) bad(at + 'BITCAST ' + s + '->' + d); + break; + } + case OP.RINIT: { + const rg = region(ins.rg, at); + if (rg.cls !== 1) bad(at + 'RINIT of non-local region ' + rg.name); + if (ty(ins.a, at + 'init') !== rg.tag) bad(at + 'RINIT type'); + break; + } + default: bad(at + 'opcode ' + ins.op); + } + }); + definiteAssignment(u, f, fi, bad); +} + +// Uses and definitions of one instruction (registers only). +function usesOf(ins) { + switch (ins.op) { + case OP.MOV: case OP.UN: case OP.CAST: case OP.BITCAST: case OP.RET: case OP.CBR: case OP.RINIT: + return ins.a === NONE ? [] : [ins.a]; + case OP.BIN: case OP.CMP: return [ins.a, ins.b]; + case OP.CALL: return ins.args; + case OP.RLOAD: return [ins.b]; + case OP.RSTORE: return [ins.b, ins.c]; + default: return []; + } +} +function defOf(ins) { + switch (ins.op) { + case OP.CONST: case OP.MOV: case OP.BIN: case OP.UN: case OP.CMP: case OP.CAST: + case OP.RLOAD: case OP.BITCAST: case OP.CALL: + return ins.dst === NONE ? -1 : ins.dst; + default: return -1; + } +} + +// Greatest-fixed-point must-analysis over registers and local-region +// initialization bits (spec 10.2). State vector: nvregs register bits, then +// one bit per region (only class-1 regions owned by this function matter). +function definiteAssignment(u, f, fi, bad) { + const nb = f.blocks.length, nr = f.nvregs, ng = u.regions.length, n = nr + ng; + const entry = new Uint8Array(n); + for (let k = 0; k < f.nparams; k++) entry[k] = 1; + const succ = f.blocks.map((b) => { + const t = f.insns[b.first + b.count - 1]; + return t.op === OP.BR ? [t.blkT] : t.op === OP.CBR ? [t.blkT, t.blkF] : []; + }); + const reach = new Uint8Array(nb); + const stack = [0]; + reach[0] = 1; + while (stack.length) { + for (const s of succ[stack.pop()]) if (!reach[s]) { reach[s] = 1; stack.push(s); } + } + const preds = Array.from({ length: nb }, () => []); + succ.forEach((ss, b) => { if (reach[b]) for (const s of ss) preds[s].push(b); }); + + const transfer = (b, inState, check) => { + const st = Uint8Array.from(inState); + const blk = f.blocks[b]; + for (let j = blk.first; j < blk.first + blk.count; j++) { + const ins = f.insns[j]; + if (check) { + for (const r of usesOf(ins)) if (!st[r]) bad('insn ' + j + ' uses register ' + r + ' before definite assignment'); + if ((ins.op === OP.RLOAD || ins.op === OP.RSTORE) && u.regions[ins.rg].cls === 1 && !st[nr + ins.rg]) { // M:initcheck + bad('insn ' + j + ' accesses local region ' + u.regions[ins.rg].name + ' before RINIT on every path'); + } + } + const d = defOf(ins); + if (d >= 0) st[d] = 1; + if (ins.op === OP.RINIT) st[nr + ins.rg] = 1; + } + return st; + }; + + // Reachable blocks start at TOP (everything assigned) and only shrink. + const out = f.blocks.map((_, b) => (reach[b] ? new Uint8Array(n).fill(1) : null)); + const inOf = (b) => { + if (!reach[b]) return entry; // unreachable: validated from the entry state + const st = b === 0 ? Uint8Array.from(entry) : new Uint8Array(n).fill(1); + for (const p of preds[b]) { + const o = out[p]; + for (let k = 0; k < n; k++) st[k] &= o[k]; // M:meet + } + return st; + }; + let changed = true; + while (changed) { + changed = false; + for (let b = 0; b < nb; b++) { + if (!reach[b]) continue; + const o = transfer(b, inOf(b), false); + for (let k = 0; k < n; k++) if (o[k] !== out[b][k]) { out[b] = o; changed = true; break; } + } + } + for (let b = 0; b < nb; b++) transfer(b, inOf(b), true); +} + +function validateUnit(u) { + const bad = (m) => refuse('INVALID', m); + const module = new Set(); + u.regions.forEach((rg, i) => { + if (rg.cls === 1) { + if (rg.owner === NONE || rg.owner >= u.funcs.length) bad('region ' + i + ' local owner ' + rg.owner); + } else { + if (rg.owner !== NONE) bad('region ' + i + ' module owner ' + rg.owner); + if (module.has(rg.name)) bad('duplicate module region ' + rg.name); + module.add(rg.name); + } + }); + const locals = new Set(); + u.regions.forEach((rg) => { + if (rg.cls !== 1) return; + if (module.has(rg.name)) bad('local region ' + rg.name + ' shadows a module region'); + const key = rg.owner + '/' + rg.name; + if (locals.has(key)) bad('duplicate local region ' + rg.name + ' in function ' + rg.owner); + locals.add(key); + }); + const exportsSeen = new Set(); + const exp = (name) => { if (exportsSeen.has(name)) bad('export name collision ' + JSON.stringify(name)); exportsSeen.add(name); }; // M:export + if (u.version === 7) { + exp('memory'); + u.regions.forEach((rg) => { if (rg.cls !== 1) exp('region.' + rg.name); }); + } + u.funcs.forEach((f) => exp(f.name)); +} + +function usesV7(u) { + if (u.regions.length > 0) return true; + for (const f of u.funcs) { + if (isFloat(f.ret) || f.vtype.some(isFloat)) return true; + if (f.insns.some((i) => i.op >= 30 && i.op <= 33)) return true; + } + return false; +} + +function decode(buf) { + if (buf.length > V7_INPUT_MAX) refuse('CAPACITY', 'input ' + buf.length + ' bytes'); + const headIsV7 = buf.length >= 8 && MAGIC.every((m, i) => buf[i] === m) && buf[7] === 7; + if (buf.length > V1_INPUT_MAX && !headIsV7) refuse('CAPACITY', 'input ' + buf.length + ' bytes'); + const r = new Reader(buf); + for (let i = 0; i < 7; i++) if (r.u8('magic') !== MAGIC[i]) refuse('MAGIC', 'byte ' + i); + const version = r.u8('version'); + if (version !== 1 && version !== 7) refuse('VERSION', 'version ' + version); + const v7 = version === 7; + const u = { version, regions: [], funcs: [] }; + if (v7) { + const ns = r.u8('nstructs'); + if (ns > MAX_STRUCTS) refuse('CAPACITY', 'nstructs ' + ns); + if (ns > 0) refuse('UNSUPPORTED_TAG', 'struct table (' + ns + ' structs)'); + const ng = r.u16('nregions'); + if (ng > MAX_REGIONS) refuse('INVALID', 'nregions ' + ng); + for (let i = 0; i < ng; i++) u.regions.push(readRegion(r, i)); + } + const nf = r.u16('nfuncs'); + if (nf > MAX_FUNCS) refuse('CAPACITY', 'nfuncs ' + nf); + for (let i = 0; i < nf; i++) { + u.funcs.push(readFunc(r, v7, i)); + validateFunc(u, i); + } + validateUnit(u); + if (r.pos !== buf.length) refuse('TRAILING', (buf.length - r.pos) + ' bytes after last function'); // M:trailing + if (v7 && !usesV7(u)) refuse('VERSION', 'gratuitous v7: no region, float or memory opcode'); // M:gratuitous + return u; +} + +// ----------------------------------------------------------- interpreter +// Integer and bool registers hold their mathematical value as a BigInt +// (unsigned 0..2^w-1, signed sign-extended, bool 0/1). Float registers hold +// their raw IEEE bits as a BigInt so CONST/MOV/RLOAD/RSTORE/RINIT/BITCAST +// transport NaN payloads exactly; arithmetic converts through a DataView. +class Trap { constructor(code) { this.code = code; } } +const trap = (code) => { throw new Trap(code); }; + +const scratch = new DataView(new ArrayBuffer(8)); +function f32val(bits) { scratch.setUint32(0, Number(bits), true); return scratch.getFloat32(0, true); } +function f32bits(x) { scratch.setFloat32(0, x, true); return BigInt(scratch.getUint32(0, true)); } +function f64val(bits) { scratch.setBigUint64(0, bits, true); return scratch.getFloat64(0, true); } +function f64bits(x) { scratch.setFloat64(0, x, true); return scratch.getBigUint64(0, true); } +const fval = (t, bits) => (t === T_F32 ? f32val(bits) : f64val(bits)); +const fbits = (t, x) => (t === T_F32 ? f32bits(Math.fround(x)) : f64bits(x)); + +function intMin(t) { return isSigned(t) ? -(1n << BigInt(intWidth(t) - 1)) : 0n; } +function intMax(t) { return isSigned(t) ? (1n << BigInt(intWidth(t) - 1)) - 1n : (1n << BigInt(intWidth(t))) - 1n; } +function wrapInt(t, v) { const w = intWidth(t); return isSigned(t) ? BigInt.asIntN(w, v) : BigInt.asUintN(w, v); } +function checked(t, v) { + if (!isSigned(t)) return BigInt.asUintN(intWidth(t), v); + if (v < intMin(t) || v > intMax(t)) trap(1); + return v; +} + +// Integer to f32 with one rounding (nearest, ties to even), from the exact +// BigInt. Converting through a double first could round twice. +function bigToF32Bits(v) { + const neg = v < 0n; + let m = neg ? -v : v; + let x; + if (m < (1n << 24n)) x = Number(m); + else { + const shift = m.toString(2).length - 24; + let top = m >> BigInt(shift); + const rest = m - (top << BigInt(shift)); + const half = 1n << BigInt(shift - 1); + if (rest > half || (rest === half && (top & 1n) === 1n)) top += 1n; + x = Number(top) * 2 ** shift; // M:i2f32 + } + return f32bits(neg ? -x : x); +} + +function castValue(dt, st, v) { + if (isInt(st) && isInt(dt)) { + if (v < intMin(dt) || v > intMax(dt)) trap(5); // M:cast5 + return v; + } + if (isFloat(st) && isInt(dt)) { + const x = fval(st, v); + if (Number.isNaN(x)) return 0n; // M:satnan + if (x === Infinity) return intMax(dt); + if (x === -Infinity) return intMin(dt); + const t = BigInt(Math.trunc(x)); + if (t < intMin(dt)) return intMin(dt); // M:satlow + if (t > intMax(dt)) return intMax(dt); // M:sathigh + return t; + } + if (isInt(st) && isFloat(dt)) return dt === T_F32 ? bigToF32Bits(v) : f64bits(Number(v)); + if (st === dt) return v; + return fbits(dt, fval(st, v)); +} + +function bitcastValue(dt, st, v) { + let bits; + if (isFloat(st)) bits = v; + else bits = BigInt.asUintN(intWidth(st), v); + if (isFloat(dt)) return bits; + return isSigned(dt) ? BigInt.asIntN(intWidth(dt), bits) : bits; // M:bcsign +} + +function binInt(t, sub, a, b, tb) { + const w = intWidth(t); + switch (sub) { + case 1: return checked(t, a + b); + case 2: return checked(t, a - b); + case 3: return checked(t, a * b); + case 4: + if (b === 0n) trap(2); + return checked(t, a / b); + case 5: + if (b === 0n) trap(2); + return a % b; + case 6: return a & b; + case 7: return a | b; + case 8: return a ^ b; + case 9: + if (b < 0n || b >= BigInt(w)) trap(7); + return isSigned(t) ? checked(t, a << b) : BigInt.asUintN(w, a << b); + case 10: + if (b < 0n || b >= BigInt(w)) trap(7); + return a >> b; + } + throw new Error('unreachable BIN ' + sub + ' ' + tb); +} + +function binFloat(t, sub, a, b) { + const x = fval(t, a), y = fval(t, b); + const r = sub === 1 ? x + y : sub === 2 ? x - y : sub === 3 ? x * y : x / y; + return fbits(t, r); +} + +function cmpValues(sub, x, y) { + switch (sub) { + case 1: return x === y; + case 2: return !(x === y); + case 3: return x < y; + case 4: return x <= y; + case 5: return x > y; + case 6: return x >= y; + } + return false; +} + +class Region { + constructor(rg) { + this.rg = rg; + this.w = elemBytes(rg.tag); + this.mem = new Uint8Array(rg.count * this.w); + if (rg.cls === 2) this.mem.set(rg.init); + } + index(i) { + if (i < 0n || i >= BigInt(this.rg.count)) trap(3); // M:bounds + return Number(i) * this.w; + } + elemBits(v) { return isFloat(this.rg.tag) ? v : BigInt.asUintN(8 * this.w, v); } + load(i) { + const off = this.index(i); + let bits = 0n; + for (let k = this.w - 1; k >= 0; k--) bits = (bits << 8n) | BigInt(this.mem[off + k]); + const t = this.rg.tag; + if (isFloat(t)) return bits; + return isSigned(t) ? BigInt.asIntN(8 * this.w, bits) : bits; + } + writeAt(off, bits) { + for (let k = 0; k < this.w; k++) { this.mem[off + k] = Number(bits & 0xffn); bits >>= 8n; } + } + store(i, v) { this.writeAt(this.index(i), this.elemBits(v)); } + fill(v) { + this.writeAt(0, this.elemBits(v)); + for (let have = this.w; have < this.mem.length; have *= 2) { + this.mem.copyWithin(have, 0, Math.min(have, this.mem.length - have)); + } + } + hex() { return Buffer.from(this.mem).toString('hex'); } +} + +class Instance { + constructor(u) { + this.u = u; + this.steps = 0; + this.module = u.regions.map((rg) => (rg.cls === 1 ? null : new Region(rg))); + } + call(fi, args) { + const u = this.u, f = u.funcs[fi]; + const regs = new Array(f.nvregs).fill(0n); + for (let k = 0; k < args.length; k++) regs[k] = args[k]; + // Fresh local storage on every invocation (spec 10.1 class 1). + const local = u.regions.map((rg) => (rg.cls === 1 && rg.owner === fi ? new Region(rg) : null)); + const reg = (g) => this.module[g] || local[g]; + let b = 0; + for (;;) { + const blk = f.blocks[b]; + let nextBlock = -1; + for (let j = blk.first; j < blk.first + blk.count; j++) { + if (++this.steps > STEP_LIMIT) throw new Error('step limit ' + STEP_LIMIT + ' exceeded'); + const ins = f.insns[j]; + const T = (r) => f.vtype[r]; + switch (ins.op) { + case OP.CONST: regs[ins.dst] = isSigned(T(ins.dst)) ? BigInt.asIntN(intWidth(T(ins.dst)), ins.imm) : ins.imm; break; + case OP.MOV: regs[ins.dst] = regs[ins.a]; break; + case OP.BIN: { + const t = T(ins.dst); + if (isFloat(t)) regs[ins.dst] = binFloat(t, ins.sub, regs[ins.a], regs[ins.b]); + else regs[ins.dst] = binInt(t, ins.sub, regs[ins.a], regs[ins.b], T(ins.b)); + break; + } + case OP.UN: { + const t = T(ins.dst), a = regs[ins.a]; + if (ins.sub === 1) { + if (t === T_F32) regs[ins.dst] = a ^ 0x80000000n; // M:fneg + else if (t === T_F64) regs[ins.dst] = a ^ 0x8000000000000000n; + else regs[ins.dst] = isSigned(t) ? checked(t, -a) : wrapInt(t, -a); + } else if (ins.sub === 2) regs[ins.dst] = wrapInt(t, ~a); + else regs[ins.dst] = 1n - a; + break; + } + case OP.CMP: { + const t = T(ins.a); + const x = isFloat(t) ? fval(t, regs[ins.a]) : regs[ins.a]; + const y = isFloat(t) ? fval(t, regs[ins.b]) : regs[ins.b]; + regs[ins.dst] = cmpValues(ins.sub, x, y) ? 1n : 0n; + break; + } + case OP.CAST: regs[ins.dst] = castValue(T(ins.dst), T(ins.a), regs[ins.a]); break; + case OP.CALL: { + const r = this.call(ins.callee, ins.args.map((a) => regs[a])); + if (ins.dst !== NONE) regs[ins.dst] = r; + break; + } + case OP.TRAP: trap(Number(ins.imm)); break; + case OP.BR: nextBlock = ins.blkT; break; + case OP.CBR: nextBlock = regs[ins.a] === 1n ? ins.blkT : ins.blkF; break; + case OP.RET: return ins.a === NONE ? null : regs[ins.a]; + case OP.RLOAD: regs[ins.dst] = reg(ins.rg).load(regs[ins.b]); break; + case OP.RSTORE: reg(ins.rg).store(regs[ins.b], regs[ins.c]); break; + case OP.BITCAST: regs[ins.dst] = bitcastValue(T(ins.dst), T(ins.a), regs[ins.a]); break; + case OP.RINIT: reg(ins.rg).fill(regs[ins.a]); break; + default: throw new Error('unreachable opcode ' + ins.op); + } + } + b = nextBlock; + } + } +} + +// ------------------------------------------------------------------- CLI +function usage(msg) { + process.stderr.write('oracle: ' + msg + '\n' + + 'usage: oracle.js check UNIT\n oracle.js run [--codes] UNIT SCRIPT\n'); + process.exit(2); +} + +function readFile(p) { + try { return fs.readFileSync(p); } catch (e) { usage('cannot read ' + p + ': ' + e.code); } +} + +function decodeOrRefusal(buf) { + try { return { unit: decode(buf) }; } catch (e) { + if (e instanceof Refusal) return { refusal: e }; + throw e; + } +} + +function parseArg(t, s) { + if (isFloat(t)) { + const digits = t === T_F32 ? 8 : 16; + if (!new RegExp('^0x[0-9a-fA-F]{' + digits + '}$').test(s)) return null; + return BigInt(s); + } + if (!/^-?[0-9]+$/.test(s)) return null; + const v = BigInt(s); + if (t === T_BOOL) return v === 0n || v === 1n ? v : null; + return v >= intMin(t) && v <= intMax(t) ? v : null; +} + +function formatValue(t, v) { + if (t === T_VOID) return 'void'; + if (t === T_F32) return '0x' + v.toString(16).padStart(8, '0'); + if (t === T_F64) return '0x' + v.toString(16).padStart(16, '0'); + return v.toString(10); +} + +function main(argv) { + const codes = argv.includes('--codes'); + const args = argv.filter((a) => a !== '--codes'); + const cmd = args[0]; + if (cmd === 'check' && args.length === 2 && !codes) { + const res = decodeOrRefusal(readFile(args[1])); + if (res.refusal) { + process.stdout.write('REFUSE ' + res.refusal.cls + ' ' + res.refusal.detail + '\n'); + return 1; + } + process.stdout.write('ACCEPT v' + res.unit.version + '\n'); + return 0; + } + if (cmd === 'run' && args.length === 3) { + const res = decodeOrRefusal(readFile(args[1])); + if (res.refusal) { + process.stderr.write('REFUSE ' + res.refusal.cls + ' ' + res.refusal.detail + '\n'); + return 1; + } + const u = res.unit; + const script = readFile(args[2]).toString('utf8').split('\n'); + const inst = new Instance(u); + const out = []; + const flush = () => { if (out.length) process.stdout.write(out.join('')); out.length = 0; }; + for (let ln = 0; ln < script.length; ln++) { + const line = script[ln].trim(); + if (line === '' || line.startsWith('#')) continue; + const w = line.split(/\s+/); + const bad = (m) => { flush(); usage('script line ' + (ln + 1) + ': ' + m); }; + if (w[0] === 'call') { + const fi = u.funcs.findIndex((f) => f.name === w[1]); + if (fi < 0) bad('no function ' + w[1]); + const f = u.funcs[fi]; + if (w.length - 2 !== f.nparams) bad('expected ' + f.nparams + ' arguments'); + const vals = w.slice(2).map((s, k) => { + const v = parseArg(f.vtype[k], s); + if (v === null) bad('argument ' + s + ' does not fit tag ' + f.vtype[k]); + return v; + }); + try { + const r = inst.call(fi, vals); // M:callsite + out.push(f.name + ' = ' + formatValue(f.ret, r) + '\n'); + } catch (e) { + if (!(e instanceof Trap)) { flush(); process.stderr.write('oracle: ' + e.message + '\n'); return 3; } + out.push(f.name + ' TRAP' + (codes ? ' code=' + e.code : '') + '\n'); + } + } else if (w[0] === 'region' && w.length === 2) { + const g = u.regions.findIndex((rg) => rg.cls !== 1 && rg.name === w[1]); + if (g < 0) bad('no module region ' + w[1]); + out.push('region ' + w[1] + ' ' + inst.module[g].hex() + '\n'); + } else bad('unknown command ' + JSON.stringify(line)); + } + flush(); + return 0; + } + usage('bad arguments'); +} + +process.exitCode = main(process.argv.slice(2)); diff --git a/seed/wasm/v7/test-oracle.sh b/seed/wasm/v7/test-oracle.sh new file mode 100755 index 000000000..a08bb14b2 --- /dev/null +++ b/seed/wasm/v7/test-oracle.sh @@ -0,0 +1,208 @@ +#!/usr/bin/env bash +# SPDX-License-Identifier: Apache-2.0 +# Self-test of the independent OSC IR v7 oracle (seed/wasm/v7/oracle.js). +# +# 1. writer.js emits every fixture unit into $BUILD/v7-oracle/units. +# 2. oracle.js check / run results are compared with the hand-written +# expectations in seed/wasm/v7/expect/ (check.txt, run/*.script + .out). +# 3. Usage/exit-code controls and a source-independence check. +# 4. Mutants: patched copies of oracle.js in $BUILD/v7-oracle/mutants/ (each +# replaces the one line tagged `// M:`); every mutant must produce +# at least one mismatch against the same expectations. +# +# Exit 0 PASS, 1 FAIL. +set -euo pipefail + +here=$(cd "$(dirname "$0")" && pwd) +root=$(cd "$here/../../.." && pwd) +out=${BUILD:-$root/build}/v7-oracle +units=$out/units +expect=$here/expect +fail=0 +note() { printf '%s\n' "$*"; } +bad() { note "FAIL: $*"; fail=1; } + +rm -rf "$out" +mkdir -p "$units" "$out/runs" +node "$here/writer.js" "$units" >/dev/null + +# suite ORACLE STOP_AT_FIRST -> prints mismatches to stdout, returns count via $mism +suite() { + local oracle=$1 first=$2 name want got rc wantrc u s base + mism=0 + checks=0 + runs=0 + while IFS=$'\t' read -r name want; do + case $name in ''|'#'*) continue ;; esac + checks=$((checks + 1)) + set +e + got=$(node "$oracle" check "$units/$name.unit" 2>&1) + rc=$? + set -e + wantrc=1 + [ "${want%% *}" = ACCEPT ] && wantrc=0 + if [ "$(printf '%s' "$got" | cut -d' ' -f1-2)" != "$want" ] || [ "$rc" != "$wantrc" ]; then + note " mismatch check $name: want '$want' rc $wantrc, got '$got' rc $rc" + mism=$((mism + 1)) + [ "$first" = 1 ] && return 0 + fi + done < "$expect/check.txt" + for s in "$expect"/run/*.script; do + base=$(basename "$s" .script) + u=$units/$base.unit + runs=$((runs + 1)) + set +e + node "$oracle" run --codes "$u" "$s" > "$out/runs/$base.got" 2> "$out/runs/$base.err" + rc=$? + set -e + if [ "$rc" != 0 ] || ! cmp -s "$out/runs/$base.got" "$expect/run/$base.out"; then + note " mismatch run $base (rc $rc)" + mism=$((mism + 1)) + [ "$first" = 1 ] && return 0 + fi + if [ -f "$expect/run/$base.plain" ]; then + runs=$((runs + 1)) + set +e + node "$oracle" run "$u" "$s" > "$out/runs/$base.plain.got" 2>&1 + rc=$? + set -e + if [ "$rc" != 0 ] || ! cmp -s "$out/runs/$base.plain.got" "$expect/run/$base.plain"; then + note " mismatch run (plain) $base (rc $rc)" + mism=$((mism + 1)) + [ "$first" = 1 ] && return 0 + fi + fi + done + return 0 +} + +# --- fixture set == expectation set (no silently skipped unit) +comm -3 <(cd "$units" && ls -- *.unit | sed 's/\.unit$//' | sort) \ + <(grep -v '^#' "$expect/check.txt" | cut -f1 | sed '/^$/d' | sort) > "$out/setdiff" +if [ -s "$out/setdiff" ]; then bad "fixture set differs from expect/check.txt:"; cat "$out/setdiff"; fi +for s in "$expect"/run/*.script; do + [ -f "$units/$(basename "$s" .script).unit" ] || bad "run script without unit: $s" + [ -f "${s%.script}.out" ] || bad "run script without .out: $s" +done + +# --- unpatched oracle: zero mismatches +suite "$here/oracle.js" 0 +[ "$mism" = 0 ] || bad "oracle: $mism mismatches" +nchecks=$checks nruns=$runs +note "oracle: checks=$nchecks runs=$nruns mismatches=$mism" + +# --- usage and exit-code controls +expect_rc() { # WANT_RC DESCRIPTION CMD... + local want=$1 what=$2 rc + shift 2 + set +e + "$@" > "$out/ctl.out" 2> "$out/ctl.err" + rc=$? + set -e + [ "$rc" = "$want" ] || bad "control '$what': rc $rc, want $want" +} +ctl=$out/ctl +mkdir -p "$ctl" +printf 'call nosuch\n' > "$ctl/unknown.s" +printf 'call get\n' > "$ctl/arity.s" +printf 'call set 0 256\n' > "$ctl/range.s" +printf 'call set 0 -1\n' > "$ctl/neg.s" +printf 'call set 0x1 0x3f8000\n' > "$ctl/floatarg.s" +printf 'jump get 0\n' > "$ctl/verb.s" +printf 'region nosuch\n' > "$ctl/region.s" +printf 'call get 0\n' > "$ctl/ok.s" +O=$here/oracle.js +expect_rc 2 'check without unit' node "$O" check +expect_rc 2 'unknown command' node "$O" frob "$units/rt_u8.unit" +expect_rc 2 'run without script' node "$O" run --codes "$units/rt_u8.unit" +expect_rc 2 'missing unit file' node "$O" check "$units/does-not-exist.unit" +expect_rc 2 'unknown function' node "$O" run "$units/rt_u8.unit" "$ctl/unknown.s" +expect_rc 2 'wrong arity' node "$O" run "$units/rt_u8.unit" "$ctl/arity.s" +expect_rc 2 'u8 argument 256' node "$O" run "$units/rt_u8.unit" "$ctl/range.s" +expect_rc 2 'u8 argument -1' node "$O" run "$units/rt_u8.unit" "$ctl/neg.s" +expect_rc 2 'short float argument' node "$O" run "$units/rt_f32.unit" "$ctl/floatarg.s" +expect_rc 2 'unknown script verb' node "$O" run "$units/rt_u8.unit" "$ctl/verb.s" +expect_rc 2 'unknown region' node "$O" run "$units/rt_u8.unit" "$ctl/region.s" +expect_rc 1 'run of refused unit' node "$O" run "$units/inv_missing_init.unit" "$ctl/ok.s" +[ -s "$out/ctl.out" ] && bad 'run of refused unit wrote stdout' +grep -q '^REFUSE INVALID ' "$out/ctl.err" || bad 'run of refused unit: no REFUSE on stderr' +expect_rc 0 '--codes before verb' node "$O" --codes run "$units/rt_u8.unit" "$ctl/ok.s" +[ "$(cat "$out/ctl.out")" = 'get = 0' ] || bad "--codes before verb printed '$(cat "$out/ctl.out")'" + +# --- independence: neither file loads the other or any repo producer/backend +for f in oracle.js writer.js; do + reqs=$(grep -v '^ *//' "$here/$f" | grep -o "require([^)]*)" | sort -u | tr '\n' ' ') + case $f in + oracle.js) [ "$reqs" = "require('fs') " ] || bad "$f requires: $reqs" ;; + writer.js) [ "$reqs" = "require('fs') require('path') " ] || bad "$f requires: $reqs" ;; + esac +done +# Outside comments, neither file names a sibling implementation. +for pair in 'oracle.js:writer\.js' 'writer.js:oracle\.js'; do + f=${pair%%:*} + if grep -v '^ *//' "$here/$f" | grep -q "irwasm\|omc\.om\|osc_ir\|${pair#*:}"; then + bad "$f names a sibling implementation outside comments" + fi +done + +# A mutant "caught" against a failing baseline proves nothing. +if [ "$fail" != 0 ]; then + note "V7ORACLE FAIL checks=$nchecks runs=$nruns mutants=NOT_RUN (baseline failed)" + exit 1 +fi + +# --- mutants +mutants=$(cat <<'EOF' +bounds if (i < 0n) trap(3); +bounds:off1 if (i < 0n || i > BigInt(this.rg.count)) trap(3); +initcheck if (false) { +meet for (let k = 0; k < n; k++) st[k] |= o[k]; +satnan if (Number.isNaN(x)) return intMax(dt); +satlow if (t < intMin(dt)) return 0n; +sathigh if (t > intMax(dt)) return wrapInt(dt, t); +owner void rg; +textro void rg; +callsite const r = new Instance(u).call(fi, vals); +callsite:rollback const snap = inst.module.map((g) => g && g.mem.slice()); let r; try { r = inst.call(fi, vals); } catch (e) { inst.module.forEach((g, k) => { if (g) g.mem.set(snap[k]); }); throw e; } +gratuitous void 0; +f32upper if (t === T_F32) return true; +i2f32 x = Number(m); +bcsign return bits; +export const exp = () => {}; +trailing void 0; +fneg if (t === T_F32) regs[ins.dst] = f32bits(-f32val(a)); +cast5 if (false) trap(5); +EOF +) +nm=0 caught=0 +mdir=$out/mutants +while IFS=$'\t' read -r tag repl; do + marker=${tag%%:*} + mname=${tag/:/_} + nm=$((nm + 1)) + hits=$(grep -c "// M:$marker\$" "$O" || true) + if [ "$hits" != 1 ]; then bad "mutant $mname: marker M:$marker found $hits times"; continue; fi + mkdir -p "$mdir/$mname" + MREPL=$repl MMARK="// M:$marker" node -e ' + const fs = require("fs"); + const lines = fs.readFileSync(process.argv[1], "utf8").split("\n"); + const i = lines.findIndex((l) => l.endsWith(process.env.MMARK)); + lines[i] = process.env.MREPL; + fs.writeFileSync(process.argv[2], lines.join("\n")); + ' "$O" "$mdir/$mname/oracle.js" + if cmp -s "$O" "$mdir/$mname/oracle.js"; then bad "mutant $mname: patch changed nothing"; continue; fi + suite "$mdir/$mname/oracle.js" 1 > "$mdir/$mname/log" + if [ "$mism" -gt 0 ]; then + caught=$((caught + 1)) + note "mutant $mname: caught ($(sed 's/^ mismatch //' "$mdir/$mname/log" | head -n1))" + else + bad "mutant $mname: SURVIVED" + fi +done <<< "$mutants" + +if [ "$fail" = 0 ] && [ "$caught" = "$nm" ]; then + note "V7ORACLE PASS checks=$nchecks runs=$nruns mutants=$caught/$nm" + exit 0 +fi +note "V7ORACLE FAIL checks=$nchecks runs=$nruns mutants=$caught/$nm" +exit 1 diff --git a/seed/wasm/v7/writer.js b/seed/wasm/v7/writer.js new file mode 100755 index 000000000..cda8b2183 --- /dev/null +++ b/seed/wasm/v7/writer.js @@ -0,0 +1,453 @@ +#!/usr/bin/env node +// SPDX-License-Identifier: Apache-2.0 +// Hand-written OSC IR v1/v7 byte writer and fixture set for the v7 oracle. +// +// Independent of seed/wasm/v7/oracle.js (no shared code: this file encodes, +// the oracle decodes) and of every producer/backend. Wire layout from +// spec/one-layer-format.md sections 3 and 10. +// +// node writer.js OUTDIR writes OUTDIR/.unit for every fixture +// require('./writer.js').encode(desc) -> Buffer +// +// desc = { version: 1|7, regions: [{ name, tag, cls, owner, count, init }], +// funcs: [{ name, params: [tag..], ret: tag, regs: [tag..] (after the +// params), blocks: [[insn..]..] }] } +// Tags may be names (u8 .. f64) or numbers. Register operand 'none' = 0xFFFF. +'use strict'; + +const fs = require('fs'); +const path = require('path'); + +const TAG = { void: 0, bool: 1, u8: 2, u16: 3, u32: 4, u64: 5, i8: 6, i16: 7, i32: 8, i64: 9, f32: 13, f64: 14 }; +const tagOf = (t) => (typeof t === 'number' ? t : TAG[t]); +const BINSUB = { add: 1, sub: 2, mul: 3, div: 4, rem: 5, and: 6, or: 7, xor: 8, shl: 9, shr: 10 }; +const UNSUB = { neg: 1, bnot: 2, lnot: 3 }; +const CMPSUB = { eq: 1, ne: 2, lt: 3, le: 4, gt: 5, ge: 6 }; + +class Bytes { + constructor() { this.a = []; } + u8(v) { if (v < 0 || v > 255) throw new Error('u8 ' + v); this.a.push(v); } + u16(v) { this.u8(v & 0xff); this.u8((v >>> 8) & 0xff); } + u32(v) { for (let k = 0; k < 4; k++) this.u8(Math.floor(v / 2 ** (8 * k)) % 256); } + u64(v) { let x = BigInt.asUintN(64, BigInt(v)); for (let k = 0; k < 8; k++) { this.u8(Number(x & 0xffn)); x >>= 8n; } } + raw(list) { for (const b of list) this.u8(b); } + str(s, lenWidth) { + const b = Buffer.from(s, 'latin1'); + if (lenWidth === 2) this.u16(b.length); else this.u8(b.length); + this.raw(b); + } +} + +const regField = (v) => (v === 'none' ? 0xffff : v); + +function insn(w, ins) { + const [op, ...f] = ins; + const r = (v) => w.u16(regField(v)); + switch (op) { + case 'const': w.u8(1); r(f[0]); w.u64(f[1]); break; + case 'mov': w.u8(2); r(f[0]); r(f[1]); break; + case 'bin': w.u8(3); w.u8(typeof f[0] === 'number' ? f[0] : BINSUB[f[0]]); r(f[1]); r(f[2]); r(f[3]); break; + case 'un': w.u8(4); w.u8(typeof f[0] === 'number' ? f[0] : UNSUB[f[0]]); r(f[1]); r(f[2]); break; + case 'cmp': w.u8(5); w.u8(CMPSUB[f[0]]); r(f[1]); r(f[2]); r(f[3]); break; + case 'cast': w.u8(6); r(f[0]); r(f[1]); break; + case 'call': w.u8(11); r(f[0]); r(f[1]); w.u8(f.length - 2); f.slice(2).forEach(r); break; + case 'trap': w.u8(12); w.u8(f[0]); break; + case 'br': w.u8(13); r(f[0]); break; + case 'cbr': w.u8(14); r(f[0]); r(f[1]); r(f[2]); break; + case 'ret': w.u8(15); r(f[0]); break; + case 'rload': w.u8(30); r(f[0]); r(f[1]); r(f[2]); break; + case 'rstore': w.u8(31); r(f[0]); r(f[1]); r(f[2]); break; + case 'bitcast': w.u8(32); r(f[0]); r(f[1]); break; + case 'rinit': w.u8(33); r(f[0]); r(f[1]); break; + case 'raw': w.raw(f); break; // opcode byte and fields verbatim + default: throw new Error('writer: unknown insn ' + op); + } +} + +const TERMS = new Set(['br', 'cbr', 'ret']); + +function func(w, fn, version) { + w.str(fn.name, 1); + const params = fn.params || []; + w.u8(fn.nparams === undefined ? params.length : fn.nparams); + w.u8(tagOf(fn.ret || 'void')); + const regs = params.concat(fn.regs || []); + w.u16(regs.length); + regs.forEach((t) => w.u8(tagOf(t))); + const blocks = fn.blocks; + w.u16(blocks.length); + let first = 0; + for (const b of blocks) { w.u32(first); w.u32(b.length); first += b.length; } + w.u32(first); + for (const b of blocks) for (const i of b) insn(w, i); + w.str(fn.requires || '', 2); + w.str(fn.ensures || '', 2); + void version; +} + +function encode(desc) { + const w = new Bytes(); + w.raw([0x4f, 0x53, 0x43, 0x31, 0x49, 0x52, 0x00]); + w.u8(desc.version); + if (desc.version !== 1) { + w.u8(desc.nstructs || 0); + const regions = desc.regions || []; + w.u16(desc.nregions === undefined ? regions.length : desc.nregions); + for (const g of regions) { + w.str(g.name, 1); + w.u8(tagOf(g.tag)); + w.u8(g.cls); + w.u16(g.owner === undefined ? (g.cls === 1 ? 0 : 0xffff) : g.owner); + w.u32(g.count); + const init = g.init ? Buffer.from(g.init, 'latin1') : Buffer.alloc(0); + w.u32(g.initlen === undefined ? init.length : g.initlen); + w.raw(init); + } + } + w.u16(desc.nfuncs === undefined ? desc.funcs.length : desc.nfuncs); + for (const fn of desc.funcs) func(w, fn, desc.version); + return Buffer.from(w.a); +} + +// ------------------------------------------------------------- fixtures +// Register numbering in each function: params first, then `regs`. +const fx = {}; +const mod = (name, tag, count) => ({ name, tag, cls: 0, owner: 0xffff, count }); +const text = (name, s) => ({ name, tag: 'u8', cls: 2, owner: 0xffff, count: Buffer.from(s, 'latin1').length, init: s }); +const loc = (name, tag, count, owner) => ({ name, tag, cls: 1, owner, count }); + +// set(i, v) -> void ; get(i) -> T over one module region 0 of element T. +function setget(tag, idx, count, extra) { + return { + version: 7, + regions: [mod('g', tag, count)], + funcs: [ + { name: 'set', params: [idx, tag], blocks: [[['rstore', 0, 0, 1], ['ret', 'none']]] }, + { name: 'get', params: [idx], ret: tag, regs: [tag], blocks: [[['rload', 1, 0, 0], ['ret', 1]]] }, + ].concat(extra || []), + }; +} + +fx.rt_u8 = setget('u8', 'i32', 4); +fx.rt_i32 = setget('i32', 'u32', 3); +fx.rt_u64 = setget('u64', 'u64', 2, [ + { name: 'geti', params: ['i64'], ret: 'u64', regs: ['u64'], blocks: [[['rload', 1, 0, 0], ['ret', 1]]] }, +]); +fx.rt_f32 = setget('f32', 'u8', 2); +fx.rt_f64 = setget('f64', 'i16', 2); + +// Text read, including an empty text table. +fx.text_read = { + version: 7, + regions: [text('t', 'RGBX'), text('e', '')], + funcs: [ + { name: 'tget', params: ['u32'], ret: 'u8', regs: ['u8'], blocks: [[['rload', 1, 0, 0], ['ret', 1]]] }, + { name: 'eget', params: ['u32'], ret: 'u8', regs: ['u8'], blocks: [[['rload', 1, 1, 0], ['ret', 1]]] }, + ], +}; + +// Local storage: initialized to 5 on every call, old value returned, then overwritten. +// reinit: RINIT 5, store 9, RINIT 7, load -> 7 (re-init resets the whole region). +fx.local_fresh = { + version: 7, + regions: [loc('l', 'u32', 4, 0), loc('l', 'u32', 2, 1)], + funcs: [ + { name: 'swap', params: ['u32', 'u32'], ret: 'u32', regs: ['u32', 'u32'], + blocks: [[['const', 2, 5], ['rinit', 0, 2], ['rload', 3, 0, 0], ['rstore', 0, 0, 1], ['ret', 3]]] }, + { name: 'reinit', params: ['u32'], ret: 'u32', regs: ['u32', 'u32', 'u32', 'u32'], + blocks: [[['const', 1, 5], ['rinit', 1, 1], ['const', 2, 9], ['rstore', 1, 0, 2], + ['const', 3, 7], ['rinit', 1, 3], ['rload', 4, 1, 0], ['ret', 4]]] }, + ], +}; + +// Module persistence across calls, a partial write before a bounds trap, and +// an overflow trap before any write. +fx.persist = { + version: 7, + regions: [mod('c', 'u32', 1)], + funcs: [ + { name: 'bump', ret: 'u32', regs: ['u64', 'u32', 'u32', 'u32'], + blocks: [[['const', 0, 0], ['rload', 1, 0, 0], ['const', 2, 1], ['bin', 'add', 3, 1, 2], + ['rstore', 0, 0, 3], ['ret', 3]]] }, + { name: 'partial', ret: 'u32', regs: ['u64', 'u32', 'u32', 'u32', 'u64', 'u32'], + blocks: [[['const', 0, 0], ['rload', 1, 0, 0], ['const', 2, 1], ['bin', 'add', 3, 1, 2], + ['rstore', 0, 0, 3], ['const', 4, 1], ['rload', 5, 0, 4], ['ret', 5]]] }, + { name: 'ovf', params: ['i32'], ret: 'u32', regs: ['i32', 'u64', 'u32', 'u32'], + blocks: [[['bin', 'add', 1, 0, 0], ['const', 2, 0], ['const', 3, 77], ['rstore', 0, 2, 3], + ['ret', 3]]] }, + ], +}; + +// Bitcasts across each width set. +const bc = (name, from, to) => ({ name, params: [from], ret: to, regs: [to], blocks: [[['bitcast', 1, 0], ['ret', 1]]] }); +fx.bitcast = { + version: 7, + funcs: [ + bc('f32_u32', 'f32', 'u32'), bc('u32_f32', 'u32', 'f32'), bc('i32_f32', 'i32', 'f32'), + bc('f64_i64', 'f64', 'i64'), bc('u64_f64', 'u64', 'f64'), bc('u8_i8', 'u8', 'i8'), + bc('i8_u8', 'i8', 'u8'), bc('i16_u16', 'i16', 'u16'), bc('u16_i16', 'u16', 'i16'), + bc('f32_f32', 'f32', 'f32'), + ], +}; + +// Conversions (CAST). +const cv = (name, from, to) => ({ name, params: [from], ret: to, regs: [to], blocks: [[['cast', 1, 0], ['ret', 1]]] }); +fx.convert = { + version: 7, + funcs: [ + cv('f64_i32', 'f64', 'i32'), cv('f64_u8', 'f64', 'u8'), cv('f32_i8', 'f32', 'i8'), + cv('f64_u64', 'f64', 'u64'), cv('f64_i64', 'f64', 'i64'), cv('f32_u32', 'f32', 'u32'), + cv('f32_i16', 'f32', 'i16'), + cv('u64_f32', 'u64', 'f32'), cv('i32_f32', 'i32', 'f32'), cv('u64_f64', 'u64', 'f64'), + cv('i64_f64', 'i64', 'f64'), cv('f64_f32', 'f64', 'f32'), cv('f32_f64', 'f32', 'f64'), + cv('f64_f64', 'f64', 'f64'), cv('i32_u8', 'i32', 'u8'), cv('u64_i64', 'u64', 'i64'), + ], +}; + +// Float arithmetic, negation and comparison. +const fb = (name, t, sub) => ({ name, params: [t, t], ret: t, regs: [t], blocks: [[['bin', sub, 2, 0, 1], ['ret', 2]]] }); +const fc = (name, t, sub) => ({ name, params: [t, t], ret: 'bool', regs: ['bool'], blocks: [[['cmp', sub, 2, 0, 1], ['ret', 2]]] }); +fx.float_ops = { + version: 7, + funcs: [ + fb('fadd', 'f32', 'add'), fb('fmul', 'f32', 'mul'), fb('dsub', 'f64', 'sub'), fb('ddiv', 'f64', 'div'), + { name: 'fneg', params: ['f32'], ret: 'f32', regs: ['f32'], blocks: [[['un', 'neg', 1, 0], ['ret', 1]]] }, + { name: 'dneg', params: ['f64'], ret: 'f64', regs: ['f64'], blocks: [[['un', 'neg', 1, 0], ['ret', 1]]] }, + fc('feq', 'f32', 'eq'), fc('fne', 'f32', 'ne'), fc('flt', 'f32', 'lt'), fc('dge', 'f64', 'ge'), + { name: 'kf', ret: 'f32', regs: ['f32'], blocks: [[['const', 0, 0x7fa00001], ['ret', 0]]] }, + { name: 'kd', ret: 'f64', regs: ['f64'], blocks: [[['const', 0, 0xfff0000000000001n], ['ret', 0]]] }, + ], +}; + +// Initialization on both branches of a diamond, used after the join (accept). +fx.init_diamond = { + version: 7, + regions: [loc('d', 'i16', 3, 0)], + funcs: [ + { name: 'pick', params: ['bool'], ret: 'i16', regs: ['i16', 'i16', 'u8', 'i16'], + blocks: [ + [['cbr', 0, 1, 2]], + [['const', 1, 0xfffffffffffffffdn], ['rinit', 0, 1], ['br', 3]], + [['const', 2, 4], ['rinit', 0, 2], ['br', 3]], + [['const', 3, 2], ['rload', 4, 0, 3], ['ret', 4]], + ] }, + ], +}; + +// v1 units (no region, no float, no v7 opcode). +fx.v1_arith = { + version: 1, + funcs: [ + { name: 'addi', params: ['i32', 'i32'], ret: 'i32', regs: ['i32'], blocks: [[['bin', 'add', 2, 0, 1], ['ret', 2]]] }, + { name: 'addu', params: ['u8', 'u8'], ret: 'u8', regs: ['u8'], blocks: [[['bin', 'add', 2, 0, 1], ['ret', 2]]] }, + { name: 'divi', params: ['i64', 'i64'], ret: 'i64', regs: ['i64'], blocks: [[['bin', 'div', 2, 0, 1], ['ret', 2]]] }, + { name: 'remi', params: ['i64', 'i64'], ret: 'i64', regs: ['i64'], blocks: [[['bin', 'rem', 2, 0, 1], ['ret', 2]]] }, + { name: 'shl8', params: ['i8', 'u32'], ret: 'i8', regs: ['i8'], blocks: [[['bin', 'shl', 2, 0, 1], ['ret', 2]]] }, + { name: 'shr8', params: ['i8', 'u32'], ret: 'i8', regs: ['i8'], blocks: [[['bin', 'shr', 2, 0, 1], ['ret', 2]]] }, + { name: 'negu', params: ['u16'], ret: 'u16', regs: ['u16'], blocks: [[['un', 'neg', 1, 0], ['ret', 1]]] }, + { name: 'memory', params: ['bool'], ret: 'bool', regs: ['bool'], blocks: [[['un', 'lnot', 1, 0], ['ret', 1]]] }, + { name: 'stop', params: [], ret: 'void', blocks: [[['trap', 9], ['ret', 'none']]] }, + ], +}; +// sum(n) = 0 + 1 + .. + (n-1), looping; twice(n) calls sum. +fx.v1_loop = { + version: 1, + funcs: [ + { name: 'sum', params: ['u32'], ret: 'u32', regs: ['u32', 'u32', 'bool', 'u32'], + blocks: [ + [['const', 1, 0], ['const', 2, 0], ['br', 1]], + [['cmp', 'lt', 3, 2, 0], ['cbr', 3, 2, 3]], + [['bin', 'add', 1, 1, 2], ['const', 4, 1], ['bin', 'add', 2, 2, 4], ['br', 1]], + [['ret', 1]], + ] }, + { name: 'twice', params: ['u32'], ret: 'u32', regs: ['u32', 'u32'], + blocks: [[['call', 1, 0, 0], ['bin', 'add', 2, 1, 1], ['ret', 2]]] }, + ], +}; + +// ----------------------------------------------------- malformed units +const tiny = (extra) => Object.assign({ + version: 7, + regions: [mod('g', 'u32', 2)], + funcs: [{ name: 'f', params: ['u32'], ret: 'u32', regs: ['u32'], blocks: [[['rload', 1, 0, 0], ['ret', 1]]] }], +}, extra || {}); +const local1 = (body, regs, extra) => Object.assign({ + version: 7, + regions: [loc('l', 'u32', 2, 0)], + funcs: [{ name: 'f', params: ['u32'], ret: 'u32', regs: regs || ['u32', 'u32'], blocks: body }], +}, extra || {}); + +// Reader classes. +const bytesOf = {}; +bytesOf.bad_magic = () => { const b = encode(tiny()); b[3] = 0x32; return b; }; +bytesOf.bad_version2 = () => { const b = encode(tiny()); b[7] = 2; return b; }; +bytesOf.bad_version6 = () => { const b = encode(tiny()); b[7] = 6; return b; }; +bytesOf.bad_version8 = () => { const b = encode(tiny()); b[7] = 8; return b; }; +bytesOf.trunc_empty = () => Buffer.alloc(0); +bytesOf.trunc_magic = () => encode(tiny()).subarray(0, 4); +bytesOf.trunc_header = () => encode(tiny()).subarray(0, 8); +bytesOf.trunc_last = () => { const b = encode(tiny()); return b.subarray(0, b.length - 1); }; +bytesOf.trunc_initdata = () => encode({ version: 7, regions: [text('t', 'RGBX')], funcs: [] }).subarray(0, 8 + 1 + 2 + 2 + 1 + 1 + 2 + 4 + 4 + 2); +bytesOf.trailing = () => Buffer.concat([encode(tiny()), Buffer.from([0])]); +bytesOf.cap_v7_input = () => { const b = Buffer.alloc(8388609); encode(tiny()).copy(b); return b; }; +bytesOf.cap_v1_input = () => { const b = Buffer.alloc(2620252); encode(fx.v1_arith).copy(b); return b; }; +bytesOf.v7_input_at_v1_cap = () => { + // A v7 header over the v1 cap is not a capacity refusal; it fails later as TRAILING. + const b = Buffer.alloc(2620252); encode(tiny()).copy(b); return b; +}; + +fx.cap_nfuncs = { version: 7, regions: [mod('g', 'u8', 1)], nfuncs: 33, funcs: [] }; +fx.cap_fname = tiny({ funcs: [{ name: 'x'.repeat(64), ret: 'void', blocks: [[['ret', 'none']]] }] }); +fx.cap_nstructs = tiny({ nstructs: 17 }); +fx.unsup_structs = tiny({ nstructs: 1 }); +fx.unsup_class3 = tiny({ regions: [{ name: 'w', tag: 'u32', cls: 3, owner: 0xffff, count: 4 }] }); +fx.unsup_op7 = tiny({ funcs: [{ name: 'f', params: ['u32'], ret: 'u32', regs: ['u32'], blocks: [[['raw', 7, 1, 0, 0, 0], ['ret', 0]]] }] }); +fx.unsup_op20 = tiny({ funcs: [{ name: 'f', ret: 'void', blocks: [[['raw', 20, 0, 0], ['ret', 'none']]] }] }); +fx.unsup_op34 = tiny({ funcs: [{ name: 'f', ret: 'void', blocks: [[['raw', 34], ['ret', 'none']]] }] }); +fx.unsup_op0 = tiny({ funcs: [{ name: 'f', ret: 'void', blocks: [[['raw', 0], ['ret', 'none']]] }] }); +fx.unsup_op9 = tiny({ funcs: [{ name: 'f', params: ['u32'], ret: 'u32', regs: ['u32'], blocks: [[['raw', 9, 1, 0, 0, 0, 0, 0], ['ret', 1]]] }] }); +fx.v1_unsup_op10 = { version: 1, funcs: [{ name: 'f', params: ['u32'], ret: 'void', blocks: [[['raw', 10, 0, 0, 0, 0, 0, 0], ['ret', 'none']]] }] }; +fx.unsup_tag10 = tiny({ funcs: [{ name: 'f', ret: 'void', regs: [10], blocks: [[['ret', 'none']]] }] }); +fx.unsup_tag15 = tiny({ funcs: [{ name: 'f', ret: 'void', regs: [15], blocks: [[['ret', 'none']]] }] }); +fx.v1_op30 = { version: 1, funcs: [{ name: 'f', params: ['u32'], ret: 'u32', regs: ['u32'], blocks: [[['raw', 30, 1, 0, 0, 0, 0, 0], ['ret', 1]]] }] }; +fx.v1_tag13 = { version: 1, funcs: [{ name: 'f', params: ['f32'], ret: 'void', blocks: [[['ret', 'none']]] }] }; +fx.v1_tag14_ret = { version: 1, funcs: [{ name: 'f', ret: 'f64', regs: ['f64'], blocks: [[['const', 0, 0], ['ret', 0]]] }] }; + +// Region table INVALIDs. +const oneRegion = (g) => tiny({ regions: [g] }); +fx.inv_class4 = oneRegion({ name: 'g', tag: 'u32', cls: 4, owner: 0xffff, count: 2 }); +fx.inv_rtag_bool = oneRegion({ name: 'g', tag: 'bool', cls: 0, owner: 0xffff, count: 2 }); +fx.inv_rtag_cells = oneRegion({ name: 'g', tag: 12, cls: 0, owner: 0xffff, count: 2 }); +fx.inv_rname_empty = oneRegion({ name: '', tag: 'u32', cls: 0, owner: 0xffff, count: 2 }); +fx.inv_rname_dot = oneRegion({ name: 'a.b', tag: 'u32', cls: 0, owner: 0xffff, count: 2 }); +fx.inv_rname_nul = oneRegion({ name: 'a\u0000', tag: 'u32', cls: 0, owner: 0xffff, count: 2 }); +fx.inv_rname_8bit = oneRegion({ name: 'g\u00e9', tag: 'u32', cls: 0, owner: 0xffff, count: 2 }); +fx.inv_rname_64 = oneRegion({ name: 'r'.repeat(64), tag: 'u32', cls: 0, owner: 0xffff, count: 2 }); +fx.inv_mod_init = oneRegion({ name: 'g', tag: 'u32', cls: 0, owner: 0xffff, count: 1, init: 'abcd' }); +fx.inv_text_len = oneRegion({ name: 'g', tag: 'u8', cls: 2, owner: 0xffff, count: 5, init: 'RGBX' }); +fx.inv_text_u32 = oneRegion({ name: 'g', tag: 'u32', cls: 2, owner: 0xffff, count: 4, init: 'RGBX' }); +fx.inv_count0 = oneRegion({ name: 'g', tag: 'u32', cls: 0, owner: 0xffff, count: 0 }); +fx.inv_count_big = oneRegion({ name: 'g', tag: 'u8', cls: 0, owner: 0xffff, count: 16777217 }); +fx.ok_count_max = tiny({ regions: [mod('g', 'u32', 2), mod('h', 'u8', 16777216)] }); +fx.inv_nregions = tiny({ nregions: 1025, regions: [] }); +fx.inv_owner_local_module = tiny({ regions: [mod('g', 'u32', 2), loc('l', 'u32', 2, 0xffff)] }); +fx.inv_owner_missing = tiny({ regions: [mod('g', 'u32', 2), loc('l', 'u32', 2, 1)] }); +fx.inv_owner_module0 = oneRegion({ name: 'g', tag: 'u32', cls: 0, owner: 0, count: 2 }); +fx.inv_dup_module = tiny({ regions: [mod('g', 'u32', 2), text('g', 'xy')] }); +fx.inv_shadow = tiny({ regions: [mod('g', 'u32', 2), loc('g', 'u32', 2, 0)] }); +fx.inv_dup_local = tiny({ regions: [mod('g', 'u32', 2), loc('l', 'u8', 1, 0), loc('l', 'u8', 1, 0)] }); + +// Export collisions (v7 reserves memory and region.NAME); duplicate functions. +const named = (n) => ({ name: n, ret: 'void', blocks: [[['ret', 'none']]] }); +fx.inv_export_memory = tiny({ funcs: tiny().funcs.concat([named('memory')]) }); +fx.inv_export_region = tiny({ funcs: tiny().funcs.concat([named('region.g')]) }); +fx.inv_dup_func = tiny({ funcs: tiny().funcs.concat([named('f')]) }); +fx.inv_v1_dup_func = { version: 1, funcs: [named('f'), named('f')] }; +fx.ok_fname_empty = tiny({ funcs: tiny().funcs.concat([named('')]) }); +fx.inv_fname_8bit = tiny({ funcs: tiny().funcs.concat([named('h\u00e9')]) }); + +// Local initialization controls. +fx.inv_missing_init = local1([[['const', 1, 0], ['rload', 2, 0, 1], ['ret', 2]]]); +fx.inv_init_after_use = local1([[['const', 1, 0], ['rload', 2, 0, 1], ['rinit', 0, 0], ['ret', 2]]]); +fx.inv_store_before_init = local1([[['const', 1, 0], ['rstore', 0, 1, 0], ['rinit', 0, 0], ['ret', 0]]]); +fx.inv_init_one_branch = local1([ + [['const', 1, 0], ['cmp', 'eq', 3, 0, 1], ['cbr', 3, 1, 2]], + [['rinit', 0, 0], ['br', 2]], + [['rload', 2, 0, 1], ['ret', 2]], +], ['u32', 'u32', 'bool']); +// Header 1 loads; RINIT happens only in body 2, which loops back to 1. +fx.inv_loop_carried = local1([ + [['const', 1, 0], ['br', 1]], + [['rload', 2, 0, 1], ['cmp', 'eq', 3, 2, 0], ['cbr', 3, 3, 2]], + [['rinit', 0, 0], ['br', 1]], + [['ret', 2]], +], ['u32', 'u32', 'bool']); +fx.ok_loop_init_before = local1([ + [['const', 1, 0], ['rinit', 0, 0], ['br', 1]], + [['rload', 2, 0, 1], ['cmp', 'eq', 3, 2, 0], ['cbr', 3, 3, 2]], + [['rinit', 0, 1], ['br', 1]], + [['ret', 2]], +], ['u32', 'u32', 'bool']); +fx.inv_unreachable_use = local1([ + [['rinit', 0, 0], ['const', 1, 0], ['rload', 2, 0, 1], ['ret', 2]], + [['const', 1, 0], ['rload', 2, 0, 1], ['ret', 2]], +]); +fx.inv_reg_undef = local1([[['rinit', 0, 0], ['rload', 2, 0, 1], ['ret', 2]]]); + +// Ownership. +const twoFuncLocal = (body1) => ({ + version: 7, + regions: [loc('l', 'u32', 2, 0), mod('g', 'u32', 2)], + funcs: [ + { name: 'a', params: ['u32'], ret: 'u32', regs: ['u32', 'u32'], + blocks: [[['rinit', 0, 0], ['const', 1, 0], ['rload', 2, 0, 1], ['ret', 2]]] }, + { name: 'b', params: ['u32'], ret: 'u32', regs: ['u32', 'u32'], blocks: [body1] }, + ], +}); +fx.ok_own_local = twoFuncLocal([['rload', 2, 1, 0], ['ret', 2]]); +fx.inv_foreign_load = twoFuncLocal([['const', 1, 0], ['rload', 2, 0, 1], ['ret', 2]]); +fx.inv_foreign_init = twoFuncLocal([['rinit', 0, 0], ['ret', 0]]); +fx.inv_foreign_store = twoFuncLocal([['const', 1, 0], ['rstore', 0, 1, 0], ['ret', 0]]); +fx.inv_rinit_module = twoFuncLocal([['rinit', 1, 0], ['ret', 0]]); + +// Read-only text. +const textUnit = (body, regs) => ({ + version: 7, + regions: [text('t', 'RGBX')], + funcs: [{ name: 'f', params: ['u8'], ret: 'u8', regs: regs || ['u32'], blocks: [body] }], +}); +fx.inv_text_store = textUnit([['const', 1, 0], ['rstore', 0, 1, 0], ['ret', 0]]); +fx.inv_text_init = textUnit([['rinit', 0, 0], ['ret', 0]]); + +// Operands: sentinel / out of table. +fx.inv_rload_dst_none = tiny({ funcs: [{ name: 'f', params: ['u32'], ret: 'u32', blocks: [[['rload', 'none', 0, 0], ['ret', 0]]] }] }); +fx.inv_rload_region_none = tiny({ funcs: [{ name: 'f', params: ['u32'], ret: 'u32', regs: ['u32'], blocks: [[['rload', 1, 'none', 0], ['ret', 1]]] }] }); +fx.inv_rload_index_none = tiny({ funcs: [{ name: 'f', params: ['u32'], ret: 'u32', regs: ['u32'], blocks: [[['rload', 1, 0, 'none'], ['ret', 1]]] }] }); +fx.inv_rload_region_oob = tiny({ funcs: [{ name: 'f', params: ['u32'], ret: 'u32', regs: ['u32'], blocks: [[['rload', 1, 1, 0], ['ret', 1]]] }] }); +fx.inv_rload_index_8000 = tiny({ funcs: [{ name: 'f', params: ['u32'], ret: 'u32', regs: ['u32'], blocks: [[['rload', 1, 0, 0x8000], ['ret', 1]]] }] }); +fx.inv_rstore_src_fffe = tiny({ funcs: [{ name: 'f', params: ['u32'], ret: 'u32', blocks: [[['rstore', 0, 0, 0xfffe], ['ret', 0]]] }] }); +fx.inv_rstore_src_oob = tiny({ funcs: [{ name: 'f', params: ['u32'], ret: 'u32', blocks: [[['rstore', 0, 0, 1], ['ret', 0]]] }] }); +fx.inv_bitcast_none = tiny({ funcs: [{ name: 'f', params: ['u32'], ret: 'i32', regs: ['i32'], blocks: [[['bitcast', 1, 'none'], ['ret', 1]]] }] }); +fx.inv_rinit_none = local1([[['rinit', 0, 'none'], ['ret', 0]]]); +fx.inv_rinit_region_none = local1([[['rinit', 'none', 0], ['ret', 0]]]); + +// Typing. +fx.inv_index_float = tiny({ funcs: [{ name: 'f', params: ['f32'], ret: 'u32', regs: ['u32'], blocks: [[['rload', 1, 0, 0], ['ret', 1]]] }] }); +fx.inv_index_bool = tiny({ funcs: [{ name: 'f', params: ['bool'], ret: 'u32', regs: ['u32'], blocks: [[['rload', 1, 0, 0], ['ret', 1]]] }] }); +fx.inv_rload_type = tiny({ funcs: [{ name: 'f', params: ['u32'], ret: 'i32', regs: ['i32'], blocks: [[['rload', 1, 0, 0], ['ret', 1]]] }] }); +fx.inv_rstore_type = tiny({ funcs: [{ name: 'f', params: ['u32', 'u64'], ret: 'void', blocks: [[['rstore', 0, 0, 1], ['ret', 'none']]] }] }); +fx.inv_rinit_type = local1([[['const', 1, 0], ['cast', 2, 1], ['rinit', 0, 2], ['ret', 0]]], ['u32', 'i32']); +fx.inv_bitcast_cross = { version: 7, funcs: [bc('f', 'u32', 'f64')] }; +fx.inv_bitcast_bool = { version: 7, funcs: [bc('f', 'bool', 'u8'), bc('g', 'f32', 'f32')] }; +fx.inv_bitcast_width = { version: 7, funcs: [bc('f', 'u8', 'u16'), bc('g', 'f32', 'f32')] }; +fx.inv_float_rem = { version: 7, funcs: [fb('f', 'f32', 'rem')] }; +fx.inv_float_shl = { version: 7, funcs: [fb('f', 'f64', 'shl')] }; +fx.inv_float_and = { version: 7, funcs: [fb('f', 'f64', 'and')] }; +fx.inv_float_bnot = { version: 7, funcs: [{ name: 'f', params: ['f32'], ret: 'f32', regs: ['f32'], blocks: [[['un', 'bnot', 1, 0], ['ret', 1]]] }] }; +fx.inv_float_lnot = { version: 7, funcs: [{ name: 'f', params: ['f64'], ret: 'f64', regs: ['f64'], blocks: [[['un', 'lnot', 1, 0], ['ret', 1]]] }] }; +fx.inv_f32_const_upper = { version: 7, funcs: [{ name: 'f', ret: 'f32', regs: ['f32'], blocks: [[['const', 0, 0x100000000n], ['ret', 0]]] }] }; +fx.inv_cast_bool = { version: 7, funcs: [cv('f', 'bool', 'f32')] }; +fx.inv_cast_to_bool = { version: 7, funcs: [cv('f', 'f64', 'bool')] }; + +// Version selection. +fx.gratuitous_v7 = { version: 7, funcs: fx.v1_loop.funcs }; +fx.gratuitous_v7_empty = { version: 7, funcs: [] }; + +function writeAll(dir) { + fs.mkdirSync(dir, { recursive: true }); + const names = []; + for (const [n, d] of Object.entries(fx)) { fs.writeFileSync(path.join(dir, n + '.unit'), encode(d)); names.push(n); } + for (const [n, f] of Object.entries(bytesOf)) { fs.writeFileSync(path.join(dir, n + '.unit'), f()); names.push(n); } + return names; +} + +module.exports = { encode, fixtures: fx, rawFixtures: bytesOf, writeAll }; + +if (require.main === module) { + if (process.argv.length !== 3) { + process.stderr.write('usage: writer.js OUTDIR\n'); + process.exit(2); + } + const names = writeAll(process.argv[2]); + process.stdout.write(names.length + ' fixtures\n'); +} ```