Skip to content
Open

7.0.0 #101

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 0 additions & 20 deletions src/Jitex/Framework/NETFramework.cs

This file was deleted.

7 changes: 5 additions & 2 deletions src/Jitex/Framework/Offsets/CEEInfoOffset.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ namespace Jitex.Framework.Offsets
internal static class CEEInfoOffset
{
public static int ResolveToken { get; private set; }

public static int ConstructStringLiteral { get; private set; }
public static int GetEHInfo { get; private set; }

static CEEInfoOffset()
{
Expand All @@ -18,10 +18,13 @@ private static void ReadOffset(bool isCore, Version version)
{
if (isCore && version >= new Version(8, 0, 0))
{
GetEHInfo = 0xB;
ResolveToken = 0x1C;
ConstructStringLiteral = 0x91;
}else if (isCore && version >= new Version(7, 0, 0))
}
else if (isCore && version >= new Version(7, 0, 0))
{
GetEHInfo = 0xB;
ResolveToken = 0x1D;
ConstructStringLiteral = 0x95;
}
Expand Down
19 changes: 9 additions & 10 deletions src/Jitex/Framework/RuntimeFramework.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using Jitex.JIT.CorInfo;

Expand Down Expand Up @@ -70,7 +71,7 @@ protected RuntimeFramework(bool isCore)
IsCore = isCore;
Jit = GetJitAddress();
ICorJitCompileVTable = Marshal.ReadIntPtr(Jit);
IntPtr compileMethodPtr = Marshal.ReadIntPtr(ICorJitCompileVTable);
var compileMethodPtr = Marshal.ReadIntPtr(ICorJitCompileVTable);
CompileMethod = Marshal.GetDelegateForFunctionPointer<CompileMethodDelegate>(compileMethodPtr);
IdentifyFrameworkVersion();
}
Expand All @@ -84,11 +85,9 @@ private static RuntimeFramework GetFramework()
if (_framework != null)
return _framework;

string frameworkRunning = RuntimeInformation.FrameworkDescription;
var frameworkRunning = RuntimeInformation.FrameworkDescription;

if (frameworkRunning.StartsWith(".NET Framework"))
_framework = new NETFramework();
else if (frameworkRunning.StartsWith(".NET"))
if (frameworkRunning.StartsWith(".NET"))
_framework = new NETCore();
else
throw new NotSupportedException($"Framework {frameworkRunning} is not supported!");
Expand All @@ -113,21 +112,21 @@ public void ReadICorJitInfoVTable(IntPtr iCorJitInfo)

private void IdentifyFrameworkVersion()
{
Assembly assembly = typeof(System.Runtime.GCSettings).GetTypeInfo().Assembly;
var assembly = typeof(System.Runtime.GCSettings).GetTypeInfo().Assembly;
string[] assemblyPath = assembly.CodeBase.Split(new[] { '/', '\\' }, StringSplitOptions.RemoveEmptyEntries);

string frameworkName = IsCore ? "Microsoft.NETCore.App" : "Framework64";
var frameworkName = IsCore ? "Microsoft.NETCore.App" : "Framework64";

int frameworkIndex = Array.IndexOf(assemblyPath, frameworkName);
var frameworkIndex = Array.IndexOf(assemblyPath, frameworkName);

if (frameworkIndex > 0 && frameworkIndex < assemblyPath.Length - 2)
{
string version = assemblyPath[frameworkIndex + 1];
var version = assemblyPath[frameworkIndex + 1];

if (!IsCore)
version = version[1..];

int[] versionsNumbers = version.Split('.').Select(int.Parse).ToArray();
var versionsNumbers = version.Split('.').Select(int.Parse).ToArray();
FrameworkVersion = new Version(versionsNumbers[0], versionsNumbers[1], versionsNumbers[2]);
}
else if (AppContext.TargetFrameworkName.StartsWith(".NETCoreApp"))
Expand Down
57 changes: 0 additions & 57 deletions src/Jitex/Hook/HookManager.cs

This file was deleted.

26 changes: 0 additions & 26 deletions src/Jitex/Hook/VTableHook.cs

This file was deleted.

15 changes: 14 additions & 1 deletion src/Jitex/Intercept/CallContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,16 @@ public class CallContext
/// </summary>
public int ParametersCount => _parameters.Length;

public Exception? Exception { get; set; }

/// <summary>
/// If context is waiting for end of call
/// </summary>
/// <remarks>
/// Is used to hold original call after call ContinueAsync.
/// </remarks>
internal bool IsWaitingForEnd { get; private set; }

/// <summary>
/// Create a new context from call (Should not be called directly).
/// It's for 32 Bits.
Expand Down Expand Up @@ -365,6 +367,17 @@ public void SetReturnValue<T>(T value, bool validateType = true)
ProceedCall = false;
}

public void SetException(Exception exception)
{

}

public void ThrowExceptionIfNecessary()
{
if(Exception != null)
throw Exception;
}

internal void ContinueWithCode()
{
if (_autoResetEvent == null)
Expand Down
5 changes: 0 additions & 5 deletions src/Jitex/Intercept/InterceptManager.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using Jitex.Exceptions;
using Jitex.JIT.Context;
using Jitex.Utils.Comparer;

namespace Jitex.Intercept
{
Expand Down
82 changes: 72 additions & 10 deletions src/Jitex/Intercept/InterceptorBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using Jitex.PE;
using System.Reflection;
Expand Down Expand Up @@ -52,6 +53,12 @@ internal class InterceptorBuilder : IDisposable
private static readonly MethodInfo GetTypeFromHandle =
GetTypeFromHandle = typeof(Type).GetMethod(nameof(Type.GetTypeFromHandle))!;

private static readonly MethodInfo SetException = typeof(CallContext)
.GetMethod(nameof(CallContext.SetException), BindingFlags.Public | BindingFlags.Instance)!;

private static readonly MethodInfo GetThrowExceptionIfNecessary = typeof(CallContext)
.GetMethod(nameof(CallContext.ThrowExceptionIfNecessary), BindingFlags.Public | BindingFlags.Instance)!;

private readonly MethodBase _method;
private readonly MethodBody _body;

Expand Down Expand Up @@ -106,13 +113,11 @@ public MethodBody InjectInterceptor(bool reuseReferences)
_image = _imageReader.LoadImage(reuseReferences);
}

IList<LocalVariableInfo> localVariables = _body.LocalVariables;
var localVariables = _body.LocalVariables;

localVariables.Add(new LocalVariableInfo(typeof(CallContext)));
byte callContextVariableIndex = (byte)(localVariables.Count - 1);
var callContextVariableIndex = CreateVariable<CallContext>(localVariables);
var callManagerVariableIndex = CreateVariable<CallManager>(localVariables);

localVariables.Add(new LocalVariableInfo(typeof(CallManager)));
byte callManagerVariableIndex = (byte)(localVariables.Count - 1);
int callContextCtorMetadataToken;

if (OSHelper.IsX86)
Expand Down Expand Up @@ -171,17 +176,22 @@ public MethodBody InjectInterceptor(bool reuseReferences)

instructions.Add(Ldloc_S, callContextVariableIndex);
instructions.Add(Callvirt, getProceedCallMetadataToken);
Instruction gotoInstruction = instructions.Add(Brfalse, 0); //if(context.ProceedCall)

//if(context.ProceedCall)
Instruction gotoReleaseTask = instructions.Add(Brfalse, 0);
var rr = _body.ReadIL();
instructions.AddRange(_body.ReadIL());
instructions.RemoveLast(); //Remove Ret instruction.

if (returnType != typeof(void))
instructions.Add(Stloc_S, returnVariableIndex);

Instruction endpointGoto = instructions.Add(Ldloc_S, callManagerVariableIndex);
// WriteExceptionHandler(localVariables, instructions, callContextVariableIndex);

//callManager.ReleaseTask();
var endpointGoto = instructions.Add(Ldloc_S, callManagerVariableIndex);
instructions.Add(Callvirt, releaseTaskMetadataToken);
gotoInstruction.Value = (endpointGoto.Offset - gotoInstruction.Offset - gotoInstruction.Size);
gotoReleaseTask.Value = (endpointGoto.Offset - gotoReleaseTask.Offset - gotoReleaseTask.Size);

WriteGetReturnValue(instructions, callContextVariableIndex, callManagerVariableIndex, returnType);

Expand All @@ -194,9 +204,54 @@ public MethodBody InjectInterceptor(bool reuseReferences)
LocalVariables = localVariables
};

var ll = body.ReadIL();
return body;
}

