// SPDX-License-Identifier: MIT // Copyright (C) 2026 Pooya Parsa // The module loader's parser (js/core/kmod.ts parseSideModule) on side // modules built byte by byte: what it reads from dylink.0 and the import // section, and what it refuses. The loader itself runs against the kernels // with modules in test/kmod.test.ts. // // node --test test/kmod-host.test.ts import test from "node:test"; import assert from "node:assert/strict"; import { KmodError, parseSideModule } from "../js/core/kmod.ts"; const leb = (n: number): number[] => { const out: number[] = []; do { let b = n % 128; n = Math.floor(n / 128); if (n) b |= 0x80; out.push(b); } while (n); return out; }; const str = (s: string) => { const b = [...new TextEncoder().encode(s)]; return [...leb(b.length), ...b]; }; const section = (id: number, payload: number[]) => [id, ...leb(payload.length), ...payload]; const custom = (name: string, payload: number[]) => section(0, [...str(name), ...payload]); const sub = (type: number, payload: number[]) => [type, ...leb(payload.length), ...payload]; const memInfo = (memSize: number, memAlign: number, tableSize: number, tableAlign: number) => sub(1, [...leb(memSize), ...leb(memAlign), ...leb(tableSize), ...leb(tableAlign)]); const HEADER = [0, 0x61, 0x73, 0x6d, 1, 0, 0, 0]; const mod = (...sections: number[][]) => new Uint8Array([...HEADER, ...sections.flat()]); /** The imports of a lowered SMP module: a shared 32-bit memory, a 32-bit table, globals, a function. */ const imports = section(2, [ 6, ...str("env"), ...str("memory"), 2, 0x03, ...leb(1), ...leb(65536), ...str("env"), ...str("__indirect_function_table"), 1, 0x70, 0x00, ...leb(5), ...str("env"), ...str("__memory_base"), 3, 0x7e, 0, ...str("GOT.mem"), ...str("jiffies"), 3, 0x7e, 1, ...str("env"), ...str("kfree"), 0, ...leb(3), ...str("env"), ...str("maybe"), 0, ...leb(0), ]); test("parseSideModule: memory info, needed, weak imports, import types", () => { const dylink = custom("dylink.0", [ ...memInfo(296, 3, 5, 0), ...sub(2, [1, ...str("libfoo.so")]), ...sub(3, [1, ...str("kmt_a_add"), 0]), ...sub(4, [1, ...str("env"), ...str("maybe"), 1]), ...sub(9, [1, 2, 3]), // an unknown kind is skipped ]); const bytes = mod(dylink, imports, custom(".debug_info", [1, 2, 3])); const info = parseSideModule(bytes); assert.deepEqual([info.memSize, info.memAlign, info.tableSize, info.tableAlign], [296, 3, 5, 0]); assert.deepEqual(info.needed, ["libfoo.so"]); assert.deepEqual([...info.weak], ["env\0maybe"]); assert.equal(info.imports.length, 6); assert.deepEqual(info.imports[0], { module: "env", name: "memory", kind: "memory", limits: { address64: false, shared: true, min: 1, max: 65536 } }); assert.deepEqual(info.imports[1]!.limits, { address64: false, shared: false, min: 5, max: undefined }); assert.deepEqual(info.imports[2]!.global, { type: "i64", mutable: false }); assert.deepEqual(info.imports[3]!.global, { type: "i64", mutable: true }); }); test("parseSideModule: memory64 and table64 limits", () => { const imp = section(2, [ 2, ...str("env"), ...str("memory"), 2, 0x07, ...leb(1), ...leb(262144), ...str("env"), ...str("__indirect_function_table"), 1, 0x70, 0x04, ...leb(5), ]); const info = parseSideModule(mod(custom("dylink.0", memInfo(0, 0, 0, 0)), imp)); assert.deepEqual(info.imports[0]!.limits, { address64: true, shared: true, min: 1, max: 262144 }); assert.equal(info.imports[1]!.limits!.address64, true); }); test("parseSideModule refuses what is not a side module, naming why", () => { const ok = custom("dylink.0", memInfo(16, 0, 0, 0)); const refuse = (bytes: Uint8Array, re: RegExp) => assert.throws(() => parseSideModule(bytes), (e: unknown) => e instanceof KmodError && re.test(e.message)); refuse(new Uint8Array(0), /not a wasm module/); refuse(new Uint8Array([0x7f, 0x45, 0x4c, 0x46, 2, 1, 1, 0]), /not a wasm module/); // an ELF .ko refuse(new Uint8Array([0, 0x61, 0x73, 0x6d, 0x0d, 0, 1, 0]), /version/); // a component refuse(mod(), /no dylink\.0/); refuse(mod(section(1, [0])), /no dylink\.0/); refuse(mod(section(1, [0]), ok), /not the first section/); refuse(mod(custom("dylink.0", sub(2, [0]))), /no memory info/); refuse(mod(ok, [2, 0x80, 0x80, 0x80, 0x80, 0x80, 0x01]), /LEB128 too long/); refuse(mod(ok, [2, 10, 1]), /size past its section/); refuse(mod(ok).subarray(0, 12), /unexpected end|past its section/); refuse(mod(ok, section(2, [1, ...str("env"), ...str("x"), 9])), /import kind 9/); refuse(mod(ok, section(2, [1, ...str("env"), ...str("t"), 1, 0x70, 0x03, 1, 1])), /shared table/); refuse(mod(custom("dylink.0", memInfo(16, 40, 0, 0))), /alignment/); refuse(mod(ok, section(2, [1, 2, 0xff, 0xfe])), /malformed/); // not UTF-8 });