Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
49 changes: 38 additions & 11 deletions llvm/lib/Transforms/IPO/LowerTypeTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1524,7 +1524,12 @@ Triple::ArchType LowerTypeTestsModule::selectJumpTableArmEncoding(
return ArmCount > ThumbCount ? Triple::arm : Triple::thumb;
}

static llvm::DILocation *createDebugInfo(Function *F) {
// Create location for each function entry which should look like this:
// frame #0: __ubsan_check_cfi_icall_jt at sanitizer/ubsan_interface.h:0
// frame #1: c::c() (.cfi_jt) at sanitizer/ubsan_interface.h:0:0
// frame #2: .cfi.jumptable.81 at sanitizer/ubsan_interface.h:0:0
static SmallVector<llvm::DILocation *>
createJumpTableDebugInfo(Function *F, ArrayRef<GlobalTypeMember *> Functions) {
Module &M = *F->getParent();
DICompileUnit *CU = nullptr;
auto CUs = M.debug_compile_units();
Expand All @@ -1534,36 +1539,55 @@ static llvm::DILocation *createDebugInfo(Function *F) {
DIBuilder DIB(M, /*AllowUnresolved=*/true, CU);
llvm::DIFile *File = DIB.createFile("ubsan_interface.h", "sanitizer");
if (!CU) {
// Even with debug info enabled it can be missing if not info yet.
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(
llvm::DISubprogram *JTSP = DIB.createFunction(
File, F->getName(), StringRef(), File, 0, DIFnTy, 0,
DINode::FlagArtificial, DISubprogram::SPFlagDefinition);
F->setSubprogram(NormalSP);
F->setSubprogram(JTSP);

llvm::DISubprogram *InlineSP = DIB.createFunction(
llvm::DILocation *JTLoc = llvm::DILocation::get(M.getContext(), 0, 0, JTSP);

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

SmallVector<llvm::DILocation *> Locations;
Locations.reserve(Functions.size());

for (auto *Func : Functions) {
StringRef FuncName = Func->getGlobal()->getName();
FuncName.consume_back(".cfi");
llvm::DISubprogram *JumpSP = DIB.createFunction(
File, (FuncName + ".cfi_jt").str(), StringRef(), File, 0, DIFnTy, 0,
DINode::FlagArtificial, DISubprogram::SPFlagDefinition);

llvm::DILocation *EntryLoc = JTLoc;
EntryLoc = llvm::DILocation::get(M.getContext(), 0, 0, JumpSP, EntryLoc);
EntryLoc = llvm::DILocation::get(M.getContext(), 0, 0, UbsanSP, EntryLoc);
Locations.push_back(EntryLoc);
}

DIB.finalize();

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

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

SmallVector<llvm::DILocation *> Locations;
if (M.getDwarfVersion() != 0)
IRB.SetCurrentDebugLocation(createDebugInfo(F));
Locations = createJumpTableDebugInfo(F, Functions);

InlineAsm *JumpTableAsm = createJumpTableEntryAsm(JumpTableArch);

Expand All @@ -1572,12 +1596,15 @@ void LowerTypeTestsModule::createJumpTable(
// cfi.jumptable as NoUnwind, otherwise, direct calls
// to the jump table will not handle exceptions properly
bool areAllEntriesNounwind = true;
for (GlobalTypeMember *GTM : Functions) {
if (!llvm::cast<llvm::Function>(GTM->getGlobal())
assert(Locations.empty() || Functions.size() == Locations.size());
for (auto [GTM, Loc] : llvm::zip_longest(Functions, Locations)) {
if (Loc.has_value())
IRB.SetCurrentDebugLocation(*Loc);
if (!llvm::cast<llvm::Function>((*GTM)->getGlobal())
->hasFnAttribute(llvm::Attribute::NoUnwind)) {
areAllEntriesNounwind = false;
}
IRB.CreateCall(JumpTableAsm, GTM->getGlobal());
IRB.CreateCall(JumpTableAsm, (*GTM)->getGlobal());
}
IRB.CreateUnreachable();

Expand Down
11 changes: 8 additions & 3 deletions llvm/test/Transforms/LowerTypeTests/aarch64-jumptable-dbg.ll
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ define i1 @foo(ptr %p) {
; 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-NEXT: call void asm sideeffect "bti c\0Ab $0\0A", "s"(ptr @g.cfi), !dbg [[DBG13:![0-9]+]]
; AARCH64-NEXT: unreachable, !dbg [[DBG13]]
;
;.
; AARCH64: attributes #[[ATTR0:[0-9]+]] = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
Expand All @@ -75,5 +75,10 @@ define i1 @foo(ptr %p) {
; 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]])
; AARCH64: [[META10]] = !DILocation(line: 0, scope: [[META11:![0-9]+]], inlinedAt: [[META12:![0-9]+]])
; AARCH64: [[META11]] = distinct !DISubprogram(name: "f.cfi_jt", scope: [[META4]], file: [[META4]], type: [[META7]], flags: DIFlagArtificial, spFlags: DISPFlagDefinition, unit: [[META3]])
; AARCH64: [[META12]] = !DILocation(line: 0, scope: [[META6]])
; AARCH64: [[DBG13]] = !DILocation(line: 0, scope: [[META9]], inlinedAt: [[META14:![0-9]+]])
; AARCH64: [[META14]] = !DILocation(line: 0, scope: [[META15:![0-9]+]], inlinedAt: [[META12]])
; AARCH64: [[META15]] = distinct !DISubprogram(name: "g.cfi_jt", scope: [[META4]], file: [[META4]], type: [[META7]], flags: DIFlagArtificial, spFlags: DISPFlagDefinition, unit: [[META3]])
;.
22 changes: 16 additions & 6 deletions llvm/test/Transforms/LowerTypeTests/x86-jumptable-dbg.ll
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ define i1 @foo(ptr %p) {
; 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_32-NEXT: call void asm sideeffect "endbr32\0Ajmp ${0:c}@plt\0A.balign 16, 0xcc\0A", "s"(ptr @g.cfi), !dbg [[DBG13:![0-9]+]]
; X86_32-NEXT: unreachable, !dbg [[DBG13]]
;
;
; X86_64-LABEL: @f.cfi(
Expand All @@ -80,8 +80,8 @@ define i1 @foo(ptr %p) {
; 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_64-NEXT: call void asm sideeffect "endbr64\0Ajmp ${0:c}@plt\0A.balign 16, 0xcc\0A", "s"(ptr @g.cfi), !dbg [[DBG13:![0-9]+]]
; X86_64-NEXT: unreachable, !dbg [[DBG13]]
;
;.
; X86_32: attributes #[[ATTR0:[0-9]+]] = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
Expand All @@ -102,7 +102,12 @@ define i1 @foo(ptr %p) {
; 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_32: [[META10]] = !DILocation(line: 0, scope: [[META11:![0-9]+]], inlinedAt: [[META12:![0-9]+]])
; X86_32: [[META11]] = distinct !DISubprogram(name: "f.cfi_jt", scope: [[META4]], file: [[META4]], type: [[META7]], flags: DIFlagArtificial, spFlags: DISPFlagDefinition, unit: [[META3]])
; X86_32: [[META12]] = !DILocation(line: 0, scope: [[META6]])
; X86_32: [[DBG13]] = !DILocation(line: 0, scope: [[META9]], inlinedAt: [[META14:![0-9]+]])
; X86_32: [[META14]] = !DILocation(line: 0, scope: [[META15:![0-9]+]], inlinedAt: [[META12]])
; X86_32: [[META15]] = distinct !DISubprogram(name: "g.cfi_jt", scope: [[META4]], file: [[META4]], type: [[META7]], flags: DIFlagArtificial, spFlags: DISPFlagDefinition, unit: [[META3]])
;.
; X86_64: [[META0:![0-9]+]] = !{i32 8, !"cf-protection-branch", i32 1}
; X86_64: [[META1:![0-9]+]] = !{i32 7, !"Dwarf Version", i32 5}
Expand All @@ -114,5 +119,10 @@ define i1 @foo(ptr %p) {
; 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]])
; X86_64: [[META10]] = !DILocation(line: 0, scope: [[META11:![0-9]+]], inlinedAt: [[META12:![0-9]+]])
; X86_64: [[META11]] = distinct !DISubprogram(name: "f.cfi_jt", scope: [[META4]], file: [[META4]], type: [[META7]], flags: DIFlagArtificial, spFlags: DISPFlagDefinition, unit: [[META3]])
; X86_64: [[META12]] = !DILocation(line: 0, scope: [[META6]])
; X86_64: [[DBG13]] = !DILocation(line: 0, scope: [[META9]], inlinedAt: [[META14:![0-9]+]])
; X86_64: [[META14]] = !DILocation(line: 0, scope: [[META15:![0-9]+]], inlinedAt: [[META12]])
; X86_64: [[META15]] = distinct !DISubprogram(name: "g.cfi_jt", scope: [[META4]], file: [[META4]], type: [[META7]], flags: DIFlagArtificial, spFlags: DISPFlagDefinition, unit: [[META3]])
;.
Loading