From 15bdcca1196e7248b7047f889f3be9a5fd4a5d03 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ezgi=20=C3=87i=C3=A7ek?= Date: Mon, 10 Sep 2018 08:10:11 -0700 Subject: [PATCH] [Loop-hoisting] Add a new model for Javax's Provider.get() and make it invariant only Reviewed By: mbouaziz, ddino Differential Revision: D9748162 fbshipit-source-id: bedd2300e --- infer/src/absint/PatternMatch.ml | 2 ++ infer/src/absint/PatternMatch.mli | 3 +++ infer/src/checkers/hoisting.ml | 2 +- infer/src/checkers/invariantModels.ml | 12 ++++++++++-- infer/tests/codetoanalyze/java/hoisting/Makefile | 2 +- 5 files changed, 17 insertions(+), 4 deletions(-) diff --git a/infer/src/absint/PatternMatch.ml b/infer/src/absint/PatternMatch.ml index a7ef8e527..414e53cef 100644 --- a/infer/src/absint/PatternMatch.ml +++ b/infer/src/absint/PatternMatch.ml @@ -66,6 +66,8 @@ let implements_pseudo_collection t s = let implements_enumeration = implements "java.util.Enumeration" +let implements_inject class_name = implements ("javax.inject." ^ class_name) + let implements_io class_name = implements ("java.io." ^ class_name) let implements_map = implements "java.util.Map" diff --git a/infer/src/absint/PatternMatch.mli b/infer/src/absint/PatternMatch.mli index 2396be05c..3ba4598be 100644 --- a/infer/src/absint/PatternMatch.mli +++ b/infer/src/absint/PatternMatch.mli @@ -43,6 +43,9 @@ val implements_pseudo_collection : Tenv.t -> string -> bool val implements_enumeration : Tenv.t -> string -> bool (** Check whether class implements a Java's Enumeration *) +val implements_inject : string -> Tenv.t -> string -> bool +(** Check whether class implements a Javax Inject *) + val implements_io : string -> Tenv.t -> string -> bool (** Check whether class implements a Java IO *) diff --git a/infer/src/checkers/hoisting.ml b/infer/src/checkers/hoisting.ml index 5c6a074dd..d6ee09500 100644 --- a/infer/src/checkers/hoisting.ml +++ b/infer/src/checkers/hoisting.ml @@ -55,7 +55,7 @@ let get_hoist_inv_map tenv reaching_defs_invariant_map loop_head_to_source_nodes let loop_nodes = Loop_control.get_all_nodes_upwards_until loop_head source_nodes in let inv_vars_in_loop = LoopInvariant.get_inv_vars_in_loop tenv reaching_defs_invariant_map loop_head loop_nodes - ~is_inv_by_default:true + ~is_inv_by_default:Config.cost_invariant_by_default in let hoist_instrs = get_hoistable_calls inv_vars_in_loop loop_nodes source_nodes idom in LoopHeadToHoistInstrs.add loop_head hoist_instrs inv_map ) diff --git a/infer/src/checkers/invariantModels.ml b/infer/src/checkers/invariantModels.ml index 5b0ad773e..318ed6e5f 100644 --- a/infer/src/checkers/invariantModels.ml +++ b/infer/src/checkers/invariantModels.ml @@ -7,12 +7,19 @@ open! IStd -type model = Variant | VariantForHoisting +type model = Invariant | Variant | VariantForHoisting (* Even though functions marked with VariantForHoisting are pure, we don't want to report them in hoisting. Hence, we model them as Variant. *) -let is_invariant = function VariantForHoisting -> not Config.loop_hoisting | Variant -> false +let is_invariant = function + | Invariant -> + true + | VariantForHoisting -> + not Config.loop_hoisting + | Variant -> + false + module Call = struct let dispatch : (Tenv.t, model) ProcnameDispatcher.Call.dispatcher = @@ -55,6 +62,7 @@ module Call = struct ; +PatternMatch.implements_io "PrintStream" &:: "println" <>--> Variant ; +PatternMatch.implements_io "Reader" &:: "read" <>--> Variant ; +PatternMatch.implements_io "BufferedReader" &:: "readLine" <>--> Variant + ; +PatternMatch.implements_inject "Provider" &:: "get" <>--> Invariant ; -"__new" <>--> Variant ; +(fun _ name -> BuiltinDecl.is_declared (Typ.Procname.from_string_c_fun name)) <>--> VariantForHoisting ] diff --git a/infer/tests/codetoanalyze/java/hoisting/Makefile b/infer/tests/codetoanalyze/java/hoisting/Makefile index 2c2bb0556..1bd20565e 100644 --- a/infer/tests/codetoanalyze/java/hoisting/Makefile +++ b/infer/tests/codetoanalyze/java/hoisting/Makefile @@ -6,7 +6,7 @@ TESTS_DIR = ../../.. ANALYZER = checkers -INFER_OPTIONS = --loop-hoisting-only --debug-exceptions +INFER_OPTIONS = --loop-hoisting-only --invariant-by-default --debug-exceptions INFERPRINT_OPTIONS = --issues-tests SOURCES = $(wildcard *.java)