From 342bfb418af84590b6309f0fcdf74ed9d225dfbe Mon Sep 17 00:00:00 2001 From: Nikos Gorogiannis Date: Fri, 26 Oct 2018 02:57:50 -0700 Subject: [PATCH] [racerd] fix NSObject.init treatment of ownership Summary: NSObject.init has no summary yet isn't recognised as allocating memory. This breaks ownership tracking. Reviewed By: jvillard Differential Revision: D10852627 fbshipit-source-id: 95e016c84 --- infer/src/concurrency/RacerDModels.ml | 13 ++++++- .../tests/codetoanalyze/objcpp/racerd/Ctor.mm | 35 +++++++++++++++++++ .../objcpp/racerd/CtorInherit.mm | 27 ++++++++++++++ 3 files changed, 74 insertions(+), 1 deletion(-) create mode 100644 infer/tests/codetoanalyze/objcpp/racerd/Ctor.mm create mode 100644 infer/tests/codetoanalyze/objcpp/racerd/CtorInherit.mm diff --git a/infer/src/concurrency/RacerDModels.ml b/infer/src/concurrency/RacerDModels.ml index c0822b3bc..c610950b8 100644 --- a/infer/src/concurrency/RacerDModels.ml +++ b/infer/src/concurrency/RacerDModels.ml @@ -201,9 +201,20 @@ let is_functional pname = is_annotated_functional pname || is_modeled_functional pname +let nsobject = Typ.Name.Objc.from_qual_name (QualifiedCppName.of_qual_string "NSObject") + let acquires_ownership pname tenv = + let is_nsobject_init = function + | Typ.Procname.ObjC_Cpp + {kind= Typ.Procname.ObjC_Cpp.ObjCInstanceMethod; method_name= "init"; class_name} -> + Typ.Name.equal class_name nsobject + | _ -> + false + in let is_allocation pn = - Typ.Procname.equal pn BuiltinDecl.__new || Typ.Procname.equal pn BuiltinDecl.__new_array + Typ.Procname.equal pn BuiltinDecl.__new + || Typ.Procname.equal pn BuiltinDecl.__new_array + || is_nsobject_init pn in (* identify library functions that maintain ownership invariants behind the scenes *) let is_owned_in_library = function diff --git a/infer/tests/codetoanalyze/objcpp/racerd/Ctor.mm b/infer/tests/codetoanalyze/objcpp/racerd/Ctor.mm new file mode 100644 index 000000000..542c68c95 --- /dev/null +++ b/infer/tests/codetoanalyze/objcpp/racerd/Ctor.mm @@ -0,0 +1,35 @@ +/* + * Copyright (c) 2018-present, Facebook, Inc. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ +#import +#import + +@interface Ctor : NSObject +- (instancetype)init; +- (void)write:(int)data; +@end + +@implementation Ctor { + std::mutex _mutex; + int _data; +} + +- (instancetype)init { + if (!(self = [super init])) { + return nil; + } + + int i = _data; + + return self; +} + +- (void)write:(int)data { + _mutex.lock(); + _data = data; + _mutex.unlock(); +} +@end diff --git a/infer/tests/codetoanalyze/objcpp/racerd/CtorInherit.mm b/infer/tests/codetoanalyze/objcpp/racerd/CtorInherit.mm new file mode 100644 index 000000000..fe7721011 --- /dev/null +++ b/infer/tests/codetoanalyze/objcpp/racerd/CtorInherit.mm @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2018-present, Facebook, Inc. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ +#import "Ctor.mm" +#import + +@interface CtorInherit : Ctor +- (instancetype)init; +- (void)writeZero; +@end + +@implementation CtorInherit + +- (instancetype)init { + if (!(self = [super init])) { + return nil; + } + return self; +} + +- (void)writeZero { + [self write:0]; +} +@end