55 * 
66 * Author: Tomas Dacik (idacik@fit.vut.cz), 2024 *)  
77
8- 
8+ open   Cil_types 
99open  Cil_datatype 
1010
1111module  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.
43-       |>  List. map Base. of_varinfo 
72+     let  aliases  = 
73+       LvalStructEq .Set.
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.
93+       LvalStructEq .Set.
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
82125end 
0 commit comments