Add sample checker to detect reads and writes to globals

Reviewed By: jvillard

Differential Revision: D3392625

fbshipit-source-id: f76e57e
master
Cristiano Calcagno 9 years ago committed by Facebook Github Bot 9
parent d2f7cb44fa
commit de0f98389a

@ -566,3 +566,31 @@ let callback_print_c_method_calls { Callbacks.proc_desc; proc_name } =
description description
| _ -> () in | _ -> () in
Cfg.Procdesc.iter_instrs do_instr proc_desc 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

@ -53,3 +53,4 @@ val callback_check_write_to_parcel : Callbacks.proc_callback_t
val callback_find_deserialization : Callbacks.proc_callback_t val callback_find_deserialization : Callbacks.proc_callback_t
val callback_check_field_access : 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_c_method_calls : Callbacks.proc_callback_t
val callback_print_access_to_globals : Callbacks.proc_callback_t

@ -38,6 +38,7 @@ let active_procedure_checkers () =
RepeatedCallsChecker.callback_check_repeated_calls, checkers_enabled; RepeatedCallsChecker.callback_check_repeated_calls, checkers_enabled;
PrintfArgs.callback_printf_args, checkers_enabled; PrintfArgs.callback_printf_args, checkers_enabled;
AnnotationReachability.Interprocedural.check_and_report, checkers_enabled; AnnotationReachability.Interprocedural.check_and_report, checkers_enabled;
Checkers.callback_print_access_to_globals, false;
] in ] in
IList.map (fun (x, y) -> (x, y, Some Config.Java)) l in IList.map (fun (x, y) -> (x, y, Some Config.Java)) l in
let c_cpp_checkers = let c_cpp_checkers =
@ -45,6 +46,7 @@ let active_procedure_checkers () =
[ [
Checkers.callback_print_c_method_calls, false; Checkers.callback_print_c_method_calls, false;
CheckDeadCode.callback_check_dead_code, false; CheckDeadCode.callback_check_dead_code, false;
Checkers.callback_print_access_to_globals, false;
] in ] in
IList.map (fun (x, y) -> (x, y, Some Config.Clang)) l in IList.map (fun (x, y) -> (x, y, Some Config.Clang)) l in

Loading…
Cancel
Save