2017-03-03 06:58:06 +08:00
|
|
|
// RUN: %clang_cc1 -triple x86_64-apple-darwin10 -analyze -analyzer-checker=core -analyzer-store=region -fblocks -analyzer-opt-analyze-nested-blocks -verify %s
|
|
|
|
// RUN: %clang_cc1 -triple x86_64-apple-darwin10 -analyze -analyzer-checker=core -analyzer-store=region -fblocks -analyzer-opt-analyze-nested-blocks -verify -x objective-c++ %s
|
2009-12-02 06:47:46 +08:00
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// The following code is reduced using delta-debugging from Mac OS X headers:
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
typedef __builtin_va_list va_list;
|
|
|
|
typedef unsigned int uint32_t;
|
|
|
|
typedef struct dispatch_queue_s *dispatch_queue_t;
|
|
|
|
typedef struct dispatch_queue_attr_s *dispatch_queue_attr_t;
|
|
|
|
typedef void (^dispatch_block_t)(void);
|
|
|
|
void dispatch_async(dispatch_queue_t queue, dispatch_block_t block);
|
|
|
|
__attribute__((visibility("default"))) __attribute__((__malloc__)) __attribute__((__warn_unused_result__)) __attribute__((__nothrow__)) dispatch_queue_t dispatch_queue_create(const char *label, dispatch_queue_attr_t attr);
|
|
|
|
typedef long dispatch_once_t;
|
|
|
|
void dispatch_once(dispatch_once_t *predicate, dispatch_block_t block);
|
2013-05-31 02:14:27 +08:00
|
|
|
dispatch_queue_t
|
|
|
|
dispatch_queue_create(const char *label, dispatch_queue_attr_t attr);
|
|
|
|
|
|
|
|
|
2009-12-02 06:47:46 +08:00
|
|
|
typedef signed char BOOL;
|
|
|
|
typedef unsigned long NSUInteger;
|
|
|
|
typedef struct _NSZone NSZone;
|
|
|
|
@class NSInvocation, NSMethodSignature, NSCoder, NSString, NSEnumerator;
|
|
|
|
@protocol NSObject
|
|
|
|
- (BOOL)isEqual:(id)object;
|
|
|
|
- (oneway void)release;
|
|
|
|
@end
|
|
|
|
@protocol NSCopying - (id)copyWithZone:(NSZone *)zone; @end
|
|
|
|
@protocol NSMutableCopying - (id)mutableCopyWithZone:(NSZone *)zone; @end
|
|
|
|
@protocol NSCoding - (void)encodeWithCoder:(NSCoder *)aCoder; @end
|
|
|
|
@interface NSObject <NSObject> {}
|
|
|
|
+ (id)alloc;
|
2012-11-15 07:03:55 +08:00
|
|
|
- (id)init;
|
2012-08-18 04:16:34 +08:00
|
|
|
- (id)copy;
|
2009-12-02 06:47:46 +08:00
|
|
|
@end
|
|
|
|
extern id NSAllocateObject(Class aClass, NSUInteger extraBytes, NSZone *zone);
|
2012-08-18 04:16:34 +08:00
|
|
|
@interface NSString : NSObject <NSCopying, NSMutableCopying, NSCoding>
|
|
|
|
- (NSUInteger)length;
|
|
|
|
- (const char *)UTF8String;
|
2009-12-02 06:47:46 +08:00
|
|
|
- (id)initWithFormat:(NSString *)format arguments:(va_list)argList __attribute__((format(__NSString__, 1, 0)));
|
|
|
|
@end
|
|
|
|
@class NSString, NSData;
|
|
|
|
typedef struct cssm_sample {} CSSM_SAMPLEGROUP, *CSSM_SAMPLEGROUP_PTR;
|
|
|
|
typedef struct __aslclient *aslclient;
|
|
|
|
typedef struct __aslmsg *aslmsg;
|
|
|
|
aslclient asl_open(const char *ident, const char *facility, uint32_t opts);
|
|
|
|
int asl_log(aslclient asl, aslmsg msg, int level, const char *format, ...) __attribute__((__format__ (__printf__, 4, 5)));
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Begin actual test cases.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
// test1 - This test case exposed logic that caused the analyzer to crash because of a memory bug
|
|
|
|
// in BlockDataRegion. It represents real code that contains two block literals. Eventually
|
|
|
|
// via IPA 'logQueue' and 'client' should be updated after the call to 'dispatch_once'.
|
|
|
|
void test1(NSString *format, ...) {
|
|
|
|
static dispatch_queue_t logQueue;
|
|
|
|
static aslclient client;
|
|
|
|
static dispatch_once_t pred;
|
|
|
|
do {
|
|
|
|
if (__builtin_expect(*(&pred), ~0l) != ~0l)
|
|
|
|
dispatch_once(&pred, ^{
|
2013-05-31 02:14:27 +08:00
|
|
|
logQueue = dispatch_queue_create("com.mycompany.myproduct.asl", 0);
|
|
|
|
client = asl_open(((char*)0), "com.mycompany.myproduct", 0);
|
2009-12-02 06:47:46 +08:00
|
|
|
});
|
|
|
|
} while (0);
|
|
|
|
|
|
|
|
va_list args;
|
|
|
|
__builtin_va_start(args, format);
|
|
|
|
|
|
|
|
NSString *str = [[NSString alloc] initWithFormat:format arguments:args];
|
2013-05-31 02:14:27 +08:00
|
|
|
dispatch_async(logQueue, ^{ asl_log(client, ((aslmsg)0), 4, "%s", [str UTF8String]); });
|
2009-12-02 06:47:46 +08:00
|
|
|
[str release];
|
|
|
|
|
|
|
|
__builtin_va_end(args);
|
|
|
|
}
|
2010-02-16 16:33:59 +08:00
|
|
|
|
|
|
|
// test2 - Test that captured variables that are uninitialized are flagged
|
|
|
|
// as such.
|
|
|
|
void test2() {
|
|
|
|
static int y = 0;
|
|
|
|
int x;
|
2011-01-26 03:13:42 +08:00
|
|
|
^{ y = x + 1; }(); // expected-warning{{Variable 'x' is uninitialized when captured by block}}
|
2010-02-16 16:33:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void test2_b() {
|
|
|
|
static int y = 0;
|
|
|
|
__block int x;
|
2012-06-02 04:04:04 +08:00
|
|
|
^{ y = x + 1; }(); // expected-warning {{left operand of '+' is a garbage value}}
|
2010-02-16 16:33:59 +08:00
|
|
|
}
|
2010-02-17 00:55:10 +08:00
|
|
|
|
|
|
|
void test2_c() {
|
|
|
|
typedef void (^myblock)(void);
|
2011-01-26 03:13:42 +08:00
|
|
|
myblock f = ^() { f(); }; // expected-warning{{Variable 'f' is uninitialized when captured by block}}
|
2012-08-18 04:16:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void testMessaging() {
|
|
|
|
// <rdar://problem/12119814>
|
|
|
|
[[^(){} copy] release];
|
|
|
|
}
|
2012-11-15 07:03:55 +08:00
|
|
|
|
|
|
|
|
2012-11-15 07:09:52 +08:00
|
|
|
@interface rdar12415065 : NSObject
|
2012-11-15 07:03:55 +08:00
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation rdar12415065
|
|
|
|
- (void)test {
|
|
|
|
// At one point this crashed because we created a path note at a
|
|
|
|
// PreStmtPurgeDeadSymbols point but only knew how to deal with PostStmt
|
|
|
|
// points. <rdar://problem/12687586>
|
|
|
|
|
|
|
|
extern dispatch_queue_t queue;
|
|
|
|
|
|
|
|
if (!queue)
|
|
|
|
return;
|
|
|
|
|
2012-12-06 15:17:26 +08:00
|
|
|
// This previously was a false positive with 'x' being flagged as being
|
|
|
|
// uninitialized when captured by the exterior block (when it is only
|
|
|
|
// captured by the interior block).
|
2012-11-15 07:03:55 +08:00
|
|
|
dispatch_async(queue, ^{
|
|
|
|
double x = 0.0;
|
|
|
|
if (24.0f < x) {
|
|
|
|
dispatch_async(queue, ^{ (void)x; });
|
|
|
|
[self test];
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
@end
|
2013-05-31 02:14:27 +08:00
|
|
|
|
|
|
|
void testReturnVariousSignatures() {
|
|
|
|
(void)^int(){
|
|
|
|
return 42;
|
|
|
|
}();
|
|
|
|
|
|
|
|
(void)^int{
|
|
|
|
return 42;
|
|
|
|
}();
|
|
|
|
|
|
|
|
(void)^(){
|
|
|
|
return 42;
|
|
|
|
}();
|
|
|
|
|
|
|
|
(void)^{
|
|
|
|
return 42;
|
|
|
|
}();
|
|
|
|
}
|
2013-11-20 08:11:42 +08:00
|
|
|
|
|
|
|
// This test used to cause infinite loop in the region invalidation.
|
|
|
|
void blockCapturesItselfInTheLoop(int x, int m) {
|
|
|
|
void (^assignData)(int) = ^(int x){
|
|
|
|
x++;
|
|
|
|
};
|
|
|
|
while (m < 0) {
|
|
|
|
void (^loop)(int);
|
|
|
|
loop = ^(int x) {
|
|
|
|
assignData(x);
|
|
|
|
};
|
|
|
|
assignData = loop;
|
|
|
|
m++;
|
|
|
|
}
|
|
|
|
assignData(x);
|
|
|
|
}
|
[analyzer] When forced to fake a block type, do it correctly.
BlockDecl has a poor AST representation because it doesn't carry its type
with it. Instead, the containing BlockExpr has the full type. This almost
never matters for the analyzer, but if the block decl contains static
local variables we need to synthesize a region to put them in, and this
region will necessarily not have the right type.
Even /that/ doesn't matter, unless
(1) the block calls the function or method containing the block, and
(2) the value of the block expr is used in some interesting way.
In this case, we actually end up needing the type of the block region,
and it will be set to our synthesized type. It turns out we've been doing
a terrible job faking that type -- it wasn't a block pointer type at all.
This commit fixes that to at least guarantee a block pointer type, using
the signature written by the user if there is one.
This is not really a correct answer because the block region's type will
/still/ be wrong, but further efforts to make this right in the analyzer
would probably be silly. We should just change the AST.
rdar://problem/21698099
llvm-svn: 241944
2015-07-11 05:41:59 +08:00
|
|
|
|
|
|
|
// Blocks that called the function they were contained in that also have
|
|
|
|
// static locals caused crashes.
|
|
|
|
// rdar://problem/21698099
|
|
|
|
void takeNonnullBlock(void (^)(void)) __attribute__((nonnull));
|
|
|
|
void takeNonnullIntBlock(int (^)(void)) __attribute__((nonnull));
|
|
|
|
|
|
|
|
void testCallContainingWithSignature1()
|
|
|
|
{
|
|
|
|
takeNonnullBlock(^{
|
|
|
|
static const char str[] = "Lost connection to sharingd";
|
|
|
|
testCallContainingWithSignature1();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
void testCallContainingWithSignature2()
|
|
|
|
{
|
|
|
|
takeNonnullBlock(^void{
|
|
|
|
static const char str[] = "Lost connection to sharingd";
|
|
|
|
testCallContainingWithSignature2();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
void testCallContainingWithSignature3()
|
|
|
|
{
|
|
|
|
takeNonnullBlock(^void(){
|
|
|
|
static const char str[] = "Lost connection to sharingd";
|
|
|
|
testCallContainingWithSignature3();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
void testCallContainingWithSignature4()
|
|
|
|
{
|
|
|
|
takeNonnullBlock(^void(void){
|
|
|
|
static const char str[] = "Lost connection to sharingd";
|
|
|
|
testCallContainingWithSignature4();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
void testCallContainingWithSignature5()
|
|
|
|
{
|
|
|
|
takeNonnullIntBlock(^{
|
|
|
|
static const char str[] = "Lost connection to sharingd";
|
|
|
|
testCallContainingWithSignature5();
|
|
|
|
return 0;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-02-24 06:26:04 +08:00
|
|
|
__attribute__((objc_root_class))
|
|
|
|
@interface SuperClass
|
|
|
|
- (void)someMethod;
|
|
|
|
@end
|
|
|
|
|
|
|
|
@interface SomeClass : SuperClass
|
|
|
|
@end
|
|
|
|
|
|
|
|
// Make sure to properly handle super-calls when a block captures
|
|
|
|
// a local variable named 'self'.
|
|
|
|
@implementation SomeClass
|
|
|
|
-(void)foo; {
|
|
|
|
/*__weak*/ SomeClass *weakSelf = self;
|
|
|
|
(void)(^(void) {
|
|
|
|
SomeClass *self = weakSelf;
|
|
|
|
(void)(^(void) {
|
|
|
|
(void)self;
|
|
|
|
[super someMethod]; // no-warning
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
@end
|
2016-11-16 02:40:46 +08:00
|
|
|
|
|
|
|
// The incorrect block variable initialization below is a hard compile-time
|
|
|
|
// error in C++.
|
|
|
|
#if !defined(__cplusplus)
|
|
|
|
void call_block_with_fewer_arguments() {
|
|
|
|
void (^b)() = ^(int a) { };
|
|
|
|
b(); // expected-warning {{Block taking 1 argument is called with fewer (0)}}
|
|
|
|
}
|
|
|
|
#endif
|