Files
gex/hosts/kernel/add-memory.mjs
velzieandClaude Opus 5.5 a6e7c17f9e Restructure: hosts/, page/, ports/, site/, toolchain/, wine/, scripts/
The tree by what each part is, behind the top-level Makefile:
- shims/ -> hosts/ (the browser, kernel, native and Wayland hosts)
- web/ -> page/ (the page's source, build and serve), ports/mesa,
  ports/xwayland and ports/tint (the ports the page loads), site/ (static
  sites and the eject page)
- tools/gexcc -> toolchain/gexcc, with the sysroot and busybox builds from
  tools/wine-wasm in toolchain/sysroot and toolchain/busybox, and one
  toolchain/pins (musl, the LP64 musl, LLVM, busybox)
- tools/wine-wasm -> wine/: dist.sh (was wine64-dist.sh), wine.pins (64-bit
  Wine, and the 32-bit tree its wineserver builds from), libs.pins (the Unix
  libraries), the 32-bit-only pieces in wine/legacy32 (its own pins and
  legacy.mk, which wine/Makefile includes)
- tools/*.py and tools/*.sh -> scripts/ (the GL generators in scripts/gl)
- wine32/src.sh is scripts/fetch-pinned.sh, which takes the trees the old
  script marked as its own

References follow the moves: paths from the tree's root by the map, paths
from a script's own directory by where their target went.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_e0aa53aa95eb4b86910519b4d14812a5
2026-11-04 17:30:24 -05:00

165 lines
6.0 KiB
JavaScript

// Give a linked wasm module a second imported memory.
//
// node hosts/kernel/add-memory.mjs <in.wasm> <out.wasm> [module] [name] [min] [shared] [max]
//
// The global kernel module addresses its own memory as memory 0 -- that is
// what `--import-memory` gives it and what every Rust pointer in it means --
// and reaches a guest's buffers in a *second* memory with `memory.copy`.
// `gex-kmem` emits those instructions, but nothing in the Rust toolchain can
// declare the memory they name: there is no attribute for it, and wasm-ld
// emits exactly one memory. Without the declaration the module is not valid
// wasm and will not compile.
//
// So it is added here, after linking. This is a small edit and a safe one:
// memories have their own index space, so appending one cannot disturb a
// function, table or global index, and appending it *after* the existing
// import leaves the module's own memory at 0 where all its code expects it.
import { readFileSync, writeFileSync } from 'node:fs';
// A process's address space, in wasm pages. Growable: `min` is what the host
// allocates when the process is created -- `GUEST_MEM_INITIAL_BYTES`, the
// fixed layout below the arena -- and `max` is how far it may grow. They are
// different numbers because there is one memory per process and the first is
// paid for immediately.
const GUEST_MAX_PAGES = 0xffff0000 / 65536;
const GUEST_MIN_PAGES = 0x07000000 / 65536;
const [
inPath,
outPath,
mod = 'env',
name = 'process',
pages,
shared = '1',
maxPages = GUEST_MAX_PAGES,
// `64`: a memory64 (limits flag 0x04), for processes with a 64-bit guest
// memory.
index = '32',
] = process.argv.slice(2);
if (!inPath || !outPath) {
console.error(
'usage: add-memory.mjs <in.wasm> <out.wasm> [module] [name] [min] [shared] [max] [32|64]',
);
process.exit(2);
}
const src = new Uint8Array(readFileSync(inPath));
/** LEB128 at `at`: the value, and where it ended. */
function uleb(bytes, at) {
let value = 0, shift = 0, b;
do {
b = bytes[at++];
value |= (b & 0x7f) << shift;
shift += 7;
} while (b & 0x80);
return [value >>> 0, at];
}
/** LEB128, encoded. */
function enc(n) {
const out = [];
do {
let b = n & 0x7f;
n >>>= 7;
if (n) b |= 0x80;
out.push(b);
} while (n);
return out;
}
const str = (s) => [...enc(s.length), ...[...s].map((c) => c.charCodeAt(0))];
// The entry to append: kind 0x02 (memory), limits flag 0x03 -- a maximum, and
// shared.
//
// Shared, because the process memory *is* shared: it is one guest process's
// address space and every thread of that process is a Worker running on it.
// Only the sharing is narrower than the kernel's, which spans every process.
// So there is no "shared one and plain one" here; there are two shared
// memories with different scopes.
//
// That matters concretely rather than nominally: a memory import's `shared`
// flag is part of its *type*, and so are its minimum and maximum. A module
// that declares a plain memory will not accept a `SharedArrayBuffer` however
// large it is, and one that declares the wrong bounds will not link at all.
// Both numbers are the guest's fixed address space.
//
// `shared` is an argument for exactly that reason. A process whose threads
// are Workers has a shared memory and needs flag 0x03; a process that is a
// single Worker has a plain one and needs 0x01, and a module built for the
// first will not instantiate against the second. So the kernel module is
// emitted twice, once for each, and a host picks by what its processes are.
// An empty argument means "whatever the layout says", so the size lives in
// one place rather than in every caller's command line.
const min = pages ? Number(pages) : GUEST_MIN_PAGES;
const entry = [
...str(mod), ...str(name),
0x02, (shared === '0' ? 0x01 : 0x03) | (index === '64' ? 0x04 : 0),
...enc(min), ...enc(Number(maxPages)),
];
let at = 8; // past the magic and version
// Chunks, concatenated once at the end, rather than one big array pushed
// into. `out.push(...section)` spreads every byte as an argument, and a
// section of any real size overflows the call stack -- which it duly did as
// soon as this module grew a standard library.
const chunks = [src.subarray(0, 8)];
let found = false;
while (at < src.length) {
const id = src[at];
let size, bodyAt;
[size, bodyAt] = uleb(src, at + 1);
const end = bodyAt + size;
if (id === 2) {
// Rewrite the import section with one more entry. The count is a LEB and
// may itself grow a byte, which is why the section length is recomputed
// rather than adjusted.
let count, after;
[count, after] = uleb(src, bodyAt);
const body = Uint8Array.from([
...enc(count + 1),
...src.subarray(after, end),
...entry,
]);
chunks.push(Uint8Array.from([2, ...enc(body.length)]), body);
found = true;
} else {
chunks.push(src.subarray(at, end));
}
at = end;
}
if (!found) {
// A module that imports nothing at all has no section to extend. That is
// not the kernel module -- it imports its own memory -- so this is a build
// that went wrong rather than a case to handle.
console.error(`add-memory: ${inPath} has no import section`);
process.exit(1);
}
const total = chunks.reduce((n, c) => n + c.length, 0);
const result = new Uint8Array(total);
{
let at = 0;
for (const c of chunks) {
result.set(c, at);
at += c.length;
}
}
// Compile it here rather than discovering at run time that the surgery was
// wrong. This is the only check that the appended entry is well formed and
// that the `memory.copy` instructions referring to it now validate.
// `ADD_MEMORY_MORE=1`: another memory is still to be added (the kernel's
// `process64`), whose instructions do not validate until it is.
try {
if (!process.env.ADD_MEMORY_MORE) await WebAssembly.compile(result);
} catch (err) {
console.error(`add-memory: the result does not compile: ${err.message}`);
process.exit(1);
}
writeFileSync(outPath, result);
console.log(` ${outPath}: ${result.length} bytes, imports ${mod}.${name} as its last memory`);