Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 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
36 changes: 36 additions & 0 deletions llvm/lib/Transforms/IPO/LowerTypeTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
#include "llvm/IR/BasicBlock.h"
#include "llvm/IR/Constant.h"
#include "llvm/IR/Constants.h"
#include "llvm/IR/DIBuilder.h"
#include "llvm/IR/DataLayout.h"
#include "llvm/IR/DerivedTypes.h"
#include "llvm/IR/Function.h"
Expand Down Expand Up @@ -1523,11 +1524,46 @@ Triple::ArchType LowerTypeTestsModule::selectJumpTableArmEncoding(
return ArmCount > ThumbCount ? Triple::arm : Triple::thumb;
}

static llvm::DILocation *createDebugInfo(Function *F) {
Module &M = *F->getParent();
DICompileUnit *CU = nullptr;
auto CUs = M.debug_compile_units();
if (!CUs.empty())
CU = *CUs.begin();

DIBuilder DIB(M, /*AllowUnresolved=*/true, CU);
llvm::DIFile *File = DIB.createFile("ubsan_interface.h", "sanitizer");
if (!CU) {
CU = DIB.createCompileUnit(
DISourceLanguageName(dwarf::DW_LANG_C), File, "llvm", true, "", 0, "",
DICompileUnit::DebugEmissionKind::LineTablesOnly);
}

DISubroutineType *DIFnTy = DIB.createSubroutineType(nullptr);

llvm::DISubprogram *NormalSP = DIB.createFunction(
File, F->getName(), StringRef(), File, 0, DIFnTy, 0,
DINode::FlagArtificial, DISubprogram::SPFlagDefinition);
F->setSubprogram(NormalSP);

llvm::DISubprogram *InlineSP = DIB.createFunction(
File, "__ubsan_check_cfi_icall_jt", StringRef(), File, 0, DIFnTy, 0,
DINode::FlagArtificial, DISubprogram::SPFlagDefinition);

DIB.finalize();

return llvm::DILocation::get(
M.getContext(), 0, 0, InlineSP,
llvm::DILocation::get(M.getContext(), 0, 0, NormalSP));
}

