-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
feat(wasm): initialised sentryWasmImages for webworkers #18812
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
harshit078
wants to merge
10
commits into
getsentry:develop
Choose a base branch
from
harshit078:support-wasm-web-workers
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
32071e7
feat(wasm): initialised sentryWasmImages for webworkers
harshit078 96a750e
Merge branch 'develop' into support-wasm-web-workers
harshit078 02ea888
feat(wasm): added tests for webworker
harshit078 a38298e
Merge branch 'develop' into support-wasm-web-workers
harshit078 4d5657e
Merge branch 'develop' into support-wasm-web-workers
harshit078 dc1a2c1
feat(wasm): address cursor comments and added test suite
harshit078 4796ccb
fix(wasm): address cursor comments
harshit078 0d66a9b
Merge branch 'develop' into support-wasm-web-workers
harshit078 2cff4bd
fix(wasm): resolve bug mentioned by cursor
harshit078 e8206de
Merge branch 'develop' into support-wasm-web-workers
harshit078 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
86 changes: 86 additions & 0 deletions
86
dev-packages/browser-integration-tests/suites/wasm/webWorker/assets/worker.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| // This worker manually just replicates what the actual Sentry.registerWebWorkerWasm() does | ||
|
|
||
| const origInstantiateStreaming = WebAssembly.instantiateStreaming; | ||
| WebAssembly.instantiateStreaming = function instantiateStreaming(response, importObject) { | ||
| return Promise.resolve(response).then(res => { | ||
| return origInstantiateStreaming(res, importObject).then(rv => { | ||
| if (res.url) { | ||
| registerModuleAndForward(rv.module, res.url); | ||
| } | ||
| return rv; | ||
| }); | ||
| }); | ||
| }; | ||
|
|
||
| function registerModuleAndForward(module, url) { | ||
| const buildId = getBuildId(module); | ||
|
|
||
| if (buildId) { | ||
| const image = { | ||
| type: 'wasm', | ||
| code_id: buildId, | ||
| code_file: url, | ||
| debug_file: null, | ||
| debug_id: (buildId + '00000000000000000000000000000000').slice(0, 32) + '0', | ||
| }; | ||
|
|
||
| self.postMessage({ | ||
| _sentryMessage: true, | ||
| _sentryWasmImages: [image], | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| // Extract build ID from WASM module | ||
| function getBuildId(module) { | ||
| const sections = WebAssembly.Module.customSections(module, 'build_id'); | ||
| if (sections.length > 0) { | ||
| const buildId = Array.from(new Uint8Array(sections[0])) | ||
| .map(b => b.toString(16).padStart(2, '0')) | ||
| .join(''); | ||
| return buildId; | ||
| } | ||
| return null; | ||
| } | ||
|
|
||
| // Handle messages from the main thread | ||
| self.addEventListener('message', async event => { | ||
| if (event.data.type === 'load-wasm-and-crash') { | ||
| const wasmUrl = event.data.wasmUrl; | ||
|
|
||
| function crash() { | ||
| throw new Error('WASM error from worker'); | ||
| } | ||
|
|
||
| try { | ||
| const { instance } = await WebAssembly.instantiateStreaming(fetch(wasmUrl), { | ||
Check warningCode scanning / CodeQL Client-side request forgery Medium
The
URL Error loading related location Loading user-provided value Error loading related location Loading |
||
| env: { | ||
| external_func: crash, | ||
| }, | ||
| }); | ||
|
|
||
| instance.exports.internal_func(); | ||
| } catch (err) { | ||
| self.postMessage({ | ||
| _sentryMessage: true, | ||
| _sentryWorkerError: { | ||
| reason: err, | ||
| filename: self.location.href, | ||
| }, | ||
| }); | ||
| } | ||
| } | ||
| }); | ||
|
|
||
| self.addEventListener('unhandledrejection', event => { | ||
| self.postMessage({ | ||
| _sentryMessage: true, | ||
| _sentryWorkerError: { | ||
| reason: event.reason, | ||
| filename: self.location.href, | ||
| }, | ||
| }); | ||
| }); | ||
|
|
||
| // Let the main thread know that worker is ready | ||
| self.postMessage({ _sentryMessage: false, type: 'WORKER_READY' }); | ||
15 changes: 15 additions & 0 deletions
15
dev-packages/browser-integration-tests/suites/wasm/webWorker/init.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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({ | ||
| dsn: 'https://public@dsn.ingest.sentry.io/1337', | ||
| integrations: [wasmIntegration({ applicationKey: 'wasm-worker-app' })], | ||
| }); | ||
|
|
||
| const worker = new Worker('/worker.js'); | ||
|
|
||
| Sentry.addIntegration(Sentry.webWorkerIntegration({ worker })); | ||
|
|
||
| window.wasmWorker = worker; |
8 changes: 8 additions & 0 deletions
8
dev-packages/browser-integration-tests/suites/wasm/webWorker/subject.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| window.events = []; | ||
|
|
||
| window.triggerWasmError = () => { | ||
| window.wasmWorker.postMessage({ | ||
| type: 'load-wasm-and-crash', | ||
| wasmUrl: 'https://localhost:5887/simple.wasm', | ||
| }); | ||
| }; |
9 changes: 9 additions & 0 deletions
9
dev-packages/browser-integration-tests/suites/wasm/webWorker/template.html
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| <!doctype html> | ||
| <html> | ||
| <head> | ||
| <meta charset="utf-8" /> | ||
| </head> | ||
| <body> | ||
| <button id="triggerWasmError">Trigger WASM Error in Worker</button> | ||
| </body> | ||
| </html> |
139 changes: 139 additions & 0 deletions
139
dev-packages/browser-integration-tests/suites/wasm/webWorker/test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,139 @@ | ||
| import { expect } from '@playwright/test'; | ||
| import fs from 'fs'; | ||
| import path from 'path'; | ||
| import { sentryTest } from '../../../utils/fixtures'; | ||
| import { envelopeRequestParser, waitForErrorRequest } from '../../../utils/helpers'; | ||
| import { shouldSkipWASMTests } from '../../../utils/wasmHelpers'; | ||
|
|
||
| declare global { | ||
| interface Window { | ||
| wasmWorker: Worker; | ||
| triggerWasmError: () => void; | ||
| } | ||
| } | ||
|
|
||
| const bundle = process.env.PW_BUNDLE || ''; | ||
| if (bundle.startsWith('bundle')) { | ||
| sentryTest.skip(); | ||
| } | ||
|
|
||
| sentryTest( | ||
| 'WASM debug images from worker should be forwarded to main thread and attached to events', | ||
| async ({ getLocalTestUrl, page, browserName }) => { | ||
| if (shouldSkipWASMTests(browserName)) { | ||
| sentryTest.skip(); | ||
| } | ||
|
|
||
| const url = await getLocalTestUrl({ testDir: __dirname }); | ||
|
|
||
| await page.route('**/simple.wasm', route => { | ||
| const wasmModule = fs.readFileSync(path.resolve(__dirname, '../simple.wasm')); | ||
| return route.fulfill({ | ||
| status: 200, | ||
| body: wasmModule, | ||
| headers: { | ||
| 'Content-Type': 'application/wasm', | ||
| }, | ||
| }); | ||
| }); | ||
|
|
||
| await page.route('**/worker.js', route => { | ||
| return route.fulfill({ | ||
| path: `${__dirname}/assets/worker.js`, | ||
| }); | ||
| }); | ||
|
|
||
| const errorEventPromise = waitForErrorRequest(page, e => { | ||
| return e.exception?.values?.[0]?.value === 'WASM error from worker'; | ||
| }); | ||
|
|
||
| await page.goto(url); | ||
|
|
||
| await page.waitForFunction(() => window.wasmWorker !== undefined); | ||
|
|
||
| await page.evaluate(() => { | ||
| window.triggerWasmError(); | ||
| }); | ||
|
|
||
| const errorEvent = envelopeRequestParser(await errorEventPromise); | ||
|
|
||
| expect(errorEvent.exception?.values?.[0]?.value).toBe('WASM error from worker'); | ||
|
|
||
| expect(errorEvent.debug_meta?.images).toBeDefined(); | ||
| expect(errorEvent.debug_meta?.images).toEqual( | ||
| expect.arrayContaining([ | ||
| expect.objectContaining({ | ||
| type: 'wasm', | ||
| code_file: expect.stringMatching(/simple\.wasm$/), | ||
| code_id: '0ba020cdd2444f7eafdd25999a8e9010', | ||
| debug_id: '0ba020cdd2444f7eafdd25999a8e90100', | ||
| }), | ||
| ]), | ||
| ); | ||
|
|
||
| expect(errorEvent.exception?.values?.[0]?.stacktrace?.frames).toEqual( | ||
| expect.arrayContaining([ | ||
| expect.objectContaining({ | ||
| filename: expect.stringMatching(/simple\.wasm$/), | ||
| platform: 'native', | ||
| instruction_addr: expect.stringMatching(/^0x[a-fA-F0-9]+$/), | ||
| addr_mode: expect.stringMatching(/^rel:\d+$/), | ||
| }), | ||
| ]), | ||
| ); | ||
| }, | ||
| ); | ||
|
|
||
| sentryTest( | ||
| 'WASM frames from worker should be recognized as first-party when applicationKey is configured', | ||
| async ({ getLocalTestUrl, page, browserName }) => { | ||
| if (shouldSkipWASMTests(browserName)) { | ||
| sentryTest.skip(); | ||
| } | ||
|
|
||
| const url = await getLocalTestUrl({ testDir: __dirname }); | ||
|
|
||
| await page.route('**/simple.wasm', route => { | ||
| const wasmModule = fs.readFileSync(path.resolve(__dirname, '../simple.wasm')); | ||
| return route.fulfill({ | ||
| status: 200, | ||
| body: wasmModule, | ||
| headers: { | ||
| 'Content-Type': 'application/wasm', | ||
| }, | ||
| }); | ||
| }); | ||
|
|
||
| await page.route('**/worker.js', route => { | ||
| return route.fulfill({ | ||
| path: `${__dirname}/assets/worker.js`, | ||
| }); | ||
| }); | ||
|
|
||
| const errorEventPromise = waitForErrorRequest(page, e => { | ||
| return e.exception?.values?.[0]?.value === 'WASM error from worker'; | ||
| }); | ||
|
|
||
| await page.goto(url); | ||
|
|
||
| await page.waitForFunction(() => window.wasmWorker !== undefined); | ||
|
|
||
| await page.evaluate(() => { | ||
| window.triggerWasmError(); | ||
| }); | ||
|
|
||
| const errorEvent = envelopeRequestParser(await errorEventPromise); | ||
|
|
||
| expect(errorEvent.exception?.values?.[0]?.stacktrace?.frames).toEqual( | ||
| expect.arrayContaining([ | ||
| expect.objectContaining({ | ||
| filename: expect.stringMatching(/simple\.wasm$/), | ||
| platform: 'native', | ||
| module_metadata: expect.objectContaining({ | ||
| '_sentryBundlerPluginAppKey:wasm-worker-app': true, | ||
| }), | ||
| }), | ||
| ]), | ||
| ); | ||
| }, | ||
| ); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Check warning
Code scanning / CodeQL
Missing origin verification in `postMessage` handler Medium