307 lines · cpp
1// REQUIRES: aarch64-registered-target2 3// RUN: %clang_cc1 -S -x c++ -std=c++11 -triple aarch64-linux-android31 \4// RUN: -fsanitize=memtag-globals -o %t.out %s5// RUN: FileCheck %s --input-file=%t.out6// RUN: FileCheck %s --input-file=%t.out --check-prefix=CHECK-A7// RUN: FileCheck %s --input-file=%t.out --check-prefix=CHECK-B8// RUN: FileCheck %s --input-file=%t.out --check-prefix=CHECK-C9// RUN: FileCheck %s --input-file=%t.out --check-prefix=CHECK-D10// RUN: FileCheck %s --input-file=%t.out --check-prefix=CHECK-E11// RUN: FileCheck %s --input-file=%t.out --check-prefix=CHECK-F12// RUN: FileCheck %s --input-file=%t.out --check-prefix=CHECK-G13// RUN: FileCheck %s --input-file=%t.out --check-prefix=CHECK-H14// RUN: FileCheck %s --input-file=%t.out --check-prefix=CHECK-I15// RUN: FileCheck %s --input-file=%t.out --check-prefix=CHECK-J16// RUN: FileCheck %s --input-file=%t.out --check-prefix=CHECK-K17// RUN: FileCheck %s --input-file=%t.out --check-prefix=CHECK-L18// RUN: FileCheck %s --input-file=%t.out --check-prefix=CHECK-M19// RUN: FileCheck %s --input-file=%t.out --check-prefix=CHECK-N20// RUN: FileCheck %s --input-file=%t.out --check-prefix=CHECK-O21// RUN: FileCheck %s --input-file=%t.out --check-prefix=CHECK-P22// RUN: FileCheck %s --input-file=%t.out --check-prefix=CHECK-Q23// RUN: FileCheck %s --input-file=%t.out --check-prefix=CHECK-R24// RUN: FileCheck %s --input-file=%t.out --check-prefix=CHECK-S25 26// RUN: %clang_cc1 -O3 -S -x c++ -std=c++11 -triple aarch64-linux-android31 \27// RUN: -fsanitize=memtag-globals -o %t.out %s28// RUN: FileCheck %s --input-file=%t.out29// RUN: FileCheck %s --input-file=%t.out --check-prefix=CHECK-A30// RUN: FileCheck %s --input-file=%t.out --check-prefix=CHECK-B31// RUN: FileCheck %s --input-file=%t.out --check-prefix=CHECK-C32// RUN: FileCheck %s --input-file=%t.out --check-prefix=CHECK-D33// RUN: FileCheck %s --input-file=%t.out --check-prefix=CHECK-E34// RUN: FileCheck %s --input-file=%t.out --check-prefix=CHECK-F35// RUN: FileCheck %s --input-file=%t.out --check-prefix=CHECK-G36// RUN: FileCheck %s --input-file=%t.out --check-prefix=CHECK-H37// RUN: FileCheck %s --input-file=%t.out --check-prefix=CHECK-I38// RUN: FileCheck %s --input-file=%t.out --check-prefix=CHECK-J39// RUN: FileCheck %s --input-file=%t.out --check-prefix=CHECK-K40// RUN: FileCheck %s --input-file=%t.out --check-prefix=CHECK-L41// RUN: FileCheck %s --input-file=%t.out --check-prefix=CHECK-M42// RUN: FileCheck %s --input-file=%t.out --check-prefix=CHECK-N43// RUN: FileCheck %s --input-file=%t.out --check-prefix=CHECK-O44// RUN: FileCheck %s --input-file=%t.out --check-prefix=CHECK-P45// RUN: FileCheck %s --input-file=%t.out --check-prefix=CHECK-Q46// RUN: FileCheck %s --input-file=%t.out --check-prefix=CHECK-R47// RUN: FileCheck %s --input-file=%t.out --check-prefix=CHECK-S48 49/// Ensure that emulated TLS also doesn't get sanitized.50// RUN: %clang_cc1 -S -x c++ -std=c++11 -triple aarch64-linux-android31 \51// RUN: -fsanitize=memtag-globals -o - %s | FileCheck %s52 53// CHECK-A: .memtag global_int54// CHECK-A: .globl global_int55// CHECK-A: .p2align 4, 0x056// CHECK-A: .zero 1657// CHECK-A: .size global_int, 1658int global_int;59// CHECK-B: .memtag _ZL9local_int60// CHECK-B: .local _ZL9local_int61// CHECK-B: .comm _ZL9local_int,16,1662static int local_int;63 64// CHECK-C: .memtag _ZL12local_buffer65// CHECK-C: .local _ZL12local_buffer66// CHECK-C: .comm _ZL12local_buffer,16,1667static char local_buffer[16];68// CHECK-D: .memtag _ZL22local_buffer_local_end69// CHECK-D: .p2align 4, 0x070// CHECK-D: _ZL22local_buffer_local_end:71// CHECK-D: .xword _ZL12local_buffer+1672// CHECK-D: .zero 873// CHECK-D: .size _ZL22local_buffer_local_end, 1674static char* local_buffer_local_end = &local_buffer[16];75// CHECK-E: .memtag local_buffer_global_end76// CHECK-E: .globl local_buffer_global_end77// CHECK-E .p2align 4, 0x078// CHECK-E: local_buffer_global_end:79// CHECK-E: .xword _ZL12local_buffer+1680// CHECK-E: .zero 881// CHECK-E: .size local_buffer_global_end, 1682char* local_buffer_global_end = &local_buffer[16];83 84// CHECK-F: .memtag global_buffer85// CHECK-F: .globl global_buffer86// CHECK-F: .p2align 4, 0x087// CHECK-F: .zero 1688// CHECK-F: .size global_buffer, 1689char global_buffer[16];90// CHECK-G: .memtag _ZL23global_buffer_local_end91// CHECK-G: .p2align 4, 0x092// CHECK-G: _ZL23global_buffer_local_end:93// CHECK-G: .xword global_buffer+1694// CHECK-G: .zero 895// CHECK-G: .size _ZL23global_buffer_local_end, 1696static char* global_buffer_local_end = &global_buffer[16];97// CHECK-H: .memtag global_buffer_global_end98// CHECK-H: .p2align 4, 0x099// CHECK-H: global_buffer_global_end:100// CHECK-H: .xword global_buffer+16101// CHECK-H: .size global_buffer_global_end, 16102char* global_buffer_global_end = &global_buffer[16];103 104// CHECK-S-NOT: .memtag zero_sized105struct empty {};106char zero_sized[0];107 108class MyClass {109 public:110 virtual ~MyClass() {}111 static int my_class_int;112 static const int my_class_const_int;113 virtual int virtual_func() { return 1; }114};115// CHECK-I: .memtag _ZN7MyClass12my_class_intE116// CHECK-I: .globl _ZN7MyClass12my_class_intE117// CHECK-I: .p2align 4, 0x0118// CHECK-I: .zero 16119// CHECK-I: .size _ZN7MyClass12my_class_intE, 16120int MyClass::my_class_int;121// CHECK-NOT: .memtag _ZN7MyClass18my_class_const_intE122const int MyClass::my_class_const_int = 1;123 124// CHECK-J: .memtag global_my_class125// CHECK-J: .globl global_my_class126// CHECK-J: .p2align 4, 0x0127// CHECK-J: .zero 8128// CHECK-J: .size global_my_class, 16129MyClass global_my_class;130// CHECK-K: .memtag _ZL14local_my_class131// CHECK-K: .p2align 4, 0x0132// CHECK-K: .zero 8133// CHECK-K: .size _ZL14local_my_class, 16134static MyClass local_my_class;135 136// CHECK-NOT: .memtag _ZL18local_const_string137static const char local_const_string[] = "this is a local string";138// CHECK-L: .memtag _ZL12local_string139// CHECK-L: .p2align 4, 0x0140// CHECK-L: .zero 9141// CHECK-L: .size _ZL12local_string, 32142static char local_string[] = "this is a local string";143 144// CHECK-M: .memtag global_atomic_int145// CHECK-M: .globl global_atomic_int146// CHECK-M: .p2align 4, 0x0147// CHECK-M: .zero 16148// CHECK-M: .size global_atomic_int, 16149_Atomic(int) global_atomic_int;150// CHECK-N: .memtag _ZL16local_atomic_int151// CHECK-N: .local _ZL16local_atomic_int152// CHECK-N: .comm _ZL16local_atomic_int,16,16153static _Atomic(int) local_atomic_int;154 155union MyUnion {156 int i;157 char c;158};159 160// CHECK-O: .memtag global_union161// CHECK-O: .globl global_union162// CHECK-O: .p2align 4, 0x0163// CHECK-O: .zero 16164// CHECK-O: .size global_union, 16165MyUnion global_union;166// CHECK-P: .memtag _ZL11local_union167// CHECK-P: .local _ZL11local_union168// CHECK-P: .comm _ZL11local_union,16,16169static MyUnion local_union;170 171// CHECK-NOT: .memtag {{.*}}global_tls172thread_local int global_tls;173// CHECK-NOT: .memtag {{.*}}local_tls174static thread_local int local_tls;175 176/// Prevent the compiler from realising that non-const local variables are not177/// modified, and constant inlining into f().178const void* export_pointers(int c) {179 switch (c) {180 case 0: return &local_int;181 case 1: return &local_buffer;182 case 2: return &local_buffer_local_end;183 case 3: return &global_buffer_local_end;184 case 4: return &MyClass::my_class_int;185 case 6: return &local_my_class;186 case 8: return &local_string;187 case 9: return &local_atomic_int;188 case 10: return &local_union;189 case 11: return &local_tls;190 }191 return nullptr;192}193 194/// Ensure that all tagged globals are loaded/referenced via. the GOT.195// CHECK-NOT: .memtag _Z1fi196// CHECK-Q: _Z1fi:197int f(int x) {198 // CHECK-R: .memtag _ZZ1fiE12function_int199 // CHECK-R: .local _ZZ1fiE12function_int200 // CHECK-R: .comm _ZZ1fiE12function_int,16,16201 static int function_int = 0;202 /// Prevent non-const `f` from being promoted to a constant and inlined.203 function_int += x;204 205 return206 // CHECK-Q-DAG: adrp [[REG_A:x[0-9]+]], :got:global_int207 // CHECK-Q-DAG: ldr [[REG_A2:x[0-9]+]], [[[REG_A]], :got_lo12:global_int]208 // CHECK-Q-DAG: ldr {{.*}}, [[[REG_A2]]]209 global_int +210 // CHECK-Q-DAG: adrp [[REG_B:x[0-9]+]], :got:_ZL9local_int211 // CHECK-Q-DAG: ldr [[REG_B2:x[0-9]+]], [[[REG_B]], :got_lo12:_ZL9local_int]212 // CHECK-Q-DAG: ldr {{.*}}, [[[REG_B2]]]213 local_int +214 // CHECK-Q-DAG: adrp [[REG_C:x[0-9]+]], :got:_ZL12local_buffer215 // CHECK-Q-DAG: ldr [[REG_C2:x[0-9]+]], [[[REG_C]], :got_lo12:_ZL12local_buffer]216 // CHECK-Q-DAG: ldrsb {{.*}}, [[[REG_C2]]]217 local_buffer[0] +218 // CHECK-Q-DAG: adrp [[REG_D:x[0-9]+]], :got:_ZL22local_buffer_local_end219 // CHECK-Q-DAG: ldr [[REG_D2:x[0-9]+]], [[[REG_D]], :got_lo12:_ZL22local_buffer_local_end]220 // CHECK-Q-DAG: ldr [[REG_D3:x[0-9]+]], [[[REG_D2]]]221 // CHECK-Q-DAG: ldursb {{.*}}, [[[REG_D3]], #-16]222 local_buffer_local_end[-16] +223 // CHECK-Q-DAG: adrp [[REG_E:x[0-9]+]], :got:local_buffer_global_end224 // CHECK-Q-DAG: ldr [[REG_E2:x[0-9]+]], [[[REG_E]], :got_lo12:local_buffer_global_end]225 // CHECK-Q-DAG: ldr [[REG_E3:x[0-9]+]], [[[REG_E2]]]226 // CHECK-Q-DAG: ldursb {{.*}}, [[[REG_E3]], #-16]227 local_buffer_global_end[-16] +228 // CHECK-Q-DAG: adrp [[REG_F:x[0-9]+]], :got:global_buffer{{$}}229 // CHECK-Q-DAG: ldr [[REG_F2:x[0-9]+]], [[[REG_F]], :got_lo12:global_buffer]230 // CHECK-Q-DAG: ldrsb {{.*}}, [[[REG_F2]]]231 global_buffer[0] +232 // CHECK-Q-DAG: adrp [[REG_G:x[0-9]+]], :got:_ZL23global_buffer_local_end233 // CHECK-Q-DAG: ldr [[REG_G2:x[0-9]+]], [[[REG_G]], :got_lo12:_ZL23global_buffer_local_end]234 // CHECK-Q-DAG: ldr [[REG_G3:x[0-9]+]], [[[REG_G2]]]235 // CHECK-Q-DAG: ldursb {{.*}}, [[[REG_G3]], #-16]236 global_buffer_local_end[-16] +237 // CHECK-Q-DAG: adrp [[REG_H:x[0-9]+]], :got:global_buffer_global_end238 // CHECK-Q-DAG: ldr [[REG_H2:x[0-9]+]], [[[REG_H]], :got_lo12:global_buffer_global_end]239 // CHECK-Q-DAG: ldr [[REG_H3:x[0-9]+]], [[[REG_H2]]]240 // CHECK-Q-DAG: ldursb {{.*}}, [[[REG_H3]], #-16]241 global_buffer_global_end[-16] +242 // CHECK-Q-DAG: adrp [[REG_I:x[0-9]+]], :got:_ZN7MyClass12my_class_intE243 // CHECK-Q-DAG: ldr [[REG_I2:x[0-9]+]], [[[REG_I]], :got_lo12:_ZN7MyClass12my_class_intE]244 // CHECK-Q-DAG: ldr {{.*}}, [[[REG_I2]]]245 MyClass::my_class_int +246 /// Constant values - ignore.247 MyClass::my_class_const_int +248 global_my_class.virtual_func() +249 local_my_class.virtual_func() +250 local_const_string[0] +251 // CHECK-Q-DAG: adrp [[REG_J:x[0-9]+]], :got:_ZL12local_string252 // CHECK-Q-DAG: ldr [[REG_J2:x[0-9]+]], [[[REG_J]], :got_lo12:_ZL12local_string]253 // CHECK-Q-DAG: ldrsb {{.*}}, [[[REG_J2]]]254 local_string[0] +255 // CHECK-Q-DAG: adrp [[REG_K:x[0-9]+]], :got:_ZL16local_atomic_int256 // CHECK-Q-DAG: ldr [[REG_K2:x[0-9]+]], [[[REG_K]], :got_lo12:_ZL16local_atomic_int]257 // CHECK-Q-DAG: ldar {{.*}}, [[[REG_K2]]]258 local_atomic_int +259 // CHECK-Q-DAG: adrp [[REG_L:x[0-9]+]], :got:global_atomic_int260 // CHECK-Q-DAG: ldr [[REG_L2:x[0-9]+]], [[[REG_L]], :got_lo12:global_atomic_int]261 // CHECK-Q-DAG: ldar {{.*}}, [[[REG_L2]]]262 global_atomic_int +263 // CHECK-Q-DAG: adrp [[REG_M:x[0-9]+]], :got:global_union264 // CHECK-Q-DAG: ldr [[REG_M2:x[0-9]+]], [[[REG_M]], :got_lo12:global_union]265 // CHECK-Q-DAG: ldr {{.*}}, [[[REG_M2]]]266 global_union.i +267 // CHECK-Q-DAG: adrp [[REG_N:x[0-9]+]], :got:_ZL11local_union268 // CHECK-Q-DAG: ldr [[REG_N2:x[0-9]+]], [[[REG_N]], :got_lo12:_ZL11local_union]269 // CHECK-Q-DAG: ldrsb {{.*}}, [[[REG_N2]]]270 local_union.c +271 /// Global variables - ignore.272 global_tls +273 local_tls +274 // CHECK-Q-DAG: adrp [[REG_O:x[0-9]+]], :got:_ZZ1fiE12function_int275 // CHECK-Q-DAG: ldr [[REG_O2:x[0-9]+]], [[[REG_O]], :got_lo12:_ZZ1fiE12function_int]276 // CHECK-Q-DAG: ldr {{.*}}, [[[REG_O2]]]277 function_int;278}279 280typedef void (*func_t)(void);281#define CONSTRUCTOR(section_name) \282 __attribute__((used)) __attribute__((section(section_name)))283 284__attribute__((constructor(0))) void func_constructor() {}285CONSTRUCTOR(".init") func_t func_init = func_constructor;286CONSTRUCTOR(".fini") func_t func_fini = func_constructor;287CONSTRUCTOR(".ctors") func_t func_ctors = func_constructor;288CONSTRUCTOR(".dtors") func_t func_dtors = func_constructor;289CONSTRUCTOR(".init_array") func_t func_init_array = func_constructor;290CONSTRUCTOR(".fini_array") func_t func_fini_array = func_constructor;291CONSTRUCTOR(".preinit_array") func_t preinit_array = func_constructor;292CONSTRUCTOR("array_of_globals") int global1;293CONSTRUCTOR("array_of_globals") int global2;294CONSTRUCTOR("array_of_globals") int global_string;295 296// CHECK-NOT: .memtag func_constructor297// CHECK-NOT: .memtag func_init298// CHECK-NOT: .memtag func_fini299// CHECK-NOT: .memtag func_ctors300// CHECK-NOT: .memtag func_dtors301// CHECK-NOT: .memtag func_init_array302// CHECK-NOT: .memtag func_fini_array303// CHECK-NOT: .memtag preinit_array304// CHECK-NOT: .memtag global1305// CHECK-NOT: .memtag global2306// CHECK-NOT: .memtag global_string307