Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import * as Sentry from '@sentry/browser';
import { wasmIntegration } from '@sentry/wasm';

window.Sentry = Sentry;

Sentry.init({
traceLifecycle: 'static',
dsn: 'https://public@dsn.ingest.sentry.io/1337',
integrations: [wasmIntegration()],
beforeSend: event => {
window.events.push(event);
return null;
},
});
window.events = [];
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
function leb128(n) {
const out = [];
do {
let byte = n & 0x7f;
n >>>= 7;
if (n !== 0) {
byte |= 0x80;
}
out.push(byte);
} while (n !== 0);
return out;
}

// Appends a custom section with `padding` payload bytes so the module wire
// bytes cross V8's 16383-byte content-hashing cutoff.
function pad(bytes, padding) {
const payload = new Uint8Array(padding);
for (let i = 0; i < padding; i++) {
payload[i] = (i * 31 + 7) & 0xff;
}
const content = [1, 0x70, ...leb128(payload.length)];
const header = [0x00, ...leb128(2 + payload.length)];
const out = new Uint8Array(bytes.length + header.length + 2 + payload.length);
out.set(bytes, 0);
out.set(header, bytes.length);
out.set([1, 0x70], bytes.length + header.length);
out.set(payload, bytes.length + header.length + 2);
return out;
}

window.getEvent = async padding => {
function crash() {
throw new Error('whoops');
}

const response = await fetch('https://localhost:5887/simple.wasm');
const buffer = await response.arrayBuffer();
const bytes = padding ? pad(new Uint8Array(buffer), padding) : new Uint8Array(buffer);

const { instance } = await WebAssembly.instantiate(bytes, {
env: {
external_func: crash,
},
});

try {
instance.exports.internal_func();
} catch (err) {
Sentry.captureException(err);
return { event: window.events.pop(), byteLength: bytes.byteLength };
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
import type { Page, Route } from '@playwright/test';
import { expect } from '@playwright/test';
import fs from 'fs';
import path from 'path';
import { sentryTest } from '../../../utils/fixtures';
import { shouldSkipWASMTests } from '../../../utils/wasmHelpers';

function serveWasmFixture(page: Page): Promise<void> {
return page.route('**/simple.wasm', (route: Route) => {
const wasmModule = fs.readFileSync(path.resolve(__dirname, '..', 'simple.wasm'));

return route.fulfill({
status: 200,
body: wasmModule,
headers: {
'Content-Type': 'application/wasm',
},
});
});
}

const IMAGE_MATCHER = {
code_file: expect.stringMatching(/^wasm:\/\/wasm\/[0-9a-f]{8}$/),
code_id: '0ba020cdd2444f7eafdd25999a8e9010',
debug_file: null,
debug_id: '0ba020cdd2444f7eafdd25999a8e90100',
type: 'wasm',
};

const FRAME_MATCHER = {
function: 'internal_func',
in_app: true,
instruction_addr: '0x8c',
addr_mode: 'rel:0',
platform: 'native',
};

sentryTest(
'captured exception should include modified frames and debug_meta for non-streaming instantiation',
async ({ getLocalTestUrl, page, browserName }) => {
if (shouldSkipWASMTests(browserName) || browserName === 'firefox') {
sentryTest.skip();
}

const url = await getLocalTestUrl({ testDir: __dirname });
await serveWasmFixture(page);
await page.goto(url);

const { event } = await page.evaluate(async () => {
// @ts-expect-error this function exists
return window.getEvent();
});

expect(event.exception.values[0].stacktrace.frames).toEqual(
expect.arrayContaining([
expect.objectContaining({
...FRAME_MATCHER,
filename: expect.stringMatching(/^wasm:\/\/wasm\/[0-9a-f]{8}$/),
}),
]),
);

expect(event.debug_meta).toMatchObject({ images: [IMAGE_MATCHER] });

// On V8 the small-module (content-hashed) synthetic name must match
// exactly, frames and image alike.
const wasmFrame = event.exception.values[0].stacktrace.frames.find(
(frame: { platform?: string }) => frame.platform === 'native',
);
expect(event.debug_meta.images[0].code_file).toBe(wasmFrame.filename);
},
);

sentryTest(
'captured exception should include modified frames and debug_meta for non-streaming instantiation @firefox',
async ({ getLocalTestUrl, page, browserName }) => {
if (shouldSkipWASMTests(browserName) || browserName !== 'firefox') {
sentryTest.skip();
}

const url = await getLocalTestUrl({ testDir: __dirname });
await serveWasmFixture(page);
await page.goto(url);

const { event } = await page.evaluate(async () => {
// @ts-expect-error this function exists
return window.getEvent();
});

// Firefox derives the script name from the compile call site, so the
// frame matches through the single-buffer-module fallback.
expect(event.exception.values[0].stacktrace.frames).toEqual(
expect.arrayContaining([
expect.objectContaining({
...FRAME_MATCHER,
filename: expect.stringContaining('> WebAssembly.instantiate'),
}),
]),
);

expect(event.debug_meta).toMatchObject({ images: [IMAGE_MATCHER] });
},
);

sentryTest(
'exactly matches the length-derived synthetic name for modules above the content-hash cutoff',
async ({ getLocalTestUrl, page, browserName }) => {
if (shouldSkipWASMTests(browserName) || browserName === 'firefox') {
sentryTest.skip();
}

const url = await getLocalTestUrl({ testDir: __dirname });
await serveWasmFixture(page);
await page.goto(url);

const { event, byteLength } = await page.evaluate(async () => {
// @ts-expect-error this function exists
return window.getEvent(17000);
});

// V8 does not content-hash modules above 16383 bytes; the synthetic name
// derives from the byte length alone on every V8 version.
expect(byteLength).toBeGreaterThan(16383);
const expectedUrl = `wasm://wasm/${(byteLength * 4 + 2).toString(16).padStart(8, '0')}`;

expect(event.exception.values[0].stacktrace.frames).toEqual(
expect.arrayContaining([
expect.objectContaining({
...FRAME_MATCHER,
filename: expectedUrl,
}),
]),
);

expect(event.debug_meta).toMatchObject({
images: [{ ...IMAGE_MATCHER, code_file: expectedUrl }],
});
},
);

sentryTest(
'falls back to the single buffer module for call-site-derived names above the content-hash cutoff @firefox',
async ({ getLocalTestUrl, page, browserName }) => {
if (shouldSkipWASMTests(browserName) || browserName !== 'firefox') {
sentryTest.skip();
}

const url = await getLocalTestUrl({ testDir: __dirname });
await serveWasmFixture(page);
await page.goto(url);

const { event } = await page.evaluate(async () => {
// @ts-expect-error this function exists
return window.getEvent(17000);
});

expect(event.exception.values[0].stacktrace.frames).toEqual(
expect.arrayContaining([
expect.objectContaining({
...FRAME_MATCHER,
filename: expect.stringContaining('> WebAssembly.instantiate'),
}),
]),
);

expect(event.debug_meta).toMatchObject({ images: [IMAGE_MATCHER] });
},
);
Loading
Loading