[cost] add alloc for NSObject and NSString.init as an example function using alloc

Summary: Implement `alloc` and implement initialisation method that uses the return value of `alloc`, i.e. `NSString.init`.

Reviewed By: ezgicicek

Differential Revision: D22840080

fbshipit-source-id: 47a7523e3
master
Qianyi Shu 5 years ago committed by Facebook GitHub Bot
parent 54c89408c2
commit 3d561ca1db

@ -149,18 +149,6 @@ let malloc ~can_be_zero size_exp =
{exec; check} {exec; check}
let objc_malloc exp =
let can_be_zero = true in
let exec ({tenv} as model) ~ret mem =
match exp with
| Exp.Sizeof {typ} when PatternMatch.ObjectiveC.implements "NSArray" tenv (Typ.to_string typ) ->
(malloc ~can_be_zero Exp.zero).exec model ~ret mem
| _ ->
(malloc ~can_be_zero exp).exec model ~ret mem
in
{exec; check= check_alloc_size ~can_be_zero exp}
let calloc size_exp stride_exp = let calloc size_exp stride_exp =
let byte_size_exp = Exp.BinOp (Binop.Mult (Some Typ.size_t), size_exp, stride_exp) in let byte_size_exp = Exp.BinOp (Binop.Mult (Some Typ.size_t), size_exp, stride_exp) in
malloc byte_size_exp malloc byte_size_exp
@ -1379,7 +1367,7 @@ end
module NSString = struct module NSString = struct
let fn = JavaString.fn let fn = JavaString.fn
let create_string_from_c_string src_exp = let create_with_c_string src_exp =
let exec model_env ~ret mem = let exec model_env ~ret mem =
let v = Sem.eval_string_len src_exp mem in let v = Sem.eval_string_len src_exp mem in
JavaString.create_with_length model_env ~ret ~begin_idx:Exp.zero ~end_v:v mem JavaString.create_with_length model_env ~ret ~begin_idx:Exp.zero ~end_v:v mem
@ -1422,6 +1410,21 @@ module NSString = struct
{exec; check= no_check} {exec; check= no_check}
end end
let objc_malloc exp =
let can_be_zero = true in
let exec ({tenv} as model) ~ret mem =
match exp with
| Exp.Sizeof {typ} when PatternMatch.ObjectiveC.implements "NSArray" tenv (Typ.to_string typ) ->
(malloc ~can_be_zero Exp.zero).exec model ~ret mem
| Exp.Sizeof {typ} when PatternMatch.ObjectiveC.implements "NSString" tenv (Typ.to_string typ)
->
(NSString.create_with_c_string (Exp.Const (Const.Cstr ""))).exec model ~ret mem
| _ ->
(malloc ~can_be_zero exp).exec model ~ret mem
in
{exec; check= check_alloc_size ~can_be_zero exp}
module Preconditions = struct module Preconditions = struct
let check_argument exp = let check_argument exp =
let exec {integer_type_widths; location} ~ret:_ mem = let exec {integer_type_widths; location} ~ret:_ mem =
@ -1566,8 +1569,8 @@ module Call = struct
; -"CFArrayGetValueAtIndex" <>$ capt_var_exn $+ capt_exp $!--> NSCollection.get_at_index ; -"CFArrayGetValueAtIndex" <>$ capt_var_exn $+ capt_exp $!--> NSCollection.get_at_index
; -"CFDictionaryGetCount" <>$ capt_exp $!--> NSCollection.length ; -"CFDictionaryGetCount" <>$ capt_exp $!--> NSCollection.length
; -"MCFArrayGetCount" <>$ capt_exp $!--> NSCollection.length ; -"MCFArrayGetCount" <>$ capt_exp $!--> NSCollection.length
; +PatternMatch.ObjectiveC.implements "NSArray" &:: "array" <>--> NSCollection.empty_array
; +PatternMatch.ObjectiveC.implements "NSArray" &:: "init" <>$ capt_exp $--> id ; +PatternMatch.ObjectiveC.implements "NSArray" &:: "init" <>$ capt_exp $--> id
; +PatternMatch.ObjectiveC.implements "NSArray" &:: "array" <>--> NSCollection.empty_array
; +PatternMatch.ObjectiveC.implements "NSArray" ; +PatternMatch.ObjectiveC.implements "NSArray"
&:: "count" <>$ capt_exp $!--> NSCollection.length &:: "count" <>$ capt_exp $!--> NSCollection.length
; +PatternMatch.ObjectiveC.implements "NSArray" ; +PatternMatch.ObjectiveC.implements "NSArray"
@ -1591,8 +1594,9 @@ module Call = struct
&:: "allValues" <>$ capt_exp $--> create_copy_array &:: "allValues" <>$ capt_exp $--> create_copy_array
; +PatternMatch.ObjectiveC.implements "NSNumber" &:: "numberWithInt:" <>$ capt_exp $--> id ; +PatternMatch.ObjectiveC.implements "NSNumber" &:: "numberWithInt:" <>$ capt_exp $--> id
; +PatternMatch.ObjectiveC.implements "NSNumber" &:: "integerValue" <>$ capt_exp $--> id ; +PatternMatch.ObjectiveC.implements "NSNumber" &:: "integerValue" <>$ capt_exp $--> id
; +PatternMatch.ObjectiveC.implements "NSString" &:: "init" <>$ capt_exp $--> id
; +PatternMatch.ObjectiveC.implements "NSString" ; +PatternMatch.ObjectiveC.implements "NSString"
&:: "stringWithUTF8String:" <>$ capt_exp $!--> NSString.create_string_from_c_string &:: "stringWithUTF8String:" <>$ capt_exp $!--> NSString.create_with_c_string
; +PatternMatch.ObjectiveC.implements "NSString" ; +PatternMatch.ObjectiveC.implements "NSString"
&:: "length" <>$ capt_exp $--> NSString.length &:: "length" <>$ capt_exp $--> NSString.length
; +PatternMatch.ObjectiveC.implements "NSString" ; +PatternMatch.ObjectiveC.implements "NSString"

