Skip to content

Commit 306952d

Browse files
Cleanup some option related code (#1689)
* Cleanup `option` related code * relationAnalysis: Drastically simplify `pass_to_callee` * Simplify some things in `relationAnalysis` * Remove custom `default` function from `arrayDomain` * Simplification in `addressDomain` * Further cleanup * Some further obvious cases * Undo eta expansion Co-authored-by: Simmo Saan <simmo.saan@gmail.com> * Indentation Co-authored-by: Simmo Saan <simmo.saan@gmail.com> * Indentation * Add TODO back * Add comment back * Indentation --------- Co-authored-by: Simmo Saan <simmo.saan@gmail.com>
1 parent f5cebf7 commit 306952d

20 files changed

Lines changed: 231 additions & 362 deletions

src/analyses/abortUnless.ml

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,7 @@ struct
3232
| [arg] when isIntegralType arg.vtype ->
3333
(match man.ask (EvalInt (Lval (Var arg, NoOffset))) with
3434
| v when Queries.ID.is_bot v -> false
35-
| v ->
36-
match Queries.ID.to_bool v with
37-
| Some b -> b
38-
| None -> false)
35+
| v -> BatOption.default false (Queries.ID.to_bool v))
3936
| _ ->
4037
(* should not happen, man.local should always be false in this case *)
4138
false

src/analyses/accessAnalysis.ml

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,7 @@ struct
7979
man.local
8080

8181
let return man exp fundec : D.t =
82-
begin match exp with
83-
| Some exp -> access_one_top man Read false exp
84-
| None -> ()
85-
end;
82+
Option.iter (access_one_top man Read false) exp;
8683
man.local
8784

8885
let body man f : D.t =
@@ -99,9 +96,7 @@ struct
9996
LibraryDesc.Accesses.iter desc.accs (fun {kind; deep = reach} exp ->
10097
access_one_top ~deref:true man kind reach exp (* access dereferenced using special accesses *)
10198
) arglist;
102-
(match lv with
103-
| Some x -> access_one_top ~deref:true man Write false (AddrOf x)
104-
| None -> ());
99+
Option.iter (fun x -> access_one_top ~deref:true man Write false (AddrOf x)) lv;
105100
List.iter (access_one_top man Read false) arglist; (* always read all argument expressions without dereferencing *)
106101
man.local
107102

@@ -115,19 +110,15 @@ struct
115110
au
116111

117112
let combine_assign man lv fexp f args fc al f_ask =
118-
begin match lv with
119-
| None -> ()
120-
| Some lval -> access_one_top ~deref:true man Write false (AddrOf lval)
121-
end;
113+
Option.iter (fun lval -> access_one_top ~deref:true man Write false (AddrOf lval)) lv;
122114
man.local
123115

124116

