// 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, }), ) }