xref: /llvm-project/clang/test/CodeGen/fake-use-sanitizer.cpp (revision 822f74a91106b7ca10e85508eb642e62ef945afa)
1 // RUN: %clang_cc1 %s -triple x86_64-unknown-linux-gnu -emit-llvm -fextend-variable-liveness -fsanitize=null -fsanitize-trap=null -o - | FileCheck --check-prefixes=CHECK,NULL --implicit-check-not=ubsantrap %s
2 // RUN: %clang_cc1 %s -triple x86_64-unknown-linux-gnu -emit-llvm -fextend-variable-liveness -o - | FileCheck %s
3 
4 // With -fextend-variable-liveness, the compiler previously generated a fake.use of any
5 // reference variable at the end of the scope in which its alloca exists. This
6 // caused two issues, where we would get fake uses for uninitialized variables
7 // if that variable was declared after an early-return, and UBSan's null checks
8 // would complain about this.
9 // This test verifies that UBSan does not produce null-checks for arguments to
10 // llvm.fake.use, and that fake uses are not emitted for a variable on paths
11 // it has not been declared.
12 
13 struct A { short s1, s2; };
14 extern long& getA();
15 
16 void foo()
17 {
18   auto& va = getA();
19   if (va < 5)
20     return;
21 
22   auto& vb = getA();
23 }
24 
25 // CHECK-LABEL:  define{{.*}}foo
26 // CHECK:  [[VA_CALL:%.+]] = call{{.*}} ptr @_Z4getAv()
27 
28 /// We check here for the first UBSan check for "va".
29 // NULL:   [[VA_ISNULL:%.+]] = icmp ne ptr [[VA_CALL]], null
30 // NULL:   br i1 [[VA_ISNULL]], label %{{[^,]+}}, label %[[VA_TRAP:[^,]+]]
31 // NULL: [[VA_TRAP]]:
32 // NULL:   call void @llvm.ubsantrap(
33 
34 // CHECK:       [[VA_PTR:%.+]] = load ptr, ptr %va
35 // CHECK-NEXT:  [[VA_CMP:%.+]] = load i64, ptr [[VA_PTR]]
36 // CHECK-NEXT:  [[VA_CMP_RES:%.+]] = icmp slt i64 [[VA_CMP]], 5
37 // CHECK-NEXT:  br i1 [[VA_CMP_RES]], label %[[EARLY_EXIT:[^,]+]], label %[[NOT_EARLY_EXIT:[^,]+]]
38 
39 // CHECK: [[EARLY_EXIT]]:
40 // CHECK:   br label %cleanup
41 
42 /// The fake use for "vb" only appears on the path where its declaration is
43 /// reached.
44 // CHECK:     [[NOT_EARLY_EXIT]]:
45 // CHECK:  [[VB_CALL:%.+]] = call{{.*}} ptr @_Z4getAv()
46 
47 /// We check here for the second UBSan check for "vb".
48 // NULL:   [[VB_ISNULL:%.+]] = icmp ne ptr [[VB_CALL]], null
49 // NULL:   br i1 [[VB_ISNULL]], label %{{[^,]+}}, label %[[VB_TRAP:[^,]+]]
50 // NULL: [[VB_TRAP]]:
51 // NULL:   call void @llvm.ubsantrap(
52 
53 // CHECK:       [[VB_FAKE_USE:%.+]] = load ptr, ptr %vb
54 // CHECK-NEXT:  call void (...) @llvm.fake.use(ptr [[VB_FAKE_USE]])
55 // CHECK:       br label %cleanup
56 
57 // CHECK:     cleanup:
58 // CHECK:       [[VA_FAKE_USE:%.+]] = load ptr, ptr %va
59 // CHECK-NEXT:  call void (...) @llvm.fake.use(ptr [[VA_FAKE_USE]])
60 
61 // NULL: declare void @llvm.ubsantrap
62