diff --git a/infer/src/checkers/checkers.ml b/infer/src/checkers/checkers.ml index a2e074154..3b8f4f832 100644 --- a/infer/src/checkers/checkers.ml +++ b/infer/src/checkers/checkers.ml @@ -566,3 +566,31 @@ let callback_print_c_method_calls { Callbacks.proc_desc; proc_name } = description | _ -> () in Cfg.Procdesc.iter_instrs do_instr proc_desc + +(** Print access to globals. *) +let callback_print_access_to_globals { Callbacks.proc_desc; proc_name } = + let do_pvar is_read pvar loc = + let description = + Printf.sprintf "%s of global %s" + (if is_read then "read" else "write") + (Pvar.to_string pvar) in + ST.report_error + proc_name + proc_desc + "CHECKERS_ACCESS_GLOBAL" + loc + description in + let rec get_global_var = function + | Sil.Lvar pvar when Pvar.is_global pvar -> + Some pvar + | Sil.Lfield (e, _, _) -> + get_global_var e + | _ -> + None in + let do_instr _ = function + | Sil.Letderef (_, e, _, loc) when get_global_var e <> None -> + Option.may (fun pvar -> do_pvar true pvar loc) (get_global_var e) + | Sil.Set (e, _, _, loc) when get_global_var e <> None -> + Option.may (fun pvar -> do_pvar false pvar loc) (get_global_var e) + | _ -> () in + Cfg.Procdesc.iter_instrs do_instr proc_desc diff --git a/infer/src/checkers/checkers.mli b/infer/src/checkers/checkers.mli index 0483c59d2..b360c30af 100644 --- a/infer/src/checkers/checkers.mli +++ b/infer/src/checkers/checkers.mli @@ -53,3 +53,4 @@ val callback_check_write_to_parcel : Callbacks.proc_callback_t val callback_find_deserialization : Callbacks.proc_callback_t val callback_check_field_access : Callbacks.proc_callback_t val callback_print_c_method_calls : Callbacks.proc_callback_t +val callback_print_access_to_globals : Callbacks.proc_callback_t diff --git a/infer/src/checkers/registerCheckers.ml b/infer/src/checkers/registerCheckers.ml index 2fd25b9d3..269e9a5be 100644 --- a/infer/src/checkers/registerCheckers.ml +++ b/infer/src/checkers/registerCheckers.ml @@ -38,6 +38,7 @@ let active_procedure_checkers () = RepeatedCallsChecker.callback_check_repeated_calls, checkers_enabled; PrintfArgs.callback_printf_args, checkers_enabled; AnnotationReachability.Interprocedural.check_and_report, checkers_enabled; + Checkers.callback_print_access_to_globals, false; ] in IList.map (fun (x, y) -> (x, y, Some Config.Java)) l in let c_cpp_checkers = @@ -45,6 +46,7 @@ let active_procedure_checkers () = [ Checkers.callback_print_c_method_calls, false; CheckDeadCode.callback_check_dead_code, false; + Checkers.callback_print_access_to_globals, false; ] in IList.map (fun (x, y) -> (x, y, Some Config.Clang)) l in