[ConfigImpact] Handle known expensive calls

Summary: This diff handles String.append and Preconditions.check functions as expensive.

Reviewed By: ezgicicek

Differential Revision: D27650669

fbshipit-source-id: 9b933b713
master
Sungkeun Cho 4 years ago committed by Facebook GitHub Bot
parent fb350e779f
commit f0ea201f25

@ -64,6 +64,7 @@ module UncheckedCallee = struct
type t = type t =
{ callee: callee_name { callee: callee_name
; is_known_expensive: bool
; location: Location.t [@compare.ignore] ; location: Location.t [@compare.ignore]
; call_type: call_type [@compare.ignore] } ; call_type: call_type [@compare.ignore] }
[@@deriving compare] [@@deriving compare]
@ -87,6 +88,12 @@ module UncheckedCallee = struct
f unchecked_callees f unchecked_callees
let make ~is_known_expensive callee location =
{callee; is_known_expensive; location; call_type= Direct}
let is_known_expensive {is_known_expensive} = is_known_expensive
let replace_location_by_call location x = {x with location; call_type= Indirect x} let replace_location_by_call location x = {x with location; call_type= Indirect x}
let rec make_err_trace ({location} as x) = let rec make_err_trace ({location} as x) =
@ -107,6 +114,8 @@ module UncheckedCallees = struct
let decode enc_str = Marshal.from_string (Base64.decode_exn enc_str) 0 let decode enc_str = Marshal.from_string (Base64.decode_exn enc_str) 0
let pp_without_location f x = UncheckedCallee.pp_without_location_list f (elements x) let pp_without_location f x = UncheckedCallee.pp_without_location_list f (elements x)
let has_known_expensive_callee x = exists (fun {is_known_expensive} -> is_known_expensive) x
end end
module UncheckedCalleesCond = struct module UncheckedCalleesCond = struct
@ -197,6 +206,10 @@ module Summary = struct
unchecked_callees_cond unchecked_callees unchecked_callees_cond unchecked_callees
in in
{x with unchecked_callees; unchecked_callees_cond= UncheckedCalleesCond.bottom} {x with unchecked_callees; unchecked_callees_cond= UncheckedCalleesCond.bottom}
let has_known_expensive_callee {unchecked_callees} =
UncheckedCallees.has_known_expensive_callee unchecked_callees
end end
module Dom = struct module Dom = struct
@ -328,35 +341,73 @@ module Dom = struct
fields field_checks } ) fields field_checks } )
let call analyze_dependency callee location let is_known_expensive_method =
let dispatch : (Tenv.t, unit, unit) ProcnameDispatcher.Call.dispatcher =
let open ProcnameDispatcher.Call in
make_dispatcher
[ +PatternMatch.Java.implements_google "common.base.Preconditions"
&:: "checkArgument" $ any_arg $+ any_arg $+...$--> ()
; +PatternMatch.Java.implements_google "common.base.Preconditions"
&:: "checkElementIndex" $ any_arg $+ any_arg $+ any_arg $+...$--> ()
; +PatternMatch.Java.implements_google "common.base.Preconditions"
&:: "checkNotNull" $ any_arg $+ any_arg $+...$--> ()
; +PatternMatch.Java.implements_google "common.base.Preconditions"
&:: "checkPositionIndex" $ any_arg $+ any_arg $+ any_arg $+...$--> ()
; +PatternMatch.Java.implements_google "common.base.Preconditions"
&:: "checkState" $ any_arg $+ any_arg $+...$--> ()
; +PatternMatch.Java.implements_lang "String" &:: "concat" &--> ()
; +PatternMatch.Java.implements_lang "StringBuilder" &:: "append" &--> () ]
in
fun tenv pname args ->
let args =
List.map args ~f:(fun (exp, typ) ->
ProcnameDispatcher.Call.FuncArg.{exp; typ; arg_payload= ()} )
in
dispatch tenv pname args |> Option.is_some
let call tenv analyze_dependency callee args location
({config_checks; field_checks; unchecked_callees; unchecked_callees_cond} as astate) = ({config_checks; field_checks; unchecked_callees; unchecked_callees_cond} as astate) =
if ConfigChecks.is_top config_checks then if ConfigChecks.is_top config_checks then
let callee_summary = analyze_dependency callee in let (callee_summary : Summary.t option), (cost_summary : CostDomain.summary option) =
match analyze_dependency callee with
| None ->
(None, None)
| Some (_, analysis_data) ->
analysis_data
in
let is_expensive = is_known_expensive_method tenv callee args in
let has_expensive_callee =
Option.exists callee_summary ~f:Summary.has_known_expensive_callee
in
if if
Option.exists callee_summary ~f:(fun (_, (_, (cost_summary : CostDomain.summary option))) -> (not is_expensive) && (not has_expensive_callee)
Option.exists cost_summary ~f:(fun (cost_summary : CostDomain.summary) -> && Option.exists cost_summary ~f:(fun (cost_summary : CostDomain.summary) ->
let callee_cost = CostDomain.get_operation_cost cost_summary.post in let callee_cost = CostDomain.get_operation_cost cost_summary.post in
not (CostDomain.BasicCost.is_symbolic callee_cost.cost) ) ) not (CostDomain.BasicCost.is_symbolic callee_cost.cost) )
then (* If callee is cheap by heuristics, ignore it. *) then (* If callee is cheap by heuristics, ignore it. *)
astate astate
else else
let new_unchecked_callees, new_unchecked_callees_cond = let new_unchecked_callees, new_unchecked_callees_cond =
match callee_summary with if is_expensive then
| Some ( UncheckedCallees.singleton
( _ (UncheckedCallee.make ~is_known_expensive:true callee location)
, ( Some , UncheckedCalleesCond.empty )
{ Summary.unchecked_callees= callee_summary else
; unchecked_callees_cond= callee_summary_cond match callee_summary with
; has_call_stmt } | Some
, _callee_cost_summary ) ) { Summary.unchecked_callees= callee_summary
when has_call_stmt -> ; unchecked_callees_cond= callee_summary_cond
(* If callee's summary is not leaf, use it. *) ; has_call_stmt }
( UncheckedCallees.replace_location_by_call location callee_summary when has_call_stmt ->
, UncheckedCalleesCond.replace_location_by_call location callee_summary_cond ) (* If callee's summary is not leaf, use it. *)
| _ -> ( UncheckedCallees.replace_location_by_call location callee_summary
(* Otherwise, add callee's name. *) , UncheckedCalleesCond.replace_location_by_call location callee_summary_cond )
( UncheckedCallees.singleton {callee; location; call_type= Direct} | _ ->
, UncheckedCalleesCond.empty ) (* Otherwise, add callee's name. *)
( UncheckedCallees.singleton
(UncheckedCallee.make ~is_known_expensive:false callee location)
, UncheckedCalleesCond.empty )
in in
if FieldChecks.is_top field_checks then if FieldChecks.is_top field_checks then
{ astate with { astate with
@ -451,7 +502,7 @@ module TransferFunctions = struct
astate astate
| None -> | None ->
(* normal function calls *) (* normal function calls *)
Dom.call analyze_dependency callee location astate ) Dom.call tenv analyze_dependency callee args location astate )
| Prune (e, _, _, _) -> | Prune (e, _, _, _) ->
Dom.prune e astate Dom.prune e astate
| _ -> | _ ->

@ -12,6 +12,8 @@ module Fields : AbstractDomain.FiniteSetS
module UncheckedCallee : sig module UncheckedCallee : sig
type t type t
val is_known_expensive : t -> bool
val make_err_trace : t -> Errlog.loc_trace val make_err_trace : t -> Errlog.loc_trace
val pp_without_location_list : Format.formatter -> t list -> unit val pp_without_location_list : Format.formatter -> t list -> unit

@ -418,7 +418,14 @@ module ConfigImpactItem = struct
let nb_callees = UncheckedCallees.cardinal unchecked_callees in let nb_callees = UncheckedCallees.cardinal unchecked_callees in
if nb_callees <= 0 then assert false ; if nb_callees <= 0 then assert false ;
let is_singleton = nb_callees <= 1 in let is_singleton = nb_callees <= 1 in
let unchecked_callees = UncheckedCallees.elements unchecked_callees in let unchecked_callees =
UncheckedCallees.elements unchecked_callees
|> List.stable_sort ~compare:(fun x y ->
(* Known expensive callees come first. *)
compare_bool
(UncheckedCallee.is_known_expensive y)
(UncheckedCallee.is_known_expensive x) )
in
let qualifier = let qualifier =
Format.asprintf "Function call%s to %a %s %a without GK/QE." Format.asprintf "Function call%s to %a %s %a without GK/QE."
(if is_singleton then "" else "s") (if is_singleton then "" else "s")

Loading…
Cancel
Save