Skip to content

Commit 482c605

Browse files
authored
merge main into amd-staging (llvm#1862)
2 parents a06e864 + ae25b4d commit 482c605

File tree

33 files changed

+838
-288
lines changed

33 files changed

+838
-288
lines changed

clang/lib/StaticAnalyzer/Checkers/WebKit/RetainPtrCtorAdoptChecker.cpp

Lines changed: 250 additions & 35 deletions
Large diffs are not rendered by default.

clang/test/Analysis/Checkers/WebKit/objc-mock-types.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ typedef struct CF_BRIDGED_TYPE(id) __CVBuffer *CVBufferRef;
6161
typedef CVBufferRef CVImageBufferRef;
6262
typedef CVImageBufferRef CVPixelBufferRef;
6363
typedef signed int CVReturn;
64-
CVReturn CVPixelBufferCreateWithIOSurface(CFAllocatorRef allocator, IOSurfaceRef surface, CFDictionaryRef pixelBufferAttributes, CVPixelBufferRef * pixelBufferOut);
64+
CVReturn CVPixelBufferCreateWithIOSurface(CFAllocatorRef allocator, IOSurfaceRef surface, CFDictionaryRef pixelBufferAttributes, CF_RETURNS_RETAINED CVPixelBufferRef * pixelBufferOut);
6565

6666
CFRunLoopRef CFRunLoopGetCurrent(void);
6767
CFRunLoopRef CFRunLoopGetMain(void);
@@ -70,6 +70,12 @@ extern void CFRelease(CFTypeRef cf);
7070
#define CFSTR(cStr) ((CFStringRef) __builtin___CFStringMakeConstantString ("" cStr ""))
7171
extern Class NSClassFromString(NSString *aClassName);
7272

73+
#if __has_feature(objc_arc)
74+
id CFBridgingRelease(CFTypeRef X) {
75+
return (__bridge_transfer id)X;
76+
}
77+
#endif
78+
7379
__attribute__((objc_root_class))
7480
@interface NSObject
7581
+ (instancetype) alloc;
@@ -130,11 +136,13 @@ __attribute__((objc_root_class))
130136

131137
@interface NSNumber : NSValue
132138
- (char)charValue;
139+
- (int)intValue;
133140
- (id)initWithInt:(int)value;
134141
+ (NSNumber *)numberWithInt:(int)value;
135142
@end
136143

137144
@interface SomeObj : NSObject
145+
- (instancetype)_init;
138146
- (SomeObj *)mutableCopy;
139147
- (SomeObj *)copyWithValue:(int)value;
140148
- (void)doWork;

clang/test/Analysis/Checkers/WebKit/retain-ptr-ctor-adopt-use-arc.mm

Lines changed: 193 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,78 @@ void basic_wrong() {
2727
RetainPtr<CFMutableArrayRef> cf1 = CFArrayCreateMutable(kCFAllocatorDefault, 10);
2828
// expected-warning@-1{{Incorrect use of RetainPtr constructor. The argument is +1 and results in a memory leak [alpha.webkit.RetainPtrCtorAdoptChecker]}}
2929
RetainPtr<CFMutableArrayRef> cf2 = adoptCF(provide_cf());
30-
// expected-warning@-1{{Incorrect use of adoptCF. The argument is +0 and results in an use-after-free [alpha.webkit.RetainPtrCtorAdoptChecker]}}
30+
// expected-warning@-1{{Incorrect use of adoptCF. The argument is +0 and results in an use-after-free when ARC is disabled [alpha.webkit.RetainPtrCtorAdoptChecker]}}
3131
RetainPtr<CFTypeRef> cf3 = SecTaskCreateFromSelf(kCFAllocatorDefault);
3232
// expected-warning@-1{{Incorrect use of RetainPtr constructor. The argument is +1 and results in a memory leak [alpha.webkit.RetainPtrCtorAdoptChecker]}}
3333
CFCopyArray(cf1);
3434
// expected-warning@-1{{The return value is +1 and results in a memory leak [alpha.webkit.RetainPtrCtorAdoptChecker]}}
3535
}
3636

37+
void basic_correct_arc() {
38+
auto *obj = [[SomeObj alloc] init];
39+
[obj doWork];
40+
}
41+
42+
@implementation SomeObj {
43+
NSNumber *_number;
44+
SomeObj *_next;
45+
SomeObj *_other;
46+
}
47+
48+
- (instancetype)_init {
49+
self = [super init];
50+
_number = nil;
51+
_next = nil;
52+
_other = nil;
53+
return self;
54+
}
55+
56+
- (SomeObj *)mutableCopy {
57+
auto *copy = [[SomeObj alloc] init];
58+
[copy setValue:_number];
59+
[copy setNext:_next];
60+
[copy setOther:_other];
61+
return copy;
62+
}
63+
64+
- (SomeObj *)copyWithValue:(int)value {
65+
auto *copy = [[SomeObj alloc] init];
66+
[copy setValue:_number];
67+
[copy setNext:_next];
68+
[copy setOther:_other];
69+
return copy;
70+
}
71+
72+
- (void)doWork {
73+
_number = [[NSNumber alloc] initWithInt:5];
74+
}
75+
76+
- (SomeObj *)other {
77+
return _other;
78+
}
79+
80+
- (void)setOther:(SomeObj *)obj {
81+
_other = obj;
82+
}
83+
84+
- (SomeObj *)next {
85+
return _next;
86+
}
87+
88+
- (void)setNext:(SomeObj *)obj {
89+
_next = obj;
90+
}
91+
92+
- (int)value {
93+
return [_number intValue];
94+
}
95+
96+
- (void)setValue:(NSNumber *)value {
97+
_number = value;
98+
}
99+
100+
@end;
101+
37102
RetainPtr<CVPixelBufferRef> cf_out_argument() {
38103
auto surface = adoptCF(IOSurfaceCreate(nullptr));
39104
CVPixelBufferRef rawBuffer = nullptr;
@@ -60,7 +125,46 @@ void cast_retainptr() {
60125
RetainPtr<CFArrayRef> v = static_cast<CFArrayRef>(baz);
61126
}
62127

63-
SomeObj* allocSomeObj();
128+
CFTypeRef CopyWrapper() {
129+
return CopyValueForSomething();
130+
}
131+
132+
CFTypeRef LeakWrapper() {
133+
return CopyValueForSomething();
134+
// expected-warning@-1{{The return value is +1 and results in a memory leak [alpha.webkit.RetainPtrCtorAdoptChecker]}}
135+
}
136+
137+
NSArray *makeArray() NS_RETURNS_RETAINED {
138+
return CFBridgingRelease(CFArrayCreateMutable(kCFAllocatorDefault, 10));
139+
}
140+
141+
extern Class (*getNSArrayClass)();
142+
NSArray *allocArrayInstance() NS_RETURNS_RETAINED {
143+
return [[getNSArrayClass() alloc] init];
144+
}
145+
146+
extern int (*GetObj)(CF_RETURNS_RETAINED CFTypeRef* objOut);
147+
RetainPtr<CFTypeRef> getObject() {
148+
CFTypeRef obj = nullptr;
149+
if (GetObj(&obj))
150+
return nullptr;
151+
return adoptCF(obj);
152+
}
153+
154+
CFArrayRef CreateSingleArray(CFStringRef);
155+
CFArrayRef CreateSingleArray(CFDictionaryRef);
156+
CFArrayRef CreateSingleArray(CFArrayRef);
157+
template <typename ElementType>
158+
static RetainPtr<CFArrayRef> makeArrayWithSingleEntry(ElementType arg) {
159+
return adoptCF(CreateSingleArray(arg));
160+
}
161+
162+
void callMakeArayWithSingleEntry() {
163+
auto dictionary = adoptCF(CFDictionaryCreate(kCFAllocatorDefault, nullptr, nullptr, 0));
164+
makeArrayWithSingleEntry(dictionary.get());
165+
}
166+
167+
SomeObj* allocSomeObj() CF_RETURNS_RETAINED;
64168

65169
void adopt_retainptr() {
66170
RetainPtr<NSObject> foo = adoptNS([[SomeObj alloc] init]);
@@ -118,3 +222,90 @@ void mutable_copy_array() {
118222
void string_copy(NSString *str) {
119223
RetainPtr<NSString> copy = adoptNS(str.copy);
120224
}
225+
226+
void alloc_init_spi() {
227+
auto ptr = adoptNS([[SomeObj alloc] _init]);
228+
}
229+
230+
void alloc_init_c_function() {
231+
RetainPtr ptr = adoptNS([allocSomeObj() init]);
232+
}
233+
234+
CFArrayRef make_array() CF_RETURNS_RETAINED;
235+
236+
RetainPtr<CFArrayRef> adopt_make_array() {
237+
return adoptCF(make_array());
238+
}
239+
240+
@interface SomeObject : NSObject
241+
-(void)basic_correct;
242+
-(void)basic_wrong;
243+
-(NSString *)leak_string;
244+
-(NSString *)make_string NS_RETURNS_RETAINED;
245+
@property (nonatomic, readonly) SomeObj *obj;
246+
@end
247+
248+
@implementation SomeObject
249+
-(void)basic_correct {
250+
auto ns1 = adoptNS([SomeObj alloc]);
251+
auto ns2 = adoptNS([[SomeObj alloc] init]);
252+
RetainPtr<SomeObj> ns3 = [ns1.get() next];
253+
auto ns4 = adoptNS([ns3 mutableCopy]);
254+
auto ns5 = adoptNS([ns3 copyWithValue:3]);
255+
auto ns6 = retainPtr([ns3 next]);
256+
CFMutableArrayRef cf1 = adoptCF(CFArrayCreateMutable(kCFAllocatorDefault, 10));
257+
auto cf2 = adoptCF(SecTaskCreateFromSelf(kCFAllocatorDefault));
258+
auto cf3 = adoptCF(checked_cf_cast<CFArrayRef>(CFCopyArray(cf1)));
259+
}
260+
261+
-(void)basic_wrong {
262+
RetainPtr<SomeObj> ns1 = [[SomeObj alloc] init];
263+
// expected-warning@-1{{Incorrect use of RetainPtr constructor. The argument is +1 and results in a memory leak when ARC is disabled [alpha.webkit.RetainPtrCtorAdoptChecker]}}
264+
auto ns2 = adoptNS([ns1.get() next]);
265+
// expected-warning@-1{{Incorrect use of adoptNS. The argument is +0 and results in an use-after-free when ARC is disabled [alpha.webkit.RetainPtrCtorAdoptChecker]}}
266+
RetainPtr<CFMutableArrayRef> cf1 = CFArrayCreateMutable(kCFAllocatorDefault, 10);
267+
// expected-warning@-1{{Incorrect use of RetainPtr constructor. The argument is +1 and results in a memory leak [alpha.webkit.RetainPtrCtorAdoptChecker]}}
268+
RetainPtr<CFMutableArrayRef> cf2 = adoptCF(provide_cf());
269+
// expected-warning@-1{{Incorrect use of adoptCF. The argument is +0 and results in an use-after-free when ARC is disabled [alpha.webkit.RetainPtrCtorAdoptChecker]}}
270+
RetainPtr<CFTypeRef> cf3 = SecTaskCreateFromSelf(kCFAllocatorDefault);
271+
// expected-warning@-1{{Incorrect use of RetainPtr constructor. The argument is +1 and results in a memory leak [alpha.webkit.RetainPtrCtorAdoptChecker]}}
272+
CFCopyArray(cf1);
273+
// expected-warning@-1{{The return value is +1 and results in a memory leak [alpha.webkit.RetainPtrCtorAdoptChecker]}}
274+
}
275+
276+
-(NSString *)leak_string {
277+
return [[NSString alloc] initWithUTF8String:"hello"];
278+
}
279+
280+
-(NSString *)make_string {
281+
return [[NSString alloc] initWithUTF8String:"hello"];
282+
}
283+
284+
-(void)local_leak_string {
285+
if ([[NSString alloc] initWithUTF8String:"hello"]) {
286+
}
287+
}
288+
289+
-(void)make_some_obj {
290+
auto some_obj = adoptNS([allocSomeObj() init]);
291+
}
292+
293+
-(void)alloc_init_bad_order {
294+
auto *obj = [NSObject alloc];
295+
auto ptr = adoptNS([obj init]);
296+
// expected-warning@-1{{Incorrect use of adoptNS. The argument is +0 and results in an use-after-free when ARC is disabled [alpha.webkit.RetainPtrCtorAdoptChecker]}}
297+
}
298+
299+
-(void)alloc_init_good_order {
300+
auto obj = adoptNS([NSObject alloc]);
301+
(void)[obj init];
302+
}
303+
304+
-(void)copy_assign_ivar {
305+
_obj = [allocSomeObj() init];
306+
}
307+
308+
-(void)do_more_work:(OtherObj *)otherObj {
309+
[otherObj doMoreWork:[[OtherObj alloc] init]];
310+
}
311+
@end

0 commit comments

Comments
 (0)