Skip to content

Commit b10c80b

Browse files
author
Max Charlamb
committed
isolate PEDecoder to use only Stream
1 parent 1c4606e commit b10c80b

File tree

2 files changed

+45
-15
lines changed

2 files changed

+45
-15
lines changed

src/native/managed/cdacreader/src/Decoder/PEDecoder.cs

Lines changed: 38 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,35 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4+
using System;
45
using System.IO;
6+
using System.Text;
57
using Microsoft.Diagnostics.DataContractReader.Decoder.PETypes;
6-
using Microsoft.Diagnostics.DataContractReader.Legacy;
78

89
namespace Microsoft.Diagnostics.DataContractReader.Decoder;
9-
internal sealed class PEDecoder
10+
internal sealed class PEDecoder : IDisposable
1011
{
11-
private readonly ICLRDataTarget _dataTarget;
12-
private readonly ulong _baseAddress;
12+
private readonly Stream _stream;
1313
private uint _peSigOffset;
1414
private ushort _optHeaderMagic;
1515
private IMAGE_EXPORT_DIRECTORY _exportDir;
16+
private bool _disposedValue;
1617

1718
public bool IsValid { get; init; }
1819

19-
public PEDecoder(ICLRDataTarget dataTarget, ulong baseAddress)
20+
/// <summary>
21+
/// Create PEDecoder with stream beginning at the base address of the module.
22+
/// </summary>
23+
public PEDecoder(Stream stream)
2024
{
21-
_dataTarget = dataTarget;
22-
_baseAddress = baseAddress;
25+
_stream = stream;
2326

2427
IsValid = Initialize();
2528
}
2629

2730
private bool Initialize()
2831
{
29-
using BinaryReader reader = new(new DataTargetStream(_dataTarget, _baseAddress));
32+
using BinaryReader reader = new(_stream, Encoding.UTF8, leaveOpen: true);
3033

3134
ushort dosMagic = reader.ReadUInt16();
3235
if (dosMagic != 0x5A4D) // "MZ"
@@ -71,12 +74,13 @@ private bool Initialize()
7174
return true;
7275
}
7376

74-
public TargetPointer GetSymbolAddress(string symbol)
77+
public bool TryGetRelativeSymbolAddress(string symbol, out ulong address)
7578
{
79+
address = 0;
7680
if (!IsValid)
77-
return TargetPointer.Null;
81+
return false;
7882

79-
using BinaryReader reader = new(new DataTargetStream(_dataTarget, _baseAddress));
83+
using BinaryReader reader = new(_stream, Encoding.UTF8, leaveOpen: true);
8084

8185
for (int nameIndex = 0; nameIndex < _exportDir.NumberOfNames; nameIndex++)
8286
{
@@ -99,10 +103,31 @@ public TargetPointer GetSymbolAddress(string symbol)
99103
reader.BaseStream.Seek(_exportDir.AddressOfFunctions + sizeof(uint) * ordinalForNamedExport, SeekOrigin.Begin);
100104
uint symbolRVA = reader.ReadUInt32();
101105

102-
return new TargetPointer(_baseAddress + symbolRVA);
106+
address = symbolRVA;
107+
return true;
108+
}
109+
}
110+
111+
return false;
112+
}
113+
114+
private void Dispose(bool disposing)
115+
{
116+
if (!_disposedValue)
117+
{
118+
if (disposing)
119+
{
120+
_stream.Close();
103121
}
122+
123+
_disposedValue = true;
104124
}
125+
}
105126

106-
return TargetPointer.Null;
127+
void IDisposable.Dispose()
128+
{
129+
// Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
130+
Dispose(disposing: true);
131+
GC.SuppressFinalize(this);
107132
}
108133
}

src/native/managed/cdacreader/src/Entrypoints.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,13 @@ private static unsafe int CLRDataCreateInstance(Guid* pIID, IntPtr /*ICLRDataTar
8888
}
8989

9090

91-
PEDecoder peDecoder = new(dataTarget, baseAddress);
92-
ulong contractDescriptor = peDecoder.GetSymbolAddress("DotNetRuntimeContractDescriptor");
91+
DataTargetStream dataTargetStream = new(dataTarget, baseAddress);
92+
using PEDecoder peDecoder = new(dataTargetStream);
93+
94+
if (!peDecoder.TryGetRelativeSymbolAddress("DotNetRuntimeContractDescriptor", out ulong contractDescriptor))
95+
{
96+
return -1;
97+
}
9398

9499
if (!ContractDescriptorTarget.TryCreate(contractDescriptor, (address, buffer) =>
95100
{

0 commit comments

Comments
 (0)