1 // RUN: %clang_analyze_cc1 -triple x86_64-apple-darwin10 -analyzer-checker=core,alpha.core.StackAddressAsyncEscape -fblocks -fobjc-arc -verify %s
3 typedef struct dispatch_queue_s *dispatch_queue_t;
4 typedef void (^dispatch_block_t)(void);
5 void dispatch_async(dispatch_queue_t queue, dispatch_block_t block);
6 typedef long dispatch_once_t;
7 void dispatch_once(dispatch_once_t *predicate, dispatch_block_t block);
8 typedef long dispatch_time_t;
9 void dispatch_after(dispatch_time_t when, dispatch_queue_t queue, dispatch_block_t block);
10 void dispatch_barrier_sync(dispatch_queue_t queue, dispatch_block_t block);
13 extern dispatch_queue_t queue;
14 extern dispatch_once_t *predicate;
15 extern dispatch_time_t when;
17 void test_block_expr_async() {
21 dispatch_async(queue, ^{
24 // expected-warning@-3 {{Address of stack memory associated with local variable 'x' \
25 is captured by an asynchronously-executed block}}
28 void test_block_expr_once_no_leak() {
31 // synchronous, no warning
32 dispatch_once(predicate, ^{
37 void test_block_expr_after() {
40 dispatch_after(when, queue, ^{
43 // expected-warning@-3 {{Address of stack memory associated with local variable 'x' \
44 is captured by an asynchronously-executed block}}
47 void test_block_expr_async_no_leak() {
51 dispatch_async(queue, ^{
57 void test_block_var_async() {
60 void (^b)(void) = ^void(void) {
63 dispatch_async(queue, b);
64 // expected-warning@-1 {{Address of stack memory associated with local variable 'x' \
65 is captured by an asynchronously-executed block}}
68 void test_block_with_ref_async() {
71 void (^b)(void) = ^void(void) {
74 dispatch_async(queue, b);
75 // expected-warning@-1 {{Address of stack memory associated with local variable 'x' \
76 is captured by an asynchronously-executed block}}
79 dispatch_block_t get_leaking_block() {
85 // expected-warning@-3 {{Address of stack memory associated with local variable 'leaked_x' \
86 is captured by a returned block}}
89 void test_returned_from_func_block_async() {
90 dispatch_async(queue, get_leaking_block());
91 // expected-warning@-1 {{Address of stack memory associated with local variable 'leaked_x' \
92 is captured by an asynchronously-executed block}}
95 // synchronous, no leak
96 void test_block_var_once() {
99 void (^b)(void) = ^void(void) {
102 dispatch_once(predicate, b); // no-warning
105 void test_block_var_after() {
108 void (^b)(void) = ^void(void) {
111 dispatch_after(when, queue, b);
112 // expected-warning@-1 {{Address of stack memory associated with local variable 'x' \
113 is captured by an asynchronously-executed block}}
116 void test_block_var_async_no_leak() {
119 void (^b)(void) = ^void(void) {
123 dispatch_async(queue, b); // no-warning
126 void test_block_inside_block_async_no_leak() {
129 void (^inner)(void) = ^void(void) {
133 void (^outer)(void) = ^void(void) {
138 dispatch_async(queue, outer); // no-warning
141 dispatch_block_t accept_and_pass_back_block(dispatch_block_t block) {
143 return block; // no-warning
146 void test_passing_continuation_no_leak() {
149 void (^cont)(void) = ^void(void) {
152 accept_and_pass_back_block(cont); // no-warning
157 @protocol OS_dispatch_semaphore
159 typedef NSObject<OS_dispatch_semaphore> *dispatch_semaphore_t;
160 dispatch_semaphore_t dispatch_semaphore_create(long value);
161 long dispatch_semaphore_wait(dispatch_semaphore_t dsema, dispatch_time_t timeout);
162 long dispatch_semaphore_signal(dispatch_semaphore_t dsema);
164 void test_no_leaks_on_semaphore_pattern() {
167 dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
168 dispatch_async(queue, ^{
171 dispatch_semaphore_signal(semaphore);
174 // Do some other work concurrently with the asynchronous work
175 // Wait for the asynchronous work to finish
176 dispatch_semaphore_wait(semaphore, 1000);
179 void test_dispatch_barrier_sync() {
181 for (int n = 0; n < 16; ++n) {
183 // FIXME: Should not warn. The dispatch_barrier_sync() call ensures
184 // that the block does not outlive 'buf'.
185 dispatch_async(queue, ^{ // expected-warning{{Address of stack memory associated with local variable 'buf' is captured by an asynchronously-executed block}}
189 dispatch_barrier_sync(queue, ^{});
192 void output_block(dispatch_block_t * blk) {
197 // Block objects themselves can never leak under ARC.
198 void test_no_block_leak() {
199 __block dispatch_block_t blk;
201 dispatch_block_t p = ^{
212 // Block objects do not leak under ARC but stack variables of
213 // non-object kind indirectly referred by a block can leak.
214 dispatch_block_t test_block_referencing_variable_leak() {
216 __block int * p = &x;
217 __block int * q = &x;
219 dispatch_async(queue, ^{// expected-warning {{Address of stack memory associated with local variable 'x' is captured by an asynchronously-executed block \
220 [alpha.core.StackAddressAsyncEscape]}}
223 return (dispatch_block_t) ^{// expected-warning {{Address of stack memory associated with local variable 'x' is captured by a returned block \
224 [core.StackAddressEscape]}}