Skip to content

Commit a79065a

Browse files
committed
Make region analysis use escape information.
1 parent 9ea11c7 commit a79065a

2 files changed

Lines changed: 29 additions & 30 deletions

File tree

src/analyses/region.ml

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,20 @@ struct
2020
include StdV
2121
end
2222

23-
let regions exp part st : Lval.CilLval.t list =
24-
match st with
23+
let regions ctx exp part : Lval.CilLval.t list =
24+
match ctx.local with
2525
| `Lifted reg ->
26-
let ev = Reg.eval_exp exp in
26+
let ask = Analyses.ask_of_ctx ctx in
27+
let ev = Reg.eval_exp ask exp in
2728
let to_exp (v,f) = (v,Lval.Fields.to_offs' f) in
28-
List.map to_exp (Reg.related_globals ev (part,reg))
29+
List.map to_exp (Reg.related_globals ask ev (part,reg))
2930
| `Top -> Messages.info ~category:Unsound "Region state is broken :("; []
3031
| `Bot -> []
3132

32-
let is_bullet exp part st : bool =
33-
match st with
33+
let is_bullet ctx exp part : bool =
34+
match ctx.local with
3435
| `Lifted reg ->
35-
begin match Reg.eval_exp exp with
36+
begin match Reg.eval_exp (Analyses.ask_of_ctx ctx) exp with
3637
| Some (_,v,_) -> (try RegionDomain.RS.is_single_bullet (RegMap.find v reg) with Not_found -> false)
3738
| _ -> false
3839
end
@@ -41,18 +42,18 @@ struct
4142

4243
let get_region ctx e =
4344
let regpart = ctx.global () in
44-
if is_bullet e regpart ctx.local then
45+
if is_bullet ctx e regpart then
4546
None
4647
else
47-
Some (regions e regpart ctx.local)
48+
Some (regions ctx e regpart)
4849

4950
(* queries *)
5051
let query ctx (type a) (q: a Queries.t): a Queries.result =
5152
match q with
5253
| Queries.Regions e ->
5354
let regpart = ctx.global () in
54-
if is_bullet e regpart ctx.local then Queries.Result.bot q (* TODO: remove bot *) else
55-
let ls = List.fold_right Queries.LS.add (regions e regpart ctx.local) (Queries.LS.empty ()) in
55+
if is_bullet ctx e regpart then Queries.Result.bot q (* TODO: remove bot *) else
56+
let ls = List.fold_right Queries.LS.add (regions ctx e regpart) (Queries.LS.empty ()) in
5657
ls
5758
| _ -> Queries.Result.top q
5859

