[ondemand] remove ref to execution environment

Summary: Whenever the interface functions are called, there is always an execution environment present, so it is safer and better to get rid of the setter/getter reference thing.

Reviewed By: jvillard

Differential Revision: D25421335

fbshipit-source-id: 7110c932b
master
Nikos Gorogiannis 4 years ago committed by Facebook GitHub Bot
parent 1bf82d9f86
commit f4c52851fb

@ -19,12 +19,12 @@ let () =
let mk_interprocedural_t ~f_analyze_dep ~f_analyze_pdesc_dep ~get_payload exe_env summary let mk_interprocedural_t ~f_analyze_dep ~f_analyze_pdesc_dep ~get_payload exe_env summary
?(tenv = Exe_env.get_tenv exe_env (Summary.get_proc_name summary)) () = ?(tenv = Exe_env.get_tenv exe_env (Summary.get_proc_name summary)) () =
let analyze_dependency proc_name = let analyze_dependency proc_name =
let summary = Ondemand.analyze_proc_name ~caller_summary:summary proc_name in let summary = Ondemand.analyze_proc_name exe_env ~caller_summary:summary proc_name in
Option.bind summary ~f:(fun {Summary.payloads; proc_desc; _} -> Option.bind summary ~f:(fun {Summary.payloads; proc_desc; _} ->
f_analyze_dep proc_desc (get_payload payloads) ) f_analyze_dep proc_desc (get_payload payloads) )
in in
let analyze_pdesc_dependency proc_desc = let analyze_pdesc_dependency proc_desc =
let summary = Ondemand.analyze_proc_desc ~caller_summary:summary proc_desc in let summary = Ondemand.analyze_proc_desc exe_env ~caller_summary:summary proc_desc in
Option.bind summary ~f:(fun {Summary.payloads; _} -> f_analyze_pdesc_dep (get_payload payloads)) Option.bind summary ~f:(fun {Summary.payloads; _} -> f_analyze_pdesc_dep (get_payload payloads))
in in
let stats = ref summary.Summary.stats in let stats = ref summary.Summary.stats in
@ -65,7 +65,7 @@ let interprocedural_with_field payload_field checker {Callbacks.summary; exe_env
let interprocedural_file payload_field checker {Callbacks.procedures; exe_env; source_file} = let interprocedural_file payload_field checker {Callbacks.procedures; exe_env; source_file} =
let analyze_file_dependency proc_name = let analyze_file_dependency proc_name =
let summary = Ondemand.analyze_proc_name_no_caller proc_name in let summary = Ondemand.analyze_proc_name_no_caller exe_env proc_name in
Option.bind summary ~f:(fun {Summary.payloads; proc_desc; _} -> Option.bind summary ~f:(fun {Summary.payloads; proc_desc; _} ->
Field.get payload_field payloads |> Option.map ~f:(fun payload -> (proc_desc, payload)) ) Field.get payload_field payloads |> Option.map ~f:(fun payload -> (proc_desc, payload)) )
in in

@ -71,7 +71,7 @@ let report exe_env work_set =
let acc = let acc =
Starvation.report_on_pair Starvation.report_on_pair
~analyze_ondemand:(fun pname -> ~analyze_ondemand:(fun pname ->
Ondemand.analyze_proc_name ~caller_summary:summary pname Ondemand.analyze_proc_name exe_env ~caller_summary:summary pname
|> Option.bind ~f:(fun summary -> |> Option.bind ~f:(fun summary ->
Option.map summary.Summary.payloads.starvation ~f:(fun starvation -> Option.map summary.Summary.payloads.starvation ~f:(fun starvation ->
(Summary.get_proc_desc summary, starvation) ) ) ) (Summary.get_proc_desc summary, starvation) ) ) )

@ -12,8 +12,6 @@ open! IStd
module L = Logging module L = Logging
module F = Format module F = Format
let exe_env_ref = ref None
module LocalCache = struct module LocalCache = struct
let results = let results =
lazy lazy
@ -36,10 +34,6 @@ module LocalCache = struct
Procname.LRUHash.replace (Lazy.force results) proc_name summary_option Procname.LRUHash.replace (Lazy.force results) proc_name summary_option
end end
let set_exe_env (env : Exe_env.t) = exe_env_ref := Some env
let unset_exe_env () = exe_env_ref := None
(* always incremented before use *) (* always incremented before use *)
let nesting = ref (-1) let nesting = ref (-1)
@ -156,14 +150,13 @@ let update_taskbar callee_pdesc =
!ProcessPoolState.update_status t0 status !ProcessPoolState.update_status t0 status
let analyze callee_summary = let analyze exe_env callee_summary =
let exe_env = Option.value_exn !exe_env_ref in
let summary = Callbacks.iterate_procedure_callbacks exe_env callee_summary in let summary = Callbacks.iterate_procedure_callbacks exe_env callee_summary in
BackendStats.incr_ondemand_procs_analyzed () ; BackendStats.incr_ondemand_procs_analyzed () ;
summary summary
let run_proc_analysis ~caller_pdesc callee_pdesc = let run_proc_analysis exe_env ~caller_pdesc callee_pdesc =
let callee_pname = Procdesc.get_proc_name callee_pdesc in let callee_pname = Procdesc.get_proc_name callee_pdesc in
let log_elapsed_time = let log_elapsed_time =
let start_time = Mtime_clock.counter () in let start_time = Mtime_clock.counter () in
@ -179,7 +172,7 @@ let run_proc_analysis ~caller_pdesc callee_pdesc =
let preprocess () = let preprocess () =
incr nesting ; incr nesting ;
update_taskbar callee_pdesc ; update_taskbar callee_pdesc ;
Preanal.do_preanalysis (Option.value_exn !exe_env_ref) callee_pdesc ; Preanal.do_preanalysis exe_env callee_pdesc ;
if Config.debug_mode then if Config.debug_mode then
DotCfg.emit_proc_desc (Procdesc.get_attributes callee_pdesc).translation_unit callee_pdesc DotCfg.emit_proc_desc (Procdesc.get_attributes callee_pdesc).translation_unit callee_pdesc
|> ignore ; |> ignore ;
@ -216,7 +209,7 @@ let run_proc_analysis ~caller_pdesc callee_pdesc =
let attributes = Procdesc.get_attributes callee_pdesc in let attributes = Procdesc.get_attributes callee_pdesc in
try try
let callee_summary = let callee_summary =
if attributes.ProcAttributes.is_defined then analyze initial_callee_summary if attributes.ProcAttributes.is_defined then analyze exe_env initial_callee_summary
else initial_callee_summary else initial_callee_summary
in in
let final_callee_summary = postprocess callee_summary in let final_callee_summary = postprocess callee_summary in
@ -254,14 +247,14 @@ let run_proc_analysis ~caller_pdesc callee_pdesc =
(* shadowed for tracing *) (* shadowed for tracing *)
let run_proc_analysis ~caller_pdesc callee_pdesc = let run_proc_analysis exe_env ~caller_pdesc callee_pdesc =
PerfEvent.( PerfEvent.(
log (fun logger -> log (fun logger ->
let callee_pname = Procdesc.get_proc_name callee_pdesc in let callee_pname = Procdesc.get_proc_name callee_pdesc in
log_begin_event logger ~name:"ondemand" ~categories:["backend"] log_begin_event logger ~name:"ondemand" ~categories:["backend"]
~arguments:[("proc", `String (Procname.to_string callee_pname))] ~arguments:[("proc", `String (Procname.to_string callee_pname))]
() )) ; () )) ;
let summary = run_proc_analysis ~caller_pdesc callee_pdesc in let summary = run_proc_analysis exe_env ~caller_pdesc callee_pdesc in
PerfEvent.(log (fun logger -> log_end_event logger ())) ; PerfEvent.(log (fun logger -> log_end_event logger ())) ;
summary summary
@ -334,7 +327,7 @@ let get_callee_proc_desc = function
get_proc_desc proc_name get_proc_desc proc_name
let analyze_callee ?caller_summary callee = let analyze_callee exe_env ?caller_summary callee =
let callee_pname = proc_name_of_callee callee in let callee_pname = proc_name_of_callee callee in
register_callee ?caller_summary callee_pname ; register_callee ?caller_summary callee_pname ;
if is_active callee_pname then None if is_active callee_pname then None
@ -349,7 +342,7 @@ let analyze_callee ?caller_summary callee =
| Some callee_pdesc -> | Some callee_pdesc ->
RestartScheduler.lock_exn callee_pname ; RestartScheduler.lock_exn callee_pname ;
let callee_summary = let callee_summary =
run_proc_analysis run_proc_analysis exe_env
~caller_pdesc:(Option.map ~f:Summary.get_proc_desc caller_summary) ~caller_pdesc:(Option.map ~f:Summary.get_proc_desc caller_summary)
callee_pdesc callee_pdesc
in in
@ -363,39 +356,36 @@ let analyze_callee ?caller_summary callee =
summ_opt summ_opt
let analyze_proc_desc ~caller_summary callee_pdesc = let analyze_proc_desc exe_env ~caller_summary callee_pdesc =
analyze_callee ~caller_summary (ProcDesc callee_pdesc) analyze_callee exe_env ~caller_summary (ProcDesc callee_pdesc)
let analyze_proc_name ~caller_summary callee_pname = let analyze_proc_name exe_env ~caller_summary callee_pname =
analyze_callee ~caller_summary (ProcName callee_pname) analyze_callee exe_env ~caller_summary (ProcName callee_pname)
let analyze_proc_name_no_caller callee_pname = let analyze_proc_name_no_caller exe_env callee_pname =
analyze_callee ?caller_summary:None (ProcName callee_pname) analyze_callee exe_env ?caller_summary:None (ProcName callee_pname)
let analyze_procedures exe_env procs_to_analyze source_file_opt = let analyze_procedures exe_env procs_to_analyze source_file_opt =
let saved_language = !Language.curr_language in let saved_language = !Language.curr_language in
set_exe_env exe_env ;
let analyze_proc_name_call pname = let analyze_proc_name_call pname =
ignore (analyze_proc_name_no_caller pname : Summary.t option) ignore (analyze_proc_name_no_caller exe_env pname : Summary.t option)
in in
List.iter ~f:analyze_proc_name_call procs_to_analyze ; List.iter ~f:analyze_proc_name_call procs_to_analyze ;
Option.iter source_file_opt ~f:(fun source_file -> Option.iter source_file_opt ~f:(fun source_file ->
if Config.dump_duplicate_symbols then dump_duplicate_procs source_file procs_to_analyze ) ; if Config.dump_duplicate_symbols then dump_duplicate_procs source_file procs_to_analyze ) ;
Option.iter source_file_opt ~f:(fun source_file -> Option.iter source_file_opt ~f:(fun source_file ->
Callbacks.iterate_file_callbacks_and_store_issues procs_to_analyze exe_env source_file ) ; Callbacks.iterate_file_callbacks_and_store_issues procs_to_analyze exe_env source_file ) ;
unset_exe_env () ;
Language.curr_language := saved_language Language.curr_language := saved_language
(** Invoke all procedure-level and file-level callbacks on a given environment. *) (** Invoke all procedure-level and file-level callbacks on a given environment. *)
let analyze_file (exe_env : Exe_env.t) source_file = let analyze_file exe_env source_file =
let procs_to_analyze = SourceFiles.proc_names_of_source source_file in let procs_to_analyze = SourceFiles.proc_names_of_source source_file in
analyze_procedures exe_env procs_to_analyze (Some source_file) analyze_procedures exe_env procs_to_analyze (Some source_file)
(** Invoke procedure callbacks on a given environment. *) (** Invoke procedure callbacks on a given environment. *)
let analyze_proc_name_toplevel (exe_env : Exe_env.t) proc_name = let analyze_proc_name_toplevel exe_env proc_name = analyze_procedures exe_env [proc_name] None
analyze_procedures exe_env [proc_name] None

@ -12,21 +12,18 @@ open! IStd
val get_proc_desc : Procname.t -> Procdesc.t option val get_proc_desc : Procname.t -> Procdesc.t option
(** Find a proc desc for the procedure, perhaps loading it from disk. *) (** Find a proc desc for the procedure, perhaps loading it from disk. *)
val analyze_proc_desc : caller_summary:Summary.t -> Procdesc.t -> Summary.t option val analyze_proc_desc : Exe_env.t -> caller_summary:Summary.t -> Procdesc.t -> Summary.t option
(** [analyze_proc_desc ~caller_summary callee_pdesc] performs an on-demand analysis of (** [analyze_proc_desc exe_env ~caller_summary callee_pdesc] performs an on-demand analysis of
[callee_pdesc] triggered during the analysis of [caller_summary] *) [callee_pdesc] triggered during the analysis of [caller_summary] *)
val analyze_proc_name : caller_summary:Summary.t -> Procname.t -> Summary.t option val analyze_proc_name : Exe_env.t -> caller_summary:Summary.t -> Procname.t -> Summary.t option
(** [analyze_proc_name ~caller_summary callee_pname] performs an on-demand analysis of (** [analyze_proc_name exe_env ~caller_summary callee_pname] performs an on-demand analysis of
[callee_pname] triggered during the analysis of [caller_summary] *) [callee_pname] triggered during the analysis of [caller_summary] *)
val analyze_proc_name_no_caller : Procname.t -> Summary.t option val analyze_proc_name_no_caller : Exe_env.t -> Procname.t -> Summary.t option
(** [analyze_proc_name_no_caller callee_pname] performs an on-demand analysis of [callee_pname] (** [analyze_proc_name_no_caller exe_env callee_pname] performs an on-demand analysis of
triggered by the top-level of a file-level checker. This must not be used in any other context, [callee_pname] triggered by the top-level of a file-level checker. This must not be used in any
as this will break incremental analysis. *) other context, as this will break incremental analysis. *)
val set_exe_env : Exe_env.t -> unit
(** Set the execution enviroment used during on-demand analysis. *)
module LocalCache : sig module LocalCache : sig
val clear : unit -> unit val clear : unit -> unit

@ -114,8 +114,6 @@ let tests =
make_load_fld ~rhs_typ:StdTyp.void lhs_id_str fld_str (Exp.Var (ident_of_str root_str)) make_load_fld ~rhs_typ:StdTyp.void lhs_id_str fld_str (Exp.Var (ident_of_str root_str))
in in
let assert_empty = invariant "{ }" in let assert_empty = invariant "{ }" in
let exe_env = Exe_env.mk () in
Ondemand.set_exe_env exe_env ;
let test_list = let test_list =
[ ("source recorded", [assign_to_source "ret_id"; invariant "{ ret_id$0* => (SOURCE -> ?) }"]) [ ("source recorded", [assign_to_source "ret_id"; invariant "{ ret_id$0* => (SOURCE -> ?) }"])
; ("non-source not recorded", [assign_to_non_source "ret_id"; assert_empty]) ; ("non-source not recorded", [assign_to_non_source "ret_id"; assert_empty])

Loading…
Cancel
Save