[PATCH] D106888: [RISC-V] Implement jump tables for CFI-icall
Wende Tan via Phabricator via cfe-commits
cfe-commits at lists.llvm.org
Tue Jul 27 09:29:53 PDT 2021
twd2 created this revision.
twd2 added reviewers: MaskRay, asb, eugenis, pcc.
twd2 created this object with edit policy "Administrators".
twd2 added projects: LLVM, Sanitizers, clang.
Herald added subscribers: ormris, vkmr, luismarques, sameer.abuasal, s.egerton, Jim, PkmX, rogfer01, shiva0217, kito-cheng, simoncook, hiraditya.
twd2 requested review of this revision.
Herald added a subscriber: cfe-commits.
This patch implements jump tables for RISC-V so that CFI-icall can be enabled for it.
Repository:
rG LLVM Github Monorepo
https://reviews.llvm.org/D106888
Files:
clang/lib/Driver/ToolChain.cpp
clang/test/Driver/fsanitize.c
llvm/lib/Transforms/IPO/LowerTypeTests.cpp
Index: llvm/lib/Transforms/IPO/LowerTypeTests.cpp
===================================================================
--- llvm/lib/Transforms/IPO/LowerTypeTests.cpp
+++ llvm/lib/Transforms/IPO/LowerTypeTests.cpp
@@ -1211,6 +1211,7 @@
static const unsigned kX86JumpTableEntrySize = 8;
static const unsigned kARMJumpTableEntrySize = 4;
static const unsigned kARMBTIJumpTableEntrySize = 8;
+static const unsigned kRISCVJumpTableEntrySize = 8;
unsigned LowerTypeTestsModule::getJumpTableEntrySize() {
switch (Arch) {
@@ -1226,6 +1227,9 @@
if (BTE->getZExtValue())
return kARMBTIJumpTableEntrySize;
return kARMJumpTableEntrySize;
+ case Triple::riscv32:
+ case Triple::riscv64:
+ return kRISCVJumpTableEntrySize;
default:
report_fatal_error("Unsupported architecture for jump tables");
}
@@ -1253,6 +1257,10 @@
AsmOS << "b $" << ArgIndex << "\n";
} else if (JumpTableArch == Triple::thumb) {
AsmOS << "b.w $" << ArgIndex << "\n";
+ } else if (JumpTableArch == Triple::riscv32 ||
+ JumpTableArch == Triple::riscv64) {
+ AsmOS << "1: auipc t0, %pcrel_hi($" << ArgIndex << ")\n"
+ << "jr %pcrel_lo(1b)(t0)\n";
} else {
report_fatal_error("Unsupported architecture for jump tables");
}
@@ -1270,7 +1278,8 @@
void LowerTypeTestsModule::buildBitSetsFromFunctions(
ArrayRef<Metadata *> TypeIds, ArrayRef<GlobalTypeMember *> Functions) {
if (Arch == Triple::x86 || Arch == Triple::x86_64 || Arch == Triple::arm ||
- Arch == Triple::thumb || Arch == Triple::aarch64)
+ Arch == Triple::thumb || Arch == Triple::aarch64 ||
+ Arch == Triple::riscv32 || Arch == Triple::riscv64)
buildBitSetsFromFunctionsNative(TypeIds, Functions);
else if (Arch == Triple::wasm32 || Arch == Triple::wasm64)
buildBitSetsFromFunctionsWASM(TypeIds, Functions);
@@ -1415,6 +1424,11 @@
F->addFnAttr("branch-target-enforcement", "false");
F->addFnAttr("sign-return-address", "none");
}
+ if (JumpTableArch == Triple::riscv32 || JumpTableArch == Triple::riscv64) {
+ // Make sure the jump table assembly is not modified by the assembler or
+ // the linker.
+ F->addFnAttr("target-features", "-c,-relax");
+ }
// Make sure we don't emit .eh_frame for this function.
F->addFnAttr(Attribute::NoUnwind);
Index: clang/test/Driver/fsanitize.c
===================================================================
--- clang/test/Driver/fsanitize.c
+++ clang/test/Driver/fsanitize.c
@@ -603,6 +603,8 @@
// RUN: %clang -target arm-linux-android -fvisibility=hidden -fsanitize=cfi -flto -c %s -### 2>&1 | FileCheck %s --check-prefix=CHECK-CFI
// RUN: %clang -target aarch64-linux-android -fvisibility=hidden -fsanitize=cfi -flto -c %s -### 2>&1 | FileCheck %s --check-prefix=CHECK-CFI
// RUN: %clang -target aarch64_be -fvisibility=hidden -fsanitize=cfi -flto -c %s -### 2>&1 | FileCheck %s --check-prefix=CHECK-CFI
+// RUN: %clang -target riscv32 -fvisibility=hidden -fsanitize=cfi -flto -c %s -### 2>&1 | FileCheck %s --check-prefix=CHECK-CFI
+// RUN: %clang -target riscv64 -fvisibility=hidden -fsanitize=cfi -flto -c %s -### 2>&1 | FileCheck %s --check-prefix=CHECK-CFI
// CHECK-CFI: -emit-llvm-bc{{.*}}-fsanitize=cfi-derived-cast,cfi-icall,cfi-mfcall,cfi-unrelated-cast,cfi-nvcall,cfi-vcall
// CHECK-CFI-NOMFCALL: -emit-llvm-bc{{.*}}-fsanitize=cfi-derived-cast,cfi-icall,cfi-unrelated-cast,cfi-nvcall,cfi-vcall
// CHECK-CFI-DCAST: -emit-llvm-bc{{.*}}-fsanitize=cfi-derived-cast
Index: clang/lib/Driver/ToolChain.cpp
===================================================================
--- clang/lib/Driver/ToolChain.cpp
+++ clang/lib/Driver/ToolChain.cpp
@@ -1008,7 +1008,7 @@
if (getTriple().getArch() == llvm::Triple::x86 ||
getTriple().getArch() == llvm::Triple::x86_64 ||
getTriple().getArch() == llvm::Triple::arm || getTriple().isWasm() ||
- getTriple().isAArch64())
+ getTriple().isAArch64() || getTriple().isRISCV())
Res |= SanitizerKind::CFIICall;
if (getTriple().getArch() == llvm::Triple::x86_64 ||
getTriple().isAArch64(64) || getTriple().isRISCV())
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D106888.362061.patch
Type: text/x-patch
Size: 4141 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20210727/a3c926ec/attachment.bin>
More information about the cfe-commits
mailing list