@ -63,6 +63,12 @@ void init_with_bytes_linear_FP(const void* bytes,
} }
} }
void init_string_constant() {
NSString* str = [[NSString alloc] init];
for (int i = 0; i < str.length; i++) {
}
}
void init_with_string_constant_FP() { void init_with_string_constant_FP() {
NSString* s = @"abcd"; NSString* s = @"abcd";
NSString* str = [[NSString alloc] initWithString:s]; NSString* str = [[NSString alloc] initWithString:s];

@ -56,6 +56,7 @@ codetoanalyze/objc/performance/NSString.m, call_init_with_string_constant_FP,
codetoanalyze/objc/performance/NSString.m, component_seperated_by_char_linear, 6 + m.length.ub + 3 ⋅ (-1+max(2, m.length.ub)) + 3 ⋅ (max(2, m.length.ub)), OnUIThread:false, [{max(2, m.length.ub)},Loop,{-1+max(2, m.length.ub)},Loop,{m.length.ub},Modeled call to NSString.componentsSeparatedByString:] codetoanalyze/objc/performance/NSString.m, component_seperated_by_char_linear, 6 + m.length.ub + 3 ⋅ (-1+max(2, m.length.ub)) + 3 ⋅ (max(2, m.length.ub)), OnUIThread:false, [{max(2, m.length.ub)},Loop,{-1+max(2, m.length.ub)},Loop,{m.length.ub},Modeled call to NSString.componentsSeparatedByString:]
codetoanalyze/objc/performance/NSString.m, component_seperated_by_string_linear, 6 + sep.length.ub × m.length.ub + 3 ⋅ (-1+max(2, m.length.ub)) + 3 ⋅ (max(2, m.length.ub)), OnUIThread:false, [{max(2, m.length.ub)},Loop,{-1+max(2, m.length.ub)},Loop,{m.length.ub},Modeled call to NSString.componentsSeparatedByString:,{sep.length.ub},Modeled call to NSString.componentsSeparatedByString:] codetoanalyze/objc/performance/NSString.m, component_seperated_by_string_linear, 6 + sep.length.ub × m.length.ub + 3 ⋅ (-1+max(2, m.length.ub)) + 3 ⋅ (max(2, m.length.ub)), OnUIThread:false, [{max(2, m.length.ub)},Loop,{-1+max(2, m.length.ub)},Loop,{m.length.ub},Modeled call to NSString.componentsSeparatedByString:,{sep.length.ub},Modeled call to NSString.componentsSeparatedByString:]
codetoanalyze/objc/performance/NSString.m, has_prefix_constant, 13, OnUIThread:false, [] codetoanalyze/objc/performance/NSString.m, has_prefix_constant, 13, OnUIThread:false, []
codetoanalyze/objc/performance/NSString.m, init_string_constant, 9, OnUIThread:false, []
codetoanalyze/objc/performance/NSString.m, init_with_bytes_linear_FP, , OnUIThread:false, [Unbounded loop,Loop] codetoanalyze/objc/performance/NSString.m, init_with_bytes_linear_FP, , OnUIThread:false, [Unbounded loop,Loop]
codetoanalyze/objc/performance/NSString.m, init_with_string_constant_FP, , OnUIThread:false, [Unbounded loop,Loop] codetoanalyze/objc/performance/NSString.m, init_with_string_constant_FP, , OnUIThread:false, [Unbounded loop,Loop]
codetoanalyze/objc/performance/NSString.m, init_with_string_linear_FP, , OnUIThread:false, [Unbounded loop,Loop] codetoanalyze/objc/performance/NSString.m, init_with_string_linear_FP, , OnUIThread:false, [Unbounded loop,Loop]

@ -19,6 +19,7 @@ codetoanalyze/objc/performance/NSMutableArray.m, nsmarray_remove_in_loop_constan
codetoanalyze/objc/performance/NSMutableArray.m, nsmarray_set_in_loop_constant, 6, CONDITION_ALWAYS_FALSE, no_bucket, WARNING, [Here] codetoanalyze/objc/performance/NSMutableArray.m, nsmarray_set_in_loop_constant, 6, CONDITION_ALWAYS_FALSE, no_bucket, WARNING, [Here]
codetoanalyze/objc/performance/NSMutableArray.m, nsmarray_set_linear, 2, INTEGER_OVERFLOW_U5, no_bucket, ERROR, [<LHS trace>,Unknown value from: NSNumber.intValue,Binary operation: ([-oo, +oo] + 1):signed32] codetoanalyze/objc/performance/NSMutableArray.m, nsmarray_set_linear, 2, INTEGER_OVERFLOW_U5, no_bucket, ERROR, [<LHS trace>,Unknown value from: NSNumber.intValue,Binary operation: ([-oo, +oo] + 1):signed32]
codetoanalyze/objc/performance/NSString.m, call_init_with_string_constant_FP, 0, INFINITE_EXECUTION_TIME, no_bucket, ERROR, [Call to init_with_string_linear_FP,Unbounded loop,Loop] codetoanalyze/objc/performance/NSString.m, call_init_with_string_constant_FP, 0, INFINITE_EXECUTION_TIME, no_bucket, ERROR, [Call to init_with_string_linear_FP,Unbounded loop,Loop]
codetoanalyze/objc/performance/NSString.m, init_string_constant, 2, CONDITION_ALWAYS_FALSE, no_bucket, WARNING, [Here]
codetoanalyze/objc/performance/NSString.m, init_with_bytes_linear_FP, 0, INFINITE_EXECUTION_TIME, no_bucket, ERROR, [Unbounded loop,Loop] codetoanalyze/objc/performance/NSString.m, init_with_bytes_linear_FP, 0, INFINITE_EXECUTION_TIME, no_bucket, ERROR, [Unbounded loop,Loop]
codetoanalyze/objc/performance/NSString.m, init_with_bytes_linear_FP, 6, INTEGER_OVERFLOW_U5, no_bucket, ERROR, [<LHS trace>,Unknown value from: NSString.initWithBytes:length:encoding:,Binary operation: ([0, +oo] + 1):signed32] codetoanalyze/objc/performance/NSString.m, init_with_bytes_linear_FP, 6, INTEGER_OVERFLOW_U5, no_bucket, ERROR, [<LHS trace>,Unknown value from: NSString.initWithBytes:length:encoding:,Binary operation: ([0, +oo] + 1):signed32]
codetoanalyze/objc/performance/NSString.m, init_with_string_constant_FP, 0, INFINITE_EXECUTION_TIME, no_bucket, ERROR, [Unbounded loop,Loop] codetoanalyze/objc/performance/NSString.m, init_with_string_constant_FP, 0, INFINITE_EXECUTION_TIME, no_bucket, ERROR, [Unbounded loop,Loop]

Loading…
Cancel
Save