Skip to content

Commit 1655f39

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 4a1de1c commit 1655f39

7 files changed

Lines changed: 91 additions & 8 deletions

File tree

.babelrc

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
],
1616
plugins: [
1717
"transform-class-properties",
18-
[ "transform-object-rest-spread", { "useBuiltIns": true }]
18+
[ "transform-object-rest-spread", { "useBuiltIns": true }],
19+
"syntax-dynamic-import"
1920
],
2021
env: {
2122
// Needed for tests, as node doesn't support ES2015 modules yet.
@@ -28,7 +29,8 @@
2829
],
2930
plugins: [
3031
"transform-class-properties",
31-
[ "transform-object-rest-spread", { "useBuiltIns": true }]
32+
[ "transform-object-rest-spread", { "useBuiltIns": true }],
33+
"dynamic-import-node"
3234
],
3335
}
3436
}

package.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,16 @@
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",
4244
"babel-runtime": "^6.26.0",
4345
"bisection": "0.0.3",
4446
"clamp": "^1.0.1",
4547
"classnames": "^2.2.5",
4648
"common-tags": "^1.7.2",
4749
"copy-to-clipboard": "^3.0.8",
4850
"escape-string-regexp": "^1.0.5",
51+
"gecko-profiler-demangle": "^0.1.0",
4952
"jszip": "^3.1.5",
5053
"memoize-immutable": "^3.0.0",
5154
"mixedtuplemap": "^1.0.0",
@@ -134,7 +137,8 @@
134137
],
135138
"moduleNameMapper": {
136139
"\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$": "<rootDir>/src/test/fixtures/mocks/file-mock.js",
137-
"\\.(css|less)$": "<rootDir>/src/test/fixtures/mocks/style-mock.js"
140+
"\\.(css|less)$": "<rootDir>/src/test/fixtures/mocks/style-mock.js",
141+
"^gecko-profiler-demangle$": "<rootDir>/src/test/gecko-profiler-demangle-mock.js"
138142
},
139143
"restoreMocks": true,
140144
"resetMocks": true,

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

@@ -113,7 +115,8 @@ export class SymbolStore {
113115
// This format is documented at the SymbolTableAsTuple flow type definition.
114116
_readSymbolsFromSymbolTable(
115117
addresses: Set<number>,
116-
symbolTable: SymbolTableAsTuple
118+
symbolTable: SymbolTableAsTuple,
119+
demangleCallback: string => string
117120
): Map<number, AddressResult> {
118121
const [symbolTableAddrs, symbolTableIndex, symbolTableBuffer] = symbolTable;
119122
const addressArray = Uint32Array.from(addresses);
@@ -150,7 +153,9 @@ export class SymbolStore {
150153
const startOffset = symbolTableIndex[symbolIndex];
151154
const endOffset = symbolTableIndex[symbolIndex + 1];
152155
const subarray = symbolTableBuffer.subarray(startOffset, endOffset);
153-
currentSymbol = decoder.decode(subarray);
156+
// C++ or rust symbols in the symbol table may have mangled names.
157+
// Demangle them here.
158+
currentSymbol = demangleCallback(decoder.decode(subarray));
154159
currentSymbolIndex = symbolIndex;
155160
}
156161
results.set(address, {
@@ -268,10 +273,16 @@ export class SymbolStore {
268273
// symbolication for the libraries for which we found symbol tables in the
269274
// database. This is delayed until after the request has been kicked off
270275
// because it can take some time.
276+
// We also need a demangling function for this, which is in an async module.
277+
const demangleCallback = (await demangleModulePromise).demangle_any;
271278
for (const { request, symbolTable } of requestsForCachedLibs) {
272279
successCb(
273280
request,
274-
this._readSymbolsFromSymbolTable(request.addresses, symbolTable)
281+
this._readSymbolsFromSymbolTable(
282+
request.addresses,
283+
symbolTable,
284+
demangleCallback
285+
)
275286
);
276287
}
277288

@@ -312,7 +323,11 @@ export class SymbolStore {
312323
// Did not throw, option 3 was successful!
313324
successCb(
314325
request,
315-
this._readSymbolsFromSymbolTable(addresses, symbolTable)
326+
this._readSymbolsFromSymbolTable(
327+
addresses,
328+
symbolTable,
329+
demangleCallback
330+
)
316331
);
317332

318333
// Store the symbol table in the database.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
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+
9+
// @flow
10+
11+
// There's only one exported function.
12+
// Do the simplest thing possible: no demangling.
13+
export function demangle_any(s: string): string {
14+
return s;
15+
}
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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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: {

yarn.lock

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -892,6 +892,13 @@ babel-plugin-check-es2015-constants@^6.22.0:
892892
dependencies:
893893
babel-runtime "^6.22.0"
894894

895+
babel-plugin-dynamic-import-node@^2.0.0:
896+
version "2.0.0"
897+
resolved "https://registry.yarnpkg.com/babel-plugin-dynamic-import-node/-/babel-plugin-dynamic-import-node-2.0.0.tgz#d6fc3f6c5e3bdc34e49c15faca7ce069755c0a57"
898+
dependencies:
899+
babel-plugin-syntax-dynamic-import "^6.18.0"
900+
object.assign "^4.1.0"
901+
895902
babel-plugin-istanbul@^4.1.6:
896903
version "4.1.6"
897904
resolved "https://registry.yarnpkg.com/babel-plugin-istanbul/-/babel-plugin-istanbul-4.1.6.tgz#36c59b2192efce81c5b378321b74175add1c9a45"
@@ -913,6 +920,10 @@ babel-plugin-syntax-class-properties@^6.8.0:
913920
version "6.13.0"
914921
resolved "https://registry.yarnpkg.com/babel-plugin-syntax-class-properties/-/babel-plugin-syntax-class-properties-6.13.0.tgz#d7eb23b79a317f8543962c505b827c7d6cac27de"
915922

923+
babel-plugin-syntax-dynamic-import@^6.18.0:
924+
version "6.18.0"
925+
resolved "https://registry.yarnpkg.com/babel-plugin-syntax-dynamic-import/-/babel-plugin-syntax-dynamic-import-6.18.0.tgz#8d6a26229c83745a9982a441051572caa179b1da"
926+
916927
babel-plugin-syntax-exponentiation-operator@^6.8.0:
917928
version "6.13.0"
918929
resolved "https://registry.yarnpkg.com/babel-plugin-syntax-exponentiation-operator/-/babel-plugin-syntax-exponentiation-operator-6.13.0.tgz#9ee7e8337290da95288201a6a57f4170317830de"
@@ -3912,6 +3923,10 @@ gauge@~2.7.3:
39123923
strip-ansi "^3.0.1"
39133924
wide-align "^1.1.0"
39143925

3926+
gecko-profiler-demangle@^0.1.0:
3927+
version "0.1.0"
3928+
resolved "https://registry.yarnpkg.com/gecko-profiler-demangle/-/gecko-profiler-demangle-0.1.0.tgz#7bc08d43e3572e64a6c09c4ed6680e4bc4ce356e"
3929+
39153930
get-caller-file@^1.0.1:
39163931
version "1.0.3"
39173932
resolved "https://registry.yarnpkg.com/get-caller-file/-/get-caller-file-1.0.3.tgz#f978fa4c90d1dfe7ff2d6beda2a515e713bdcf4a"

0 commit comments

Comments
 (0)