Browse documentation

A module's cell table is an object kind of its own

Date: 2026-11-29 Area: engine, modules, bytecode Related: Bytecode VM, Binary Format, Value System, Heap Snapshots, #2382, #2421

Context

A module's code reads its imports through its cell table: the %modcells parameter of the module function, one slot per import or local export, each slot the binding's cell (TUpval). opModGet reads a slot's cell and opMakeModCell stores one. Nested functions capture the table as an ordinary upvalue.

The table was an ordinary array. Code from a .gbc file, which the engine reads as untrusted input, can copy any register into one JavaScript reads, so it could hand JavaScript the table. JavaScript then indexed it, got a raw cell, an engine record that is not a JavaScript value (typeof said "undefined", and WeakMap accepted it as a key), and could overwrite a slot with Object.defineProperty, replacing a binding every import read of that module trusts. #2382 made opModGet in code from a file refuse a slot that is not a cell, and left this open.

A heap snapshot reached the same assumption from the other side. A closure that reads an import captures the table, so the writer wrote it as an array of cells, and the reader restored any array a snapshot named in that binding. A snapshot with an array holding an object there restored, and the closure's code would read the object as a cell. A snapshot could also name a module's own code as a closure's, which a caller would run with any value as its table.

Three ways to close it were weighed:

  • A verifier rule that the table register is only ever an operand of opModGet and opMakeModCell. Nested functions copy the table from an upvalue into a temporary first, so the rule needs a data-flow analysis over every path, exception handlers included, and still leaves every other way a register reaches JavaScript to enumerate.
  • No register at all: opModGet finds the table through its function. That changes the instruction format, the compiler, eval's import names and all four tiers.
  • A value kind of its own for the table, so whatever path hands it over hands over nothing.

Decision

Johannes chose a value kind of its own (29 November 2026, "Agreed, let's go").

  • The table is an object of kind okModCells (Goccia.Core, NewModCells). Its slots are a value array in Aux1, which only opModGet, opMakeModCell and Goccia.Modules read or write (ModCellsOf). It has no elements, no properties and a null prototype, and it cannot be extended.
  • JavaScript that gets one holds an inert object. It is not an array, has no own keys, cannot be given a property or a definition, and gives no path to a cell. It is an object, so using it as a WeakMap key is ordinary.
  • Code from a file is checked against the kind. opModGet and opMakeModCell refuse a register that holds anything but an okModCells object with the "Invalid bytecode" TypeError, where they refused anything but an array before, and opModGet still refuses a slot that is not a cell or none.
  • The compiler's code reads the table as before, one indirection through Aux1 instead of Elements: FastLoop masks the tag, and the baseline JIT, which the optimizing tier embeds for this instruction, untags it with one xor.
  • Heap snapshots never carry a table. The writer has no codec for the kind, so a closure that reads an import refuses (no snapshot codec for TGocciaModuleCellTable, where it refused at restore before), and the reader refuses a closure whose code captures %modcells, whatever value the snapshot gives that binding. It also refuses a closure of a Script's or module's own code, which no value holds and whose %modcells is its caller's argument.

Consequences

  • A file that copies the table into JavaScript, from the module function or from a nested function's upvalue, gets false object null [] undefined false for Array.isArray, typeof, the prototype, Reflect.ownKeys, typeof table[0] and Object.isExtensible, and a TypeError for a WeakMap key of table[0], a write and a definition. scripts/engine/artifact-tamper-tests.py checks both paths.
  • structuredClone refuses the table, as it refuses other engine objects; String(table) throws as for any object with a null prototype.
  • A snapshot of a closure that reads an import refuses when it is written rather than when it is restored. scripts/engine/module-cell-table-snapshot-test.pas checks that, and that both forgeries refuse at restore.
  • An import read costs the untag more: one instruction per read in the baseline JIT, three in FastLoop. On #1634's repro (2M calls of an imported const arrow, callgrind, prod build) a top-level loop goes from 658.72M to 660.72M instructions (+0.30%) with the JIT and from 1,695.98M to 1,701.98M (+0.35%) without it; the loop inside a function goes from 625.02M to 627.02M (+0.32%) and from 1,752.11M to 1,758.11M (+0.34%).
  • An object kind number (30) is taken. A future change to how imports are read (#2384) reads the slots through ModCellsOf.