Skip to content

Commit 4938b0d

Browse files
Copy over #1729
1 parent b41d9f7 commit 4938b0d

6 files changed

Lines changed: 70 additions & 0 deletions

File tree

src/config/options.schema.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2546,6 +2546,12 @@
25462546
"type": "boolean",
25472547
"default": false
25482548
},
2549+
"eliminate-cyclic-dead": {
2550+
"title": "solvers.td3.narrow-sides.eliminate-cyclic-dead",
2551+
"description": "Collect recursive garbage.",
2552+
"type": "boolean",
2553+
"default": false
2554+
},
25492555
"apinis": {
25502556
"title": "solvers.td3.narrow-sides.apinis",
25512557
"description": "apply warrowing to globals instead of contributions; Enabling this option is incompatible with stable and immediate-growth and ignores conservative-widen and narrow-gas.",

src/constraint/constrSys.ml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ sig
5151
(** Compute incremental constraint system change from old solution. *)
5252

5353
val postmortem: v -> v list
54+
val must_same_context: v -> v -> bool
5455
end
5556

5657
(** Any system of side-effecting equations over lattices. *)
@@ -68,6 +69,7 @@ sig
6869
val iter_vars: (LVar.t -> D.t) -> (GVar.t -> G.t) -> VarQuery.t -> LVar.t VarQuery.f -> GVar.t VarQuery.f -> unit
6970
val sys_change: (LVar.t -> D.t) -> (GVar.t -> G.t) -> [`L of LVar.t | `G of GVar.t] sys_change_info
7071
val postmortem: LVar.t -> LVar.t list
72+
val must_same_context: LVar.t -> LVar.t -> bool
7173
end
7274

7375
(** A solver is something that can translate a system into a solution (hash-table).
@@ -218,6 +220,11 @@ struct
218220
match leaf with
219221
| `L g -> List.map (fun x -> `L x) @@ S.postmortem g
220222
| _ -> []
223+
224+
let must_same_context x y =
225+
match x, y with
226+
| `L x, `L y -> S.must_same_context x y
227+
| _ -> false
221228
end
222229

223230
(** Splits a [EqConstrSys] solution into a [GlobConstrSys] solution with given [Hashtbl.S] for the [EqConstrSys]. *)

src/framework/constraints.ml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -554,6 +554,8 @@ struct
554554
match leaf with
555555
| FunctionEntry fd, c -> [(Function fd, c)]
556556
| _ -> []
557+
558+
let must_same_context (_,cx) (_,cy) = S.C.equal cx cy
557559
end
558560

559561

src/solver/td3.ml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,7 @@ module Base =
286286
let narrow_sides_gas_default = GobConfig.get_int "solvers.td3.narrow-sides.narrow-gas" in
287287
let narrow_sides_gas_default = if narrow_sides_gas_default < 0 then None else Some (narrow_sides_gas_default, D_Widen) in
288288
let narrow_sides_eliminate_dead = GobConfig.get_bool "solvers.td3.narrow-sides.eliminate-dead" in
289+
let narrow_sides_eliminate_cyclic_dead = GobConfig.get_bool "solvers.td3.narrow-sides.eliminate-cyclic-dead" in
289290
let apinis = GobConfig.get_bool "solvers.td3.narrow-sides.apinis" in
290291
let narrow_sides_stats = GobConfig.get_bool "solvers.td3.narrow-sides.stats" in
291292

@@ -481,6 +482,24 @@ module Base =
481482
| None -> S.Dom.bot () in
482483
let orphaned = HM.find_default orphan_side_effects y (S.Dom.bot()) in
483484
S.Dom.join combined orphaned
485+
and combined_side_elim_dead narrow_gas x y =
486+
if HM.mem orphan_side_effects y then
487+
(* If there is a start value, then we cannot get this back to bot anyway *)
488+
combined_side y
489+
else
490+
let contribs = Option.get @@ HM.find_option data.divided_side_effects y in
491+
let find_fundec v = Node.find_fundec (S.Var.node y) in
492+
let y_fundec = find_fundec y in
493+
let has_outside_contrib = HM.exists (fun k _ -> (not @@ S.Var.equal k x) && ((not @@ CilType.Fundec.equal y_fundec (find_fundec k)) || (not @@ S.must_same_context y k))) contribs in
494+
if has_outside_contrib then
495+
(* If there is a contribution from outside the fundec, we cannot eliminate it *)
496+
combined_side y
497+
else
498+
(* We can eliminate the contribution *)
499+
if narrow_gas = None then
500+
(HM.clear contribs; S.Dom.bot ())
501+
else
502+
(HM.map_inplace (fun _ (value, gas) -> (S.Dom.bot(), gas)) contribs; S.Dom.bot ())
484503
and side_acc acc changed x y d =
485504
let new_acc = match HM.find_option acc y with
486505
| Some acc -> if not @@ S.Dom.leq d acc then Some (S.Dom.join acc d) else None
@@ -623,6 +642,8 @@ module Base =
623642
let y_newval = if S.Dom.leq old_side new_side then
624643
(* If new side is strictly greater than the old one, the value of y can only increase. *)
625644
S.Dom.join y_oldval new_side
645+
else if S.Dom.is_bot new_side && narrow_sides_eliminate_cyclic_dead then
646+
combined_side_elim_dead narrow_gas x y
626647
else
627648
combined_side y in
628649
if not (S.Dom.equal y_newval y_oldval) then (
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// PARAM: --enable solvers.td3.narrow-sides.enabled --enable ana.int.interval --enable solvers.td3.narrow-sides.eliminate-dead --enable solvers.td3.narrow-sides.eliminate-cyclic-dead --enable ana.base.priv.protection.changes-only
2+
#include <pthread.h>
3+
#include <goblint.h>
4+
5+
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
6+
int a = 0;
7+
int b = 0;
8+
9+
void annoy() {
10+
b = 1;
11+
if(b == 1) {
12+
annoy();
13+
}
14+
}
15+
16+
void* f(void *d) {
17+
pthread_mutex_lock(&mutex);
18+
if (a < 10) {
19+
a++;
20+
}
21+
if (a > 20) {
22+
annoy();
23+
}
24+
pthread_mutex_unlock(&mutex);
25+
return NULL;
26+
}
27+
28+
int main(void) {
29+
pthread_t id;
30+
pthread_create(&id, NULL, f, NULL);
31+
__goblint_check(!b); //NORACE
32+
return 0;
33+
}

tests/unit/solver/solverTest.ml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ module ConstrSys = struct
4848
let iter_vars _ _ _ _ _ = ()
4949
let sys_change _ _ = {obsolete = []; delete = []; reluctant = []; restart = []}
5050
let postmortem _ = []
51+
let must_same_context _ _ = false
5152
end
5253

5354
module LH = BatHashtbl.Make (ConstrSys.LVar)

0 commit comments

Comments
 (0)