From ce209f6e4fb4b801e8f94c5c01cef29dca33fd18 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ezgi=20=C3=87i=C3=A7ek?= Date: Wed, 28 Apr 2021 08:53:36 -0700 Subject: [PATCH] [cost][inferbo] Add more models for NSDictionary and NSMutableDictionary Reviewed By: skcho Differential Revision: D28055754 fbshipit-source-id: 4cdc34e8d --- .../src/bufferoverrun/bufferOverrunModels.ml | 10 +++++++++ infer/src/cost/costModels.ml | 15 +++++++++++++ .../objc/performance/NSMutableDictionary.m | 21 +++++++++++++++++++ .../objc/performance/cost-issues.exp | 8 +++++-- 4 files changed, 52 insertions(+), 2 deletions(-) diff --git a/infer/src/bufferoverrun/bufferOverrunModels.ml b/infer/src/bufferoverrun/bufferOverrunModels.ml index 33c66aeef..d3a1a1871 100644 --- a/infer/src/bufferoverrun/bufferOverrunModels.ml +++ b/infer/src/bufferoverrun/bufferOverrunModels.ml @@ -1154,6 +1154,14 @@ module NSCollection = struct {exec; check= no_check} + let new_collection_by_init coll_exp = + let exec model_env ~ret:((ret_id, _) as ret) mem = + create_collection model_env ~ret mem ~size_exp:Exp.zero + |> (addAll ret_id coll_exp).exec model_env ~ret + in + {exec; check= no_check} + + let iterator coll_exp = let exec {integer_type_widths= _; location} ~ret:(id, _) mem = let elements_locs = eval_collection_internal_array_locs coll_exp mem in @@ -1700,6 +1708,8 @@ module Call = struct &:: "firstObject" <>$ capt_var_exn $!--> NSCollection.get_first ; +PatternMatch.ObjectiveC.implements "NSDictionary" &:: "initWithDictionary:" <>$ capt_var_exn $+ capt_exp $--> NSCollection.copy + ; +PatternMatch.ObjectiveC.implements "NSDictionary" + &:: "dictionaryWithDictionary:" <>$ capt_exp $--> NSCollection.new_collection_by_init ; +PatternMatch.ObjectiveC.implements "NSSet" &:: "initWithArray:" <>$ capt_var_exn $+ capt_exp $--> NSCollection.copy ; +PatternMatch.ObjectiveC.implements "NSArray" diff --git a/infer/src/cost/costModels.ml b/infer/src/cost/costModels.ml index 563c66a14..fbdf66e58 100644 --- a/infer/src/cost/costModels.ml +++ b/infer/src/cost/costModels.ml @@ -259,6 +259,21 @@ module Call = struct ; +PatternMatch.ObjectiveC.implements "NSMutableArray" &:: "removeAllObjects" <>$ capt_exp $--> BoundsOfNSCollection.linear_length ~of_function:"NSArray.removeAllObjects" + ; +PatternMatch.ObjectiveC.implements "NSDictionary" + &:: "dictionaryWithDictionary:" <>$ capt_exp + $--> BoundsOfNSCollection.linear_length + ~of_function:"NSDictionary.dictionaryWithDictionary:" + ; +PatternMatch.ObjectiveC.implements "NSDictionary" + &:: "initWithDictionary:" <>$ any_arg $+ capt_exp + $--> BoundsOfNSCollection.linear_length ~of_function:"NSArray.initWithArray:" + ; +PatternMatch.ObjectiveC.implements "NSMutableDictionary" + &:: "removeAllObjects" <>$ capt_exp + $--> BoundsOfNSCollection.linear_length + ~of_function:"NSMutableDictionary.removeAllObjects" + ; +PatternMatch.ObjectiveC.implements "NSMutableDictionary" + &:: "addEntriesFromDictionary:" <>$ any_arg $+ capt_exp + $--> BoundsOfNSCollection.linear_length + ~of_function:"addEntriesFromDictionary.NSMutableDictionary:" ; +PatternMatch.ObjectiveC.implements "NSMutableArray" &:: "addObjectsFromArray:" <>$ any_arg $+ capt_exp $--> BoundsOfNSCollection.linear_length ~of_function:"NSArray.addObjectsFromArray:" diff --git a/infer/tests/codetoanalyze/objc/performance/NSMutableDictionary.m b/infer/tests/codetoanalyze/objc/performance/NSMutableDictionary.m index 7b3469761..4ffa4972a 100644 --- a/infer/tests/codetoanalyze/objc/performance/NSMutableDictionary.m +++ b/infer/tests/codetoanalyze/objc/performance/NSMutableDictionary.m @@ -12,3 +12,24 @@ void nsmutabledictionary_set_element_in_loop_linear(NSMutableDictionary* dict) { dict[key] = key; } } + +void nsmutabledictionary_removeAll_linear(NSMutableDictionary* dict) { + [dict removeAllObjects]; +} + +void nsmutabledictionary_addAll_linear(NSMutableDictionary* dict1, + NSMutableDictionary* dict2) { + [dict1 addEntriesFromDictionary:dict2]; +} + +void nsmutabledictionary_init_linear(NSMutableDictionary* dict) { + NSMutableDictionary* new_dict = + [[NSMutableDictionary alloc] initWithDictionary:dict]; +} + +void nsmutabledictionary_dict_init_linear(NSMutableDictionary* dict) { + NSMutableDictionary* new_dict = + [NSMutableDictionary dictionaryWithDictionary:dict]; + for (NSString* key in new_dict.allKeys) { + } +} diff --git a/infer/tests/codetoanalyze/objc/performance/cost-issues.exp b/infer/tests/codetoanalyze/objc/performance/cost-issues.exp index 1ec19f288..ee723e960 100644 --- a/infer/tests/codetoanalyze/objc/performance/cost-issues.exp +++ b/infer/tests/codetoanalyze/objc/performance/cost-issues.exp @@ -44,7 +44,7 @@ codetoanalyze/objc/performance/NSArray.m, objc_blocknsarray_binary_search_log_FN codetoanalyze/objc/performance/NSDictionary.m, nsdictionary_all_keys_linear1, 2 + 3 ⋅ 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/NSDictionary.m, nsdictionary_all_keys_linear2, 5 + 3 ⋅ dict->elements.length.ub + 3 ⋅ (dict->elements.length.ub + 1), OnUIThread:false, [{dict->elements.length.ub + 1},Loop,{dict->elements.length.ub},Loop] codetoanalyze/objc/performance/NSDictionary.m, nsdictionary_all_values_linear, 2 + 3 ⋅ 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/NSDictionary.m, nsdictionary_alloc_with_zone_init_with_dictionary_linear, 6 + 3 ⋅ 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/NSDictionary.m, nsdictionary_alloc_with_zone_init_with_dictionary_linear, 5 + 3 ⋅ dict->elements.length.ub + dict->elements.length.ub + 4 ⋅ (dict->elements.length.ub + 1), OnUIThread:false, [{dict->elements.length.ub + 1},Loop,{dict->elements.length.ub},Modeled call to NSArray.initWithArray:,{dict->elements.length.ub},Loop] codetoanalyze/objc/performance/NSDictionary.m, nsdictionary_dictionary_constant, 10, OnUIThread:false, [] codetoanalyze/objc/performance/NSDictionary.m, nsdictionary_dictionary_with_objects_linear, 13 + 15 ⋅ n_entries + 3 ⋅ n_entries + 2 ⋅ (1+max(0, n_entries)) + 4 ⋅ (1+max(0, n_entries)), OnUIThread:false, [{1+max(0, n_entries)},Loop,{1+max(0, n_entries)},Loop,{n_entries},Loop,{n_entries},Loop] codetoanalyze/objc/performance/NSDictionary.m, nsdictionary_enumerate_call_constant, 63, OnUIThread:false, [] @@ -54,7 +54,7 @@ codetoanalyze/objc/performance/NSDictionary.m, nsdictionary_fast_enumerate_linea codetoanalyze/objc/performance/NSDictionary.m, nsdictionary_find_key_constant, 18, OnUIThread:false, [] codetoanalyze/objc/performance/NSDictionary.m, nsdictionary_init_dictionary_constant, 2, OnUIThread:false, [] codetoanalyze/objc/performance/NSDictionary.m, nsdictionary_init_literal_constant, 44, OnUIThread:false, [] -codetoanalyze/objc/performance/NSDictionary.m, nsdictionary_init_with_dictionary_linear, 6 + 3 ⋅ 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/NSDictionary.m, nsdictionary_init_with_dictionary_linear, 5 + 3 ⋅ dict->elements.length.ub + dict->elements.length.ub + 4 ⋅ (dict->elements.length.ub + 1), OnUIThread:false, [{dict->elements.length.ub + 1},Loop,{dict->elements.length.ub},Modeled call to NSArray.initWithArray:,{dict->elements.length.ub},Loop] codetoanalyze/objc/performance/NSEnumeration.m, call_loop_enumerator_constant, 33, OnUIThread:false, [] codetoanalyze/objc/performance/NSEnumeration.m, call_loop_id_enumerator_linear, 5 + 5 ⋅ (arr->elements.length.ub + 1), OnUIThread:false, [{arr->elements.length.ub + 1},Call to loop_id_enumerator_linear,Loop] codetoanalyze/objc/performance/NSEnumeration.m, loop_enumerator_linear, 4 + 5 ⋅ (enumeration->elements.length.ub + 1), OnUIThread:false, [{enumeration->elements.length.ub + 1},Loop] @@ -77,6 +77,10 @@ codetoanalyze/objc/performance/NSMutableArray.m, nsmarray_remove_constant, 16, codetoanalyze/objc/performance/NSMutableArray.m, nsmarray_remove_in_loop_constant, 181, OnUIThread:false, [] codetoanalyze/objc/performance/NSMutableArray.m, nsmarray_set_in_loop_constant, 50, OnUIThread:false, [] codetoanalyze/objc/performance/NSMutableArray.m, nsmarray_set_linear, 2 + 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_addAll_linear, 2 + dict2->elements.length.ub, OnUIThread:false, [{dict2->elements.length.ub},Modeled call to addEntriesFromDictionary.NSMutableDictionary:] +codetoanalyze/objc/performance/NSMutableDictionary.m, nsmutabledictionary_dict_init_linear, 7 + dict->elements.length.ub + 5 ⋅ (dict->elements.length.ub + 1), OnUIThread:false, [{dict->elements.length.ub + 1},Loop,{dict->elements.length.ub},Modeled call to NSDictionary.dictionaryWithDictionary:] +codetoanalyze/objc/performance/NSMutableDictionary.m, nsmutabledictionary_init_linear, 3 + dict->elements.length.ub, OnUIThread:false, [{dict->elements.length.ub},Modeled call to NSArray.initWithArray:] +codetoanalyze/objc/performance/NSMutableDictionary.m, nsmutabledictionary_removeAll_linear, 1 + dict->elements.length.ub, OnUIThread:false, [{dict->elements.length.ub},Modeled call to NSMutableDictionary.removeAllObjects] codetoanalyze/objc/performance/NSMutableDictionary.m, nsmutabledictionary_set_element_in_loop_linear, 4 + 9 ⋅ (dict->elements.length.ub + 1), OnUIThread:false, [{dict->elements.length.ub + 1},Loop] codetoanalyze/objc/performance/NSMutableString.m, copy_string_constant_FP, 10 + 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, 13 + 3 ⋅ (str.length.ub + 5) + 3 ⋅ (str.length.ub + 6), OnUIThread:false, [{str.length.ub + 6},Loop,{str.length.ub + 5},Loop]