John Lam 1997 — 2026

What's a method?

This morning, I decided to investigate how the CLR treats methods. First, here’s some background information; there are at least three different pieces of information that describe a method in the CLR:

  • MethodDef tokens describe the definition of a method within the scope of the module that it was defined. MethodDef tokens are always scoped by the module in which it was defined. In reality a MethodDef token is simply an the row number of the corresponding entry into the MethodDef table of a module. MethodDef tokens are static data; they are generated at compile time.
  • FunctionID’s are provided by the CLR to profilers. The profiler API documentation describes it as “an opaque 32-bit handle”. They are used to identify a function that is being called. For example, the ICorProfilerCallback::JITCompilationStarted() callback passes a FunctionID as an in-parameter. FunctionID’s are global in scope - that is they are not tied to a specific module.[1] FunctionID’s are dynamic data; they are generated at runtime by the CLR.
  • RuntimeMethodHandles are managed structures that are used by the System.Reflection API’s to locate a method. Typically you pass a RuntimeMethodHandle to the static MethodInfo.GetMethodFromHandle() method to retrieve a MethodInfo object that corresponds to the runtime method. RuntimeMethodHandles are also dynamic data generated by the CLR.

There are a number of different ways that you can convert between these values. Some are available if you’re using the profiling API’s, others are available to you if you’re writing IL.

The profiling API’s provide some helper methods that let you map a FunctionID to a MethodDef token and a MethodDef token to a FunctionID. These helper methods are ICorProfilerInfo::GetFunctionInfo(), which returns a ModuleID and MethodDef token given a FunctionID, and ICorProfilerInfo::GetFunctionFromToken(), which returns you a FunctionID given a ModuleID and a MethodDef token.

IL lets you retrieve a RuntimeMethodHandle object reference for a MethodDef token within the currently executing module, using the ldtoken instruction. This instruction takes a MethodDef token on the call stack and replaces it with a reference to a RuntimeMethodHandle value type.

With that background out of the way, let’s get to the point of this entry: Is there a difference between FunctionID’s and RuntimeMethodHandles? In the old days, I would figure this out by sending some email to friends at Microsoft who knew how such things behaved. But nowadays there’s a much better (and generally faster[2]) way to figure these things out: use the Rotor sources.

Jason Whittington’s Introduction to Rotor article in MSDN helped greatly in getting thins setup and making sense of the Rotor build environment. Once I had a debug build up and running, I started spelunking around inside the SSCLI using the VS.NET debugger. It didn’t take much time to figure out that FunctionID’s mapped to MethodDesc structs in the CLR. Next, there was a comment in RuntimeMethodHandle.cs that said: “This corresponds to EE MethodDesc.”. Hmmm … it doesn’t take a rocket scientist to make the connection, huh?

Just to make sure, though, I also compiled and built a couple of test rigs that would find out for sure. In one case, I used the Reflection API’s to scrape out the RuntimeMethodHandle method for a the static Main() method in a console application:

`namespace rmh { class Class1 { [STAThread] public static void Main(string[] args) { // Get a handle to this method

Type class1 = Type.GetType( “rmh.Class1” ); MethodInfo main = class1.GetMethod( “Main”, BindingFlags.Static | BindingFlags.Public ); RuntimeMethodHandle handle = main.MethodHandle;

  Console.WriteLine( "Handle == {0}", handle.Value );
}

} }`

Then I made a special build of CLAW that dumped out the FunctionID’s to the debug console window. The code in question is:

STDMETHODIMP JITCompilationStarted( FunctionID functionId, BOOL fIsSafeToBlock ) { ATLTRACE( "JIT started on function id = %p", functionId ); return S_OK; }

As it turns out, the method handles match up perfectly, thus demonstrating (at least in this case) that a RuntimeMethodHandle’s m_ptr field == FunctionID == MethodDesc*.[3]

[1] I’m not currently sure about what the scope of FunctionID’s are. They are likely scoped on a per-managed process basis. But I need to do some more experiments to find out for sure.

[2] The email culture at Microsoft is one of getting hundreds of emails a day. If I can help reduce this load, I will, so that I can save up some karma points for help with really difficult questions ;)

[3] Yes, I know I really should have written more code to dump out the method name using the unmanaged metadata API’s, but I’ll leave that as an exercise for the reader.

1 archived reader comment

Anonymous (2003-03-04)

crap