1313 * See the License for the specific language governing permissions and
1414 * limitations under the License.
1515 */
16+ import * as util from "util" ;
17+
18+ export const Mode = {
19+ Fuzzing : "fuzzing" ,
20+ Regression : "regression" ,
21+ } as const ;
22+
23+ export type Mode = ( typeof Mode ) [ keyof typeof Mode ] ;
1624
1725export type LibAflOptions = {
1826 mode : "fuzzing" | "regression" ;
@@ -65,7 +73,7 @@ export interface Options {
6573 // Part of filepath names to include in the instrumentation.
6674 includes : string [ ] ;
6775 // Fuzzing mode.
68- mode : "fuzzing" | "regression" ;
76+ mode : Mode ;
6977 // Whether to run the fuzzer in sync mode or not.
7078 sync : boolean ;
7179 // Timeout for one fuzzing iteration in milliseconds.
@@ -115,7 +123,7 @@ export const defaultCLIOptions: Options = Object.freeze({
115123 fuzzTarget : "" ,
116124 idSyncFile : "" ,
117125 includes : [ "*" ] ,
118- mode : "fuzzing" ,
126+ mode : Mode . Fuzzing ,
119127 sync : false ,
120128 timeout : 5000 ,
121129 verbose : false ,
@@ -124,7 +132,7 @@ export const defaultCLIOptions: Options = Object.freeze({
124132export const defaultJestOptions : Options = Object . freeze ( {
125133 ...defaultCLIOptions ,
126134 engine : "libfuzzer" ,
127- mode : "regression" ,
135+ mode : Mode . Regression ,
128136} ) ;
129137
130138export type KeyFormatSource = ( key : string ) => string ;
@@ -174,7 +182,6 @@ export enum OptionSource {
174182}
175183
176184export type FuzzingEngine = Options [ "engine" ] ;
177- export type FuzzingMode = Options [ "mode" ] ;
178185
179186export function resolveEngine ( engine : string ) : FuzzingEngine {
180187 switch ( engine ) {
@@ -190,16 +197,23 @@ export function resolveEngine(engine: string): FuzzingEngine {
190197 }
191198}
192199
193- export function resolveMode ( mode : string ) : FuzzingMode {
194- switch ( mode ) {
195- case "fuzzing" :
196- case "regression" :
197- return mode ;
198- default :
199- throw new Error (
200- `Unknown fuzzing mode '${ mode } '. Supported modes are 'fuzzing' and 'regression'.` ,
201- ) ;
200+ function isOneOf < T extends Record < string , string > > (
201+ obj : T ,
202+ value : string ,
203+ ) : value is T [ keyof T ] {
204+ return Object . values ( obj ) . includes ( value as T [ keyof T ] ) ;
205+ }
206+
207+ export function resolveMode ( mode : string ) : Mode {
208+ if ( isOneOf ( Mode , mode ) ) {
209+ return mode ;
202210 }
211+
212+ throw new Error (
213+ `Unknown fuzzer mode '${ mode } '. Supported modes are ${ Object . values ( Mode )
214+ . map ( ( v ) => `'${ v } '` )
215+ . join ( ", " ) } .`,
216+ ) ;
203217}
204218
205219type DefaultSourceInfo = {
@@ -455,6 +469,18 @@ export function toOptionsWithPrintableSources(
455469 return result ;
456470}
457471
472+ export function printOptions ( options : OptionsManager , infix = "" ) {
473+ if ( process . env . JAZZER_DEBUG ) {
474+ console . error (
475+ util . formatWithOptions (
476+ { maxArrayLength : null , depth : null , colors : false } ,
477+ `DEBUG: [core] Jazzer.js options ${ infix } : \n%O` ,
478+ toOptionsWithPrintableSources ( options ) ,
479+ ) ,
480+ ) ;
481+ }
482+ }
483+
458484export function validateKeySource ( key : keyof Options , source : OptionSource ) {
459485 const sourceName = defaultOptions [ source ] . name ;
460486
0 commit comments