fix(wasm): keep linking-critical custom sections when protecting a module (#186)

This commit is contained in:
Dery Ferdika
2026-11-13 10:38:17 +07:00
committed by GitHub
parent 862a56c241
commit f7cdd44735
2 changed files with 89 additions and 1 deletions
+30 -1
View File
@@ -594,6 +594,23 @@ export function appendWasmCustomSection(
* 2. Multiple `@custom` architectural chaff sections (`"bla.enclave.manifest"`, `"producers"`, `"dylink.0"`)
* while keeping the WASM module 100% valid and executable by V8 / WebAssembly runtimes.
*/
/**
* Custom sections a linker or the host runtime needs to load the module:
* `dylink.0`/`linking`/`reloc.*` for dynamically linked modules (Emscripten side
* modules) and `target_features` for feature negotiation. Debug/metadata
* sections such as `name` and `producers` are still stripped.
*/
const LINKING_CRITICAL_CUSTOM_SECTIONS: ReadonlySet<string> = new Set([
"dylink.0",
"dylink",
"linking",
"target_features",
]);
export function isLinkingCriticalCustomSection(name: string): boolean {
return LINKING_CRITICAL_CUSTOM_SECTIONS.has(name) || name.startsWith("reloc.");
}
export function protectWasmBinary(
bytes: Buffer,
options: {
@@ -675,7 +692,19 @@ export function protectWasmBinary(
if (sectionEnd > bytes.length) break;
if (sectionId === 0) {
// strip original custom/debug
// Strip original custom/debug sections, but keep the ones a linker or the
// host runtime needs: dropping dylink/linking/reloc breaks dynamically
// linked modules, and target_features breaks host feature negotiation.
const payload = bytes.subarray(nextOffset, sectionEnd);
let keepCustom = false;
try {
const { value: nameLen, nextOffset: nameStart } = decodeUnsignedLeb128(payload, 0);
const customName = payload.subarray(nameStart, nameStart + nameLen).toString("utf8");
keepCustom = isLinkingCriticalCustomSection(customName);
} catch {
keepCustom = false;
}
if (keepCustom) keptChunks.push(Buffer.from(bytes.subarray(sectionStart, sectionEnd)));
} else if (sectionId === 7) {
let payload = bytes.subarray(nextOffset, sectionEnd);
// Issue #40.3: optional rename of non-public / internal exports first.
@@ -0,0 +1,59 @@
import { describe, expect, it } from "vitest";
import { inspectWasmBinary, protectWasmBinary } from "../src/shields/formats/wasm.js";
const HEADER = Buffer.from([0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00]);
function leb(n: number): Buffer {
const out: number[] = [];
let v = n;
do {
let byte = v & 0x7f;
v >>>= 7;
if (v !== 0) byte |= 0x80;
out.push(byte);
} while (v !== 0);
return Buffer.from(out);
}
function customSection(name: string, content: string): Buffer {
const nameBytes = Buffer.from(name, "utf8");
const payload = Buffer.concat([leb(nameBytes.length), nameBytes, Buffer.from(content, "utf8")]);
return Buffer.concat([Buffer.from([0x00]), leb(payload.length), payload]);
}
const LINKING_SECTIONS = ["dylink.0", "linking", "reloc.CODE", "target_features"] as const;
describe("WASM protection preserves linking-critical custom sections", () => {
const input = Buffer.concat([
HEADER,
customSection("name", "debug-module-name"),
customSection("producers", "producer=toolchain"),
...LINKING_SECTIONS.map((name) => customSection(name, `payload-for-${name}`)),
]);
it("keeps dylink/linking/reloc/target_features and still drops debug metadata", () => {
const res = protectWasmBinary(input, { intensity: "balanced", seed: "wasm-preserve" });
const out = Buffer.isBuffer(res) ? res : res.buffer;
for (const name of LINKING_SECTIONS) {
expect(out.toString("latin1")).toContain(`payload-for-${name}`);
}
expect(out.toString("latin1")).not.toContain("producer=toolchain");
// The module must still be a parseable WASM binary with the preserved sections listed.
const inspected = inspectWasmBinary(out);
expect(inspected.validMagic).toBe(true);
for (const name of LINKING_SECTIONS) {
expect(inspected.customSectionNames).toContain(name);
}
});
it("still drops unrelated custom sections", () => {
const res = protectWasmBinary(
Buffer.concat([HEADER, customSection("producers", "producer=other")]),
{ intensity: "balanced", seed: "wasm-drop" },
);
const out = Buffer.isBuffer(res) ? res : res.buffer;
expect(out.toString("latin1")).not.toContain("producer=other");
});
});