Skip to content

Commit

Permalink
Merge pull request Washi1337#583 from DaZombieKiller/pe-debug
Browse files Browse the repository at this point in the history
Support EOF data in debug data entries.
  • Loading branch information
Washi1337 authored Nov 9, 2024
2 parents 969342d + 798a4ed commit 72eee24
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/AsmResolver.PE/Debug/Builder/DebugDirectoryBuffer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public class DebugDirectoryBuffer : ISegment
public void AddEntry(DebugDataEntry entry)
{
_headers.Add(entry);
if (entry.Contents != null)
if (entry.Contents is not null and not EmptyDebugDataSegment)
_streamsTable.Add(entry.Contents, 4);
}

Expand Down
43 changes: 43 additions & 0 deletions src/AsmResolver.PE/Debug/EmptyDebugDataSegment.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using System.Diagnostics;
using AsmResolver.IO;

namespace AsmResolver.PE.Debug;

/// <summary>
/// Represents the contents of an empty debug data entry.
/// </summary>
public sealed class EmptyDebugDataSegment : IDebugDataSegment
{
/// <summary>
/// Creates a new empty debug data segment for the provided debug data content.
/// </summary>
/// <param name="type">The content type.</param>
public EmptyDebugDataSegment(DebugDataType type)
{
Type = type;
}

/// <inheritdoc />
public DebugDataType Type { get; }

bool ISegment.CanUpdateOffsets => false;

ulong IOffsetProvider.Offset => 0;

uint IOffsetProvider.Rva => 0;

/// <inheritdoc />
void ISegment.UpdateOffsets(in RelocationParameters parameters)
{
}

/// <inheritdoc />
uint IWritable.GetPhysicalSize() => 0;

/// <inheritdoc />
uint ISegment.GetVirtualSize() => 0;

void IWritable.Write(BinaryStreamWriter writer)
{
}
}
16 changes: 12 additions & 4 deletions src/AsmResolver.PE/Debug/SerializedDebugDataEntry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,17 +43,25 @@ public SerializedDebugDataEntry(
/// <inheritdoc />
protected override IDebugDataSegment? GetContents()
{
BinaryStreamReader reader;

if (_sizeOfData == 0)
return null;
return new EmptyDebugDataSegment(_type);

var reference = _context.File.GetReferenceToRva(_addressOfRawData);
if (!reference.CanRead)
if (_addressOfRawData == 0 || _context.File.MappingMode == File.PEMappingMode.Unmapped)
{
if (!_context.File.TryCreateReaderAtFileOffset(_pointerToRawData, out reader))
{
_context.BadImage("Debug data entry contains an invalid file offset.");
return null;
}
}
else if (!_context.File.TryCreateReaderAtRva(_addressOfRawData, out reader))
{
_context.BadImage("Debug data entry contains an invalid RVA.");
return null;
}

var reader = reference.CreateReader();
if (_sizeOfData > reader.Length)
{
_context.BadImage("Debug data entry contains a too large size.");
Expand Down

1 comment on commit 72eee24

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Test Results

Total Skipped Passed Failed
Unique 1875 0 💤 1875 ✅ 0 ❌
Total 5625 24 💤 5601 ✅ 0 ❌

Failing runs

Please sign in to comment.