From 75e5e94f2c9b8c1325b02b317a1f46182e243240 Mon Sep 17 00:00:00 2001 From: Qianyi Shu Date: Mon, 13 Jul 2020 04:51:34 -0700 Subject: [PATCH] [cost] add inferbo model for NSNumber Summary: As title Reviewed By: ezgicicek Differential Revision: D22411709 fbshipit-source-id: 994946a85 --- infer/src/bufferoverrun/bufferOverrunModels.ml | 2 ++ infer/tests/codetoanalyze/objc/performance/NSInteger.m | 7 +++++++ infer/tests/codetoanalyze/objc/performance/cost-issues.exp | 1 + infer/tests/codetoanalyze/objc/performance/issues.exp | 6 +++--- 4 files changed, 13 insertions(+), 3 deletions(-) diff --git a/infer/src/bufferoverrun/bufferOverrunModels.ml b/infer/src/bufferoverrun/bufferOverrunModels.ml index b3cf8b154..a2f25cae5 100644 --- a/infer/src/bufferoverrun/bufferOverrunModels.ml +++ b/infer/src/bufferoverrun/bufferOverrunModels.ml @@ -1473,6 +1473,8 @@ module Call = struct ; -"NSArray" &:: "count" <>$ capt_exp $!--> NSArray.length ; -"NSArray" &:: "objectAtIndexedSubscript:" <>$ capt_arg $+ capt_arg $!--> NSArray.at ; -"NSArray" &:: "arrayWithObjects:count:" <>$ capt_exp $+ capt_exp $--> NSArray.create_array + ; -"NSNumber" &:: "numberWithInt:" <>$ capt_exp $--> id + ; -"NSNumber" &:: "integerValue" <>$ capt_exp $--> id ; (* C++ models *) -"boost" &:: "split" $ capt_arg_of_typ (-"std" &:: "vector") diff --git a/infer/tests/codetoanalyze/objc/performance/NSInteger.m b/infer/tests/codetoanalyze/objc/performance/NSInteger.m index 2ffe0655e..5e130346c 100644 --- a/infer/tests/codetoanalyze/objc/performance/NSInteger.m +++ b/infer/tests/codetoanalyze/objc/performance/NSInteger.m @@ -10,3 +10,10 @@ void nsinteger_value_linear(NSInteger integer) { for (int count = 0; count < integer; count++) { } } + +void nsnumber_number_with_int_integer_value_constant() { + int n = 4; + NSNumber* number = [NSNumber numberWithInt:n]; + for (int i = 0; i < [number integerValue]; i++) { + } +} diff --git a/infer/tests/codetoanalyze/objc/performance/cost-issues.exp b/infer/tests/codetoanalyze/objc/performance/cost-issues.exp index 4ad25747d..2cbad537b 100644 --- a/infer/tests/codetoanalyze/objc/performance/cost-issues.exp +++ b/infer/tests/codetoanalyze/objc/performance/cost-issues.exp @@ -34,6 +34,7 @@ codetoanalyze/objc/performance/NSDictionary.m, nsdictionary_init_dictionary_cons codetoanalyze/objc/performance/NSDictionary.m, nsdictionary_init_literal_constant, 9, OnUIThread:false, [] codetoanalyze/objc/performance/NSDictionary.m, nsdictionary_init_with_dictionary_linear_FP, ⊤, OnUIThread:false, [Unbounded loop,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/NSMutableArray.m, nsmarray_add_all_constant, 25, OnUIThread:false, [] codetoanalyze/objc/performance/NSMutableArray.m, nsmarray_add_in_loop_constant_FP, ⊤, OnUIThread:false, [Unbounded loop,Loop] codetoanalyze/objc/performance/NSMutableArray.m, nsmarray_add_in_loop_linear, 8 + 7 ⋅ n + 3 ⋅ n + 2 ⋅ (n + 1) + 2 ⋅ (n + 1), OnUIThread:false, [{n + 1},Loop,{n + 1},Loop,{n},Loop,{n},Loop] diff --git a/infer/tests/codetoanalyze/objc/performance/issues.exp b/infer/tests/codetoanalyze/objc/performance/issues.exp index ebd0536bf..01c1c6c2a 100644 --- a/infer/tests/codetoanalyze/objc/performance/issues.exp +++ b/infer/tests/codetoanalyze/objc/performance/issues.exp @@ -9,7 +9,7 @@ codetoanalyze/objc/performance/NSArray.m, nsarray_init_with_array_linear_FP, 3, codetoanalyze/objc/performance/NSArray.m, nsarray_iterate_linear_FN, 3, INTEGER_OVERFLOW_U5, no_bucket, ERROR, [,Assignment,,Unknown value from: NSArray.nextObject,Assignment,Binary operation: ([-oo, +oo] + [-oo, +oo]):signed64] codetoanalyze/objc/performance/NSArray.m, nsarray_object_at_indexed_constant_FP, 0, INFINITE_EXECUTION_TIME, no_bucket, ERROR, [Unbounded loop,Loop] codetoanalyze/objc/performance/NSArray.m, nsarray_object_at_indexed_constant_FP, 3, BUFFER_OVERRUN_U5, no_bucket, ERROR, [,Unknown value from: NSArray.arrayWithObjects:count:,Assignment,Array access: Offset: [-oo, +oo] Size: [0, +oo]] -codetoanalyze/objc/performance/NSArray.m, nsarray_object_at_indexed_constant_FP, 3, INTEGER_OVERFLOW_U5, no_bucket, ERROR, [,Unknown value from: NSNumber.integerValue,Binary operation: ([0, +oo] + 1):signed32] +codetoanalyze/objc/performance/NSArray.m, nsarray_object_at_indexed_constant_FP, 3, INTEGER_OVERFLOW_L5, no_bucket, ERROR, [,Assignment,Binary operation: ([0, +oo] + 1):signed32] codetoanalyze/objc/performance/NSArray.m, nsarray_sort_using_descriptors_constant, 4, BUFFER_OVERRUN_U5, no_bucket, ERROR, [,Unknown value from: NSSortDescriptor.initWithKey:ascending:,Assignment,Array access: Offset added: [-oo, +oo] Size: [0, +oo]] codetoanalyze/objc/performance/NSArray.m, nsarray_sort_using_descriptors_nlogn_FN, 3, BUFFER_OVERRUN_U5, no_bucket, ERROR, [,Unknown value from: NSSortDescriptor.initWithKey:ascending:,Assignment,Array access: Offset added: [-oo, +oo] Size: [0, +oo]] codetoanalyze/objc/performance/NSDictionary.m, nsdictionary_all_keys_linear_FP, 0, INFINITE_EXECUTION_TIME, no_bucket, ERROR, [Unbounded loop,Loop] @@ -46,7 +46,7 @@ codetoanalyze/objc/performance/NSString.m, string_with_utf8_string_linear_FP, 0, codetoanalyze/objc/performance/NSString.m, string_with_utf8_string_linear_FP, 4, INTEGER_OVERFLOW_U5, no_bucket, ERROR, [,Unknown value from: NSString.length,Binary operation: ([0, +oo] + 1):signed64] codetoanalyze/objc/performance/NSString.m, substring_no_end_linear_FP, 0, INFINITE_EXECUTION_TIME, no_bucket, ERROR, [Unbounded loop,Loop] codetoanalyze/objc/performance/NSString.m, substring_no_end_linear_FP, 2, INTEGER_OVERFLOW_U5, no_bucket, ERROR, [,Unknown value from: NSString.length,Binary operation: ([0, +oo] + 1):signed32] -codetoanalyze/objc/performance/block.m, objc_blockblock_multiply_array_linear_FN_1, 3, INTEGER_OVERFLOW_U5, no_bucket, ERROR, [,Assignment,,Unknown value from: NSNumber.integerValue,Binary operation: ([-oo, +oo] + [-oo, +oo]):signed64] +codetoanalyze/objc/performance/block.m, objc_blockblock_multiply_array_linear_FN_1, 3, INTEGER_OVERFLOW_U5, no_bucket, ERROR, [,Assignment,,Unknown value from: NSArray.nextObject,Assignment,Binary operation: ([-oo, +oo] + [-oo, +oo]):signed64] codetoanalyze/objc/performance/compound_loop_guard.m, compound_while, 3, CONDITION_ALWAYS_TRUE, no_bucket, WARNING, [Here] codetoanalyze/objc/performance/compound_loop_guard.m, nested_while_and_or_constant, 3, CONDITION_ALWAYS_TRUE, no_bucket, WARNING, [Here] codetoanalyze/objc/performance/compound_loop_guard.m, nested_while_and_or_constant, 3, CONDITION_ALWAYS_TRUE, no_bucket, WARNING, [Here] @@ -74,7 +74,7 @@ codetoanalyze/objc/performance/cost_test_deps.m, two_loops, 5, INTEGER_OVERFLOW_ codetoanalyze/objc/performance/exit.m, call_exit_unreachable, 0, EXECUTION_TIME_UNREACHABLE_AT_EXIT, no_bucket, ERROR, [Unreachable node] codetoanalyze/objc/performance/exit.m, compute_exit_unreachable, 0, EXECUTION_TIME_UNREACHABLE_AT_EXIT, no_bucket, ERROR, [Unreachable node] codetoanalyze/objc/performance/exit.m, exit_unreachable, 0, EXECUTION_TIME_UNREACHABLE_AT_EXIT, no_bucket, ERROR, [Unreachable node] -codetoanalyze/objc/performance/field_access.m, create_common_person_constant, 2, INTEGER_OVERFLOW_U5, no_bucket, ERROR, [Unknown value from: NSNumber.numberWithInt:,Call,,Parameter `self->_bank_account`,Call,Parameter `self->_bank_account`,Assignment,,Parameter `income`,Binary operation: ([0, +oo] + [0, +oo]):unsigned64 by call to `Person.add_income_constant:` ] +codetoanalyze/objc/performance/field_access.m, create_common_person_constant, 2, INTEGER_OVERFLOW_U5, no_bucket, ERROR, [Call,Unknown value from: NSObject.init,Call,,Parameter `self->_bank_account`,Call,Parameter `self->_bank_account`,Assignment,,Parameter `income`,Binary operation: ([0, +oo] + 100):unsigned64 by call to `Person.add_income_constant:` ] codetoanalyze/objc/performance/invariant.m, while_infinite_FN, 2, CONDITION_ALWAYS_TRUE, no_bucket, WARNING, [Here] codetoanalyze/objc/performance/loops.m, if_in_loop, 5, INTEGER_OVERFLOW_L5, no_bucket, ERROR, [,Assignment,Binary operation: ([0, +oo] + 1):signed32] codetoanalyze/objc/performance/switch_continue.m, test_switch_FN, 3, CONDITION_ALWAYS_TRUE, no_bucket, WARNING, [Here]