xref: /llvm-project/clang/test/CodeGen/fake-use-sanitizer.cpp (revision 822f74a91106b7ca10e85508eb642e62ef945afa)
18ad9e1ecSStephen Tozer // 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
28ad9e1ecSStephen Tozer // RUN: %clang_cc1 %s -triple x86_64-unknown-linux-gnu -emit-llvm -fextend-variable-liveness -o - | FileCheck %s
34424c44cSWolfgang Pieb 
4*822f74a9SStephen Tozer // With -fextend-variable-liveness, the compiler previously generated a fake.use of any
54424c44cSWolfgang Pieb // reference variable at the end of the scope in which its alloca exists. This
64424c44cSWolfgang Pieb // caused two issues, where we would get fake uses for uninitialized variables
74424c44cSWolfgang Pieb // if that variable was declared after an early-return, and UBSan's null checks
84424c44cSWolfgang Pieb // would complain about this.
94424c44cSWolfgang Pieb // This test verifies that UBSan does not produce null-checks for arguments to
104424c44cSWolfgang Pieb // llvm.fake.use, and that fake uses are not emitted for a variable on paths
114424c44cSWolfgang Pieb // it has not been declared.
124424c44cSWolfgang Pieb 
134424c44cSWolfgang Pieb struct A { short s1, s2; };
144424c44cSWolfgang Pieb extern long& getA();
154424c44cSWolfgang Pieb 
164424c44cSWolfgang Pieb void foo()
174424c44cSWolfgang Pieb {
184424c44cSWolfgang Pieb   auto& va = getA();
194424c44cSWolfgang Pieb   if (va < 5)
204424c44cSWolfgang Pieb     return;
214424c44cSWolfgang Pieb 
224424c44cSWolfgang Pieb   auto& vb = getA();
234424c44cSWolfgang Pieb }
244424c44cSWolfgang Pieb 
254424c44cSWolfgang Pieb // CHECK-LABEL:  define{{.*}}foo
264424c44cSWolfgang Pieb // CHECK:  [[VA_CALL:%.+]] = call{{.*}} ptr @_Z4getAv()
274424c44cSWolfgang Pieb 
284424c44cSWolfgang Pieb /// We check here for the first UBSan check for "va".
294424c44cSWolfgang Pieb // NULL:   [[VA_ISNULL:%.+]] = icmp ne ptr [[VA_CALL]], null
304424c44cSWolfgang Pieb // NULL:   br i1 [[VA_ISNULL]], label %{{[^,]+}}, label %[[VA_TRAP:[^,]+]]
314424c44cSWolfgang Pieb // NULL: [[VA_TRAP]]:
324424c44cSWolfgang Pieb // NULL:   call void @llvm.ubsantrap(
334424c44cSWolfgang Pieb 
344424c44cSWolfgang Pieb // CHECK:       [[VA_PTR:%.+]] = load ptr, ptr %va
354424c44cSWolfgang Pieb // CHECK-NEXT:  [[VA_CMP:%.+]] = load i64, ptr [[VA_PTR]]
364424c44cSWolfgang Pieb // CHECK-NEXT:  [[VA_CMP_RES:%.+]] = icmp slt i64 [[VA_CMP]], 5
374424c44cSWolfgang Pieb // CHECK-NEXT:  br i1 [[VA_CMP_RES]], label %[[EARLY_EXIT:[^,]+]], label %[[NOT_EARLY_EXIT:[^,]+]]
384424c44cSWolfgang Pieb 
394424c44cSWolfgang Pieb // CHECK: [[EARLY_EXIT]]:
404424c44cSWolfgang Pieb // CHECK:   br label %cleanup
414424c44cSWolfgang Pieb 
424424c44cSWolfgang Pieb /// The fake use for "vb" only appears on the path where its declaration is
434424c44cSWolfgang Pieb /// reached.
444424c44cSWolfgang Pieb // CHECK:     [[NOT_EARLY_EXIT]]:
454424c44cSWolfgang Pieb // CHECK:  [[VB_CALL:%.+]] = call{{.*}} ptr @_Z4getAv()
464424c44cSWolfgang Pieb 
474424c44cSWolfgang Pieb /// We check here for the second UBSan check for "vb".
484424c44cSWolfgang Pieb // NULL:   [[VB_ISNULL:%.+]] = icmp ne ptr [[VB_CALL]], null
494424c44cSWolfgang Pieb // NULL:   br i1 [[VB_ISNULL]], label %{{[^,]+}}, label %[[VB_TRAP:[^,]+]]
504424c44cSWolfgang Pieb // NULL: [[VB_TRAP]]:
514424c44cSWolfgang Pieb // NULL:   call void @llvm.ubsantrap(
524424c44cSWolfgang Pieb 
534424c44cSWolfgang Pieb // CHECK:       [[VB_FAKE_USE:%.+]] = load ptr, ptr %vb
544424c44cSWolfgang Pieb // CHECK-NEXT:  call void (...) @llvm.fake.use(ptr [[VB_FAKE_USE]])
554424c44cSWolfgang Pieb // CHECK:       br label %cleanup
564424c44cSWolfgang Pieb 
574424c44cSWolfgang Pieb // CHECK:     cleanup:
584424c44cSWolfgang Pieb // CHECK:       [[VA_FAKE_USE:%.+]] = load ptr, ptr %va
594424c44cSWolfgang Pieb // CHECK-NEXT:  call void (...) @llvm.fake.use(ptr [[VA_FAKE_USE]])
604424c44cSWolfgang Pieb 
614424c44cSWolfgang Pieb // NULL: declare void @llvm.ubsantrap
62