Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

can not catch Assembly.GetCallingAssembly().GetManifestResourceStream() #78

Closed
gitlsl opened this issue Feb 21, 2022 · 4 comments
Closed
Assignees
Labels
bug Something isn't working

Comments

@gitlsl
Copy link

gitlsl commented Feb 21, 2022

using Jitex;

JitexManager.AddMethodResolver(context =>
{
    
    Console.WriteLine(context.Method.Name);
    if (context.Method.Name.Contains("main", StringComparison.OrdinalIgnoreCase))
    {
        Console.WriteLine();
    }
});


var manifestResourceStream = Assembly.GetCallingAssembly().GetManifestResourceStream("xxxx");

I use latest 6.4.1 .net core is 6.0
I want to Interceptor GetManifestResourceStream return my Stream
but result is

get_IsEnabled
get_IsEnabled
.cctor
Initialize
LoadRuntimeImplFromPath
op_Equality

are there something I got miss? waiting for your help

@Hitmasu
Copy link
Owner

Hitmasu commented Feb 21, 2022

That's because GetManifestResourceStream is a R2R method and called before even Jitex load.

Normally, that can be resolved in this way:

MethodHelper.DisableR2R(method);
MethodHelper.ForceRecompile(method);

However, ForceRecompile did not worked for this method...

I'll open a bug and try to fix it.

@Hitmasu Hitmasu self-assigned this Feb 21, 2022
@Hitmasu Hitmasu added the bug Something isn't working label Feb 21, 2022
@Hitmasu
Copy link
Owner

Hitmasu commented Feb 24, 2022

"Fixed" on version 6.4.2.

However, GetManifestResourceStream is used internally on Jitex and as i said before, GetManifestResourceStream is a ReadyToRun method and called before Jitex load. To intercept him, you need prepare InterceptCall (intercepting any method before GetManifestResourceStream), disable ReadyToRun and force recompile on next call.

Below, you can see a example how to intercept, get and change parameter value:

//Get RuntimeAssembly.GetManifestResourceStream(string)
MethodInfo method = Type.GetType("System.Reflection.RuntimeAssembly")
    .GetMethods(BindingFlags.Public | BindingFlags.Instance).Where(w => w.Name == "GetManifestResourceStream").ElementAt(1);

JitexManager.MethodResolver += context =>
{
    if (context.Method == method || context.Method.Name.Contains(nameof(MethodStubIntercept)))
        context.InterceptCall();
};

JitexManager.Interceptor += async context =>
{
    //Just ignore our MethodStubIntercept
    if (context.Method.Name.Contains(nameof(MethodStubIntercept)))
        return;

    string resourceName = context.GetParameterValue<string>(0);
    Console.WriteLine("Resource name: " + resourceName); //show parameter value
    context.SetParameterValue(0, "app.manifest"); //Change parameter value from "---" to "app.manifest"
};

//Just to prepare InterceptCall
MethodStubIntercept();

MethodHelper.DisableReadyToRun(method); //Disable ReadyToRun on method
MethodHelper.ForceRecompile(method); //Force recompile

Assembly.GetExecutingAssembly().GetManifestResourceStream("----"); //returns GetManifestResourceStream("app.manifest") 

[MethodImpl(MethodImplOptions.NoInlining)]
static void MethodStubIntercept() { }

In most of cases, you don't need to prepare InterceptCall, however in this case, you need to do that.

Let me know if works for you.

@Hitmasu
Copy link
Owner

Hitmasu commented Feb 24, 2022

Just a adjusment on my response.

If you don't need intercept call, you can do just this:

//Get RuntimeAssembly.GetManifestResourceStream(string)
MethodInfo method = Type.GetType("System.Reflection.RuntimeAssembly")
    .GetMethods(BindingFlags.Public | BindingFlags.Instance).Where(w => w.Name == "GetManifestResourceStream").ElementAt(1);

JitexManager.MethodResolver += context =>
{
    Console.WriteLine(context.Method.Name);
};

MethodStubIntercept();

MethodHelper.DisableReadyToRun(method); //Disable ReadyToRun on method
MethodHelper.ForceRecompile(method); //Force recompile

Assembly.GetExecutingAssembly().GetManifestResourceStream("----");

[MethodImpl(MethodImplOptions.NoInlining)]
static void MethodStubIntercept() { }

@gitlsl
Copy link
Author

gitlsl commented Feb 25, 2022

It work fine now, thank you, We can do more something magical

@gitlsl gitlsl closed this as completed Feb 25, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants