Giter Club home page Giter Club logo

Comments (6)

dudikeleti avatar dudikeleti commented on June 30, 2024 1

It seems to work for me if I do it that way,

For x86: var offset = (someVal2 & 0xFF) * 4;
For x64: var offset = (someVal2 & 0xFF) * 8;
(or just use runtime.PointerSize)

It's works for instance (methodPtr) and for static method (methodPtrAux). Still need to check for all types @mattwarren posted above

Update
I tried it in several cases including with invocation list and it works, If somebody think of special case that I need to test, please let me know.

from clrmd.

leculver avatar leculver commented on June 30, 2024

I agree there should be a method in ClrMD to do the dirty work for you. I'll add that to the todo list.

Also, your last line of code you should be able to replace with this:

ClrMethod method = clrRuntime.GetMethodByHandle(methodDescriptorPtr);
return method.Name;  // or return the method itself

As for it not working on all instances...sometimes _methodPtrAux is set and it means something slightly different (I think it's the MethodDesc itself but that's based on my fuzzy memory). If you handle that case then you should be able to handle all delegate types.

In any case I agree this should be part of ClrMD itself so I'll try to get to it next time I have a chance to work on ClrMD (probably a big effort the last week of December).

from clrmd.

mattwarren avatar mattwarren commented on June 30, 2024

As for it not working on all instances...sometimes _methodPtrAux is set and it means something slightly different (I think it's the MethodDesc itself but that's based on my fuzzy memory). If you handle that case then you should be able to handle all delegate types.

FYI this table from the CoreCLR source explains the difference usages of _methodPtr and _methodPtrAux:

    // DELEGATE KINDS TABLE
    //
    //                                  _target         _methodPtr              _methodPtrAux       _invocationList     _invocationCount
    //
    // 1- Instance closed               'this' ptr      target method           null                null                0
    // 2- Instance open non-virt        delegate        shuffle thunk           target method       null                0
    // 3- Instance open virtual         delegate        Virtual-stub dispatch   method id           null                0
    // 4- Static closed                 first arg       target method           null                null                0
    // 5- Static closed (special sig)   delegate        specialSig thunk        target method       first arg           0
    // 6- Static opened                 delegate        shuffle thunk           target method       null                0
    // 7- Secure                        delegate        call thunk              MethodDesc (frame)  target delegate     creator assembly 
    //
    // Delegate invoke arg count == target method arg count - 2, 3, 6
    // Delegate invoke arg count == 1 + target method arg count - 1, 4, 5
    //
    // 1, 4     - MulticastDelegate.ctor1 (simply assign _target and _methodPtr)
    // 5        - MulticastDelegate.ctor2 (see table, takes 3 args)
    // 2, 6     - MulticastDelegate.ctor3 (take shuffle thunk)
    // 3        - MulticastDelegate.ctor4 (take shuffle thunk, retrieve MethodDesc) ???
    //
    // 7 - Needs special handling

from clrmd.

dudikeleti avatar dudikeleti commented on June 30, 2024

Is this still supposed to work? Because at the moment I can not make it work.
GetMethodByHandle return always null.

In the dump (x86) I have this: Func<string, bool> func = M; (M is an instance method) I take the methodPtr of func and do that:

   var methodPtrWithOffset = methodPtr + 5;
   heap.ReadPointer(methodPtrWithOffset + 2, out ulong someVal1);
   heap.ReadPointer(methodPtrWithOffset + 1, out ulong someVal2);
   heap.ReadPointer(methodPtrWithOffset + (someVal1 & 0xFF) * 8 + 3, out ulong baseMethodDesc);
   var offset = (someVal2 & 0xFF) * 8;
   var handle = baseMethodDesc + offset;
   var method = runtime.GetMethodByHandle(handle);

from clrmd.

leculver avatar leculver commented on June 30, 2024

Looking back at this issue, it's a borderline case of whether I want to add something to ClrMD to answer this question directly. I try to keep ClrMD as the raw building blocks to implement what you need. In this case I think it's possible to correctly get the function from a delegate, but it looks like it's really complicated due to the implementation. When it's that fiddly to get right, that's a case where we should consider adding something to ClrMD to support it.

I'm leaving the issue open for now.

from clrmd.

leculver avatar leculver commented on June 30, 2024

I'm going to close this issue. I'm in the process of streamlining ClrMD and I don't think this will make the cut since there's example code to do this.

from clrmd.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.