[lld] 4140f07 - [LLD][COFF] Fix crash with /summary and PCH input files

Alexandre Ganea via llvm-commits llvm-commits at lists.llvm.org
Thu Oct 1 14:08:51 PDT 2020


Author: Alexandre Ganea
Date: 2020-10-01T17:08:35-04:00
New Revision: 4140f0744fb2deccb74e77282e23ff731f67821b

URL: https://github.com/llvm/llvm-project/commit/4140f0744fb2deccb74e77282e23ff731f67821b
DIFF: https://github.com/llvm/llvm-project/commit/4140f0744fb2deccb74e77282e23ff731f67821b.diff

LOG: [LLD][COFF] Fix crash with /summary and PCH input files

Before this patch /summary was crashing with some .PCH.OBJ files, because tpiMap[srcIdx++] was reading at the wrong location. When the TpiSource depends on a .PCH.OBJ file, the types should be offset by the previously merged PCH.OBJ set of indices.

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

Added: 
    lld/test/COFF/Inputs/precomp2-a.yaml
    lld/test/COFF/Inputs/precomp2.yaml
    lld/test/COFF/precomp-summary-fail.test

Modified: 
    lld/COFF/DebugTypes.cpp

Removed: 
    


################################################################################
diff  --git a/lld/COFF/DebugTypes.cpp b/lld/COFF/DebugTypes.cpp
index 4ce031118c82..baec05d1e87a 100644
--- a/lld/COFF/DebugTypes.cpp
+++ b/lld/COFF/DebugTypes.cpp
@@ -319,6 +319,9 @@ Error TpiSource::mergeDebugT(TypeMerger *m) {
   BinaryStreamReader reader(file->debugTypes, support::little);
   cantFail(reader.readArray(types, reader.getLength()));
 
+  // When dealing with PCH.OBJ, some indices were already merged.
+  unsigned nbHeadIndices = indexMapStorage.size();
+
   if (auto err = mergeTypeAndIdRecords(
           m->idTable, m->typeTable, indexMapStorage, types, file->pchSignature))
     fatal("codeview::mergeTypeAndIdRecords failed: " +
@@ -335,7 +338,7 @@ Error TpiSource::mergeDebugT(TypeMerger *m) {
     // collecting statistics.
     m->tpiCounts.resize(m->getTypeTable().size());
     m->ipiCounts.resize(m->getIDTable().size());
-    uint32_t srcIdx = 0;
+    uint32_t srcIdx = nbHeadIndices;
     for (CVType &ty : types) {
       TypeIndex dstIdx = tpiMap[srcIdx++];
       // Type merging may fail, so a complex source type may become the simple

diff  --git a/lld/test/COFF/Inputs/precomp2-a.yaml b/lld/test/COFF/Inputs/precomp2-a.yaml
new file mode 100644
index 000000000000..a9d497ba10a3
--- /dev/null
+++ b/lld/test/COFF/Inputs/precomp2-a.yaml
@@ -0,0 +1,84 @@
+--- !COFF
+header:
+  Machine:         IMAGE_FILE_MACHINE_AMD64
+  Characteristics: [  ]
+sections:
+  - Name:            '.debug$S'
+    Characteristics: [ IMAGE_SCN_CNT_INITIALIZED_DATA, IMAGE_SCN_MEM_DISCARDABLE, IMAGE_SCN_MEM_READ ]
+    Alignment:       1
+    Subsections:
+      - !Symbols
+        Records:
+          - Kind:            S_OBJNAME
+            ObjNameSym:
+              Signature:       545589255
+              ObjectName:      'D:\llvm-project\lld\test\COFF\Inputs\precomp2-a.obj'
+          - Kind:            S_COMPILE3
+            Compile3Sym:
+              Flags:           [ SecurityChecks, HotPatch ]
+              Machine:         X64
+              FrontendMajor:   19
+              FrontendMinor:   13
+              FrontendBuild:   26131
+              FrontendQFE:     1
+              BackendMajor:    19
+              BackendMinor:    13
+              BackendBuild:    26131
+              BackendQFE:      1
+              Version:         'Microsoft (R) Optimizing Compiler'
+      - !StringTable
+        Strings:
+          - 'D:\llvm-project\lld\test\COFF\precomp\precomp.pch'
+          - 'D:\llvm-project\lld\test\COFF\precomp\precomp.h'
+          - 'D:\llvm-project\lld\test\COFF\precomp\a.cpp'
+  - Name:            '.debug$T'
+    Characteristics: [ IMAGE_SCN_CNT_INITIALIZED_DATA, IMAGE_SCN_MEM_DISCARDABLE, IMAGE_SCN_MEM_READ ]
+    Alignment:       1
+    Types:
+      - Kind:            LF_PRECOMP
+        Precomp:
+          StartTypeIndex:  4096
+          TypesCount:      3
+          Signature:       545589255
+          PrecompFilePath: 'D:\llvm-project\lld\test\COFF\Inputs\precomp2.obj'
+      - Kind:            LF_STRING_ID
+        StringId:
+          Id:              0
+          String:          'test test test test test'
+      - Kind:            LF_STRING_ID
+        StringId:
+          Id:              0
+          String:          'test test test test test'
+      - Kind:            LF_STRING_ID
+        StringId:
+          Id:              0
+          String:          'test test test test test'
+      - Kind:            LF_BUILDINFO
+        BuildInfo:
+          ArgIndices:      [ 4101, 4101, 4101, 4101, 4101 ]
+symbols:
+  - Name:            '.debug$S'
+    Value:           0
+    SectionNumber:   1
+    SimpleType:      IMAGE_SYM_TYPE_NULL
+    ComplexType:     IMAGE_SYM_DTYPE_NULL
+    StorageClass:    IMAGE_SYM_CLASS_STATIC
+    SectionDefinition:
+      Length:          0
+      NumberOfRelocations: 0
+      NumberOfLinenumbers: 0
+      CheckSum:        0
+      Number:          0
+  - Name:            '.debug$T'
+    Value:           0
+    SectionNumber:   2
+    SimpleType:      IMAGE_SYM_TYPE_NULL
+    ComplexType:     IMAGE_SYM_DTYPE_NULL
+    StorageClass:    IMAGE_SYM_CLASS_STATIC
+    SectionDefinition:
+      Length:          0
+      NumberOfRelocations: 0
+      NumberOfLinenumbers: 0
+      CheckSum:        0
+      Number:          0
+...

diff  --git a/lld/test/COFF/Inputs/precomp2.yaml b/lld/test/COFF/Inputs/precomp2.yaml
new file mode 100644
index 000000000000..7a4ec2f25af4
--- /dev/null
+++ b/lld/test/COFF/Inputs/precomp2.yaml
@@ -0,0 +1,82 @@
+--- !COFF
+header:
+  Machine:         IMAGE_FILE_MACHINE_AMD64
+  Characteristics: [  ]
+sections:
+  - Name:            '.debug$S'
+    Characteristics: [ IMAGE_SCN_CNT_INITIALIZED_DATA, IMAGE_SCN_MEM_DISCARDABLE, IMAGE_SCN_MEM_READ ]
+    Alignment:       1
+    Subsections:
+      - !Symbols
+        Records:
+          - Kind:            S_OBJNAME
+            ObjNameSym:
+              Signature:       545589255
+              ObjectName:      'D:\llvm-project\lld\test\COFF\Inputs\precomp2.obj'
+          - Kind:            S_COMPILE3
+            Compile3Sym:
+              Flags:           [ SecurityChecks, HotPatch ]
+              Machine:         X64
+              FrontendMajor:   19
+              FrontendMinor:   13
+              FrontendBuild:   26131
+              FrontendQFE:     1
+              BackendMajor:    19
+              BackendMinor:    13
+              BackendBuild:    26131
+              BackendQFE:      1
+              Version:         'Microsoft (R) Optimizing Compiler'
+      - !StringTable
+        Strings:
+          - 'D:\llvm-project\lld\test\COFF\precomp\precomp.pch'
+  - Name:            '.debug$P'
+    Characteristics: [ IMAGE_SCN_CNT_INITIALIZED_DATA, IMAGE_SCN_MEM_DISCARDABLE, IMAGE_SCN_MEM_READ ]
+    Alignment:       1
+    PrecompTypes:
+      - Kind:            LF_STRUCTURE
+        Class:
+          MemberCount:     0
+          Options:         [ None, ForwardReference, HasUniqueName ]
+          FieldList:       0
+          Name:            _s__CatchableType
+          UniqueName:      '.?AU_s__CatchableType@@'
+          DerivationList:  0
+          VTableShape:     0
+          Size:            0
+      - Kind:            LF_MODIFIER
+        Modifier:
+          ModifiedType:    4096
+          Modifiers:       [ None, Const ]
+      - Kind:            LF_POINTER
+        Pointer:
+          ReferentType:    4096
+          Attrs:           65548
+      - Kind:            LF_ENDPRECOMP
+        EndPrecomp:
+          Signature:       545589255
+symbols:
+  - Name:            '.debug$S'
+    Value:           0
+    SectionNumber:   1
+    SimpleType:      IMAGE_SYM_TYPE_NULL
+    ComplexType:     IMAGE_SYM_DTYPE_NULL
+    StorageClass:    IMAGE_SYM_CLASS_STATIC
+    SectionDefinition:
+      Length:          0
+      NumberOfRelocations: 0
+      NumberOfLinenumbers: 0
+      CheckSum:        0
+      Number:          0
+  - Name:            '.debug$P'
+    Value:           0
+    SectionNumber:   2
+    SimpleType:      IMAGE_SYM_TYPE_NULL
+    ComplexType:     IMAGE_SYM_DTYPE_NULL
+    StorageClass:    IMAGE_SYM_CLASS_STATIC
+    SectionDefinition:
+      Length:          0
+      NumberOfRelocations: 0
+      NumberOfLinenumbers: 0
+      CheckSum:        0
+      Number:          0
+...

diff  --git a/lld/test/COFF/precomp-summary-fail.test b/lld/test/COFF/precomp-summary-fail.test
new file mode 100644
index 000000000000..b689839be9d2
--- /dev/null
+++ b/lld/test/COFF/precomp-summary-fail.test
@@ -0,0 +1,22 @@
+
+The input files were tailored so that we end up with a resulting IPI stream
+smaller than the TPI stream, which would previously trigger a crash with
+/summary.
+
+RUN: rm -rf %t && mkdir %t
+RUN: yaml2obj < %S/Inputs/precomp2.yaml -o %t\precomp2.obj
+RUN: yaml2obj < %S/Inputs/precomp2-a.yaml -o %t\precomp2-a.obj
+RUN: lld-link %t\precomp2-a.obj %t\precomp2.obj /nodefaultlib /noentry \
+RUN:    /dll /out:%t.dll /debug /summary | FileCheck %s -check-prefix SUMMARY
+
+SUMMARY:                                     Summary
+SUMMARY-NEXT: --------------------------------------------------------------------------------
+SUMMARY-NEXT:               2 Input OBJ files (expanded from all cmd-line inputs)
+SUMMARY-NEXT:               0 PDB type server dependencies
+SUMMARY-NEXT:               1 Precomp OBJ dependencies
+SUMMARY-NEXT:               3 Merged TPI records
+SUMMARY-NEXT:               2 Merged IPI records
+SUMMARY-NEXT:               1 Output PDB strings
+SUMMARY-NEXT:               0 Global symbol records
+SUMMARY-NEXT:               4 Module symbol records
+SUMMARY-NEXT:               0 Public symbol records


        


More information about the llvm-commits mailing list