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
omega-mv / seed / wasm / oracle.js
| 1 | // SPDX-License-Identifier: Apache-2.0 |
| 2 | // Verification oracle only: reads artifacts; never generates IR, wasm, or native code. |
| 3 | 'use strict'; |
| 4 | const fs = require('node:fs'); |
| 5 | const path = require('node:path'); |
| 6 | const assert = require('node:assert/strict'); |
| 7 | const [dir, table] = process.argv.slice(2); |
| 8 | const rows = fs.readFileSync(table, 'utf8').split('\n') |
| 9 | .filter(line => line && !line.startsWith('#')).map(line => line.split('\t')); |
| 10 | const modules = new Map(); |
| 11 | const covered = new Map(); |
| 12 | let traps = 0; |
| 13 | for (const [index, [unit, name, ret, typed, status]] of rows.entries()) { |
| 14 | if (!modules.has(unit)) { |
| 15 | const file = unit === 'mutant' && process.argv[4] === '--mutant-baseline' |
| 16 | ? 'mutant-flipped.wasm' : `${unit}.wasm`; |
| 17 | const bytes = fs.readFileSync(path.join(dir, file)); |
| 18 | assert(WebAssembly.validate(bytes), `${unit}: invalid wasm`); |
| 19 | const module = new WebAssembly.Module(bytes); |
| 20 | assert.deepEqual(WebAssembly.Module.imports(module), [], `${unit}: unexpected import`); |
| 21 | modules.set(unit, new WebAssembly.Instance(module).exports); |
| 22 | covered.set(unit, new Set()); |
| 23 | } |
| 24 | const exports = modules.get(unit); |
| 25 | assert.equal(typeof exports[name], 'function', `${unit}: missing export ${name}`); |
| 26 | covered.get(unit).add(name); |
| 27 | const args = typed === '-' ? [] : typed.split(',').map(arg => { |
| 28 | const [type, value] = arg.split(':'); |
| 29 | if (type === 'bool') return value === 'true' ? 1 : 0; |
| 30 | return type === 'ulong' || type === 'long' ? BigInt(value) : Number(value); |
| 31 | }); |
| 32 | const nativeStatus = Number(fs.readFileSync(path.join(dir, `case-${index + 1}.status`), 'utf8')); |
| 33 | assert.equal(nativeStatus, Number(status), `${unit}.${name}: unexpected native status`); |
| 34 | const native = fs.readFileSync(path.join(dir, `case-${index + 1}.out`)); |
| 35 | let result; |
| 36 | let error; |
| 37 | try { result = exports[name](...args); } catch (caught) { error = caught; } |
| 38 | if (nativeStatus) { |
| 39 | assert(nativeStatus >= 101 && nativeStatus <= 114, 'native status is not an Omega trap'); |
| 40 | assert(error instanceof WebAssembly.RuntimeError, `${unit}.${name}: missing wasm trap`); |
| 41 | assert.equal(native.length, 0, `${unit}.${name}: native trap wrote a value`); |
| 42 | traps++; |
| 43 | } else { |
| 44 | assert.equal(error, undefined, `${unit}.${name}: unexpected wasm trap ${error}`); |
| 45 | assert.equal(native.length, 8, `${unit}.${name}: incomplete native result`); |
| 46 | if (ret === 'void') result = 0n; |
| 47 | else if (ret === 'uint') result = BigInt(result >>> 0); |
| 48 | else result = BigInt(result); |
| 49 | assert.equal(BigInt.asUintN(64, result), native.readBigUInt64LE(), `${unit}.${name}: result mismatch at case ${index + 1}`); |
| 50 | } |
| 51 | } |
| 52 | for (const [unit, exports] of modules) { |
| 53 | assert.deepEqual([...covered.get(unit)].sort(), Object.keys(exports).sort(), `${unit}: untested export`); |
| 54 | } |
| 55 | const mutant = fs.readFileSync(path.join(dir, 'mutant-flipped.wasm')); |
| 56 | assert(WebAssembly.validate(mutant), 'mutant must remain valid wasm'); |
| 57 | const changed = new WebAssembly.Instance(new WebAssembly.Module(mutant)).exports.mutadd(19, 7); |
| 58 | assert.notEqual(changed, modules.get('mutant').mutadd(19, 7), 'add-to-sub mutant escaped'); |
| 59 | console.log(`PASS parity cases=${rows.length} units=${modules.size} traps=${traps}; every export exercised`); |
| 60 | console.log('PASS add-to-sub IR-byte mutant changes executed result'); |