Skip to content

Commit 79ac8c0

Browse files
authored
Merge pull request #717 from goblint/issue-689
Interactive: fix expression locations
2 parents 71c7e1c + 6bfc853 commit 79ac8c0

File tree

4 files changed

+56
-9
lines changed

4 files changed

+56
-9
lines changed

.semgrep/cil.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ rules:
66
- pattern: Cil.typeOfInit
77
- pattern: Cil.typeOffset
88
- pattern: Cil.get_stmtLoc
9+
- pattern: Cil.get_instrLoc
910
paths:
1011
exclude:
1112
- cilfacade0.ml

src/transform/evalAssert.ml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -106,16 +106,16 @@ module EvalAssert = struct
106106
let rec instrument_instructions = function
107107
| i1 :: ((i2 :: _) as is) ->
108108
(* List contains successor statement, use location of successor for values *)
109-
let loc = get_instrLoc i2 in
109+
let loc = get_instrLoc i2 in (* TODO: why not using Cilfacade.get_instrLoc? *)
110110
i1 :: ((instrument i1 loc) @ instrument_instructions is)
111111
| [i] when unique_succ ->
112112
(* Last statement in list *)
113113
(* Successor of it has only one predecessor, we can query for the value there *)
114-
let loc = get_stmtLoc (List.hd s.succs).skind in
114+
let loc = get_stmtLoc (List.hd s.succs).skind in (* TODO: why not using Cilfacade.get_stmtLoc? *)
115115
i :: (instrument i loc)
116116
| [i] when s.succs <> [] ->
117117
(* Successor has multiple predecessors, results may be imprecise but remain correct *)
118-
let loc = get_stmtLoc (List.hd s.succs).skind in
118+
let loc = get_stmtLoc (List.hd s.succs).skind in (* TODO: why not using Cilfacade.get_stmtLoc? *)
119119
i :: (instrument i loc)
120120
| x -> x
121121
in
@@ -134,7 +134,7 @@ module EvalAssert = struct
134134
match s.preds with
135135
| [p1; p2] when not only_at_locks ->
136136
(* exactly two predecessors -> join point, assert locals if they changed *)
137-
let join_loc = get_stmtLoc s.skind in
137+
let join_loc = get_stmtLoc s.skind in (* TODO: why not using Cilfacade.get_stmtLoc? *)
138138
(* Possible enhancement: It would be nice to only assert locals here that were modified in either branch if trans.assert.full is false *)
139139
let asserts = make_assert join_loc None in
140140
self#queueInstr asserts; ()
@@ -153,7 +153,7 @@ module EvalAssert = struct
153153
let add_asserts block =
154154
if block.bstmts <> [] then
155155
let with_asserts =
156-
let b_loc = get_stmtLoc (List.hd block.bstmts).skind in
156+
let b_loc = get_stmtLoc (List.hd block.bstmts).skind in (* TODO: why not using Cilfacade.get_stmtLoc? *)
157157
let b_assert_instr = asserts b_loc vars in
158158
[cStmt "{ %I:asserts %S:b }" (fun n t -> makeVarinfo true "unknown" (TVoid [])) b_loc [("asserts", FI b_assert_instr); ("b", FS block.bstmts)]]
159159
in

src/util/cilfacade0.ml

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,32 @@ let rec get_labelsLoc = function
1616
else
1717
loc
1818

19-
let get_stmtkindLoc = Cil.get_stmtLoc (* CIL has a confusing name for this function *)
19+
(** Following functions are similar to [Cil] versions, but return expression location instead of entire statement location, where possible. *)
20+
(* Ideally we would have both copies of the functions available, but UpdateCil would have to be adapted per-stmtkind/instr to store and update either one or two locations. *)
2021

21-
let get_stmtLoc stmt =
22+
(** Get expression location for [Cil.instr]. *)
23+
let get_instrLoc = function
24+
| Set (_, _, _loc, eloc) -> eloc
25+
| Call (_, _, _, _loc, eloc) -> eloc
26+
| Asm (_, _, _, _, _, loc) -> loc
27+
| VarDecl (_, loc) -> loc
28+
29+
(** Get expression location for [Cil.stmt]. *)
30+
(* confusingly CIL.get_stmtLoc works on stmtkind instead *)
31+
let rec get_stmtLoc stmt =
2232
match stmt.skind with
23-
(* Cil.get_stmtLoc returns Cil.locUnknown in these cases, so try labels instead *)
33+
(* no stmtkind/instr location in these cases, so try labels instead *)
2434
| Instr []
2535
| Block {bstmts = []; _} ->
2636
get_labelsLoc stmt.labels
27-
| _ -> get_stmtkindLoc stmt.skind
37+
38+
| Instr (hd :: _) -> get_instrLoc hd
39+
| Return (_, loc) -> loc
40+
| Goto (_, loc) -> loc
41+
| ComputedGoto (_, loc) -> loc
42+
| Break loc -> loc
43+
| Continue loc -> loc
44+
| If (_, _, _, _loc, eloc) -> eloc
45+
| Switch (_, _, _, _loc, eloc) -> eloc
46+
| Loop (_, _loc, eloc, _, _) -> eloc
47+
| Block {bstmts = hd :: _; _} -> get_stmtLoc hd
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#include <pthread.h>
2+
#include <stdio.h>
3+
4+
int myglobal;
5+
6+
void *t_fun(void *arg) {
7+
// awkward formatting to check that warning is just on condition expression, not entire if
8+
if // NORACE
9+
(myglobal > 0) { // RACE!
10+
printf("Stupid!");
11+
printf("Stupid!");
12+
printf("Stupid!");
13+
printf("Stupid!");
14+
printf("Stupid!");
15+
printf("Stupid!");
16+
}
17+
return NULL;
18+
}
19+
20+
int main(void) {
21+
pthread_t id;
22+
pthread_create(&id, NULL, t_fun, NULL);
23+
myglobal=myglobal+1; // RACE!
24+
pthread_join (id, NULL);
25+
return 0;
26+
}

0 commit comments

Comments
 (0)