125117
let threadspawn man ~multiple lval f args fman =
126118
(* must explicitly access thread ID lval because special to pthread_create doesn't if singlethreaded before *)
127-
begin match lval with
128-
| None -> ()
129-
| Some lval -> access_one_top ~force:true ~deref:true man Write false (AddrOf lval) (* must force because otherwise doesn't if singlethreaded before *)
130-
end;
119+
Option.iter (fun lval ->
120+
access_one_top ~force:true ~deref:true man Write false (AddrOf lval) (* must force because otherwise doesn't if singlethreaded before *)
121+
) lval;
131122
man.local
132123

133124
let query man (type a) (q: a Queries.t): a Queries.result =

src/analyses/apron/relationAnalysis.apron.ml

Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -292,16 +292,14 @@ struct
292292
in
293293
List.fold (fun vs e -> VS.join vs (to_vs e)) (VS.empty ()) args
294294

295-
let pass_to_callee fundec any_local_reachable var =
296-
(* TODO: currently, we pass all locals of the caller to the callee, provided one of them is reachbale to preserve relationality *)
295+
let belongs_to_fundec fundec var =
296+
(* TODO: currently, we pass all locals of the caller to the callee, provided one of them is reachable to preserve relationality *)
297297
(* there should be smarter ways to do this, e.g. by keeping track of which values are written etc. ... *)
298298
(* See, e.g, Beckschulze E, Kowalewski S, Brauer J (2012) Access-based localization for octagons. Electron Notes Theor Comput Sci 287:29–40 *)
299299
(* Also, a local *)
300300
let vname = GobApron.Var.show var in
301-
let locals = fundec.sformals @ fundec.slocals in
302-
match List.find_opt (fun v -> VM.var_name (Local v) = vname) locals with (* TODO: optimize *)
303-
| None -> true
304-
| Some v -> any_local_reachable
301+
let equiv v = VM.var_name (Local v) = vname in (* TODO: optimize *)
302+
(not @@ List.exists equiv fundec.sformals) && (not @@ List.exists equiv fundec.slocals)
305303

306304
let make_callee_rel ~thread man f args =
307305
let fundec = Node.find_fundec man.node in
@@ -329,7 +327,7 @@ struct
329327
let any_local_reachable = any_local_reachable fundec reachable_from_args in
330328
RD.remove_filter_with new_rel (fun var ->
331329
match RV.find_metadata var with
332-
| Some (Local _) when not (pass_to_callee fundec any_local_reachable var) -> true (* remove caller locals provided they are unreachable *)
330+
| Some (Local _) when not (belongs_to_fundec fundec var || any_local_reachable) -> true (* remove caller locals provided they are unreachable *)
333331
| Some (Arg _) when not (List.mem_cmp Apron.Var.compare var arg_vars) -> true (* remove caller args, but keep just added args *)
334332
| _ -> false (* keep everything else (just added args, globals, global privs) *)
335333
);
@@ -359,16 +357,14 @@ struct
359357
let st = man.local in
360358
let ask = Analyses.ask_of_man man in
361359
let new_rel =
362-
if RD.Tracked.type_tracked (Cilfacade.fundec_return_type f) then (
360+
if RD.Tracked.type_tracked (Cilfacade.fundec_return_type f) then
363361
let rel' = RD.add_vars st.rel [RV.return] in
364-
match e with
365-
| Some e ->
362+
Option.map_default (fun e ->
366363
assign_from_globals_wrapper ask man.global {st with rel = rel'} e (fun rel' e' ->
367364
RD.assign_exp ask rel' RV.return e' (no_overflow ask e)
368-
)
369-
| None ->
370-
rel' (* leaves V.return unconstrained *)
371-
)
365+
)
366+
) rel' e
367+
(* default value rel' leaves V.return unconstrained *)
372368
else
373369
RD.copy st.rel
374370
in
@@ -426,7 +422,7 @@ struct
426422
let tainted_vars = TaintPartialContexts.conv_varset tainted in
427423
let new_rel = RD.keep_filter st.rel (fun var ->
428424
match RV.find_metadata var with
429-
| Some (Local _) when not (pass_to_callee fundec any_local_reachable var) -> true (* keep caller locals, provided they were not passed to the function *)
425+
| Some (Local _) when not (belongs_to_fundec fundec var || any_local_reachable) -> true (* keep caller locals, provided they were not passed to the function *)
430426
| Some (Arg _) -> true (* keep caller args *)
431427
| Some ((Local _ | Global _)) when not (RD.mem_var new_fun_rel var) -> false (* remove locals and globals, for which no record exists in the new_fun_apr *)
432428
| Some ((Local v | Global v)) when not (TaintPartialContexts.VS.mem v tainted_vars) -> true (* keep locals and globals, which have not been touched by the call *)
@@ -439,20 +435,17 @@ struct
439435

440436
let combine_assign man r fe f args fc fun_st (f_ask : Queries.ask) =
441437
let unify_st = man.local in
442-
if RD.Tracked.type_tracked (Cilfacade.fundec_return_type f) then (
443-
let unify_st' = match r with
444-
| Some lv ->
438+
if RD.Tracked.type_tracked (Cilfacade.fundec_return_type f) then
439+
let unify_st' = Option.map_default (fun lv ->
445440
let ask = Analyses.ask_of_man man in
446441
assign_to_global_wrapper ask man.global man.sideg unify_st lv (fun st v ->
447442
let rel = RD.assign_var st.rel (RV.local v) RV.return in
448443
assert_type_bounds ask rel v (* TODO: should be done in return instead *)
449444
)
450-
| None ->
451-
unify_st
445+
) unify_st r
452446
in
453447
RD.remove_vars_with unify_st'.rel [RV.return]; (* mutates! *)
454448
unify_st'
455-
)
456449
else
457450
unify_st
458451

src/analyses/assert.ml

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,7 @@ struct
1818
let check_assert e st =
1919
match man.ask (Queries.EvalInt e) with
2020
| v when Queries.ID.is_bot v -> `Bot
21-
| v ->
22-
match Queries.ID.to_bool v with
23-
| Some b -> `Lifted b
24-
| None -> `Top
21+
| v -> Option.map_default (fun b -> `Lifted b) `Top (Queries.ID.to_bool v)
2522
in
2623
let expr = CilType.Exp.show e in
2724
let warn warn_fn ?annot msg = if check then

0 commit comments

Comments
 (0)