File: check_preinit.cpp

package info (click to toggle)
llvm-toolchain-19 1%3A19.1.7-3
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 1,998,520 kB
  • sloc: cpp: 6,951,680; ansic: 1,486,157; asm: 913,598; python: 232,024; f90: 80,126; objc: 75,281; lisp: 37,276; pascal: 16,990; sh: 10,009; ml: 5,058; perl: 4,724; awk: 3,523; makefile: 3,167; javascript: 2,504; xml: 892; fortran: 664; cs: 573
file content (61 lines) | stat: -rw-r--r-- 1,536 bytes parent folder | download | duplicates (16)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
// RUN: %clang_tsan -fno-sanitize=thread -shared -fPIC -O1 -DBUILD_SO=1 %s -o \
// RUN:  %t.so && \
// RUN:   %clang_tsan -O1 %s %t.so -o %t && %run %t 2>&1 | FileCheck %s
// RUN: llvm-objdump -t %t | FileCheck %s --check-prefix=CHECK-DUMP
// CHECK-DUMP:  {{[.]preinit_array.*preinit}}

// SANITIZER_CAN_USE_PREINIT_ARRAY is undefined on android.
// UNSUPPORTED: android

// Test checks if __tsan_init is called from .preinit_array.
// Without initialization from .preinit_array, __tsan_init will be called from
// constructors of the binary which are called after constructors of shared
// library.

#include <sanitizer/tsan_interface.h>
#include <stdio.h>

#if BUILD_SO

// "volatile" is needed to avoid compiler optimize-out constructors.
volatile int counter = 0;
volatile int lib_constructor_call = 0;
volatile int tsan_init_call = 0;

__attribute__ ((constructor))
void LibConstructor() {
  lib_constructor_call = ++counter;
};

#else  // BUILD_SO

extern int counter;
extern int lib_constructor_call;
extern int tsan_init_call;

volatile int bin_constructor_call = 0;

__attribute__ ((constructor))
void BinConstructor() {
  bin_constructor_call = ++counter;
};

namespace __tsan {

void OnInitialize() {
  tsan_init_call = ++counter;
}

}

int main() {
  // CHECK: TSAN_INIT 1
  // CHECK: LIB_CONSTRUCTOR 2
  // CHECK: BIN_CONSTRUCTOR 3
  printf("TSAN_INIT %d\n", tsan_init_call);
  printf("LIB_CONSTRUCTOR %d\n", lib_constructor_call);
  printf("BIN_CONSTRUCTOR %d\n", bin_constructor_call);
  return 0;
}

#endif  // BUILD_SO