Skip to content

Commit b642d35

Browse files
bertschneideroetr
authored andcommitted
core: Use global Jazzer.js object for callbacks
1 parent fd09239 commit b642d35

File tree

3 files changed

+60
-10
lines changed

3 files changed

+60
-10
lines changed

packages/core/callback.ts

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
* limitations under the License.
1515
*/
1616

17+
import { getOrSetJazzerJsGlobal } from "./api";
18+
1719
export type Thunk = () => void;
1820

1921
/**
@@ -33,24 +35,23 @@ export class Callbacks {
3335
}
3436

3537
runAfterEachCallbacks() {
36-
for (const c of this._afterEachCallbacks) {
37-
c();
38-
}
38+
this._afterEachCallbacks.forEach((c) => c());
3939
}
4040

4141
runBeforeEachCallbacks() {
42-
for (const c of this._beforeEachCallbacks) {
43-
c();
44-
}
42+
this._beforeEachCallbacks.forEach((c) => c());
4543
}
4644
}
4745

48-
export const callbacks = new Callbacks();
46+
const defaultCallbacks = new Callbacks();
47+
export function getCallbacks(): Callbacks {
48+
return getOrSetJazzerJsGlobal("callbacks", defaultCallbacks);
49+
}
4950

5051
export function registerAfterEachCallback(callback: Thunk) {
51-
callbacks.registerAfterEachCallback(callback);
52+
getCallbacks().registerAfterEachCallback(callback);
5253
}
5354

5455
export function registerBeforeEachCallback(callback: Thunk) {
55-
callbacks.registerBeforeEachCallback(callback);
56+
getCallbacks().registerBeforeEachCallback(callback);
5657
}

packages/core/callbacks.test.ts

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* Copyright 2023 Code Intelligence GmbH
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
import {
18+
getCallbacks,
19+
registerAfterEachCallback,
20+
registerBeforeEachCallback,
21+
} from "./callback";
22+
23+
describe("callbacks", () => {
24+
beforeEach(() => {
25+
globalThis.JazzerJS = new Map();
26+
});
27+
28+
it("executes registered beforeEach callbacks", () => {
29+
const callback = jest.fn();
30+
registerBeforeEachCallback(callback);
31+
registerBeforeEachCallback(callback);
32+
registerBeforeEachCallback(callback);
33+
const callbacks = getCallbacks();
34+
callbacks.runBeforeEachCallbacks();
35+
expect(callback).toBeCalledTimes(3);
36+
});
37+
38+
it("executes registered afterEach callbacks", () => {
39+
const callback = jest.fn();
40+
registerAfterEachCallback(callback);
41+
registerAfterEachCallback(callback);
42+
registerAfterEachCallback(callback);
43+
const callbacks = getCallbacks();
44+
callbacks.runAfterEachCallbacks();
45+
expect(callback).toBeCalledTimes(3);
46+
});
47+
});

packages/core/core.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ import {
3535
MemorySyncIdStrategy,
3636
registerInstrumentor,
3737
} from "@jazzer.js/instrumentor";
38-
import { callbacks } from "./callback";
38+
import { getCallbacks } from "./callback";
3939
import { ensureFilepath, importModule } from "./utils";
4040
import { buildFuzzerOption, Options } from "./options";
4141
import { jazzerJs } from "./globals";
@@ -334,6 +334,7 @@ export function wrapFuzzFunctionForBugDetection(
334334
return (data: Buffer): unknown | Promise<unknown> => {
335335
let fuzzTargetError: unknown;
336336
let result: unknown | Promise<unknown> = undefined;
337+
const callbacks = getCallbacks();
337338
try {
338339
callbacks.runBeforeEachCallbacks();
339340
result = (originalFuzzFn as fuzzer.FuzzTargetAsyncOrValue)(data);
@@ -365,6 +366,7 @@ export function wrapFuzzFunctionForBugDetection(
365366
data: Buffer,
366367
done: (err?: Error) => void,
367368
): unknown | Promise<unknown> => {
369+
const callbacks = getCallbacks();
368370
try {
369371
callbacks.runBeforeEachCallbacks();
370372
// Return result of fuzz target to enable sanity checks in C++ part.

0 commit comments

Comments
 (0)