Skip to content

Commit

Permalink
Enhance memory reading and COM object management with error handling …
Browse files Browse the repository at this point in the history
…and platform checks
  • Loading branch information
AllDwarf committed Jan 20, 2025
1 parent 990a79d commit 8c0ae1d
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 22 deletions.
17 changes: 12 additions & 5 deletions src/BinaryParsers/ElfBinary/Dwarf/DwarfMemoryReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -121,13 +121,20 @@ public ulong ReadLength(out bool is64bit)
/// <summary>
/// Reads the string from the current position in the stream.
/// </summary>
[HandleProcessCorruptedStateExceptions]
public string ReadString()
{
string result = Marshal.PtrToStringAnsi(pointer + Position);

Position += result.Length + 1;
return result;
try
{
string result = Marshal.PtrToStringAnsi(pointer + Position);
Position += result.Length + 1;
return result;
}
catch (Exception ex)
{
// Handle the exception as needed, for example, log it or rethrow
// For now, we will just rethrow it
throw new InvalidOperationException("Failed to read string from memory.", ex);
}
}

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion src/BinaryParsers/PEBinary/PEBinary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public static void ClearLocalSymbolDirectoriesCache()

public PE PE { get; private set; }

public Pdb Pdb => this.pdb?.Value;
public Pdb Pdb => this.pdb?.Value ?? null;

public StringBuilder PdbLoadTrace { get; set; }

Expand Down
3 changes: 3 additions & 0 deletions src/BinaryParsers/PEBinary/ProgramDatabase/MSDiaComWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;

using Dia2Lib;

Expand All @@ -23,6 +24,7 @@ private static extern int DllGetClassObject(
[In, MarshalAs(UnmanagedType.LPStruct)] Guid riid,
out IntPtr ppvObject);

