[CodeView] Clamp Frontend version
authormodimo <modimo@fb.com>
Tue, 28 Dec 2021 22:48:15 +0000 (14:48 -0800)
committermodimo <modimo@fb.com>
Tue, 28 Dec 2021 23:22:18 +0000 (15:22 -0800)
D43002 introduced a test debug-info-objname.cpp that outputted the current compiler version into CodeView. Internally we appended a date to the patch version and overflowed the 16-bits allocated to that space. This change clamps the Frontend version outputted values to 16-bits like rGd1185fc081ead71a8bf239ff1814f5ff73084c15 did for the Backend version.

Testing:
ninja check-all
newly added tests correctly clamps and no longer asserts when trying to output the field

Reviewed By: aganea

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

llvm/lib/CodeGen/AsmPrinter/CodeViewDebug.cpp
llvm/test/MC/COFF/cv-compiler-info-clamp.ll [new file with mode: 0644]

index d621108..ed74d2b 100644 (file)
@@ -857,8 +857,10 @@ void CodeViewDebug::emitCompilerInformation() {
   StringRef CompilerVersion = CU->getProducer();
   Version FrontVer = parseVersion(CompilerVersion);
   OS.AddComment("Frontend version");
-  for (int N : FrontVer.Part)
+  for (int N : FrontVer.Part) {
+    N = std::min<int>(N, std::numeric_limits<uint16_t>::max());
     OS.emitInt16(N);
+  }
 
   // Some Microsoft tools, like Binscope, expect a backend version number of at
   // least 8.something, so we'll coerce the LLVM version into a form that
diff --git a/llvm/test/MC/COFF/cv-compiler-info-clamp.ll b/llvm/test/MC/COFF/cv-compiler-info-clamp.ll
new file mode 100644 (file)
index 0000000..240d4cc
--- /dev/null
@@ -0,0 +1,61 @@
+; Check that a large version number (4.0.20211223) is clamped to proper size
+; RUN: llc -mtriple i686-pc-windows-msvc < %s | FileCheck %s --check-prefixes=CHECK,STDOUT
+; RUN: llc -mtriple i686-pc-windows-msvc < %s -o %t
+; RUN: FileCheck %s --input-file=%t --check-prefixes=CHECK,FILE
+
+; RUN: llvm-mc %t -triple=i686-pc-win32 -filetype=obj -o %t.obj
+; RUN: llvm-pdbutil dump -il -symbols %t.obj | FileCheck %s --check-prefixes=CODEVIEW
+
+; ModuleID = 'D:\src\scopes\foo.cpp'
+source_filename = "D:\5Csrc\5Cscopes\5Cfoo.cpp"
+target datalayout = "e-m:x-p:32:32-i64:64-f80:32-n8:16:32-a:0:32-S32"
+target triple = "i686-pc-windows-msvc19.0.23918"
+
+; Function Attrs: nounwind sspstrong
+define i32 @"\01?foo@@YAHXZ"() #0 !dbg !10 {
+entry:
+  ret i32 42, !dbg !14
+}
+
+attributes #0 = { nounwind sspstrong "correctly-rounded-divide-sqrt-fp-math"="false" "disable-tail-calls"="false" "less-precise-fpmad"="false" "frame-pointer"="all" "no-infs-fp-math"="false" "no-jump-tables"="false" "no-nans-fp-math"="false" "no-signed-zeros-fp-math"="false" "no-trapping-math"="false" "stack-protector-buffer-size"="8" "target-cpu"="pentium4" "target-features"="+fxsr,+mmx,+sse,+sse2,+x87" "unsafe-fp-math"="false" "use-soft-float"="false" }
+
+!llvm.dbg.cu = !{!0}
+!llvm.module.flags = !{!7, !8}
+!llvm.ident = !{!9}
+
+!0 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus, file: !1, producer: "clang version 4.0.20211223 ", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug, enums: !2)
+; One .debug$S section should contain an S_COMPILE3 record that identifies the
+; source language and the version of the compiler based on the DICompileUnit.
+; CHECK:       .section        .debug$S,"dr"
+; CHECK:        .short  4353                    # Record kind: S_OBJNAME
+; CHECK-NEXT:   .long   0                       # Signature
+; STDOUT-NEXT:  .byte   0                       # Object name
+; FILE-NEXT:    .asciz "{{.*}}{{\\\\|/}}cv-compiler-info-clamp.ll.tmp" # Object name
+; CHECK:               .short  4412                    # Record kind: S_COMPILE3
+; CHECK-NEXT:   .long  1                       # Flags and language
+; CHECK-NEXT:  .short  7                       # CPUType
+; CHECK-NEXT:  .short  4                       # Frontend version
+; CHECK-NEXT:  .short  0
+; CHECK-NEXT:  .short  65535
+; CHECK-NEXT:  .short  0
+; CHECK-NEXT:  .short  [[BACKEND_VERSION:[0-9]+]]  # Backend version
+; CHECK-NEXT:  .short  0
+; CHECK-NEXT:  .short  0
+; CHECK-NEXT:  .short  0
+; CHECK-NEXT:  .asciz  "clang version 4.0.20211223 "  # Null-terminated compiler version string
+; CHECK-NOT:    .short 4412                  # Record kind: S_COMPILE3
+!1 = !DIFile(filename: "D:\5Csrc\5Cscopes\5Cfoo.cpp", directory: "D:\5Csrc\5Cscopes\5Cclang")
+!2 = !{}
+!7 = !{i32 2, !"CodeView", i32 1}
+!8 = !{i32 2, !"Debug Info Version", i32 3}
+!9 = !{!"clang version 4.0.20211223 "}
+!10 = distinct !DISubprogram(name: "foo", linkageName: "\01?foo@@YAHXZ", scope: !1, file: !1, line: 1, type: !11, isLocal: false, isDefinition: true, scopeLine: 1, flags: DIFlagPrototyped, isOptimized: false, unit: !0, retainedNodes: !2)
+!11 = !DISubroutineType(types: !12)
+!12 = !{!13}
+!13 = !DIBasicType(name: "int", size: 32, align: 32, encoding: DW_ATE_signed)
+!14 = !DILocation(line: 2, scope: !10)
+
+; CODEVIEW:      S_COMPILE3 [size = 56]
+; CODEVIEW-NEXT: machine = intel pentium 3, Ver = clang version 4.0.20211223 , language = c++
+; Backend version is based off of compiler version building this which is variable
+; CODEVIEW-NEXT:   frontend = 4.0.65535.0, backend =