[llvm] 7454439 - [zero-call-used-regs] Mark only non-debug instruction's register as used

Shivam Gupta via llvm-commits llvm-commits at lists.llvm.org
Tue Jan 24 21:33:44 PST 2023


Author: Shivam Gupta
Date: 2023-01-25T11:04:22+05:30
New Revision: 7454439674a4a78b9fc9dc9ffba7a60717ee310b

URL: https://github.com/llvm/llvm-project/commit/7454439674a4a78b9fc9dc9ffba7a60717ee310b
DIFF: https://github.com/llvm/llvm-project/commit/7454439674a4a78b9fc9dc9ffba7a60717ee310b.diff

LOG: [zero-call-used-regs] Mark only non-debug instruction's register as used

zero-call-used-regs pass generate an xor instruction to help mitigate
return-oriented programming exploits via zeroing out used registers. But
in this below test case with -g option there is dbg.value instruction
associating the register with the debug-info description of the formal
parameter d, which makes the register appear used, therefore it zero the
register edi in -g case and makes binary different from without -g option.

The pass should be looking only at the non-debug uses.

$ cat test.c
char a[];
int b;
__attribute__((zero_call_used_regs("used"))) char c(int d) {
  *a = ({
    int e = d;
    b;
  });
}

This fixes https://github.com/llvm/llvm-project/issues/57962.

Differential Revision: https://reviews.llvm.org/D138757

Added: 
    llvm/test/CodeGen/X86/zero-call-used-regs-debug-info.mir

Modified: 
    llvm/lib/CodeGen/PrologEpilogInserter.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/CodeGen/PrologEpilogInserter.cpp b/llvm/lib/CodeGen/PrologEpilogInserter.cpp
