Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use AddressDomain for MayPointTo and ReachableFrom queries #1142

Merged
merged 58 commits into from
Sep 18, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
58 commits
Select commit Hold shift + click to select a range
24c07c4
Add MayPointTo query with address domain
karoliineh Aug 22, 2023
0ecf575
Use MayPointToA in mutexEventAnalysis
karoliineh Aug 22, 2023
fb43b5f
Use MayPointToA in threadEscape
karoliineh Aug 22, 2023
e1263bc
Use MayPointToA in memLeak
karoliineh Aug 22, 2023
eb951b6
Use MayPointToA in mutexTypeAnalysis
karoliineh Aug 22, 2023
4bd38f5
Use MayPointToA in pthreadSignals
karoliineh Aug 22, 2023
68f5bb2
Use MayPointToA in useAfterFree
karoliineh Aug 22, 2023
940a771
Use MayPointToA in relationAnalysis
karoliineh Aug 22, 2023
ca39daf
Add mutex addr to unlock warning
karoliineh Aug 22, 2023
158a450
Use MayPointToA in relationAnalysis cont.
karoliineh Aug 22, 2023
4bfa786
Add TODO for removing fold from memLeak
karoliineh Aug 22, 2023
71789d1
Use MayPointToA in useAfterFree cont.
karoliineh Aug 22, 2023
3cbd31f
Use MayPointToA in varEq
karoliineh Aug 22, 2023
b5f6c4b
Use MayPointToA in uninit
karoliineh Aug 22, 2023
8937455
Use MayPointToA in spec
karoliineh Aug 22, 2023
e417443
Use MayPointToA in mallocFresh
karoliineh Aug 22, 2023
16f3500
Use MayPointToA in malloc_null
karoliineh Aug 22, 2023
68d6588
Use MayPointToA in fileUse
karoliineh Aug 22, 2023
e697504
Use MayPointToA in extractPthread
karoliineh Aug 22, 2023
076da53
Use MayPointToA in condVars
karoliineh Aug 22, 2023
1c7186c
Use MayPointToA in mvalMapDomain
karoliineh Aug 22, 2023
f7b38a0
Add ReachableFrom query with address domain
karoliineh Aug 22, 2023
b49f0e0
Use MayPointToA and ReachableFromA in accessAnalysis
karoliineh Aug 22, 2023
20b75c6
Use MayPointToA and ReachableFromA in varEq
karoliineh Aug 22, 2023
5e8e2c7
Make getaddrinfo specification more precise
sim642 Aug 23, 2023
e079212
Remove unnecessary offset double conversions
sim642 Aug 23, 2023
2f4119d
Use AddressDomain helper functions for to_mval and to_var_may
sim642 Aug 23, 2023
cfab28c
Use ReachableFromA in extractPthread
karoliineh Aug 23, 2023
178a9c6
Remove duplicated Tasks module from extractPthread
karoliineh Aug 23, 2023
08c9f1d
Use ReachableFromA in malloc_null
karoliineh Aug 23, 2023
5dbfb02
Use ReachableFromA in poisonVariables
karoliineh Aug 23, 2023
de9c475
Use ReachableFromA in threadEscape
karoliineh Aug 23, 2023
2cd0ae6
Extract to_extra fun from fold in malloc_null
karoliineh Aug 23, 2023
e4bef9c
Use ReachableFromA in uninit
karoliineh Aug 23, 2023
973edf6
Use ReachableFromA in useAfterFree
karoliineh Aug 23, 2023
cf58111
Use ReachableFromA in relationAnalysis
karoliineh Aug 23, 2023
2e4b9f3
Refactor everything until memLeak
karoliineh Aug 25, 2023
641de7d
Refactor everything after memLeak
karoliineh Aug 25, 2023
f3c3d18
Use MayPointToA and ReachableFromA in taindPartialContexts
karoliineh Aug 25, 2023
22da3df
Use MayPointToA for may_point_to in queries
karoliineh Aug 25, 2023
5cf96d1
Remove MayPointTo and ReachableFrom queries with LS
karoliineh Aug 25, 2023
9291c8a
Rename MayPointToA -> MayPointTo
karoliineh Aug 25, 2023
43223d0
Rename ReachableFromA -> ReachableFrom
karoliineh Aug 25, 2023
866f517
Rename a -> ad, where missed
karoliineh Aug 25, 2023
57549d0
Simplify cardinal > 0 to not is_empty
karoliineh Aug 25, 2023
3cbc8e7
Remove done TODO from uninit
karoliineh Aug 28, 2023
6bfb97b
Use AddressDomain for queries in base
karoliineh Aug 28, 2023
5938225
Remove unneccessary module calls before AD and Addr in base
karoliineh Aug 28, 2023
c8cc6c4
Add TODOs about AddressDomain queries usage
sim642 Sep 11, 2023
7edb312
Simplify some AddressDomain query result manipulation
sim642 Sep 11, 2023
3e01426
Cherry-pick AddressDomain.filter from 224615f63b07448ba45afb79f42c2d8…
sim642 Sep 11, 2023
bb8a926
Merge branch 'master' into queries-ad
sim642 Sep 11, 2023
5583f04
Merge branch 'master' into queries-ad
karoliineh Sep 12, 2023
dce70e2
Add comment about unsoundness back to extractPthread
karoliineh Sep 12, 2023
0568edd
Simplify do_exp in uninit and malloc_null
karoliineh Sep 12, 2023
3ee9af8
Simplify remove_unreachable in uninit and malloc_null
karoliineh Sep 12, 2023
f18d808
D.remove instead of diffing singleton in memLeak
karoliineh Sep 13, 2023
64a0a6f
Merge branch 'master' into queries-ad
sim642 Sep 18, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/analyses/apron/relationAnalysis.apron.ml
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ struct
let reachable_from_args ctx args =
let to_vs e =
ctx.ask (ReachableFrom e)
|> LockDomain.MayLocksetNoRW.to_var_may
|> Queries.AD.to_var_may
|> VS.of_list
in
List.fold (fun vs e -> VS.join vs (to_vs e)) (VS.empty ()) args
Expand Down
4 changes: 2 additions & 2 deletions src/analyses/base.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1319,8 +1319,8 @@ struct
(* ignore @@ printf "EvalStr Address: %a -> %s (must %i, may %i)\n" d_plainexp e (VD.short 80 (Address a)) (List.length @@ AD.to_var_must a) (List.length @@ AD.to_var_may a); *)
begin match unrollType (Cilfacade.typeOf e) with
| TPtr(TInt(IChar, _), _) ->
let (v,o) = List.hd (AD.to_mval a) in
let lval = Mval.Exp.to_cil @@ (v, Addr.Offs.to_exp o) in
let mval = List.hd (AD.to_mval a) in
let lval = Addr.Mval.to_cil mval in
(try `Lifted (Bytes.to_string (Hashtbl.find char_array lval))
with Not_found -> Queries.Result.top q)
| _ -> (* what about ISChar and IUChar? *)
Expand Down
22 changes: 10 additions & 12 deletions src/analyses/condVars.ml
Original file line number Diff line number Diff line change
Expand Up @@ -64,18 +64,16 @@ struct
let (>?) = Option.bind

let mayPointTo ctx exp =
match ctx.ask (Queries.MayPointTo exp) with
| ad when not (Queries.AD.is_top ad) && not (Queries.AD.is_empty ad) ->
let a' = if Queries.AD.mem UnknownPtr ad then (
M.info ~category:Unsound "mayPointTo: query result for %a contains TOP!" d_exp exp; (* UNSOUND *)
Queries.AD.remove UnknownPtr ad
) else ad
in
List.filter_map (function
| ValueDomain.Addr.Addr (v,o) -> Some (v, ValueDomain.Addr.Offs.to_exp o)
| _ -> None
) (Queries.AD.elements a')
| _ -> []
let ad = ctx.ask (Queries.MayPointTo exp) in
let a' = if Queries.AD.mem UnknownPtr ad then (
M.info ~category:Unsound "mayPointTo: query result for %a contains TOP!" d_exp exp; (* UNSOUND *)
Queries.AD.remove UnknownPtr ad
) else ad
in
List.filter_map (function
| ValueDomain.Addr.Addr (v,o) -> Some (v, ValueDomain.Addr.Offs.to_exp o) (* TODO: use unconverted addrs in domain? *)
| _ -> None
) (Queries.AD.elements a')

let mustPointTo ctx exp = (* this is just to get Mval.Exp *)
match mayPointTo ctx exp with
Expand Down
21 changes: 5 additions & 16 deletions src/analyses/extractPthread.ml
Original file line number Diff line number Diff line change
Expand Up @@ -877,16 +877,9 @@ module Spec : Analyses.MCPSpec = struct

module ExprEval = struct
let eval_ptr ctx exp =
let ad = ctx.ask (Queries.MayPointTo exp) in
if (not (Queries.AD.is_top ad)) && not (Queries.AD.is_empty ad) then
if Queries.AD.mem UnknownPtr ad
then (* UNSOUND *)
Queries.AD.remove UnknownPtr ad
|> Queries.AD.to_var_may
else
Queries.AD.to_var_may ad
else
[]
ctx.ask (Queries.MayPointTo exp)
|> Queries.AD.remove UnknownPtr
karoliineh marked this conversation as resolved.
Show resolved Hide resolved
|> Queries.AD.to_var_may

let eval_var ctx exp =
match exp with
Expand Down Expand Up @@ -1126,11 +1119,7 @@ module Spec : Analyses.MCPSpec = struct
ad
in
let thread_fun =
Queries.AD.fold (fun addr vars ->
match addr with
| Queries.AD.Addr.Addr (v,_) -> v :: vars
| _ -> vars
) funs_ad []
Queries.AD.to_var_may funs_ad
|> List.unique ~eq:(fun a b -> a.vid = b.vid)
|> List.hd
in
Expand Down Expand Up @@ -1255,7 +1244,7 @@ module Spec : Analyses.MCPSpec = struct
(* TODO: optimize finding *)
let tasks_f =
let var_in_ad ad f = Queries.AD.exists (function
| Queries.AD.Addr.Addr (ls_f,_) -> ls_f = f
| Queries.AD.Addr.Addr (ls_f,_) -> CilType.Varinfo.equal ls_f f
| _ -> false
) ad
in
Expand Down
2 changes: 1 addition & 1 deletion src/analyses/malloc_null.ml
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ struct
| ad when not (Queries.AD.is_top ad) ->
let to_extra addr ads =
match addr with
| Queries.AD.Addr.Addr mval -> AD.of_mval mval :: ads
| Queries.AD.Addr.Addr mval -> AD.of_mval mval :: ads (* TODO: why list of singleton AD-s? *)
| _ -> ads
in
Queries.AD.fold to_extra ad []
Expand Down
2 changes: 1 addition & 1 deletion src/analyses/memLeak.ml
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ struct
| Queries.AD.Addr.Addr (v,_) when ctx.ask (Queries.IsHeapVar v) && not @@ ctx.ask (Queries.IsMultiple v) -> D.singleton v
| _ -> D.empty ()
in
D.diff state unique_pointed_to_heap_vars
D.diff state unique_pointed_to_heap_vars (* TODO: use D.remove instead of diffing singleton *)
| _ -> state
end
| Abort ->
Expand Down
4 changes: 2 additions & 2 deletions src/analyses/mutexTypeAnalysis.ml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ struct
module O = Offset.Unit

module V = struct
include Printable.Prod(CilType.Varinfo)(O)
include Printable.Prod(CilType.Varinfo)(O) (* TODO: use Mval.Unit *)
let is_write_only _ = false
end

Expand Down Expand Up @@ -56,7 +56,7 @@ struct
let attr = ctx.ask (Queries.EvalMutexAttr attr) in
let mutexes = ctx.ask (Queries.MayPointTo mutex) in
(* It is correct to iter over these sets here, as mutexes need to be intialized before being used, and an analysis that detects usage before initialization is a different analysis. *)
Queries.AD.iter (function addr ->
Queries.AD.iter (function addr ->
match addr with
| Queries.AD.Addr.Addr (v,o) -> ctx.sideg (v,O.of_offs o) attr
| _ -> ()
Expand Down
2 changes: 1 addition & 1 deletion src/analyses/poisonVariables.ml
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ struct
| ad ->
Queries.AD.fold (fun addr vs ->
match addr with
| Queries.AD.Addr.Addr (v,o) -> rem_mval vs (v, ValueDomain.Offs.to_exp o)
| Queries.AD.Addr.Addr (v,o) -> rem_mval vs (v, ValueDomain.Offs.to_exp o) (* TODO: use unconverted addrs in domain? *)
| _ -> vs
) ad ctx.local
end
Expand Down
6 changes: 2 additions & 4 deletions src/analyses/pthreadSignals.ml
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,8 @@ struct
module C = MustSignals
module G = SetDomain.ToppedSet (MHP) (struct let topname = "All Threads" end)

let eval_exp_addr (a: Queries.ask) exp = Queries.AD.elements (a.f (Queries.MayPointTo exp))

let possible_vinfos a cv_arg =
List.filter_map ValueDomain.Addr.to_var_may (eval_exp_addr a cv_arg)
let possible_vinfos (a: Queries.ask) cv_arg =
Queries.AD.to_var_may (a.f (Queries.MayPointTo cv_arg))

(* transfer functions *)

Expand Down
2 changes: 1 addition & 1 deletion src/analyses/taintPartialContexts.ml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ let to_mvals ad =
(* TODO: should one handle ad with unknown pointers separately like in (all) other analyses? *)
Queries.AD.fold (fun addr mvals ->
match addr with
| Queries.AD.Addr.Addr (v,o) -> D.add (v, ValueDomain.Offs.to_exp o) mvals
| Queries.AD.Addr.Addr (v,o) -> D.add (v, ValueDomain.Offs.to_exp o) mvals (* TODO: use unconverted addrs in domain? *)
| _ -> mvals
) ad (D.empty ())

Expand Down
2 changes: 1 addition & 1 deletion src/analyses/uninit.ml
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ struct
| ad when not (Queries.AD.is_top ad) ->
let to_extra ad ads =
match ad with
| Queries.AD.Addr.Addr mval -> AD.of_mval mval :: ads
| Queries.AD.Addr.Addr mval -> AD.of_mval mval :: ads (* TODO: why list of singleton AD-s? *)
| _ -> ads
in
Queries.AD.fold to_extra ad []
Expand Down
11 changes: 2 additions & 9 deletions src/analyses/useAfterFree.ml
Original file line number Diff line number Diff line change
Expand Up @@ -160,15 +160,8 @@ struct
if Queries.AD.is_top reachable_from_args || D.is_top caller_state then
[caller_state, caller_state]
else
let reachable_vars =
let get_vars addr vars =
match addr with
| Queries.AD.Addr.Addr (v,_) -> v :: vars
| _ -> vars
in
Queries.AD.fold get_vars reachable_from_args []
in
let callee_state = D.filter (fun var -> List.mem var reachable_vars) caller_state in
let reachable_vars = Queries.AD.to_var_may reachable_from_args in
let callee_state = D.filter (fun var -> List.mem var reachable_vars) caller_state in (* TODO: use AD.mem directly *)
[caller_state, callee_state]
)

Expand Down
4 changes: 2 additions & 2 deletions src/analyses/varEq.ml
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ struct
if Queries.AD.is_top aad
then false
else Queries.AD.exists (function
| Addr (u,s) -> CilType.Varinfo.equal v u && oleq o (Addr.Offs.to_exp s)
| Addr (u,s) -> CilType.Varinfo.equal v u && oleq o (Addr.Offs.to_exp s) (* TODO: avoid conversion? *)
| _ -> false
) aad
in
Expand Down Expand Up @@ -298,7 +298,7 @@ struct
else if Queries.AD.is_top bad
then ((*Messages.warn ~category:Analyzer "No PT-set: switching to types ";*) type_may_change_apt a )
else Queries.AD.exists (function
| Addr (v,o) -> lval_may_change_pt a (v, Addr.Offs.to_exp o)
| Addr (v,o) -> lval_may_change_pt a (v, Addr.Offs.to_exp o) (* TODO: avoid conversion? *)
| _ -> false
) bad
in
Expand Down
2 changes: 2 additions & 0 deletions src/cdomains/addressDomain.ml
Original file line number Diff line number Diff line change
Expand Up @@ -440,4 +440,6 @@ struct
let r = narrow x y in
if M.tracing then M.traceu "ad" "-> %a\n" pretty r;
r

let filter f ad = fold (fun addr ad -> if f addr then add addr ad else ad) ad (empty ())
end
Loading