Skip to content

Commit 2453b35

Browse files
committed
Demangle symbols when reading them out of the symbol table.
This also adds our first wasm module and updates the configuration so that that's possible. I've used wasm-pack to publish a gecko-profiler-demangle module to npm instead of copying the generated code into this repository. I'm not sure if that was a good or a bad idea; I mostly did it so that our Flow and ESLint rules wouldn't be applied to the wasm-bindgen generated code. I've also needed to autogenerate a flow libdef for this module. wasm-bindgen doesn't create those automatically yet: wasm-bindgen/wasm-bindgen#180 I could have created one manually and included it in the published package, but I don't know if wasm-pack would have included my additional files in the published package (via `wasm-pack publish`).
1 parent 5498749 commit 2453b35

9 files changed

Lines changed: 254 additions & 21 deletions

File tree

.babelrc

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
// obeys the spec more, but we get a bundle that's 8kB larger. That was the
1010
// default in babel v6.
1111
["@babel/plugin-proposal-class-properties", { loose: true }],
12-
["@babel/plugin-proposal-object-rest-spread", { useBuiltIns: true }]
12+
["@babel/plugin-proposal-object-rest-spread", { useBuiltIns: true }],
13+
"syntax-dynamic-import"
1314
],
1415
env: {
1516
test: {
@@ -20,8 +21,9 @@
2021
],
2122
plugins: [
2223
["@babel/plugin-proposal-class-properties", { loose: true }],
23-
["@babel/plugin-proposal-object-rest-spread", { useBuiltIns: true }]
24-
]
24+
["@babel/plugin-proposal-object-rest-spread", { useBuiltIns: true }],
25+
"dynamic-import-node"
26+
],
2527
}
2628
}
2729
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// This module replaces the wasm-pack generated module 'gecko-profiler-demangle'
2+
// in our tests.
3+
// The reason for this replacement is the fact that wasm-pack (or rather,
4+
// wasm-bindgen), when targeting the browser + webpack, generates an ES6 module
5+
// that node cannot deal with. Most importantly, it uses the syntax
6+
// "import * as wasm from './gecko_profiler_demangle_bg';" in order to load
7+
// the wasm module, which is currently only supported by webpack.
8+
// The long-term path to make this work correctly is to wait for node to
9+
// support ES6 modules (and WASM as ES6 modules) natively [1]. It's possible
10+
// that in the medium term, wasm-bindgen will get support for outputting JS
11+
// files which work in both webpack and in node natively [2].
12+
// [1] https://medium.com/@giltayar/native-es-modules-in-nodejs-status-and-future-directions-part-i-ee5ea3001f71
13+
// [2] https://github.com/rustwasm/wasm-bindgen/issues/233
14+
15+
// @flow
16+
17+
// There's only one exported function.
18+
// Do the simplest thing possible: no demangling.
19+
export function demangle_any(s: string): string {
20+
return s;
21+
}

