diff --git a/src/Engine.ml b/src/Engine.ml index b5dd18eda9ba4d60720c7b07d0dc9a2e0535c188..b2969150283c6af79516a7fe969be556f6df0f90 100644 --- a/src/Engine.ml +++ b/src/Engine.ml @@ -1340,14 +1340,17 @@ let test fuel = (* -------------------------------------------------------------------------- *) -(* [run prologue fuel] performs one test run. *) +(* [run ?prologue fuel] performs one test run. *) (* The function [prologue] is executed after the source channel has been opened, so it is allowed to call the data generation functions in the module [Gen]. It is also allowed to call [declare], [override_exn_eq], [dprintf], etc. Thus, it can modify our global state in several ways. *) -let run prologue fuel = +(* [prologue] is an optional argument. Its default value is [ignore], + which has no effect. *) + +let run ?(prologue = ignore) fuel = (* Restore a correct initial state for this iteration. *) GlobalState.reset(); (* Execute [prologue()], which can declare new operations, etc. *) @@ -1384,7 +1387,7 @@ let init () = (* -------------------------------------------------------------------------- *) -(* [run_afl source prologue fuel] performs many test runs in AFL mode. *) +(* [run_afl ?prologue source fuel] performs many test runs in AFL mode. *) (* [source] is the name of the input file that AFL has created for us. *) @@ -1392,12 +1395,12 @@ let init () = must open and close the source file, as it is recreated afresh by AFL for each run. *) -let run_afl source prologue fuel = +let run_afl ?prologue source fuel = init(); AflPersistent.run (fun () -> Gen.with_source (Some source) (fun () -> try - run prologue fuel + run ?prologue fuel with Abort (scenario, _fuel) -> (* Print the scenario to [stdout]. This is necessary for the user to be able to find out what happened. *) @@ -1439,7 +1442,7 @@ let log scenario fuel = (* -------------------------------------------------------------------------- *) -(* [run_random_loop prologue clock fuel accu] performs an unbounded number of +(* [run_random_loop ?prologue clock fuel accu] performs an unbounded number of test runs in random mode. *) (* While the tests run, we print information roughly every second. We print @@ -1462,12 +1465,12 @@ let log scenario fuel = The accumulator [accu] records the number of failure scenarios discovered so far. *) -let rec run_random_loop prologue clock fuel (accu : int) : int = +let rec run_random_loop ?prologue clock fuel (accu : int) : int = try (* An infinite loop. *) while true do (* Perform one run. *) - run prologue fuel; + run ?prologue fuel; (* Tick the clock and, once in a while, display statistics. *) tick clock fuel done; @@ -1480,11 +1483,11 @@ let rec run_random_loop prologue clock fuel (accu : int) : int = fuel. Try again, with this amount. This restricts our search space, and (with luck) we might now be able to find an even shorter scenario. *) - run_random_loop prologue clock fuel (accu + 1) + run_random_loop ?prologue clock fuel (accu + 1) | Clock.Timeout -> accu -(* [run_random prologue fuel] performs an unbounded number of test +(* [run_random ?prologue fuel] performs an unbounded number of test runs in random mode. *) (* The source of random bits, /dev/urandom, is opened just once; @@ -1496,19 +1499,19 @@ let rec run_random_loop prologue clock fuel (accu : int) : int = The main reason for this change is to allow imposing a time limit on the whole process. *) -let run_random ?timeout prologue fuel : int = +let run_random ?timeout ?prologue fuel : int = init(); let source = None in Gen.with_source source @@ fun () -> let granularity = 1000 in let clock = Clock.make ?timeout granularity in - run_random_loop prologue clock fuel 0 + run_random_loop ?prologue clock fuel 0 (* -------------------------------------------------------------------------- *) (* [main ?prologue fuel] parses the command line and starts testing. *) -let main ?prologue:(prologue=ignore) fuel = +let main ?prologue fuel = (* Parse the command line. *) let usage = sprintf "Usage: %s <input file>" Sys.argv.(0) in @@ -1519,7 +1522,7 @@ let main ?prologue:(prologue=ignore) fuel = otherwise, enter random testing mode. *) match !source with | Some source -> - run_afl source prologue fuel + run_afl ?prologue source fuel | None -> - let (failures : int) = run_random prologue fuel in + let (failures : int) = run_random ?prologue fuel in ignore failures