void LowerTypeTestsModule::createJumpTable(
Function *F, ArrayRef<GlobalTypeMember *> Functions,
Triple::ArchType JumpTableArch) {
BasicBlock *BB = BasicBlock::Create(M.getContext(), "entry", F);
IRBuilder<> IRB(BB);
if (M.getDwarfVersion() != 0)
IRB.SetCurrentDebugLocation(createDebugInfo(F));

InlineAsm *JumpTableAsm = createJumpTableEntryAsm(JumpTableArch);

Expand Down
79 changes: 79 additions & 0 deletions llvm/test/Transforms/LowerTypeTests/aarch64-jumptable-dbg.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --check-attributes --check-globals --include-generated-funcs
; RUN: opt -S -passes=lowertypetests -mtriple=aarch64-unknown-linux-gnu %s | FileCheck --check-prefixes=AARCH64 %s

; Test for the jump table generation with branch protection on AArch64

target datalayout = "e-p:64:64"

@0 = private unnamed_addr constant [2 x ptr] [ptr @f, ptr @g], align 16


define void @f() !type !0 {
ret void
}

define internal void @g() !type !0 {
ret void
}

!0 = !{i32 0, !"typeid1"}

declare i1 @llvm.type.test(ptr %ptr, metadata %bitset) nounwind readnone

define i1 @foo(ptr %p) {
%x = call i1 @llvm.type.test(ptr %p, metadata !"typeid1")
ret i1 %x
}

!llvm.module.flags = !{!1, !2, !3}
!1 = !{i32 4, !"branch-target-enforcement", i32 1}
!2 = !{i32 7, !"Dwarf Version", i32 5}
!3 = !{i32 2, !"Debug Info Version", i32 3}


;.
; AARCH64: @[[GLOB0:[0-9]+]] = private unnamed_addr constant [2 x ptr] [ptr @f, ptr @g], align 16
; AARCH64: @[[GLOB1:[0-9]+]] = private constant [0 x i8] zeroinitializer
; AARCH64: @f = alias [8 x i8], ptr @.cfi.jumptable
; AARCH64: @g = internal alias [8 x i8], getelementptr inbounds ([2 x [8 x i8]], ptr @.cfi.jumptable, i64 0, i64 1)
;.
; AARCH64-LABEL: @f.cfi(
; AARCH64-NEXT: ret void
;
;
; AARCH64-LABEL: @g.cfi(
; AARCH64-NEXT: ret void
;
;
; AARCH64-LABEL: @foo(
; AARCH64-NEXT: [[TMP1:%.*]] = ptrtoint ptr [[P:%.*]] to i64
; AARCH64-NEXT: [[TMP2:%.*]] = sub i64 ptrtoint (ptr getelementptr (i8, ptr @.cfi.jumptable, i64 8) to i64), [[TMP1]]
; AARCH64-NEXT: [[TMP3:%.*]] = call i64 @llvm.fshr.i64(i64 [[TMP2]], i64 [[TMP2]], i64 3)
; AARCH64-NEXT: [[TMP4:%.*]] = icmp ule i64 [[TMP3]], 1
; AARCH64-NEXT: ret i1 [[TMP4]]
;
;
; AARCH64: Function Attrs: naked noinline
; AARCH64-LABEL: @.cfi.jumptable(
; AARCH64-NEXT: entry:
; AARCH64-NEXT: call void asm sideeffect "bti c\0Ab $0\0A", "s"(ptr @f.cfi), !dbg [[DBG8:![0-9]+]]
; AARCH64-NEXT: call void asm sideeffect "bti c\0Ab $0\0A", "s"(ptr @g.cfi), !dbg [[DBG8]]
; AARCH64-NEXT: unreachable, !dbg [[DBG8]]
;
;.
; AARCH64: attributes #[[ATTR0:[0-9]+]] = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
; AARCH64: attributes #[[ATTR1:[0-9]+]] = { naked noinline }
; AARCH64: attributes #[[ATTR2:[0-9]+]] = { nocallback nocreateundeforpoison nofree nosync nounwind speculatable willreturn memory(none) }
;.
; AARCH64: [[META0:![0-9]+]] = !{i32 4, !"branch-target-enforcement", i32 1}
; AARCH64: [[META1:![0-9]+]] = !{i32 7, !"Dwarf Version", i32 5}
; AARCH64: [[META2:![0-9]+]] = !{i32 2, !"Debug Info Version", i32 3}
; AARCH64: [[META3:![0-9]+]] = distinct !DICompileUnit(language: DW_LANG_C, file: [[META4:![0-9]+]], producer: "llvm", isOptimized: true, runtimeVersion: 0, emissionKind: LineTablesOnly)
; AARCH64: [[META4]] = !DIFile(filename: "{{.*}}ubsan_interface.h", directory: {{.*}})
; AARCH64: [[META5:![0-9]+]] = !{i32 0, !"typeid1"}
; AARCH64: [[META6:![0-9]+]] = distinct !DISubprogram(name: ".cfi.jumptable", scope: [[META4]], file: [[META4]], type: [[META7:![0-9]+]], flags: DIFlagArtificial, spFlags: DISPFlagDefinition, unit: [[META3]])
; AARCH64: [[META7]] = !DISubroutineType(types: null)
; AARCH64: [[DBG8]] = !DILocation(line: 0, scope: [[META9:![0-9]+]], inlinedAt: [[META10:![0-9]+]])
; AARCH64: [[META9]] = distinct !DISubprogram(name: "__ubsan_check_cfi_icall_jt", scope: [[META4]], file: [[META4]], type: [[META7]], flags: DIFlagArtificial, spFlags: DISPFlagDefinition, unit: [[META3]])
; AARCH64: [[META10]] = !DILocation(line: 0, scope: [[META6]])
;.
118 changes: 118 additions & 0 deletions llvm/test/Transforms/LowerTypeTests/x86-jumptable-dbg.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --check-globals --include-generated-funcs
;; Test jump table generation with Indirect Branch Tracking on x86.
; RUN: opt -S -passes=lowertypetests -mtriple=i686 %s | FileCheck --check-prefixes=X86_32 %s
; RUN: opt -S -passes=lowertypetests -mtriple=x86_64 %s | FileCheck --check-prefixes=X86_64 %s

@0 = private unnamed_addr constant [2 x ptr] [ptr @f, ptr @g], align 16

define void @f() !type !0 {
ret void
}

define internal void @g() !type !0 {
ret void
}

declare i1 @llvm.type.test(ptr %ptr, metadata %bitset) nounwind readnone

define i1 @foo(ptr %p) {
%x = call i1 @llvm.type.test(ptr %p, metadata !"typeid1")
ret i1 %x
}

!llvm.module.flags = !{!1, !2, !3}
!0 = !{i32 0, !"typeid1"}
!1 = !{i32 8, !"cf-protection-branch", i32 1}
!2 = !{i32 7, !"Dwarf Version", i32 5}
!3 = !{i32 2, !"Debug Info Version", i32 3}


;.
; X86_32: @[[GLOB0:[0-9]+]] = private unnamed_addr constant [2 x ptr] [ptr @f, ptr @g], align 16
; X86_32: @[[GLOB1:[0-9]+]] = private constant [0 x i8] zeroinitializer
; X86_32: @f = alias [16 x i8], ptr @.cfi.jumptable
; X86_32: @g = internal alias [16 x i8], getelementptr inbounds ([2 x [16 x i8]], ptr @.cfi.jumptable, i32 0, i32 1)
;.
; X86_64: @[[GLOB0:[0-9]+]] = private unnamed_addr constant [2 x ptr] [ptr @f, ptr @g], align 16
; X86_64: @[[GLOB1:[0-9]+]] = private constant [0 x i8] zeroinitializer
; X86_64: @f = alias [16 x i8], ptr @.cfi.jumptable
; X86_64: @g = internal alias [16 x i8], getelementptr inbounds ([2 x [16 x i8]], ptr @.cfi.jumptable, i64 0, i64 1)
;.
; X86_32-LABEL: @f.cfi(
; X86_32-NEXT: ret void
;
;
; X86_32-LABEL: @g.cfi(
; X86_32-NEXT: ret void
;
;
; X86_32-LABEL: @foo(
; X86_32-NEXT: [[TMP1:%.*]] = ptrtoint ptr [[P:%.*]] to i32
; X86_32-NEXT: [[TMP2:%.*]] = sub i32 ptrtoint (ptr getelementptr (i8, ptr @.cfi.jumptable, i32 16) to i32), [[TMP1]]
; X86_32-NEXT: [[TMP3:%.*]] = call i32 @llvm.fshr.i32(i32 [[TMP2]], i32 [[TMP2]], i32 4)
; X86_32-NEXT: [[TMP4:%.*]] = icmp ule i32 [[TMP3]], 1
; X86_32-NEXT: ret i1 [[TMP4]]
;
;
; X86_32-LABEL: @.cfi.jumptable(
; X86_32-NEXT: entry:
; X86_32-NEXT: call void asm sideeffect "endbr32\0Ajmp ${0:c}@plt\0A.balign 16, 0xcc\0A", "s"(ptr @f.cfi), !dbg [[DBG8:![0-9]+]]
; X86_32-NEXT: call void asm sideeffect "endbr32\0Ajmp ${0:c}@plt\0A.balign 16, 0xcc\0A", "s"(ptr @g.cfi), !dbg [[DBG8]]
; X86_32-NEXT: unreachable, !dbg [[DBG8]]
;
;
; X86_64-LABEL: @f.cfi(
; X86_64-NEXT: ret void
;
;
; X86_64-LABEL: @g.cfi(
; X86_64-NEXT: ret void
;
;
; X86_64-LABEL: @foo(
; X86_64-NEXT: [[TMP1:%.*]] = ptrtoint ptr [[P:%.*]] to i64
; X86_64-NEXT: [[TMP2:%.*]] = sub i64 ptrtoint (ptr getelementptr (i8, ptr @.cfi.jumptable, i64 16) to i64), [[TMP1]]
; X86_64-NEXT: [[TMP3:%.*]] = call i64 @llvm.fshr.i64(i64 [[TMP2]], i64 [[TMP2]], i64 4)
; X86_64-NEXT: [[TMP4:%.*]] = icmp ule i64 [[TMP3]], 1
; X86_64-NEXT: ret i1 [[TMP4]]
;
;
; X86_64-LABEL: @.cfi.jumptable(
; X86_64-NEXT: entry:
; X86_64-NEXT: call void asm sideeffect "endbr64\0Ajmp ${0:c}@plt\0A.balign 16, 0xcc\0A", "s"(ptr @f.cfi), !dbg [[DBG8:![0-9]+]]
; X86_64-NEXT: call void asm sideeffect "endbr64\0Ajmp ${0:c}@plt\0A.balign 16, 0xcc\0A", "s"(ptr @g.cfi), !dbg [[DBG8]]
; X86_64-NEXT: unreachable, !dbg [[DBG8]]
;
;.
; X86_32: attributes #[[ATTR0:[0-9]+]] = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
; X86_32: attributes #[[ATTR1:[0-9]+]] = { naked nocf_check noinline }
; X86_32: attributes #[[ATTR2:[0-9]+]] = { nocallback nocreateundeforpoison nofree nosync nounwind speculatable willreturn memory(none) }
;.
; X86_64: attributes #[[ATTR0:[0-9]+]] = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
; X86_64: attributes #[[ATTR1:[0-9]+]] = { naked nocf_check noinline }
; X86_64: attributes #[[ATTR2:[0-9]+]] = { nocallback nocreateundeforpoison nofree nosync nounwind speculatable willreturn memory(none) }
;.
; X86_32: [[META0:![0-9]+]] = !{i32 8, !"cf-protection-branch", i32 1}
; X86_32: [[META1:![0-9]+]] = !{i32 7, !"Dwarf Version", i32 5}
; X86_32: [[META2:![0-9]+]] = !{i32 2, !"Debug Info Version", i32 3}
; X86_32: [[META3:![0-9]+]] = distinct !DICompileUnit(language: DW_LANG_C, file: [[META4:![0-9]+]], producer: "llvm", isOptimized: true, runtimeVersion: 0, emissionKind: LineTablesOnly)
; X86_32: [[META4]] = !DIFile(filename: "{{.*}}ubsan_interface.h", directory: {{.*}})
; X86_32: [[META5:![0-9]+]] = !{i32 0, !"typeid1"}
; X86_32: [[META6:![0-9]+]] = distinct !DISubprogram(name: ".cfi.jumptable", scope: [[META4]], file: [[META4]], type: [[META7:![0-9]+]], flags: DIFlagArtificial, spFlags: DISPFlagDefinition, unit: [[META3]])
; X86_32: [[META7]] = !DISubroutineType(types: null)
; X86_32: [[DBG8]] = !DILocation(line: 0, scope: [[META9:![0-9]+]], inlinedAt: [[META10:![0-9]+]])
; X86_32: [[META9]] = distinct !DISubprogram(name: "__ubsan_check_cfi_icall_jt", scope: [[META4]], file: [[META4]], type: [[META7]], flags: DIFlagArtificial, spFlags: DISPFlagDefinition, unit: [[META3]])
; X86_32: [[META10]] = !DILocation(line: 0, scope: [[META6]])
;.
; X86_64: [[META0:![0-9]+]] = !{i32 8, !"cf-protection-branch", i32 1}
; X86_64: [[META1:![0-9]+]] = !{i32 7, !"Dwarf Version", i32 5}
; X86_64: [[META2:![0-9]+]] = !{i32 2, !"Debug Info Version", i32 3}
; X86_64: [[META3:![0-9]+]] = distinct !DICompileUnit(language: DW_LANG_C, file: [[META4:![0-9]+]], producer: "llvm", isOptimized: true, runtimeVersion: 0, emissionKind: LineTablesOnly)
; X86_64: [[META4]] = !DIFile(filename: "{{.*}}ubsan_interface.h", directory: {{.*}})
; X86_64: [[META5:![0-9]+]] = !{i32 0, !"typeid1"}
; X86_64: [[META6:![0-9]+]] = distinct !DISubprogram(name: ".cfi.jumptable", scope: [[META4]], file: [[META4]], type: [[META7:![0-9]+]], flags: DIFlagArtificial, spFlags: DISPFlagDefinition, unit: [[META3]])
; X86_64: [[META7]] = !DISubroutineType(types: null)
; X86_64: [[DBG8]] = !DILocation(line: 0, scope: [[META9:![0-9]+]], inlinedAt: [[META10:![0-9]+]])
; X86_64: [[META9]] = distinct !DISubprogram(name: "__ubsan_check_cfi_icall_jt", scope: [[META4]], file: [[META4]], type: [[META7]], flags: DIFlagArtificial, spFlags: DISPFlagDefinition, unit: [[META3]])
; X86_64: [[META10]] = !DILocation(line: 0, scope: [[META6]])
;.
Loading