Skip to content

LowerTypeTests: Start using !elf_section_properties metadata to mark CFI jump table sections. #149261

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: users/pcc/spr/main.wip-lowertypetests-start-using-elf_section_properties-metadata-to-mark-cfi-jump-table-sections
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions compiler-rt/test/cfi/mfcall.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,12 @@ int main(int argc, char **argv) {
switch (argv[1][0]) {
case 'a':
// A: runtime error: control flow integrity check for type 'int (S::*)()' failed during non-virtual pointer to member function call
// A: note: S::f1() defined here
// A: note: S::f1() {{.*}}defined here
(s.*bitcast<S_int>(&S::f1))();
break;
case 'b':
// B: runtime error: control flow integrity check for type 'int (T::*)()' failed during non-virtual pointer to member function call
// B: note: S::f2() defined here
// B: note: S::f2() {{.*}}defined here
(t.*bitcast<T_int>(&S::f2))();
break;
case 'c':
Expand Down
27 changes: 22 additions & 5 deletions llvm/lib/Transforms/IPO/LowerTypeTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
#include "llvm/Analysis/TargetTransformInfo.h"
#include "llvm/Analysis/TypeMetadataUtils.h"
#include "llvm/Analysis/ValueTracking.h"
#include "llvm/BinaryFormat/ELF.h"
#include "llvm/DebugInfo/CodeView/CodeView.h"
#include "llvm/IR/Attributes.h"
#include "llvm/IR/BasicBlock.h"
#include "llvm/IR/Constant.h"
Expand Down Expand Up @@ -73,6 +75,7 @@
#include "llvm/Support/raw_ostream.h"
#include "llvm/TargetParser/Triple.h"
#include "llvm/Transforms/IPO.h"
#include "llvm/Transforms/IPO/CrossDSOCFI.h"
#include "llvm/Transforms/Utils/BasicBlockUtils.h"
#include "llvm/Transforms/Utils/ModuleUtils.h"
#include <algorithm>
Expand Down Expand Up @@ -498,6 +501,10 @@ class LowerTypeTestsModule {
GlobalVariable *GlobalAnnotation;
DenseSet<Value *> FunctionAnnotations;

// Cross-DSO CFI emits jumptable entries for exported functions as well as
// address taken functions in case they are address taken in other modules.
bool CrossDsoCfi = M.getModuleFlag("Cross-DSO CFI") != nullptr;

bool shouldExportConstantsAsAbsoluteSymbols();
uint8_t *exportTypeId(StringRef TypeId, const TypeIdLowering &TIL);
TypeIdLowering importTypeId(StringRef TypeId);
Expand Down Expand Up @@ -1527,6 +1534,20 @@ Triple::ArchType LowerTypeTestsModule::selectJumpTableArmEncoding(
void LowerTypeTestsModule::createJumpTable(
Function *F, ArrayRef<GlobalTypeMember *> Functions,
Triple::ArchType JumpTableArch) {
unsigned JumpTableEntrySize = getJumpTableEntrySize(JumpTableArch);
// Give the jumptable section this type in order to enable jumptable
// relaxation. Only do this if cross-DSO CFI is disabled because jumptable
// relaxation violates cross-DSO CFI's restrictions on the ordering of the
// jumptable relative to other sections.
if (!CrossDsoCfi)
F->setMetadata(LLVMContext::MD_elf_section_properties,
MDNode::get(F->getContext(),
ArrayRef<Metadata *>{
ConstantAsMetadata::get(ConstantInt::get(
Int64Ty, ELF::SHT_LLVM_CFI_JUMP_TABLE)),
ConstantAsMetadata::get(ConstantInt::get(
Int64Ty, JumpTableEntrySize))}));

BasicBlock *BB = BasicBlock::Create(M.getContext(), "entry", F);
IRBuilder<> IRB(BB);

Expand All @@ -1547,7 +1568,7 @@ void LowerTypeTestsModule::createJumpTable(
IRB.CreateUnreachable();

// Align the whole table by entry size.
F->setAlignment(Align(getJumpTableEntrySize(JumpTableArch)));
F->setAlignment(Align(JumpTableEntrySize));
// Skip prologue.
// Disabled on win32 due to https://llvm.org/bugs/show_bug.cgi?id=28641#c3.
// Luckily, this function does not get any prologue even without the
Expand Down Expand Up @@ -2114,10 +2135,6 @@ bool LowerTypeTestsModule::lower() {
unsigned CurUniqueId = 0;
SmallVector<MDNode *, 2> Types;

// Cross-DSO CFI emits jumptable entries for exported functions as well as
// address taken functions in case they are address taken in other modules.
const bool CrossDsoCfi = M.getModuleFlag("Cross-DSO CFI") != nullptr;

struct ExportedFunctionInfo {
CfiFunctionLinkage Linkage;
MDNode *FuncMD; // {name, linkage, type[, type...]}
Expand Down
2 changes: 1 addition & 1 deletion llvm/test/Transforms/LowerTypeTests/aarch64-jumptable.ll
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ define i1 @foo(ptr %p) {
;
; AARCH64: Function Attrs: naked noinline
; AARCH64-LABEL: define private void @.cfi.jumptable
; AARCH64-SAME: () #[[ATTR1:[0-9]+]] align 8 {
; AARCH64-SAME: () #[[ATTR1:[0-9]+]] align 8
; AARCH64-NEXT: entry:
; AARCH64-NEXT: call void asm sideeffect "bti c\0Ab $0\0A", "s"(ptr @f.cfi)
; AARCH64-NEXT: call void asm sideeffect "bti c\0Ab $0\0A", "s"(ptr @g.cfi)
Expand Down
4 changes: 2 additions & 2 deletions llvm/test/Transforms/LowerTypeTests/function-arm-thumb.ll
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,14 @@ define void @addrtaken() {
!0 = !{i32 0, !"typeid1"}
!1 = !{i32 0, !"typeid2"}

; CHECK: define private void {{.*}} #[[AT:.*]] align 4 {
; CHECK: define private void {{.*}} #[[AT:.*]] align 4
; CHECK-NEXT: entry:
; CHECK-NEXT: call void asm sideeffect "b.w $0\0A", "s"(ptr @f1.cfi)
; CHECK-NEXT: call void asm sideeffect "b.w $0\0A", "s"(ptr @g1.cfi)
; CHECK-NEXT: unreachable
; CHECK-NEXT: }

; CHECK: define private void {{.*}} #[[AA:.*]] align 4 {
; CHECK: define private void {{.*}} #[[AA:.*]] align 4
; CHECK-NEXT: entry:
; CHECK-NEXT: call void asm sideeffect "b $0\0A", "s"(ptr @f2.cfi)
; CHECK-NEXT: call void asm sideeffect "b $0\0A", "s"(ptr @g2.cfi)
Expand Down
4 changes: 2 additions & 2 deletions llvm/test/Transforms/LowerTypeTests/function-disjoint.ll
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ define i1 @foo(ptr %p) {
ret i1 %z
}

; X64: define private void @[[JT1]]() #{{.*}} align 8 {
; X64: define private void @[[JT1]]() #{{.*}} align 8
; X64: call void asm sideeffect "jmp ${0:c}@plt\0Aint3\0Aint3\0Aint3\0A", "s"(ptr @g.cfi)

; X64: define private void @[[JT0]]() #{{.*}} align 8 {
; X64: define private void @[[JT0]]() #{{.*}} align 8
; X64: call void asm sideeffect "jmp ${0:c}@plt\0Aint3\0Aint3\0Aint3\0A", "s"(ptr @f.cfi)

; WASM32: ![[I1]] = !{i64 2}
Expand Down
8 changes: 4 additions & 4 deletions llvm/test/Transforms/LowerTypeTests/function-weak.ll
Original file line number Diff line number Diff line change
Expand Up @@ -133,10 +133,10 @@ define i1 @foo(ptr %p) {
ret i1 %x
}

; X86: define private void @[[JT]]() #{{.*}} align 8 {
; ARM: define private void @[[JT]]() #{{.*}} align 4 {
; RISCV: define private void @[[JT]]() #{{.*}} align 8 {
; LOONGARCH64: define private void @[[JT]]() #{{.*}} align 8 {
; X86: define private void @[[JT]]() #{{.*}} align 8
; ARM: define private void @[[JT]]() #{{.*}} align 4
; RISCV: define private void @[[JT]]() #{{.*}} align 8
; LOONGARCH64: define private void @[[JT]]() #{{.*}} align 8

; CHECK-LABEL: define internal void @__cfi_global_var_init() section ".text.startup" {
; CHECK-NEXT: entry:
Expand Down
10 changes: 7 additions & 3 deletions llvm/test/Transforms/LowerTypeTests/function.ll
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,9 @@ define i1 @foo(ptr %p) {
ret i1 %x
}

; JT4: define private void @[[JT]]() #[[ATTR:.*]] align 4 {
; JT8: define private void @[[JT]]() #[[ATTR:.*]] align 8 {
; JT16: define private void @[[JT]]() #[[ATTR:.*]] align 16 {
; JT4: define private void @[[JT]]() #[[ATTR:.*]] align 4 !elf_section_properties ![[PROP:[0-9]*]] {
; JT8: define private void @[[JT]]() #[[ATTR:.*]] align 8 !elf_section_properties ![[PROP:[0-9]*]] {
; JT16: define private void @[[JT]]() #[[ATTR:.*]] align 16 !elf_section_properties ![[PROP:[0-9]*]] {

; X86: jmp ${0:c}@plt
; X86-SAME: int3
Expand Down Expand Up @@ -122,5 +122,9 @@ define i1 @foo(ptr %p) {
; RISCV: attributes #[[ATTR]] = { naked noinline "target-features"="-c,-relax" }
; LOONGARCH64: attributes #[[ATTR]] = { naked noinline }

; JT4: ![[PROP]] = !{i64 1879002126, i64 4}
; JT8: ![[PROP]] = !{i64 1879002126, i64 8}
; JT16: ![[PROP]] = !{i64 1879002126, i64 16}

; WASM32: ![[I0]] = !{i64 1}
; WASM32: ![[I1]] = !{i64 2}
2 changes: 1 addition & 1 deletion llvm/test/Transforms/LowerTypeTests/x86-jumptable.ll
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ define i1 @foo(ptr %p) {
!0 = !{i32 0, !"typeid1"}
!1 = !{i32 8, !"cf-protection-branch", i32 1}

; X86: define private void @.cfi.jumptable() #[[#ATTR:]] align 16 {
; X86: define private void @.cfi.jumptable() #[[#ATTR:]] align 16
; X86-NEXT: entry:
; X86_32-NEXT: call void asm sideeffect "endbr32\0Ajmp ${0:c}@plt\0A.balign 16, 0xcc\0A", "s"(ptr @f.cfi)
; X86_32-NEXT: call void asm sideeffect "endbr32\0Ajmp ${0:c}@plt\0A.balign 16, 0xcc\0A", "s"(ptr @g.cfi)
Expand Down
Loading