From b4a22a5bdd09e4ccc04b6c1504c1158f63b3cc38 Mon Sep 17 00:00:00 2001 From: Nikos Gorogiannis Date: Sat, 26 Jan 2019 02:30:46 -0800 Subject: [PATCH] [classloads] prune and multidimensional arrays Reviewed By: ezgicicek Differential Revision: D13818684 fbshipit-source-id: 8e5289f8f --- infer/src/concurrency/classLoads.ml | 18 ++++++++++----- .../codetoanalyze/java/classloads/Arrays.java | 4 ++++ .../codetoanalyze/java/classloads/Prune.java | 22 +++++++++++++++++++ 3 files changed, 39 insertions(+), 5 deletions(-) create mode 100644 infer/tests/codetoanalyze/java/classloads/Prune.java diff --git a/infer/src/concurrency/classLoads.ml b/infer/src/concurrency/classLoads.ml index bfad3077f..0e22c5294 100644 --- a/infer/src/concurrency/classLoads.ml +++ b/infer/src/concurrency/classLoads.ml @@ -14,8 +14,6 @@ module L = Logging (* TODO - catch / throw with exception classes - - multidimensional arrays (multinewarray) ? - - prune *) module Payload = SummaryPayload.Make (struct @@ -57,6 +55,14 @@ let load_type proc_desc tenv loc (typ : Typ.t) astate = astate +let rec load_array proc_desc tenv loc (typ : Typ.t) astate = + match typ with + | {desc= Tarray {elt}} -> + load_array proc_desc tenv loc elt astate + | _ -> + load_type proc_desc tenv loc typ astate + + let rec add_loads_of_exp proc_desc tenv loc (exp : Exp.t) astate = match exp with | Const (Cclass class_ident) -> @@ -65,8 +71,8 @@ let rec add_loads_of_exp proc_desc tenv loc (exp : Exp.t) astate = let class_name = Typ.JavaClass class_str in load_class proc_desc tenv loc astate class_name | Sizeof {typ= {desc= Tarray {elt}}} -> - (* anewarray *) - load_type proc_desc tenv loc elt astate + (* anewarray / multinewarray *) + load_array proc_desc tenv loc elt astate | Sizeof {typ; subtype} when Subtype.is_cast subtype || Subtype.is_instof subtype -> (* checkcast / instanceof *) load_type proc_desc tenv loc typ astate @@ -88,7 +94,9 @@ let exec_instr pdesc tenv astate _ (instr : Sil.instr) = (* invokeinterface / invokespecial / invokestatic / invokevirtual / new *) List.fold args ~init:astate ~f:(fun acc (exp, _) -> add_loads_of_exp pdesc tenv loc exp acc) |> do_call pdesc callee loc - | Load (_, exp, _, loc) -> + | Load (_, exp, _, loc) | Prune (exp, loc, _, _) -> + (* NB the java frontend seems to always translate complex guards into a sequence of + instructions plus a prune on logical vars only. So the below is only for completeness. *) add_loads_of_exp pdesc tenv loc exp astate | Store (e1, _, e2, loc) -> add_loads_of_exp pdesc tenv loc e1 astate |> add_loads_of_exp pdesc tenv loc e2 diff --git a/infer/tests/codetoanalyze/java/classloads/Arrays.java b/infer/tests/codetoanalyze/java/classloads/Arrays.java index f75c3b9df..c6cf01f43 100644 --- a/infer/tests/codetoanalyze/java/classloads/Arrays.java +++ b/infer/tests/codetoanalyze/java/classloads/Arrays.java @@ -14,6 +14,8 @@ class Arrays { ArraysA[] arrayA = new ArraysA[10]; System.out.println(ArraysF.foo()[0]); + + ArraysG[][] arrayG = new ArraysG[10][10]; } } @@ -34,3 +36,5 @@ class ArraysF { return new ArraysE[10]; } } + +class ArraysG {} diff --git a/infer/tests/codetoanalyze/java/classloads/Prune.java b/infer/tests/codetoanalyze/java/classloads/Prune.java new file mode 100644 index 000000000..2e2e009ea --- /dev/null +++ b/infer/tests/codetoanalyze/java/classloads/Prune.java @@ -0,0 +1,22 @@ +/* + * Copyright (c) 2018-present, Facebook, Inc. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +class Prune { + public static void main(String args[]) { + if (PruneA.f == 0) { + System.out.println(PruneB.g < 0); + } + } +} + +class PruneA { + static int f; +} + +class PruneB { + static int g; +}