14
14
* limitations under the License.
15
15
*/
16
16
17
- /*eslint @typescript-eslint/no-explicit-any: 0 */
17
+ // Stores package names and function names [packageName -> [fnName, ...], ...].
18
+ // This structure is used to keep track of all functions seen during instrumentation and execution of the fuzzing run,
19
+ // to determine which hooks have been applied, are available, and have not been applied.
20
+ export class TrackedHooks {
21
+ hooks : Map < string , Set < string > > ;
22
+ constructor ( ) {
23
+ this . hooks = new Map ( ) ;
24
+ }
25
+
26
+ add ( pkg : string , target : string ) {
27
+ if ( ! this . hooks . has ( pkg ) ) {
28
+ this . hooks . set ( pkg , new Set ( ) ) ;
29
+ }
30
+ this . hooks . get ( pkg ) ?. add ( target ) ;
31
+ }
32
+
33
+ has ( pkg : string , target : string ) {
34
+ if ( ! this . hooks . has ( pkg ) ) {
35
+ return false ;
36
+ }
37
+ return this . hooks . get ( pkg ) ?. has ( target ) ;
38
+ }
39
+
40
+ serialize ( ) : TrackedHookData [ ] {
41
+ const result : TrackedHookData [ ] = [ ] ;
42
+ for ( const [ pkg , targets ] of [ ...this . hooks ] . sort ( ) ) {
43
+ for ( const target of [ ...targets ] . sort ( ) ) {
44
+ result . push ( { pkg : pkg , target : target } ) ;
45
+ }
46
+ }
47
+ return result ;
48
+ }
49
+
50
+ clear ( ) {
51
+ this . hooks . clear ( ) ;
52
+ }
53
+
54
+ get size ( ) {
55
+ let size = 0 ;
56
+ for ( const targets of this . hooks . values ( ) ) {
57
+ size += targets . size ;
58
+ }
59
+ return size ;
60
+ }
61
+ }
62
+
63
+ export interface TrackedHookData {
64
+ target : string ;
65
+ pkg : string ;
66
+ }
18
67
19
68
class hookTracker {
20
- public applied : Set < string > = new Set ( ) ;
21
- public available : Set < string > = new Set ( ) ;
22
- public notApplied : Set < string > = new Set ( ) ;
69
+ public applied = new TrackedHooks ( ) ;
70
+ public available = new TrackedHooks ( ) ;
71
+ public notApplied = new TrackedHooks ( ) ;
23
72
24
73
print ( ) {
25
74
console . log ( "DEBUG: [Hook] Summary:" ) ;
26
- console . log ( "DEBUG: [Hook] Not applied:" ) ;
27
- [ ... this . notApplied ] . sort ( ) . forEach ( ( hook ) => {
28
- console . log ( `DEBUG: [Hook] ${ hook } ` ) ;
75
+ console . log ( "DEBUG: [Hook] Not applied: " + this . notApplied . size ) ;
76
+ this . notApplied . serialize ( ) . forEach ( ( hook ) => {
77
+ console . log ( `DEBUG: [Hook] ${ hook . pkg } -> ${ hook . target } ` ) ;
29
78
} ) ;
30
- console . log ( "DEBUG: [Hook] Applied:" ) ;
31
- [ ... this . applied ] . sort ( ) . forEach ( ( hook ) => {
32
- console . log ( `DEBUG: [Hook] ${ hook } ` ) ;
79
+ console . log ( "DEBUG: [Hook] Applied: " + this . applied . size ) ;
80
+ this . applied . serialize ( ) . forEach ( ( hook ) => {
81
+ console . log ( `DEBUG: [Hook] ${ hook . pkg } -> ${ hook . target } ` ) ;
33
82
} ) ;
34
- console . log ( "DEBUG: [Hook] Available:" ) ;
35
- [ ... this . available ] . sort ( ) . forEach ( ( hook ) => {
36
- console . log ( `DEBUG: [Hook] ${ hook } ` ) ;
83
+ console . log ( "DEBUG: [Hook] Available: " + this . available . size ) ;
84
+ this . available . serialize ( ) . forEach ( ( hook ) => {
85
+ console . log ( `DEBUG: [Hook] ${ hook . pkg } -> ${ hook . target } ` ) ;
37
86
} ) ;
38
87
}
39
88
40
89
categorizeUnknown ( requestedHooks : Hook [ ] ) : this {
41
90
requestedHooks . forEach ( ( hook ) => {
42
- if ( ! this . applied . has ( hook . target ) && ! this . available . has ( hook . target ) ) {
43
- this . notApplied . add ( hook . target ) ;
91
+ if (
92
+ ! this . applied . has ( hook . pkg , hook . target ) &&
93
+ ! this . available . has ( hook . pkg , hook . target )
94
+ ) {
95
+ this . notApplied . add ( hook . pkg , hook . target ) ;
44
96
}
45
97
} ) ;
46
98
return this ;
@@ -53,8 +105,22 @@ class hookTracker {
53
105
}
54
106
}
55
107
108
+ export function logHooks ( hooks : Hook [ ] ) {
109
+ hooks . forEach ( ( hook ) => {
110
+ if ( process . env . JAZZER_DEBUG ) {
111
+ console . log (
112
+ `DEBUG: Applied %s-hook in %s#%s` ,
113
+ HookType [ hook . type ] ,
114
+ hook . pkg ,
115
+ hook . target
116
+ ) ;
117
+ }
118
+ } ) ;
119
+ }
120
+
56
121
export const trackedHooks = new hookTracker ( ) ;
57
122
123
+ /*eslint @typescript-eslint/no-explicit-any: 0 */
58
124
export enum HookType {
59
125
Before ,
60
126
After ,
0 commit comments