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
opModGetandopMakeModCell. 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:
opModGetfinds 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 inAux1, which onlyopModGet,opMakeModCellandGoccia.Modulesread 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
WeakMapkey is ordinary. - Code from a file is checked against the kind.
opModGetandopMakeModCellrefuse a register that holds anything but anokModCellsobject with the "Invalid bytecode" TypeError, where they refused anything but an array before, andopModGetstill refuses a slot that is not a cell or none. - The compiler's code reads the table as before, one indirection through
Aux1instead ofElements: FastLoop masks the tag, and the baseline JIT, which the optimizing tier embeds for this instruction, untags it with onexor. - 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%modcellsis 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 falseforArray.isArray,typeof, the prototype,Reflect.ownKeys,typeof table[0]andObject.isExtensible, and aTypeErrorfor aWeakMapkey oftable[0], a write and a definition.scripts/engine/artifact-tamper-tests.pychecks both paths. structuredClonerefuses 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.paschecks 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
constarrow, 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.