Skip to content

Commit 4632f6a

Browse files
committed
Use LazyEval instead of Lazy in C2PODomain.
1 parent 91ba355 commit 4632f6a

File tree

4 files changed

+212
-147
lines changed

4 files changed

+212
-147
lines changed

src/analyses/c2poAnalysis.ml

Lines changed: 57 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,14 @@ struct
1515
module D = D
1616
module C = D
1717

18+
type marshal = unit
19+
1820
let name () = "c2po"
1921
let startcontext () = D.top ()
2022

23+
let init d = ()
24+
let finalize () = ()
25+
2126
(** Find reachable variables in a function *)
2227
let reachable_from_args ctx args =
2328
let collect_reachable_from_exp acc e =
@@ -32,7 +37,8 @@ struct
3237
(* Returns Some true if we know for sure that it is true,
3338
and Some false if we know for sure that it is false,
3439
and None if we don't know anyhing. *)
35-
let eval_guard ask cc e ik =
40+
let eval_guard ask d e ik =
41+
let cc = d.data in
3642
let open Queries in
3743
let prop_list = T.prop_of_cil ask e true in
3844
match split prop_list with
@@ -71,28 +77,30 @@ struct
7177
let open Queries in
7278
match ctx.local with
7379
| `Bot -> Result.top q
74-
| `Lifted cc ->
80+
| `Lifted d ->
7581
match q with
7682
| EvalInt e ->
7783
let ik = Cilfacade.get_ikind_exp e in
78-
eval_guard (ask_of_man ctx) cc e ik
84+
eval_guard (ask_of_man ctx) d e ik
7985
| Queries.Invariant context ->
8086
let scope = Node.find_fundec ctx.node in
81-
let t = D.remove_vars_not_in_scope scope cc in
82-
let conj = get_conjunction t in
87+
let cc = D.remove_vars_not_in_scope scope d.data in
88+
let conj = get_conjunction_from_data cc in
8389
let ask = ask_of_man ctx in
8490
conj_to_invariant ask conj
8591
| _ ->
8692
Result.top q
8793

8894
(** Assign the right hand side rhs (that is already
8995
converted to a C-2PO term) to the term `lterm`. *)
90-
let assign_term cc ask lterm rhs lval_t =
96+
let assign_term d ask lterm rhs lval_t =
97+
let cc = d.data in
9198
(* ignore assignments to values that are not 64 bits *)
9299
match T.get_element_size_in_bits lval_t, rhs with
93100
(* Indefinite assignment *)
94101
| lval_size, (None, _) ->
95-
D.remove_may_equal_terms ask lval_size lterm cc
102+
let cc = D.remove_may_equal_terms ask lval_size lterm cc in
103+
data_to_t cc
96104
(* Definite assignment *)
97105
| lval_size, (Some rterm, Some roffset) ->
98106
let dummy_var = MayBeEqual.dummy_var lval_t in
@@ -106,8 +114,8 @@ struct
106114
meet_conjs_opt equal_dummy_rterm |>
107115
D.remove_may_equal_terms ask lval_size lterm |>
108116
meet_conjs_opt equal_dummy_lterm |>
109-
D.remove_terms_containing_aux_variable
110-
117+
D.remove_terms_containing_aux_variable |>
118+
data_to_t
111119
| _ -> (* this is impossible *)
112120
C2PODomain.top ()
113121

@@ -131,8 +139,8 @@ struct
131139
match ctx.local with
132140
| `Bot ->
133141
`Bot
134-
| `Lifted cc ->
135-
let cc = assign_lval cc ask lval (T.of_cil ask expr) in
142+
| `Lifted d ->
143+
let cc = assign_lval d ask lval (T.of_cil ask expr) in
136144
let cc = reset_normal_form cc in
137145
let res = `Lifted cc in
138146
if M.tracing then M.trace "c2po-assign" "assign: var: %a; expr: %a; result: %s.\n" d_lval lval d_plainexp expr (D.show res);
@@ -144,13 +152,13 @@ struct
144152
let res =
145153
match ctx.local with
146154
| `Bot -> `Bot
147-
| `Lifted cc ->
155+
| `Lifted d ->
148156
if List.is_empty valid_props then
149-
`Lifted cc
157+
`Lifted d
150158
else
151159
try
152-
let meet = meet_conjs_opt valid_props cc in
153-
let t = reset_normal_form meet in
160+
let meet = meet_conjs_opt valid_props d.data in
161+
let t = data_to_t meet in
154162
`Lifted t
155163
with Unsat ->
156164
`Bot
@@ -167,8 +175,8 @@ struct
167175
match T.of_cil ask expr with
168176
| (Some term, Some offset) ->
169177
let ret_var_eq_term = [Equal (return_var, term, offset)] in
170-
let assign_by_meet = meet_conjs_opt ret_var_eq_term d in
171-
reset_normal_form assign_by_meet
178+
let assign_by_meet = meet_conjs_opt ret_var_eq_term d.data in
179+
data_to_t assign_by_meet
172180
| _ -> d
173181

174182
let return ctx exp_opt f =
@@ -194,25 +202,27 @@ struct
194202
let ask = ask_of_man ctx in
195203
match ctx.local with
196204
| `Bot -> `Bot
197-
| `Lifted cc ->
205+
| `Lifted d ->
198206
let t =
199207
begin match lval_opt with
200208
| None ->
201-
cc
209+
d
202210
| Some lval ->
203211
(* forget information about var,
204212
but ignore assignments to values that are not 64 bits *)
205213
try
206214
let size = T.get_element_size_in_bits (typeOfLval lval) in
207215
let lterm = T.of_lval ask lval in
208-
let cc = D.remove_may_equal_terms ask size lterm cc in
209-
begin match desc.special exprs with
216+
let cc = D.remove_may_equal_terms ask size lterm d.data in
217+
let cc = begin match desc.special exprs with
210218
| Malloc _
211219
| Calloc _
212220
| Alloca _ ->
213221
add_block_diseqs cc lterm
214222
| _ -> cc
215223
end
224+
in
225+
data_to_t cc
216226
with T.UnsupportedCilExpression _ ->
217227
C2PODomain.top ()
218228
end
@@ -224,7 +234,7 @@ struct
224234
else
225235
branch ctx exp true
226236
| _ ->
227-
`Lifted (reset_normal_form t)
237+
`Lifted t
228238

229239
(** First all local variables of the function are duplicated,
230240
then we remember the value of each local variable at the beginning of the function by using the analysis startState.
@@ -233,13 +243,14 @@ struct
233243
(* add duplicated variables, and set them equal to the original variables *)
234244
match ctx.local with
235245
| `Bot -> [`Bot, `Bot]
236-
| `Lifted cc ->
246+
| `Lifted d ->
237247
let ghost_equality v =
238248
Equal (T.term_of_varinfo (DuplicVar v), T.term_of_varinfo (NormalVar v), Z.zero)
239249
in
240250
let ghost_equalities_for_params = List.map ghost_equality f.sformals in
241251
let equalities_to_add = T.filter_valid_pointers ghost_equalities_for_params in
242-
let state_with_ghosts = meet_conjs_opt equalities_to_add cc in
252+
let state_with_ghosts = meet_conjs_opt equalities_to_add d.data in
253+
let state_with_ghosts = data_to_t state_with_ghosts in
243254
if M.tracing then begin
244255
let dummy_lval = Var (Var.dummy_varinfo (TVoid [])), NoOffset in
245256
let lval = BatOption.default dummy_lval var_opt in
@@ -250,7 +261,8 @@ struct
250261
let reachable = f.sformals @ f.slocals @ reachable_from_args ctx args in
251262
Var.from_varinfo reachable f.sformals
252263
in
253-
let new_state = D.remove_terms_not_containing_variables reachable_variables state_with_ghosts in
264+
let new_state = D.remove_terms_not_containing_variables reachable_variables state_with_ghosts.data in
265+
let new_state = data_to_t new_state in
254266
if M.tracing then M.trace "c2po-function" "enter2: result: %s\n" (C2PODomain.show new_state);
255267
let new_state = reset_normal_form new_state in
256268
[ctx.local, `Lifted new_state]
@@ -264,7 +276,7 @@ struct
264276
let combine_env ctx lval_opt expr f args t_context_opt f_d (f_ask: Queries.ask) =
265277
match ctx.local with
266278
| `Bot -> `Bot
267-
| `Lifted cc ->
279+
| `Lifted d ->
268280
let caller_ask = ask_of_man ctx in
269281
(* assign function parameters to duplicated values *)
270282
let arg_assigns = GobList.combine_short f.sformals args in
@@ -273,30 +285,32 @@ struct
273285
let arg = T.of_cil f_ask arg in
274286
assign_term st caller_ask ghost_var arg var.vtype
275287
in
276-
let state_with_assignments = List.fold_left assign_term cc arg_assigns in
288+
let state_with_assignments = List.fold_left assign_term d arg_assigns in
277289

278290
if M.tracing then M.trace "c2po-function" "combine_env0: state_with_assignments: %s\n" (C2PODomain.show state_with_assignments);
279291

280292
(*remove all variables that were tainted by the function*)
281293
let tainted = f_ask.f (MayBeTainted) in
282294
if M.tracing then M.trace "c2po-tainted" "combine_env1: %a\n" MayBeEqual.AD.pretty tainted;
283295

284-
let local = D.remove_tainted_terms caller_ask tainted state_with_assignments in
296+
let local = D.remove_tainted_terms caller_ask tainted state_with_assignments.data in
297+
let local = data_to_t local in
285298
match D.meet (`Lifted local) f_d with
286299
| `Bot -> `Bot
287-
| `Lifted cc ->
288-
let cc = reset_normal_form @@ remove_out_of_scope_vars cc f in
300+
| `Lifted d ->
301+
let cc = remove_out_of_scope_vars d.data f in
302+
let d = data_to_t cc in
289303
if M.tracing then begin
290304
let dummy_lval = Var (Var.dummy_varinfo (TVoid[])), NoOffset in
291305
let lval = BatOption.default dummy_lval lval_opt in
292-
M.trace "c2po-function" "combine_env2: var_opt: %a; local_state: %s; f_state: %s; meeting everything: %s\n" d_lval lval (D.show ctx.local) (D.show f_d) (C2PODomain.show cc)
306+
M.trace "c2po-function" "combine_env2: var_opt: %a; local_state: %s; f_state: %s; meeting everything: %s\n" d_lval lval (D.show ctx.local) (D.show f_d) (C2PODomain.show d)
293307
end;
294-
`Lifted cc
308+
`Lifted d
295309

296310
let combine_assign ctx var_opt expr f args t_context_opt f_d (f_ask: Queries.ask) =
297311
match ctx.local with
298312
| `Bot -> `Bot
299-
| `Lifted cc ->
313+
| `Lifted d ->
300314
let caller_ask = ask_of_man ctx in
301315
(* assign function parameters to duplicated values *)
302316
let arg_assigns = GobList.combine_short f.sformals args in
@@ -305,24 +319,24 @@ struct
305319
let arg = T.of_cil f_ask arg in
306320
assign_term st caller_ask ghost_var arg var.vtype
307321
in
308-
let state_with_assignments = List.fold_left assign_term cc arg_assigns in
322+
let state_with_assignments = List.fold_left assign_term d arg_assigns in
309323
match D.meet (`Lifted state_with_assignments) f_d with
310324
| `Bot -> `Bot
311-
| `Lifted cc ->
312-
let cc = match var_opt with
325+
| `Lifted d ->
326+
let d = match var_opt with
313327
| None ->
314-
cc
328+
d
315329
| Some lval ->
316330
let return_type = typeOfLval lval in
317331
let return_var = MayBeEqual.return_var return_type in
318332
let return_var = (Some return_var, Some Z.zero) in
319-
assign_lval cc f_ask lval return_var
333+
assign_lval d f_ask lval return_var
320334
in
321-
if M.tracing then M.trace "c2po-function" "combine_assign1: assigning return value: %s\n" (C2PODomain.show cc);
322-
let cc = remove_out_of_scope_vars cc f in
323-
let cc = reset_normal_form cc in
324-
if M.tracing then M.trace "c2po-function" "combine_assign2: result: %s\n" (C2PODomain.show cc);
325-
`Lifted cc
335+
if M.tracing then M.trace "c2po-function" "combine_assign1: assigning return value: %s\n" (C2PODomain.show d);
336+
let d = remove_out_of_scope_vars d.data f in
337+
let d = data_to_t d in
338+
if M.tracing then M.trace "c2po-function" "combine_assign2: result: %s\n" (C2PODomain.show d);
339+
`Lifted d
326340

327341
let startstate v =
328342
D.top ()

0 commit comments

Comments
 (0)