brintos

brintos / llvm-project-archived public Read only

0
0
Text · 1.6 KiB · 5e06615 Raw
55 lines · c
1// Bugs caught within missing GCD dispatch blocks result in thread being reported as T-12// with an empty stack.3// This tests that dispatch_apply blocks can capture valid thread number and stack.4 5// RUN: %clang_asan %s -o %t6// RUN: not %run %t func 2>&1 | FileCheck %s --check-prefixes=CHECK-FUNC,CHECK7// RUN: not %run %t block 2>&1 | FileCheck %s --check-prefixes=CHECK-BLOCK,CHECK8 9#include <dispatch/dispatch.h>10#include <stdio.h>11#include <stdlib.h>12 13__attribute__((noinline)) void access_memory_frame(char *x) { *x = 0; }14 15__attribute__((noinline)) void test_dispatch_apply() {16  char *x = (char *)malloc(4);17  dispatch_apply(8, dispatch_get_global_queue(0, 0), ^(size_t i) {18    access_memory_frame(&x[i]);19  });20}21 22typedef struct {23  char *data;24} Context;25 26void da_func(void *ctx, size_t i) {27  Context *c = (Context *)ctx;28  access_memory_frame(&c->data[i]);29}30 31__attribute__((noinline)) void test_dispatch_apply_f() {32  Context *ctx = (Context *)malloc(sizeof(Context));33  ctx->data = (char *)malloc(4);34  dispatch_apply_f(8, dispatch_get_global_queue(0, 0), ctx, da_func);35}36 37int main(int argc, const char *argv[]) {38  if (strcmp(argv[1], "func") == 0) {39    fprintf(stderr, "Test dispatch_apply with function\n");40    // CHECK-FUNC: dispatch_apply with function41    test_dispatch_apply_f();42  } else if (strcmp(argv[1], "block") == 0) {43    fprintf(stderr, "Test dispatch_apply with block\n");44    // CHECK-BLOCK: dispatch_apply with block45    test_dispatch_apply();46  } else {47    abort();48  }49  return 0;50}51 52// CHECK: ERROR: AddressSanitizer: heap-buffer-overflow53// CHECK: #0 0x{{.*}} in {{.*}}access_memory_frame54// CHECK-NOT: T-155