Debian Codegraph @

clang/test/CodeGen/fat-lto-objects-cfi.cpp

raw
1 // REQUIRES: x86-registered-target
2
3 // RUN: rm -rf %t && split-file %s %t
4 // RUN: %clang_cc1 -triple x86_64-unknown-fuchsia -O2 -flto -ffat-lto-objects \
5 // RUN: -fsanitize=cfi-icall -fsanitize-trap=cfi-icall -fvisibility=hidden \
6 // RUN: -emit-llvm -o - %t/a.cpp \
7 // RUN: | FileCheck %s --check-prefix=TYPE_TEST
8
9 //--- a.cpp
10 // TYPE_TEST: llvm.embedded.object
11 // TYPE_TEST-SAME: section ".llvm.lto"
12
13 // COM: The FatLTO pipeline should remove all llvm.type.test instructions.
14 // TYPE_TEST-LABEL: define hidden void @foo
15 // TYPE_TEST-NOT: @llvm.type.test
16 // TYPE_TEST-NEXT: entry:
17 // TYPE_TEST-NEXT: %cmp14.not = icmp eq i64 %len, 0
18 // TYPE_TEST-NEXT: br i1 %cmp14.not, label %for.end7, label %for.cond1.preheader.preheader
19 // TYPE_TEST-LABEL: for.cond1.preheader.preheader: ; preds = %entry
20 // TYPE_TEST-NEXT: %arrayidx.1 = getelementptr inbounds nuw i8, ptr %ptr, i64 4
21 // TYPE_TEST-NEXT: br label %for.cond1.preheader
22
23 // The code below is a reduced case from https://github.com/llvm/llvm-project/issues/112053
24 #define __PRINTFLIKE(__fmt, __varargs) __attribute__((__format__(__printf__, __fmt, __varargs)))
25 typedef void func(void* arg, const char* fmt, ...) __PRINTFLIKE(2, 3);
26 typedef __SIZE_TYPE__ size_t;
27 typedef unsigned long uintptr_t;
28
29 extern "C"
30 void foo(const void* ptr, size_t len, long disp_addr,
31 func* printf_func, void* printf_arg) {
32 uintptr_t address = (uintptr_t)ptr;
33 size_t count;
34
35 for (count = 0; count < len; count += 16) {
36 union {
37 unsigned int buf[4];
38 unsigned char cbuf[16];
39 } u;
40 size_t s = 10;
41 size_t i;
42
43 for (i = 0; i < s / 4; i++) {
44 u.buf[i] = ((const unsigned int*)address)[i];
45 printf_func(printf_arg, "%08x ", static_cast<unsigned int>(u.buf[i]));
46 }
47 }
48 }
49
50 //--- b.cpp
51 // COM: Prior to the introduction of the FatLTO cleanup pass, this used to cause
52 // COM: the backend to crash, either due to an assertion failure, or because
53 // COM: the CFI instructions couldn't be correctly generated. So, check to make
54 // COM: sure that the FatLTO pipeline used by clang does not regress.
55
56 // COM: Check the generated IR doesn't contain llvm.type.checked.load in the final IR.
57 // RUN: %clang_cc1 -triple=x86_64-unknown-fuchsia -O1 -emit-llvm -o - \
58 // RUN: -ffat-lto-objects -fvisibility=hidden \
59 // RUN: -fno-rtti -fsanitize=cfi-icall,cfi-mfcall,cfi-nvcall,cfi-vcall \
60 // RUN: -fsanitize-trap=cfi-icall,cfi-mfcall,cfi-nvcall,cfi-vcall \
61 // RUN: -fwhole-program-vtables %t/b.cpp 2>&1 | FileCheck %s --check-prefix=NO_CHECKED_LOAD
62
63 // RUN: %clang_cc1 -triple=x86_64-unknown-fuchsia -O1 -emit-llvm -o - \
64 // RUN: -ffat-lto-objects -fvisibility=hidden -fexperimental-relative-c++-abi-vtables \
65 // RUN: -fno-rtti -fsanitize=cfi-icall,cfi-mfcall,cfi-nvcall,cfi-vcall \
66 // RUN: -fsanitize-trap=cfi-icall,cfi-mfcall,cfi-nvcall,cfi-vcall \
67 // RUN: -fwhole-program-vtables %t/b.cpp 2>&1 | FileCheck %s --check-prefix=NO_CHECKED_LOAD
68
69 // COM: Note that the embedded bitcode section will contain references to
70 // COM: llvm.type.checked.load, so we need to match the function body first.
71 // NO_CHECKED_LOAD-LABEL: entry:
72 // NO_CHECKED_LOAD-NEXT: %vtable = load ptr, ptr %p1
73 // NO_CHECKED_LOAD-NOT: llvm.type.checked.load.relative
74 // NO_CHECKED_LOAD-NEXT: %rel_load = tail call ptr @llvm.load.relative.i32(ptr %vtable, i32 0)
75 // NO_CHECKED_LOAD-NEXT: %call = tail call {{.*}} %rel_load(ptr {{.*}} %p1)
76 // NO_CHECKED_LOAD-NEXT: ret void
77
78 // COM: Ensure that we don't crash in the backend anymore when clang uses
79 // COM: CFI checks with -ffat-lto-objects.
80 // RUN: %clang_cc1 -triple=x86_64-unknown-fuchsia -O1 -emit-codegen-only \
81 // RUN: -ffat-lto-objects -fvisibility=hidden \
82 // RUN: -fno-rtti -fsanitize=cfi-icall,cfi-mfcall,cfi-nvcall,cfi-vcall \
83 // RUN: -fsanitize-trap=cfi-icall,cfi-mfcall,cfi-nvcall,cfi-vcall \
84 // RUN: -fwhole-program-vtables %t/b.cpp
85
86 class a {
87 public:
88 virtual long b();
89 };
90 void c(a &p1) { p1.b(); }