Files
faunosteo/scripts/browser/r4-import-page/proofs-worker.js
T
bchanot c84186b032 test(tests): R4.2e browser measurements of the Rust import (module worker page, same perimeter as the TS baseline)
r4-import.mjs runs the 226 baseline files and the hostile classes through a disposable
module worker calling SheetHandle.import_pdf in Chromium 156, Firefox 157 and WebKit 27.2
under the RSS watchdog (not a guaranteed limit); compare-imports.mjs prints TS vs Rust;
serve.mjs gains opt-in mounts (/r4/, /pkg/). Results 2026-10-18-r4-import.json: 666/678
agreements, the 12 others are the 4 declared files; W4 met (max 68 MiB linear); the
1 GiB ceiling traps in every engine.
2026-10-18 16:58:42 +02:00

73 lines
2.3 KiB
JavaScript

// Memory-protection proofs of the R4 measurement page (R4.2e), on the proofs
// build (packages/core-wasm/pkg-proofs, the R0 and R4.2e test exports): the
// 1 GiB linear ceiling (`allocate_for_test`: 256 MiB returns, 1100 MiB must
// trap) and the cost the OD-3 value cap prevents (`import_pdf_unbounded_
// for_test`, the import with the cap off). The worker fetches and compiles
// the module itself, so the worker's CSP is exercised too.
import init, {
allocate_for_test,
import_pdf_unbounded_for_test,
} from '/pkg-proofs/faunosteo_wasm.js'
const WASM_URL = '/pkg-proofs/faunosteo_wasm_bg.wasm'
const cspViolations = []
let memory = null
self.addEventListener('securitypolicyviolation', (event) => {
cspViolations.push(`${event.effectiveDirective} ${event.blockedURI}`)
})
const linearBytes = () => memory?.buffer.byteLength ?? null
const describe = (error) =>
`${error?.name ?? 'Error'}: ${error?.message ?? error}`
.split('\n')[0]
.slice(0, 200)
/** The call's value, or the throwable's name and message. */
function attempt(work) {
const startedAt = performance.now()
try {
const value = work()
return { returned: value, ms: performance.now() - startedAt }
} catch (error) {
return { threw: describe(error), ms: performance.now() - startedAt }
}
}
function ceiling() {
const first = attempt(() => allocate_for_test(256))
const linearAfterFirst = linearBytes()
const second = attempt(() => allocate_for_test(1100))
return { first, linearAfterFirst, second, linearBytes: linearBytes() }
}
async function capOff({ sheet, file }) {
const bytes = new Uint8Array(await file.arrayBuffer())
const run = attempt(() => import_pdf_unbounded_for_test(sheet, bytes))
return { run, linearBytes: linearBytes() }
}
async function prove(message) {
const exports = await init({ module_or_path: WASM_URL })
memory = exports.memory
const linearAtStart = linearBytes()
const value = message.name === 'ceiling' ? ceiling() : await capOff(message)
return { linearAtStart, ...value }
}
self.onmessage = (event) => {
prove(event.data).then(
(value) =>
self.postMessage({ v: 1, type: 'proof', value, csp: cspViolations }),
(error) =>
self.postMessage({
v: 1,
type: 'failed',
phase: 'start',
kind: 'other',
error: describe(error),
csp: cspViolations,
}),
)
}