Skip to content

Commit 0287995

Browse files
committed
Abstract Invariant.t
1 parent 85b4249 commit 0287995

File tree

5 files changed

+46
-40
lines changed

5 files changed

+46
-40
lines changed

src/cdomain/value/domains/invariant.ml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ include Lattice.LiftConf (N) (ExpLat)
3737

3838
let none = top ()
3939
let of_exp = lift
40+
let to_exp = function
41+
| `Lifted x -> Some x
42+
| `Top | `Bot -> None
4043

4144
let ( && ) = meet
4245
let ( || ) = join

src/cdomain/value/domains/invariant.mli

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
(** Invariants for witnesses. *)
22

3-
include Lattice.S with type t = [ `Bot | `Lifted of GoblintCil.exp | `Top ]
3+
include Lattice.S
44

55
val none: t
66
val of_exp: GoblintCil.exp -> t
77

8+
val to_exp: t -> GoblintCil.exp option
9+
810
val (&&): t -> t -> t
911
val (||): t -> t -> t
1012

src/transform/evalAssert.ml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,8 @@ struct
5858
| Some lval -> Lval.(Set.singleton lval)
5959
in
6060
let context = {Invariant.default_context with lvals} in
61-
match (ask ~node loc).f (Queries.Invariant context) with
62-
| `Lifted e ->
61+
match Invariant.to_exp ((ask ~node loc).f (Queries.Invariant context)) with
62+
| Some e ->
6363
let es = WitnessUtil.InvariantExp.process_exp e in
6464
let asserts = List.map (fun e -> cInstr ("%v:assert (%e:exp);") loc [("assert", Fv assert_function); ("exp", Fe e)]) es in
6565
if surroundByAtomic then
@@ -68,7 +68,7 @@ struct
6868
abegin :: (asserts @ [aend])
6969
else
7070
asserts
71-
| _ -> []
71+
| None -> []
7272
in
7373

7474
let instrument_instructions il s =

src/witness/witness.ml

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ open Svcomp
66
open GobConfig
77

88
module M = Messages
9+
module OuterInvariant = Invariant
910

1011
module type WitnessTaskResult = TaskResult with module Arg.Edge = MyARG.InlineEdge
1112

@@ -38,8 +39,8 @@ let write_file filename (module Task:Task) (module TaskResult:WitnessTaskResult)
3839
| MyARG.CFGEdge (Test _) -> true
3940
| _ -> false
4041
end || begin if Invariant.is_invariant_node to_cfgnode then
41-
match to_cfgnode, TaskResult.invariant to_node with
42-
| Statement _, `Lifted _ -> true
42+
match to_cfgnode, OuterInvariant.to_exp (TaskResult.invariant to_node) with
43+
| Statement _, Some _ -> true
4344
| _, _ -> false
4445
else
4546
false
@@ -137,8 +138,8 @@ let write_file filename (module Task:Task) (module TaskResult:WitnessTaskResult)
137138
end;
138139
begin
139140
if Invariant.is_invariant_node cfgnode then
140-
match cfgnode, TaskResult.invariant node with
141-
| Statement _, `Lifted i ->
141+
match cfgnode, OuterInvariant.to_exp (TaskResult.invariant node) with
142+
| Statement _, Some i ->
142143
let i = InvariantCil.exp_replace_original_name i in
143144
[("invariant", CilType.Exp.show i);
144145
("invariant.scope", (Node.find_fundec cfgnode).svar.vname)]

src/witness/yamlWitness.ml

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -293,8 +293,8 @@ struct
293293
Invariant.(acc || R.ask_local_node n ~local (Invariant {Invariant.default_context with lvals})) [@coverage off] (* bisect_ppx cannot handle redefined (||) *)
294294
) (Invariant.bot ()) ns
295295
in
296-
match inv with
297-
| `Lifted inv ->
296+
match Invariant.to_exp inv with
297+
| Some inv ->
298298
let fundec = Node.find_fundec (List.hd ns) in (* TODO: fix location hack *)
299299
let location_function = fundec.svar.vname in
300300
let location = Entry.location ~location:loc ~location_function in
@@ -305,7 +305,7 @@ struct
305305
incr cnt_location_invariant;
306306
entry :: acc
307307
) acc invs
308-
| `Bot | `Top -> (* TODO: 0 for bot (dead code)? *)
308+
| None -> (* TODO: 0 for bot (dead code)? *)
309309
acc
310310
) (Lazy.force location_nodes) entries
311311
)
@@ -323,8 +323,8 @@ struct
323323
Invariant.(acc || R.ask_local_node n ~local (Invariant Invariant.default_context)) [@coverage off] (* bisect_ppx cannot handle redefined (||) *)
324324
) (Invariant.bot ()) ns
325325
in
326-
match inv with
327-
| `Lifted inv ->
326+
match Invariant.to_exp inv with
327+
| Some inv ->
328328
let fundec = Node.find_fundec (List.hd ns) in (* TODO: fix location hack *)
329329
let location_function = fundec.svar.vname in
330330
let location = Entry.location ~location:loc ~location_function in
@@ -335,7 +335,7 @@ struct
335335
incr cnt_loop_invariant;
336336
entry :: acc
337337
) acc invs
338-
| `Bot | `Top -> (* TODO: 0 for bot (dead code)? *)
338+
| None -> (* TODO: 0 for bot (dead code)? *)
339339
acc
340340
)
341341
else
@@ -372,24 +372,24 @@ struct
372372
GHT.fold (fun g v acc ->
373373
match g with
374374
| `Left g -> (* global unknown from analysis Spec *)
375-
begin match R.ask_global (InvariantGlobal (Obj.repr g)), GobConfig.get_string "witness.invariant.flow_insensitive-as" with
376-
| `Lifted inv, "flow_insensitive_invariant" ->
375+
begin match Invariant.to_exp (R.ask_global (InvariantGlobal (Obj.repr g))), GobConfig.get_string "witness.invariant.flow_insensitive-as" with
376+
| Some inv, "flow_insensitive_invariant" ->
377377
let invs = WitnessUtil.InvariantExp.process_exp inv in
378378
List.fold_left (fun acc inv ->
379379
let invariant = Entry.invariant (CilType.Exp.show inv) in
380380
let entry = Entry.flow_insensitive_invariant ~task ~invariant in
381381
incr cnt_flow_insensitive_invariant;
382382
entry :: acc
383383
) acc invs
384-
| `Lifted inv, "location_invariant" ->
384+
| Some inv, "location_invariant" ->
385385
fold_flow_insensitive_as_location ~inv (fun ~location ~inv acc ->
386386
let invariant = Entry.invariant (CilType.Exp.show inv) in
387387
let entry = Entry.location_invariant ~task ~location ~invariant in
388388
incr cnt_location_invariant;
389389
entry :: acc
390390
) acc
391-
| `Lifted _, _
392-
| `Bot, _ | `Top, _ -> (* global bot might only be possible for alloc variables, if at all, so emit nothing *)
391+
| Some _, _
392+
| None, _ -> (* global bot might only be possible for alloc variables, if at all, so emit nothing *)
393393
acc
394394
end
395395
| `Right _ -> (* global unknown for FromSpec contexts *)
@@ -450,11 +450,11 @@ struct
450450
let fc_map : con_inv list FCMap.t = FCMap.create 103 in
451451
FMap.iter (fun f con_invs ->
452452
List.iter (fun current_c ->
453-
begin match current_c.invariant with
454-
| `Lifted c_inv ->
453+
begin match Invariant.to_exp current_c.invariant with
454+
| Some c_inv ->
455455
(* Collect all start states that may satisfy the invariant of current_c *)
456456
List.iter (fun c ->
457-
let x = R.ask_local (c.node, c.context) ~local:c.state (Queries.EvalInt c_inv) in
457+
let x = R.ask_local (c.node, c.context) ~local:c.state (Queries.EvalInt c_inv) in (* TODO: illegal query *)
458458
if Queries.ID.is_bot x || Queries.ID.is_bot_ikind x then (* dead code *)
459459
failwith "Bottom not expected when querying context state" (* Maybe this is reachable, failwith for now so we see when this happens *)
460460
else if Queries.ID.to_bool x = Some false then () (* Nothing to do, the c does definitely not satisfy the predicate of current_c *)
@@ -463,7 +463,7 @@ struct
463463
FCMap.modify_def [] (f, current_c.context) (fun cs -> c::cs) fc_map;
464464
end
465465
) con_invs;
466-
| `Bot | `Top ->
466+
| None ->
467467
(* If the context invariant is None, we will not generate a precondition invariant. Nothing to do here. *)
468468
()
469469
end
@@ -484,25 +484,25 @@ struct
484484
let fundec = Node.find_fundec n in
485485
let pre_lvar = (Node.FunctionEntry fundec, c) in
486486
let query = Queries.Invariant Invariant.default_context in
487-
begin match R.ask_local pre_lvar query with
488-
| `Lifted c_inv ->
487+
begin match Invariant.to_exp (R.ask_local pre_lvar query) with
488+
| Some c_inv ->
489489
(* Find unknowns for which the preceding start state satisfies the precondtion *)
490490
let xs = find_matching_states lvar in
491491

492492
(* Generate invariants. Give up in case one invariant could not be generated. *)
493493
let invs = GobList.fold_while_some (fun acc local ->
494494
let lvals = local_lvals n local in
495-
match R.ask_local_node n ~local (Invariant {Invariant.default_context with lvals}) with
496-
| `Lifted c -> Some ((`Lifted c)::acc)
497-
| `Bot | `Top -> None
495+
match Invariant.to_exp (R.ask_local_node n ~local (Invariant {Invariant.default_context with lvals})) with
496+
| Some c -> Some ((Invariant.of_exp c)::acc)
497+
| None -> None
498498
) [] xs
499499
in
500500
begin match invs with
501501
| None
502502
| Some [] -> acc
503503
| Some (x::xs) ->
504-
begin match List.fold_left (fun acc inv -> Invariant.(acc || inv) [@coverage off]) x xs with (* bisect_ppx cannot handle redefined (||) *)
505-
| `Lifted inv ->
504+
begin match Invariant.to_exp (List.fold_left (fun acc inv -> Invariant.(acc || inv) [@coverage off]) x xs) with (* bisect_ppx cannot handle redefined (||) *)
505+
| Some inv ->
506506
let invs = WitnessUtil.InvariantExp.process_exp inv in
507507
let c_inv = InvariantCil.exp_replace_original_name c_inv in (* cannot be split *)
508508
List.fold_left (fun acc inv ->
@@ -513,7 +513,7 @@ struct
513513
let entry = Entry.precondition_loop_invariant ~task ~location ~precondition ~invariant in
514514
entry :: acc
515515
) acc invs
516-
| `Bot | `Top -> acc
516+
| None -> acc
517517
end
518518
end
519519
| _ -> (* Do not construct precondition invariants if we cannot express precondition *)
@@ -542,8 +542,8 @@ struct
542542
Invariant.(acc || R.ask_local_node n ~local (Invariant {Invariant.default_context with lvals})) [@coverage off] (* bisect_ppx cannot handle redefined (||) *)
543543
) (Invariant.bot ()) ns
544544
in
545-
match inv with
546-
| `Lifted inv ->
545+
match Invariant.to_exp inv with
546+
| Some inv ->
547547
let fundec = Node.find_fundec (List.hd ns) in (* TODO: fix location hack *)
548548
let location_function = fundec.svar.vname in
549549
let location = Entry.location ~location:loc ~location_function in
@@ -554,7 +554,7 @@ struct
554554
incr cnt_location_invariant;
555555
invariant :: acc
556556
) acc invs
557-
| `Bot | `Top -> (* TODO: 0 for bot (dead code)? *)
557+
| None -> (* TODO: 0 for bot (dead code)? *)
558558
acc
559559
) (Lazy.force location_nodes) invariants
560560
)
@@ -572,8 +572,8 @@ struct
572572
Invariant.(acc || R.ask_local_node n ~local (Invariant Invariant.default_context)) [@coverage off] (* bisect_ppx cannot handle redefined (||) *)
573573
) (Invariant.bot ()) ns
574574
in
575-
match inv with
576-
| `Lifted inv ->
575+
match Invariant.to_exp inv with
576+
| Some inv ->
577577
let fundec = Node.find_fundec (List.hd ns) in (* TODO: fix location hack *)
578578
let location_function = fundec.svar.vname in
579579
let location = Entry.location ~location:loc ~location_function in
@@ -584,7 +584,7 @@ struct
584584
incr cnt_loop_invariant;
585585
invariant :: acc
586586
) acc invs
587-
| `Bot | `Top -> (* TODO: 0 for bot (dead code)? *)
587+
| None -> (* TODO: 0 for bot (dead code)? *)
588588
acc
589589
)
590590
else
@@ -601,15 +601,15 @@ struct
601601
GHT.fold (fun g v acc ->
602602
match g with
603603
| `Left g -> (* global unknown from analysis Spec *)
604-
begin match R.ask_global (InvariantGlobal (Obj.repr g)) with
605-
| `Lifted inv ->
604+
begin match Invariant.to_exp (R.ask_global (InvariantGlobal (Obj.repr g))) with
605+
| Some inv ->
606606
fold_flow_insensitive_as_location ~inv (fun ~location ~inv acc ->
607607
let invariant = CilType.Exp.show inv in
608608
let invariant = Entry.location_invariant' ~location ~invariant in
609609
incr cnt_location_invariant;
610610
invariant :: acc
611611
) acc
612-
| `Bot | `Top -> (* global bot might only be possible for alloc variables, if at all, so emit nothing *)
612+
| None -> (* global bot might only be possible for alloc variables, if at all, so emit nothing *)
613613
acc
614614
end
615615
| `Right _ -> (* global unknown for FromSpec contexts *)

0 commit comments

Comments
 (0)