diff --git a/package.json b/package.json index 26fdcfe..df5bb4e 100644 --- a/package.json +++ b/package.json @@ -93,7 +93,7 @@ ], "scripts": { "test": "node scripts/flatsql-release-e2e.mjs", - "test:host-surfaces": "node --test test/node-host.test.js test/host-abi.test.js test/browser-host.test.js test/browser-module-harness.test.js test/testing-harness.test.js", + "test:host-surfaces": "node --test test/node-host.test.js test/host-abi.test.js test/browser-host.test.js test/browser-module-harness.test.js test/testing-harness.test.js test/browser-owner-tls.test.js", "test:runtime-matrix": "SPACE_DATA_MODULE_SDK_ENABLE_RUNTIME_MATRIX=1 node --test test/runtime-matrix.test.js", "test:parity": "node --test test/parity-harness.test.js", "test:parity-tri-lane": "SPACE_DATA_MODULE_SDK_ENABLE_TRI_RUNTIME_PARITY=1 node --test test/parity-tri-lane.test.js", diff --git a/src/host/browserModuleHarness.js b/src/host/browserModuleHarness.js index ca28743..cb1a2f3 100644 --- a/src/host/browserModuleHarness.js +++ b/src/host/browserModuleHarness.js @@ -378,30 +378,10 @@ async function instantiateBrowserModule(options = {}) { "wasi-threads module imports wasi.thread-spawn but no shared imported memory is available.", ); } - threadHost = await createWasiThreadSpawn({ - wasmModule: options.wasmModule, - memory: providedMemory, - processState, - // Browser warm-pool sizing: cap pooled workers at how many guest threads - // the module will actually ask for. Ignored by the Node lazy path. - requestedThreads: options.maxThreads, - poolSize: options.wasiThreadPoolSize, - hostcallChannel: options.threadHostcallChannel, - requiresHostcalls: needsHostBridge, - enableBrowserThreads: options.enableBrowserWasiThreads, - // Explicit worker anchor for BUNDLED consumers: when this host source is - // bundled into an engine artifact, `import.meta.url` inside the SDK host - // points at the bundle and the sibling worker asset 404s. A host shim - // passes the directory its build actually serves the SDK host chain from - // (e.g. OrbPro: `js/vendor/space-data-module-sdk/src/host/`). Unset = - // packaged sibling, i.e. today's unbundled behavior. - browserWorkerBaseUrl: options.wasiThreadWorkerBaseUrl, - browserWorkerUrl: options.wasiThreadWorkerUrl, - spawnWaitMs: options.wasiThreadSpawnWaitMs, - }); + importObject.wasi = { ...(importObject.wasi ?? {}), - "thread-spawn": threadHost.threadSpawn, + "thread-spawn": (...args) => threadHost.threadSpawn(...args), }; } @@ -437,6 +417,32 @@ async function instantiateBrowserModule(options = {}) { } wasi.setMemory(memory); } + // Instantiate the owner before secondary instances can initialize shared + // memory. The WASI startup winner establishes the owner's TLS base. + // Constructors run after the pool is ready so they can spawn threads. + if (isThreaded) { + threadHost = await createWasiThreadSpawn({ + wasmModule: options.wasmModule, + memory: providedMemory, + processState, + // Browser warm-pool sizing: cap pooled workers at how many guest threads + // the module will actually ask for. Ignored by the Node lazy path. + requestedThreads: options.maxThreads, + poolSize: options.wasiThreadPoolSize, + hostcallChannel: options.threadHostcallChannel, + requiresHostcalls: needsHostBridge, + enableBrowserThreads: options.enableBrowserWasiThreads, + // Explicit worker anchor for BUNDLED consumers: when this host source is + // bundled into an engine artifact, `import.meta.url` inside the SDK host + // points at the bundle and the sibling worker asset 404s. A host shim + // passes the directory its build actually serves the SDK host chain from + // (e.g. OrbPro: `js/vendor/space-data-module-sdk/src/host/`). Unset = + // packaged sibling, i.e. today's unbundled behavior. + browserWorkerBaseUrl: options.wasiThreadWorkerBaseUrl, + browserWorkerUrl: options.wasiThreadWorkerUrl, + spawnWaitMs: options.wasiThreadSpawnWaitMs, + }); + } initializeInstance(instance, options.directSurface === true); } catch (error) { await threadHost?.terminateAll(); diff --git a/test/browser-owner-tls.test.js b/test/browser-owner-tls.test.js new file mode 100644 index 0000000..c645e2a --- /dev/null +++ b/test/browser-owner-tls.test.js @@ -0,0 +1,124 @@ +// A prewarmed secondary instance must not claim the shared-memory startup that +// establishes the owner's TLS base. Compile the fixture through the SDK; no +// binary or toolchain-specific address is pinned. The reversed order in Node +// is a negative control for the same TLS/stack overlap observed in browsers. +import test from "node:test"; +import assert from "node:assert/strict"; +import fs from "node:fs/promises"; +import http from "node:http"; +import os from "node:os"; +import path from "node:path"; +import { resolveWasiThreadsToolchain } from "../src/compiler/wasiThreadsToolchain.js"; +import { fileURLToPath } from "node:url"; +import { compileModuleFromSource, cleanupCompilation } from "../src/compiler/compileModule.js"; +import { createBrowserModuleHarness } from "../src/host/browserModuleHarness.js"; +import { threadWaveToolchainAvailable } from "./support/threadWaveGuest.mjs"; + +const available = threadWaveToolchainAvailable(); +const browserEnabled = process.env.SPACE_DATA_MODULE_SDK_ENABLE_BROWSER_THREADS === "1"; +const browsers = (process.env.SPACE_DATA_MODULE_SDK_BROWSERS ?? "chromium,firefox,webkit").split(","); +let compilation, compilerDir; +async function guest() { + if (compilation) return compilation.wasmBytes; + // Match modules linked with --stack-first: an uninitialized TLS base then + // aliases the owner stack. The compiler override remains local to this test. + const toolchain = resolveWasiThreadsToolchain(); + compilerDir = await fs.mkdtemp(path.join(os.tmpdir(), "owner-tls-compiler-")); + const wrapper = path.join(compilerDir, "clangxx"); + await fs.writeFile(wrapper, "#!/usr/bin/env node\n" + + "const {execFileSync}=require('node:child_process');const a=process.argv.slice(2);" + + "if(!a.includes('-c')&&!a.includes('--version'))a.push('-Wl,--stack-first');" + + "try{execFileSync(" + JSON.stringify(toolchain.clangxx) + ",a,{stdio:'inherit'});}catch(e){process.exit(e.status||1);}", { mode: 0o755 }); + const prior = process.env.SDN_WASI_CLANGXX; + process.env.SDN_WASI_CLANGXX = wrapper; + resolveWasiThreadsToolchain({ force: true }); + try { + compilation = await compileModuleFromSource({ + language: "c++", threadModel: "emscripten-pthreads", + sourceCode: await fs.readFile(new URL("./support/tlsOwnerGuest.cpp", import.meta.url), "utf8"), + manifest: { + pluginId: "com.digitalarsenal.test.owner-tls", name: "Owner TLS", version: "0.1.0", + pluginFamily: "analysis", runtimeTargets: ["browser", "wasmedge"], invokeSurfaces: ["direct"], + methods: [{ methodId: "probe", displayName: "Probe", inputPorts: [], outputPorts: [], maxBatch: 1, drainPolicy: "single-shot" }], + }, + }); + } finally { + if (prior === undefined) delete process.env.SDN_WASI_CLANGXX; + else process.env.SDN_WASI_CLANGXX = prior; + resolveWasiThreadsToolchain({ force: true }); + } + assert.equal(compilation.report.ok, true); + return compilation.wasmBytes; +} +test.after(async () => { if (compilation) await cleanupCompilation(compilation); if (compilerDir) await fs.rm(compilerDir, {recursive:true, force:true}); }); +const CANARY = 0x12345678; +function check(result) { + assert.ok(result.tls >= result.stackTop, JSON.stringify(result)); + assert.equal(result.constructorResult, 0, "constructors can spawn after pool warm-up"); + assert.equal(result.before, CANARY); + assert.equal(result.after, CANARY, "stack writes preserve owner TLS"); + assert.equal(result.spawnResult, 0); + assert.equal(result.afterChild, CANARY, "child TLS is independent"); +} + +test("Node owner TLS survives an explicit pool and constructor thread", { skip: !available }, async () => { + const h = await createBrowserModuleHarness({ wasmSource: await guest(), surface: "direct", wasiThreadPoolSize: 2 }); + try { + const x = h.instance.exports; + const result = { tls: x.tls_address(), stackTop: x.stack_top(), constructorResult: x.constructor_result(), before: x.tls_get() }; + x.stack_write(); result.after = x.tls_get(); result.spawnResult = x.spawn(); result.afterChild = x.tls_get(); + check(result); + } finally { await h.destroy(); } +}); + +test("Node secondary-first negative control overlaps TLS and stack", { skip: !available }, async () => { + const module = await WebAssembly.compile(await guest()); + for (const ownerFirst of [true, false]) { + const memory = new WebAssembly.Memory({ initial: 1024, maximum: 32768, shared: true }); + const imports = {}; + for (const entry of WebAssembly.Module.imports(module)) { + imports[entry.module] ??= {}; + imports[entry.module][entry.name] = entry.kind === "memory" ? memory : () => -1; + } + let owner; + if (ownerFirst) { owner = new WebAssembly.Instance(module, imports); new WebAssembly.Instance(module, imports); } + else { new WebAssembly.Instance(module, imports); owner = new WebAssembly.Instance(module, imports); } + // The exported probes need no constructors for this raw instantiation control. + const x = owner.exports; x.tls_set(); const before = x.tls_get(); x.stack_write(); + assert.equal(x.tls_get() === before, ownerFirst); + } +}); + +async function server(wasmBytes) { + const { build } = await import("esbuild"); + const bundle = async (entry) => (await build({ entryPoints: [fileURLToPath(new URL(entry, import.meta.url))], + bundle: true, write: false, format: "esm", platform: "browser", target: "es2022", external: ["node:*", "hd-wallet-wasm"], logLevel: "silent", + })).outputFiles[0].text; + const routes = { + "/": ["text/html", ''], + "/owner.js": ["text/javascript", await bundle("./support/tlsOwnerBrowser.mjs")], + "/thread.js": ["text/javascript", await bundle("../src/host/wasiThreadBrowserWorker.mjs")], + "/module.wasm": ["application/wasm", Buffer.from(wasmBytes)], + }; + const s = http.createServer((req, res) => { + const route = routes[req.url]; + res.writeHead(route ? 200 : 404, { "Content-Type": route?.[0] ?? "text/plain", "Cross-Origin-Opener-Policy": "same-origin", "Cross-Origin-Embedder-Policy": "require-corp", "Cross-Origin-Resource-Policy": "same-origin" }); + res.end(route?.[1]); + }); + await new Promise(resolve => s.listen(0, "127.0.0.1", resolve)); + return { url: `http://127.0.0.1:${s.address().port}/`, close: () => new Promise(resolve => { s.closeAllConnections(); s.close(resolve); }) }; +} +for (const name of browsers) { + test(`${name}: owner TLS is outside its stack with a prewarmed pool`, { + skip: !available || (!browserEnabled && "set SPACE_DATA_MODULE_SDK_ENABLE_BROWSER_THREADS=1"), timeout: 120000, + }, async () => { + const s = await server(await guest()); let browser; + try { + browser = await (await import("playwright-core"))[name].launch({ headless: true }); + const page = await browser.newPage(); await page.goto(s.url); + await page.waitForFunction(() => window.result !== undefined); + const reply = await page.evaluate(() => window.result); + assert.equal(reply.error, undefined); assert.equal(reply.result.poolSize, 2); check(reply.result); + } finally { await browser?.close(); await s.close(); } + }); +} diff --git a/test/support/tlsOwnerBrowser.mjs b/test/support/tlsOwnerBrowser.mjs new file mode 100644 index 0000000..8bcbf0c --- /dev/null +++ b/test/support/tlsOwnerBrowser.mjs @@ -0,0 +1,28 @@ +import { createBrowserModuleHarness } from "../../src/host/browserModuleHarness.js"; +self.onmessage = async () => { + let harness; + try { + harness = await createBrowserModuleHarness({ + wasmSource: new Uint8Array(await (await fetch("/module.wasm")).arrayBuffer()), + surface: "direct", + enableBrowserWasiThreads: true, + wasiThreadPoolSize: 2, + wasiThreadWorkerUrl: new URL("/thread.js", self.location.href).href, + }); + const x = harness.instance.exports; + const result = { + tls: x.tls_address(), stackTop: x.stack_top(), + constructorResult: x.constructor_result(), + before: x.tls_get(), poolSize: harness.threadHost.spawnReport().poolSize, + }; + x.stack_write(); + result.after = x.tls_get(); + result.spawnResult = x.spawn(); + result.afterChild = x.tls_get(); + self.postMessage({ result }); + } catch (error) { + self.postMessage({ error: String(error) }); + } finally { + await harness?.destroy(); + } +}; diff --git a/test/support/tlsOwnerGuest.cpp b/test/support/tlsOwnerGuest.cpp new file mode 100644 index 0000000..225534e --- /dev/null +++ b/test/support/tlsOwnerGuest.cpp @@ -0,0 +1,37 @@ +#include +#include +#define EXPORT(n) extern "C" __attribute__((export_name(n))) + +thread_local volatile uint32_t tls_words[8192]; +static int constructor_spawn_result = -1; +EXPORT("tls_address") uintptr_t tls_address() { + return reinterpret_cast(&tls_words[0]); +} +EXPORT("tls_get") uint32_t tls_get() { return tls_words[7000]; } +EXPORT("tls_set") void tls_set() { tls_words[7000] = 0x12345678; } +EXPORT("stack_top") uintptr_t stack_top() { + pthread_attr_t attr; + void* base; + size_t bytes; + pthread_getattr_np(pthread_self(), &attr); + pthread_attr_getstack(&attr, &base, &bytes); + pthread_attr_destroy(&attr); + return reinterpret_cast(base) + bytes; +} +EXPORT("stack_write") __attribute__((noinline)) uint32_t stack_write() { + volatile uint32_t words[10000]; + for (unsigned i = 0; i < 10000; ++i) words[i] = 0xabcdef00u + i; + return words[9999]; +} +static void* child(void*) { tls_words[7000] = 1; return nullptr; } +EXPORT("spawn") int spawn() { + pthread_t thread; + int rc = pthread_create(&thread, nullptr, child, nullptr); + return rc ? rc : pthread_join(thread, nullptr); +} +struct Initializer { + Initializer() { tls_set(); constructor_spawn_result = spawn(); } +} initializer; +EXPORT("constructor_result") int constructor_result() { return constructor_spawn_result; } +EXPORT("probe") int probe() { return 0; } +int main() { return 0; }