diff --git a/src/Microsoft.DiaSymReader.Converter/PdbConverterWindowsToPortable.cs b/src/Microsoft.DiaSymReader.Converter/PdbConverterWindowsToPortable.cs index 5f79f526..0c27f9ae 100644 --- a/src/Microsoft.DiaSymReader.Converter/PdbConverterWindowsToPortable.cs +++ b/src/Microsoft.DiaSymReader.Converter/PdbConverterWindowsToPortable.cs @@ -251,7 +251,17 @@ public void Convert(PEReader peReader, Stream sourcePdbStream, Stream targetPdbS var symSequencePoints = symMethod.GetSequencePoints().ToImmutableArray(); - BlobHandle sequencePointsBlob = SerializeSequencePoints(metadataBuilder, localSignatureRowId, symSequencePoints, documentIndex, out var singleDocumentHandle); + // add a dummy document: + if (documentIndex.Count == 0 && symSequencePoints.Length > 0) + { + documentIndex.Add(string.Empty, metadataBuilder.AddDocument( + name: metadataBuilder.GetOrAddDocumentName(string.Empty), + hashAlgorithm: default(GuidHandle), + hash: default(BlobHandle), + language: default(GuidHandle))); + } + + BlobHandle sequencePointsBlob = SerializeSequencePoints(metadataBuilder, localSignatureRowId, symSequencePoints, documentIndex, methodToken, out var singleDocumentHandle); metadataBuilder.AddMethodDebugInformation( document: singleDocumentHandle, @@ -1022,11 +1032,12 @@ private static LocalVariableHandle NextHandle(LocalVariableHandle handle) => private static LocalConstantHandle NextHandle(LocalConstantHandle handle) => MetadataTokens.LocalConstantHandle(MetadataTokens.GetRowNumber(handle) + 1); - private static BlobHandle SerializeSequencePoints( + private BlobHandle SerializeSequencePoints( MetadataBuilder metadataBuilder, int localSignatureRowId, ImmutableArray sequencePoints, - Dictionary documentIndex, + IReadOnlyDictionary documentIndex, + int methodIndex, out DocumentHandle singleDocumentHandle) { if (sequencePoints.Length == 0) @@ -1043,7 +1054,7 @@ private static BlobHandle SerializeSequencePoints( // header: writer.WriteCompressedInteger(localSignatureRowId); - DocumentHandle previousDocument = TryGetSingleDocument(sequencePoints, documentIndex); + DocumentHandle previousDocument = TryGetSingleDocument(sequencePoints, documentIndex, methodIndex); singleDocumentHandle = previousDocument; int previousOffset = -1; @@ -1051,7 +1062,7 @@ private static BlobHandle SerializeSequencePoints( { var sequencePoint = SanitizeSequencePoint(sequencePoints[i], previousOffset); - var currentDocument = documentIndex[sequencePoint.Document.GetName()]; + var currentDocument = GetDocumentHandle(sequencePoint.Document, documentIndex, methodIndex); if (previousDocument != currentDocument) { // optional document in header or document record: @@ -1143,12 +1154,12 @@ private static SymUnmanagedSequencePoint SanitizeSequencePoint(SymUnmanagedSeque return new SymUnmanagedSequencePoint(offset, sequencePoint.Document, startLine, startColumn, endLine, endColumn); } - private static DocumentHandle TryGetSingleDocument(ImmutableArray sequencePoints, Dictionary documentIndex) + private DocumentHandle TryGetSingleDocument(ImmutableArray sequencePoints, IReadOnlyDictionary documentIndex, int methodToken) { - DocumentHandle singleDocument = documentIndex[sequencePoints[0].Document.GetName()]; + DocumentHandle singleDocument = GetDocumentHandle(sequencePoints[0].Document, documentIndex, methodToken); for (int i = 1; i < sequencePoints.Length; i++) { - if (documentIndex[sequencePoints[i].Document.GetName()] != singleDocument) + if (GetDocumentHandle(sequencePoints[i].Document, documentIndex, methodToken) != singleDocument) { return default(DocumentHandle); } @@ -1157,6 +1168,28 @@ private static DocumentHandle TryGetSingleDocument(ImmutableArray documentIndex, int methodToken) + { + string name; + try + { + name = document.GetName(); + } + catch (Exception) + { + ReportDiagnostic(PdbDiagnosticId.InvalidSequencePointDocument, methodToken); + return default(DocumentHandle); + } + + if (documentIndex.TryGetValue(name, out var handle)) + { + return handle; + } + + ReportDiagnostic(PdbDiagnosticId.InvalidSequencePointDocument, methodToken); + return default(DocumentHandle); + } + private static void SerializeDeltaLinesAndColumns(BlobBuilder writer, SymUnmanagedSequencePoint sequencePoint) { int deltaLines = sequencePoint.EndLine - sequencePoint.StartLine;