Where does nativeGetUninitializedObject actually exist?

蓝咒 提交于 2019-12-01 22:37:06

问题


I was curious about some serialization stuff so I went poking around FormatterServices and found a method called nativeGetUninitializedObject that actually handles the initialization (without calling the custructor) of a given type. This method is decorated with the extern keyword and the following attribute: [MethodImpl(MethodImplOptions.InternalCall), SecurityCritical]

I'm left wondering: where does this method actually exist? What code does the CLR call to get the given type initialized (without calling the constructor)?


回答1:


This method actually exists in the native portion of the CLR. The MethodImplOptions.InternalCall signifies a call which is forwarded to the CLR native code and is implemented there.

From MSDN:

Specifies an internal call. An internal call is a call to a method that is implemented within the common language runtime itself.




回答2:


The method exists in the CLR. The JIT compiler has access to a table inside the CLR that contains the addresses of all MethodImplOptions.InternalCall functions. The section of the table that's relevant to your question looks like this in the SSCLI20 source code (clr/src/vm/ecall.cpp):

FCFuncStart(gSerializationFuncs)
    FCFuncElement("nativeGetSafeUninitializedObject", ReflectionSerialization::GetSafeUninitializedObject)
    FCFuncElement("nativeGetUninitializedObject", ReflectionSerialization::GetUninitializedObject)
FCFuncEnd()

To jit the method call, it merely looks up the function name in that table and generates a direct CALL instruction to the function address as listed in the table. Very fast, direct transition from managed code to code written in C++ inside the CLR.

The ReflectionSerialization::GetUninitializedObject() method lives inside clr/src/vm/reflectioninvocation.cpp, it's too big to post here. You can have a look-see at the downloadable SSCLI20 source code. There's a bunch of error checking, then a call to a raw Allocate() method to allocate the memory for the object. No constructor call.



来源:https://stackoverflow.com/questions/4866179/where-does-nativegetuninitializedobject-actually-exist

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!