A fast 2D and 3D game engine — TypeScript SDK, C++/WebAssembly core, visual editor. Ship one project to web, desktop, WeChat MiniGames, playable ads, and native Android / iOS.
TypeScript 2d-game-engine3d3d-game-engineandroidcppecsgame-editorgame-enginegamedeviostypescriptwebassemblywebglwebgpuwechat-minigame
fix(abi): the specified arithmetic is in the handshake, both halves
yhhc11820ae00b7
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex e45e267ee..f98fe86f0 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -49,6 +49,32 @@ published separately; it ships inside the editor.### Fixed+- **The engine's specified arithmetic was in no digest, so a module built against+ a different polynomial loaded.** The AOT subset SPECIFIES `sin` and `cos`,+ because ECMAScript does not, and a compiled module carries its own copy of that+ polynomial inside it. A host whose interpreter runs a different one therefore+ makes the same system answer differently depending on whether it was compiled —+ one ulp, which is one pixel, and a differential pixel gate reports it as a bug+ in the engine rather than as two builds that were never the same build.++ `engineAbiDigest` now covers it, in two halves because one alone misses. The+ CONSTANTS go in exactly, so a coefficient change counts whether or not it moves+ any answer — measured, and this is why: a 1-ulp nudge to `S3` moved **none** of+ 250 probed answers while still being different arithmetic that a long enough run+ disagrees on. The ANSWERS go in for what the constants are not — the quadrant+ choice, the tie in the range reduction, the order the polynomial is summed in —+ which a restructure changes while every constant stays put.++ `exact-trig.test.ts` proves the sensitivity exhaustively over the real constant+ list rather than against a second copy of it: every constant the algorithm is,+ one ulp, must move the digest. **The engine ABI digest changes again**, so an+ AOT module built against 0.60.0 or earlier is refused and must be rebuilt.++ What is still owed is written down in the inventory: the digest is taken of the+ TypeScript side, so a change to the C half **alone** moves nothing and only the+ bit-exact differential sees it — and that needs a host C compiler, so a checkout+ without one verifies the two implementations against nothing.+- **A fourth ABI struct was outside the compatibility system entirely.** Three ofthe structs a compiled system is handed carry the same three things: a wordcount authored once in `abiDigest.ts`, a `typedef char es_check_*[...]` in thediff --git a/compiler/tests/exact-trig.test.ts b/compiler/tests/exact-trig.test.tsindex 1d479dfae..86e45f811 100644--- a/compiler/tests/exact-trig.test.ts+++ b/compiler/tests/exact-trig.test.ts@@ -22,6 +22,8 @@ import { tmpdir } from 'node:os';import { join } from 'node:path';import { CFLAGS, RUNTIME_H } from '../src/codegen';import { exact } from '../../sdk/src/math/exact';+import { engineAbiParts, trigDigest } from '../../sdk/src/ecs/aot/abiDigest';+import { EXACT_CONSTANTS } from '../../sdk/src/math/exact';import { findHostCC } from '../src/hostCC';const CC = findHostCC();@@ -107,3 +109,38 @@ describe('the engine\'s trigonometry, on both sides of the compiler', () => {expect(worst).toBeLessThan(1e-12);});});++/**+ * A compiled module carries ITS polynomial inside it, so a host running another+ * one makes the same system answer differently depending on whether it was+ * compiled. The digest is what turns that into a refusal instead of an ulp.+ */+describe('the specified trigonometry is in the engine handshake', () => {+ it('is what the handshake is taken of', () => {+ expect(engineAbiParts(4).join(' ')).toContain(`trig=${trigDigest()}`);+ });++ /**+ * Exhaustive over the real list, not a second copy of it: every constant,+ * one ulp. Sampling answers alone did not do this — a 1-ulp `S3` moved none+ * of 250 probed answers while still being different arithmetic.+ */+ it('moves for a single ulp of any constant the algorithm is', () => {+ const base = trigDigest();+ expect(EXACT_CONSTANTS.length).toBeGreaterThan(10);+ for (let i = 0; i < EXACT_CONSTANTS.length; i++) {+ const nudged = [...EXACT_CONSTANTS];+ nudged[i] = nextUp(nudged[i]!);+ expect([i, nudged[i]]).not.toEqual([i, EXACT_CONSTANTS[i]]);+ expect([i, trigDigest(nudged)]).not.toEqual([i, base]);+ }+ });+});++/** The next double away from zero — one ulp, done on the bits. */+function nextUp(x: number): number {+ const view = new DataView(new ArrayBuffer(8));+ view.setFloat64(0, x);+ view.setBigUint64(0, view.getBigUint64(0) + 1n);+ return view.getFloat64(0);+}diff --git a/sdk/src/ecs/aot/abiDigest.ts b/sdk/src/ecs/aot/abiDigest.tsindex 18b4e915d..c9b737242 100644--- a/sdk/src/ecs/aot/abiDigest.ts+++ b/sdk/src/ecs/aot/abiDigest.ts@@ -12,7 +12,8 @@** - `engineAbi` is everything about the ENGINE the module baked in:* EHT's struct offsets, the resource shapes, the sizes of the four- * ABI structs and the width of an address. A mismatch means rebuild+ * ABI structs, the specified trigonometry and the width of an+ * address. A mismatch means rebuild* the module against this engine.* - `projectShapes` is the `defineComponent` shapes it compiled* against, scoped to the components it actually names. A mismatch@@ -29,6 +30,7 @@import { PTR_LAYOUTS } from '../../wasm/ptrLayouts.generated';import { RESOURCE_NAMES, RESOURCE_SHAPES, resourceLayout } from '../resourceShapes';+import { EXACT_CONSTANTS, exact } from '../../math/exact';/** Sizes of the four ABI structs, in address-wide words. */export const SYSCTX_WORDS = 6;@@ -56,6 +58,44 @@ export interface ShapeDigestInput {readonly fields: readonly string[];}+/**+ * Where the digest LOOKS: off the quadrant grid so every branch of the+ * reduction is reached, then the tie, the boundaries, and arguments large+ * enough that Cody-Waite loses digits. Sampling alone does not do — a 1-ulp+ * `S3` moved none of these, which is why the constants go in exactly.+ */+function trigProbes(): number[] {+ const out: number[] = [];+ for (let i = -120; i <= 120; i++) out.push(i * 0.37);+ const HALF_PI = Math.PI / 2;+ for (let q = -4; q <= 4; q++) for (const d of [-1e-9, 0, 1e-9]) out.push(q * HALF_PI + d);+ for (let k = -3; k <= 3; k++) out.push((k + 0.5) * HALF_PI);+ out.push(0, -0, 1, -1, 1e-8, 12345.6789, -98765.4321, 1e10, -1e10);+ return out;+}++/** A double as the bits it is, because one ulp apart is one pixel apart. */+function bitsOf(x: number): string {+ const view = new DataView(new ArrayBuffer(8));+ view.setFloat64(0, x);+ return view.getBigUint64(0).toString(16).padStart(16, '0');+}++/**+ * Two halves because one alone misses: the CONSTANTS exactly, so a coefficient+ * change counts whether or not it moves a sampled answer, and the ANSWERS for+ * what the constants are not — quadrant choice, the reduction's tie, summation+ * order. A parameter so a test can nudge one constant and watch this move.+ *+ * @internal+ */+export function trigDigest(consts_: readonly number[] = EXACT_CONSTANTS): string {+ const consts = consts_.map(bitsOf).join(',');+ const answers = trigProbes().map((x) => `${bitsOf(exact.sin(x))}/${bitsOf(exact.cos(x))}`).join(',');+ return fnv1a64(`${consts}|${answers}`);+}++/*** Everything about the engine the module baked in. Recomputable by the SDK from* what it ships, which is what makes this the check that can actually run.@@ -75,6 +115,10 @@ export function engineAbiParts(addressBytes: AddressBytes): readonly string[] {`addr=${addressBytes}`,`sysctx=${SYSCTX_WORDS} rows=${QUERYROWS_WORDS} cmd=${CMD_WORDS} eventout=${EVENT_OUT_WORDS}`,`cmdkinds=${CMD_DESPAWN},${CMD_REMOVE}`,+ // The module compiled ITS polynomial in, so a host running another makes+ // the same system answer differently depending on whether it was+ // compiled — an ulp, which is a pixel a differential gate calls a bug.+ `trig=${trigDigest()}`,];// EHT's table: every field of every engine component, at the offset the C++// struct puts it. This is the half that changes when the engine is rebuilt.diff --git a/sdk/src/math/exact.ts b/sdk/src/math/exact.tsindex 32f770709..83a517446 100644--- a/sdk/src/math/exact.ts+++ b/sdk/src/math/exact.ts@@ -43,6 +43,20 @@ const C4 = -2.7557314351390663e-07;const C5 = 2.0875723212981748e-09;const C6 = -1.1359647557788195e-11;+/**+ * Every constant the algorithm IS, in one place a digest can take exactly.+ * Sampling answers is not enough on its own: a 1-ulp nudge to `S3` moved none+ * of 250 probed answers (measured), while still making two builds different+ * arithmetic that a long enough run would disagree on.+ *+ * @internal+ */+export const EXACT_CONSTANTS: readonly number[] = [+ PIO2_HI, PIO2_LO, TWO_OVER_PI,+ S1, S2, S3, S4, S5, S6,+ C1, C2, C3, C4, C5, C6,+];+/*** `Math.round`'s rule — ties toward +Infinity — spelled out, because C's* `round` breaks ties away from zero and would disagree on every .5.diff --git a/src/esengine/aot/EngineDigest.generated.h b/src/esengine/aot/EngineDigest.generated.hindex d66cfbc7c..4fd2cda34 100644--- a/src/esengine/aot/EngineDigest.generated.h+++ b/src/esengine/aot/EngineDigest.generated.h@@ -6,7 +6,7 @@/* One per address width, because the width is IN the digest: a host thattook the other one would refuse every module built for it. */-#define ES_ENGINE_ABI_DIGEST_32 0x7cfc11262dd79126ULL-#define ES_ENGINE_ABI_DIGEST_64 0x03e22db83f2a93daULL+#define ES_ENGINE_ABI_DIGEST_32 0xa3962ff88387fdfcULL+#define ES_ENGINE_ABI_DIGEST_64 0xaa8849ed8d31d9a0ULL#define ES_ENGINE_ABI_DIGEST \(sizeof(es_addr_t) == 8 ? ES_ENGINE_ABI_DIGEST_64 : ES_ENGINE_ABI_DIGEST_32)diff --git a/tests/aot/generated/move_system_decl.c b/tests/aot/generated/move_system_decl.cindex 2825c702e..480aecd25 100644--- a/tests/aot/generated/move_system_decl.c+++ b/tests/aot/generated/move_system_decl.c@@ -10,7 +10,7 @@answer, it is a read of a different field. The address width belongsto the MACHINE, so it mixes in here rather than in the compiler and a32-bit artifact cannot load into a 64-bit host. */-#define ES_ABI_ENGINE_DIGEST 0x03e22db83f2a93daULL+#define ES_ABI_ENGINE_DIGEST 0xaa8849ed8d31d9a0ULLES_EXPORT const uint64_t es_abi_hash =ES_ABI_ENGINE_DIGEST ^ (0x9e3779b97f4a7c15ULL * (uint64_t)sizeof(es_addr_t));diff --git a/tests/aot/generated/move_system_hash.h b/tests/aot/generated/move_system_hash.hindex b52a6fed4..062d08ee5 100644--- a/tests/aot/generated/move_system_hash.h+++ b/tests/aot/generated/move_system_hash.h@@ -1,3 +1,3 @@/* GENERATED by @estella/compiler — do not edit. *//* The contract half of the handshake. The width half is the harness. */-#define ES_EXPECTED_CONTRACT_HASH 0x03e22db83f2a93daULL+#define ES_EXPECTED_CONTRACT_HASH 0xaa8849ed8d31d9a0ULLdiff --git a/tools/lib/contractFacts.mjs b/tools/lib/contractFacts.mjsindex ccfaf1d81..f52daba79 100644--- a/tools/lib/contractFacts.mjs+++ b/tools/lib/contractFacts.mjs@@ -149,10 +149,11 @@ export const FACTS = [verification: [{ path: 'compiler/tests/exact-trig.test.ts', how: 'test', probe: /bit for bit/i },],- digest: null,- owed: 'no digest covers it: a module built against a different polynomial loads and'- + ' answers different pixels. The differential is bit-exact but needs a host C'- + ' compiler, so a checkout without one verifies nothing here.',+ digest: { name: 'engine-abi', path: 'sdk/src/ecs/aot/abiDigest.ts', probe: /trig=/ },+ owed: 'the digest is taken of the TS side, so it refuses a module built against'+ + ' different arithmetic — but a change to the C half ALONE moves nothing, and'+ + ' only the differential sees it. That needs a host C compiler, so a checkout'+ + ' without one verifies the two implementations against nothing.',},{id: 'tweenWireEnums',