[SupportedOSPlatform("windows")]
private static void CoCreateFromMsdia(Guid clsidOfServer, Guid riid, out IntPtr pvObject)
{
IntPtr pClassFactory = IntPtr.Zero;
Expand All @@ -40,6 +42,7 @@ private static void CoCreateFromMsdia(Guid clsidOfServer, Guid riid, out IntPtr
private const string IDiaDataSourceRiid = "79F1BB5F-B66E-48E5-B6A9-1545C323CA3D";
private const string DiaSourceClsid = "E6756135-1E65-4D17-8576-610761398C3C";

[SupportedOSPlatform("windows")]
public static IDiaDataSource GetDiaSource()
{
IntPtr diaSourcePtr = IntPtr.Zero;
Expand Down
47 changes: 32 additions & 15 deletions src/BinaryParsers/PEBinary/ProgramDatabase/Pdb.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using System.Text;
using System.Threading;

Expand Down Expand Up @@ -72,10 +73,7 @@ public Pdb(

public string LoadTrace
{
get
{
return $"{this.loadTrace}";
}
get => $"{this.loadTrace}";
set
{
// We make this settable to allow the tool to clear the trace. This
Expand Down Expand Up @@ -203,7 +201,7 @@ private IEnumerable<SourceFile> CreateSourceFileIteratorImpl(IDiaSymbol inObject
{
if (sourceFilesEnum != null)
{
Marshal.ReleaseComObject(sourceFilesEnum);
ReleaseComObject(sourceFilesEnum);
}
}
}
Expand Down Expand Up @@ -239,9 +237,9 @@ private T CreateDiaTable<T>() where T : class
for (int i = 0; i < enumTables.Count; i++)
{
IDiaTable table = enumTables.Item(i);
if (!(table is T result))
if (table is not T result)
{
Marshal.ReleaseComObject(table);
ReleaseComObject(table);
}
else
{
Expand All @@ -253,7 +251,7 @@ private T CreateDiaTable<T>() where T : class
{
if (enumTables != null)
{
Marshal.ReleaseComObject(enumTables);
ReleaseComObject(enumTables);
}
}

Expand Down Expand Up @@ -288,15 +286,15 @@ private HashSet<uint> GenerateWritableSegmentSet()
}
finally
{
Marshal.ReleaseComObject(segment);
ReleaseComObject(segment);
}
}
}
finally
{
if (enumSegments != null)
{
Marshal.ReleaseComObject(enumSegments);
ReleaseComObject(enumSegments);
}
}

Expand Down Expand Up @@ -341,15 +339,15 @@ private HashSet<uint> GenerateExecutableSectionContribIds()
}
finally
{
Marshal.ReleaseComObject(sectionContrib);
ReleaseComObject(sectionContrib);
}
}
}
finally
{
if (enumSectionContribs != null)
{
Marshal.ReleaseComObject(enumSectionContribs);
ReleaseComObject(enumSectionContribs);
}
}
return result;
Expand Down Expand Up @@ -397,12 +395,12 @@ public void Dispose()

if (this.session != null)
{
Marshal.ReleaseComObject(this.session);
ReleaseComObject(this.session);
}

if (this.dataSource != null)
{
Marshal.ReleaseComObject(this.dataSource);
ReleaseComObject(this.dataSource);
}
}

Expand All @@ -416,7 +414,9 @@ private void Init(string pePath, string symbolPath, string localSymbolDirectorie
try
{
PlatformSpecificHelpers.ThrowIfNotOnWindows();
#if WINDOWS
this.WindowsNativeLoadPdbFromPEUsingDia(pePath, symbolPath, localSymbolDirectories);
#endif
}
catch (PlatformNotSupportedException ex)
{
Expand Down Expand Up @@ -448,7 +448,9 @@ private void Init(string pdbPath)
try
{
PlatformSpecificHelpers.ThrowIfNotOnWindows();
#if WINDOWS
this.WindowsNativeLoadPdbUsingDia(pdbPath);
#endif
}
catch (PlatformNotSupportedException ex)
{
Expand All @@ -457,13 +459,14 @@ private void Init(string pdbPath)
}
}

[SupportedOSPlatform("windows")]
private void WindowsNativeLoadPdbFromPEUsingDia(string peOrPdbPath, string symbolPath, string localSymbolDirectories)
{
IDiaDataSource diaSource = null;
Environment.SetEnvironmentVariable("_NT_SYMBOL_PATH", "");
Environment.SetEnvironmentVariable("_NT_ALT_SYMBOL_PATH", "");

object pCallback = this.loadTrace != null ? this : (object)IntPtr.Zero;
object pCallback = this.loadTrace != null ? this : nint.Zero;

if (!string.IsNullOrEmpty(localSymbolDirectories))
{
Expand Down Expand Up @@ -501,6 +504,7 @@ private void WindowsNativeLoadPdbFromPEUsingDia(string peOrPdbPath, string symbo
this.dataSource = diaSource;
}

[SupportedOSPlatform("windows")]
private void WindowsNativeLoadPdbUsingDia(string pdbPath)
{
this.restrictReferenceAndOriginalPathAccess = false;
Expand Down Expand Up @@ -647,5 +651,18 @@ public bool RestrictSystemRootAccess()
{
return true;
}
/// <summary>
/// Releases the COM object if running on Windows.
/// </summary>
/// <param name="comObject">The COM object to release.</param>
private void ReleaseComObject(object comObject)
{
#if WINDOWS
if (comObject != null)
{
Marshal.ReleaseComObject(comObject);
}
#endif
}
}
}
2 changes: 2 additions & 0 deletions src/BinaryParsers/PEBinary/ProgramDatabase/SourceFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,9 @@ public void Dispose()
{
if (!this.disposed)
{
#if WINDOWS
Marshal.ReleaseComObject(this.sourceFile);
#endif
}

this.disposed = true;
Expand Down
6 changes: 5 additions & 1 deletion src/BinaryParsers/PEBinary/ProgramDatabase/Symbol.cs
Original file line number Diff line number Diff line change
Expand Up @@ -364,9 +364,11 @@ public void Dispose()
{
if (!this.disposed)
{
#if WINDOWS
Marshal.ReleaseComObject(this.sym);
}
#endif

}
this.disposed = true;
}

Expand Down Expand Up @@ -505,7 +507,9 @@ private IEnumerable<Symbol> CreateChildrenImpl(SymTagEnum symbolTagType, string
{
if (enumSymbols != null)
{
#if WINDOWS
Marshal.ReleaseComObject(enumSymbols);
#endif
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/Test.UnitTests.BinaryParsers/PEBinary/PEBinaryTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.Versioning;

using Dia2Lib;

Expand Down Expand Up @@ -212,6 +213,7 @@ public void PEBinary_IsDotNetNativeBootstrapExe()
}

[Fact]
[SupportedOSPlatform("windows")]
public void PEBinary_CanCreateIDiaSourceFromMsdia()
{
if (!PlatformSpecificHelpers.RunningOnWindows()) { return; }
Expand Down

0 comments on commit 8c0ae1d

Please sign in to comment.