-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
321 additions
and
30 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
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
80 changes: 80 additions & 0 deletions
80
src/Platforms/Echo.Platforms.AsmResolver/Emulation/Invocation/AllocationResult.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,80 @@ | ||
using System.Diagnostics; | ||
using System.Diagnostics.CodeAnalysis; | ||
using Echo.Memory; | ||
|
||
namespace Echo.Platforms.AsmResolver.Emulation.Invocation; | ||
|
||
/// <summary> | ||
/// Describes the result of an allocation of an object. | ||
/// </summary> | ||
[DebuggerDisplay("{Tag,nq}({DebuggerDisplay})")] | ||
public readonly struct AllocationResult | ||
{ | ||
private AllocationResult(AllocationResultType resultType, BitVector? address, ObjectHandle exceptionObject) | ||
{ | ||
ResultType = resultType; | ||
Address = address; | ||
ExceptionObject = exceptionObject; | ||
} | ||
|
||
/// <summary> | ||
/// Gets the type of result this object contains. | ||
/// </summary> | ||
public AllocationResultType ResultType { get; } | ||
|
||
/// <summary> | ||
/// Gets a value indicating whether the invocation was inconclusive and not handled yet. | ||
/// </summary> | ||
public bool IsInconclusive => ResultType is AllocationResultType.Inconclusive; | ||
|
||
/// <summary> | ||
/// Determines whether the invocation was successful. | ||
/// </summary> | ||
[MemberNotNullWhen(true, nameof(Address))] | ||
public bool IsSuccess => ResultType is AllocationResultType.Allocated or AllocationResultType.FullyConstructed; | ||
|
||
/// <summary> | ||
/// When <see cref="ResultType"/> is <see cref="InvocationResultType.StepOver"/>, gets the address of the object | ||
/// that was allocated or constructed. | ||
/// </summary> | ||
public BitVector? Address { get; } | ||
|
||
/// <summary> | ||
/// When <see cref="ResultType"/> is <see cref="InvocationResultType.Exception"/>, gets the handle to the | ||
/// exception object that was thrown. | ||
/// </summary> | ||
public ObjectHandle ExceptionObject { get; } | ||
|
||
[DebuggerBrowsable(DebuggerBrowsableState.Never)] | ||
internal string Tag => ResultType.ToString(); | ||
|
||
[DebuggerBrowsable(DebuggerBrowsableState.Never)] | ||
internal object? DebuggerDisplay => IsSuccess ? Address : ExceptionObject; | ||
|
||
/// <summary> | ||
/// Constructs a new inconclusive allocation result, where the allocation was not handled yet. | ||
/// </summary> | ||
public static AllocationResult Inconclusive() => new(AllocationResultType.Inconclusive, null, default); | ||
|
||
/// <summary> | ||
/// Constructs a new conclusive allocation result, where the object was successfully allocated but not yet | ||
/// initialized yet. | ||
/// </summary> | ||
/// <param name="address">The address of the allocated object.</param> | ||
public static AllocationResult Allocated(BitVector address) => new(AllocationResultType.Allocated, address, default); | ||
|
||
/// <summary> | ||
/// Constructs a new conclusive allocation result, where the object was successfully allocated and is also fully | ||
/// initialized by a constructor. | ||
/// </summary> | ||
/// <param name="address">The address of the allocated object.</param> | ||
public static AllocationResult FullyConstructed(BitVector address) => new(AllocationResultType.FullyConstructed, address, default); | ||
|
||
/// <summary> | ||
/// Constructs a new failed allocation result with the provided pointer to an exception object describing the | ||
/// error that occurred. | ||
/// </summary> | ||
/// <param name="exceptionObject">The handle to the exception object that was thrown.</param> | ||
public static AllocationResult Exception(ObjectHandle exceptionObject) => | ||
new(AllocationResultType.Exception, null, exceptionObject); | ||
} |
28 changes: 28 additions & 0 deletions
28
src/Platforms/Echo.Platforms.AsmResolver/Emulation/Invocation/AllocationResultType.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,28 @@ | ||
namespace Echo.Platforms.AsmResolver.Emulation.Invocation; | ||
|
||
/// <summary> | ||
/// Provides members describing the different types of allocation results that can be produced during an object | ||
/// allocation in a CIL virtual machine. | ||
/// </summary> | ||
public enum AllocationResultType | ||
{ | ||
/// <summary> | ||
/// Indicates the allocation was not handled yet. | ||
/// </summary> | ||
Inconclusive, | ||
|
||
/// <summary> | ||
/// Indicates the object was allocated but not initialized yet and a constructor should be called. | ||
/// </summary> | ||
Allocated, | ||
|
||
/// <summary> | ||
/// Indicates the object was allocated and also initialized. | ||
/// </summary> | ||
FullyConstructed, | ||
|
||
/// <summary> | ||
/// Indicates the allocation failed with an exception. | ||
/// </summary> | ||
Exception | ||
} |
37 changes: 37 additions & 0 deletions
37
src/Platforms/Echo.Platforms.AsmResolver/Emulation/Invocation/DefaultAllocators.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,37 @@ | ||
namespace Echo.Platforms.AsmResolver.Emulation.Invocation; | ||
|
||
/// <summary> | ||
/// Provides methods for constructing object allocators using a set of default allocators implementations. | ||
/// </summary> | ||
public static class DefaultAllocators | ||
{ | ||
/// <summary> | ||
/// Gets an object allocator that returns unknown addresses. | ||
/// </summary> | ||
public static UnknownAddressAllocator UnknownAddress => UnknownAddressAllocator.Instance; | ||
|
||
/// <summary> | ||
/// Gets an allocator that allocates objects in the virtualalized heap of the underlying virtual machine. | ||
/// </summary> | ||
public static VirtualHeapAllocator VirtualHeap => VirtualHeapAllocator.Instance; | ||
|
||
/// <summary> | ||
/// Chains the first object allocator with the provided object allocator in such a way that if the result of the | ||
/// first allocator is inconclusive, the second allocator will be used as a fallback allocator. | ||
/// </summary> | ||
/// <param name="self">The first object allocator</param> | ||
/// <param name="other">The fallback object allocator</param> | ||
/// <returns>The constructed allocator chain.</returns> | ||
public static ObjectAllocatorChain WithFallback(this IObjectAllocator self, IObjectAllocator other) | ||
{ | ||
if (self is not ObjectAllocatorChain chain) | ||
{ | ||
chain = new ObjectAllocatorChain(); | ||
chain.Allocators.Add(self); | ||
} | ||
|
||
chain.Allocators.Add(other); | ||
return chain; | ||
} | ||
|
||
} |
21 changes: 21 additions & 0 deletions
21
src/Platforms/Echo.Platforms.AsmResolver/Emulation/Invocation/IObjectAllocator.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,21 @@ | ||
using System.Collections.Generic; | ||
using AsmResolver.DotNet; | ||
using Echo.Memory; | ||
using Echo.Platforms.AsmResolver.Emulation.Dispatch; | ||
|
||
namespace Echo.Platforms.AsmResolver.Emulation.Invocation; | ||
|
||
/// <summary> | ||
/// Provides members for emulating object allocation. | ||
/// </summary> | ||
public interface IObjectAllocator | ||
{ | ||
/// <summary> | ||
/// Allocates a new object with the provided constructor and arguments. | ||
/// </summary> | ||
/// <param name="context">The execution context the call originates from.</param> | ||
/// <param name="ctor">The constructor to invoke after allocation.</param> | ||
/// <param name="arguments">The arguments to invoke the constructor with.</param> | ||
/// <returns>The result</returns> | ||
AllocationResult Allocate(CilExecutionContext context, IMethodDescriptor ctor, IList<BitVector> arguments); | ||
} |
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.