-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* [third_party/ScubaDiver] Handle bootstrapper unloading in DLL entrypoint * [third_party/ScubaDiver] Migrate ClrMD methods to SnapshotService class * [MTGOSDK/Core/Remoting/Interop] Move ParseType method to TypeDump class * [MTGOSDK/Core/Reflection] Migrate ClrMD snapshot runtime * [third_party/ScubaDiver] Fix deadlock starting diver host * [MTGOSDK/Resources] Migrate Bootstrapper Move Bootstrapper class to resources namespace, removing conditional namespace imports for `MTGOSDK.Core`.
- Loading branch information
Showing
34 changed files
with
1,034 additions
and
832 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
MTGOSDK.Win32/src/API/Kernel32/ProcessShapshot/PssFreeSnapshot.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/** @file | ||
Copyright (c) 2024, Cory Bennett. All rights reserved. | ||
SPDX-License-Identifier: Apache-2.0 | ||
**/ | ||
|
||
using System; | ||
using System.Runtime.InteropServices; | ||
|
||
|
||
namespace MTGOSDK.Win32.API; | ||
|
||
public static partial class Kernel32 | ||
{ | ||
/// <summary> | ||
/// Frees a snapshot of the address space of a process. | ||
/// </summary> | ||
[DllImport("kernel32")] // SetLastError=true ? | ||
public static extern int PssFreeSnapshot( | ||
/// <summary> | ||
/// A handle to the process that contains the snapshot. | ||
/// </summary> | ||
/// <remarks> | ||
/// The handle must have the PROCESS_VM_READ, PROCESS_VM_OPERATION, | ||
/// and PROCESS_DUP_HANDLE rights. If the snapshot was captured from the | ||
/// current process, or duplicated into the current process, then pass in | ||
/// the result of <see cref="GetCurrentProcess"/>. | ||
/// </remarks> | ||
[In] IntPtr ProcessHandle, | ||
/// <summary> | ||
/// A handle to the snapshot to be freed. | ||
/// </summary> | ||
[In] IntPtr SnapshotHandle | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
/** @file | ||
Copyright (c) 2021, Xappy. | ||
Copyright (c) 2024, Cory Bennett. All rights reserved. | ||
SPDX-License-Identifier: Apache-2.0 and MIT | ||
**/ | ||
|
||
using System; | ||
using System.Reflection.Emit; | ||
|
||
using MTGOSDK.Core.Remoting.Interop.Extensions; | ||
|
||
|
||
namespace MTGOSDK.Core.Reflection.Emit; | ||
|
||
/// <summary> | ||
/// A class that converts an IntPtr to an object reference. | ||
/// </summary> | ||
public class Converter<T> | ||
{ | ||
/// <summary> | ||
/// The delegate that converts an IntPtr to an object reference. | ||
/// </summary> | ||
delegate U Void2ObjectConverter<U>(IntPtr pManagedObject); | ||
|
||
/// <summary> | ||
/// The converter instance that converts an IntPtr to an object reference. | ||
/// </summary> | ||
private static Void2ObjectConverter<T> myConverter; | ||
|
||
static Converter() | ||
{ | ||
// | ||
// The type initializer is run every time the converter is instantiated | ||
// using a different generic argument. | ||
// | ||
GenerateDynamicMethod(); | ||
} | ||
|
||
/// <summary> | ||
/// Generates a dynamic method that converts an IntPtr to an object reference. | ||
/// </summary> | ||
/// <remarks> | ||
/// The dynamic method trick is discussed originally by Alois Kraus here: | ||
/// https://social.microsoft.com/Forums/windows/en-US/06ac44b0-30d8-44a1-86a4-1716dc431c62/how-to-convert-an-intptr-to-an-object-in-c?forum=clr | ||
/// </remarks> | ||
static void GenerateDynamicMethod() | ||
{ | ||
if (myConverter == null) | ||
{ | ||
DynamicMethod method = new("ConvertPtrToObjReference", | ||
typeof(T), | ||
new Type[] { typeof(IntPtr) }, | ||
typeof(IntPtr), | ||
true); | ||
var gen = method.GetILGenerator(); | ||
// Load first argument | ||
gen.Emit(OpCodes.Ldarg_0); | ||
// Return it directly. The Clr will take care of the cast! | ||
// This construct is unverifiable so we need to plug this into an assembly | ||
// with IL Verification disabled. | ||
gen.Emit(OpCodes.Ret); | ||
myConverter = (Void2ObjectConverter<T>)method | ||
.CreateDelegate(typeof(Void2ObjectConverter<T>)); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Handles the conversion of an IntPtr to an object reference. | ||
/// </summary> | ||
/// <param name="pObj">The IntPtr to convert.</param> | ||
/// <param name="expectedMethodTable">The expected method table of the object.</param> | ||
/// <returns>The object reference.</returns> | ||
/// <exception cref="ArgumentException"> | ||
/// Thrown when the actual method table value is not as expected. | ||
/// </exception> | ||
/// <remarks> | ||
/// This methods reads the method table of the object to make sure we aren't | ||
/// mistakenly pointing at another type by now (could be caused by the GC). | ||
/// </remarks> | ||
public T ConvertFromIntPtr(IntPtr pObj, IntPtr expectedMethodTable) | ||
{ | ||
IntPtr actualMethodTable = pObj.GetMethodTable(); | ||
if (actualMethodTable != expectedMethodTable) | ||
{ | ||
throw new ArgumentException("Actual Method Table value was not as expected"); | ||
} | ||
return myConverter(pObj); | ||
} | ||
|
||
/// <summary> | ||
/// Handles the conversion of an IntPtr to an object reference. | ||
/// </summary> | ||
/// <param name="pObj">The IntPtr to convert.</param> | ||
/// <param name="expectedMethodTable">The expected method table of the object.</param> | ||
/// <returns>The object reference.</returns> | ||
/// <exception cref="ArgumentException"> | ||
/// Thrown when the actual method table value is not as expected. | ||
/// </exception> | ||
/// <remarks> | ||
/// This methods reads the method table of the object to make sure we aren't | ||
/// mistakenly pointing at another type by now (could be caused by the GC). | ||
/// </remarks> | ||
public T ConvertFromIntPtr(ulong pObj, ulong expectedMethodTable) => | ||
ConvertFromIntPtr( | ||
new IntPtr((long) pObj), | ||
new IntPtr((long) expectedMethodTable)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
/** @file | ||
Copyright (c) 2024, Cory Bennett. All rights reserved. | ||
SPDX-License-Identifier: Apache-2.0 | ||
**/ | ||
|
||
using System; | ||
|
||
|
||
namespace MTGOSDK.Core.Reflection; | ||
|
||
/// <summary> | ||
/// Wraps a generic method to be used as an event handler for subscription. | ||
/// </summary> | ||
/// <typeparam name="T">The type of the event arguments.</typeparam> | ||
/// <param name="handler">The method to be wrapped.</param> | ||
public class EventWrapper<T>(EventHandler handler) where T : EventArgs | ||
{ | ||
/// <summary> | ||
/// The handler method to be invoked when the event is raised. | ||
/// </summary> | ||
public void Handle(object sender, T args) | ||
{ | ||
handler.Invoke(sender, args); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.