Git Product home page Git Product logo

methodredirect's Introduction

MethodRedirect

MethodRedirect is a MethodInfo extension written in C# that can be used to redirect a method call to another using reflection.

This implementation uses marshalling to modify the address of the corresponding Method Descriptor without the need to use unsafe block.

This project was inspired by one of the answers given for this question on StackOverflow about replacing the content of a C# method. The answer did not provide sufficient explanation on how it actually works and neither shows an example of its usage.The following are development notes and references used to implement this project and hopefully explain how it works too.

From the CLR documentation:
  • Each MethodDesc has a slot, which contains the entry point of the method. The slot and entry point must exist for all methods, even the ones that never run like abstract methods.

  • The slot is either in MethodTable or in MethodDesc itself. The location of the slot is determined by mdcHasNonVtableSlot bit on MethodDesc.

  • The slot is stored in MethodTable for methods that require efficient lookup via slot index, e.g. virtual methods or methods on generic types. The MethodDesc contains the slot index to allow fast lookup of the entry point in this case.

  • Each class and interface will be represented in memory by a MethodTable (MT) data structure. A pointer to the MethodTable can be acquired through the Type.RuntimeTypeHandle property. The TypeHandle (contained in the ObjectInstance) points to an offset from the beginning of the MethodTable. This offset is 12 bytes by default.

    MethodTable

    MethodTable Layout

    Embedded within the MethodTable is a table of slots that point to the respective method descriptors (MethodDesc), enabling the behavior of the type. The Method Slot Table is created based on the linearized list of implementation methods laid out in the following order:

    1. Inherited virtuals
    2. Introduced virtuals
    3. Instance methods
    4. Static methods
    

    The first four methods of any type will always be ToString(), Equals(), GetHashCode(), and Finalize() in this order. On x86, each method's address is represented on 4 bytes. On x64 build, each method's address is represented on 8 bytes.

    • On x86 build, the fixed-size portion of MethodTable is 40 bytes.
    • On x64 build, the fixed-size portion of MethodTable is 64 bytes.

    The object constructor (.ctor) is automatically generated by the C# compiler for all objects having no constructor explicitly defined.

    The class constructor (.cctor) is generated by the C# compiler when at least one static variable is defined.

    MethodTable Layout Example

    MethodTable Layout Example

    MethodTable Layout

    Method Descriptor

    Method Descriptor

    • Method Descriptor (MethodDesc) is an encapsulation of method implementation as the CLR knows it.

    • Each MethodDesc is padded with a PreJitStub, which is responsible for triggering JIT compilation.

    • The method table slot entry actually points to the stub instead of the actual MethodDesc data structure. This is at a negative offset of 5 bytes from the actual MethodDesc and is part of the 8-byte padding every method inherits.

    • MethodDesc is always 5 bytes after the location pointed by the Method Slot Table entry. After the compilation is complete, the 5 bytes containing the call instruction will be overwritten with an unconditional jump to the JIT-compiled code.

    • The Flags field in the method descriptor is encoded to contain the information about the type of the method, such as static, instance, interface method, or COM implementation. The Flags field is represented on 3-bit [0-7] and can be one of the MethodClassification enumeration value.

      [UPDATE] The method descriptor structure shows a Slot Number field of 2 bytes, which correspond to a maximum of 255 per type. This must be incorrect as this number can be much larger. A reasonable assumption is that the field must take 3 bytes by using the first byte of the following Flags field.

References


MIT License

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

methodredirect's People

Watchers

James Cloos avatar

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.