brintos

brintos / llvm-project-archived public Read only

0
0
Text · 1.5 KiB · 3b19713 Raw
62 lines · cpp
1// RUN: %clang_tsan -fno-sanitize=thread -shared -fPIC -O1 -DBUILD_SO=1 %s -o \2// RUN:  %t.so && \3// RUN:   %clang_tsan -O1 %s %t.so -o %t && %run %t 2>&1 | FileCheck %s4// RUN: llvm-objdump -t %t | FileCheck %s --check-prefix=CHECK-DUMP5// CHECK-DUMP:  {{[.]preinit_array.*preinit}}6 7// SANITIZER_CAN_USE_PREINIT_ARRAY is undefined on android.8// UNSUPPORTED: android9 10// Test checks if __tsan_init is called from .preinit_array.11// Without initialization from .preinit_array, __tsan_init will be called from12// constructors of the binary which are called after constructors of shared13// library.14 15#include <sanitizer/tsan_interface.h>16#include <stdio.h>17 18#if BUILD_SO19 20// "volatile" is needed to avoid compiler optimize-out constructors.21volatile int counter = 0;22volatile int lib_constructor_call = 0;23volatile int tsan_init_call = 0;24 25__attribute__ ((constructor))26void LibConstructor() {27  lib_constructor_call = ++counter;28};29 30#else  // BUILD_SO31 32extern int counter;33extern int lib_constructor_call;34extern int tsan_init_call;35 36volatile int bin_constructor_call = 0;37 38__attribute__ ((constructor))39void BinConstructor() {40  bin_constructor_call = ++counter;41};42 43namespace __tsan {44 45void OnInitialize() {46  tsan_init_call = ++counter;47}48 49}50 51int main() {52  // CHECK: TSAN_INIT 153  // CHECK: LIB_CONSTRUCTOR 254  // CHECK: BIN_CONSTRUCTOR 355  printf("TSAN_INIT %d\n", tsan_init_call);56  printf("LIB_CONSTRUCTOR %d\n", lib_constructor_call);57  printf("BIN_CONSTRUCTOR %d\n", bin_constructor_call);58  return 0;59}60 61#endif  // BUILD_SO62