|
14 | 14 | * limitations under the License.
|
15 | 15 | */
|
16 | 16 |
|
17 |
| -/*eslint @typescript-eslint/no-explicit-any: 0 */ |
| 17 | +export interface TrackedHook { |
| 18 | + target: string; |
| 19 | + pkg: string; |
| 20 | +} |
18 | 21 |
|
19 |
| -class hookTracker { |
20 |
| - public applied: Set<string> = new Set(); |
21 |
| - public available: Set<string> = new Set(); |
22 |
| - public notApplied: Set<string> = new Set(); |
| 22 | +// HookTracker keeps track of hooks that were applied, are available, and were not applied. |
| 23 | +// This is helpful when debugging custom hooks and bug detectors. |
| 24 | +class HookTracker { |
| 25 | + private _applied = new HookTable(); |
| 26 | + private _available = new HookTable(); |
| 27 | + private _notApplied = new HookTable(); |
23 | 28 |
|
24 | 29 | print() {
|
25 | 30 | console.log("DEBUG: [Hook] Summary:");
|
26 |
| - console.log("DEBUG: [Hook] Not applied:"); |
27 |
| - [...this.notApplied].sort().forEach((hook) => { |
28 |
| - console.log(`DEBUG: [Hook] ${hook}`); |
| 31 | + console.log("DEBUG: [Hook] Not applied: " + this._notApplied.length); |
| 32 | + this._notApplied.serialize().forEach((hook) => { |
| 33 | + console.log(`DEBUG: [Hook] not applied: ${hook.pkg} -> ${hook.target}`); |
29 | 34 | });
|
30 |
| - console.log("DEBUG: [Hook] Applied:"); |
31 |
| - [...this.applied].sort().forEach((hook) => { |
32 |
| - console.log(`DEBUG: [Hook] ${hook}`); |
| 35 | + console.log("DEBUG: [Hook] Applied: " + this._applied.length); |
| 36 | + this._applied.serialize().forEach((hook) => { |
| 37 | + console.log(`DEBUG: [Hook] applied: ${hook.pkg} -> ${hook.target}`); |
33 | 38 | });
|
34 |
| - console.log("DEBUG: [Hook] Available:"); |
35 |
| - [...this.available].sort().forEach((hook) => { |
36 |
| - console.log(`DEBUG: [Hook] ${hook}`); |
| 39 | + console.log("DEBUG: [Hook] Available: " + this._available.length); |
| 40 | + this._available.serialize().forEach((hook) => { |
| 41 | + console.log(`DEBUG: [Hook] available: ${hook.pkg} -> ${hook.target}`); |
37 | 42 | });
|
38 | 43 | }
|
39 | 44 |
|
40 | 45 | categorizeUnknown(requestedHooks: Hook[]): this {
|
41 | 46 | requestedHooks.forEach((hook) => {
|
42 |
| - if (!this.applied.has(hook.target) && !this.available.has(hook.target)) { |
43 |
| - this.notApplied.add(hook.target); |
| 47 | + if ( |
| 48 | + !this._applied.has(hook.pkg, hook.target) && |
| 49 | + !this._available.has(hook.pkg, hook.target) |
| 50 | + ) { |
| 51 | + this.addNotApplied(hook.pkg, hook.target); |
44 | 52 | }
|
45 | 53 | });
|
46 | 54 | return this;
|
47 | 55 | }
|
48 | 56 |
|
49 | 57 | clear() {
|
50 |
| - this.applied.clear(); |
51 |
| - this.notApplied.clear(); |
52 |
| - this.available.clear(); |
| 58 | + this._applied.clear(); |
| 59 | + this._notApplied.clear(); |
| 60 | + this._available.clear(); |
| 61 | + } |
| 62 | + |
| 63 | + addApplied(pkg: string, target: string) { |
| 64 | + this._applied.add(pkg, target); |
| 65 | + } |
| 66 | + |
| 67 | + addAvailable(pkg: string, target: string) { |
| 68 | + this._available.add(pkg, target); |
| 69 | + } |
| 70 | + |
| 71 | + addNotApplied(pkg: string, target: string) { |
| 72 | + this._notApplied.add(pkg, target); |
| 73 | + } |
| 74 | + |
| 75 | + get applied(): TrackedHook[] { |
| 76 | + return this._applied.serialize(); |
| 77 | + } |
| 78 | + |
| 79 | + get available(): TrackedHook[] { |
| 80 | + return this._available.serialize(); |
| 81 | + } |
| 82 | + |
| 83 | + get notApplied(): TrackedHook[] { |
| 84 | + return this._notApplied.serialize(); |
53 | 85 | }
|
54 | 86 | }
|
55 | 87 |
|
56 |
| -export const trackedHooks = new hookTracker(); |
| 88 | +// Stores package names and names of functions of interest (targets) from that package [packageName0 -> [target0, ...], ...]. |
| 89 | +// This structure is used to keep track of all functions seen during instrumentation and execution of the fuzzing run, |
| 90 | +// to determine which hooks have been applied, are available, and have not been applied. |
| 91 | +class HookTable { |
| 92 | + hooks: Map<string, Set<string>> = new Map(); |
| 93 | + |
| 94 | + add(pkg: string, target: string) { |
| 95 | + if (!this.hooks.has(pkg)) { |
| 96 | + this.hooks.set(pkg, new Set()); |
| 97 | + } |
| 98 | + this.hooks.get(pkg)?.add(target); |
| 99 | + } |
57 | 100 |
|
| 101 | + has(pkg: string, target: string) { |
| 102 | + if (!this.hooks.has(pkg)) { |
| 103 | + return false; |
| 104 | + } |
| 105 | + return this.hooks.get(pkg)?.has(target); |
| 106 | + } |
| 107 | + |
| 108 | + serialize(): TrackedHook[] { |
| 109 | + const result: TrackedHook[] = []; |
| 110 | + for (const [pkg, targets] of [...this.hooks].sort()) { |
| 111 | + for (const target of [...targets].sort()) { |
| 112 | + result.push({ pkg: pkg, target: target }); |
| 113 | + } |
| 114 | + } |
| 115 | + return result; |
| 116 | + } |
| 117 | + |
| 118 | + clear() { |
| 119 | + this.hooks.clear(); |
| 120 | + } |
| 121 | + |
| 122 | + get length() { |
| 123 | + let size = 0; |
| 124 | + for (const targets of this.hooks.values()) { |
| 125 | + size += targets.size; |
| 126 | + } |
| 127 | + return size; |
| 128 | + } |
| 129 | +} |
| 130 | + |
| 131 | +export function logHooks(hooks: Hook[]) { |
| 132 | + hooks.forEach((hook) => { |
| 133 | + if (process.env.JAZZER_DEBUG) { |
| 134 | + console.log( |
| 135 | + `DEBUG: Applied %s-hook in %s#%s`, |
| 136 | + HookType[hook.type], |
| 137 | + hook.pkg, |
| 138 | + hook.target |
| 139 | + ); |
| 140 | + } |
| 141 | + }); |
| 142 | +} |
| 143 | + |
| 144 | +export const hookTracker = new HookTracker(); |
| 145 | + |
| 146 | +/*eslint @typescript-eslint/no-explicit-any: 0 */ |
58 | 147 | export enum HookType {
|
59 | 148 | Before,
|
60 | 149 | After,
|
|
0 commit comments