@@ -93,7 +94,7 @@ struct
9394
match ctx.local with
9495
| `Lifted reg ->
9596
let old_regpart = ctx.global () in
96-
let regpart, reg = Reg.assign lval rval (old_regpart, reg) in
97+
let regpart, reg = Reg.assign (Analyses.ask_of_ctx ctx) lval rval (old_regpart, reg) in
9798
if not (RegPart.leq regpart old_regpart) then
9899
ctx.sideg () regpart;
99100
`Lifted reg
@@ -113,7 +114,7 @@ struct
113114
let regpart, reg = match exp with
114115
| Some exp ->
115116
let module BS = (val Base.get_main ()) in
116-
Reg.assign (BS.return_lval ()) exp (old_regpart, reg)
117+
Reg.assign (Analyses.ask_of_ctx ctx) (BS.return_lval ()) exp (old_regpart, reg)
117118
| None -> (old_regpart, reg)
118119
in
119120
let regpart, reg = Reg.kill_vars locals (Reg.remove_vars locals (regpart, reg)) in
@@ -131,7 +132,7 @@ struct
131132
in
132133
match ctx.local with
133134
| `Lifted reg ->
134-
let f x r reg = Reg.assign (var x) r reg in
135+
let f x r reg = Reg.assign (Analyses.ask_of_ctx ctx) (var x) r reg in
135136
let old_regpart = ctx.global () in
136137
let regpart, reg = fold_right2 f fundec.sformals args (old_regpart,reg) in
137138
if not (RegPart.leq regpart old_regpart) then
@@ -146,7 +147,7 @@ struct
146147
let module BS = (val Base.get_main ()) in
147148
let regpart, reg = match lval with
148149
| None -> (old_regpart, reg)
149-
| Some lval -> Reg.assign lval (AddrOf (BS.return_lval ())) (old_regpart, reg)
150+
| Some lval -> Reg.assign (Analyses.ask_of_ctx ctx) lval (AddrOf (BS.return_lval ())) (old_regpart, reg)
150151
in
151152
let regpart, reg = Reg.remove_vars [BS.return_varinfo ()] (regpart, reg) in
152153
if not (RegPart.leq regpart old_regpart) then
@@ -163,7 +164,7 @@ struct
163164
| `Lifted reg, Some lv ->
164165
let old_regpart = ctx.global () in
165166
(* TODO: should realloc use arg region if failed/in-place? *)
166-
let regpart, reg = Reg.assign_bullet lv (old_regpart, reg) in
167+
let regpart, reg = Reg.assign_bullet (Analyses.ask_of_ctx ctx) lv (old_regpart, reg) in
167168
if not (RegPart.leq regpart old_regpart) then
168169
ctx.sideg () regpart;
169170
`Lifted reg
@@ -186,7 +187,7 @@ struct
186187
let fd = Cilfacade.find_varinfo_fundec f in
187188
match args, fd.sformals with
188189
| [exp], [param] ->
189-
let reg = Reg.assign (var param) exp (ctx.global (), RegMap.bot ()) in
190+
let reg = Reg.assign (Analyses.ask_of_ctx ctx) (var param) exp (ctx.global (), RegMap.bot ()) in
190191
[`Lifted (snd reg)]
191192
| _ -> [`Lifted (RegMap.bot ())]
192193
let threadspawn ctx lval f args fctx = ctx.local

src/cdomains/regionDomain.ml

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,6 @@ struct
116116

117117
let closure p m = RegMap.map (RegPart.closure p) m
118118

119-
let is_global (v,fd) = v.vglob
120-
121119
let remove v (p,m) = p, RegMap.remove (v,[]) m
122120
let remove_vars (vs: varinfo list) (cp:t): t =
123121
List.fold_right remove vs cp
@@ -140,7 +138,7 @@ struct
140138
| _ -> kill x st
141139

142140
type eval_t = (bool * elt * F.t) option
143-
let eval_exp exp: eval_t =
141+
let eval_exp (ask: Queries.ask) exp: eval_t =
144142
let offsornot offs = if (get_bool "exp.region-offsets") then F.listify offs else [] in
145143
let rec do_offs deref def = function
146144
| Field (fd, offs) -> begin
@@ -170,7 +168,7 @@ struct
170168
| (Var x, NoOffset) when Goblintutil.is_blessed x.vtype <> None ->
171169
begin match Goblintutil.is_blessed x.vtype with
172170
| Some v -> Some (deref, (v,[]), [])
173-
| _ when x.vglob -> Some (deref, (x, []), [])
171+
| _ when BaseUtil.is_global ask x -> Some (deref, (x, []), [])
174172
| _ -> None
175173
end
176174
| (Var x, offs) -> do_offs deref (Some (deref, (x, offsornot offs), [])) offs
@@ -197,11 +195,11 @@ struct
197195
let p = RegPart.add s p in
198196
p, closure p m
199197

200-
let assign (lval: lval) (rval: exp) (st: t): t =
198+
let assign (ask: Queries.ask) (lval: lval) (rval: exp) (st: t): t =
201199
(* let _ = printf "%a = %a\n" (printLval plainCilPrinter) lval (printExp plainCilPrinter) rval in *)
202200
let t = Cilfacade.typeOf rval in
203201
if isPointerType t then begin (* TODO: this currently allows function pointers, e.g. in iowarrior, but should it? *)
204-
match eval_exp (Lval lval), eval_exp rval with
202+
match eval_exp ask (Lval lval), eval_exp ask rval with
205203
(* TODO: should offs_x matter? *)
206204
| Some (deref_x, x,offs_x), Some (deref_y,y,offs_y) ->
207205
if VF.equal x y then st else
@@ -211,7 +209,7 @@ struct
211209
| `Right () -> `Right ()
212210
)
213211
in
214-
match is_global x, deref_x, is_global y with
212+
match BaseUtil.is_global ask (fst x), deref_x, BaseUtil.is_global ask (fst y) with
215213
| false, false, true ->
216214
p, RegMap.add x (append_offs_y (RegPart.closure p (RS.single_vf y))) m
217215
| false, false, false ->
@@ -232,29 +230,29 @@ struct
232230
| Var x, NoOffset -> update x rval st
233231
| _ -> st
234232
end else
235-
match eval_exp (Lval lval) with
233+
match eval_exp ask (Lval lval) with
236234
| Some (false, (x,_),_) -> remove x st
237235
| _ -> st
238236

239-
let assign_bullet lval (p,m:t):t =
240-
match eval_exp (Lval lval) with
237+
let assign_bullet ask lval (p,m:t):t =
238+
match eval_exp ask (Lval lval) with
241239
| Some (_,x,_) -> p, RegMap.add x RS.single_bullet m
242240
| _ -> p,m
243241

244-
let related_globals (deref_vfd: eval_t) (p,m: t): elt list =
242+
let related_globals (ask: Queries.ask) (deref_vfd: eval_t) (p,m: t): elt list =
245243
let add_o o2 (v,o) = (v,o@o2) in
246244
match deref_vfd with
247245
| Some (true, vfd, os) ->
248246
let vfd_class =
249-
if is_global vfd then
247+
if BaseUtil.is_global ask (fst vfd) then
250248
RegPart.find_class (VFB.of_vf vfd) p
251249
else
252250
RegMap.find vfd m
253251
in
254252
(* Messages.warn ~msg:("ok? "^sprint 80 (V.pretty () (fst vfd)++F.pretty () (snd vfd))) (); *)
255253
List.map (add_o os) (RS.to_vf_list vfd_class)
256254
| Some (false, vfd, os) ->
257-
if is_global vfd then [vfd] else []
255+
if BaseUtil.is_global ask (fst vfd) then [vfd] else []
258256
| None -> Messages.info ~category:Unsound "Access to unknown address could be global"; []
259257
end
260258

0 commit comments

Comments
 (0)