Skip to content

Commit 5b2d529

Browse files
authored
Add RTrace shadow memory and instrumentation (#1476)
BREAKING CHANGE: RTrace is now a class and the onrealloc callback has been split into onresize and onmove.
1 parent de022c9 commit 5b2d529

File tree

103 files changed

+1114
-1664
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

103 files changed

+1114
-1664
lines changed

lib/rtrace/README.md

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,34 @@ A tiny utility that records allocations, retains, releases and frees performed b
55
Instructions
66
------------
77

8-
Compile your module that uses the full or half runtime with `-use ASC_RTRACE=1` and include an instance of this module as the import named `rtrace`.
8+
Compile your module that uses the full or half runtime with `-use ASC_RTRACE=1 --explicitStart` and include an instance of this module as the import named `rtrace`.
99

1010
```js
11-
var rtr = rtrace(e => {
12-
// handle error
11+
var rtrace = new Rtrace({
12+
onerror(err, info) {
13+
// handle error
14+
},
15+
oninfo(msg) {
16+
// print message, optional
17+
},
18+
getMemory() {
19+
// obtain the module's memory,
20+
// e.g. with --explicitStart:
21+
return instance.exports.memory;
22+
}
1323
});
1424

15-
WebAssembly.instantiate(..., { rtrace: rtr, ... });
25+
var { module, instance } = await WebAssembly.instantiate(..., {
26+
rtrace,
27+
env: Object.assign({ //
28+
... // only required when instrumenting memory
29+
}, rtrace.env), //
30+
...
31+
});
32+
instance.exports._start();
1633
...
1734

18-
if (rtr.active) {
35+
if (rtrace.active) {
1936
let leakCount = rtr.check();
2037
if (leakCount) {
2138
// handle error

lib/rtrace/index.d.ts

Lines changed: 34 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,41 @@
1-
/** Creates a new `RTrace` instance, tracking allocations, frees and reference counts. */
2-
declare function rtrace(
1+
/** Block information. */
2+
export declare interface BlockInfo {
3+
/** Pointer to the block. */
4+
ptr: number,
5+
/** Block size. */
6+
size: number,
7+
/** Runtime header. */
8+
header: {
9+
/** Memory manager info bits. */
10+
mmInfo: number,
11+
/** Garbage collector info bits. */
12+
gcInfo: number,
13+
/** Runtime id. */
14+
rtId: number,
15+
/** Runtime size. */
16+
rtSize: number
17+
},
18+
toString(): string
19+
}
20+
21+
export declare interface RtraceOptions {
322
/** Function being called when a problem is detected. */
4-
onerror?: (error: Error) => void,
23+
onerror?: (error: Error, info: BlockInfo) => void,
524
/** Function being called with information messages. */
6-
oninfo?: (info: string) => void
7-
): rtrace.RTrace;
8-
9-
declare namespace rtrace {
10-
/** The rtrace instance used as the `rtrace` import to the Wasm module. */
11-
export interface RTrace {
12-
/** Number of allocations so far. */
13-
allocCount: number;
14-
/** Number of reallocations so far. */
15-
reallocCount: number;
16-
/** Number of frees so far. */
17-
freeCount: number;
18-
/** Number of RC increments (retains) so far. */
19-
incrementCount: number;
20-
/** Number of RC decrements (releases) so far. */
21-
decrementCount: number;
22-
23-
/** Called when a new block is allocated. */
24-
onalloc(
25-
/** New block being allocated. */
26-
block: number
27-
): void;
28-
29-
/** Called when a block is reallocated and must be moved. */
30-
onrealloc(
31-
/** Block being moved. */
32-
oldBlock: number,
33-
/** New block used from now on. */
34-
newBlock: number
35-
): void;
36-
37-
/** Called when a block is freed, implicitly or explicitly. */
38-
onfree(
39-
/** Block being freed. */
40-
block: number
41-
): void;
25+
oninfo?: (msg: string) => void,
26+
/** Obtains the module's memory instance. */
27+
getMemory()
28+
}
4229

43-
/** Called when a reference to a block is retained (RC incremented by one). */
44-
onincrement(
45-
/** Block a reference to is being retained. */
46-
block: number
47-
): void;
30+
export declare class Rtrace {
31+
[key: string]: unknown; // can be used as a Wasm import
4832

49-
/** Called when a reference to a block is released (RC decremented by one). */
50-
ondecrement(
51-
/** Block a reference to is being released. */
52-
block: number
53-
): void;
33+
/** Creates a new `RTrace` instance. */
34+
constructor(options: RtraceOptions);
5435

55-
/** Checks if rtrace is active, i.e. at least one event has occurred. */
56-
readonly active: boolean;
36+
/** Checks if rtrace is active, i.e. at least one event has occurred. */
37+
readonly active: boolean;
5738

58-
/** Checks if there are any leaks and emits them via `oninfo`. Returns the number of live blocks. */
59-
check(): number;
60-
}
39+
/** Checks if there are any leaks and emits them via `oninfo`. Returns the number of live blocks. */
40+
check(): number;
6141
}
62-
63-
export = rtrace;

0 commit comments

Comments
 (0)