From 12478e1238b8c88b2c17264f91a282d5deb6be47 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ezgi=20=C3=87i=C3=A7ek?= Date: Mon, 16 Dec 2019 06:04:12 -0800 Subject: [PATCH] [inferbo] Add models for Java Collections Summary: Add models for - `singletonMap` - `singletonList` - `emptyList` - `emptyMap` Reviewed By: skcho Differential Revision: D19087648 fbshipit-source-id: 059e36383 --- infer/src/bufferoverrun/bufferOverrunModels.ml | 6 ++++++ .../codetoanalyze/java/performance/CollectionsTest.java | 8 ++++++++ infer/tests/codetoanalyze/java/performance/issues.exp | 1 + 3 files changed, 15 insertions(+) diff --git a/infer/src/bufferoverrun/bufferOverrunModels.ml b/infer/src/bufferoverrun/bufferOverrunModels.ml index 380dab659..916c5f547 100644 --- a/infer/src/bufferoverrun/bufferOverrunModels.ml +++ b/infer/src/bufferoverrun/bufferOverrunModels.ml @@ -1312,7 +1312,13 @@ module Call = struct (* model sets as lists *) ; +PatternMatch.implements_collections &::+ unmodifiable <>$ capt_exp $--> Collection.iterator ; +PatternMatch.implements_collections &:: "singleton" <>--> Collection.singleton_collection + ; +PatternMatch.implements_collections + &:: "singletonList" <>--> Collection.singleton_collection + ; +PatternMatch.implements_collections + &:: "singletonMap" <>--> Collection.singleton_collection + ; +PatternMatch.implements_collections &:: "emptyList" <>--> Collection.new_collection ; +PatternMatch.implements_collections &:: "emptySet" <>--> Collection.new_collection + ; +PatternMatch.implements_collections &:: "emptyMap" <>--> Collection.new_collection (* model maps as lists *) ; +PatternMatch.implements_collections &:: "singletonMap" <>--> Collection.singleton_collection diff --git a/infer/tests/codetoanalyze/java/performance/CollectionsTest.java b/infer/tests/codetoanalyze/java/performance/CollectionsTest.java index c839d82b8..2c06abdb9 100644 --- a/infer/tests/codetoanalyze/java/performance/CollectionsTest.java +++ b/infer/tests/codetoanalyze/java/performance/CollectionsTest.java @@ -65,4 +65,12 @@ class CollectionsTest { Set set = Collections.emptySet(); for (String s : set) {} } + + void emptyList_constant() { + for (int i = 0; i < Collections.emptyList().size(); i++) {} + } + + void singletonMap_constant() { + for (int i = 0; i < Collections.singletonMap(1, 1).size(); i++) {} + } } diff --git a/infer/tests/codetoanalyze/java/performance/issues.exp b/infer/tests/codetoanalyze/java/performance/issues.exp index cb8fbd3b9..e942d47f0 100644 --- a/infer/tests/codetoanalyze/java/performance/issues.exp +++ b/infer/tests/codetoanalyze/java/performance/issues.exp @@ -73,6 +73,7 @@ codetoanalyze/java/performance/CollectionTest.java, CollectionTest.nested_iterat codetoanalyze/java/performance/CollectionTest.java, CollectionTest.sparse_array_linear(android.util.SparseArray):void, 1, EXPENSIVE_EXECUTION_TIME, no_bucket, ERROR, [with estimated cost 2 + 5 ⋅ arr.length + 3 ⋅ (arr.length + 1), O(arr.length), degree = 1,{arr.length + 1},Loop at line 64,{arr.length},Loop at line 64] codetoanalyze/java/performance/CollectionsTest.java, CollectionsTest.binary_search_log(java.util.List):int, 1, EXPENSIVE_EXECUTION_TIME, no_bucket, ERROR, [with estimated cost 2 + log(list.length), O(log(list.length)), degree = 0 + 1⋅log,{list.length},Modeled call to Collections.binarySearch] codetoanalyze/java/performance/CollectionsTest.java, CollectionsTest.copy_linear(java.util.List,java.util.List):void, 1, EXPENSIVE_EXECUTION_TIME, no_bucket, ERROR, [with estimated cost 3 + list_to.length, O(list_to.length), degree = 1,{list_to.length},Modeled call to Collections.copy] +codetoanalyze/java/performance/CollectionsTest.java, CollectionsTest.emptyList_constant():void, 1, CONDITION_ALWAYS_FALSE, no_bucket, WARNING, [Here] codetoanalyze/java/performance/CollectionsTest.java, CollectionsTest.emptySet_zero():void, 2, CONDITION_ALWAYS_FALSE, no_bucket, WARNING, [Here] codetoanalyze/java/performance/CollectionsTest.java, CollectionsTest.fill_linear(java.util.List,java.lang.String):void, 1, EXPENSIVE_EXECUTION_TIME, no_bucket, ERROR, [with estimated cost 3 + list.length, O(list.length), degree = 1,{list.length},Modeled call to Collections.fill] codetoanalyze/java/performance/CollectionsTest.java, CollectionsTest.reverse_linear(java.util.List):void, 1, EXPENSIVE_EXECUTION_TIME, no_bucket, ERROR, [with estimated cost 2 + list.length, O(list.length), degree = 1,{list.length},Modeled call to Collections.reverse]