/// <summary>
/// Write exception handler on body.
/// </summary>
/// <remarks>
/// Inject the follow code:
/// ----
/// try {
/// #code...
/// } catch (Exception ex) {
/// context.SetException(ex);
/// }
///
/// context.ThrowExceptionIfNecessary();
/// ----
/// </remarks>
/// <param name="localVariables"></param>
/// <param name="instructions"></param>
/// <param name="callContextVariableIndex"></param>
private void WriteExceptionHandler(IList<LocalVariableInfo> localVariables, Instructions instructions,
byte callContextVariableIndex)
{
//try{
// <code>
//} ...
var exceptionVariableIndex = CreateVariable<Exception>(localVariables);
var leaveTryInstruction = instructions.Add(Leave, 0);
instructions.Add(Stloc_S, exceptionVariableIndex);

//catch (Exception ex){
// context.SetException(ex);
//}
instructions.Add(Ldloc_S, callContextVariableIndex);
instructions.Add(Ldloc_S, exceptionVariableIndex);
instructions.Add(Callvirt, SetException);
var leaveCatchInstruction = instructions.Add(Leave, 0);

//context.ThrowExceptionIfNecessary();
var endpointLeave = instructions.Add(Ldloc_S, callContextVariableIndex);
instructions.Add(Callvirt, GetThrowExceptionIfNecessary);

leaveTryInstruction.Value = (endpointLeave.Offset - leaveTryInstruction.Offset - leaveTryInstruction.Size);
leaveCatchInstruction.Value = (endpointLeave.Offset - leaveCatchInstruction.Offset - leaveCatchInstruction.Size);
}

