Skip to content

Commit 96fb1bd

Browse files
committed
Hotfix: thread arguments in Alias backend
1 parent c135a4b commit 96fb1bd

File tree

7 files changed

+74
-24
lines changed

7 files changed

+74
-24
lines changed

common/src/concurrency_model/ConcurrencyModel.ml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,5 +119,8 @@ let classify_stmt stmt = match stmt.skind with
119119
| Return _ -> Return
120120
| _ -> Other
121121

122+
let get_thread_arg stmt = match classify_stmt stmt with
123+
| Thread_create (_, _, arg) -> arg
124+
122125
let is_atomic_fn var =
123126
AtomicFunctions.exists (fun a -> Str.string_match (Str.regexp a) var.vname 0)

common/src/concurrency_model/ConcurrencyModel.mli

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,6 @@ type stmt =
3939

4040
val classify_stmt : Stmt.t -> stmt
4141

42+
val get_thread_arg : Stmt.t -> Exp.t
43+
4244
val is_atomic_fn : Varinfo.t -> bool

common/src/thread_analysis/ThreadAnalysis.ml

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -224,8 +224,13 @@ module Make (ValueAnalysis : VALUE_ANALYSIS) = struct
224224
(Stmt.Set.cardinal stmts)
225225
Core0.pp_globals (fst initial');
226226

227+
let args =
228+
Stmt.Set.elements stmts
229+
|> List.map ConcurrencyModel.get_thread_arg
230+
|> Exp.Set.of_list
231+
in
227232
ValueAnalysis.set_active_thread parent;
228-
ValueAnalysis.update_thread child initial';
233+
ValueAnalysis.update_thread child args initial';
229234
initial'
230235

231236
module WTO = Graph.WeakTopological.Make(ThreadGraph)
@@ -313,8 +318,6 @@ module Make (ValueAnalysis : VALUE_ANALYSIS) = struct
313318
graph := res.thread_graph;
314319
let res = compute_fixpoint res in
315320

316-
ValueAnalysis.set_escaped_bases res.escaped_bases;
317-
318321
Logger.feedback "%a" ThreadAnalysis0.pp_short res;
319322
Logger.debug ~level:3 "Thread analysis: %a" pp res;
320323
Logger.debug ~level:3 "Active threads: %s" (ThreadAnalysis0.show_stmt_summaries res);

common/src/value_analysis/AliasF.ml

Lines changed: 53 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,34 @@
55
*
66
* Author: Tomas Dacik (idacik@fit.vut.cz), 2024 *)
77

8-
8+
open Cil_types
99
open Cil_datatype
1010

1111
module Self = struct
1212

1313
let name = "alias"
1414

15+
module Logger = Core0.Logger(struct let dkey = "alias" end)
16+
1517
(** Include references to active thread and initial states. *)
1618
include ValueAnalysis_Base.Make ()
1719

20+
let compute () =
21+
Alias.Analysis.compute ();
22+
let print_kf_alias_sets kf =
23+
if Kernel_function.has_definition kf then
24+
let alias_sets = Alias.API.Function.alias_sets_lvals ~kf in
25+
List.iter (fun lset ->
26+
Logger.debug "%a: %a" Kernel_function.pretty kf Alias.API.LSet.pretty lset
27+
) alias_sets
28+
else ()
29+
in
30+
Globals.Functions.iter print_kf_alias_sets
31+
1832
let init main =
1933
init main;
2034
Syntactic.init main;
21-
Alias.Analysis.compute ()
35+
compute ()
2236

2337
let set_active_thread thread =
2438
set_active_thread thread;
@@ -36,23 +50,47 @@ module Self = struct
3650
let eval_expr = Syntactic.Self.eval_expr
3751
let stmt_state = Syntactic.Self.stmt_state
3852

53+
let lval_to_base lval = match lval with
54+
| Var var, NoOffset -> Base.of_varinfo var
55+
| _, _ -> failwith "TODO"
56+
57+
(** TODO: this is a quick fix for issue with thread arguments *)
58+
let process_thread_arg thread base =
59+
if BaseUtils.is_thread_arg thread base then base
60+
else try
61+
Thread.Map.find thread !thread_args
62+
|> Exp.Set.elements
63+
|> List.map Cil.extract_varinfos_from_exp
64+
|> List.map Varinfo.Set.elements
65+
|> List.concat
66+
|> List.hd
67+
|> Base.of_varinfo
68+
with _ -> base
69+
3970
let choose_representant keep_local lval base varset =
4071
let thread = get_active_thread () in
41-
try
42-
Varinfo.Set.elements varset
43-
|> List.map Base.of_varinfo
72+
let aliases =
73+
LvalStructEq.Set.elements varset
74+
|> List.map lval_to_base
4475
|> List.filter (fun base -> keep_local || BaseUtils.keep_for_racer thread base)
45-
|> List.hd
46-
with _ -> base
76+
in
77+
let representant =
78+
if List.is_empty aliases then base
79+
else List.hd aliases
80+
in
81+
process_thread_arg thread representant
82+
4783

4884
let normalise_address keep_local stmt (base, offset) =
4985
let lval = Cil.var @@ Base.to_varinfo base in
50-
let aliases = Alias.API.Statement.points_to_vars ~stmt lval in
86+
let kf = Kernel_function.find_englobing_kf stmt in
87+
let state = Option.get @@ Alias.API.get_state_before_stmt kf stmt in
88+
let aliases = Alias.API.Abstract_state.find_synonyms lval state in
5189
Core0.debug "Aliases of %a (as lval: %a) at %a: %a"
5290
Base.pretty base
5391
LvalStructEq.pretty lval
5492
Print_utils.pretty_stmt_short stmt
55-
Varinfo.Set.pretty aliases;
93+
LvalStructEq.Set.pretty aliases;
5694
choose_representant keep_local lval base aliases, offset
5795

5896
(** Apply aliases:
@@ -73,10 +111,15 @@ module Self = struct
73111

74112
let memory_accesses ?(local=false) stmt =
75113
let reads, writes = Syntactic.memory_accesses ~local:true stmt in
114+
115+
Format.printf "Memory accesses at %a:\n" Print_utils.pretty_stmt_short stmt;
116+
List.iter (fun m -> Format.printf " - r: %s\n" (MemoryAddress.show m)) reads;
117+
List.iter (fun m -> Format.printf " - w: %s\n" (MemoryAddress.show m)) writes;
118+
76119
(normalise stmt reads, normalise stmt writes)
77120

78121
let expr_reads ?(local=false) stmt expr =
79-
Syntactic.expr_reads ~local stmt expr
122+
Syntactic.expr_reads ~local:true stmt expr
80123
|> normalise stmt
81124

82125
end

common/src/value_analysis/Syntactic.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,13 +132,13 @@ module Self = struct
132132
|> List.filter (fun (base, _) -> (BaseUtils.keep_for_racer thread base))
133133

134134
let memory_accesses ?(local=false) stmt =
135-
try
135+
(*try
136136
Logger.debug "R: %a : %a" Stmt.pretty stmt Locations.Zone.pretty (Inout.stmt_inputs stmt);
137137
Logger.debug "W: %a : %a" Stmt.pretty stmt Locations.Zone.pretty (Inout.stmt_outputs stmt);
138138
let reads = get_accesses local @@ Inout.stmt_inputs stmt in
139139
let writes = get_accesses local @@ Inout.stmt_outputs stmt in
140140
(reads, writes)
141-
with _ ->
141+
with _ ->*)
142142
let reads = compute_reads local stmt in
143143
let writes = compute_writes local stmt in
144144
(reads, writes)

common/src/value_analysis/ValueAnalysis_Base.ml

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,24 @@
22
*
33
* Author: Tomas Dacik (idacik@fit.vut.cz), 2024 *)
44

5+
open Cil_datatype
6+
57
module Make () = struct
68

79
let main_thread : Thread.t option ref = ref None
810
let active_thread : Thread.t option ref = ref None
911
let thread_states = ref (Thread.Map.empty : Thread.InitialState.t Thread.Map.t)
10-
let escaped_bases: Base.t list ref = ref []
12+
let thread_args = ref (Thread.Map.empty : Exp.Set.t Thread.Map.t)
1113

1214
let init main =
1315
let main = Thread.mk main in
1416
main_thread := Some main;
1517
active_thread := Some main;
16-
thread_states := Thread.Map.singleton main Thread.InitialState.bottom
18+
thread_states := Thread.Map.singleton main Thread.InitialState.bottom;
19+
thread_args := Thread.Map.singleton main Exp.Set.empty
1720

18-
let update_thread thread state =
21+
let update_thread thread args state =
22+
thread_args := Thread.Map.add thread args !thread_args;
1923
try
2024
let old = Thread.Map.find thread !thread_states in
2125
let join = Thread.InitialState.join state old in
@@ -34,7 +38,4 @@ module Make () = struct
3438
try Thread.Map.find thread !thread_states
3539
with Not_found -> Thread.InitialState.bottom
3640

37-
let set_escaped_bases bases =
38-
escaped_bases := bases
39-
4041
end

common/src/value_analysis/ValueAnalysis_sig.mli

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,10 @@ module type VALUE_ANALYSIS = sig
4141

4242
include VALUE_ANALYSIS_BASE
4343

44-
val update_thread : Thread.t -> Thread.InitialState.t -> unit
44+
val update_thread : Thread.t -> Exp.Set.t -> Thread.InitialState.t -> unit
4545

4646
val set_active_thread : Thread.t -> unit
4747

4848
val get_active_thread : unit -> Thread.t
4949

50-
val set_escaped_bases : Base.t list -> unit
51-
5250
end

0 commit comments

Comments
 (0)