brintos

brintos / llvm-project-archived public Read only

0
0
Text · 2.3 KiB · 1831e23 Raw
82 lines · plain
1// RUN: %clang_analyze_cc1 -triple x86_64-apple-darwin10 -analyzer-checker=core,alpha.core.StackAddressAsyncEscape -fblocks -verify %s2 3typedef struct dispatch_queue_s *dispatch_queue_t;4typedef void (^dispatch_block_t)(void);5void dispatch_async(dispatch_queue_t queue, dispatch_block_t block);6extern dispatch_queue_t queue;7void f(int);8 9void test_block_inside_block_async_no_leak() {10  int x = 123;11  int *p = &x;12  void (^inner)(void) = ^void(void) {13    int y = x;14    ++y; 15  };16  // Block_copy(...) copies the captured block ("inner") too,17  // there is no leak in this case.18  dispatch_async(queue, ^void(void) {19    int z = x;20    ++z;21    inner(); 22  }); // no-warning23}24 25dispatch_block_t test_block_inside_block_async_leak() {26  int x = 123;27  void (^inner)(void) = ^void(void) {28    int y = x;29    ++y; 30  };31  void (^outer)(void) = ^void(void) {32    int z = x;33    ++z;34    inner(); 35  }; 36  return outer; // expected-warning-re{{Address of stack-allocated block declared on line {{.+}} is captured by a returned block}}37}38 39// The block literal defined in this function could leak once being40// called.41void output_block(dispatch_block_t * blk) {42  int x = 0;43  *blk = ^{ f(x); }; // expected-warning {{Address of stack-allocated block declared on line 43 is still referred to by the caller variable 'blk' upon returning to the caller.  This will be a dangling reference [core.StackAddressEscape]}}44}45 46// The block literal captures nothing thus is treated as a constant.47void output_constant_block(dispatch_block_t * blk) {48  *blk = ^{ };49}50 51// A block can leak if it captures at least one variable and is not52// under ARC when its' stack frame expires.53void test_block_leak() {54  __block dispatch_block_t blk;55  int x = 0;56  dispatch_block_t p = ^{57    blk = ^{ // expected-warning {{Address of stack-allocated block declared on line 57 is still referred to by the caller variable 'blk' upon returning to the caller.  This will be a dangling reference [core.StackAddressEscape]}}58      f(x);59    };60  };61 62  p();63  blk();64  output_block(&blk);65  blk();66}67 68// A block captures nothing is a constant thus never leaks.69void test_constant_block_no_leak() {70  __block dispatch_block_t blk;71  dispatch_block_t p = ^{72    blk = ^{73      f(0);74    };75  };76  77  p();78  blk();79  output_constant_block(&blk);80  blk();81}82