Microsoft MVP성태의 닷넷 이야기
.NET Framework: 1130. C# - ELEMENT_TYPE_INTERNAL 유형의 사용 예 [링크 복사], [링크+제목 복사],
조회: 7389
글쓴 사람
정성태 (techsharer at outlook.com)
홈페이지
첨부 파일
 

(시리즈 글이 9개 있습니다.)
.NET Framework: 491. 닷넷 Generic 타입의 메타 데이터 토큰 값 알아내는 방법
; https://www.sysnet.pe.kr/2/0/1848

.NET Framework: 494. 값(struct) 형식의 제네릭(Generic) 타입이 박싱되는 경우의 메타데이터 토큰 값
; https://www.sysnet.pe.kr/2/0/1857

.NET Framework: 495. CorElementType의 요소 값 설명
; https://www.sysnet.pe.kr/2/0/1860

.NET Framework: 509. ELEMENT_TYPE_MODIFIER의 조합
; https://www.sysnet.pe.kr/2/0/2894

.NET Framework: 510. 제네릭(Generic) 인자에 대한 메타데이터 등록 확인
; https://www.sysnet.pe.kr/2/0/2907

.NET Framework: 844. C# - 박싱과 언박싱
; https://www.sysnet.pe.kr/2/0/11943

.NET Framework: 955.  .NET 메서드의 Signature 바이트 코드 분석
; https://www.sysnet.pe.kr/2/0/12379

.NET Framework: 1130. C# - ELEMENT_TYPE_INTERNAL 유형의 사용 예
; https://www.sysnet.pe.kr/2/0/12903

.NET Framework: 1174. C# - ELEMENT_TYPE_FNPTR 유형의 사용 예
; https://www.sysnet.pe.kr/2/0/12998




C# - ELEMENT_TYPE_INTERNAL 유형의 사용 예

제가 간간이, IL 코드 수준에서 메서드의 signature 분석을 다뤘었는데요,

.NET 메서드의 Signature 바이트 코드 분석
; https://www.sysnet.pe.kr/2/0/12379

값(struct) 형식의 제네릭(Generic) 타입이 박싱되는 경우의 메타데이터 토큰 값
; https://www.sysnet.pe.kr/2/0/1857#parse_sig

닷넷 Generic 타입의 메타 데이터 토큰 값 알아내는 방법
; https://www.sysnet.pe.kr/2/0/1848#parse_sig

이에 대한 사전 지식으로 필요한 것이 바로 CorElementType의 이해입니다.

CorElementType의 요소 값 설명
; https://www.sysnet.pe.kr/2/0/1860

개인적으로 CorElementType에 대한 이해는 아래의 글로 정리한 것이 전부인데요,

ELEMENT_TYPE_MODIFIER의 조합
; https://www.sysnet.pe.kr/2/0/2894

사실 저 글을 쓸 때도 ELEMENT_TYPE_INTERNAL은 이제는 쓰이지 않는 것인 줄 알았습니다.

typedef enum CorElementType
{
    ELEMENT_TYPE_END            = 0x00,
    ELEMENT_TYPE_VOID           = 0x01,
    ELEMENT_TYPE_BOOLEAN        = 0x02,
    ELEMENT_TYPE_CHAR           = 0x03,
    // ...[생략]...

    // This is for signatures generated internally (which will not be persisted in any way).
    ELEMENT_TYPE_INTERNAL       = 0x21,     // INTERNAL <typehandle>

    // Note that this is the max of base type excluding modifiers
    ELEMENT_TYPE_MAX            = 0x22,     // first invalid element type


    ELEMENT_TYPE_MODIFIER       = 0x40,
    ELEMENT_TYPE_SENTINEL       = 0x01 | ELEMENT_TYPE_MODIFIER, // sentinel for varargs
    ELEMENT_TYPE_PINNED         = 0x05 | ELEMENT_TYPE_MODIFIER,

} CorElementType;

왜냐하면, 간혹 보게 되는 CLI 글들이나 소스 코드 내의 주석에 deprecated 되는 기호에 대한 것들을 종종 봤기 때문에, 저는 ELEMENT_TYPE_INTERNAL도 그런 부류의 하나인 줄 알았습니다. 게다가 주석에도 나오지만 ("which will not be persisted in any way") 내부 CLR 동작 과정 중에 메모리상에만 존재할 듯 싶어 무시를 했었는데요.

그런데, 오늘 우연히 보게된 .NET 6.0의 System.Text.Json에서 사용 예를 발견했습니다.

c:\temp> ildasm /metadata=hex /bytes "C:\Program Files\dotnet\shared\Microsoft.NETCore.App\6.0.0\System.Text.Json.dll" /out=sample_il.txt

