Skip to content

Commit

Permalink
Implement and use IsHeapVar and IsDynamicallyAlloced right
Browse files Browse the repository at this point in the history
  • Loading branch information
mrstanb committed Sep 21, 2023
1 parent 6e9bb2f commit d9df607
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 10 deletions.
8 changes: 4 additions & 4 deletions src/analyses/base.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1254,7 +1254,7 @@ struct
| Address a ->
(* If there's a non-heap var or an offset in the lval set, we answer with bottom *)
if AD.exists (function
| Addr (v,o) -> (not @@ ctx.ask (Queries.IsHeapVar v)) || (ctx.ask (Queries.IsHeapVar v) && not @@ ctx.ask (Queries.IsDynamicallyAlloced v)) || o <> `NoOffset
| Addr (v,o) -> (not @@ ctx.ask (Queries.IsDynamicallyAlloced v)) || (ctx.ask (Queries.IsDynamicallyAlloced v) && not @@ ctx.ask (Queries.IsHeapVar v)) || o <> `NoOffset
| _ -> false) a then
Queries.Result.bot q
else (
Expand Down Expand Up @@ -1383,7 +1383,7 @@ struct
let t = match t_override with
| Some t -> t
| None ->
if a.f (Q.IsHeapVar x) then
if a.f (Q.IsDynamicallyAlloced x) then
(* the vtype of heap vars will be TVoid, so we need to trust the pointer we got to this to be of the right type *)
(* i.e. use the static type of the pointer here *)
lval_type
Expand Down Expand Up @@ -1429,7 +1429,7 @@ struct
(* Optimization to avoid evaluating integer values when setting them.
The case when invariant = true requires the old_value to be sound for the meet.
Allocated blocks are representend by Blobs with additional information, so they need to be looked-up. *)
let old_value = if not invariant && Cil.isIntegralType x.vtype && not (a.f (IsHeapVar x)) && offs = `NoOffset then begin
let old_value = if not invariant && Cil.isIntegralType x.vtype && not (a.f (IsDynamicallyAlloced x)) && offs = `NoOffset then begin
VD.bot_value ~varAttr:x.vattr lval_type
end else
Priv.read_global a priv_getg st x
Expand Down Expand Up @@ -1995,7 +1995,7 @@ struct

let check_invalid_mem_dealloc ctx special_fn ptr =
let has_non_heap_var = AD.exists (function
| Addr (v,_) -> not (ctx.ask (Q.IsHeapVar v)) || (ctx.ask (Q.IsHeapVar v) && not @@ ctx.ask (Q.IsDynamicallyAlloced v))
| Addr (v,_) -> not (ctx.ask (Q.IsDynamicallyAlloced v)) || (ctx.ask (Q.IsDynamicallyAlloced v) && not @@ ctx.ask (Q.IsHeapVar v))
| _ -> false)
in
let has_non_zero_offset = AD.exists (function
Expand Down
2 changes: 1 addition & 1 deletion src/analyses/memLeak.ml
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ struct
| ad when not (Queries.AD.is_top ad) && Queries.AD.cardinal ad = 1 ->
(* Note: Need to always set "ana.malloc.unique_address_count" to a value > 0 *)
begin match Queries.AD.choose ad with
| Queries.AD.Addr.Addr (v,_) when ctx.ask (Queries.IsHeapVar v) && ctx.ask (Queries.IsDynamicallyAlloced v) && not @@ ctx.ask (Queries.IsMultiple v) -> D.remove v state (* Unique pointed to heap vars *)
| Queries.AD.Addr.Addr (v,_) when ctx.ask (Queries.IsDynamicallyAlloced v) && ctx.ask (Queries.IsHeapVar v) && not @@ ctx.ask (Queries.IsMultiple v) -> D.remove v state (* Unique pointed to heap vars *)
| _ -> state
end
| _ -> state
Expand Down
4 changes: 2 additions & 2 deletions src/analyses/useAfterFree.ml
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ struct
let pointed_to_heap_vars =
Queries.AD.fold (fun addr vars ->
match addr with
| Queries.AD.Addr.Addr (v,_) when ctx.ask (Queries.IsHeapVar v) -> v :: vars
| Queries.AD.Addr.Addr (v,_) when ctx.ask (Queries.IsDynamicallyAlloced v) -> v :: vars
| _ -> vars
) ad []
in
Expand Down Expand Up @@ -185,7 +185,7 @@ struct
let pointed_to_heap_vars =
Queries.AD.fold (fun addr state ->
match addr with
| Queries.AD.Addr.Addr (var,_) when ctx.ask (Queries.IsHeapVar var) -> D.add var state
| Queries.AD.Addr.Addr (var,_) when ctx.ask (Queries.IsDynamicallyAlloced var) -> D.add var state
| _ -> state
) ad (D.empty ())
in
Expand Down
4 changes: 2 additions & 2 deletions src/analyses/wrapperFunctionAnalysis.ml
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,9 @@ module MallocWrapper : MCPSpec = struct
if on_stack then var.vattr <- addAttribute (Attr ("stack_alloca", [])) var.vattr; (* If the call was for stack allocation, add an attr to mark the heap var *)
`Lifted var
| Q.IsHeapVar v ->
NodeVarinfoMap.mem_varinfo v
| Q.IsDynamicallyAlloced v ->
NodeVarinfoMap.mem_varinfo v && not @@ hasAttribute "stack_alloca" v.vattr
| Q.IsDynamicallyAlloced v ->
NodeVarinfoMap.mem_varinfo v
| Q.IsMultiple v ->
begin match NodeVarinfoMap.from_varinfo v with
| Some (_, _, c) -> UniqueCount.is_top c || not (ctx.ask Q.MustBeUniqueThread)
Expand Down
2 changes: 1 addition & 1 deletion src/domains/queries.ml
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ type _ t =
| DYojson: FlatYojson.t t (** Get local state Yojson of one path under [PathQuery]. *)
| HeapVar: {on_stack: bool} -> VI.t t (* If on_stack is [true], then alloca() or a similar function was called *)
| IsHeapVar: varinfo -> MayBool.t t (* TODO: is may or must? *)
| IsDynamicallyAlloced: varinfo -> MayBool.t t (* [true] if heap var represents dynamically alloced memory, [false] if it represents the result of an alloca() call *)
| IsDynamicallyAlloced: varinfo -> MayBool.t t (* [true] if heap var represents dynamically alloced memory *)
| IsMultiple: varinfo -> MustBool.t t
(* For locals: Is another copy of this local variable reachable via pointers? *)
(* For dynamically allocated memory: Does this abstract variable corrrespond to a unique heap location? *)
Expand Down

0 comments on commit d9df607

Please sign in to comment.