Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix corrupted debug header directory entry when writing multiple such entries #869

Merged
merged 1 commit into from
Sep 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Mono.Cecil.PE/ImageWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -719,7 +719,7 @@ void BuildTextMap ()
entry.Directory = directory;

data_len += entry.Data.Length;
data_address += data_len;
data_address += entry.Data.Length;
}

debug_dir_len = directories_len + data_len;
Expand Down
12 changes: 7 additions & 5 deletions Test/Mono.Cecil.Tests/PortablePdbTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -911,7 +911,7 @@ public void ReadEmbeddedPortablePdbChecksum ()
Assert.AreEqual ("SHA256", algorithmName);
GetCodeViewPdbId (module, out byte [] pdbId);

GetEmbeddedPdb (debugHeader, out byte [] embeddedPdb);
GetEmbeddedPdb (module.Image, debugHeader, out byte [] embeddedPdb);
CalculatePdbChecksumAndId (embeddedPdb, out byte [] expectedChecksum, out byte [] expectedPdbId);

CollectionAssert.AreEqual (expectedChecksum, checksum);
Expand Down Expand Up @@ -1005,7 +1005,7 @@ public void WriteEmbeddedPortablePdbChecksum ()
Assert.AreEqual ("SHA256", algorithmName);
GetCodeViewPdbId (module, out byte [] pdbId);

GetEmbeddedPdb (debugHeader, out byte [] embeddedPdb);
GetEmbeddedPdb (module.Image, debugHeader, out byte [] embeddedPdb);
CalculatePdbChecksumAndId (embeddedPdb, out byte [] expectedChecksum, out byte [] expectedPdbId);

CollectionAssert.AreEqual (expectedChecksum, checksum);
Expand All @@ -1026,7 +1026,7 @@ public void DoubleWriteEmbeddedPortablePdbChecksum ()
byte [] pdbIdOne;
using (var module = ModuleDefinition.ReadModule (destination, new ReaderParameters { ReadSymbols = true })) {
var debugHeader = module.GetDebugHeader ();
GetEmbeddedPdb (debugHeader, out byte [] embeddedPdb);
GetEmbeddedPdb (module.Image, debugHeader, out byte [] embeddedPdb);
CalculatePdbChecksumAndId (embeddedPdb, out byte [] expectedChecksum, out pdbIdOne);
}

Expand All @@ -1037,18 +1037,20 @@ public void DoubleWriteEmbeddedPortablePdbChecksum ()
byte [] pdbIdTwo;
using (var module = ModuleDefinition.ReadModule (destination, new ReaderParameters { ReadSymbols = true })) {
var debugHeader = module.GetDebugHeader ();
GetEmbeddedPdb (debugHeader, out byte [] embeddedPdb);
GetEmbeddedPdb (module.Image, debugHeader, out byte [] embeddedPdb);
CalculatePdbChecksumAndId (embeddedPdb, out byte [] expectedChecksum, out pdbIdTwo);
}

CollectionAssert.AreEqual (pdbIdOne, pdbIdTwo);
}

private void GetEmbeddedPdb (ImageDebugHeader debugHeader, out byte [] embeddedPdb)
private void GetEmbeddedPdb (Image image, ImageDebugHeader debugHeader, out byte [] embeddedPdb)
{
var entry = Mixin.GetEmbeddedPortablePdbEntry (debugHeader);
Assert.IsNotNull (entry);

Assert.AreEqual (entry.Directory.PointerToRawData, image.ResolveVirtualAddress ((uint)entry.Directory.AddressOfRawData));

var compressed_stream = new MemoryStream (entry.Data);
var reader = new BinaryStreamReader (compressed_stream);
Assert.AreEqual (0x4244504D, reader.ReadInt32 ());
Expand Down