System.Text.Json.Serialization.Converters.ArrayConverter`2 타입의 Add 메서드를 보면,

protected override void Add(in TElement value, ref ReadStack state)
{
	((List<TElement>)state.Current.ReturnValue).Add(value);
}

C# signature 상으로는 별다른 것이 없는데, IL 코드 수준의 signature 구성을 보면 다음과 같습니다.

20 02 01 1f 21 10 13 01  10 11 81 04 

20: IMAGE_CEE_CS_CALLCONV_HASTHIS
02: 2개의 인자
01: 반환 타입 ELEMENT_TYPE_VOID

[첫 번째 인자의 형식]
1f: ELEMENT_TYPE_CMOD_REQD
21: ELEMENT_TYPE_INTERNAL
10: <typehandle>
13: ELEMENT_TYPE_VAR
01: 클래스 var의 첫 번째 generic 인자(TElement)

[두 번째 인자의 형식]
10: ELEMENT_TYPE_BYREF
11: ELEMENT_TYPE_VALUETYPE
81 04: <type> System.Text.Json.ReadStack

여기서 문제는, ELEMENT_TYPE_INTERNAL의 경우 그다음 명시된 값이 "typehandle"이라고 나오는데, Profiler에서 제공하는 IMetaDataImport2 인터페이스로는 typehandle 관련한 조회 함수를 전혀 제공하고 있지 않다는 점입니다.

그러고 보니, typehandle에 대한 설명을 예전 글에서 다뤘는데요,

C# 코드로 접근하는 MethodDesc, MethodTable
; https://www.sysnet.pe.kr/2/0/12142

typehandle은 결국 MethodTable의 위치를 가리키는 포인터 값입니다. 반면 메서드 signature에서 나온 값은 단지 0x10이기 때문에 아마도 저 값은 MethodTable이 등록된 특정 메타데이터 테이블의 인덱스가 아닐까 싶은데... ^^;




ildasm.exe를 이용해 덤프를 해보면,

C:\temp> ildasm /metadata=hex /bytes "C:\Program Files\dotnet\shared\Microsoft.NETCore.App\6.0.0\System.Text.Json.dll" /out=sample_il.txt

다음과 같은 출력 결과를 얻을 수 있습니다.

// 	Method #2 (060009ec) 
// 	-------------------------------------------------------
// 		MethodName: Add (060009EC)
// 		Flags     : [Family] [Virtual] [HideBySig] [ReuseSlot]  (000000c4)
// 		RVA       : 0x000f5e0c
// 		ImplFlags : [IL] [Managed]  (00000000)
// 		CallCnvntn: [DEFAULT]
// 		hasThis 
// 		ReturnType: Void
// 		2 Arguments
// 			Argument #1:  CMOD_REQD System.Runtime.InteropServices.InAttribute ByRef Var!1
// 			Argument #2:  ByRef ValueClass System.Text.Json.ReadStack
// 		Signature : 20 02 01 1f 21 10 13 01  10 11 81 04 
// 		2 Parameters
// 			(1) ParamToken : (08000b87) Name : value flags: [In]  (00000001)
// 			CustomAttribute #1 (0c0005bd)
// 			-------------------------------------------------------
// 				CustomAttribute Type: 0a000050
// 				CustomAttributeName: System.Runtime.CompilerServices.IsReadOnlyAttribute :: instance void .ctor()
// 				Length: 4
// 				Value : 01 00 00 00                                      >                <
// 				ctor args: ()
// 
// 			(2) ParamToken : (08000b88) Name : state flags: [none] (00000000)

그러니까, 아마도 typehandle == 0x10인 값은 메타데이터 내에 InAttribute 특성을 가리키고 있음을 유추할 수 있습니다. 하지만, 같은 메타데이터 내에서 InAttribute의 등록을 찾아보면 유일하게 TypeRef 영역에만 다음과 같이 기록돼 있습니다.

// TypeRef #8 (01000008)
// -------------------------------------------------------
// Token:             0x01000008
// ResolutionScope:   0x23000001
// TypeRefName:       System.Runtime.InteropServices.InAttribute
// 
...[생략]...
// TypeRef #47 (0100002f)
// -------------------------------------------------------
// Token:             0x0100002f
// ResolutionScope:   0x23000001
// TypeRefName:       System.Runtime.CompilerServices.IsReadOnlyAttribute
// 	MemberRef #1 (0a000050)
// 	-------------------------------------------------------
// 		Member: (0a000050) .ctor: 
// 		CallCnvntn: [DEFAULT]
// 		hasThis 
// 		ReturnType: Void
// 		No arguments.
// 		Signature : 20 00 01 

어허~~~ ^^; 0x10과 0x08 사이에서 어떤 연관성도 나오지 않는군요.

검색해 보면,

Read DynamicMethod's LocalSignature: non standard type tokens?
; https://stackoverflow.com/questions/10614484/read-dynamicmethods-localsignature-non-standard-type-tokens?rq=1

자문자답하고 있는 글에서도, "uncompressed data"라고 답변을 하고 있습니다. 또한, 과거의 SSCLI 소스 코드를 보면,

// https://github.com/g15ecb/shared-source-cli-2.0/blob/master/clr/src/vm/siginfo.cpp#L1587
case ELEMENT_TYPE_INTERNAL :
    {
        TypeHandle hType;
        CorSigUncompressPointer(psig.GetPtr(), (void**)&hType);
        // workaround unreachable code warning
        // RETURN(hType);
        thRet = hType;
        break;}
    }

signature에 온 값을 4(또는 8) 바이트 포인터로 해석하고, 최신의 CoreCLR에서도,

// https://github.com/dotnet/runtime/blob/main/src/coreclr/vm/jitinterface.cpp#L3095

case DeclaringTypeHandleSlot:
    _ASSERTE(pTemplateMD != NULL);
    sigBuilder.AppendElementType(ELEMENT_TYPE_INTERNAL);
    sigBuilder.AppendPointer(pTemplateMD->GetMethodTable());
    FALLTHROUGH;

MethodTable의 포인터 값을 추가하고 있습니다. 참... 이걸 어떻게 다뤄야 할지...




그나저나, 혹시 우리도 in 예약어를 사용하면 ELEMENT_TYPE_INTERNAL로 기록이 될까요? .NET 소스 코드에 따라,

// https://github.com/dotnet/runtime/blob/main/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/Collection/ArrayConverter.cs

// ...[생략]...
internal sealed class ArrayConverter<TCollection, TElement> : IEnumerableDefaultConverter<TElement[], TElement>
{
    internal override bool CanHaveIdMetadata => false;

    protected override void Add(in TElement value, ref ReadStack state)
    {
        ((List<TElement>)state.Current.ReturnValue!).Add(value);
    }

    // ...[생략]...
}

우리도 유사하게 구성해 보면,

internal class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("Hello World!");
    }

    public class Test<T1, T2>
    {
        protected void Add(in T1 value, ref Program state)
        {
            Console.WriteLine(value);
            Console.WriteLine(state);
        }
    }
}

// ildasm /metadata=hex /bytes "Console1.dll" /out=sample_il.txt

아쉽게도, 이때의 ildasm.exe 결과에는 0x21(ELEMENT_TYPE_INTERNAL) 코드가 산출되지 않습니다.

// 	Method #1 (06000007) 
// 	-------------------------------------------------------
// 		MethodName: Add (06000007)
// 		Flags     : [Family] [HideBySig] [ReuseSlot]  (00000084)
// 		RVA       : 0x000020a9
// 		ImplFlags : [IL] [Managed]  (00000000)
// 		CallCnvntn: [DEFAULT]
// 		hasThis 
// 		ReturnType: Void
// 		2 Arguments
// 			Argument #1:  ByRef Var!0
// 			Argument #2:  ByRef Class Program
// 		Signature : 20 02 01 10 13 00 10 12  14 
// 		2 Parameters
// 			(1) ParamToken : (08000002) Name : value flags: [In]  (00000001)
// 			CustomAttribute #1 (0c00000d)
// 			-------------------------------------------------------
// 				CustomAttribute Type: 0a00000d
// 				CustomAttributeName: System.Runtime.CompilerServices.IsReadOnlyAttribute :: instance void .ctor()
// 				Length: 4
// 				Value : 01 00 00 00                                      >                <
// 				ctor args: ()
// 
// 			(2) ParamToken : (08000003) Name : state flags: [none] (00000000)
// 		CustomAttribute #1 (0c000017)
// 		-------------------------------------------------------
// 			CustomAttribute Type: 06000004
// 			CustomAttributeName: System.Runtime.CompilerServices.NullableContextAttribute :: instance void .ctor(unsigned int8)
// 			Length: 5
// 			Value : 01 00 01 00 00                                   >                <
// 			ctor args: (1)

사용 예는 확인했지만, 재현은 할 수가 없군요. ^^; 보는 바와 같이 C#의 경우에는 In 특성이 메서드 signature 레벨이 아닌, Parameter 정보에 CorParamAttr::pdIn 속성으로 등록돼 있습니다.

파면 팔수록 모르는 것 투성이인 것을 보면, 저도 아직 한참 멀은 것 같습니다. ^^;




[이 글에 대해서 여러분들과 의견을 공유하고 싶습니다. 틀리거나 미흡한 부분 또는 의문 사항이 있으시면 언제든 댓글 남겨주십시오.]







[최초 등록일: ]
[최종 수정일: 3/4/2024]

Creative Commons License
이 저작물은 크리에이티브 커먼즈 코리아 저작자표시-비영리-변경금지 2.0 대한민국 라이센스에 따라 이용하실 수 있습니다.
by SeongTae Jeong, mailto:techsharer at outlook.com

비밀번호

댓글 작성자
 




... 31  32  33  34  35  36  [37]  38  39  40  41  42  43  44  45  ...
NoWriterDateCnt.TitleFile(s)
12711정성태7/15/20218528개발 환경 구성: 578. Azure - Java Web App Service를 위한 Site Extension 제작 방법
12710정성태7/15/202110327개발 환경 구성: 577. MQTT - emqx.io 서비스 소개
12709정성태7/14/20216912Linux: 42. 실행 중인 docker 컨테이너에 대한 구동 시점의 docker run 명령어를 확인하는 방법
12708정성태7/14/202110315Linux: 41. 리눅스 환경에서 디스크 용량 부족 시 원인 분석 방법
12707정성태7/14/202177583오류 유형: 734. MySQL - Authentication method 'caching_sha2_password' not supported by any of the available plugins.
12706정성태7/14/20218746.NET Framework: 1076. C# - AsyncLocal 기능을 CallContext만으로 구현하는 방법 [2]파일 다운로드1
12705정성태7/13/20218919VS.NET IDE: 168. x64 DLL 프로젝트의 컨트롤이 Visual Studio의 Designer에서 보이지 않는 문제 - 두 번째 이야기
12704정성태7/12/20218056개발 환경 구성: 576. Azure VM의 서비스를 Azure Web App Service에서만 접근하도록 NSG 설정을 제한하는 방법
12703정성태7/11/202113700개발 환경 구성: 575. Azure VM에 (ICMP) ping을 허용하는 방법
12702정성태7/11/20218853오류 유형: 733. TaskScheduler에 등록된 wacs.exe의 Let's Encrypt 인증서 업데이트 문제
12701정성태7/9/20218490.NET Framework: 1075. C# - ThreadPool의 스레드는 반환 시 ThreadStatic과 AsyncLocal 값이 초기화 될까요?파일 다운로드1
12700정성태7/8/20218879.NET Framework: 1074. RuntimeType의 메모리 누수? [1]
12699정성태7/8/20217688VS.NET IDE: 167. Visual Studio 디버깅 중 GC Heap 상태를 보여주는 "Show Diagnostic Tools" 메뉴 사용법
12698정성태7/7/202111652오류 유형: 732. Windows 11 업데이트 시 3% 또는 0%에서 다운로드가 멈춘 경우
12697정성태7/7/20217546개발 환경 구성: 574. Windows 11 (Insider Preview) 설치하는 방법
12696정성태7/6/20218143VC++: 146. 운영체제의 스레드 문맥 교환(Context Switch)을 유사하게 구현하는 방법파일 다운로드2
12695정성태7/3/20218179VC++: 145. C 언어의 setjmp/longjmp 기능을 Thread Context를 이용해 유사하게 구현하는 방법파일 다운로드1
12694정성태7/2/202110139Java: 24. Azure - Spring Boot 앱을 Java SE(Embedded Web Server)로 호스팅 시 로그 파일 남기는 방법 [1]
12693정성태6/30/20217875오류 유형: 731. Azure Web App Site Extension - Failed to install web app extension [...]. {1}
12692정성태6/30/20217767디버깅 기술: 180. Azure - Web App의 비정상 종료 시 남겨지는 로그 확인
12691정성태6/30/20218597개발 환경 구성: 573. 테스트 용도이지만 테스트에 적합하지 않은 Azure D1 공유(shared) 요금제
12690정성태6/28/20219409Java: 23. Azure - 자바(Java)로 만드는 Web App Service - Tomcat 호스팅
12689정성태6/25/20219978오류 유형: 730. Windows Forms 디자이너 - The class Form1 can be designed, but is not the first class in the file. [1]
12688정성태6/24/20219640.NET Framework: 1073. C# - JSON 역/직렬화 시 리플렉션 손실을 없애는 JsonSrcGen [2]파일 다운로드1
12687정성태6/22/20217612오류 유형: 729. Invalid data: Invalid artifact, java se app service only supports .jar artifact
12686정성태6/21/202110064Java: 22. Azure - 자바(Java)로 만드는 Web App Service - Java SE (Embedded Web Server) 호스팅
... 31  32  33  34  35  36  [37]  38  39  40  41  42  43  44  45  ...