Microsoft MVP성태의 닷넷 이야기
.NET Framework: 1130. C# - ELEMENT_TYPE_INTERNAL 유형의 사용 예 [링크 복사], [링크+제목 복사]
조회: 7146
글쓴 사람
정성태 (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

비밀번호

댓글 작성자
 




1  2  3  4  5  6  7  8  9  10  11  [12]  13  14  15  ...
NoWriterDateCnt.TitleFile(s)
13321정성태4/14/20233734개발 환경 구성: 676. WSL/Linux Octave - Python 스크립트 연동
13320정성태4/13/20233739개발 환경 구성: 675. Windows Octave 8.1.0 - Python 스크립트 연동
13319정성태4/12/20234181개발 환경 구성: 674. WSL 2 환경에서 GNU Octave 설치
13318정성태4/11/20233989개발 환경 구성: 673. JetBrains IDE에서 "Squash Commits..." 메뉴가 비활성화된 경우
13317정성태4/11/20234145오류 유형: 855. WSL 2 Ubuntu 20.04 - error: cannot communicate with server: Post http://localhost/v2/snaps/...
13316정성태4/10/20233473오류 유형: 854. docker-compose 시 "json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)" 오류 발생
13315정성태4/10/20233667Windows: 245. Win32 - 시간 만료를 갖는 컨텍스트 메뉴와 윈도우 메시지의 영역별 정의파일 다운로드1
13314정성태4/9/20233743개발 환경 구성: 672. DosBox를 이용한 Turbo C, Windows 3.1 설치
13313정성태4/9/20233835개발 환경 구성: 671. Hyper-V VM에 Turbo C 2.0 설치 [2]
13312정성태4/8/20233822Windows: 244. Win32 - 시간 만료를 갖는 MessageBox 대화창 구현 (개선된 버전)파일 다운로드1
13311정성태4/7/20234316C/C++: 163. Visual Studio 2022 - DirectShow 예제 컴파일(WAV Dest)
13310정성태4/6/20233879C/C++: 162. Visual Studio - /NODEFAULTLIB 옵션 설정 후 수동으로 추가해야 할 library
13309정성태4/5/20234034.NET Framework: 2107. .NET 6+ FileStream의 구조 변화
13308정성태4/4/20233924스크립트: 47. 파이썬의 time.time() 실숫값을 GoLang / C#에서 사용하는 방법
13307정성태4/4/20233720.NET Framework: 2106. C# - .NET Core/5+ 환경의 Windows Forms 응용 프로그램에서 HINSTANCE 구하는 방법
13306정성태4/3/20233567Windows: 243. Win32 - 윈도우(cbWndExtra) 및 윈도우 클래스(cbClsExtra) 저장소 사용 방법
13305정성태4/1/20233907Windows: 242. Win32 - 시간 만료를 갖는 MessageBox 대화창 구현 (쉬운 버전)파일 다운로드1
13304정성태3/31/20234263VS.NET IDE: 181. Visual Studio - C/C++ 프로젝트에 application manifest 적용하는 방법
13303정성태3/30/20233564Windows: 241. 환경 변수 %PATH%에 DLL을 찾는 규칙
13302정성태3/30/20234181Windows: 240. RDP 환경에서 바뀌는 %TEMP% 디렉터리 경로
13301정성태3/29/20234319Windows: 239. C/C++ - Windows 10 Version 1607부터 지원하는 /DEPENDENTLOADFLAG 옵션파일 다운로드1
13300정성태3/28/20233950Windows: 238. Win32 - Modal UI 창에 올바른 Owner(HWND)를 설정해야 하는 이유
13299정성태3/27/20233736Windows: 237. Win32 - 모든 메시지 루프를 탈출하는 WM_QUIT 메시지
13298정성태3/27/20233675Windows: 236. Win32 - MessageBeep 소리가 안 들린다면?
13297정성태3/26/20234348Windows: 235. Win32 - Code Modal과 UI Modal
13296정성태3/25/20233691Windows: 234. IsDialogMessage와 협업하는 WM_GETDLGCODE Win32 메시지 [1]파일 다운로드1
1  2  3  4  5  6  7  8  9  10  11  [12]  13  14  15  ...