From be590bcd4c73cc6cd7ef6b87bba134cbfa065741 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ezgi=20=C3=87i=C3=A7ek?= Date: Wed, 8 Jul 2020 04:28:33 -0700 Subject: [PATCH] [cost] Add model for ImmutableSet.of Reviewed By: skcho Differential Revision: D22411208 fbshipit-source-id: 55f462cd9 --- infer/src/bufferoverrun/bufferOverrunModels.ml | 11 +++++++++++ .../java/performance/CollectionTest.java | 13 +++++++++++++ .../codetoanalyze/java/performance/cost-issues.exp | 2 ++ .../tests/codetoanalyze/java/performance/issues.exp | 1 + 4 files changed, 27 insertions(+) diff --git a/infer/src/bufferoverrun/bufferOverrunModels.ml b/infer/src/bufferoverrun/bufferOverrunModels.ml index edb3c2f94..d525c2f7a 100644 --- a/infer/src/bufferoverrun/bufferOverrunModels.ml +++ b/infer/src/bufferoverrun/bufferOverrunModels.ml @@ -886,6 +886,15 @@ module Collection = struct {exec; check= no_check} + let of_list list = + let exec env ~ret:((id, _) as ret) mem = + let mem = new_collection.exec env ~ret mem in + List.fold_left list ~init:mem ~f:(fun acc {exp= elem_exp} -> + (add id elem_exp).exec env ~ret acc ) + in + {exec; check= no_check} + + let singleton_collection = let exec env ~ret:((id, _) as ret) mem = let {exec= new_exec; check= _} = new_collection in @@ -1576,6 +1585,8 @@ module Call = struct ; +PatternMatch.implements_collections &:: "singletonMap" <>--> Collection.singleton_collection ; +PatternMatch.implements_collections &::+ unmodifiable <>$ capt_exp $--> Collection.iterator + ; +PatternMatch.implements_google "common.collect.ImmutableSet" + &:: "of" &++> Collection.of_list ; +PatternMatch.implements_google "common.base.Preconditions" &:: "checkArgument" <>$ capt_exp $+...$--> Preconditions.check_argument ; +PatternMatch.implements_google "common.base.Preconditions" diff --git a/infer/tests/codetoanalyze/java/performance/CollectionTest.java b/infer/tests/codetoanalyze/java/performance/CollectionTest.java index 98c99bbef..1e32a7d15 100644 --- a/infer/tests/codetoanalyze/java/performance/CollectionTest.java +++ b/infer/tests/codetoanalyze/java/performance/CollectionTest.java @@ -6,6 +6,7 @@ */ import android.util.SparseArray; +import com.google.common.collect.ImmutableSet; import java.util.Collection; import java.util.concurrent.ConcurrentLinkedQueue; @@ -89,4 +90,16 @@ public class CollectionTest { } }; } + + void immutable_set_of_constant() { + + ImmutableSet set = ImmutableSet.of(); + for (int i = 0; i < set.size(); i++) {} + } + + void immutable_set_of_multiple_constant() { + + ImmutableSet set = ImmutableSet.of(1, 2, 3, 4, 5); + for (int i = 0; i < set.size(); i++) {} + } } diff --git a/infer/tests/codetoanalyze/java/performance/cost-issues.exp b/infer/tests/codetoanalyze/java/performance/cost-issues.exp index 615697d77..7ebc7c4ea 100644 --- a/infer/tests/codetoanalyze/java/performance/cost-issues.exp +++ b/infer/tests/codetoanalyze/java/performance/cost-issues.exp @@ -91,6 +91,8 @@ codetoanalyze/java/performance/CollectionTest.java, CollectionTest$MyEnumType.va codetoanalyze/java/performance/CollectionTest.java, CollectionTest$MyEnumType.values():CollectionTest$MyEnumType[], 7, OnUIThread:false, [] codetoanalyze/java/performance/CollectionTest.java, CollectionTest.(), 3, OnUIThread:false, [] codetoanalyze/java/performance/CollectionTest.java, CollectionTest.ensure_call(CollectionTest$MyCollection):void, 11 + 5 ⋅ list.length, OnUIThread:false, [{list.length},Call to void CollectionTest.iterate_over_mycollection(CollectionTest$MyCollection),Loop] +codetoanalyze/java/performance/CollectionTest.java, CollectionTest.immutable_set_of_constant():void, 10, OnUIThread:false, [] +codetoanalyze/java/performance/CollectionTest.java, CollectionTest.immutable_set_of_multiple_constant():void, 65, OnUIThread:false, [] codetoanalyze/java/performance/CollectionTest.java, CollectionTest.iterate_over_call_quad(int,CollectionTest$MyCollection):void, 6 + 18 ⋅ list.length + 5 ⋅ list.length × list.length + 3 ⋅ (list.length + 1), OnUIThread:false, [{list.length + 1},Loop,{list.length},Call to void CollectionTest.iterate_over_mycollection(CollectionTest$MyCollection),Loop,{list.length},Loop] codetoanalyze/java/performance/CollectionTest.java, CollectionTest.iterate_over_mycollection(CollectionTest$MyCollection):void, 8 + 5 ⋅ list.length, OnUIThread:false, [{list.length},Loop] codetoanalyze/java/performance/CollectionTest.java, CollectionTest.iterate_over_mycollection_quad(java.util.concurrent.ConcurrentLinkedQueue):void, 6 + 18 ⋅ mSubscribers.length + 5 ⋅ mSubscribers.length × mSubscribers.elements.*.length.ub + 3 ⋅ (mSubscribers.length + 1), OnUIThread:false, [{mSubscribers.length + 1},Loop,{mSubscribers.elements.*.length.ub},Call to void CollectionTest.iterate_over_mycollection(CollectionTest$MyCollection),Loop,{mSubscribers.length},Loop] diff --git a/infer/tests/codetoanalyze/java/performance/issues.exp b/infer/tests/codetoanalyze/java/performance/issues.exp index c3a0698f7..886b11c45 100644 --- a/infer/tests/codetoanalyze/java/performance/issues.exp +++ b/infer/tests/codetoanalyze/java/performance/issues.exp @@ -20,6 +20,7 @@ codetoanalyze/java/performance/CantHandle.java, CantHandle.square_root_variant_F codetoanalyze/java/performance/CantHandle.java, CantHandle.square_root_variant_FP(int):void, 3, INTEGER_OVERFLOW_L5, no_bucket, ERROR, [,Parameter `x`,Binary operation: ([0, +oo] + 1):signed32] codetoanalyze/java/performance/CollectionTest.java, CollectionTest$MyEnumType$1.(), 4, INTEGER_OVERFLOW_L5, no_bucket, ERROR, [,Assignment,Binary operation: ([0, +oo] + 1):signed32] codetoanalyze/java/performance/CollectionTest.java, CollectionTest$MyEnumType.():void, 0, INFINITE_EXECUTION_TIME, no_bucket, ERROR, [Call to CollectionTest$MyEnumType$1.(),Unbounded loop,Loop] +codetoanalyze/java/performance/CollectionTest.java, CollectionTest.immutable_set_of_constant():void, 3, CONDITION_ALWAYS_FALSE, no_bucket, WARNING, [Here] codetoanalyze/java/performance/CollectionsTest.java, CollectionsTest.emptyList_constant():void, 1, CONDITION_ALWAYS_FALSE, no_bucket, WARNING, [Here] codetoanalyze/java/performance/CollectionsTest.java, CollectionsTest.emptySet_constant():void, 2, CONDITION_ALWAYS_FALSE, no_bucket, WARNING, [Here] codetoanalyze/java/performance/CollectionsTest.java, CollectionsTest.globalEmptyList_constant():void, 1, CONDITION_ALWAYS_FALSE, no_bucket, WARNING, [Here]