From dbc33aa427a75dd010b05e6a1f8c1b9bbe20928d Mon Sep 17 00:00:00 2001 From: Sungkeun Cho Date: Tue, 20 Oct 2020 07:49:29 -0700 Subject: [PATCH] [cost] Add model: NSKeyedUnarchiver.decodeObjectForKey: Reviewed By: ezgicicek Differential Revision: D24417784 fbshipit-source-id: f440ba018 --- infer/src/bufferoverrun/bufferOverrunModels.ml | 11 +++++++++-- .../objc/performance/NSKeyedUnarchiver.m | 13 +++++++++++++ .../codetoanalyze/objc/performance/cost-issues.exp | 1 + 3 files changed, 23 insertions(+), 2 deletions(-) create mode 100644 infer/tests/codetoanalyze/objc/performance/NSKeyedUnarchiver.m diff --git a/infer/src/bufferoverrun/bufferOverrunModels.ml b/infer/src/bufferoverrun/bufferOverrunModels.ml index 844afbde0..4326d2793 100644 --- a/infer/src/bufferoverrun/bufferOverrunModels.ml +++ b/infer/src/bufferoverrun/bufferOverrunModels.ml @@ -832,7 +832,7 @@ module AbstractCollection (Lang : Lang) = struct let get_collection_internal_array_locs coll_id mem = let coll = Dom.Mem.find (Loc.of_id coll_id) mem in - Dom.Val.get_pow_loc coll |> PowLoc.append_field ~fn:Lang.collection_internal_array_field + Dom.Val.get_all_locs coll |> PowLoc.append_field ~fn:Lang.collection_internal_array_field let get_collection_internal_elements_locs coll_id mem = @@ -1022,12 +1022,17 @@ module AbstractCollection (Lang : Lang) = struct {exec; check= check_index ~last_included:false coll_id index_exp} - let get_at_index coll_id index_exp = + let get_any_index coll_id = let exec _model_env ~ret:(ret_id, _) mem = let locs = get_collection_internal_elements_locs coll_id mem in let v = Dom.Mem.find_set locs mem in model_by_value v ret_id mem in + {exec; check= no_check} + + + let get_at_index coll_id index_exp = + let {exec} = get_any_index coll_id in {exec; check= check_index ~last_included:false coll_id index_exp} end @@ -1708,6 +1713,8 @@ module Call = struct $--> NSCollection.new_collection_by_add_all ; +PatternMatch.ObjectiveC.implements "NSEnumerator" &:: "nextObject" <>$ capt_exp $--> NSCollection.next_object + ; +PatternMatch.ObjectiveC.implements "NSKeyedUnarchiver" + &:: "decodeObjectForKey:" $ capt_var_exn $+...$--> NSCollection.get_any_index ; +PatternMatch.ObjectiveC.implements "NSMutableArray" &:: "initWithCapacity:" <>$ capt_var_exn $+ capt_exp $--> NSCollection.new_collection_of_size diff --git a/infer/tests/codetoanalyze/objc/performance/NSKeyedUnarchiver.m b/infer/tests/codetoanalyze/objc/performance/NSKeyedUnarchiver.m new file mode 100644 index 000000000..ee2c16ba4 --- /dev/null +++ b/infer/tests/codetoanalyze/objc/performance/NSKeyedUnarchiver.m @@ -0,0 +1,13 @@ +/* + * Copyright (c) Facebook, Inc. and its affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ +#import + +void decode_object_for_key_linear(NSKeyedUnarchiver* keyed_unarchiver) { + id v = [keyed_unarchiver decodeObjectForKey:@"key"]; + for (int i = 0; i < v; i++) { + } +} diff --git a/infer/tests/codetoanalyze/objc/performance/cost-issues.exp b/infer/tests/codetoanalyze/objc/performance/cost-issues.exp index 66e7a27d4..cdf263eaf 100644 --- a/infer/tests/codetoanalyze/objc/performance/cost-issues.exp +++ b/infer/tests/codetoanalyze/objc/performance/cost-issues.exp @@ -45,6 +45,7 @@ codetoanalyze/objc/performance/NSDictionary.m, nsdictionary_init_literal_constan codetoanalyze/objc/performance/NSDictionary.m, nsdictionary_init_with_dictionary_linear, 7 + 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/NSInteger.m, nsinteger_value_linear, 3 + 3 ⋅ integer + 2 ⋅ (1+max(0, integer)), OnUIThread:false, [{1+max(0, integer)},Loop,{integer},Loop] codetoanalyze/objc/performance/NSInteger.m, nsnumber_number_with_int_integer_value_constant, 34, OnUIThread:false, [] +codetoanalyze/objc/performance/NSKeyedUnarchiver.m, decode_object_for_key_linear, 9 + 3 ⋅ keyed_unarchiver->elements[*].ub + 2 ⋅ (1+max(0, keyed_unarchiver->elements[*].ub)), OnUIThread:false, [{1+max(0, keyed_unarchiver->elements[*].ub)},Loop,{keyed_unarchiver->elements[*].ub},Loop] codetoanalyze/objc/performance/NSMutableArray.m, nsarray_new_constant, 9, OnUIThread:false, [] codetoanalyze/objc/performance/NSMutableArray.m, nsmarray_add_all_constant, 23, OnUIThread:false, [] codetoanalyze/objc/performance/NSMutableArray.m, nsmarray_add_in_loop_constant, 152, OnUIThread:false, []