Skip to content

Commit df9869b

Browse files
Handle a seq-test file compilation failure
1 parent 1d99b68 commit df9869b

File tree

1 file changed

+76
-39
lines changed

1 file changed

+76
-39
lines changed

lib/seqTests/seqTests.ml

Lines changed: 76 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,10 @@ let rec gen_sequence
198198
(filename_base : string)
199199
(src_code : string list)
200200
(fun_decls : Pp.document)
201-
: (Pp.document * test_stats, Pp.document * test_stats) Either.either
201+
: [ `PostConditionViolation of Pp.document * test_stats
202+
| `Success of Pp.document * test_stats
203+
| `CompileFailure of Pp.document * test_stats
204+
]
202205
=
203206
let max_retries = Config.get_max_backtracks () in
204207
let num_resets = Config.get_max_resets () in
@@ -212,7 +215,7 @@ let rec gen_sequence
212215
| 0 ->
213216
let unmap_stmts = List.map Fulminate.Ownership.generate_c_local_ownership_exit ctx in
214217
let unmap_str = hardline ^^ separate_map hardline stmt_to_doc unmap_stmts in
215-
Right (seq_so_far ^^ unmap_str ^^ hardline ^^ string "return 0;", stats)
218+
`Success (seq_so_far ^^ unmap_str ^^ hardline ^^ string "return 0;", stats)
216219
| n ->
217220
let fs =
218221
List.map
@@ -222,10 +225,16 @@ let rec gen_sequence
222225
let rec gen_test
223226
(args_map : Sym.t * ((C.qualifiers * C.ctype) * (Sym.t * C.ctype) list))
224227
(retries_left : int)
225-
: (SymSet.elt * C.ctype) list * int * (document, string * document) Either.either
228+
: (SymSet.elt * C.ctype) list
229+
* int
230+
* [ `OutOfTries of unit
231+
| `CompileFailure of document
232+
| `PostConditionViolation of document
233+
| `Success of string * document
234+
]
226235
=
227236
if retries_left = 0 then
228-
(ctx, prev, Either.Left empty)
237+
(ctx, prev, `OutOfTries ())
229238
else (
230239
match args_map with
231240
| f, ((qualifiers, ret_ty), args) ->
@@ -270,11 +279,12 @@ let rec gen_sequence
270279
in
271280
let output, status = out_to_list (output_dir ^ "/run_tests.sh") in
272281
(match status with
273-
| WEXITED 0 -> (ctx', prev, Either.Right (Sym.pp_string f, curr_test))
282+
| WEXITED 0 -> (ctx', prev, `Success (Sym.pp_string f, curr_test))
274283
| _ ->
275284
let violation_regex = Str.regexp {| +\^~+ .+\.c:\([0-9]+\):[0-9]+-[0-9]+|} in
276285
let is_post_regex = Str.regexp {|[ \t]*\(/\*@\)?[ \t]*ensures|} in
277286
let is_pre_regex = Str.regexp {|[ \t]*\(/\*@\)?[ \t]*requires|} in
287+
let is_bad_compile_regex = Str.regexp {|Failed to compile|} in
278288
let rec get_violation_line test_output =
279289
match test_output with
280290
| [] -> 0
@@ -295,6 +305,9 @@ let rec gen_sequence
295305
else
296306
is_precond_violation lines
297307
in
308+
let is_bad_compile code =
309+
List.exists (fun l -> Str.string_match is_bad_compile_regex l 0) code
310+
in
298311
let drop n l =
299312
(* ripped from OCaml 5.3 *)
300313
let rec aux i = function
@@ -304,32 +317,45 @@ let rec gen_sequence
304317
if n < 0 then invalid_arg "List.drop";
305318
aux 0 l
306319
in
307-
let violation_line_num = get_violation_line output in
308-
if
309-
is_precond_violation
310-
(drop (List.length src_code - violation_line_num) src_code)
311-
then
312-
gen_test
313-
(pick fs (Random.int (List.fold_left ( + ) 1 (List.map fst fs))))
314-
(retries_left - 1)
315-
else
320+
if is_bad_compile output then
316321
( ctx',
317322
prev,
318-
Either.Left
323+
`CompileFailure
319324
(string
320325
(Printf.sprintf
321-
"/* violation of post-condition at line number %d in %s \
322-
detected on this call: */"
323-
violation_line_num
326+
"/* Failed to compile while seq-testing %s */"
324327
(filename_base ^ ".c"))
325328
^^ hardline
326329
^^ curr_test
327330
^^ hardline
328-
^^ string "return 123;") )))
331+
^^ string "return 123;") )
332+
else (
333+
let violation_line_num = get_violation_line output in
334+
if
335+
is_precond_violation
336+
(drop (List.length src_code - violation_line_num) src_code)
337+
then
338+
gen_test
339+
(pick fs (Random.int (List.fold_left ( + ) 1 (List.map fst fs))))
340+
(retries_left - 1)
341+
else
342+
( ctx',
343+
prev,
344+
`PostConditionViolation
345+
(string
346+
(Printf.sprintf
347+
"/* violation of post-condition at line number %d in %s \
348+
detected on this call: */"
349+
violation_line_num
350+
(filename_base ^ ".c"))
351+
^^ hardline
352+
^^ curr_test
353+
^^ hardline
354+
^^ string "return 123;") ))))
329355
in
330356
(match List.length fs with
331357
| 0 ->
332-
Right
358+
`Success
333359
( seq_so_far ^^ string "/* unable to generate call at this point */",
334360
{ stats with failures = stats.failures + 1 } )
335361
| _ ->
@@ -338,7 +364,7 @@ let rec gen_sequence
338364
(pick fs (Random.int (List.fold_left ( + ) 1 (List.map fst fs))))
339365
max_retries
340366
with
341-
| ctx', prev, Either.Right (name, test) ->
367+
| ctx', prev, `Success (name, test) ->
342368
let distrib =
343369
match List.assoc_opt String.equal name stats.distrib with
344370
| None -> (name, 1) :: stats.distrib
@@ -370,21 +396,22 @@ let rec gen_sequence
370396
filename_base
371397
src_code
372398
fun_decls
373-
| _, prev, Either.Left err ->
374-
if is_empty err then
375-
gen_sequence
376-
funcs
377-
(n - 1)
378-
{ stats with skipped = stats.skipped + 1 }
379-
ctx
380-
prev
381-
seq_so_far
382-
output_dir
383-
filename_base
384-
src_code
385-
fun_decls
386-
else
387-
Either.Left (seq_so_far ^^ err, { stats with failures = 1 })))
399+
| _, prev, `OutOfTries () ->
400+
gen_sequence
401+
funcs
402+
(n - 1)
403+
{ stats with skipped = stats.skipped + 1 }
404+
ctx
405+
prev
406+
seq_so_far
407+
output_dir
408+
filename_base
409+
src_code
410+
fun_decls
411+
| _, _, `PostConditionViolation err ->
412+
`PostConditionViolation (seq_so_far ^^ err, { stats with failures = 1 })
413+
| _, _, `CompileFailure err ->
414+
`CompileFailure (seq_so_far ^^ err, { stats with failures = 1 })))
388415

389416

390417
let compile_sequence
@@ -395,7 +422,10 @@ let compile_sequence
395422
(filename_base : string)
396423
(src_code : string list)
397424
(fun_decls : Pp.document)
398-
: (Pp.document * test_stats, Pp.document * test_stats) Either.either
425+
: [ `PostConditionViolation of Pp.document * test_stats
426+
| `Success of Pp.document * test_stats
427+
| `CompileFailure of Pp.document * test_stats
428+
]
399429
=
400430
let fuel = num_samples in
401431
let declarations : A.sigma_declaration list =
@@ -472,7 +502,7 @@ let generate
472502
in
473503
let exit_code, seq, output_msg =
474504
match compiled_seq with
475-
| Left (seq, stats) ->
505+
| `PostConditionViolation (seq, stats) ->
476506
( 123,
477507
seq,
478508
Printf.sprintf
@@ -483,7 +513,14 @@ let generate
483513
stats.successes
484514
output_dir
485515
test_file )
486-
| Right (seq, stats) ->
516+
| `CompileFailure (seq, _) ->
517+
( 123,
518+
seq,
519+
Printf.sprintf
520+
"Failed to compile a generated test file.\nSee %s/%s for details"
521+
output_dir
522+
test_file )
523+
| `Success (seq, stats) ->
487524
let num_tests = List.fold_left (fun acc (_, num) -> acc + num) 0 stats.distrib in
488525
let distrib_to_str =
489526
if List.length stats.distrib = 0 then

0 commit comments

Comments
 (0)