package.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,16 +39,20 @@
3939
"dependencies": {
4040
"array-move": "^1.0.0",
4141
"array-range": "^1.0.1",
42+
"babel-plugin-dynamic-import-node": "^2.0.0",
43+
"babel-plugin-syntax-dynamic-import": "^6.18.0",
44+
"babel-runtime": "^6.26.0",
4245
"bisection": "0.0.3",
4346
"clamp": "^1.0.1",
4447
"classnames": "^2.2.5",
4548
"common-tags": "^1.7.2",
4649
"copy-to-clipboard": "^3.0.8",
4750
"escape-string-regexp": "^1.0.5",
51+
"gecko-profiler-demangle": "^0.1.0",
4852
"jszip": "^3.1.5",
4953
"memoize-immutable": "^3.0.0",
5054
"mixedtuplemap": "^1.0.0",
51-
"offline-plugin": "^5.0.5",
55+
"@mstange/offline-plugin": "^5.0.6",
5256
"photon-colors": "2.0.1",
5357
"prop-types": "^15.6.0",
5458
"query-string": "^5.1.0",

src/index.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,10 @@ if (process.env.NODE_ENV === 'development') {
2727
}
2828

2929
if (process.env.NODE_ENV === 'production') {
30-
const runtime = require('offline-plugin/runtime');
30+
// We use '@mstange/offline-plugin/runtime' here instead of
31+
// 'offline-plugin/runtime' because the fork contains the fix from
32+
// https://github.com/NekR/offline-plugin/pull/410
33+
const runtime = require('@mstange/offline-plugin/runtime');
3134
runtime.install({
3235
onUpdateReady: () => {
3336
runtime.applyUpdate();

src/profile-logic/symbol-store.js

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import SymbolStoreDB from './symbol-store-db';
77
import { SymbolsNotFoundError } from './errors';
88
import bisection from 'bisection';
99

10+
const demangleModulePromise = import('gecko-profiler-demangle');
11+
1012
import type { RequestedLib } from '../types/actions';
1113
import type { SymbolTableAsTuple } from './symbol-store-db';
1214

@@ -117,7 +119,8 @@ export class SymbolStore {
117119
// This format is documented at the SymbolTableAsTuple flow type definition.
118120
_readSymbolsFromSymbolTable(
119121
addresses: Set<number>,
120-
symbolTable: SymbolTableAsTuple
122+
symbolTable: SymbolTableAsTuple,
123+
demangleCallback: string => string
121124
): Map<number, AddressResult> {
122125
const [symbolTableAddrs, symbolTableIndex, symbolTableBuffer] = symbolTable;
123126
const addressArray = Uint32Array.from(addresses);
@@ -154,7 +157,9 @@ export class SymbolStore {
154157
const startOffset = symbolTableIndex[symbolIndex];
155158
const endOffset = symbolTableIndex[symbolIndex + 1];
156159
const subarray = symbolTableBuffer.subarray(startOffset, endOffset);
157-
currentSymbol = decoder.decode(subarray);
160+
// C++ or rust symbols in the symbol table may have mangled names.
161+
// Demangle them here.
162+
currentSymbol = demangleCallback(decoder.decode(subarray));
158163
currentSymbolIndex = symbolIndex;
159164
}
160165
results.set(address, {
@@ -272,10 +277,16 @@ export class SymbolStore {
272277
// symbolication for the libraries for which we found symbol tables in the
273278
// database. This is delayed until after the request has been kicked off
274279
// because it can take some time.
280+
// We also need a demangling function for this, which is in an async module.
281+
const demangleCallback = (await demangleModulePromise).demangle_any;
275282
for (const { request, symbolTable } of requestsForCachedLibs) {
276283
successCb(
277284
request,
278-
this._readSymbolsFromSymbolTable(request.addresses, symbolTable)
285+
this._readSymbolsFromSymbolTable(
286+
request.addresses,
287+
symbolTable,
288+
demangleCallback
289+
)
279290
);
280291
}
281292

@@ -316,7 +327,11 @@ export class SymbolStore {
316327
// Did not throw, option 3 was successful!
317328
successCb(
318329
request,
319-
this._readSymbolsFromSymbolTable(addresses, symbolTable)
330+
this._readSymbolsFromSymbolTable(
331+
addresses,
332+
symbolTable,
333+
demangleCallback
334+
)
320335
);
321336

322337
// Store the symbol table in the database.
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
// flow-typed signature: f84c93c5335ad9c36684cc95ca54b099
2+
// flow-typed version: <<STUB>>/@mstange/offline-plugin_v^5.0.6/flow_v0.70.0
3+
4+
/**
5+
* This is an autogenerated libdef stub for:
6+
*
7+
* '@mstange/offline-plugin'
8+
*
9+
* Fill this stub out by replacing all the `any` types.
10+
*
11+
* Once filled out, we encourage you to share your work with the
12+
* community by sending a pull request to:
13+
* https://github.com/flowtype/flow-typed
14+
*/
15+
16+
declare module '@mstange/offline-plugin' {
17+
declare module.exports: any;
18+
}
19+
20+
/**
21+
* We include stubs for each file inside this npm package in case you need to
22+
* require those files directly. Feel free to delete any files that aren't
23+
* needed.
24+
*/
25+
declare module '@mstange/offline-plugin/lib/app-cache' {
26+
declare module.exports: any;
27+
}
28+
29+
declare module '@mstange/offline-plugin/lib/default-options' {
30+
declare module.exports: any;
31+
}
32+
33+
declare module '@mstange/offline-plugin/lib/index' {
34+
declare module.exports: any;
35+
}
36+
37+
declare module '@mstange/offline-plugin/lib/loaders/fonts-css' {
38+
declare module.exports: any;
39+
}
40+
41+
declare module '@mstange/offline-plugin/lib/loaders/index' {
42+
declare module.exports: any;
43+
}
44+
45+
declare module '@mstange/offline-plugin/lib/misc/async-waituntil' {
46+
declare module.exports: any;
47+
}
48+
49+
declare module '@mstange/offline-plugin/lib/misc/get-uglify-plugin' {
50+
declare module.exports: any;
51+
}
52+
53+
declare module '@mstange/offline-plugin/lib/misc/runtime-loader' {
54+
declare module.exports: any;
55+
}
56+
57+
declare module '@mstange/offline-plugin/lib/misc/sw-loader' {
58+
declare module.exports: any;
59+
}
60+
61+
declare module '@mstange/offline-plugin/lib/misc/sw-polyfill' {
62+
declare module.exports: any;
63+
}
64+
65+
declare module '@mstange/offline-plugin/lib/misc/sw-template' {
66+
declare module.exports: any;
67+
}
68+
69+
declare module '@mstange/offline-plugin/lib/misc/utils' {
70+
declare module.exports: any;
71+
}
72+
73+
declare module '@mstange/offline-plugin/lib/service-worker' {
74+
declare module.exports: any;
75+
}
76+
77+
declare module '@mstange/offline-plugin/runtime' {
78+
declare module.exports: any;
79+
}
80+
81+
declare module '@mstange/offline-plugin/tpls/empty-entry' {
82+
declare module.exports: any;
83+
}
84+
85+
declare module '@mstange/offline-plugin/tpls/runtime-template' {
86+
declare module.exports: any;
87+
}
88+
89+
// Filename aliases
90+
declare module '@mstange/offline-plugin/lib/app-cache.js' {
91+
declare module.exports: $Exports<'@mstange/offline-plugin/lib/app-cache'>;
92+
}
93+
declare module '@mstange/offline-plugin/lib/default-options.js' {
94+
declare module.exports: $Exports<'@mstange/offline-plugin/lib/default-options'>;
95+
}
96+
declare module '@mstange/offline-plugin/lib/index.js' {
97+
declare module.exports: $Exports<'@mstange/offline-plugin/lib/index'>;
98+
}
99+
declare module '@mstange/offline-plugin/lib/loaders/fonts-css.js' {
100+
declare module.exports: $Exports<'@mstange/offline-plugin/lib/loaders/fonts-css'>;
101+
}
102+
declare module '@mstange/offline-plugin/lib/loaders/index.js' {
103+
declare module.exports: $Exports<'@mstange/offline-plugin/lib/loaders/index'>;
104+
}
105+
declare module '@mstange/offline-plugin/lib/misc/async-waituntil.js' {
106+
declare module.exports: $Exports<'@mstange/offline-plugin/lib/misc/async-waituntil'>;
107+
}
108+
declare module '@mstange/offline-plugin/lib/misc/get-uglify-plugin.js' {
109+
declare module.exports: $Exports<'@mstange/offline-plugin/lib/misc/get-uglify-plugin'>;
110+
}
111+
declare module '@mstange/offline-plugin/lib/misc/runtime-loader.js' {
112+
declare module.exports: $Exports<'@mstange/offline-plugin/lib/misc/runtime-loader'>;
113+
}
114+
declare module '@mstange/offline-plugin/lib/misc/sw-loader.js' {
115+
declare module.exports: $Exports<'@mstange/offline-plugin/lib/misc/sw-loader'>;
116+
}
117+
declare module '@mstange/offline-plugin/lib/misc/sw-polyfill.js' {
118+
declare module.exports: $Exports<'@mstange/offline-plugin/lib/misc/sw-polyfill'>;
119+
}
120+
declare module '@mstange/offline-plugin/lib/misc/sw-template.js' {
121+
declare module.exports: $Exports<'@mstange/offline-plugin/lib/misc/sw-template'>;
122+
}
123+
declare module '@mstange/offline-plugin/lib/misc/utils.js' {
124+
declare module.exports: $Exports<'@mstange/offline-plugin/lib/misc/utils'>;
125+
}
126+
declare module '@mstange/offline-plugin/lib/service-worker.js' {
127+
declare module.exports: $Exports<'@mstange/offline-plugin/lib/service-worker'>;
128+
}
129+
declare module '@mstange/offline-plugin/runtime.js' {
130+
declare module.exports: $Exports<'@mstange/offline-plugin/runtime'>;
131+
}
132+
declare module '@mstange/offline-plugin/tpls/empty-entry.js' {
133+
declare module.exports: $Exports<'@mstange/offline-plugin/tpls/empty-entry'>;
134+
}
135+
declare module '@mstange/offline-plugin/tpls/runtime-template.js' {
136+
declare module.exports: $Exports<'@mstange/offline-plugin/tpls/runtime-template'>;
137+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// flow-typed signature: e6f43a673a1ad489aea31817e5e11e45
2+
// flow-typed version: <<STUB>>/gecko-profiler-demangle_v^0.1.0/flow_v0.70.0
3+
4+
/**
5+
* This is an autogenerated libdef stub for:
6+
*
7+
* 'gecko-profiler-demangle'
8+
*
9+
* Fill this stub out by replacing all the `any` types.
10+
*
11+
* Once filled out, we encourage you to share your work with the
12+
* community by sending a pull request to:
13+
* https://github.com/flowtype/flow-typed
14+
*/
15+
16+
declare module 'gecko-profiler-demangle' {
17+
declare module.exports: any;
18+
}
19+
20+
/**
21+
* We include stubs for each file inside this npm package in case you need to
22+
* require those files directly. Feel free to delete any files that aren't
23+
* needed.
24+
*/
25+
declare module 'gecko-profiler-demangle/gecko_profiler_demangle' {
26+
declare module.exports: any;
27+
}
28+
29+
// Filename aliases
30+
declare module 'gecko-profiler-demangle/gecko_profiler_demangle.js' {
31+
declare module.exports: $Exports<'gecko-profiler-demangle/gecko_profiler_demangle'>;
32+
}

webpack.config.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
const path = require('path');
33
const webpack = require('webpack');
44
const HtmlWebpackPlugin = require('html-webpack-plugin');
5-
const OfflinePlugin = require('offline-plugin');
5+
const OfflinePlugin = require('@mstange/offline-plugin');
66
const CopyWebpackPlugin = require('copy-webpack-plugin');
77
const includes = [path.join(__dirname, 'src'), path.join(__dirname, 'res')];
88

@@ -18,7 +18,7 @@ const config = {
1818
'redux-devtools': path.join(__dirname, '..', '..', 'src'),
1919
react: path.join(__dirname, 'node_modules', 'react'),
2020
},
21-
extensions: ['.js'],
21+
extensions: ['.js', '.wasm'],
2222
},
2323
devtool: 'source-map',
2424
module: {
@@ -79,6 +79,10 @@ const config = {
7979
chunkFilename: '[id].[hash].bundle.js',
8080
publicPath: '/',
8181
},
82+
optimization: {
83+
// Workaround for https://github.com/webpack/webpack/issues/7760
84+
usedExports: false,
85+
},
8286
};
8387

8488
if (process.env.NODE_ENV === 'development') {

0 commit comments

Comments
 (0)