Skip to content

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.

fix(abi): the fourth ABI struct was outside the compatibility system

yhhd46af5fce221
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 03966ef83..24625f6df 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -14,6 +14,36 @@ published separately; it ships inside the editor.
## [Unreleased]
+### Fixed
+
+- **A fourth ABI struct was outside the compatibility system entirely.** Three of
+ the structs a compiled system is handed carry the same three things: a word
+ count authored once in `abiDigest.ts`, a `typedef char es_check_*[...]` in the
+ generated C that refuses to compile if the struct stops matching it, and a term
+ in the engine ABI digest so a module built against the old shape is turned away
+ rather than loaded. `EsEventOut` — the header a system's event queue starts
+ with — had none of them.
+
+ The number three was instead written twice and bound to nothing: once in the
+ runtime that lays the block out (`AotContext`) and once in the differential
+ host (`compiler/src/abi.ts`, as a bare `3 * 4`). A field appended to the struct
+ by either one is a header the other keeps stepping over — silently, since the
+ addresses stay valid and only the values are wrong. Nothing would have said so:
+ the digest did not know the struct existed, so a module compiled before the
+ change kept loading.
+
+ `EVENT_OUT_WORDS` now has the one author its three siblings have, both layout
+ sites derive from it, the generated header declares `es_check_eventout`, and
+ the digest names it. **The engine ABI digest changes**, so an AOT module built
+ against 0.60.0 or earlier is refused by a 0.61 host and must be rebuilt — which
+ is the mechanism working, not a regression.
+
+ Every check is now sabotaged in both directions in `compiler/tests/abi-structs.test.ts`
+ — a field added and a field dropped must each fail to compile, naming that
+ check — and the same file compares the ground against the claim: every
+ all-address struct in the contract header must be one the digest covers, so the
+ next one added without a check is a red rather than a silence.
+
## [0.60.0] - 2026-09-02
### Added
diff --git a/compiler/src/abi.ts b/compiler/src/abi.ts
index ce890abd0..27e262751 100644
--- a/compiler/src/abi.ts
+++ b/compiler/src/abi.ts
@@ -20,6 +20,7 @@
*/
import {
CMD_WORDS as CMD_WORDS_, CMD_DESPAWN as CMD_DESPAWN_, CMD_REMOVE as CMD_REMOVE_,
+ EVENT_OUT_WORDS as EVENT_OUT_WORDS_,
QUERYROWS_WORDS as QUERYROWS_WORDS_, SYSCTX_WORDS as SYSCTX_WORDS_,
engineAbiDigest, projectShapeDigest, type ShapeDigestInput,
} from '../../sdk/src/ecs/aot/abiDigest';
@@ -34,7 +35,7 @@ import { runSystemOn, type EirHost, type Fns } from './interp';
* because the code generator and this host both name them.
*/
export {
- CMD_WORDS, CMD_DESPAWN, CMD_REMOVE, SYSCTX_WORDS, QUERYROWS_WORDS,
+ CMD_WORDS, CMD_DESPAWN, CMD_REMOVE, SYSCTX_WORDS, QUERYROWS_WORDS, EVENT_OUT_WORDS,
} from '../../sdk/src/ecs/aot/abiDigest';
/**
@@ -344,7 +345,7 @@ export function materialize(mem: AbiMemory, plan: SysPlan): AbiCall {
const eventBuf = mem.scratch(Math.max(8, eventCap * 8));
const eventCount = mem.scratch(4);
mem.u32[eventCount >> 2] = 0;
- const eventOut = mem.scratch(3 * 4);
+ const eventOut = mem.scratch(EVENT_OUT_WORDS_ * 4);
mem.u32[eventOut >> 2] = eventBuf;
mem.u32[(eventOut >> 2) + 1] = eventCap;
mem.u32[(eventOut >> 2) + 2] = eventCount;
diff --git a/compiler/src/codegen.ts b/compiler/src/codegen.ts
index adab011ea..17effda1f 100644
--- a/compiler/src/codegen.ts
+++ b/compiler/src/codegen.ts
@@ -29,7 +29,7 @@
*/
import { resourceMethodBit } from '../../sdk/src/ecs/resourceShapes';
import {
- CMD_WORDS, QUERYROWS_WORDS, SYSCTX_WORDS, CMD_DESPAWN, CMD_REMOVE,
+ CMD_WORDS, QUERYROWS_WORDS, SYSCTX_WORDS, EVENT_OUT_WORDS, CMD_DESPAWN, CMD_REMOVE,
abiHandshake, planFor,
type AbiLayout, type FieldOffsets, type Leaf, type SysPlan,
} from './abi';
@@ -186,7 +186,10 @@ typedef struct EsSysCtx {
comes from the manifest — the host declared it.
Its own queue rather than a wider EsCmd: fixed-width records and
- variable-width ones cannot share a buffer without stride guesswork. */
+ variable-width ones cannot share a buffer without stride guesswork.
+
+ ${EVENT_OUT_WORDS} addresses. A host lays this block out from that same
+ number, so a field appended here without it is a header one side walks past. */
typedef struct EsEventOut {
es_addr_t buf; /* double * */
es_addr_t cap; /* capacity, in doubles */
@@ -213,11 +216,15 @@ typedef struct EsSystemDecl {
/* The C and TS sides of these structs have ONE author, and a side that drifts
from it must fail to compile. The numbers below are written by that author
- (compiler/src/abi.ts) into this file; these three declarations are how C says
- no if a struct above ever stops matching them. */
+ (sdk/src/ecs/aot/abiDigest.ts, through compiler/src/abi.ts) into this file;
+ these four declarations are how C says no if a struct above ever stops
+ matching them. Every struct a host addresses by word owes one: EsEventOut
+ went without for as long as it existed, and a fourth field would have been
+ read as the next struct by whichever side had not been told. */
typedef char es_check_sysctx[sizeof(EsSysCtx) == ${SYSCTX_WORDS} * sizeof(es_addr_t) ? 1 : -1];
typedef char es_check_rows[sizeof(EsQueryRows) == ${QUERYROWS_WORDS} * sizeof(es_addr_t) ? 1 : -1];
typedef char es_check_cmd[sizeof(EsCmd) == ${CMD_WORDS} * 4 ? 1 : -1];
+typedef char es_check_eventout[sizeof(EsEventOut) == ${EVENT_OUT_WORDS} * sizeof(es_addr_t) ? 1 : -1];
/* memcpy, not a cast through a typed pointer: a component base is whatever the
host handed over, and a wider load through an under-aligned pointer is
diff --git a/compiler/tests/abi-structs.test.ts b/compiler/tests/abi-structs.test.ts
new file mode 100644
index 000000000..474454d5c
--- /dev/null
+++ b/compiler/tests/abi-structs.test.ts
@@ -0,0 +1,135 @@
+// SPDX-License-Identifier: Apache-2.0
+// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
+/**
+ * @file abi-structs.test.ts
+ * @brief A struct a host addresses by WORD is a compatibility fact, and this
+ * is what makes it impossible to change one quietly.
+ *
+ * @details `EsEventOut` shipped for as long as it existed with none of the three
+ * things its siblings had: no word count in `abiDigest.ts`, no
+ * `es_check_` in the C header, no term in the engine digest. The number
+ * three was written once in the runtime (`AotContext`) and again in the
+ * differential host (`abi.ts`), so a fourth field appended by either
+ * would have been a header the other kept stepping over — and nothing
+ * in the ABI compatibility system knew a fact lived there at all.
+ *
+ * Two halves, because two different things can go wrong:
+ *
+ * - **The C says no.** Every check is sabotaged in both directions, a
+ * field added and a field dropped, and the compiler must name THAT
+ * check. A control compile first, so a red means the sabotage and
+ * not the harness.
+ * - **Nothing new slips in uncovered.** The ground is the header's
+ * all-address structs; the claim is the digest's sizes line. A
+ * future struct with neither is a finding here rather than a silence
+ * for however long it takes someone to append a field to it.
+ */
+import { describe, it, expect } from 'vitest';
+import { spawnSync } from 'node:child_process';
+import { mkdtempSync, writeFileSync } from 'node:fs';
+import { tmpdir } from 'node:os';
+import { join } from 'node:path';
+
+import { CFLAGS, RUNTIME_H } from '../src/codegen';
+import { findHostCC } from '../src/hostCC';
+import {
+ CMD_WORDS, EVENT_OUT_WORDS, QUERYROWS_WORDS, SYSCTX_WORDS, engineAbiParts,
+} from '../../sdk/src/ecs/aot/abiDigest';
+
+const CC = findHostCC();
+
+/**
+ * The header with one line ending. The sources it is written in are CRLF on
+ * this platform and every anchor below is a newline; LF is what the emitted
+ * artifact carries anyway.
+ */
+const HEADER = RUNTIME_H.split('\r\n').join('\n');
+
+/** The four the header declares a check for, and the word count each claims. */
+const CHECKED = [
+ { struct: 'EsSysCtx', check: 'es_check_sysctx', term: `sysctx=${SYSCTX_WORDS}` },
+ { struct: 'EsQueryRows', check: 'es_check_rows', term: `rows=${QUERYROWS_WORDS}` },
+ { struct: 'EsCmd', check: 'es_check_cmd', term: `cmd=${CMD_WORDS}` },
+ { struct: 'EsEventOut', check: 'es_check_eventout', term: `eventout=${EVENT_OUT_WORDS}` },
+] as const;
+
+/** `typedef struct EsFoo { ... } EsFoo;` -> its body, as the header wrote it. */
+function bodyOf(struct: string): string {
+ const open = `typedef struct ${struct} {\n`;
+ const close = `\n} ${struct};`;
+ const at = HEADER.indexOf(open);
+ const end = at < 0 ? -1 : HEADER.indexOf(close, at);
+ if (end < 0) throw new Error(`${struct} not found in the contract header`);
+ return HEADER.slice(at + open.length, end);
+}
+
+/** Compile a TU that only includes the header; nothing runs, the check is static. */
+function compile(header: string): { status: number | null; stderr: string } {
+ const dir = mkdtempSync(join(tmpdir(), 'estella-abi-struct-'));
+ writeFileSync(join(dir, 'estella_abi.h'), header);
+ writeFileSync(join(dir, 'main.c'), '#include "estella_abi.h"\nint main(void) { return 0; }\n');
+ const exe = join(dir, `probe${process.platform === 'win32' ? '.exe' : ''}`);
+ const r = spawnSync(CC!, [...CFLAGS, '-o', exe, join(dir, 'main.c'), '-lm'], { encoding: 'utf8' });
+ return { status: r.status, stderr: r.stderr ?? '' };
+}
+
+describe('a word-addressed ABI struct cannot change quietly', () => {
+ it('reports whether this gate could run at all', () => {
+ if (!CC) console.warn('[abi-structs] NO C COMPILER — the sabotage did NOT run.');
+ });
+
+ it.skipIf(!CC)('compiles as shipped, so a red below is the sabotage', () => {
+ const built = compile(HEADER);
+ expect(built.status, built.stderr).toBe(0);
+ });
+
+ for (const { struct, check } of CHECKED) {
+ it.skipIf(!CC)(`${check} refuses a field appended to ${struct}`, () => {
+ const body = bodyOf(struct);
+ const wider = HEADER.replace(body, `${body}\n uint32_t es_sabotage;`);
+ expect(wider).not.toBe(HEADER);
+ const built = compile(wider);
+ expect(built.status, 'a wider struct compiled').not.toBe(0);
+ // Naming the check rules out a red for some unrelated reason.
+ expect(built.stderr).toContain(check);
+ });
+
+ it.skipIf(!CC)(`${check} refuses a field dropped from ${struct}`, () => {
+ const body = bodyOf(struct);
+ const narrower = HEADER.replace(body, body.split('\n').slice(1).join('\n'));
+ expect(narrower).not.toBe(HEADER);
+ const built = compile(narrower);
+ expect(built.status, 'a narrower struct compiled').not.toBe(0);
+ expect(built.stderr).toContain(check);
+ });
+ }
+});
+
+describe('what the engine handshake is taken of', () => {
+ const parts = engineAbiParts(4).join('\n');
+
+ for (const { struct, term } of CHECKED) {
+ it(`covers ${struct}, so widening it invalidates a built module`, () => {
+ expect(parts).toContain(term);
+ });
+ }
+
+ /**
+ * Ground against claim: a struct of nothing but addresses is one a host
+ * walks by word, so its width is a compatibility fact and the digest owes it
+ * a term. `EsCmd` is the exception — four `uint32_t` at every address width,
+ * which is why its check is the only one not against `sizeof(es_addr_t)`.
+ */
+ it('and every all-address struct in the header is one of them', () => {
+ const all = [...HEADER.matchAll(/typedef struct (Es\w+) \{\n([\s\S]*?)\n\} \1;/g)];
+ expect(all.length).toBeGreaterThanOrEqual(CHECKED.length);
+ const byWord = all
+ .filter(([, , body]) => body!.split('\n').every(
+ (line) => /^\s*es_addr_t \w+;\s*$/.test(line.replace(/\/\*[\s\S]*?\*\//g, '')),
+ ))
+ .map(([, name]) => name!);
+ expect(byWord.sort()).toEqual(
+ CHECKED.filter((c) => c.struct !== 'EsCmd').map((c) => c.struct).sort(),
+ );
+ });
+});
diff --git a/sdk/src/ecs/aot/AotContext.ts b/sdk/src/ecs/aot/AotContext.ts
index 998130fb6..cf2259965 100644
--- a/sdk/src/ecs/aot/AotContext.ts
+++ b/sdk/src/ecs/aot/AotContext.ts
@@ -18,13 +18,13 @@
*/
import type { PoolBlock, PoolMemory } from '../ScriptPool';
-import { CMD_WORDS, QUERYROWS_WORDS, SYSCTX_WORDS } from './abiDigest';
+import { CMD_WORDS, EVENT_OUT_WORDS, QUERYROWS_WORDS, SYSCTX_WORDS } from './abiDigest';
// The struct sizes and the command kinds have one author, and it is not this
// file: `abiDigest.ts`, which the compiler reads too. Re-exported so call sites
// name the ABI rather than the digest.
export {
- SYSCTX_WORDS, QUERYROWS_WORDS, CMD_WORDS, CMD_DESPAWN, CMD_REMOVE,
+ SYSCTX_WORDS, QUERYROWS_WORDS, CMD_WORDS, EVENT_OUT_WORDS, CMD_DESPAWN, CMD_REMOVE,
} from './abiDigest';
/** One record the compiled code appended. */
@@ -33,14 +33,10 @@ export interface AotCommand {
readonly a: number;
}
-
/**
* The per-call scratch, in engine memory. One instance per system is enough —
* a call is over before the next begins, and nothing may outlive it.
*/
-/** The header a system's event queue starts with: buf, cap, count. */
-const EVENT_OUT_WORDS = 3;
-
export class AotContext {
private block: PoolBlock | null = null;
private words: Uint32Array = EMPTY;
diff --git a/sdk/src/ecs/aot/abiDigest.ts b/sdk/src/ecs/aot/abiDigest.ts
index bcf15f96f..18b4e915d 100644
--- a/sdk/src/ecs/aot/abiDigest.ts
+++ b/sdk/src/ecs/aot/abiDigest.ts
@@ -11,7 +11,7 @@
* Two digests, not one, because the mismatches have different fixes:
*
* - `engineAbi` is everything about the ENGINE the module baked in:
- * EHT's struct offsets, the resource shapes, the sizes of the three
+ * EHT's struct offsets, the resource shapes, the sizes of the four
* ABI structs and the width of an address. A mismatch means rebuild
* the module against this engine.
* - `projectShapes` is the `defineComponent` shapes it compiled
@@ -30,9 +30,16 @@
import { PTR_LAYOUTS } from '../../wasm/ptrLayouts.generated';
import { RESOURCE_NAMES, RESOURCE_SHAPES, resourceLayout } from '../resourceShapes';
-/** Sizes of the three ABI structs, in address-wide words. */
+/** Sizes of the four ABI structs, in address-wide words. */
export const SYSCTX_WORDS = 6;
export const QUERYROWS_WORDS = 2;
+/**
+ * The header a system's event queue starts with: buf, cap, count. Here rather
+ * than beside either consumer because BOTH the runtime and the differential
+ * host lay this block out, and the number was written in each of them: a fourth
+ * field appended by one is three words the other keeps stepping over.
+ */
+export const EVENT_OUT_WORDS = 3;
/** A command is four u32 at every address width. */
export const CMD_WORDS = 4;
@@ -66,7 +73,7 @@ export function engineAbiDigest(addressBytes: AddressBytes): string {
export function engineAbiParts(addressBytes: AddressBytes): readonly string[] {
const parts: string[] = [
`addr=${addressBytes}`,
- `sysctx=${SYSCTX_WORDS} rows=${QUERYROWS_WORDS} cmd=${CMD_WORDS}`,
+ `sysctx=${SYSCTX_WORDS} rows=${QUERYROWS_WORDS} cmd=${CMD_WORDS} eventout=${EVENT_OUT_WORDS}`,
`cmdkinds=${CMD_DESPAWN},${CMD_REMOVE}`,
];
// EHT's table: every field of every engine component, at the offset the C++
diff --git a/src/esengine/aot/EngineDigest.generated.h b/src/esengine/aot/EngineDigest.generated.h
index 7cd1ec5fa..d66cfbc7c 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 that
took the other one would refuse every module built for it. */
-#define ES_ENGINE_ABI_DIGEST_32 0xe83b13ad2f96720cULL
-#define ES_ENGINE_ABI_DIGEST_64 0xf1506f426a7da360ULL
+#define ES_ENGINE_ABI_DIGEST_32 0x7cfc11262dd79126ULL
+#define ES_ENGINE_ABI_DIGEST_64 0x03e22db83f2a93daULL
#define ES_ENGINE_ABI_DIGEST \
(sizeof(es_addr_t) == 8 ? ES_ENGINE_ABI_DIGEST_64 : ES_ENGINE_ABI_DIGEST_32)
diff --git a/src/esengine/aot/estella_abi.h b/src/esengine/aot/estella_abi.h
index c861726d1..a91311558 100644
--- a/src/esengine/aot/estella_abi.h
+++ b/src/esengine/aot/estella_abi.h
@@ -80,7 +80,10 @@ typedef struct EsSysCtx {
comes from the manifest — the host declared it.
Its own queue rather than a wider EsCmd: fixed-width records and
- variable-width ones cannot share a buffer without stride guesswork. */
+ variable-width ones cannot share a buffer without stride guesswork.
+
+ 3 addresses. A host lays this block out from that same
+ number, so a field appended here without it is a header one side walks past. */
typedef struct EsEventOut {
es_addr_t buf; /* double * */
es_addr_t cap; /* capacity, in doubles */
@@ -107,11 +110,15 @@ typedef struct EsSystemDecl {
/* The C and TS sides of these structs have ONE author, and a side that drifts
from it must fail to compile. The numbers below are written by that author
- (compiler/src/abi.ts) into this file; these three declarations are how C says
- no if a struct above ever stops matching them. */
+ (sdk/src/ecs/aot/abiDigest.ts, through compiler/src/abi.ts) into this file;
+ these four declarations are how C says no if a struct above ever stops
+ matching them. Every struct a host addresses by word owes one: EsEventOut
+ went without for as long as it existed, and a fourth field would have been
+ read as the next struct by whichever side had not been told. */
typedef char es_check_sysctx[sizeof(EsSysCtx) == 6 * sizeof(es_addr_t) ? 1 : -1];
typedef char es_check_rows[sizeof(EsQueryRows) == 2 * sizeof(es_addr_t) ? 1 : -1];
typedef char es_check_cmd[sizeof(EsCmd) == 4 * 4 ? 1 : -1];
+typedef char es_check_eventout[sizeof(EsEventOut) == 3 * sizeof(es_addr_t) ? 1 : -1];
/* memcpy, not a cast through a typed pointer: a component base is whatever the
host handed over, and a wider load through an under-aligned pointer is
diff --git a/tests/aot/generated/move_system_decl.c b/tests/aot/generated/move_system_decl.c
index 0cebcff34..2825c702e 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 belongs
to the MACHINE, so it mixes in here rather than in the compiler and a
32-bit artifact cannot load into a 64-bit host. */
-#define ES_ABI_ENGINE_DIGEST 0xf1506f426a7da360ULL
+#define ES_ABI_ENGINE_DIGEST 0x03e22db83f2a93daULL
ES_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.h
index 61d486d4c..b52a6fed4 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 0xf1506f426a7da360ULL
+#define ES_EXPECTED_CONTRACT_HASH 0x03e22db83f2a93daULL