/// <summary>
/// Write instructions to load parameters from method.
/// </summary>
Expand Down Expand Up @@ -275,8 +330,7 @@ private byte WriteInstanceParameter(Instructions instructions)

_image!.AddOrGetMemberRef(PointerBox, out int pointerBoxMetadataToken);

variables.Add(new LocalVariableInfo(returnType));
byte returnVariableIndex = (byte)(variables.Count - 1);
var returnVariableIndex = CreateVariable(variables, returnType);

instructions.Add(Ldloca_S, returnVariableIndex);

Expand Down Expand Up @@ -394,6 +448,14 @@ private void WriteTypesOnArray(Instructions instructions, IReadOnlyCollection<Ty
}
}

private byte CreateVariable<T>(IList<LocalVariableInfo> variables) => CreateVariable(variables, typeof(T));

private byte CreateVariable(IList<LocalVariableInfo> variables, Type type)
{
variables.Add(new LocalVariableInfo(type));
return (byte)(variables.Count - 1);
}

private void ValidateImageLoaded()
{
if (_image == null)
Expand Down
3 changes: 2 additions & 1 deletion src/Jitex/Internal/InternalModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
using System.Collections.Concurrent;
using System.Reflection;
using System.Reflection.Emit;
using Jitex.JIT.Context;
using Jitex.JIT.Hooks.CompileMethod;
using Jitex.JIT.Hooks.Token;
using Jitex.Utils;
using MethodInfo = System.Reflection.MethodInfo;

Expand Down
Loading