1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
let () = Random.init 42
type src =
| Random of Random.State.t
| Fd of Unix.file_descr
| Recording of Random.State.t * Buffer.t
type state = {
chan : src;
buf : Bytes.t;
mutable offset : int;
mutable len : int;
}
type 'a printer = Format.formatter -> 'a -> unit
type 'a strat =
| Choose of 'a gen list
| Map : ('f, 'a) gens * 'f -> 'a strat
| Bind : 'a gen * ('a -> 'b gen) -> 'b strat
| Option : 'a gen -> 'a option strat
| List : 'a gen -> 'a list strat
| List1 : 'a gen -> 'a list strat
| Array : 'a gen -> 'a array strat
| Array1 : 'a gen -> 'a array strat
| Unlazy of 'a gen Lazy.t
| Primitive of (state -> 'a)
| Print of 'a printer * 'a gen
and 'a gen = { strategy : 'a strat; small_examples : 'a list }
and ('k, 'res) gens =
| [] : ('res, 'res) gens
| ( :: ) : 'a gen * ('k, 'res) gens -> ('a -> 'k, 'res) gens
type nonrec +'a list = 'a list = [] | ( :: ) of 'a * 'a list
let unlazy f = { strategy = Unlazy f; small_examples = [] }
let fix f =
let rec lazygen = lazy (f (unlazy lazygen)) in
Lazy.force lazygen
let map (type f) (type a) (gens : (f, a) gens) (f : f) =
{
strategy = Map (gens, f);
small_examples = (match gens with [] -> [ f ] | _ -> []);
}
let dynamic_bind m f = { strategy = Bind (m, f); small_examples = [] }
let const x = map [] x
let choose gens =
{
strategy = Choose gens;
small_examples = List.map (fun x -> x.small_examples) gens |> List.concat;
}
let option gen = { strategy = Option gen; small_examples = [ None ] }
let list gen = { strategy = List gen; small_examples = [ [] ] }
let list1 gen =
{
strategy = List1 gen;
small_examples = List.map (fun x -> [ x ]) gen.small_examples;
}
let array gen = { strategy = Array gen; small_examples = [ [||] ] }
let array1 gen =
{
strategy = Array1 gen;
small_examples = List.map (fun x -> [| x |]) gen.small_examples;
}
let primitive f ex = { strategy = Primitive f; small_examples = [ ex ] }
let pair gena genb = map [ gena; genb ] (fun a b -> (a, b))
let concat_gen_list sep l =
match l with
| h :: t ->
List.fold_left
(fun acc e -> map [ acc; sep; e ] (fun acc sep e -> acc ^ sep ^ e))
h t
| [] -> const ""
let with_printer pp gen =
{ strategy = Print (pp, gen); small_examples = gen.small_examples }
let result gena genb =
choose [ map [ gena ] (fun va -> Ok va); map [ genb ] (fun vb -> Error vb) ]
let pp = Format.fprintf
let pp_int ppf n = pp ppf "%d" n
let pp_int32 ppf n = pp ppf "%s" (Int32.to_string n)
let pp_int64 ppf n = pp ppf "%s" (Int64.to_string n)
let pp_float ppf f = pp ppf "%f" f
let pp_bool ppf b = pp ppf "%b" b
let pp_char ppf c = pp ppf "%c" c
let pp_uchar ppf c = pp ppf "U+%04x" (Uchar.to_int c)
let pp_string ppf s = pp ppf "%S" s
let pp_print_iter ~pp_sep iter pp_v ppf v =
let is_first = ref true in
let pp_v v =
if !is_first then is_first := false else pp_sep ppf ();
pp_v ppf v
in
iter pp_v v
let pp_list pv ppf l =
pp ppf "@[<hv 1>[%a]@]"
(pp_print_iter ~pp_sep:(fun ppf () -> pp ppf ";@ ") List.iter pv)
l
let pp_array pv ppf a =
pp ppf "@[<hv 1>[|%a|]@]"
(pp_print_iter ~pp_sep:(fun ppf () -> pp ppf ";@ ") Array.iter pv)
a
let pp_option pv ppf = function
| None -> Format.fprintf ppf "None"
| Some x -> Format.fprintf ppf "(Some %a)" pv x
exception BadTest of string
exception FailedTest of unit printer
let guard = function true -> () | false -> raise (BadTest "guard failed")
let bad_test () = raise (BadTest "bad test")
let nonetheless = function None -> bad_test () | Some a -> a
let get_data chan buf off len =
match chan with
| Random rand ->
for i = off to off + len - 1 do
Bytes.set buf i (Char.chr (Random.State.bits rand land 0xff))
done;
len - off
| Recording (rand, record) ->
for i = off to off + len - 1 do
let c = Char.chr (Random.State.bits rand land 0xff) in
Bytes.set buf i c;
Buffer.add_char record c
done;
len - off
| Fd ch -> Unix.read ch buf off len
let refill src =
assert (src.offset <= src.len);
let remaining = src.len - src.offset in
Bytes.blit src.buf src.offset src.buf 0 remaining;
src.len <- remaining;
src.offset <- 0;
let read =
get_data src.chan src.buf remaining (Bytes.length src.buf - remaining)
in
if read = 0 then raise (BadTest "premature end of file")
else src.len <- remaining + read
let rec getbytes src n =
assert (src.offset <= src.len);
if n > Bytes.length src.buf then failwith "request too big";
if src.len - src.offset >= n then (
let off = src.offset in
src.offset <- src.offset + n;
off)
else (
refill src;
getbytes src n)
let read_char src =
let off = getbytes src 1 in
Bytes.get src.buf off
let read_byte src = Char.code (read_char src)
let read_bool src =
let n = read_byte src in
n land 1 = 1
let bool = with_printer pp_bool (primitive read_bool false)
let uint8 = with_printer pp_int (primitive read_byte 0)
let int8 = with_printer pp_int (map [ uint8 ] (fun n -> n - 128))
let read_uint16 src =
let off = getbytes src 2 in
Bytes.get_uint16_le src.buf off
let read_int16 src =
let off = getbytes src 2 in
Bytes.get_int16_le src.buf off
let uint16 = with_printer pp_int (primitive read_uint16 0)
let int16 = with_printer pp_int (primitive read_int16 0)
let read_int32 src =
let off = getbytes src 4 in
Bytes.get_int32_le src.buf off
let read_int64 src =
let off = getbytes src 8 in
Bytes.get_int64_le src.buf off
let int32 = with_printer pp_int32 (primitive read_int32 0l)
let int64 = with_printer pp_int64 (primitive read_int64 0L)
let int =
with_printer pp_int
(if Sys.word_size <= 32 then map [ int32 ] Int32.to_int
else map [ int64 ] Int64.to_int)
let float =
with_printer pp_float
(primitive
(fun src ->
let off = getbytes src 8 in
let i64 = Bytes.get_int64_le src.buf off in
Int64.float_of_bits i64)
0.)
let char = with_printer pp_char (primitive read_char 'a')
let bytes =
with_printer pp_string
(primitive
(fun src ->
let buf = Bytes.make 64 '\255' in
let rec read_bytes p =
if p >= Bytes.length buf then p
else
match read_char src with
| '\000' -> p
| '\001' ->
Bytes.set buf p (read_char src);
read_bytes (p + 1)
| c ->
Bytes.set buf p c;
read_bytes (p + 1)
in
let count = read_bytes 0 in
Bytes.sub_string buf 0 count)
"")
let bytes_fixed n =
with_printer pp_string
(primitive
(fun src ->
let off = getbytes src n in
Bytes.sub_string src.buf off n)
(String.make n 'a'))
let choose_int n state =
assert (n > 0);
if n = 1 then 0
else if n <= 0x100 then read_byte state mod n
else if n < 0x1000000 then
Int32.(to_int (abs (rem (read_int32 state) (of_int n))))
else Int64.(to_int (abs (rem (read_int64 state) (of_int n))))
let choice_bytes n i =
if n = 1 then ""
else if n <= 0x100 then String.make 1 (Char.chr i)
else if n < 0x1000000 then (
let b = Bytes.create 4 in
Bytes.set_int32_le b 0 (Int32.of_int i);
Bytes.to_string b)
else
let b = Bytes.create 8 in
Bytes.set_int64_le b 0 (Int64.of_int i);
Bytes.to_string b
let range ?(min = 0) n =
if n <= 0 then
raise (Invalid_argument "Alcobar.range: argument n must be positive");
if min < 0 then
raise
(Invalid_argument "Alcobar.range: argument min must be positive or null");
with_printer pp_int (primitive (fun s -> min + choose_int n s) min)
let uchar : Uchar.t gen =
map
[ range 0x110000 ]
(fun x ->
guard (Uchar.is_valid x);
Uchar.of_int x)
let uchar = with_printer pp_uchar uchar
let rec sequence = function
| g :: gs -> map [ g; sequence gs ] (fun x xs -> x :: xs)
| [] -> const []
let shuffle_arr arr =
let n = Array.length arr in
let gs = List.init n (fun i -> range ~min:i (n - i)) in
map [ sequence gs ] @@ fun js ->
js
|> List.iteri (fun i j ->
let t = arr.(i) in
arr.(i) <- arr.(j);
arr.(j) <- t);
arr
let shuffle l = map [ shuffle_arr (Array.of_list l) ] Array.to_list
exception GenFailed of exn * Printexc.raw_backtrace * unit printer
let rec generate : type a. int -> state -> a gen -> a * unit printer =
fun size input gen ->
if size <= 1 && gen.small_examples <> [] then
(List.hd gen.small_examples, fun ppf () -> pp ppf "?")
else
match gen.strategy with
| Choose gens ->
let n = choose_int (List.length gens) input in
let v, pv = generate size input (List.nth gens n) in
(v, fun ppf () -> pp ppf "#%d %a" n pv ())
| Map ([], k) -> (k, fun ppf () -> pp ppf "?")
| Map (gens, f) ->
let rec len : type k res. int -> (k, res) gens -> int =
fun acc xs -> match xs with [] -> acc | _ :: xs -> len (1 + acc) xs
in
let n = len 0 gens in
let size = (size - 1) / n in
let v, pvs = gen_apply size input gens f in
begin match v with
| Ok v -> (v, pvs)
| Error (e, bt) -> raise (GenFailed (e, bt, pvs))
end
| Bind (m, f) ->
let index, pv_index = generate (size - 1) input m in
let a, pv = generate (size - 1) input (f index) in
(a, fun ppf () -> pp ppf "(%a) => %a" pv_index () pv ())
| Option gen ->
if size < 1 then (None, fun ppf () -> pp ppf "None")
else if read_bool input then
let v, pv = generate size input gen in
(Some v, fun ppf () -> pp ppf "Some (%a)" pv ())
else (None, fun ppf () -> pp ppf "None")
| List gen ->
let elems = generate_list size input gen in
( List.map fst elems,
fun ppf () -> pp_list (fun ppf (_, pv) -> pv ppf ()) ppf elems )
| List1 gen ->
let elems = generate_list1 size input gen in
( List.map fst elems,
fun ppf () -> pp_list (fun ppf (_, pv) -> pv ppf ()) ppf elems )
| Array gen ->
let elems = generate_list size input gen in
let elems = Array.of_list elems in
( Array.map fst elems,
fun ppf () -> pp_array (fun ppf (_, pv) -> pv ppf ()) ppf elems )
| Array1 gen ->
let elems = generate_list1 size input gen in
let elems = Array.of_list elems in
( Array.map fst elems,
fun ppf () -> pp_array (fun ppf (_, pv) -> pv ppf ()) ppf elems )
| Primitive gen -> (gen input, fun ppf () -> pp ppf "?")
| Unlazy gen -> generate size input (Lazy.force gen)
| Print (ppv, gen) ->
let v, _ = generate size input gen in
(v, fun ppf () -> ppv ppf v)
and generate_list : type a. int -> state -> a gen -> (a * unit printer) list =
fun size input gen ->
if size <= 1 then []
else if read_bool input then generate_list1 size input gen
else []
and generate_list1 : type a. int -> state -> a gen -> (a * unit printer) list =
fun size input gen ->
let ans = generate (size / 2) input gen in
ans :: generate_list (size / 2) input gen
and gen_apply : type k res.
int ->
state ->
(k, res) gens ->
k ->
(res, exn * Printexc.raw_backtrace) result * unit printer =
fun size state gens f ->
let rec go : type k res.
int ->
state ->
(k, res) gens ->
k ->
(res, exn * Printexc.raw_backtrace) result * unit printer list =
fun size input gens ->
match gens with
| [] -> fun x -> (Ok x, [])
| g :: gs ->
fun f ->
let v, pv = generate size input g in
let res, pvs =
match f v with
| exception (BadTest _ as e) -> raise e
| exception e -> (Error (e, Printexc.get_raw_backtrace ()), [])
| fv -> go size input gs fv
in
(res, pv :: pvs)
in
let v, pvs = go size state gens f in
let pvs =
fun ppf () ->
match pvs with
| [ pv ] -> pv ppf ()
| pvs -> pp_list (fun ppf pv -> pv ppf ()) ppf pvs
in
(v, pvs)
let fail s = raise (FailedTest (fun ppf () -> pp ppf "%s" s))
let failf format = Format.kasprintf fail format
let check = function
| true -> ()
| false -> raise (FailedTest (fun ppf () -> pp ppf "check false"))
let check_eq ?pp:pv ?cmp ?eq a b =
let pass =
match (eq, cmp) with
| Some eq, _ -> eq a b
| None, Some cmp -> cmp a b = 0
| None, None -> Stdlib.compare a b = 0
in
if pass then ()
else
raise
(FailedTest
(fun ppf () ->
match pv with
| None -> pp ppf "different"
| Some pv -> pp ppf "@[<hv>%a@ !=@ %a@]" pv a pv b))
let () = Printexc.record_backtrace true
type sample_outcomes = {
declared : string list;
reached : (string, unit) Hashtbl.t;
}
let in_flight : sample_outcomes option ref = ref None
let reach outcome =
match !in_flight with
| None -> ()
| Some { declared; reached } ->
if List.mem outcome declared then Hashtbl.replace reached outcome ()
else
failf "reach %S: not an outcome this test declares (%s)" outcome
(String.concat ", " declared)
type test =
| Test : {
suite : string;
name : string;
outcomes : string list;
gens : ('f, unit) gens;
f : 'f;
}
-> test
type test_status =
| TestPass of unit printer
| BadInput of string
| GenFail of exn * Printexc.raw_backtrace * unit printer
| TestExn of exn * Printexc.raw_backtrace * unit printer
| TestFail of unit printer * unit printer
let run_once outcomes (gens : (_, unit) gens) f state =
let sample = { declared = outcomes; reached = Hashtbl.create 8 } in
in_flight := Some sample;
let status =
Fun.protect
~finally:(fun () -> in_flight := None)
(fun () ->
match gen_apply 100 state gens f with
| Ok (), pvs -> TestPass pvs
| Error (FailedTest p, _), pvs -> TestFail (p, pvs)
| Error (e, bt), pvs -> TestExn (e, bt, pvs)
| exception BadTest s -> BadInput s
| exception GenFailed (e, bt, pvs) -> GenFail (e, bt, pvs))
in
(status, sample.reached)
let classify_status = function
| TestPass _ -> `Pass
| BadInput _ -> `Bad
| GenFail _ -> `Fail
| TestExn _ | TestFail _ -> `Fail
let print_status ppf status =
let print_ex ppf (e, bt) =
pp ppf "%s" (Printexc.to_string e);
bt |> Printexc.raw_backtrace_to_string |> String.split_on_char '\n'
|> List.iter (pp ppf "@,%s")
in
match status with
| TestPass pvs ->
pp ppf "When given the input:@.@[<v 4>@,%a@,@]@.the test passed." pvs ()
| BadInput s -> pp ppf "The testcase was invalid:@.%s" s
| GenFail (e, bt, pvs) ->
pp ppf
"When given the input:@.@[<4>%a@]@.the testcase generator threw an \
exception:@.@[<v 4>@,\
%a@,\
@]"
pvs () print_ex (e, bt)
| TestExn (e, bt, pvs) ->
pp ppf
"When given the input:@.@[<v 4>@,\
%a@,\
@]@.the test threw an exception:@.@[<v 4>@,\
%a@,\
@]"
pvs () print_ex (e, bt)
| TestFail (err, pvs) ->
pp ppf
"When given the input:@.@[<v 4>@,\
%a@,\
@]@.the test failed:@.@[<v 4>@,\
%a@,\
@]"
pvs () err ()
let prng_state_of_seed seed =
let seed =
Int64.
[|
to_int (logand (of_int 0xffff) seed);
to_int (logand (of_int 0xffff) (shift_right seed 16));
to_int (logand (of_int 0xffff) (shift_right seed 32));
to_int (logand (of_int 0xffff) (shift_right seed 48));
|]
in
Random.State.make seed
let src_of_seed seed = Random (prng_state_of_seed seed)
type config = {
seed : int64 option;
repeat : int;
verbose : bool;
infinite : bool;
timeout : int;
budget : float;
}
exception Timeout
let default_timeout =
match Sys.getenv_opt "ALCOBAR_TIMEOUT" with
| Some s -> ( try int_of_string s with _ -> 2)
| None -> 2
let config_term ~gen_corpus =
let open Cmdliner in
let seed =
let doc = "The seed (an int64) for the PRNG." in
Arg.(value & opt (some int64) None & info [ "s"; "seed" ] ~doc)
in
let repeat =
let doc = "The number of times to repeat each test." in
Arg.(value & opt int 5000 & info [ "r"; "repeat" ] ~doc)
in
let verbose =
let doc = "Print information on each passing test." in
Arg.(value & flag & info [ "alcobar-verbose" ] ~doc)
in
let infinite =
let doc = "Run until a failure is found." in
Arg.(value & flag & info [ "i"; "infinite" ] ~doc)
in
let timeout =
let doc =
"Per-test timeout in seconds (0 to disable). Can also be set via the \
ALCOBAR_TIMEOUT environment variable."
in
Arg.(value & opt int default_timeout & info [ "timeout" ] ~doc)
in
let budget =
let doc =
"Total time budget per test in seconds (0 to disable). Iteration stops \
when the budget is exhausted."
in
Arg.(value & opt float 2. & info [ "budget" ] ~docv:"SECONDS" ~doc)
in
let corpus =
let doc =
"Write a seed corpus for $(b,afl-fuzz) into $(docv) and exit: a few \
inputs per test, each running the test it was generated for."
in
Arg.(
value & opt (some string) None & info [ "gen-corpus" ] ~docv:"DIR" ~doc)
in
let config seed repeat verbose infinite timeout budget = function
| Some dir ->
gen_corpus dir;
exit 0
| None -> { seed; repeat; verbose; infinite; timeout; budget }
in
Term.(
const config $ seed $ repeat $ verbose $ infinite $ timeout $ budget
$ corpus)
let with_timeout timeout f =
if timeout <= 0 then f ()
else
let old_handler =
Sys.signal Sys.sigalrm (Sys.Signal_handle (fun _ -> raise Timeout))
in
let old_alarm = Unix.alarm timeout in
Fun.protect
~finally:(fun () ->
ignore (Unix.alarm old_alarm);
Sys.set_signal Sys.sigalrm old_handler)
f
let pp_counts ppf (outcomes, count) =
pp ppf "%s"
(String.concat ", "
(List.map (fun o -> Printf.sprintf "%s %d" o (count o)) outcomes))
let outcome_reports = ref []
let print_outcome_reports () =
match List.rev !outcome_reports with
| [] -> ()
| reports ->
Format.print_flush ();
print_string "Outcomes reached:\n";
List.iter (Printf.printf " %s\n") reports;
flush stdout
let run_property_test (Test { suite; name; outcomes; gens; f }) config =
let seed =
match config.seed with Some s -> s | None -> Random.int64 Int64.max_int
in
let seedsrc = prng_state_of_seed seed in
let npass = ref 0 in
let ndiscarded = ref 0 in
let failure = ref None in
let counts = Hashtbl.create 8 in
let count o = try Hashtbl.find counts o with Not_found -> 0 in
let max_iter = if config.infinite then max_int else config.repeat in
let start_time = Unix.gettimeofday () in
let within_budget () =
config.budget <= 0. || Unix.gettimeofday () -. start_time < config.budget
in
while !npass < max_iter && Option.is_none !failure && within_budget () do
let s = Random.State.int64 seedsrc Int64.max_int in
let state =
{ chan = src_of_seed s; buf = Bytes.make 256 '0'; offset = 0; len = 0 }
in
let status, reached =
try with_timeout config.timeout (fun () -> run_once outcomes gens f state)
with Timeout ->
( TestExn
( Timeout,
Printexc.get_raw_backtrace (),
fun ppf () -> pp ppf "<timeout after %ds>" config.timeout ),
Hashtbl.create 0 )
in
match classify_status status with
| `Pass ->
incr npass;
Hashtbl.iter
(fun o () -> Hashtbl.replace counts o (count o + 1))
reached;
if config.verbose then Printf.printf " pass %d\n%!" !npass
| `Bad -> incr ndiscarded
| `Fail -> failure := Some status
done;
if outcomes <> [] then
outcome_reports :=
Format.asprintf "%s/%s: %d samples: %a" suite name !npass pp_counts
(outcomes, count)
:: !outcome_reports;
match !failure with
| Some status -> Alcotest.fail (Format.asprintf "%a" print_status status)
| None -> (
if !npass = 0 then
Alcotest.fail
(Printf.sprintf
"no iteration ran: %d generated input%s rejected by a guard, and \
the property was never evaluated"
!ndiscarded
(if !ndiscarded = 1 then " was" else "s were"));
match List.filter (fun o -> count o = 0) outcomes with
| [] -> ()
| missed ->
Alcotest.fail
(Format.asprintf "outcome%s %s never reached in %d samples: %a"
(if List.length missed = 1 then "" else "s")
(String.concat ", " (List.map (Printf.sprintf "%S") missed))
!npass pp_counts (outcomes, count)))
let run_verdict check (_ : config) =
match check () with
| () -> ()
| exception FailedTest p -> Alcotest.fail (Format.asprintf "%a" p ())
| exception BadTest _ -> Alcotest.skip ()
let run_with_alcotest ~gen_corpus name tests verdicts =
let groups = Hashtbl.create 16 in
List.iter
(fun (Test { suite; name; _ } as test) ->
let tc = Alcotest.test_case name `Quick (run_property_test test) in
let prev = try Hashtbl.find groups suite with Not_found -> [] in
Hashtbl.replace groups suite (tc :: prev))
tests;
let suites =
Hashtbl.fold (fun group tcs acc -> (group, List.rev tcs) :: acc) groups []
|> List.sort (fun (a, _) (b, _) -> String.compare a b)
in
let suites =
match verdicts with
| [] -> suites
| verdicts ->
suites
@ [
( "verdict",
List.map
(fun (suite, name, check) ->
Alcotest.test_case
(suite ^ ": " ^ name)
`Quick (run_verdict check))
verdicts );
]
in
at_exit print_outcome_reports;
Alcotest.run_with_args name (config_term ~gen_corpus) suites
exception TestFailure
let run_afl tests file =
AflPersistent.run (fun () ->
let fd = Unix.openfile file [ Unix.O_RDONLY ] 0o000 in
let state =
{ chan = Fd fd; buf = Bytes.make 256 '0'; offset = 0; len = 0 }
in
let status =
try
let (Test { outcomes; gens; f; _ }) =
List.nth tests (choose_int (List.length tests) state)
in
fst (run_once outcomes gens f state)
with BadTest s -> BadInput s
in
Unix.close fd;
match classify_status status with
| `Pass | `Bad -> ()
| `Fail ->
Printexc.record_backtrace false;
raise TestFailure)
let detect_afl_file () =
let n = Array.length Sys.argv in
if n >= 2 then
let last = Sys.argv.(n - 1) in
if Sys.file_exists last && not (Sys.is_directory last) then Some last
else None
else None
let shortfall ~suite ~name ~good ~seeds_per_test ~attempts ~discarded ~failed =
let reasons =
List.filter_map
(fun (n, what) ->
if n = 0 then None else Some (Printf.sprintf "%d %s" n what))
[ (discarded, "rejected by a guard"); (failed, "failed the property") ]
in
Printf.sprintf "gen-corpus: %s/%s: %s after %d attempts%s\n" suite name
(if good = 0 then "no seed"
else Printf.sprintf "%d of %d seeds" good seeds_per_test)
attempts
(match reasons with [] -> "" | l -> " (" ^ String.concat ", " l ^ ")")
let generate_corpus dir tests =
(try Unix.mkdir dir 0o755 with Unix.Unix_error (Unix.EEXIST, _, _) -> ());
let seedsrc = prng_state_of_seed 42L in
let count = ref 0 in
let seeds_per_test = 5 in
let max_attempts = 200 in
let shortfalls = ref [] in
let choices = List.length tests in
List.iteri
(fun index (Test { suite; name; outcomes; gens; f }) ->
let choice = choice_bytes choices index in
let good = ref 0 in
let attempts = ref 0 in
let discarded = ref 0 in
let failed = ref 0 in
while !good < seeds_per_test && !attempts < max_attempts do
incr attempts;
let s = Random.State.int64 seedsrc Int64.max_int in
let record = Buffer.create 256 in
Buffer.add_string record choice;
let state =
{
chan = Recording (prng_state_of_seed s, record);
buf = Bytes.make 256 '0';
offset = 0;
len = 0;
}
in
let status, _ = run_once outcomes gens f state in
match classify_status status with
| `Pass ->
let filename = Printf.sprintf "%s/seed_%03d" dir !count in
let oc = open_out_bin filename in
Buffer.output_buffer oc record;
close_out oc;
incr count;
incr good
| `Bad -> incr discarded
| `Fail -> incr failed
done;
if !good < seeds_per_test then
shortfalls :=
shortfall ~suite ~name ~good:!good ~seeds_per_test ~attempts:!attempts
~discarded:!discarded ~failed:!failed
:: !shortfalls)
tests;
List.iter prerr_string (List.rev !shortfalls);
flush stderr;
Printf.printf "gen-corpus: wrote %d seed files to %s/\n%!" !count dir;
if !count = 0 then (
prerr_endline "gen-corpus: no test could be seeded, the corpus is empty";
exit 1)
type test_case =
| TC : {
name : string;
outcomes : string list;
gens : ('f, unit) gens;
f : 'f;
}
-> test_case
| Verdict of { name : string; check : unit -> unit }
let verdict name check = Verdict { name; check }
let rec first_duplicate = function
| [] -> None
| o :: rest -> if List.mem o rest then Some o else first_duplicate rest
let test_case ?(outcomes = []) name gens f =
match first_duplicate outcomes with
| Some o ->
invalid_arg
(Printf.sprintf "Alcobar.test_case %S: outcome %S declared twice" name o)
| None -> TC { name; outcomes; gens; f }
let run name suites =
let tests =
List.concat_map
(fun (suite_name, tcs) ->
List.filter_map
(function
| TC { name; outcomes; gens; f } ->
Some (Test { suite = suite_name; name; outcomes; gens; f })
| Verdict _ -> None)
tcs)
suites
in
let verdicts =
List.concat_map
(fun (suite_name, tcs) ->
List.filter_map
(function
| Verdict { name; check } -> Some (suite_name, name, check)
| TC _ -> None)
tcs)
suites
in
match detect_afl_file () with
| Some file -> run_afl tests file
| None ->
run_with_alcotest
~gen_corpus:(fun dir -> generate_corpus dir tests)
name tests verdicts
module Syntax = struct
let ( let* ) = dynamic_bind
let ( let+ ) gen map_fn = map [ gen ] map_fn
let ( and+ ) = pair
end