From 8da46d0f0dc36600bd3c6b9c2701e918eefbb578 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ezgi=20=C3=87i=C3=A7ek?= Date: Fri, 9 Oct 2020 08:11:11 -0700 Subject: [PATCH] [inferbo] Add naive models for mutableCopy and copy Reviewed By: skcho Differential Revision: D24218517 fbshipit-source-id: 0d26e6cab --- infer/src/bufferoverrun/bufferOverrunModels.ml | 2 ++ infer/tests/codetoanalyze/objc/performance/NSArray.m | 6 ++++++ .../codetoanalyze/objc/performance/NSMutableString.m | 9 +++++++++ .../tests/codetoanalyze/objc/performance/cost-issues.exp | 2 ++ 4 files changed, 19 insertions(+) diff --git a/infer/src/bufferoverrun/bufferOverrunModels.ml b/infer/src/bufferoverrun/bufferOverrunModels.ml index e58900f20..abe38a1d7 100644 --- a/infer/src/bufferoverrun/bufferOverrunModels.ml +++ b/infer/src/bufferoverrun/bufferOverrunModels.ml @@ -1682,6 +1682,8 @@ module Call = struct ; -"CFDictionaryGetCount" <>$ capt_exp $!--> NSCollection.size ; -"MCFArrayGetCount" <>$ capt_exp $!--> NSCollection.size ; +PatternMatch.ObjectiveC.implements "NSObject" &:: "init" <>$ capt_exp $--> id + ; +PatternMatch.ObjectiveC.implements "NSObject" &:: "copy" <>$ capt_exp $--> id + ; +PatternMatch.ObjectiveC.implements "NSObject" &:: "mutableCopy" <>$ capt_exp $--> id ; +PatternMatch.ObjectiveC.implements "NSArray" &:: "array" <>--> NSCollection.new_collection ; +PatternMatch.ObjectiveC.implements "NSArray" &:: "firstObject" <>$ capt_var_exn $!--> NSCollection.get_first diff --git a/infer/tests/codetoanalyze/objc/performance/NSArray.m b/infer/tests/codetoanalyze/objc/performance/NSArray.m index bab92bb2d..77647a0dd 100644 --- a/infer/tests/codetoanalyze/objc/performance/NSArray.m +++ b/infer/tests/codetoanalyze/objc/performance/NSArray.m @@ -199,3 +199,9 @@ void nsarray_count_bounded_linear(NSArray* array) { for (int i = 0; i < array.count; i++) { } } + +void nsarray_copy_linear(NSArray* array) { + NSArray* copy = [array mutableCopy]; + for (int i = 0; i < copy.count; i++) { + } +} diff --git a/infer/tests/codetoanalyze/objc/performance/NSMutableString.m b/infer/tests/codetoanalyze/objc/performance/NSMutableString.m index 40ccae5a5..0dd3e1ff2 100644 --- a/infer/tests/codetoanalyze/objc/performance/NSMutableString.m +++ b/infer/tests/codetoanalyze/objc/performance/NSMutableString.m @@ -19,3 +19,12 @@ void nsmstring_append_string_linear(NSMutableString* str1, NSString* str2) { for (int i = 0; i < str1.length; i++) { } } + +void copy_string_constant_FP(NSMutableString* str3) { + NSMutableString* str1 = [NSMutableString new]; + NSMutableString* str2 = + [str1 mutableCopy]; // we should do deep copy but we don't + [str1 appendString:str3]; + for (int i = 0; i < str2.length; i++) { + } +} diff --git a/infer/tests/codetoanalyze/objc/performance/cost-issues.exp b/infer/tests/codetoanalyze/objc/performance/cost-issues.exp index c350fa70b..f6c1353b7 100644 --- a/infer/tests/codetoanalyze/objc/performance/cost-issues.exp +++ b/infer/tests/codetoanalyze/objc/performance/cost-issues.exp @@ -7,6 +7,7 @@ codetoanalyze/objc/performance/NSArray.m, nsarray_add_objects_from_array_linear, codetoanalyze/objc/performance/NSArray.m, nsarray_array_with_objects_constant, 27, OnUIThread:false, [] codetoanalyze/objc/performance/NSArray.m, nsarray_binary_search_log_FN, 10, OnUIThread:false, [] codetoanalyze/objc/performance/NSArray.m, nsarray_contains_object_linear, 3 + array->elements.length.ub, OnUIThread:false, [{array->elements.length.ub},Modeled call to NSArray.containsObject:] +codetoanalyze/objc/performance/NSArray.m, nsarray_copy_linear, 6 + 3 ⋅ array->elements.length.ub + 3 ⋅ (array->elements.length.ub + 1), OnUIThread:false, [{array->elements.length.ub + 1},Loop,{array->elements.length.ub},Loop] codetoanalyze/objc/performance/NSArray.m, nsarray_count_bounded_linear, 3 + 3 ⋅ array->elements.length.ub + 3 ⋅ (array->elements.length.ub + 1), OnUIThread:false, [{array->elements.length.ub + 1},Loop,{array->elements.length.ub},Loop] codetoanalyze/objc/performance/NSArray.m, nsarray_empty_array_constant, 8, OnUIThread:false, [] codetoanalyze/objc/performance/NSArray.m, nsarray_enumerator_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] @@ -58,6 +59,7 @@ codetoanalyze/objc/performance/NSMutableArray.m, nsmarray_remove_in_loop_constan codetoanalyze/objc/performance/NSMutableArray.m, nsmarray_set_in_loop_constant, 51, OnUIThread:false, [] codetoanalyze/objc/performance/NSMutableArray.m, nsmarray_set_linear, 3 + 11 ⋅ array->elements.length.ub + 3 ⋅ (array->elements.length.ub + 1), OnUIThread:false, [{array->elements.length.ub + 1},Loop,{array->elements.length.ub},Loop] codetoanalyze/objc/performance/NSMutableDictionary.m, nsmutabledictionary_set_element_in_loop_linear, 5 + 5 ⋅ dict->elements.length.ub + 4 ⋅ (dict->elements.length.ub + 1), OnUIThread:false, [{dict->elements.length.ub + 1},Loop,{dict->elements.length.ub},Loop] +codetoanalyze/objc/performance/NSMutableString.m, copy_string_constant_FP, 11 + 3 ⋅ str3.length.ub + str3.length.ub + 3 ⋅ (str3.length.ub + 1), OnUIThread:false, [{str3.length.ub + 1},Loop,{str3.length.ub},Modeled call to NSMutableString.appendString:,{str3.length.ub},Loop] codetoanalyze/objc/performance/NSMutableString.m, nsmstring_append_string_constant, 14 + 3 ⋅ (str.length.ub + 5) + 3 ⋅ (str.length.ub + 6), OnUIThread:false, [{str.length.ub + 6},Loop,{str.length.ub + 5},Loop] codetoanalyze/objc/performance/NSMutableString.m, nsmstring_append_string_linear, 5 + str2.length.ub + 3 ⋅ (str2.length.ub + str1.length.ub) + 3 ⋅ (str2.length.ub + str1.length.ub + 1), OnUIThread:false, [{str2.length.ub + str1.length.ub + 1},Loop,{str2.length.ub + str1.length.ub},Loop,{str2.length.ub},Modeled call to NSMutableString.appendString:] codetoanalyze/objc/performance/NSOrderedSet.m, nsordered_set_iterate_linear, 5 + ordered_set->elements.length.ub + 4 ⋅ (ordered_set->elements.length.ub + 1), OnUIThread:false, [{ordered_set->elements.length.ub + 1},Loop,{ordered_set->elements.length.ub},Loop]