You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
1858 lines
77 KiB
1858 lines
77 KiB
10 years ago
|
(*
|
||
9 years ago
|
* Copyright (c) 2009 - 2013 Monoidics ltd.
|
||
|
* Copyright (c) 2013 - present Facebook, Inc.
|
||
|
* All rights reserved.
|
||
|
*
|
||
|
* This source code is licensed under the BSD style license found in the
|
||
|
* LICENSE file in the root directory of this source tree. An additional grant
|
||
|
* of patent rights can be found in the PATENTS file in the same directory.
|
||
|
*)
|
||
10 years ago
|
|
||
8 years ago
|
open! IStd
|
||
9 years ago
|
|
||
10 years ago
|
(** Symbolic Execution *)
|
||
|
|
||
|
module L = Logging
|
||
|
module F = Format
|
||
|
|
||
|
let rec fldlist_assoc fld = function
|
||
7 years ago
|
| [] ->
|
||
|
raise Not_found
|
||
|
| (fld', x, _) :: l ->
|
||
|
if Typ.Fieldname.equal fld fld' then x else fldlist_assoc fld l
|
||
|
|
||
10 years ago
|
|
||
8 years ago
|
let unroll_type tenv (typ: Typ.t) (off: Sil.offset) =
|
||
|
let fail fld_to_string fld =
|
||
8 years ago
|
L.d_strln ".... Invalid Field Access ...." ;
|
||
|
L.d_str ("Fld : " ^ fld_to_string fld) ;
|
||
|
L.d_ln () ;
|
||
|
L.d_str "Type : " ;
|
||
|
Typ.d_full typ ;
|
||
|
L.d_ln () ;
|
||
8 years ago
|
raise (Exceptions.Bad_footprint __POS__)
|
||
|
in
|
||
8 years ago
|
match (typ.desc, off) with
|
||
8 years ago
|
| Tstruct name, Off_fld (fld, _) -> (
|
||
8 years ago
|
match Tenv.lookup tenv name with
|
||
|
| Some {fields; statics} -> (
|
||
7 years ago
|
try fldlist_assoc fld (fields @ statics) with Not_found -> fail Typ.Fieldname.to_string fld )
|
||
7 years ago
|
| None ->
|
||
|
fail Typ.Fieldname.to_string fld )
|
||
7 years ago
|
| Tarray {elt}, Off_index _ ->
|
||
|
elt
|
||
7 years ago
|
| _, Off_index Const Cint i when IntLit.iszero i ->
|
||
|
typ
|
||
|
| _ ->
|
||
|
fail Sil.offset_to_string off
|
||
|
|
||
10 years ago
|
|
||
|
(** Apply function [f] to the expression at position [offlist] in [strexp].
|
||
10 years ago
|
If not found, expand [strexp] and apply [f] to [None].
|
||
|
The routine should maintain the invariant that strexp and typ correspond to
|
||
|
each other exactly, without involving any re - interpretation of some type t
|
||
|
as the t array. The [fp_root] parameter indicates whether the kind of the
|
||
|
root expression of the corresponding pointsto predicate is a footprint identifier.
|
||
|
The function can expand a list of higher - order [hpara_psto] predicates, if
|
||
|
the list is stored at [offlist] in [strexp] initially. The expanded list
|
||
|
is returned as a part of the result. All these happen under [p], so that it
|
||
|
is sound to call the prover with [p]. Finally, before running this function,
|
||
|
the tool should run strexp_extend_value in rearrange.ml for the same strexp
|
||
|
and offlist, so that all the necessary extensions of strexp are done before
|
||
|
this function. If the tool follows this protocol, it will never hit the assert
|
||
|
false cases for field and array accesses. *)
|
||
8 years ago
|
let rec apply_offlist pdesc tenv p fp_root nullify_struct (root_lexp, strexp, typ) offlist
|
||
8 years ago
|
(f: Exp.t option -> Exp.t) inst lookup_inst =
|
||
10 years ago
|
let pp_error () =
|
||
8 years ago
|
L.d_strln ".... Invalid Field ...." ;
|
||
|
L.d_str "strexp : " ;
|
||
|
Sil.d_sexp strexp ;
|
||
|
L.d_ln () ;
|
||
|
L.d_str "offlist : " ;
|
||
|
Sil.d_offset_list offlist ;
|
||
|
L.d_ln () ;
|
||
|
L.d_str "type : " ;
|
||
|
Typ.d_full typ ;
|
||
|
L.d_ln () ;
|
||
|
L.d_str "prop : " ;
|
||
|
Prop.d_prop p ;
|
||
|
L.d_ln () ;
|
||
|
L.d_ln ()
|
||
|
in
|
||
|
match (offlist, strexp, typ.Typ.desc) with
|
||
7 years ago
|
| [], Sil.Eexp (e, inst_curr), _ ->
|
||
8 years ago
|
let inst_new =
|
||
|
match inst with
|
||
7 years ago
|
| Sil.Ilookup ->
|
||
|
(* a lookup does not change an inst unless it is inst_initial *)
|
||
8 years ago
|
lookup_inst := Some inst_curr ;
|
||
10 years ago
|
inst_curr
|
||
7 years ago
|
| _ ->
|
||
|
Sil.update_inst inst_curr inst
|
||
8 years ago
|
in
|
||
10 years ago
|
let e' = f (Some e) in
|
||
|
(e', Sil.Eexp (e', inst_new), typ, None)
|
||
7 years ago
|
| [], Sil.Estruct (fesl, inst'), _ ->
|
||
|
if not nullify_struct then (f None, Sil.Estruct (fesl, inst'), typ, None)
|
||
8 years ago
|
else if fp_root then (
|
||
|
pp_error () ;
|
||
|
assert false )
|
||
|
else (
|
||
|
L.d_strln "WARNING: struct assignment treated as nondeterministic assignment" ;
|
||
|
(f None, Prop.create_strexp_of_type tenv Prop.Fld_init typ None inst, typ, None) )
|
||
7 years ago
|
| [], Sil.Earray _, _ ->
|
||
|
let offlist' = Sil.Off_index Exp.zero :: offlist in
|
||
8 years ago
|
apply_offlist pdesc tenv p fp_root nullify_struct (root_lexp, strexp, typ) offlist' f inst
|
||
|
lookup_inst
|
||
7 years ago
|
| (Sil.Off_fld _) :: _, Sil.Earray _, _ ->
|
||
|
let offlist_new = Sil.Off_index Exp.zero :: offlist in
|
||
8 years ago
|
apply_offlist pdesc tenv p fp_root nullify_struct (root_lexp, strexp, typ) offlist_new f inst
|
||
|
lookup_inst
|
||
8 years ago
|
| (Sil.Off_fld (fld, fld_typ)) :: offlist', Sil.Estruct (fsel, inst'), Typ.Tstruct name -> (
|
||
8 years ago
|
match Tenv.lookup tenv name with
|
||
|
| Some ({fields} as struct_typ)
|
||
7 years ago
|
-> (
|
||
8 years ago
|
let t' = unroll_type tenv typ (Sil.Off_fld (fld, fld_typ)) in
|
||
|
match List.find ~f:(fun fse -> Typ.Fieldname.equal fld (fst fse)) fsel with
|
||
7 years ago
|
| Some (_, se') ->
|
||
|
let res_e', res_se', res_t', res_pred_insts_op' =
|
||
8 years ago
|
apply_offlist pdesc tenv p fp_root nullify_struct (root_lexp, se', t') offlist' f
|
||
|
inst lookup_inst
|
||
|
in
|
||
|
let replace_fse fse =
|
||
|
if Typ.Fieldname.equal fld (fst fse) then (fld, res_se') else fse
|
||
|
in
|
||
|
let res_se = Sil.Estruct (List.map ~f:replace_fse fsel, inst') in
|
||
|
let replace_fta (f, t, a) =
|
||
|
if Typ.Fieldname.equal fld f then (fld, res_t', a) else (f, t, a)
|
||
|
in
|
||
|
let fields' = List.map ~f:replace_fta fields in
|
||
|
ignore (Tenv.mk_struct tenv ~default:struct_typ ~fields:fields' name) ;
|
||
|
(res_e', res_se, typ, res_pred_insts_op')
|
||
7 years ago
|
| None ->
|
||
|
(* This case should not happen. The rearrangement should
|
||
8 years ago
|
have materialized all the accessed cells. *)
|
||
8 years ago
|
pp_error () ;
|
||
|
assert false )
|
||
7 years ago
|
| None ->
|
||
|
pp_error () ;
|
||
8 years ago
|
assert false )
|
||
7 years ago
|
| (Sil.Off_fld _) :: _, _, _ ->
|
||
|
pp_error () ;
|
||
10 years ago
|
assert false
|
||
7 years ago
|
| ( (Sil.Off_index idx) :: offlist'
|
||
|
, Sil.Earray (len, esel, inst1)
|
||
|
, Typ.Tarray {elt= t'; length= len'; stride= stride'} )
|
||
7 years ago
|
-> (
|
||
8 years ago
|
let nidx = Prop.exp_normalize_prop tenv p idx in
|
||
8 years ago
|
match List.find ~f:(fun ese -> Prover.check_equal tenv p nidx (fst ese)) esel with
|
||
7 years ago
|
| Some (idx_ese', se') ->
|
||
|
let res_e', res_se', res_t', res_pred_insts_op' =
|
||
8 years ago
|
apply_offlist pdesc tenv p fp_root nullify_struct (root_lexp, se', t') offlist' f inst
|
||
|
lookup_inst
|
||
|
in
|
||
8 years ago
|
let replace_ese ese =
|
||
8 years ago
|
if Exp.equal idx_ese' (fst ese) then (idx_ese', res_se') else ese
|
||
|
in
|
||
8 years ago
|
let res_se = Sil.Earray (len, List.map ~f:replace_ese esel, inst1) in
|
||
7 years ago
|
let res_t = Typ.mk_array ~default:typ res_t' ?length:len' ?stride:stride' in
|
||
8 years ago
|
(res_e', res_se, res_t, res_pred_insts_op')
|
||
7 years ago
|
| None ->
|
||
|
(* return a nondeterministic value if the index is not found after rearrangement *)
|
||
8 years ago
|
L.d_str "apply_offlist: index " ;
|
||
|
Sil.d_exp idx ;
|
||
|
L.d_strln " not materialized -- returning nondeterministic value" ;
|
||
8 years ago
|
let res_e' = Exp.Var (Ident.create_fresh Ident.kprimed) in
|
||
8 years ago
|
(res_e', strexp, typ, None) )
|
||
7 years ago
|
| (Sil.Off_index _) :: _, _, _ ->
|
||
|
(* This case should not happen. The rearrangement should
|
||
8 years ago
|
have materialized all the accessed cells. *)
|
||
8 years ago
|
pp_error () ;
|
||
10 years ago
|
raise (Exceptions.Internal_error (Localise.verbatim_desc "Array out of bounds in Symexec"))
|
||
|
|
||
7 years ago
|
|
||
10 years ago
|
(** Given [lexp |-> se: typ], if the location [offlist] exists in [se],
|
||
10 years ago
|
function [ptsto_lookup p (lexp, se, typ) offlist id] returns a tuple.
|
||
|
The first component of the tuple is an expression at position [offlist] in [se].
|
||
|
The second component is an expansion of the predicate [lexp |-> se: typ],
|
||
|
where the entity at [offlist] in [se] is expanded if the entity is a list of
|
||
|
higher - order parameters [hpara_psto]. If this expansion happens,
|
||
|
the last component of the tuple is a list of pi - sigma pairs obtained
|
||
|
by instantiating the [hpara_psto] list. Otherwise, the last component is None.
|
||
|
All these steps happen under [p]. So, we can call a prover with [p].
|
||
|
Finally, before running this function, the tool should run strexp_extend_value
|
||
|
in rearrange.ml for the same se and offlist, so that all the necessary
|
||
|
extensions of se are done before this function. *)
|
||
8 years ago
|
let ptsto_lookup pdesc tenv p (lexp, se, sizeof) offlist id =
|
||
8 years ago
|
let f = function Some exp -> exp | None -> Exp.Var id in
|
||
|
let fp_root = match lexp with Exp.Var id -> Ident.is_footprint id | _ -> false in
|
||
10 years ago
|
let lookup_inst = ref None in
|
||
|
let e', se', typ', pred_insts_op' =
|
||
8 years ago
|
apply_offlist pdesc tenv p fp_root false (lexp, se, sizeof.Exp.typ) offlist f Sil.inst_lookup
|
||
|
lookup_inst
|
||
|
in
|
||
|
let lookup_uninitialized =
|
||
|
(* true if we have looked up an uninitialized value *)
|
||
|
match !lookup_inst with Some (Sil.Iinitial | Sil.Ialloc | Sil.Ilookup) -> true | _ -> false
|
||
|
in
|
||
|
let ptsto' = Prop.mk_ptsto tenv lexp se' (Exp.Sizeof {sizeof with typ= typ'}) in
|
||
10 years ago
|
(e', ptsto', pred_insts_op', lookup_uninitialized)
|
||
|
|
||
7 years ago
|
|
||
10 years ago
|
(** [ptsto_update p (lexp,se,typ) offlist exp] takes
|
||
10 years ago
|
[lexp |-> se: typ], and updates [se] by replacing the
|
||
|
expression at [offlist] with [exp]. Then, it returns
|
||
|
the updated pointsto predicate. If [lexp |-> se: typ] gets
|
||
|
expanded during this update, the generated pi - sigma list from
|
||
|
the expansion gets returned, and otherwise, None is returned.
|
||
|
All these happen under the proposition [p], so it is ok call
|
||
|
prover with [p]. Finally, before running this function,
|
||
|
the tool should run strexp_extend_value in rearrange.ml for the same
|
||
|
se and offlist, so that all the necessary extensions of se are done
|
||
|
before this function. *)
|
||
8 years ago
|
let ptsto_update pdesc tenv p (lexp, se, sizeof) offlist exp =
|
||
10 years ago
|
let f _ = exp in
|
||
8 years ago
|
let fp_root = match lexp with Exp.Var id -> Ident.is_footprint id | _ -> false in
|
||
10 years ago
|
let lookup_inst = ref None in
|
||
|
let _, se', typ', pred_insts_op' =
|
||
|
let pos = State.get_path_pos () in
|
||
8 years ago
|
apply_offlist pdesc tenv p fp_root true (lexp, se, sizeof.Exp.typ) offlist f
|
||
|
(State.get_inst_update pos) lookup_inst
|
||
|
in
|
||
|
let ptsto' = Prop.mk_ptsto tenv lexp se' (Exp.Sizeof {sizeof with typ= typ'}) in
|
||
10 years ago
|
(ptsto', pred_insts_op')
|
||
|
|
||
7 years ago
|
|
||
10 years ago
|
let update_iter iter pi sigma =
|
||
|
let iter' = Prop.prop_iter_update_current_by_list iter sigma in
|
||
8 years ago
|
List.fold ~f:(Prop.prop_iter_add_atom false) ~init:iter' pi
|
||
10 years ago
|
|
||
7 years ago
|
|
||
10 years ago
|
(** Precondition: se should not include hpara_psto
|
||
10 years ago
|
that could mean nonempty heaps. *)
|
||
10 years ago
|
let rec execute_nullify_se = function
|
||
7 years ago
|
| Sil.Eexp _ ->
|
||
|
Sil.Eexp (Exp.zero, Sil.inst_nullify)
|
||
|
| Sil.Estruct (fsel, _) ->
|
||
|
let fsel' = List.map ~f:(fun (fld, se) -> (fld, execute_nullify_se se)) fsel in
|
||
10 years ago
|
Sil.Estruct (fsel', Sil.inst_nullify)
|
||
7 years ago
|
| Sil.Earray (len, esel, _) ->
|
||
|
let esel' = List.map ~f:(fun (idx, se) -> (idx, execute_nullify_se se)) esel in
|
||
9 years ago
|
Sil.Earray (len, esel', Sil.inst_nullify)
|
||
10 years ago
|
|
||
7 years ago
|
|
||
10 years ago
|
(** Do pruning for conditional [if (e1 != e2) ] if [positive] is true
|
||
10 years ago
|
and [(if (e1 == e2)] if [positive] is false *)
|
||
8 years ago
|
let prune_ne tenv ~positive e1 e2 prop =
|
||
10 years ago
|
let is_inconsistent =
|
||
8 years ago
|
if positive then Prover.check_equal tenv prop e1 e2 else Prover.check_disequal tenv prop e1 e2
|
||
|
in
|
||
9 years ago
|
if is_inconsistent then Propset.empty
|
||
|
else
|
||
|
let conjoin = if positive then Prop.conjoin_neq else Prop.conjoin_eq in
|
||
8 years ago
|
let new_prop = conjoin tenv ~footprint:!Config.footprint e1 e2 prop in
|
||
8 years ago
|
if Prover.check_inconsistency tenv new_prop then Propset.empty
|
||
|
else Propset.singleton tenv new_prop
|
||
10 years ago
|
|
||
7 years ago
|
|
||
9 years ago
|
(** Do pruning for conditional "if ([e1] CMP [e2])" if [positive] is
|
||
|
true and "if (!([e1] CMP [e2]))" if [positive] is false, where CMP
|
||
|
is "<" if [is_strict] is true and "<=" if [is_strict] is false.
|
||
|
*)
|
||
8 years ago
|
let prune_ineq tenv ~is_strict ~positive prop e1 e2 =
|
||
8 years ago
|
if Exp.equal e1 e2 then
|
||
8 years ago
|
if positive && not is_strict || not positive && is_strict then Propset.singleton tenv prop
|
||
9 years ago
|
else Propset.empty
|
||
|
else
|
||
|
(* build the pruning condition and its negation, as explained in
|
||
|
the comment above *)
|
||
|
(* build [e1] CMP [e2] *)
|
||
9 years ago
|
let cmp = if is_strict then Binop.Lt else Binop.Le in
|
||
8 years ago
|
let e1_cmp_e2 = Exp.BinOp (cmp, e1, e2) in
|
||
9 years ago
|
(* build !([e1] CMP [e2]) *)
|
||
9 years ago
|
let dual_cmp = if is_strict then Binop.Le else Binop.Lt in
|
||
8 years ago
|
let not_e1_cmp_e2 = Exp.BinOp (dual_cmp, e2, e1) in
|
||
9 years ago
|
(* take polarity into account *)
|
||
8 years ago
|
let prune_cond, not_prune_cond =
|
||
|
if positive then (e1_cmp_e2, not_e1_cmp_e2) else (not_e1_cmp_e2, e1_cmp_e2)
|
||
|
in
|
||
8 years ago
|
let is_inconsistent = Prover.check_atom tenv prop (Prop.mk_inequality tenv not_prune_cond) in
|
||
9 years ago
|
if is_inconsistent then Propset.empty
|
||
|
else
|
||
|
let footprint = !Config.footprint in
|
||
8 years ago
|
let prop_with_ineq = Prop.conjoin_eq tenv ~footprint prune_cond Exp.one prop in
|
||
|
Propset.singleton tenv prop_with_ineq
|
||
9 years ago
|
|
||
7 years ago
|
|
||
8 years ago
|
let rec prune tenv ~positive condition prop =
|
||
7 years ago
|
match Prop.exp_normalize_prop ~destructive:true tenv prop condition with
|
||
7 years ago
|
| Exp.Var _ | Exp.Lvar _ ->
|
||
|
prune_ne tenv ~positive condition Exp.zero prop
|
||
|
| Exp.Const Const.Cint i when IntLit.iszero i ->
|
||
|
if positive then Propset.empty else Propset.singleton tenv prop
|
||
|
| Exp.Const (Const.Cint _ | Const.Cstr _ | Const.Cclass _) | Exp.Sizeof _ ->
|
||
|
if positive then Propset.singleton tenv prop else Propset.empty
|
||
|
| Exp.Const _ ->
|
||
|
assert false
|
||
|
| Exp.Cast (_, condition') ->
|
||
|
prune tenv ~positive condition' prop
|
||
|
| Exp.UnOp (Unop.LNot, condition', _) ->
|
||
|
prune tenv ~positive:(not positive) condition' prop
|
||
|
| Exp.UnOp _ ->
|
||
|
assert false
|
||
|
| Exp.BinOp (Binop.Eq, e, Exp.Const Const.Cint i) when IntLit.iszero i && not (IntLit.isnull i) ->
|
||
|
prune tenv ~positive:(not positive) e prop
|
||
|
| Exp.BinOp (Binop.Eq, Exp.Const Const.Cint i, e) when IntLit.iszero i && not (IntLit.isnull i) ->
|
||
|
prune tenv ~positive:(not positive) e prop
|
||
|
| Exp.BinOp (Binop.Eq, e1, e2) ->
|
||
|
prune_ne tenv ~positive:(not positive) e1 e2 prop
|
||
|
| Exp.BinOp (Binop.Ne, e, Exp.Const Const.Cint i) when IntLit.iszero i && not (IntLit.isnull i) ->
|
||
|
prune tenv ~positive e prop
|
||
|
| Exp.BinOp (Binop.Ne, Exp.Const Const.Cint i, e) when IntLit.iszero i && not (IntLit.isnull i) ->
|
||
|
prune tenv ~positive e prop
|
||
|
| Exp.BinOp (Binop.Ne, e1, e2) ->
|
||
|
prune_ne tenv ~positive e1 e2 prop
|
||
|
| Exp.BinOp (Binop.Ge, e2, e1) | Exp.BinOp (Binop.Le, e1, e2) ->
|
||
|
prune_ineq tenv ~is_strict:false ~positive prop e1 e2
|
||
|
| Exp.BinOp (Binop.Gt, e2, e1) | Exp.BinOp (Binop.Lt, e1, e2) ->
|
||
|
prune_ineq tenv ~is_strict:true ~positive prop e1 e2
|
||
|
| Exp.BinOp (Binop.LAnd, condition1, condition2) ->
|
||
|
let pruner = if positive then prune_inter tenv else prune_union tenv in
|
||
9 years ago
|
pruner ~positive condition1 condition2 prop
|
||
7 years ago
|
| Exp.BinOp (Binop.LOr, condition1, condition2) ->
|
||
|
let pruner = if positive then prune_union tenv else prune_inter tenv in
|
||
9 years ago
|
pruner ~positive condition1 condition2 prop
|
||
7 years ago
|
| Exp.BinOp _ | Exp.Lfield _ | Exp.Lindex _ ->
|
||
|
prune_ne tenv ~positive condition Exp.zero prop
|
||
|
| Exp.Exn _ ->
|
||
|
assert false
|
||
|
| Exp.Closure _ ->
|
||
|
assert false
|
||
|
|
||
10 years ago
|
|
||
8 years ago
|
and prune_inter tenv ~positive condition1 condition2 prop =
|
||
10 years ago
|
let res = ref Propset.empty in
|
||
8 years ago
|
let pset1 = prune tenv ~positive condition1 prop in
|
||
8 years ago
|
let do_p p = res := Propset.union (prune tenv ~positive condition2 p) !res in
|
||
|
Propset.iter do_p pset1 ; !res
|
||
10 years ago
|
|
||
7 years ago
|
|
||
8 years ago
|
and prune_union tenv ~positive condition1 condition2 prop =
|
||
|
let pset1 = prune tenv ~positive condition1 prop in
|
||
|
let pset2 = prune tenv ~positive condition2 prop in
|
||
10 years ago
|
Propset.union pset1 pset2
|
||
|
|
||
7 years ago
|
|
||
10 years ago
|
let dangerous_functions =
|
||
|
let dangerous_list = ["gets"] in
|
||
8 years ago
|
ref (List.map ~f:Typ.Procname.from_string_c_fun dangerous_list)
|
||
10 years ago
|
|
||
7 years ago
|
|
||
10 years ago
|
let check_inherently_dangerous_function caller_pname callee_pname =
|
||
8 years ago
|
if List.exists ~f:(Typ.Procname.equal callee_pname) !dangerous_functions then
|
||
9 years ago
|
let exn =
|
||
|
Exceptions.Inherently_dangerous_function
|
||
8 years ago
|
(Localise.desc_inherently_dangerous_function callee_pname)
|
||
|
in
|
||
8 years ago
|
Reporting.log_warning_deprecated caller_pname exn
|
||
10 years ago
|
|
||
7 years ago
|
|
||
7 years ago
|
let reason_to_skip callee_summary : string option =
|
||
7 years ago
|
let attributes = Specs.get_attributes callee_summary in
|
||
7 years ago
|
if attributes.ProcAttributes.is_abstract then Some "abstract method"
|
||
|
else if not attributes.ProcAttributes.is_defined then Some "method has no implementation"
|
||
7 years ago
|
else if List.is_empty (Specs.get_specs_from_payload callee_summary) then
|
||
|
Some "empty list of specs"
|
||
7 years ago
|
else None
|
||
10 years ago
|
|
||
7 years ago
|
|
||
10 years ago
|
(** In case of constant string dereference, return the result immediately *)
|
||
|
let check_constant_string_dereference lexp =
|
||
|
let string_lookup s n =
|
||
7 years ago
|
let c = try Char.to_int s.[IntLit.to_int n] with Invalid_argument _ -> 0 in
|
||
8 years ago
|
Exp.int (IntLit.of_int c)
|
||
|
in
|
||
10 years ago
|
match lexp with
|
||
7 years ago
|
| Exp.BinOp (Binop.PlusPI, Exp.Const Const.Cstr s, e) | Exp.Lindex (Exp.Const Const.Cstr s, e) ->
|
||
|
let value =
|
||
8 years ago
|
match e with
|
||
|
| Exp.Const Const.Cint n
|
||
7 years ago
|
when IntLit.geq n IntLit.zero && IntLit.leq n (IntLit.of_int (String.length s)) ->
|
||
|
string_lookup s n
|
||
|
| _ ->
|
||
|
Exp.get_undefined false
|
||
8 years ago
|
in
|
||
10 years ago
|
Some value
|
||
7 years ago
|
| Exp.Const Const.Cstr s ->
|
||
|
Some (string_lookup s IntLit.zero)
|
||
|
| _ ->
|
||
|
None
|
||
|
|
||
10 years ago
|
|
||
|
(** Normalize an expression and check for arithmetic problems *)
|
||
8 years ago
|
let check_arith_norm_exp tenv pname exp prop =
|
||
|
match Attribute.find_arithmetic_problem tenv (State.get_path_pos ()) prop exp with
|
||
7 years ago
|
| Some Attribute.Div0 div, prop' ->
|
||
|
let desc = Errdesc.explain_divide_by_zero tenv div (State.get_node ()) (State.get_loc ()) in
|
||
9 years ago
|
let exn = Exceptions.Divide_by_zero (desc, __POS__) in
|
||
7 years ago
|
Reporting.log_warning_deprecated pname exn ;
|
||
|
(Prop.exp_normalize_prop tenv prop exp, prop')
|
||
|
| Some Attribute.UminusUnsigned (e, typ), prop' ->
|
||
|
let desc =
|
||
8 years ago
|
Errdesc.explain_unary_minus_applied_to_unsigned_expression tenv e typ (State.get_node ())
|
||
|
(State.get_loc ())
|
||
|
in
|
||
9 years ago
|
let exn = Exceptions.Unary_minus_applied_to_unsigned_expression (desc, __POS__) in
|
||
7 years ago
|
Reporting.log_warning_deprecated pname exn ;
|
||
|
(Prop.exp_normalize_prop tenv prop exp, prop')
|
||
|
| None, prop' ->
|
||
|
(Prop.exp_normalize_prop tenv prop exp, prop')
|
||
|
|
||
10 years ago
|
|
||
|
(** Check if [cond] is testing for NULL a pointer already dereferenced *)
|
||
8 years ago
|
let check_already_dereferenced tenv pname cond prop =
|
||
10 years ago
|
let find_hpred lhs =
|
||
8 years ago
|
List.find
|
||
|
~f:(function Sil.Hpointsto (e, _, _) -> Exp.equal e lhs | _ -> false)
|
||
|
prop.Prop.sigma
|
||
|
in
|
||
10 years ago
|
let rec is_check_zero = function
|
||
7 years ago
|
| Exp.Var id ->
|
||
|
Some id
|
||
|
| Exp.UnOp (Unop.LNot, e, _) ->
|
||
|
is_check_zero e
|
||
8 years ago
|
| Exp.BinOp ((Binop.Eq | Binop.Ne), Exp.Const Const.Cint i, Exp.Var id)
|
||
8 years ago
|
| Exp.BinOp ((Binop.Eq | Binop.Ne), Exp.Var id, Exp.Const Const.Cint i)
|
||
7 years ago
|
when IntLit.iszero i ->
|
||
|
Some id
|
||
7 years ago
|
(* These two patterns appear frequently in Prune nodes *)
|
||
|
| Exp.BinOp
|
||
|
( (Binop.Eq | Binop.Ne)
|
||
|
, Exp.BinOp (Binop.Eq, Exp.Var id, Exp.Const Const.Cint i)
|
||
|
, Exp.Const Const.Cint j )
|
||
|
| Exp.BinOp
|
||
|
( (Binop.Eq | Binop.Ne)
|
||
|
, Exp.BinOp (Binop.Eq, Exp.Const Const.Cint i, Exp.Var id)
|
||
|
, Exp.Const Const.Cint j )
|
||
7 years ago
|
when IntLit.iszero i && IntLit.iszero j ->
|
||
|
Some id
|
||
|
| _ ->
|
||
|
None
|
||
8 years ago
|
in
|
||
|
let dereferenced_line =
|
||
|
match is_check_zero cond with
|
||
|
| Some id -> (
|
||
|
match find_hpred (Prop.exp_normalize_prop tenv prop (Exp.Var id)) with
|
||
|
| Some Sil.Hpointsto (_, se, _) -> (
|
||
|
match Tabulation.find_dereference_without_null_check_in_sexp se with
|
||
7 years ago
|
| Some n ->
|
||
|
Some (id, n)
|
||
|
| None ->
|
||
|
None )
|
||
|
| _ ->
|
||
|
None )
|
||
|
| None ->
|
||
|
None
|
||
8 years ago
|
in
|
||
10 years ago
|
match dereferenced_line with
|
||
7 years ago
|
| Some (id, (n, _)) ->
|
||
|
let desc =
|
||
8 years ago
|
Errdesc.explain_null_test_after_dereference tenv (Exp.Var id) (State.get_node ()) n
|
||
|
(State.get_loc ())
|
||
|
in
|
||
|
let exn = Exceptions.Null_test_after_dereference (desc, __POS__) in
|
||
8 years ago
|
Reporting.log_warning_deprecated pname exn
|
||
7 years ago
|
| None ->
|
||
|
()
|
||
|
|
||
10 years ago
|
|
||
9 years ago
|
(** Check whether symbolic execution de-allocated a stack variable or a constant string,
|
||
|
raising an exception in that case *)
|
||
10 years ago
|
let check_deallocate_static_memory prop_after =
|
||
|
let check_deallocated_attribute = function
|
||
8 years ago
|
| Sil.Apred (Aresource ({ra_kind= Rrelease} as ra), [(Lvar pv)])
|
||
7 years ago
|
when Pvar.is_local pv || Pvar.is_global pv ->
|
||
|
let freed_desc = Errdesc.explain_deallocate_stack_var pv ra in
|
||
10 years ago
|
raise (Exceptions.Deallocate_stack_variable freed_desc)
|
||
7 years ago
|
| Sil.Apred (Aresource ({ra_kind= Rrelease} as ra), [(Const Cstr s)]) ->
|
||
|
let freed_desc = Errdesc.explain_deallocate_constant_string s ra in
|
||
10 years ago
|
raise (Exceptions.Deallocate_static_memory freed_desc)
|
||
7 years ago
|
| _ ->
|
||
|
()
|
||
8 years ago
|
in
|
||
8 years ago
|
let exp_att_list = Attribute.get_all prop_after in
|
||
7 years ago
|
List.iter ~f:check_deallocated_attribute exp_att_list ;
|
||
|
prop_after
|
||
|
|
||
10 years ago
|
|
||
|
let method_exists right_proc_name methods =
|
||
7 years ago
|
if Language.curr_language_is Java then
|
||
8 years ago
|
List.exists ~f:(fun meth_name -> Typ.Procname.equal right_proc_name meth_name) methods
|
||
8 years ago
|
else
|
||
|
(* ObjC/C++ case : The attribute map will only exist when we have code for the method or
|
||
9 years ago
|
the method has been called directly somewhere. It can still be that this is not the
|
||
|
case but we have a model for the method. *)
|
||
7 years ago
|
match Attributes.load right_proc_name with
|
||
7 years ago
|
| Some attrs ->
|
||
|
attrs.ProcAttributes.is_defined
|
||
|
| None ->
|
||
|
Specs.summary_exists_in_models right_proc_name
|
||
|
|
||
10 years ago
|
|
||
|
let resolve_method tenv class_name proc_name =
|
||
|
let found_class =
|
||
8 years ago
|
let visited = ref Typ.Name.Set.empty in
|
||
|
let rec resolve (class_name: Typ.Name.t) =
|
||
8 years ago
|
visited := Typ.Name.Set.add class_name !visited ;
|
||
|
let right_proc_name = Typ.Procname.replace_class proc_name class_name in
|
||
8 years ago
|
match Tenv.lookup tenv class_name with
|
||
8 years ago
|
| Some {methods; supers} when Typ.Name.is_class class_name
|
||
7 years ago
|
-> (
|
||
8 years ago
|
if method_exists right_proc_name methods then Some right_proc_name
|
||
10 years ago
|
else
|
||
8 years ago
|
match supers with
|
||
7 years ago
|
| super_classname :: _ ->
|
||
|
if not (Typ.Name.Set.mem super_classname !visited) then resolve super_classname
|
||
8 years ago
|
else None
|
||
7 years ago
|
| _ ->
|
||
|
None )
|
||
|
| _ ->
|
||
|
None
|
||
8 years ago
|
in
|
||
|
resolve class_name
|
||
|
in
|
||
10 years ago
|
match found_class with
|
||
7 years ago
|
| None ->
|
||
|
Logging.d_strln ("Couldn't find method in the hierarchy of type " ^ Typ.Name.name class_name) ;
|
||
9 years ago
|
proc_name
|
||
7 years ago
|
| Some proc_name ->
|
||
|
proc_name
|
||
|
|
||
10 years ago
|
|
||
9 years ago
|
let resolve_typename prop receiver_exp =
|
||
10 years ago
|
let typexp_opt =
|
||
|
let rec loop = function
|
||
7 years ago
|
| [] ->
|
||
|
None
|
||
|
| (Sil.Hpointsto (e, _, typexp)) :: _ when Exp.equal e receiver_exp ->
|
||
|
Some typexp
|
||
|
| _ :: hpreds ->
|
||
|
loop hpreds
|
||
8 years ago
|
in
|
||
|
loop prop.Prop.sigma
|
||
|
in
|
||
|
match typexp_opt with Some Exp.Sizeof {typ= {desc= Tstruct name}} -> Some name | _ -> None
|
||
10 years ago
|
|
||
7 years ago
|
|
||
7 years ago
|
(** If the dynamic type of the receiver actual T_actual is a subtype of the receiver type T_formal
|
||
9 years ago
|
in the signature of [pname], resolve [pname] to T_actual.[pname]. *)
|
||
8 years ago
|
let resolve_virtual_pname tenv prop actuals callee_pname call_flags : Typ.Procname.t list =
|
||
8 years ago
|
let resolve receiver_exp pname prop =
|
||
|
match resolve_typename prop receiver_exp with
|
||
7 years ago
|
| Some class_name ->
|
||
|
resolve_method tenv class_name pname
|
||
|
| None ->
|
||
|
pname
|
||
8 years ago
|
in
|
||
9 years ago
|
let get_receiver_typ pname fallback_typ =
|
||
9 years ago
|
match pname with
|
||
8 years ago
|
| Typ.Procname.Java pname_java
|
||
7 years ago
|
-> (
|
||
7 years ago
|
let name = Typ.Procname.Java.get_class_type_name pname_java in
|
||
8 years ago
|
match Tenv.lookup tenv name with
|
||
7 years ago
|
| Some _ ->
|
||
|
Typ.mk (Typ.Tptr (Typ.mk (Tstruct name), Pk_pointer))
|
||
|
| None ->
|
||
|
fallback_typ )
|
||
|
| _ ->
|
||
|
fallback_typ
|
||
8 years ago
|
in
|
||
9 years ago
|
let receiver_types_equal pname actual_receiver_typ =
|
||
9 years ago
|
(* the type of the receiver according to the function signature *)
|
||
|
let formal_receiver_typ = get_receiver_typ pname actual_receiver_typ in
|
||
8 years ago
|
Typ.equal formal_receiver_typ actual_receiver_typ
|
||
|
in
|
||
9 years ago
|
let do_resolve called_pname receiver_exp actual_receiver_typ =
|
||
8 years ago
|
if receiver_types_equal called_pname actual_receiver_typ then
|
||
|
resolve receiver_exp called_pname prop
|
||
|
else called_pname
|
||
|
in
|
||
9 years ago
|
match actuals with
|
||
7 years ago
|
| _ when not (call_flags.CallFlags.cf_virtual || call_flags.CallFlags.cf_interface) ->
|
||
|
(* if this is not a virtual or interface call, there's no need for resolution *)
|
||
9 years ago
|
[callee_pname]
|
||
8 years ago
|
| (receiver_exp, actual_receiver_typ) :: _
|
||
7 years ago
|
-> (
|
||
7 years ago
|
if !Language.curr_language <> Language.Java then
|
||
9 years ago
|
(* default mode for Obj-C/C++/Java virtual calls: resolution only *)
|
||
|
[do_resolve callee_pname receiver_exp actual_receiver_typ]
|
||
9 years ago
|
else
|
||
8 years ago
|
let resolved_target = do_resolve callee_pname receiver_exp actual_receiver_typ in
|
||
|
match call_flags.CallFlags.cf_targets with
|
||
|
| target :: _
|
||
|
when call_flags.CallFlags.cf_interface
|
||
|
&& receiver_types_equal callee_pname actual_receiver_typ
|
||
7 years ago
|
&& Typ.Procname.equal resolved_target callee_pname ->
|
||
|
(* "production mode" of dynamic dispatch for Java: unsound, but faster. the handling
|
||
9 years ago
|
is restricted to interfaces: if we can't resolve an interface call, we pick the
|
||
|
first implementation of the interface and call it *)
|
||
8 years ago
|
[target]
|
||
7 years ago
|
| _ ->
|
||
|
(* default mode for Java virtual calls: resolution only *)
|
||
8 years ago
|
[resolved_target] )
|
||
7 years ago
|
| _ ->
|
||
|
L.(die InternalError) "A virtual call must have a receiver"
|
||
|
|
||
9 years ago
|
|
||
|
(** Resolve the name of the procedure to call based on the type of the arguments *)
|
||
7 years ago
|
let resolve_java_pname tenv prop args pname_java call_flags : Typ.Procname.Java.t =
|
||
9 years ago
|
let resolve_from_args resolved_pname_java args =
|
||
7 years ago
|
let resolved_params =
|
||
|
List.fold2_exn
|
||
|
~f:(fun accu (arg_exp, _) name ->
|
||
|
match resolve_typename prop arg_exp with
|
||
7 years ago
|
| Some class_name ->
|
||
7 years ago
|
Typ.Name.Java.split_classname (Typ.Name.name class_name) :: accu
|
||
7 years ago
|
| None ->
|
||
7 years ago
|
name :: accu )
|
||
7 years ago
|
~init:[] args
|
||
7 years ago
|
(Typ.Procname.Java.get_parameters resolved_pname_java)
|
||
7 years ago
|
|> List.rev
|
||
|
in
|
||
7 years ago
|
Typ.Procname.Java.replace_parameters resolved_params resolved_pname_java
|
||
8 years ago
|
in
|
||
9 years ago
|
let resolved_pname_java, other_args =
|
||
7 years ago
|
let pname = Typ.Procname.Java pname_java
|
||
7 years ago
|
and parameters = Typ.Procname.Java.get_parameters pname_java in
|
||
7 years ago
|
let match_parameters args = Int.equal (List.length args) (List.length parameters) in
|
||
9 years ago
|
match args with
|
||
7 years ago
|
| [] ->
|
||
|
(pname_java, [])
|
||
|
| (first_arg, _) :: other_args when call_flags.CallFlags.cf_virtual ->
|
||
|
let resolved =
|
||
8 years ago
|
match resolve_typename prop first_arg with
|
||
|
| Some class_name -> (
|
||
7 years ago
|
match resolve_method tenv class_name pname with
|
||
7 years ago
|
| Typ.Procname.Java resolved_pname_java ->
|
||
|
resolved_pname_java
|
||
|
| _ ->
|
||
|
pname_java )
|
||
|
| None ->
|
||
|
pname_java
|
||
8 years ago
|
in
|
||
|
(resolved, other_args)
|
||
7 years ago
|
| _ :: other_args
|
||
7 years ago
|
when match_parameters other_args (* Non-virtual call, e.g. constructors or private methods *) ->
|
||
|
(pname_java, other_args)
|
||
|
| args when match_parameters args (* Static call *) ->
|
||
|
(pname_java, args)
|
||
|
| args ->
|
||
|
L.(die InternalError)
|
||
7 years ago
|
"Call mismatch: method %a has %i paramters but is called with %i arguments@."
|
||
|
Typ.Procname.pp pname (List.length parameters) (List.length args)
|
||
8 years ago
|
in
|
||
9 years ago
|
resolve_from_args resolved_pname_java other_args
|
||
9 years ago
|
|
||
7 years ago
|
|
||
9 years ago
|
(** Resolve the procedure name and run the analysis of the resolved procedure
|
||
9 years ago
|
if not already analyzed *)
|
||
7 years ago
|
let resolve_and_analyze tenv ~caller_pdesc prop args callee_proc_name call_flags
|
||
8 years ago
|
: Typ.Procname.t * Specs.summary option =
|
||
8 years ago
|
(* TODO (#15748878): Fix conflict with method overloading by encoding in the procedure name
|
||
9 years ago
|
whether the method is defined or generated by the specialization *)
|
||
8 years ago
|
let analyze_ondemand resolved_pname : Specs.summary option =
|
||
8 years ago
|
if Typ.Procname.equal resolved_pname callee_proc_name then
|
||
7 years ago
|
Ondemand.analyze_proc_name ~caller_pdesc callee_proc_name
|
||
9 years ago
|
else
|
||
9 years ago
|
(* Create the type sprecialized procedure description and analyze it directly *)
|
||
7 years ago
|
let analyze specialized_pdesc = Ondemand.analyze_proc_desc ~caller_pdesc specialized_pdesc in
|
||
8 years ago
|
let resolved_proc_desc_option =
|
||
|
match Ondemand.get_proc_desc resolved_pname with
|
||
7 years ago
|
| Some resolved_proc_desc ->
|
||
|
Some resolved_proc_desc
|
||
|
| None ->
|
||
|
Option.map
|
||
7 years ago
|
~f:(fun callee_proc_desc ->
|
||
|
Procdesc.specialize_types callee_proc_desc resolved_pname args )
|
||
8 years ago
|
(Ondemand.get_proc_desc callee_proc_name)
|
||
|
in
|
||
|
Option.bind resolved_proc_desc_option ~f:analyze
|
||
|
in
|
||
|
let resolved_pname =
|
||
|
match callee_proc_name with
|
||
7 years ago
|
| Typ.Procname.Java callee_proc_name_java ->
|
||
|
Typ.Procname.Java (resolve_java_pname tenv prop args callee_proc_name_java call_flags)
|
||
|
| _ ->
|
||
|
callee_proc_name
|
||
8 years ago
|
in
|
||
|
(resolved_pname, analyze_ondemand resolved_pname)
|
||
9 years ago
|
|
||
7 years ago
|
|
||
9 years ago
|
(** recognize calls to the constructor java.net.URL and splits the argument string
|
||
|
to be only the protocol. *)
|
||
|
let call_constructor_url_update_args pname actual_params =
|
||
9 years ago
|
let url_pname =
|
||
8 years ago
|
Typ.Procname.Java
|
||
7 years ago
|
(Typ.Procname.Java.make
|
||
7 years ago
|
(Typ.Name.Java.from_string "java.net.URL")
|
||
7 years ago
|
None "<init>" [(Some "java.lang", "String")] Typ.Procname.Java.Non_Static)
|
||
8 years ago
|
in
|
||
|
if Typ.Procname.equal url_pname pname then
|
||
|
match actual_params with
|
||
|
| [this; (Exp.Const Const.Cstr s, atype)]
|
||
7 years ago
|
-> (
|
||
8 years ago
|
let parts = Str.split (Str.regexp_string "://") s in
|
||
|
match parts with
|
||
7 years ago
|
| frst :: _ ->
|
||
|
if String.equal frst "http" || String.equal frst "ftp" || String.equal frst "https"
|
||
8 years ago
|
|| String.equal frst "mailto" || String.equal frst "jar"
|
||
|
then [this; (Exp.Const (Const.Cstr frst), atype)]
|
||
|
else actual_params
|
||
7 years ago
|
| _ ->
|
||
|
actual_params )
|
||
|
| [this; (_, atype)] ->
|
||
|
[this; (Exp.Const (Const.Cstr "file"), atype)]
|
||
|
| _ ->
|
||
|
actual_params
|
||
10 years ago
|
else actual_params
|
||
|
|
||
7 years ago
|
|
||
8 years ago
|
let receiver_self receiver prop =
|
||
8 years ago
|
List.exists
|
||
|
~f:(fun hpred ->
|
||
8 years ago
|
match hpred with
|
||
7 years ago
|
| Sil.Hpointsto (Exp.Lvar pv, Sil.Eexp (e, _), _) ->
|
||
|
Exp.equal e receiver && Pvar.is_seed pv
|
||
8 years ago
|
&& Mangled.equal (Pvar.get_name pv) (Mangled.from_string "self")
|
||
7 years ago
|
| _ ->
|
||
7 years ago
|
false )
|
||
8 years ago
|
prop.Prop.sigma
|
||
8 years ago
|
|
||
7 years ago
|
|
||
8 years ago
|
(* When current ObjC method is an initializer and the method call is also an initializer,
|
||
|
and the receiver is self, i.e. the call is [super init], then we want to assume that it
|
||
|
can return null, regardless of code or annotations, so that the next statement should be
|
||
|
a check for null, which is considered good practice. *)
|
||
|
let force_objc_init_return_nil pdesc callee_pname tenv ret_id pre path receiver =
|
||
|
let current_pname = Procdesc.get_proc_name pdesc in
|
||
8 years ago
|
if Typ.Procname.is_constructor callee_pname && receiver_self receiver pre && !Config.footprint
|
||
|
&& Typ.Procname.is_constructor current_pname
|
||
|
then
|
||
8 years ago
|
match ret_id with
|
||
7 years ago
|
| Some (ret_id, _) ->
|
||
|
let propset = prune_ne tenv ~positive:false (Exp.Var ret_id) Exp.zero pre in
|
||
8 years ago
|
if Propset.is_empty propset then []
|
||
|
else
|
||
|
let prop = List.hd_exn (Propset.to_proplist propset) in
|
||
|
[(prop, path)]
|
||
7 years ago
|
| _ ->
|
||
|
[]
|
||
8 years ago
|
else []
|
||
|
|
||
7 years ago
|
|
||
9 years ago
|
(* This method is used to handle the special semantics of ObjC instance method calls. *)
|
||
|
(* res = [obj foo] *)
|
||
|
(* 1. We know that obj is null, then we return null *)
|
||
|
(* 2. We don't know, but obj could be null, we return both options, *)
|
||
|
(* (obj = null, res = null), (obj != null, res = [obj foo]) *)
|
||
|
(* We want the same behavior even when we are going to skip the function. *)
|
||
8 years ago
|
let handle_objc_instance_method_call_or_skip pdesc tenv actual_pars path callee_pname pre ret_id
|
||
|
res =
|
||
9 years ago
|
let path_description =
|
||
7 years ago
|
F.sprintf "Message %s with receiver nil returns nil."
|
||
|
(Typ.Procname.to_simplified_string callee_pname)
|
||
8 years ago
|
in
|
||
|
let receiver =
|
||
10 years ago
|
match actual_pars with
|
||
7 years ago
|
| (e, _) :: _ ->
|
||
|
e
|
||
|
| _ ->
|
||
|
raise
|
||
8 years ago
|
(Exceptions.Internal_error
|
||
|
(Localise.verbatim_desc
|
||
|
"In Objective-C instance method call there should be a receiver."))
|
||
|
in
|
||
|
let is_receiver_null =
|
||
|
match actual_pars with
|
||
7 years ago
|
| (e, _) :: _ when Exp.equal e Exp.zero || Option.is_some (Attribute.get_objc_null tenv pre e) ->
|
||
|
true
|
||
|
| _ ->
|
||
|
false
|
||
8 years ago
|
in
|
||
10 years ago
|
let add_objc_null_attribute_or_nullify_result prop =
|
||
8 years ago
|
match ret_id with
|
||
|
| Some (ret_id, _) -> (
|
||
8 years ago
|
match Attribute.find_equal_formal_path tenv receiver prop with
|
||
7 years ago
|
| Some vfs ->
|
||
|
Attribute.add_or_replace tenv prop (Apred (Aobjc_null, [Exp.Var ret_id; vfs]))
|
||
|
| None ->
|
||
|
Prop.conjoin_eq tenv (Exp.Var ret_id) Exp.zero prop )
|
||
|
| _ ->
|
||
|
prop
|
||
8 years ago
|
in
|
||
7 years ago
|
if is_receiver_null then (
|
||
8 years ago
|
(* objective-c instance method with a null receiver just return objc_null(res). *)
|
||
10 years ago
|
let path = Paths.Path.add_description path path_description in
|
||
9 years ago
|
L.d_strln
|
||
7 years ago
|
(F.sprintf "Object-C method %s called with nil receiver. Returning 0/nil"
|
||
|
(Typ.Procname.to_string callee_pname)) ;
|
||
9 years ago
|
(* We wish to nullify the result. However, in some cases,
|
||
7 years ago
|
we want to add the attribute OBJC_NULL to it so that we
|
||
|
can keep track of how this object became null,
|
||
9 years ago
|
so that in a NPE we can separate it into a different error type *)
|
||
7 years ago
|
[(add_objc_null_attribute_or_nullify_result pre, path)] )
|
||
10 years ago
|
else
|
||
8 years ago
|
match force_objc_init_return_nil pdesc callee_pname tenv ret_id pre path receiver with
|
||
7 years ago
|
| [] ->
|
||
|
if !Config.footprint && Option.is_none (Attribute.get_undef tenv pre receiver)
|
||
8 years ago
|
&& not (Rearrange.is_only_pt_by_fld_or_param_nonnull pdesc tenv pre receiver)
|
||
|
then
|
||
|
let res_null =
|
||
|
(* returns: (objc_null(res) /\ receiver=0) or an empty list of results *)
|
||
8 years ago
|
let pre_with_attr_or_null = add_objc_null_attribute_or_nullify_result pre in
|
||
|
let propset = prune_ne tenv ~positive:false receiver Exp.zero pre_with_attr_or_null in
|
||
|
if Propset.is_empty propset then []
|
||
|
else
|
||
|
let prop = List.hd_exn (Propset.to_proplist propset) in
|
||
|
let path = Paths.Path.add_description path path_description in
|
||
8 years ago
|
[(prop, path)]
|
||
|
in
|
||
8 years ago
|
List.append res_null (res ())
|
||
8 years ago
|
else res ()
|
||
|
(* Not known if receiver = 0 and not footprint. Standard tabulation *)
|
||
7 years ago
|
| res_null ->
|
||
|
List.append res_null (res ())
|
||
|
|
||
9 years ago
|
|
||
|
(* This method handles ObjC instance method calls, in particular the fact that calling a method *)
|
||
|
(* with nil returns nil. The exec_call function is either standard call execution or execution *)
|
||
|
(* of ObjC getters and setters using a builtin. *)
|
||
8 years ago
|
let handle_objc_instance_method_call actual_pars actual_params pre tenv ret_id pdesc callee_pname
|
||
9 years ago
|
loc path exec_call =
|
||
8 years ago
|
let res () = exec_call tenv ret_id pdesc callee_pname loc actual_params pre path in
|
||
8 years ago
|
handle_objc_instance_method_call_or_skip pdesc tenv actual_pars path callee_pname pre ret_id res
|
||
10 years ago
|
|
||
7 years ago
|
|
||
8 years ago
|
let normalize_params tenv pdesc prop actual_params =
|
||
10 years ago
|
let norm_arg (p, args) (e, t) =
|
||
8 years ago
|
let e', p' = check_arith_norm_exp tenv pdesc e p in
|
||
8 years ago
|
(p', (e', t) :: args)
|
||
|
in
|
||
8 years ago
|
let prop, args = List.fold ~f:norm_arg ~init:(prop, []) actual_params in
|
||
8 years ago
|
(prop, List.rev args)
|
||
10 years ago
|
|
||
7 years ago
|
|
||
8 years ago
|
let add_strexp_to_footprint tenv strexp abduced_pv typ prop =
|
||
|
let abduced_lvar = Exp.Lvar abduced_pv in
|
||
9 years ago
|
let lvar_pt_fpvar =
|
||
8 years ago
|
let sizeof_exp =
|
||
|
Exp.Sizeof {typ; nbytes= None; dynamic_length= None; subtype= Subtype.subtypes}
|
||
|
in
|
||
|
Prop.mk_ptsto tenv abduced_lvar strexp sizeof_exp
|
||
|
in
|
||
8 years ago
|
let sigma_fp = prop.Prop.sigma_fp in
|
||
8 years ago
|
Prop.normalize tenv (Prop.set prop ~sigma_fp:(lvar_pt_fpvar :: sigma_fp))
|
||
9 years ago
|
|
||
7 years ago
|
|
||
8 years ago
|
let add_to_footprint tenv abduced_pv typ prop =
|
||
8 years ago
|
let fresh_fp_var = Exp.Var (Ident.create_fresh Ident.kfootprint) in
|
||
8 years ago
|
let prop' =
|
||
8 years ago
|
add_strexp_to_footprint tenv (Sil.Eexp (fresh_fp_var, Sil.Inone)) abduced_pv typ prop
|
||
|
in
|
||
|
(prop', fresh_fp_var)
|
||
9 years ago
|
|
||
7 years ago
|
|
||
9 years ago
|
(* the current abduction mechanism treats struct values differently than all other types. abduction
|
||
|
on struct values adds a a struct whose fields are initialized to fresh footprint vars to the
|
||
|
footprint. regular abduction just adds a fresh footprint value of the correct type to the
|
||
|
footprint. we can get rid of this special case if we fix the abduction on struct values *)
|
||
8 years ago
|
let add_struct_value_to_footprint tenv abduced_pv typ prop =
|
||
8 years ago
|
let struct_strexp = Prop.create_strexp_of_type tenv Prop.Fld_init typ None Sil.inst_none in
|
||
8 years ago
|
let prop' = add_strexp_to_footprint tenv struct_strexp abduced_pv typ prop in
|
||
8 years ago
|
(prop', struct_strexp)
|
||
9 years ago
|
|
||
7 years ago
|
|
||
7 years ago
|
let is_rec_call callee_pname caller_pdesc =
|
||
|
(* TODO: (t7147096) extend this to detect mutual recursion *)
|
||
|
Typ.Procname.equal callee_pname (Procdesc.get_proc_name caller_pdesc)
|
||
|
|
||
|
|
||
7 years ago
|
let add_constraints_on_retval tenv pdesc prop ret_exp ~has_nonnull_annot typ callee_pname
|
||
8 years ago
|
callee_loc =
|
||
8 years ago
|
if Typ.Procname.is_infer_undefined callee_pname then prop
|
||
9 years ago
|
else
|
||
7 years ago
|
let lookup_abduced_expression p abduced_ret_pv =
|
||
|
List.find_map
|
||
8 years ago
|
~f:(fun hpred ->
|
||
|
match hpred with
|
||
7 years ago
|
| Sil.Hpointsto (Exp.Lvar pv, _, exp) when Pvar.equal pv abduced_ret_pv ->
|
||
|
Some exp
|
||
|
| _ ->
|
||
7 years ago
|
None )
|
||
8 years ago
|
p.Prop.sigma_fp
|
||
|
in
|
||
8 years ago
|
(* find an hpred [abduced] |-> A in [prop] and add [exp] = A to prop *)
|
||
|
let bind_exp_to_abduced_val exp_to_bind abduced prop =
|
||
9 years ago
|
let bind_exp prop = function
|
||
7 years ago
|
| Sil.Hpointsto (Exp.Lvar pv, Sil.Eexp (rhs, _), _) when Pvar.equal pv abduced ->
|
||
|
Prop.conjoin_eq tenv exp_to_bind rhs prop
|
||
|
| _ ->
|
||
|
prop
|
||
8 years ago
|
in
|
||
|
List.fold ~f:bind_exp ~init:prop prop.Prop.sigma
|
||
|
in
|
||
9 years ago
|
(* To avoid obvious false positives, assume skip functions do not return null pointers *)
|
||
|
let add_ret_non_null exp typ prop =
|
||
7 years ago
|
if has_nonnull_annot then
|
||
8 years ago
|
match typ.Typ.desc with Typ.Tptr _ -> Prop.conjoin_neq tenv exp Exp.zero prop | _ -> prop
|
||
7 years ago
|
else prop
|
||
8 years ago
|
in
|
||
7 years ago
|
if not (is_rec_call callee_pname pdesc) then
|
||
9 years ago
|
(* introduce a fresh program variable to allow abduction on the return value *)
|
||
7 years ago
|
let prop_with_abduced_var =
|
||
|
let abduced_ret_pv =
|
||
|
(* in Java, always re-use the same abduced ret var to prevent false alarms with repeated method calls *)
|
||
|
let loc = if Typ.Procname.is_java callee_pname then Location.dummy else callee_loc in
|
||
|
Pvar.mk_abduced_ret callee_pname loc
|
||
8 years ago
|
in
|
||
7 years ago
|
if !Config.footprint then
|
||
|
match lookup_abduced_expression prop abduced_ret_pv with
|
||
7 years ago
|
| None ->
|
||
|
let p, fp_var = add_to_footprint tenv abduced_ret_pv typ prop in
|
||
7 years ago
|
Prop.conjoin_eq tenv ~footprint:true ret_exp fp_var p
|
||
7 years ago
|
| Some exp ->
|
||
|
Prop.conjoin_eq tenv ~footprint:true ret_exp exp prop
|
||
7 years ago
|
else
|
||
|
(* bind return id to the abduced value pointed to by the pvar we introduced *)
|
||
|
bind_exp_to_abduced_val ret_exp abduced_ret_pv prop
|
||
|
in
|
||
|
add_ret_non_null ret_exp typ prop_with_abduced_var
|
||
9 years ago
|
else add_ret_non_null ret_exp typ prop
|
||
|
|
||
7 years ago
|
|
||
8 years ago
|
let execute_load ?(report_deref_errors= true) pname pdesc tenv id rhs_exp typ loc prop_ =
|
||
7 years ago
|
let execute_load_ acc_in iter =
|
||
8 years ago
|
let iter_ren = Prop.prop_iter_make_id_primed tenv id iter in
|
||
|
let prop_ren = Prop.prop_iter_to_prop tenv iter_ren in
|
||
|
match Prop.prop_iter_current tenv iter_ren with
|
||
8 years ago
|
| Sil.Hpointsto (lexp, strexp, Exp.Sizeof sizeof_data), offlist
|
||
7 years ago
|
-> (
|
||
9 years ago
|
let contents, new_ptsto, pred_insts_op, lookup_uninitialized =
|
||
8 years ago
|
ptsto_lookup pdesc tenv prop_ren (lexp, strexp, sizeof_data) offlist id
|
||
|
in
|
||
9 years ago
|
let update acc (pi, sigma) =
|
||
8 years ago
|
let pi' = Sil.Aeq (Exp.Var id, contents) :: pi in
|
||
|
let sigma' = new_ptsto :: sigma in
|
||
9 years ago
|
let iter' = update_iter iter_ren pi' sigma' in
|
||
8 years ago
|
let prop' = Prop.prop_iter_to_prop tenv iter' in
|
||
9 years ago
|
let prop'' =
|
||
|
if lookup_uninitialized then
|
||
8 years ago
|
Attribute.add_or_replace tenv prop' (Apred (Adangling DAuninit, [Exp.Var id]))
|
||
8 years ago
|
else prop'
|
||
|
in
|
||
7 years ago
|
prop'' :: acc
|
||
8 years ago
|
in
|
||
|
match pred_insts_op with
|
||
7 years ago
|
| None ->
|
||
|
update acc_in ([], [])
|
||
|
| Some pred_insts ->
|
||
|
List.rev (List.fold ~f:update ~init:acc_in pred_insts) )
|
||
|
| Sil.Hpointsto _, _ ->
|
||
|
Errdesc.warning_err loc "no offset access in execute_load -- treating as skip@." ;
|
||
8 years ago
|
Prop.prop_iter_to_prop tenv iter_ren :: acc_in
|
||
7 years ago
|
| _ ->
|
||
|
(* The implementation of this case means that we
|
||
9 years ago
|
ignore this dereferencing operator. When the analyzer treats
|
||
|
numerical information and arrays more precisely later, we
|
||
|
should change the implementation here. *)
|
||
8 years ago
|
assert false
|
||
|
in
|
||
9 years ago
|
try
|
||
8 years ago
|
let n_rhs_exp, prop = check_arith_norm_exp tenv pname rhs_exp prop_ in
|
||
8 years ago
|
let n_rhs_exp' = Prop.exp_collapse_consecutive_indices_prop typ n_rhs_exp in
|
||
9 years ago
|
match check_constant_string_dereference n_rhs_exp' with
|
||
7 years ago
|
| Some value ->
|
||
|
[Prop.conjoin_eq tenv (Exp.Var id) value prop]
|
||
7 years ago
|
| None ->
|
||
|
try
|
||
9 years ago
|
let iter_list =
|
||
7 years ago
|
Rearrange.rearrange ~report_deref_errors pdesc tenv n_rhs_exp' typ prop loc
|
||
8 years ago
|
in
|
||
7 years ago
|
List.rev (List.fold ~f:execute_load_ ~init:[] iter_list)
|
||
7 years ago
|
with Exceptions.Symexec_memory_error _ ->
|
||
|
(* This should normally be a real alarm and should not be caught but currently happens
|
||
|
when the normalization drops hpreds of the form ident |-> footprint var. *)
|
||
|
let undef = Exp.get_undefined !Config.footprint in
|
||
|
[Prop.conjoin_eq tenv (Exp.Var id) undef prop]
|
||
9 years ago
|
with Rearrange.ARRAY_ACCESS ->
|
||
8 years ago
|
if Int.equal Config.array_level 0 then assert false
|
||
9 years ago
|
else
|
||
8 years ago
|
let undef = Exp.get_undefined false in
|
||
8 years ago
|
[Prop.conjoin_eq tenv (Exp.Var id) undef prop_]
|
||
9 years ago
|
|
||
7 years ago
|
|
||
9 years ago
|
let load_ret_annots pname =
|
||
7 years ago
|
match Attributes.load pname with
|
||
7 years ago
|
| Some attrs ->
|
||
|
let ret_annots, _ = attrs.ProcAttributes.method_annotation in
|
||
9 years ago
|
ret_annots
|
||
7 years ago
|
| None ->
|
||
|
Annot.Item.empty
|
||
|
|
||
9 years ago
|
|
||
8 years ago
|
let execute_store ?(report_deref_errors= true) pname pdesc tenv lhs_exp typ rhs_exp loc prop_ =
|
||
8 years ago
|
let execute_store_ pdesc tenv rhs_exp acc_in iter =
|
||
8 years ago
|
let lexp, strexp, sizeof, offlist =
|
||
8 years ago
|
match Prop.prop_iter_current tenv iter with
|
||
7 years ago
|
| Sil.Hpointsto (lexp, strexp, Exp.Sizeof sizeof), offlist ->
|
||
|
(lexp, strexp, sizeof, offlist)
|
||
|
| _ ->
|
||
|
assert false
|
||
8 years ago
|
in
|
||
8 years ago
|
let p = Prop.prop_iter_to_prop tenv iter in
|
||
9 years ago
|
let new_ptsto, pred_insts_op =
|
||
8 years ago
|
ptsto_update pdesc tenv p (lexp, strexp, sizeof) offlist rhs_exp
|
||
|
in
|
||
9 years ago
|
let update acc (pi, sigma) =
|
||
8 years ago
|
let sigma' = new_ptsto :: sigma in
|
||
9 years ago
|
let iter' = update_iter iter pi sigma' in
|
||
8 years ago
|
let prop' = Prop.prop_iter_to_prop tenv iter' in
|
||
8 years ago
|
prop' :: acc
|
||
|
in
|
||
9 years ago
|
match pred_insts_op with
|
||
7 years ago
|
| None ->
|
||
|
update acc_in ([], [])
|
||
|
| Some pred_insts ->
|
||
|
List.fold ~f:update ~init:acc_in pred_insts
|
||
8 years ago
|
in
|
||
9 years ago
|
try
|
||
8 years ago
|
let n_lhs_exp, prop_' = check_arith_norm_exp tenv pname lhs_exp prop_ in
|
||
|
let n_rhs_exp, prop = check_arith_norm_exp tenv pname rhs_exp prop_' in
|
||
|
let prop = Attribute.replace_objc_null tenv prop n_lhs_exp n_rhs_exp in
|
||
8 years ago
|
let n_lhs_exp' = Prop.exp_collapse_consecutive_indices_prop typ n_lhs_exp in
|
||
9 years ago
|
let iter_list = Rearrange.rearrange ~report_deref_errors pdesc tenv n_lhs_exp' typ prop loc in
|
||
7 years ago
|
let prop_list =
|
||
|
List.rev (List.fold ~f:(execute_store_ pdesc tenv n_rhs_exp) ~init:[] iter_list)
|
||
|
in
|
||
|
if !Config.footprint then List.iter ~f:(RetainCycles.report_cycle tenv pname) prop_list ;
|
||
|
prop_list
|
||
8 years ago
|
with Rearrange.ARRAY_ACCESS -> if Int.equal Config.array_level 0 then assert false else [prop_]
|
||
9 years ago
|
|
||
7 years ago
|
|
||
10 years ago
|
(** Execute [instr] with a symbolic heap [prop].*)
|
||
7 years ago
|
let rec sym_exec exe_env tenv current_pdesc instr_ (prop_: Prop.normal Prop.t) path
|
||
8 years ago
|
: (Prop.normal Prop.t * Paths.Path.t) list =
|
||
8 years ago
|
let current_pname = Procdesc.get_proc_name current_pdesc in
|
||
7 years ago
|
State.set_instr instr_ ;
|
||
8 years ago
|
(* mark instruction last seen *)
|
||
|
State.set_prop_tenv_pdesc prop_ tenv current_pdesc ;
|
||
|
(* mark prop,tenv,pdesc last seen *)
|
||
|
SymOp.pay () ;
|
||
|
(* pay one symop *)
|
||
|
let ret_old_path pl =
|
||
|
(* return the old path unchanged *)
|
||
|
List.map ~f:(fun p -> (p, path)) pl
|
||
|
in
|
||
|
let instr =
|
||
7 years ago
|
match instr_ with
|
||
7 years ago
|
| Sil.Call (ret, exp, par, loc, call_flags) ->
|
||
|
let exp' = Prop.exp_normalize_prop tenv prop_ exp in
|
||
8 years ago
|
let instr' =
|
||
|
match exp' with
|
||
7 years ago
|
| Exp.Closure c ->
|
||
|
let proc_exp = Exp.Const (Const.Cfun c.name) in
|
||
8 years ago
|
let proc_exp' = Prop.exp_normalize_prop tenv prop_ proc_exp in
|
||
8 years ago
|
let par' = List.map ~f:(fun (id_exp, _, typ) -> (id_exp, typ)) c.captured_vars in
|
||
9 years ago
|
Sil.Call (ret, proc_exp', par' @ par, loc, call_flags)
|
||
7 years ago
|
| _ ->
|
||
|
Sil.Call (ret, exp', par, loc, call_flags)
|
||
8 years ago
|
in
|
||
10 years ago
|
instr'
|
||
7 years ago
|
| _ ->
|
||
7 years ago
|
instr_
|
||
8 years ago
|
in
|
||
7 years ago
|
let skip_call ?(is_objc_instance_method= false) ~reason prop path callee_pname ret_annots loc
|
||
|
ret_id ret_typ_opt actual_args =
|
||
9 years ago
|
let skip_res () =
|
||
|
let exn = Exceptions.Skip_function (Localise.desc_skip_function callee_pname) in
|
||
8 years ago
|
Reporting.log_info_deprecated current_pname exn ;
|
||
9 years ago
|
L.d_strln
|
||
7 years ago
|
(F.sprintf "Undefined function %s, returning undefined value."
|
||
|
(Typ.Procname.to_string callee_pname)) ;
|
||
8 years ago
|
( match Specs.get_summary current_pname with
|
||
7 years ago
|
| None ->
|
||
|
()
|
||
|
| Some summary ->
|
||
7 years ago
|
let proc_name = Specs.get_proc_name summary in
|
||
7 years ago
|
Tabulation.log_call_trace proc_name callee_pname ~reason loc Tabulation.CR_skip ) ;
|
||
7 years ago
|
unknown_or_scan_call ~is_scan:false ~reason ret_typ_opt ret_annots
|
||
7 years ago
|
Builtin.
|
||
8 years ago
|
{ pdesc= current_pdesc
|
||
|
; instr
|
||
|
; tenv
|
||
|
; prop_= prop
|
||
|
; path
|
||
|
; ret_id
|
||
|
; args= actual_args
|
||
|
; proc_name= callee_pname
|
||
7 years ago
|
; loc
|
||
|
; exe_env }
|
||
8 years ago
|
in
|
||
9 years ago
|
if is_objc_instance_method then
|
||
8 years ago
|
handle_objc_instance_method_call_or_skip current_pdesc tenv actual_args path callee_pname
|
||
|
prop ret_id skip_res
|
||
|
else skip_res ()
|
||
|
in
|
||
|
let call_args prop_ proc_name args ret_id loc =
|
||
7 years ago
|
{Builtin.pdesc= current_pdesc; instr; tenv; prop_; path; ret_id; args; proc_name; loc; exe_env}
|
||
8 years ago
|
in
|
||
10 years ago
|
match instr with
|
||
7 years ago
|
| Sil.Load (id, rhs_exp, typ, loc) ->
|
||
|
execute_load current_pname current_pdesc tenv id rhs_exp typ loc prop_ |> ret_old_path
|
||
|
| Sil.Store (lhs_exp, typ, rhs_exp, loc) ->
|
||
|
execute_store current_pname current_pdesc tenv lhs_exp typ rhs_exp loc prop_ |> ret_old_path
|
||
|
| Sil.Prune (cond, loc, true_branch, ik) ->
|
||
|
let prop__ = Attribute.nullify_exp_with_objc_null tenv prop_ cond in
|
||
10 years ago
|
let check_condition_always_true_false () =
|
||
7 years ago
|
if !Language.curr_language <> Language.Clang
|
||
|
|| Config.report_condition_always_true_in_clang
|
||
8 years ago
|
then
|
||
8 years ago
|
let report_condition_always_true_false i =
|
||
8 years ago
|
let skip_loop =
|
||
|
match ik with
|
||
7 years ago
|
| Sil.Ik_while | Sil.Ik_for ->
|
||
|
not (IntLit.iszero i) (* skip wile(1) and for (;1;) *)
|
||
|
| Sil.Ik_dowhile ->
|
||
|
true (* skip do..while *)
|
||
|
| Sil.Ik_land_lor ->
|
||
|
true (* skip subpart of a condition obtained from compilation of && and || *)
|
||
|
| _ ->
|
||
|
false
|
||
8 years ago
|
in
|
||
|
true_branch && not skip_loop
|
||
|
in
|
||
8 years ago
|
match Prop.exp_normalize_prop tenv Prop.prop_emp cond with
|
||
7 years ago
|
| Exp.Const Const.Cint i when report_condition_always_true_false i ->
|
||
|
let node = State.get_node () in
|
||
8 years ago
|
let desc = Errdesc.explain_condition_always_true_false tenv i cond node loc in
|
||
|
let exn =
|
||
8 years ago
|
Exceptions.Condition_always_true_false (desc, not (IntLit.iszero i), __POS__)
|
||
|
in
|
||
8 years ago
|
Reporting.log_warning_deprecated current_pname exn
|
||
7 years ago
|
| _ ->
|
||
|
()
|
||
8 years ago
|
in
|
||
7 years ago
|
if not (Config.tracing || Typ.Procname.is_java current_pname) then
|
||
|
check_already_dereferenced tenv current_pname cond prop__ ;
|
||
8 years ago
|
check_condition_always_true_false () ;
|
||
8 years ago
|
let n_cond, prop = check_arith_norm_exp tenv current_pname cond prop__ in
|
||
|
ret_old_path (Propset.to_proplist (prune tenv ~positive:true n_cond prop))
|
||
8 years ago
|
| Sil.Call (ret_id, Exp.Const Const.Cfun callee_pname, actual_params, loc, call_flags) -> (
|
||
|
match Builtin.get callee_pname with
|
||
7 years ago
|
| Some exec_builtin ->
|
||
|
exec_builtin (call_args prop_ callee_pname actual_params ret_id loc)
|
||
8 years ago
|
| None ->
|
||
|
match callee_pname with
|
||
7 years ago
|
| Java callee_pname_java when Config.dynamic_dispatch
|
||
7 years ago
|
-> (
|
||
8 years ago
|
let norm_prop, norm_args' = normalize_params tenv current_pname prop_ actual_params in
|
||
|
let norm_args = call_constructor_url_update_args callee_pname norm_args' in
|
||
7 years ago
|
let exec_skip_call ~reason skipped_pname ret_annots ret_type =
|
||
|
skip_call ~reason norm_prop path skipped_pname ret_annots loc ret_id (Some ret_type)
|
||
|
norm_args
|
||
8 years ago
|
in
|
||
|
let resolved_pname, resolved_summary_opt =
|
||
7 years ago
|
resolve_and_analyze tenv ~caller_pdesc:current_pdesc norm_prop norm_args callee_pname
|
||
|
call_flags
|
||
8 years ago
|
in
|
||
|
match resolved_summary_opt with
|
||
7 years ago
|
| None ->
|
||
7 years ago
|
let ret_typ = Typ.Procname.Java.get_return_typ callee_pname_java in
|
||
8 years ago
|
let ret_annots = load_ret_annots callee_pname in
|
||
7 years ago
|
exec_skip_call ~reason:"unknown method" resolved_pname ret_annots ret_typ
|
||
|
| Some resolved_summary ->
|
||
|
match reason_to_skip resolved_summary with
|
||
7 years ago
|
| None ->
|
||
7 years ago
|
proc_call exe_env resolved_summary
|
||
|
(call_args prop_ callee_pname norm_args ret_id loc)
|
||
7 years ago
|
| Some reason ->
|
||
7 years ago
|
let proc_attrs = Specs.get_attributes resolved_summary in
|
||
7 years ago
|
let ret_annots, _ = proc_attrs.ProcAttributes.method_annotation in
|
||
|
exec_skip_call ~reason resolved_pname ret_annots proc_attrs.ProcAttributes.ret_type
|
||
|
)
|
||
7 years ago
|
| Java callee_pname_java ->
|
||
|
let norm_prop, norm_args = normalize_params tenv current_pname prop_ actual_params in
|
||
8 years ago
|
let url_handled_args = call_constructor_url_update_args callee_pname norm_args in
|
||
|
let resolved_pnames =
|
||
|
resolve_virtual_pname tenv norm_prop url_handled_args callee_pname call_flags
|
||
|
in
|
||
|
let exec_one_pname pname =
|
||
7 years ago
|
let exec_skip_call ~reason ret_annots ret_type =
|
||
|
skip_call ~reason norm_prop path pname ret_annots loc ret_id (Some ret_type)
|
||
|
url_handled_args
|
||
8 years ago
|
in
|
||
7 years ago
|
match Ondemand.analyze_proc_name ~caller_pdesc:current_pdesc pname with
|
||
7 years ago
|
| None ->
|
||
7 years ago
|
let ret_typ = Typ.Procname.Java.get_return_typ callee_pname_java in
|
||
8 years ago
|
let ret_annots = load_ret_annots callee_pname in
|
||
7 years ago
|
exec_skip_call ~reason:"unknown method" ret_annots ret_typ
|
||
|
| Some callee_summary ->
|
||
|
match reason_to_skip callee_summary with
|
||
7 years ago
|
| None ->
|
||
|
let handled_args = call_args norm_prop pname url_handled_args ret_id loc in
|
||
7 years ago
|
proc_call exe_env callee_summary handled_args
|
||
7 years ago
|
| Some reason ->
|
||
7 years ago
|
let proc_attrs = Specs.get_attributes callee_summary in
|
||
7 years ago
|
let ret_annots, _ = proc_attrs.ProcAttributes.method_annotation in
|
||
|
exec_skip_call ~reason ret_annots proc_attrs.ProcAttributes.ret_type
|
||
8 years ago
|
in
|
||
|
List.fold ~f:(fun acc pname -> exec_one_pname pname @ acc) ~init:[] resolved_pnames
|
||
7 years ago
|
| _ ->
|
||
|
(* Generic fun call with known name *)
|
||
8 years ago
|
let prop_r, n_actual_params = normalize_params tenv current_pname prop_ actual_params in
|
||
|
let resolved_pname =
|
||
|
match resolve_virtual_pname tenv prop_r n_actual_params callee_pname call_flags with
|
||
7 years ago
|
| resolved_pname :: _ ->
|
||
|
resolved_pname
|
||
|
| [] ->
|
||
|
callee_pname
|
||
8 years ago
|
in
|
||
7 years ago
|
(* method with block parameters *)
|
||
|
let with_block_parameters_summary_opt =
|
||
|
if call_flags.CallFlags.cf_with_block_parameters then
|
||
7 years ago
|
SymExecBlocks.resolve_method_with_block_args_and_analyze ~caller_pdesc:current_pdesc
|
||
|
resolved_pname actual_params
|
||
7 years ago
|
else None
|
||
8 years ago
|
in
|
||
7 years ago
|
match with_block_parameters_summary_opt with
|
||
|
| Some (resolved_summary, extended_actual_params) ->
|
||
|
let prop_r, n_extended_actual_params =
|
||
|
normalize_params tenv current_pname prop_r extended_actual_params
|
||
8 years ago
|
in
|
||
7 years ago
|
Logging.d_strln "Calling method specialized with blocks... " ;
|
||
7 years ago
|
proc_call exe_env resolved_summary
|
||
7 years ago
|
(call_args prop_r resolved_pname n_extended_actual_params ret_id loc)
|
||
|
| None ->
|
||
|
(* Generic fun call with known name *)
|
||
7 years ago
|
let resolved_summary_opt =
|
||
|
Ondemand.analyze_proc_name ~caller_pdesc:current_pdesc resolved_pname
|
||
|
in
|
||
7 years ago
|
let callee_pdesc_opt = Ondemand.get_proc_desc resolved_pname in
|
||
|
let ret_typ_opt = Option.map ~f:Procdesc.get_ret_type callee_pdesc_opt in
|
||
|
let sentinel_result =
|
||
7 years ago
|
if Language.curr_language_is Clang then
|
||
7 years ago
|
check_variadic_sentinel_if_present
|
||
|
(call_args prop_r callee_pname actual_params ret_id loc)
|
||
|
else [(prop_r, path)]
|
||
8 years ago
|
in
|
||
7 years ago
|
let do_call (prop, path) =
|
||
|
if Option.value_map
|
||
|
~f:(fun summary -> is_some (reason_to_skip summary))
|
||
|
~default:true resolved_summary_opt
|
||
|
then
|
||
7 years ago
|
let ret_annots =
|
||
|
match resolved_summary_opt with
|
||
|
| Some summ ->
|
||
|
let ret_annots, _ =
|
||
|
(Specs.get_attributes summ).ProcAttributes.method_annotation
|
||
|
in
|
||
|
ret_annots
|
||
7 years ago
|
| None ->
|
||
7 years ago
|
load_ret_annots resolved_pname
|
||
8 years ago
|
in
|
||
7 years ago
|
let attrs_opt = Option.map ~f:Procdesc.get_attributes callee_pdesc_opt in
|
||
|
match attrs_opt with
|
||
|
| Some attrs
|
||
|
-> (
|
||
|
let ret_type = attrs.ProcAttributes.ret_type in
|
||
|
let model_as_malloc = Objc_models.is_malloc_model ret_type callee_pname in
|
||
|
match (attrs.ProcAttributes.objc_accessor, model_as_malloc) with
|
||
|
| Some objc_accessor, _ ->
|
||
|
(* If it's an ObjC getter or setter, call the builtin rather than skipping *)
|
||
|
handle_objc_instance_method_call n_actual_params n_actual_params prop
|
||
|
tenv ret_id current_pdesc callee_pname loc path
|
||
|
(sym_exec_objc_accessor objc_accessor ret_type)
|
||
|
| None, true ->
|
||
|
(* If it's an alloc model, call alloc rather than skipping *)
|
||
7 years ago
|
sym_exec_alloc_model exe_env callee_pname ret_type tenv ret_id
|
||
|
current_pdesc loc prop path
|
||
7 years ago
|
| None, false ->
|
||
|
let is_objc_instance_method =
|
||
7 years ago
|
ProcAttributes.equal_clang_method_kind
|
||
7 years ago
|
attrs.ProcAttributes.clang_method_kind ProcAttributes.OBJC_INSTANCE
|
||
7 years ago
|
in
|
||
|
skip_call ~is_objc_instance_method ~reason:"function or method not found"
|
||
|
prop path resolved_pname ret_annots loc ret_id ret_typ_opt
|
||
|
n_actual_params )
|
||
|
| None ->
|
||
7 years ago
|
skip_call ~reason:"function or method not found" prop path resolved_pname
|
||
|
ret_annots loc ret_id ret_typ_opt n_actual_params
|
||
7 years ago
|
else
|
||
7 years ago
|
proc_call exe_env
|
||
7 years ago
|
(Option.value_exn resolved_summary_opt)
|
||
|
(call_args prop resolved_pname n_actual_params ret_id loc)
|
||
|
in
|
||
|
List.concat_map ~f:do_call sentinel_result )
|
||
7 years ago
|
| Sil.Call (ret_id, fun_exp, actual_params, loc, call_flags) ->
|
||
|
(* Call via function pointer *)
|
||
8 years ago
|
let prop_r, n_actual_params = normalize_params tenv current_pname prop_ actual_params in
|
||
|
if call_flags.CallFlags.cf_is_objc_block
|
||
|
&& not (Rearrange.is_only_pt_by_fld_or_param_nonnull current_pdesc tenv prop_r fun_exp)
|
||
|
then Rearrange.check_call_to_objc_block_error tenv current_pdesc prop_r fun_exp loc ;
|
||
|
Rearrange.check_dereference_error tenv current_pdesc prop_r fun_exp loc ;
|
||
|
if call_flags.CallFlags.cf_noreturn then (
|
||
|
L.d_str "Unknown function pointer with noreturn attribute " ;
|
||
|
Sil.d_exp fun_exp ;
|
||
|
L.d_strln ", diverging." ;
|
||
|
diverge prop_r path )
|
||
|
else (
|
||
|
L.d_str "Unknown function pointer " ;
|
||
|
Sil.d_exp fun_exp ;
|
||
|
L.d_strln ", returning undefined value." ;
|
||
8 years ago
|
let callee_pname = Typ.Procname.from_string_c_fun "__function_pointer__" in
|
||
7 years ago
|
unknown_or_scan_call ~is_scan:false ~reason:"unresolved function pointer" None
|
||
|
Annot.Item.empty
|
||
7 years ago
|
Builtin.
|
||
8 years ago
|
{ pdesc= current_pdesc
|
||
|
; instr
|
||
|
; tenv
|
||
|
; prop_= prop_r
|
||
|
; path
|
||
|
; ret_id
|
||
|
; args= n_actual_params
|
||
|
; proc_name= callee_pname
|
||
7 years ago
|
; loc
|
||
|
; exe_env } )
|
||
8 years ago
|
| Sil.Nullify (pvar, _)
|
||
7 years ago
|
-> (
|
||
8 years ago
|
let eprop = Prop.expose prop_ in
|
||
|
match
|
||
|
List.partition_tf
|
||
|
~f:(function
|
||
|
| Sil.Hpointsto (Exp.Lvar pvar', _, _) -> Pvar.equal pvar pvar' | _ -> false)
|
||
|
eprop.Prop.sigma
|
||
|
with
|
||
7 years ago
|
| [(Sil.Hpointsto (e, se, typ))], sigma' ->
|
||
|
let sigma'' =
|
||
8 years ago
|
let se' = execute_nullify_se se in
|
||
|
Sil.Hpointsto (e, se', typ) :: sigma'
|
||
|
in
|
||
|
let eprop_res = Prop.set eprop ~sigma:sigma'' in
|
||
|
ret_old_path [Prop.normalize tenv eprop_res]
|
||
7 years ago
|
| [], _ ->
|
||
|
ret_old_path [prop_]
|
||
|
| _ ->
|
||
|
L.internal_error "Pvar %a appears on the LHS of >1 heap predicate!@." (Pvar.pp Pp.text)
|
||
8 years ago
|
pvar ;
|
||
|
assert false )
|
||
7 years ago
|
| Sil.Abstract _ ->
|
||
8 years ago
|
if Prover.check_inconsistency tenv prop_ then ret_old_path []
|
||
10 years ago
|
else
|
||
9 years ago
|
ret_old_path
|
||
8 years ago
|
[ Abs.remove_redundant_array_elements current_pname tenv
|
||
|
(Abs.abstract current_pname tenv prop_) ]
|
||
7 years ago
|
| Sil.Remove_temps (temps, _) ->
|
||
7 years ago
|
ret_old_path [Prop.exist_quantify tenv temps prop_]
|
||
7 years ago
|
| Sil.Declare_locals (ptl, _) ->
|
||
|
let sigma_locals =
|
||
8 years ago
|
let add_None (x, typ) =
|
||
8 years ago
|
(x, Exp.Sizeof {typ; nbytes= None; dynamic_length= None; subtype= Subtype.exact}, None)
|
||
|
in
|
||
9 years ago
|
let sigma_locals () =
|
||
7 years ago
|
List.map
|
||
|
~f:(Prop.mk_ptsto_lvar tenv Prop.Fld_init Sil.inst_initial)
|
||
8 years ago
|
(List.map ~f:add_None ptl)
|
||
|
in
|
||
9 years ago
|
Config.run_in_re_execution_mode (* no footprint vars for locals *)
|
||
8 years ago
|
sigma_locals ()
|
||
|
in
|
||
8 years ago
|
let sigma' = prop_.Prop.sigma @ sigma_locals in
|
||
8 years ago
|
let prop' = Prop.normalize tenv (Prop.set prop_ ~sigma:sigma') in
|
||
10 years ago
|
ret_old_path [prop']
|
||
8 years ago
|
|
||
7 years ago
|
|
||
9 years ago
|
and diverge prop path =
|
||
8 years ago
|
State.add_diverging_states (Paths.PathSet.from_renamed_list [(prop, path)]) ;
|
||
|
(* diverge *)
|
||
10 years ago
|
[]
|
||
|
|
||
7 years ago
|
|
||
9 years ago
|
(** Symbolic execution of a sequence of instructions.
|
||
|
If errors occur and [mask_errors] is true, just treat as skip. *)
|
||
7 years ago
|
and instrs ?(mask_errors= false) exe_env tenv pdesc instrs ppl =
|
||
10 years ago
|
let exe_instr instr (p, path) =
|
||
8 years ago
|
L.d_str "Executing Generated Instruction " ;
|
||
|
Sil.d_instr instr ;
|
||
|
L.d_ln () ;
|
||
7 years ago
|
try sym_exec exe_env tenv pdesc instr p path with exn ->
|
||
7 years ago
|
IExn.reraise_if exn ~f:(fun () -> not mask_errors || not (SymOp.exn_not_failure exn)) ;
|
||
7 years ago
|
let error = Exceptions.recognize_exception exn in
|
||
|
let loc =
|
||
7 years ago
|
match error.ocaml_pos with
|
||
|
| Some ocaml_pos ->
|
||
|
"at " ^ L.ocaml_pos_to_string ocaml_pos
|
||
|
| None ->
|
||
|
""
|
||
7 years ago
|
in
|
||
7 years ago
|
L.d_warning
|
||
|
(F.sprintf "Generated Instruction Failed with: %s%s" error.name.IssueType.unique_id loc) ;
|
||
7 years ago
|
L.d_ln () ;
|
||
|
[(p, path)]
|
||
8 years ago
|
in
|
||
8 years ago
|
let f plist instr = List.concat_map ~f:(exe_instr instr) plist in
|
||
8 years ago
|
List.fold ~f ~init:ppl instrs
|
||
10 years ago
|
|
||
7 years ago
|
|
||
7 years ago
|
and add_constraints_on_actuals_by_ref tenv caller_pdesc prop actuals_by_ref callee_pname callee_loc =
|
||
7 years ago
|
let add_actual_by_ref_to_footprint prop (actual, actual_typ, actual_index) =
|
||
8 years ago
|
let abduced =
|
||
|
match actual with
|
||
7 years ago
|
| Exp.Lvar _ | Exp.Var _ ->
|
||
|
Pvar.mk_abduced_ref_param callee_pname actual_index callee_loc
|
||
|
| _ ->
|
||
|
L.(die InternalError) "Unexpected variable expression %a" Exp.pp actual
|
||
8 years ago
|
in
|
||
|
let already_has_abduced_retval p =
|
||
|
List.exists
|
||
8 years ago
|
~f:(fun hpred ->
|
||
|
match hpred with
|
||
7 years ago
|
| Sil.Hpointsto (Exp.Lvar pv, _, _) ->
|
||
|
Pvar.equal pv abduced
|
||
|
| _ ->
|
||
7 years ago
|
false )
|
||
8 years ago
|
p.Prop.sigma_fp
|
||
|
in
|
||
8 years ago
|
(* prevent introducing multiple abduced retvals for a single call site in a loop *)
|
||
7 years ago
|
if already_has_abduced_retval prop || is_rec_call callee_pname caller_pdesc then prop
|
||
8 years ago
|
else if !Config.footprint then
|
||
8 years ago
|
let prop', abduced_strexp =
|
||
|
match actual_typ.Typ.desc with
|
||
7 years ago
|
| Typ.Tptr (({desc= Tstruct _} as typ), _) ->
|
||
|
(* for struct types passed by reference, do abduction on the fields of the
|
||
8 years ago
|
struct *)
|
||
|
add_struct_value_to_footprint tenv abduced typ prop
|
||
7 years ago
|
| Typ.Tptr (typ, _) ->
|
||
|
(* for pointer types passed by reference, do abduction directly on the pointer *)
|
||
8 years ago
|
let prop', fresh_fp_var = add_to_footprint tenv abduced typ prop in
|
||
|
(prop', Sil.Eexp (fresh_fp_var, Sil.Inone))
|
||
7 years ago
|
| _ ->
|
||
|
L.(die InternalError)
|
||
7 years ago
|
"No need for abduction on non-pointer type %s" (Typ.to_string actual_typ)
|
||
8 years ago
|
in
|
||
8 years ago
|
let filtered_sigma =
|
||
|
List.map
|
||
|
~f:(function
|
||
7 years ago
|
| Sil.Hpointsto (lhs, _, typ_exp) when Exp.equal lhs actual ->
|
||
|
Sil.Hpointsto (lhs, abduced_strexp, typ_exp)
|
||
|
| hpred ->
|
||
|
hpred)
|
||
8 years ago
|
prop'.Prop.sigma
|
||
|
in
|
||
8 years ago
|
Prop.normalize tenv (Prop.set prop' ~sigma:filtered_sigma)
|
||
|
else
|
||
|
(* bind actual passed by ref to the abduced value pointed to by the synthetic pvar *)
|
||
|
let prop' =
|
||
|
let filtered_sigma =
|
||
|
List.filter
|
||
|
~f:(function
|
||
8 years ago
|
| Sil.Hpointsto (lhs, _, _) when Exp.equal lhs actual -> false | _ -> true)
|
||
|
prop.Prop.sigma
|
||
|
in
|
||
|
Prop.normalize tenv (Prop.set prop ~sigma:filtered_sigma)
|
||
|
in
|
||
8 years ago
|
List.fold
|
||
|
~f:(fun p hpred ->
|
||
8 years ago
|
match hpred with
|
||
7 years ago
|
| Sil.Hpointsto (Exp.Lvar pv, rhs, texp) when Pvar.equal pv abduced ->
|
||
|
let new_hpred = Sil.Hpointsto (actual, rhs, texp) in
|
||
8 years ago
|
Prop.normalize tenv (Prop.set p ~sigma:(new_hpred :: prop'.Prop.sigma))
|
||
7 years ago
|
| _ ->
|
||
7 years ago
|
p )
|
||
8 years ago
|
~init:prop' prop'.Prop.sigma
|
||
8 years ago
|
in
|
||
8 years ago
|
let non_const_actuals_by_ref =
|
||
|
let is_not_const (e, _, i) =
|
||
7 years ago
|
match Attributes.load callee_pname with
|
||
7 years ago
|
| Some attrs ->
|
||
|
let is_const = List.mem ~equal:Int.equal attrs.ProcAttributes.const_formals i in
|
||
8 years ago
|
if is_const then (
|
||
8 years ago
|
L.d_str (Printf.sprintf "Not havocing const argument number %d: " i) ;
|
||
|
Sil.d_exp e ;
|
||
|
L.d_ln () ) ;
|
||
8 years ago
|
not is_const
|
||
7 years ago
|
| None ->
|
||
|
true
|
||
8 years ago
|
in
|
||
|
List.filter ~f:is_not_const actuals_by_ref
|
||
|
in
|
||
7 years ago
|
List.fold ~f:add_actual_by_ref_to_footprint ~init:prop non_const_actuals_by_ref
|
||
10 years ago
|
|
||
7 years ago
|
|
||
10 years ago
|
(** execute a call for an unknown or scan function *)
|
||
7 years ago
|
and unknown_or_scan_call ~is_scan ~reason ret_type_option ret_annots
|
||
8 years ago
|
{Builtin.tenv; pdesc; prop_= pre; path; ret_id; args; proc_name= callee_pname; loc; instr} =
|
||
10 years ago
|
let remove_file_attribute prop =
|
||
9 years ago
|
let do_exp p (e, _) =
|
||
8 years ago
|
let do_attribute q atom =
|
||
|
match atom with
|
||
7 years ago
|
| Sil.Apred ((Aresource {ra_res= Rfile} as res), _) ->
|
||
|
Attribute.remove_for_attr tenv q res
|
||
|
| _ ->
|
||
|
q
|
||
8 years ago
|
in
|
||
|
List.fold ~f:do_attribute ~init:p (Attribute.get_for_exp tenv p e)
|
||
|
in
|
||
9 years ago
|
let filtered_args =
|
||
8 years ago
|
match (args, instr) with
|
||
7 years ago
|
| _ :: other_args, Sil.Call (_, _, _, _, {CallFlags.cf_virtual}) when cf_virtual ->
|
||
|
(* Do not remove the file attribute on the reciver for virtual calls *)
|
||
9 years ago
|
other_args
|
||
7 years ago
|
| _ ->
|
||
|
args
|
||
8 years ago
|
in
|
||
|
List.fold ~f:do_exp ~init:prop filtered_args
|
||
|
in
|
||
8 years ago
|
let should_abduce_param_value pname =
|
||
|
let open Typ.Procname in
|
||
|
match pname with
|
||
7 years ago
|
| Java _ ->
|
||
|
(* FIXME (T19882766): we need to disable this for Java because it breaks too many tests *)
|
||
8 years ago
|
false
|
||
7 years ago
|
| ObjC_Cpp cpp_name ->
|
||
7 years ago
|
(* FIXME: we need to work around a frontend hack for std::shared_ptr
|
||
8 years ago
|
* to silent some of the uninitialization warnings *)
|
||
7 years ago
|
if String.is_suffix ~suffix:"_std__shared_ptr" (Typ.Procname.to_string callee_pname)
|
||
|
(* Abduced parameters for the empty destructor body cause `Cannot star` *)
|
||
|
|| Typ.Procname.ObjC_Cpp.is_destructor cpp_name
|
||
|
then false
|
||
8 years ago
|
else true
|
||
7 years ago
|
| _ ->
|
||
|
true
|
||
8 years ago
|
in
|
||
10 years ago
|
let actuals_by_ref =
|
||
8 years ago
|
List.filter_mapi
|
||
8 years ago
|
~f:(fun i actual ->
|
||
|
match actual with
|
||
7 years ago
|
| (Exp.Lvar _ as e), ({Typ.desc= Tptr _} as t) ->
|
||
|
Some (e, t, i)
|
||
|
| (Exp.Var _ as e), ({Typ.desc= Tptr _} as t) when should_abduce_param_value callee_pname ->
|
||
|
Some (e, t, i)
|
||
|
| _ ->
|
||
7 years ago
|
None )
|
||
8 years ago
|
args
|
||
|
in
|
||
7 years ago
|
let has_nonnull_annot = Annotations.ia_is_nonnull ret_annots in
|
||
9 years ago
|
let pre_final =
|
||
|
(* in Java, assume that skip functions close resources passed as params *)
|
||
8 years ago
|
let pre_1 = if Typ.Procname.is_java callee_pname then remove_file_attribute pre else pre in
|
||
|
let pre_2 =
|
||
|
match (ret_id, ret_type_option) with
|
||
7 years ago
|
| Some (ret_id, _), Some ret_typ ->
|
||
|
(* TODO(jjb): Should this use the type of ret_id, or ret_type from the procedure type? *)
|
||
7 years ago
|
add_constraints_on_retval tenv pdesc pre_1 (Exp.Var ret_id) ret_typ ~has_nonnull_annot
|
||
8 years ago
|
callee_pname loc
|
||
7 years ago
|
| _ ->
|
||
|
pre_1
|
||
8 years ago
|
in
|
||
7 years ago
|
add_constraints_on_actuals_by_ref tenv pdesc pre_2 actuals_by_ref callee_pname loc
|
||
8 years ago
|
in
|
||
|
if is_scan (* if scan function, don't mark anything with undef attributes *) then
|
||
|
[(Tabulation.remove_constant_string_class tenv pre_final, path)]
|
||
10 years ago
|
else
|
||
|
(* otherwise, add undefined attribute to retvals and actuals passed by ref *)
|
||
7 years ago
|
let undefined_actuals_by_ref = List.map ~f:(fun (exp, _, _) -> exp) actuals_by_ref in
|
||
|
let ret_exp_opt = Option.map ~f:(fun (id, _) -> Exp.Var id) ret_id in
|
||
9 years ago
|
let prop_with_undef_attr =
|
||
|
let path_pos = State.get_path_pos () in
|
||
7 years ago
|
Attribute.mark_vars_as_undefined tenv pre_final ~ret_exp_opt ~undefined_actuals_by_ref
|
||
|
callee_pname ret_annots loc path_pos
|
||
8 years ago
|
in
|
||
7 years ago
|
let callee_loc_opt =
|
||
|
Option.map
|
||
|
~f:(fun attributes -> attributes.ProcAttributes.loc)
|
||
|
(Specs.proc_resolve_attributes callee_pname)
|
||
|
in
|
||
|
let skip_path = Paths.Path.add_skipped_call path callee_pname reason callee_loc_opt in
|
||
8 years ago
|
[(prop_with_undef_attr, skip_path)]
|
||
10 years ago
|
|
||
7 years ago
|
|
||
8 years ago
|
and check_variadic_sentinel ?(fails_on_nil= false) n_formals (sentinel, null_pos)
|
||
7 years ago
|
{Builtin.pdesc; tenv; prop_; path; args; proc_name; loc; exe_env} =
|
||
10 years ago
|
(* from clang's lib/Sema/SemaExpr.cpp: *)
|
||
|
(* "nullPos" is the number of formal parameters at the end which *)
|
||
|
(* effectively count as part of the variadic arguments. This is *)
|
||
|
(* useful if you would prefer to not have *any* formal parameters, *)
|
||
|
(* but the language forces you to have at least one. *)
|
||
|
let first_var_arg_pos = if null_pos > n_formals then 0 else n_formals - null_pos in
|
||
8 years ago
|
let nargs = List.length args in
|
||
10 years ago
|
(* sentinels start counting from the last argument to the function *)
|
||
10 years ago
|
let sentinel_pos = nargs - sentinel - 1 in
|
||
|
let mk_non_terminal_argsi (acc, i) a =
|
||
8 years ago
|
if i < first_var_arg_pos || i >= sentinel_pos then (acc, i + 1) else ((a, i) :: acc, i + 1)
|
||
|
in
|
||
8 years ago
|
(* fold_left reverses the arguments *)
|
||
|
let non_terminal_argsi = fst (List.fold ~f:mk_non_terminal_argsi ~init:([], 0) args) in
|
||
10 years ago
|
let check_allocated result ((lexp, typ), i) =
|
||
8 years ago
|
(* simulate a Load for [lexp] *)
|
||
10 years ago
|
let tmp_id_deref = Ident.create_fresh Ident.kprimed in
|
||
8 years ago
|
let load_instr = Sil.Load (tmp_id_deref, lexp, typ, loc) in
|
||
7 years ago
|
try instrs exe_env tenv pdesc [load_instr] result with e when SymOp.exn_not_failure e ->
|
||
7 years ago
|
IExn.reraise_if e ~f:(fun () -> fails_on_nil) ;
|
||
7 years ago
|
let deref_str = Localise.deref_str_nil_argument_in_variadic_method proc_name nargs i in
|
||
|
let err_desc =
|
||
7 years ago
|
Errdesc.explain_dereference proc_name tenv ~use_buckets:true ~is_premature_nil:true
|
||
|
deref_str prop_ loc
|
||
7 years ago
|
in
|
||
|
raise (Exceptions.Premature_nil_termination (err_desc, __POS__))
|
||
8 years ago
|
in
|
||
8 years ago
|
(* fold_left reverses the arguments back so that we report an *)
|
||
10 years ago
|
(* error on the first premature nil argument *)
|
||
8 years ago
|
List.fold ~f:check_allocated ~init:[(prop_, path)] non_terminal_argsi
|
||
10 years ago
|
|
||
7 years ago
|
|
||
8 years ago
|
and check_variadic_sentinel_if_present ({Builtin.prop_; path; proc_name} as builtin_args) =
|
||
9 years ago
|
match Specs.proc_resolve_attributes proc_name with
|
||
7 years ago
|
| None ->
|
||
|
[(prop_, path)]
|
||
9 years ago
|
| Some callee_attributes ->
|
||
8 years ago
|
match
|
||
|
PredSymb.get_sentinel_func_attribute_value callee_attributes.ProcAttributes.func_attributes
|
||
|
with
|
||
7 years ago
|
| None ->
|
||
|
[(prop_, path)]
|
||
|
| Some sentinel_arg ->
|
||
|
let formals = callee_attributes.ProcAttributes.formals in
|
||
8 years ago
|
check_variadic_sentinel (List.length formals) sentinel_arg builtin_args
|
||
9 years ago
|
|
||
7 years ago
|
|
||
7 years ago
|
and sym_exec_objc_getter field ret_typ tenv ret_id pdesc pname loc args prop =
|
||
|
let field_name, _, _ = field in
|
||
8 years ago
|
L.d_strln
|
||
7 years ago
|
(F.sprintf "No custom getter found. Executing the ObjC builtin getter with ivar %s."
|
||
|
(Typ.Fieldname.to_string field_name)) ;
|
||
8 years ago
|
let ret_id = match ret_id with Some (ret_id, _) -> ret_id | None -> assert false in
|
||
9 years ago
|
match args with
|
||
7 years ago
|
| [ ( lexp
|
||
7 years ago
|
, ( ({Typ.desc= Tstruct struct_name} as typ)
|
||
|
| {desc= Tptr (({desc= Tstruct struct_name} as typ), _)} ) ) ] ->
|
||
|
Tenv.add_field tenv struct_name field ;
|
||
7 years ago
|
let field_access_exp = Exp.Lfield (lexp, field_name, typ) in
|
||
8 years ago
|
execute_load ~report_deref_errors:false pname pdesc tenv ret_id field_access_exp ret_typ loc
|
||
|
prop
|
||
7 years ago
|
| _ ->
|
||
|
raise (Exceptions.Wrong_argument_number __POS__)
|
||
|
|
||
9 years ago
|
|
||
7 years ago
|
and sym_exec_objc_setter field _ tenv _ pdesc pname loc args prop =
|
||
|
let field_name, _, _ = field in
|
||
8 years ago
|
L.d_strln
|
||
7 years ago
|
(F.sprintf "No custom setter found. Executing the ObjC builtin setter with ivar %s."
|
||
|
(Typ.Fieldname.to_string field_name)) ;
|
||
9 years ago
|
match args with
|
||
7 years ago
|
| ( lexp1
|
||
7 years ago
|
, ( ({Typ.desc= Tstruct struct_name} as typ1)
|
||
7 years ago
|
| {Typ.desc= Tptr (({Typ.desc= Tstruct struct_name} as typ1), _)} ) )
|
||
7 years ago
|
:: (lexp2, typ2) :: _ ->
|
||
|
Tenv.add_field tenv struct_name field ;
|
||
7 years ago
|
let field_access_exp = Exp.Lfield (lexp1, field_name, typ1) in
|
||
8 years ago
|
execute_store ~report_deref_errors:false pname pdesc tenv field_access_exp typ2 lexp2 loc
|
||
|
prop
|
||
7 years ago
|
| _ ->
|
||
|
raise (Exceptions.Wrong_argument_number __POS__)
|
||
|
|
||
9 years ago
|
|
||
8 years ago
|
and sym_exec_objc_accessor property_accesor ret_typ tenv ret_id pdesc _ loc args prop path
|
||
8 years ago
|
: Builtin.ret_typ =
|
||
9 years ago
|
let f_accessor =
|
||
|
match property_accesor with
|
||
7 years ago
|
| ProcAttributes.Objc_getter field ->
|
||
|
sym_exec_objc_getter field
|
||
|
| ProcAttributes.Objc_setter field ->
|
||
|
sym_exec_objc_setter field
|
||
8 years ago
|
in
|
||
9 years ago
|
(* we want to execute in the context of the current procedure, not in the context of callee_pname,
|
||
|
since this is the procname of the setter/getter method *)
|
||
8 years ago
|
let cur_pname = Procdesc.get_proc_name pdesc in
|
||
8 years ago
|
f_accessor ret_typ tenv ret_id pdesc cur_pname loc args prop |> List.map ~f:(fun p -> (p, path))
|
||
10 years ago
|
|
||
7 years ago
|
|
||
7 years ago
|
and sym_exec_alloc_model exe_env pname ret_typ tenv ret_id pdesc loc prop path : Builtin.ret_typ =
|
||
7 years ago
|
let alloc_source_function_arg = (Exp.Const (Const.Cfun pname), Typ.mk Tvoid) in
|
||
|
let args =
|
||
|
let sizeof_exp =
|
||
|
Exp.Sizeof {typ= ret_typ; nbytes= None; dynamic_length= None; subtype= Subtype.exact}
|
||
|
in
|
||
|
let exp = (sizeof_exp, Typ.mk (Tint Typ.IULong)) in
|
||
|
[exp; alloc_source_function_arg]
|
||
|
in
|
||
|
let alloc_fun = Exp.Const (Const.Cfun BuiltinDecl.malloc_no_fail) in
|
||
|
let alloc_instr = Sil.Call (ret_id, alloc_fun, args, loc, CallFlags.default) in
|
||
|
L.d_strln "No spec found, method should be model as alloc, executing alloc... " ;
|
||
7 years ago
|
instrs exe_env tenv pdesc [alloc_instr] [(prop, path)]
|
||
7 years ago
|
|
||
|
|
||
10 years ago
|
(** Perform symbolic execution for a function call *)
|
||
7 years ago
|
and proc_call exe_env callee_summary
|
||
8 years ago
|
{Builtin.pdesc; tenv; prop_= pre; path; ret_id; args= actual_pars; loc} =
|
||
8 years ago
|
let caller_pname = Procdesc.get_proc_name pdesc in
|
||
7 years ago
|
let callee_attrs = Specs.get_attributes callee_summary in
|
||
8 years ago
|
let callee_pname = Specs.get_proc_name callee_summary in
|
||
8 years ago
|
check_inherently_dangerous_function caller_pname callee_pname ;
|
||
|
let formal_types = List.map ~f:snd (Specs.get_formals callee_summary) in
|
||
|
let rec comb actual_pars formal_types =
|
||
|
match (actual_pars, formal_types) with
|
||
7 years ago
|
| [], [] ->
|
||
|
actual_pars
|
||
|
| (e, t_e) :: etl', _ :: tl' ->
|
||
|
(e, t_e) :: comb etl' tl'
|
||
|
| _, [] ->
|
||
|
Errdesc.warning_err (State.get_loc ())
|
||
8 years ago
|
"likely use of variable-arguments function, or function prototype missing@." ;
|
||
|
L.d_warning "likely use of variable-arguments function, or function prototype missing" ;
|
||
|
L.d_ln () ;
|
||
|
L.d_str "actual parameters: " ;
|
||
|
Sil.d_exp_list (List.map ~f:fst actual_pars) ;
|
||
|
L.d_ln () ;
|
||
|
L.d_str "formal parameters: " ;
|
||
|
Typ.d_list formal_types ;
|
||
|
L.d_ln () ;
|
||
|
actual_pars
|
||
7 years ago
|
| [], _ ->
|
||
|
L.d_str ("**** ERROR: Procedure " ^ Typ.Procname.to_string callee_pname) ;
|
||
8 years ago
|
L.d_strln " mismatch in the number of parameters ****" ;
|
||
|
L.d_str "actual parameters: " ;
|
||
|
Sil.d_exp_list (List.map ~f:fst actual_pars) ;
|
||
|
L.d_ln () ;
|
||
|
L.d_str "formal parameters: " ;
|
||
|
Typ.d_list formal_types ;
|
||
|
L.d_ln () ;
|
||
|
raise (Exceptions.Wrong_argument_number __POS__)
|
||
|
in
|
||
|
(* Actual parameters are associated to their formal
|
||
10 years ago
|
parameter type if there are enough formal parameters, and
|
||
|
to their actual type otherwise. The latter case happens
|
||
|
with variable - arguments functions *)
|
||
7 years ago
|
let actual_params = comb actual_pars formal_types in
|
||
7 years ago
|
(* In case we call an objc instance method we add an extra spec
|
||
|
where the receiver is null and the semantics of the call is nop *)
|
||
|
match (!Language.curr_language, callee_attrs.ProcAttributes.clang_method_kind) with
|
||
|
| Language.Clang, ProcAttributes.OBJC_INSTANCE ->
|
||
|
handle_objc_instance_method_call actual_pars actual_params pre tenv ret_id pdesc callee_pname
|
||
|
loc path
|
||
7 years ago
|
(Tabulation.exe_function_call exe_env callee_summary)
|
||
7 years ago
|
| _ ->
|
||
|
(* non-objective-c method call. Standard tabulation *)
|
||
7 years ago
|
Tabulation.exe_function_call exe_env callee_summary tenv ret_id pdesc callee_pname loc
|
||
|
actual_params pre path
|
||
10 years ago
|
|
||
7 years ago
|
|
||
10 years ago
|
(** perform symbolic execution for a single prop, and check for junk *)
|
||
7 years ago
|
and sym_exec_wrapper exe_env handle_exn tenv proc_cfg instr ((prop: Prop.normal Prop.t), path)
|
||
8 years ago
|
: Paths.PathSet.t =
|
||
7 years ago
|
let pname = Procdesc.get_proc_name (ProcCfg.Exceptional.proc_desc proc_cfg) in
|
||
8 years ago
|
let prop_primed_to_normal p =
|
||
|
(* Rename primed vars with fresh normal vars, and return them *)
|
||
7 years ago
|
let ids_primed =
|
||
|
Prop.free_vars p |> Sequence.filter ~f:Ident.is_primed |> Ident.hashqueue_of_sequence
|
||
|
|> Ident.HashQueue.keys
|
||
|
in
|
||
10 years ago
|
let ids_primed_normal =
|
||
8 years ago
|
List.map ~f:(fun id -> (id, Ident.create_fresh Ident.knormal)) ids_primed
|
||
|
in
|
||
9 years ago
|
let ren_sub =
|
||
8 years ago
|
Sil.subst_of_list (List.map ~f:(fun (id1, id2) -> (id1, Exp.Var id2)) ids_primed_normal)
|
||
|
in
|
||
8 years ago
|
let p' = Prop.normalize tenv (Prop.prop_sub ren_sub p) in
|
||
7 years ago
|
let fav_normal = List.map ~f:snd ids_primed_normal in
|
||
8 years ago
|
(p', fav_normal)
|
||
|
in
|
||
|
let prop_normal_to_primed fav_normal p =
|
||
|
(* rename given normal vars to fresh primed *)
|
||
7 years ago
|
if List.is_empty fav_normal then p else Prop.exist_quantify tenv fav_normal p
|
||
8 years ago
|
in
|
||
10 years ago
|
try
|
||
|
let pre_process_prop p =
|
||
7 years ago
|
let p', fav = if Sil.instr_is_auxiliary instr then (p, []) else prop_primed_to_normal p in
|
||
10 years ago
|
let p'' =
|
||
8 years ago
|
let map_res_action e ra =
|
||
|
(* update the vpath in resource attributes *)
|
||
8 years ago
|
let vpath, _ = Errdesc.vpath_find tenv p' e in
|
||
8 years ago
|
{ra with PredSymb.ra_vpath= vpath}
|
||
|
in
|
||
|
Attribute.map_resource tenv p' map_res_action
|
||
|
in
|
||
|
(p'', fav)
|
||
|
in
|
||
10 years ago
|
let post_process_result fav_normal p path =
|
||
|
let p' = prop_normal_to_primed fav_normal p in
|
||
8 years ago
|
State.set_path path None ;
|
||
10 years ago
|
let node_has_abstraction node =
|
||
8 years ago
|
let instr_is_abstraction = function Sil.Abstract _ -> true | _ -> false in
|
||
7 years ago
|
List.exists ~f:instr_is_abstraction (ProcCfg.Exceptional.instrs node)
|
||
8 years ago
|
in
|
||
10 years ago
|
let curr_node = State.get_node () in
|
||
7 years ago
|
match ProcCfg.Exceptional.kind curr_node with
|
||
7 years ago
|
| Procdesc.Node.Prune_node _ when not (node_has_abstraction curr_node) ->
|
||
|
(* don't check for leaks in prune nodes, unless there is abstraction anyway,*)
|
||
9 years ago
|
(* but force them into either branch *)
|
||
10 years ago
|
p'
|
||
7 years ago
|
| _ ->
|
||
7 years ago
|
check_deallocate_static_memory (Abs.abstract_junk pname tenv p')
|
||
8 years ago
|
in
|
||
|
L.d_str "Instruction " ;
|
||
|
Sil.d_instr instr ;
|
||
|
L.d_ln () ;
|
||
10 years ago
|
let prop', fav_normal = pre_process_prop prop in
|
||
9 years ago
|
let res_list =
|
||
8 years ago
|
Config.run_with_abs_val_equal_zero
|
||
|
(* no exp abstraction during sym exe *)
|
||
7 years ago
|
(fun () ->
|
||
|
sym_exec exe_env tenv (ProcCfg.Exceptional.proc_desc proc_cfg) instr prop' path )
|
||
8 years ago
|
()
|
||
|
in
|
||
9 years ago
|
let res_list_nojunk =
|
||
8 years ago
|
List.map ~f:(fun (p, path) -> (post_process_result fav_normal p path, path)) res_list
|
||
|
in
|
||
9 years ago
|
let results =
|
||
8 years ago
|
List.map
|
||
|
~f:(fun (p, path) -> (Prop.prop_rename_primed_footprint_vars tenv p, path))
|
||
8 years ago
|
res_list_nojunk
|
||
|
in
|
||
|
L.d_strln "Instruction Returns" ;
|
||
|
Propgraph.d_proplist prop (List.map ~f:fst results) ;
|
||
|
L.d_ln () ;
|
||
|
State.mark_instr_ok () ;
|
||
10 years ago
|
Paths.PathSet.from_renamed_list results
|
||
7 years ago
|
with exn ->
|
||
7 years ago
|
IExn.reraise_if exn ~f:(fun () ->
|
||
|
not !Config.footprint || not (Exceptions.handle_exception exn) ) ;
|
||
7 years ago
|
handle_exn exn ;
|
||
|
(* calls State.mark_instr_fail *)
|
||
|
Paths.PathSet.empty
|
||
10 years ago
|
|
||
7 years ago
|
|
||
10 years ago
|
(** {2 Lifted Abstract Transfer Functions} *)
|
||
|
|
||
7 years ago
|
let node handle_exn exe_env tenv proc_cfg (node: ProcCfg.Exceptional.node) (pset: Paths.PathSet.t)
|
||
7 years ago
|
: Paths.PathSet.t =
|
||
|
let pname = Procdesc.get_proc_name (ProcCfg.Exceptional.proc_desc proc_cfg) in
|
||
10 years ago
|
let exe_instr_prop instr p tr (pset1: Paths.PathSet.t) =
|
||
|
let pset2 =
|
||
10 years ago
|
if Tabulation.prop_is_exn pname p && not (Sil.instr_is_auxiliary instr)
|
||
7 years ago
|
&& ProcCfg.Exceptional.kind node <> Procdesc.Node.exn_handler_kind
|
||
9 years ago
|
(* skip normal instructions if an exception was thrown,
|
||
|
unless this is an exception handler node *)
|
||
8 years ago
|
then (
|
||
|
L.d_str "Skipping instr " ;
|
||
|
Sil.d_instr instr ;
|
||
|
L.d_strln " due to exception" ;
|
||
|
Paths.PathSet.from_renamed_list [(p, tr)] )
|
||
7 years ago
|
else sym_exec_wrapper exe_env handle_exn tenv proc_cfg instr (p, tr)
|
||
8 years ago
|
in
|
||
|
Paths.PathSet.union pset2 pset1
|
||
|
in
|
||
8 years ago
|
let exe_instr_pset pset instr =
|
||
8 years ago
|
Paths.PathSet.fold (exe_instr_prop instr) pset Paths.PathSet.empty
|
||
|
in
|
||
7 years ago
|
List.fold ~f:exe_instr_pset ~init:pset (ProcCfg.Exceptional.instrs node)
|