From 9a3bfa67c7dc96df6401e494c01eca654edf64c1 Mon Sep 17 00:00:00 2001 From: Qianyi Shu Date: Tue, 25 Aug 2020 01:10:43 -0700 Subject: [PATCH] [cost] fix iterator for constant array Summary: Fix the FP when iterating through constant collection. facebook This fix is a hack for now. Reviewed By: ezgicicek, skcho Differential Revision: D23241338 fbshipit-source-id: e2e0c05f8 --- infer/src/bufferoverrun/absLoc.ml | 10 +++++++++- infer/tests/codetoanalyze/objc/performance/NSArray.m | 2 +- .../codetoanalyze/objc/performance/cost-issues.exp | 2 +- infer/tests/codetoanalyze/objc/performance/issues.exp | 1 - 4 files changed, 11 insertions(+), 4 deletions(-) diff --git a/infer/src/bufferoverrun/absLoc.ml b/infer/src/bufferoverrun/absLoc.ml index 2ad0ec1c0..546b1961f 100644 --- a/infer/src/bufferoverrun/absLoc.ml +++ b/infer/src/bufferoverrun/absLoc.ml @@ -231,6 +231,13 @@ module Loc = struct false + let is_objc_iterator_offset = function + | BoField.Field {fn} -> + Fieldname.equal fn BoField.objc_iterator_offset + | _ -> + false + + let is_frontend_tmp = function | BoField.Prim (Var x) -> not (Var.appears_in_source_code x) @@ -354,7 +361,8 @@ module Loc = struct false | BoField.Prim (Allocsite allocsite) -> Allocsite.represents_multiple_values allocsite - | BoField.Field _ as x when is_c_strlen x || is_java_collection_internal_array x -> + | BoField.Field _ as x + when is_c_strlen x || is_java_collection_internal_array x || is_objc_iterator_offset x -> false | BoField.Field {prefix= l} -> represents_multiple_values l diff --git a/infer/tests/codetoanalyze/objc/performance/NSArray.m b/infer/tests/codetoanalyze/objc/performance/NSArray.m index ad6717a80..52c3f7a3e 100644 --- a/infer/tests/codetoanalyze/objc/performance/NSArray.m +++ b/infer/tests/codetoanalyze/objc/performance/NSArray.m @@ -168,7 +168,7 @@ void nsarray_next_object_linear(NSArray* array) { } } -void nsarray_next_object_constant_FP() { +void nsarray_next_object_constant() { NSArray* array = @[ @1, @2 ]; for (id item in array) { } diff --git a/infer/tests/codetoanalyze/objc/performance/cost-issues.exp b/infer/tests/codetoanalyze/objc/performance/cost-issues.exp index 5d117037d..a2b59f79c 100644 --- a/infer/tests/codetoanalyze/objc/performance/cost-issues.exp +++ b/infer/tests/codetoanalyze/objc/performance/cost-issues.exp @@ -19,7 +19,7 @@ codetoanalyze/objc/performance/NSArray.m, nsarray_init_with_objects_constant, 39 codetoanalyze/objc/performance/NSArray.m, nsarray_is_equal_to_array_linear, 4 + array1->elements.length.ub, OnUIThread:false, [{array1->elements.length.ub},Modeled call to NSArray.isEqualToArray:] codetoanalyze/objc/performance/NSArray.m, nsarray_iterate_linear, 6 + 4 ⋅ array->elements.length.ub + 4 ⋅ (array->elements.length.ub + 1), OnUIThread:false, [{array->elements.length.ub + 1},Loop,{array->elements.length.ub},Loop] codetoanalyze/objc/performance/NSArray.m, nsarray_last_object_constant, 4, OnUIThread:false, [] -codetoanalyze/objc/performance/NSArray.m, nsarray_next_object_constant_FP, ⊤, OnUIThread:false, [Unbounded loop,Loop] +codetoanalyze/objc/performance/NSArray.m, nsarray_next_object_constant, 25, OnUIThread:false, [] codetoanalyze/objc/performance/NSArray.m, nsarray_next_object_linear, 5 + array->elements.length.ub + 4 ⋅ (array->elements.length.ub + 1), OnUIThread:false, [{array->elements.length.ub + 1},Loop,{array->elements.length.ub},Loop] codetoanalyze/objc/performance/NSArray.m, nsarray_object_at_indexed_constant, 34, OnUIThread:false, [] codetoanalyze/objc/performance/NSArray.m, nsarray_sort_using_descriptors_constant, 34, OnUIThread:false, [] diff --git a/infer/tests/codetoanalyze/objc/performance/issues.exp b/infer/tests/codetoanalyze/objc/performance/issues.exp index ecea8af8b..124bf7081 100644 --- a/infer/tests/codetoanalyze/objc/performance/issues.exp +++ b/infer/tests/codetoanalyze/objc/performance/issues.exp @@ -1,7 +1,6 @@ codetoanalyze/objc/performance/NSArray.m, nsarray_empty_array_constant, 3, CONDITION_ALWAYS_FALSE, no_bucket, WARNING, [Here] codetoanalyze/objc/performance/NSArray.m, nsarray_enumerator_linear_FP, 0, INFINITE_EXECUTION_TIME, no_bucket, ERROR, [Unbounded loop,Loop] codetoanalyze/objc/performance/NSArray.m, nsarray_init_constant, 3, CONDITION_ALWAYS_FALSE, no_bucket, WARNING, [Here] -codetoanalyze/objc/performance/NSArray.m, nsarray_next_object_constant_FP, 0, INFINITE_EXECUTION_TIME, no_bucket, ERROR, [Unbounded loop,Loop] codetoanalyze/objc/performance/NSDictionary.m, nsdictionary_init_with_dictionary_linear_FP, 0, INFINITE_EXECUTION_TIME, no_bucket, ERROR, [Unbounded loop,Loop] codetoanalyze/objc/performance/NSDictionary.m, nsdictionary_init_with_dictionary_linear_FP, 2, INTEGER_OVERFLOW_U5, no_bucket, ERROR, [,Unknown value from: NSDictionary.initWithDictionary:,Binary operation: ([0, +oo] + 1):signed32] codetoanalyze/objc/performance/NSMutableArray.m, nsmarray_add_in_loop_constant_FP, 0, INFINITE_EXECUTION_TIME, no_bucket, ERROR, [Unbounded loop,Loop]