index 88bd3ec1b823e..cc70ec4776506 100644
--- a/llvm/lib/CodeGen/PrologEpilogInserter.cpp
+++ b/llvm/lib/CodeGen/PrologEpilogInserter.cpp
@@ -1230,7 +1230,11 @@ void PEI::insertZeroCallUsedRegs(MachineFunction &MF) {
   BitVector UsedRegs(TRI.getNumRegs());
   if (OnlyUsed)
     for (const MachineBasicBlock &MBB : MF)
-      for (const MachineInstr &MI : MBB)
+      for (const MachineInstr &MI : MBB) {
+        // skip debug instructions
+        if (MI.isDebugInstr())
+          continue;
+
         for (const MachineOperand &MO : MI.operands()) {
           if (!MO.isReg())
             continue;
@@ -1240,6 +1244,7 @@ void PEI::insertZeroCallUsedRegs(MachineFunction &MF) {
               (MO.isDef() || MO.isUse()))
             UsedRegs.set(Reg);
         }
+      }
 
   // Get a list of registers that are used.
   BitVector LiveIns(TRI.getNumRegs());

diff  --git a/llvm/test/CodeGen/X86/zero-call-used-regs-debug-info.mir b/llvm/test/CodeGen/X86/zero-call-used-regs-debug-info.mir
new file mode 100644
index 0000000000000..68505b6d94528
--- /dev/null
+++ b/llvm/test/CodeGen/X86/zero-call-used-regs-debug-info.mir
@@ -0,0 +1,142 @@
+# NOTE: Assertions have been autogenerated by utils/update_mir_test_checks.py
+# RUN: llc -run-pass=prologepilog -o - %s | FileCheck %s
+
+# Test that the presence of debug instructions doesn't trigger arbitrary registers that are unused to be zeroed out.
+
+--- |
+  ; ModuleID = 'zero-call-used-regs-debug-info.ll'
+  source_filename = "zero-call-used-regs-debug-info.c"
+  target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
+  target triple = "x86_64-unknown-linux-gnu"
+
+  @b = dso_local local_unnamed_addr global i32 0, align 4, !dbg !0
+  @a = dso_local local_unnamed_addr global [1 x i8] zeroinitializer, align 1, !dbg !5
+
+  ; Function Attrs: mustprogress nofree norecurse nosync nounwind willreturn memory(readwrite, argmem: none, inaccessiblemem: none) uwtable
+  define dso_local signext i8 @c(i32 noundef %d) local_unnamed_addr #0 !dbg !19 {
+  entry:
+    call void @llvm.dbg.value(metadata i32 %d, metadata !23, metadata !DIExpression()), !dbg !26
+    call void @llvm.dbg.value(metadata i32 %d, metadata !24, metadata !DIExpression()), !dbg !27
+    %0 = load i32, ptr @b, align 4, !dbg !28, !tbaa !29
+    %conv = trunc i32 %0 to i8, !dbg !33
+    store i8 %conv, ptr @a, align 1, !dbg !34, !tbaa !35
+    ret i8 undef, !dbg !36
+  }
+
+  ; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
+  declare void @llvm.dbg.value(metadata, metadata, metadata) #2
+
+  attributes #0 = { mustprogress nofree norecurse nosync nounwind willreturn memory(readwrite, argmem: none, inaccessiblemem: none) uwtable "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" "zero-call-used-regs"="used" }
+  attributes #1 = { mustprogress nofree norecurse nosync nounwind willreturn memory(none) uwtable "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }
+  attributes #2 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
+
+  !llvm.dbg.cu = !{!2}
+  !llvm.module.flags = !{!12, !13, !14, !15, !16, !17}
+  !llvm.ident = !{!18}
+
+  !0 = !DIGlobalVariableExpression(var: !1, expr: !DIExpression())
+  !1 = distinct !DIGlobalVariable(name: "b", scope: !2, file: !3, line: 2, type: !11, isLocal: false, isDefinition: true)
+  !2 = distinct !DICompileUnit(language: DW_LANG_C11, file: !3, producer: "clang version 16.0.0 (https://github.com/llvm/llvm-project.git ba5acedd979aa94a81be555e0add8e4421499d4f)", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, globals: !4, splitDebugInlining: false, nameTableKind: None)
+  !3 = !DIFile(filename: "zero-call-used-regs-debug-info.c", directory: "/home/xgupta/compiler/llvm-project/build/bin", checksumkind: CSK_MD5, checksum: "fe3a5f45289411a008e07c7debb490ca")
+  !4 = !{!5, !0}
+  !5 = !DIGlobalVariableExpression(var: !6, expr: !DIExpression())
+  !6 = distinct !DIGlobalVariable(name: "a", scope: !2, file: !3, line: 1, type: !7, isLocal: false, isDefinition: true)
+  !7 = !DICompositeType(tag: DW_TAG_array_type, baseType: !8, size: 8, elements: !9)
+  !8 = !DIBasicType(name: "char", size: 8, encoding: DW_ATE_signed_char)
+  !9 = !{!10}
+  !10 = !DISubrange(count: 1)
+  !11 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
+  !12 = !{i32 7, !"Dwarf Version", i32 5}
+  !13 = !{i32 2, !"Debug Info Version", i32 3}
+  !14 = !{i32 1, !"wchar_size", i32 4}
+  !15 = !{i32 8, !"PIC Level", i32 2}
+  !16 = !{i32 7, !"PIE Level", i32 2}
+  !17 = !{i32 7, !"uwtable", i32 2}
+  !18 = !{!"clang version 16.0.0 (https://github.com/llvm/llvm-project.git ba5acedd979aa94a81be555e0add8e4421499d4f)"}
+  !19 = distinct !DISubprogram(name: "c", scope: !3, file: !3, line: 3, type: !20, scopeLine: 3, flags: DIFlagPrototyped | DIFlagAllCallsDescribed, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !2, retainedNodes: !22)
+  !20 = !DISubroutineType(types: !21)
+  !21 = !{!8, !11}
+  !22 = !{!23, !24}
+  !23 = !DILocalVariable(name: "d", arg: 1, scope: !19, file: !3, line: 3, type: !11)
+  !24 = !DILocalVariable(name: "e", scope: !25, file: !3, line: 5, type: !11)
+  !25 = distinct !DILexicalBlock(scope: !19, file: !3, line: 4, column: 9)
+  !26 = !DILocation(line: 0, scope: !19)
+  !27 = !DILocation(line: 0, scope: !25)
+  !28 = !DILocation(line: 6, column: 5, scope: !25)
+  !29 = !{!30, !30, i64 0}
+  !30 = !{!"int", !31, i64 0}
+  !31 = !{!"omnipotent char", !32, i64 0}
+  !32 = !{!"Simple C/C++ TBAA"}
+  !33 = !DILocation(line: 4, column: 8, scope: !19)
+  !34 = !DILocation(line: 4, column: 6, scope: !19)
+  !35 = !{!31, !31, i64 0}
+  !36 = !DILocation(line: 8, column: 1, scope: !19)
+  !37 = distinct !DISubprogram(name: "main", scope: !3, file: !3, line: 9, type: !38, scopeLine: 9, flags: DIFlagAllCallsDescribed, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !2, retainedNodes: !40)
+  !38 = !DISubroutineType(types: !39)
+  !39 = !{null}
+  !40 = !{}
+  !41 = !DILocation(line: 9, column: 14, scope: !37)
+
+...
+---
+name:            c
+alignment:       16
+exposesReturnsTwice: false
+legalized:       false
+regBankSelected: false
+selected:        false
+failedISel:      false
+tracksRegLiveness: true
+hasWinCFI:       false
+callsEHReturn:   false
+callsUnwindInit: false
+hasEHCatchret:   false
+hasEHScopes:     false
+hasEHFunclets:   false
+debugInstrRef:   true
+failsVerification: false
+tracksDebugUserValues: true
+registers:       []
+liveins:         []
+frameInfo:
+  isFrameAddressTaken: false
+  isReturnAddressTaken: false
+  hasStackMap:     false
+  hasPatchPoint:   false
+  stackSize:       0
+  offsetAdjustment: 0
+  maxAlignment:    1
+  adjustsStack:    false
+  hasCalls:        false
+  stackProtector:  ''
+  functionContext: ''
+  maxCallFrameSize: 4294967295
+  cvBytesOfCalleeSavedRegisters: 0
+  hasOpaqueSPAdjustment: false
+  hasVAStart:      false
+  hasMustTailInVarArgFunc: false
+  hasTailCall:     false
+  localFrameSize:  0
+  savePoint:       ''
+  restorePoint:    ''
+fixedStack:      []
+stack:           []
+callSites:       []
+debugValueSubstitutions: []
+constants:       []
+machineFunctionInfo: {}
+body:             |
+  bb.0.entry:
+    ; CHECK-LABEL: name: c
+    ; CHECK: DBG_VALUE $edi, $noreg, !23, !DIExpression(), debug-location !26
+    ; CHECK-NEXT: DBG_VALUE $edi, $noreg, !24, !DIExpression(), debug-location !27
+    ; CHECK-NEXT: renamable $al = MOV8rm $rip, 1, $noreg, @b, $noreg, debug-location !28 :: (dereferenceable load (s8) from @b, align 4, !tbaa !29)
+    ; CHECK-NEXT: MOV8mr $rip, 1, $noreg, @a, $noreg, killed renamable $al, debug-location !34 :: (store (s8) into @a, !tbaa !35)
+    ; CHECK-NEXT: RET 0, undef $al, debug-location !36
+    DBG_VALUE $edi, $noreg, !23, !DIExpression(), debug-location !26
+    DBG_VALUE $edi, $noreg, !24, !DIExpression(), debug-location !27
+    renamable $al = MOV8rm $rip, 1, $noreg, @b, $noreg, debug-location !28 :: (dereferenceable load (s8) from @b, align 4, !tbaa !29)
+    MOV8mr $rip, 1, $noreg, @a, $noreg, killed renamable $al, debug-location !34 :: (store (s8) into @a, !tbaa !35)
+    RET 0, undef $al, debug-location !36
+
+...


        


More information about the llvm-commits mailing list