From be1fda72a81b4640063c77a8164600f24938e2d1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ezgi=20=C3=87i=C3=A7ek?= Date: Mon, 16 Dec 2019 09:30:26 -0800 Subject: [PATCH] [inferbo] Add model for `Collection.toArray` Reviewed By: skcho Differential Revision: D19098336 fbshipit-source-id: d60b04e4b --- infer/src/bufferoverrun/bufferOverrunModels.ml | 1 + infer/tests/codetoanalyze/java/performance/Array.java | 4 ++++ infer/tests/codetoanalyze/java/performance/issues.exp | 1 + 3 files changed, 6 insertions(+) diff --git a/infer/src/bufferoverrun/bufferOverrunModels.ml b/infer/src/bufferoverrun/bufferOverrunModels.ml index 916c5f547..36f602848 100644 --- a/infer/src/bufferoverrun/bufferOverrunModels.ml +++ b/infer/src/bufferoverrun/bufferOverrunModels.ml @@ -1193,6 +1193,7 @@ module Call = struct ; -"__new" <>$ capt_exp $+...$--> malloc ~can_be_zero:true ; -"__new_array" <>$ capt_exp $+...$--> malloc ~can_be_zero:true ; +PatternMatch.implements_arrays &:: "asList" <>$ capt_exp $!--> create_copy_array + ; +PatternMatch.implements_collection &:: "toArray" <>$ capt_exp $+...$--> create_copy_array ; +PatternMatch.implements_arrays &:: "copyOf" <>$ capt_exp $+ capt_exp $+...$--> Collection.copyOf ; -"__placement_new" <>$ capt_exp $+ capt_arg $+? capt_arg $!--> placement_new diff --git a/infer/tests/codetoanalyze/java/performance/Array.java b/infer/tests/codetoanalyze/java/performance/Array.java index e36e2eb2a..3163beea6 100644 --- a/infer/tests/codetoanalyze/java/performance/Array.java +++ b/infer/tests/codetoanalyze/java/performance/Array.java @@ -87,4 +87,8 @@ public class Array { String[] r2 = gen_and_iter_types(y); for (int i = 0; i < r2.length; i++) {} // should not be infinite execution time } + + void toArray_linear(java.util.ArrayList list) { + for (int i = 0; i < list.toArray().length; i++) {} + } } diff --git a/infer/tests/codetoanalyze/java/performance/issues.exp b/infer/tests/codetoanalyze/java/performance/issues.exp index 98a1255ee..83a429c22 100644 --- a/infer/tests/codetoanalyze/java/performance/issues.exp +++ b/infer/tests/codetoanalyze/java/performance/issues.exp @@ -15,6 +15,7 @@ codetoanalyze/java/performance/Array.java, codetoanalyze.java.performance.Array. codetoanalyze/java/performance/Array.java, codetoanalyze.java.performance.Array.gen_and_iter_types(int):java.lang.String[], 2, EXPENSIVE_EXECUTION_TIME, no_bucket, ERROR, [with estimated cost 5 + 5 ⋅ length, O(length), degree = 1,{length},Loop at line 76] codetoanalyze/java/performance/Array.java, codetoanalyze.java.performance.Array.init_array_linear():void, 2, EXPENSIVE_EXECUTION_TIME, no_bucket, ERROR, [with estimated cost 2564, O(1), degree = 0] codetoanalyze/java/performance/Array.java, codetoanalyze.java.performance.Array.sort_array_nlogn(int):void, 2, EXPENSIVE_EXECUTION_TIME, no_bucket, ERROR, [with estimated cost 5 + size × log(size), O(size × log(size)), degree = 1 + 1⋅log,{size},Modeled call to Arrays.sort,{size},Modeled call to Arrays.sort] +codetoanalyze/java/performance/Array.java, codetoanalyze.java.performance.Array.toArray_linear(java.util.ArrayList):void, 1, EXPENSIVE_EXECUTION_TIME, no_bucket, ERROR, [with estimated cost 2 + 6 ⋅ list.length + 3 ⋅ (list.length + 1), O(list.length), degree = 1,{list.length + 1},Loop at line 92,{list.length},Loop at line 92] codetoanalyze/java/performance/ArrayCost.java, ArrayCost.ArrayCost(int[]):void, 5, EXPENSIVE_EXECUTION_TIME, no_bucket, ERROR, [with estimated cost 5 + 5 ⋅ mag.length, O(mag.length), degree = 1,{mag.length},Loop at line 15] codetoanalyze/java/performance/ArrayCost.java, ArrayCost.isPowOfTwo_FP(int):boolean, 4, EXPENSIVE_EXECUTION_TIME, no_bucket, ERROR, [with estimated cost 883, O(1), degree = 0] codetoanalyze/java/performance/ArrayListTest.java, ArrayListTest.arraylist_add3_overrun_bad():void, 5, BUFFER_OVERRUN_L1, no_bucket, ERROR, [,Array declaration,Through,Through,Through,Array access: Offset added: 4 Size: 3]