diff --git a/llvm/lib/Target/AArch64/AArch64BranchTargets.cpp b/llvm/lib/Target/AArch64/AArch64BranchTargets.cpp --- a/llvm/lib/Target/AArch64/AArch64BranchTargets.cpp +++ b/llvm/lib/Target/AArch64/AArch64BranchTargets.cpp @@ -118,6 +118,10 @@ auto MBBI = MBB.begin(); + // Skip the meta instuctions, those will be removed anyway. + for (; MBBI != MBB.end() && MBBI->isMetaInstruction(); ++MBBI) + ; + // PACI[AB]SP are implicitly BTI JC, so no BTI instruction needed there. if (MBBI != MBB.end() && (MBBI->getOpcode() == AArch64::PACIASP || MBBI->getOpcode() == AArch64::PACIBSP)) diff --git a/llvm/test/CodeGen/AArch64/branch-target-enforcement-metainstruction.ll b/llvm/test/CodeGen/AArch64/branch-target-enforcement-metainstruction.ll new file mode 100644 --- /dev/null +++ b/llvm/test/CodeGen/AArch64/branch-target-enforcement-metainstruction.ll @@ -0,0 +1,95 @@ +; RUN: llc < %s -mtriple=aarch64-unknown-linux-gnu -filetype=obj -o - \ +; RUN: | llvm-objdump -mattr=+v8.5a -d - \ +; RUN: | FileCheck %s + +; When paciasp is the first real instruction in the functions then bti should +; not be inserted. Meta instructions can be there before the paciacp. + +; IR is generated from this code snippet +; clang++ -g -mbranch-protection=standard -march=armv8-a -fPIC -target aarch64-linux-android -std=gnu++17 -S -emit-llvm +; extern "C" int foo(char* c); +; int bar(char* str){ +; char locstr[10]; +; locstr[0] = str[0]; +; return foo(locstr); +; } + +; ModuleID = 'main.cpp' +source_filename = "main.cpp" +target datalayout = "e-m:e-i8:8:32-i16:16:32-i64:64-i128:128-n32:64-S128" +target triple = "aarch64-unknown-linux-android" + +define i32 @_Z3barPc(i8* nocapture readonly %0) local_unnamed_addr #0 !dbg !15 { + %2 = alloca [10 x i8], align 4 + call void @llvm.dbg.value(metadata i8* %0, metadata !17, metadata !DIExpression()), !dbg !22 + %3 = getelementptr inbounds [10 x i8], [10 x i8]* %2, i64 0, i64 0, !dbg !23 + call void @llvm.lifetime.start.p0i8(i64 10, i8* nonnull %3) #4, !dbg !23 + call void @llvm.dbg.declare(metadata [10 x i8]* %2, metadata !18, metadata !DIExpression()), !dbg !24 + %4 = load i8, i8* %0, align 1, !dbg !25, !tbaa !26 + store i8 %4, i8* %3, align 4, !dbg !29, !tbaa !26 + %5 = call i32 @foo(i8* nonnull %3), !dbg !30 + call void @llvm.lifetime.end.p0i8(i64 10, i8* nonnull %3) #4, !dbg !31 + ret i32 %5, !dbg !32 +} + +; Function Attrs: nounwind readnone speculatable willreturn +declare void @llvm.dbg.declare(metadata, metadata, metadata) #1 + +; Function Attrs: argmemonly nounwind willreturn +declare void @llvm.lifetime.start.p0i8(i64 immarg, i8* nocapture) #2 + +declare !dbg !4 i32 @foo(i8*) local_unnamed_addr #3 + +; Function Attrs: argmemonly nounwind willreturn +declare void @llvm.lifetime.end.p0i8(i64 immarg, i8* nocapture) #2 + +; Function Attrs: nounwind readnone speculatable willreturn +declare void @llvm.dbg.value(metadata, metadata, metadata) #1 + +attributes #0 = { "branch-target-enforcement" "correctly-rounded-divide-sqrt-fp-math"="false" "disable-tail-calls"="false" "frame-pointer"="non-leaf" "less-precise-fpmad"="false" "min-legal-vector-width"="0" "no-infs-fp-math"="false" "no-jump-tables"="false" "no-nans-fp-math"="false" "no-signed-zeros-fp-math"="false" "no-trapping-math"="false" "sign-return-address"="non-leaf" "sign-return-address-key"="a_key" "stack-protector-buffer-size"="8" "target-cpu"="generic" "target-features"="+neon" "unsafe-fp-math"="false" "use-soft-float"="false" } +attributes #1 = { nounwind readnone speculatable willreturn } +attributes #2 = { argmemonly nounwind willreturn } +attributes #3 = { "branch-target-enforcement" "correctly-rounded-divide-sqrt-fp-math"="false" "disable-tail-calls"="false" "frame-pointer"="non-leaf" "less-precise-fpmad"="false" "no-infs-fp-math"="false" "no-nans-fp-math"="false" "no-signed-zeros-fp-math"="false" "no-trapping-math"="false" "sign-return-address"="non-leaf" "sign-return-address-key"="a_key" "stack-protector-buffer-size"="8" "target-cpu"="generic" "target-features"="+neon" "unsafe-fp-math"="false" "use-soft-float"="false" } +attributes #4 = { nounwind } + +!llvm.dbg.cu = !{!0} +!llvm.module.flags = !{!10, !11, !12, !13} +!llvm.ident = !{!14} + +!0 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus_14, file: !1, producer: "clang", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, enums: !2, retainedTypes: !3, splitDebugInlining: false, nameTableKind: None) +!1 = !DIFile(filename: "main.cpp", directory: "/") +!2 = !{} +!3 = !{!4} +!4 = !DISubprogram(name: "foo", scope: !1, file: !1, line: 1, type: !5, flags: DIFlagPrototyped, spFlags: DISPFlagOptimized, retainedNodes: !2) +!5 = !DISubroutineType(types: !6) +!6 = !{!7, !8} +!7 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed) +!8 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: !9, size: 64) +!9 = !DIBasicType(name: "char", size: 8, encoding: DW_ATE_unsigned_char) +!10 = !{i32 7, !"Dwarf Version", i32 4} +!11 = !{i32 2, !"Debug Info Version", i32 3} +!12 = !{i32 1, !"wchar_size", i32 4} +!13 = !{i32 7, !"PIC Level", i32 2} +!14 = !{!"clang"} +!15 = distinct !DISubprogram(name: "bar", linkageName: "_Z3barPc", scope: !1, file: !1, line: 2, type: !5, scopeLine: 2, flags: DIFlagPrototyped | DIFlagAllCallsDescribed, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !0, retainedNodes: !16) +!16 = !{!17, !18} +!17 = !DILocalVariable(name: "str", arg: 1, scope: !15, file: !1, line: 2, type: !8) +!18 = !DILocalVariable(name: "locstr", scope: !15, file: !1, line: 3, type: !19) +!19 = !DICompositeType(tag: DW_TAG_array_type, baseType: !9, size: 80, elements: !20) +!20 = !{!21} +!21 = !DISubrange(count: 10) +!22 = !DILocation(line: 0, scope: !15) +!23 = !DILocation(line: 3, column: 3, scope: !15) +!24 = !DILocation(line: 3, column: 8, scope: !15) +!25 = !DILocation(line: 4, column: 15, scope: !15) +!26 = !{!27, !27, i64 0} +!27 = !{!"omnipotent char", !28, i64 0} +!28 = !{!"Simple C++ TBAA"} +!29 = !DILocation(line: 4, column: 13, scope: !15) +!30 = !DILocation(line: 5, column: 10, scope: !15) +!31 = !DILocation(line: 6, column: 1, scope: !15) +!32 = !DILocation(line: 5, column: 3, scope: !15) + +; CHECK-LABEL: 0000000000000000 _Z3barPc: +; CHECK-NOT: bti c +; CHECK-NEXT: paciasp \ No newline at end of file