Microsoft MVP성태의 닷넷 이야기
.NET Framework: 1130. C# - ELEMENT_TYPE_INTERNAL 유형의 사용 예 [링크 복사], [링크+제목 복사],
조회: 7427
글쓴 사람
정성태 (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)
12738정성태7/28/20217894오류 유형: 745. Azure Active Directory - Client credential flows must have a scope value with /.default suffixed to the resource identifier (application ID URI).
12737정성태7/28/20216842오류 유형: 744. Azure Active Directory - The resource principal named api://...[client_id]... was not found in the tenant
12736정성태7/28/20217361오류 유형: 743. Active Azure Directory에서 "API permissions"의 권한 설정이 "Not granted for ..."로 나오는 문제
12735정성태7/27/20217880.NET Framework: 1081. C# - Azure AD 인증을 지원하는 데스크톱 애플리케이션 예제(Windows Forms) [2]파일 다운로드1
12734정성태7/26/202123863스크립트: 20. 특정 단어로 시작하거나/끝나는 문자열을 포함/제외하는 정규 표현식 - Look-around
12733정성태7/23/202111179.NET Framework: 1081. Self-Contained/SingleFile 유형의 .NET Core/5+ 실행 파일을 임베딩한다면? [1]파일 다운로드2
12732정성태7/23/20216467오류 유형: 742. SharePoint - The super user account utilized by the cache is not configured.
12731정성태7/23/20217685개발 환경 구성: 584. Add Internal URLs 화면에서 "Save" 버튼이 비활성화 된 경우
12730정성태7/23/20219195개발 환경 구성: 583. Visual Studio Code - Go 코드에서 입력을 받는 경우
12729정성태7/22/20218141.NET Framework: 1080. xUnit 단위 테스트에 메서드/클래스 수준의 문맥 제공 - Fixture
12728정성태7/22/20217597.NET Framework: 1079. MSTestv2 단위 테스트에 메서드/클래스/어셈블리 수준의 문맥 제공
12727정성태7/21/20218610.NET Framework: 1078. C# 단위 테스트 - MSTestv2/NUnit의 Assert.Inconclusive 사용법(?) [1]
12726정성태7/21/20218435VS.NET IDE: 169. 비주얼 스튜디오 - 단위 테스트 선택 시 MSTestv2 외의 xUnit, NUnit 사용법 [1]
12725정성태7/21/20217123오류 유형: 741. Failed to find the "go" binary in either GOROOT() or PATH
12724정성태7/21/20219828개발 환경 구성: 582. 윈도우 환경에서 Visual Studio Code + Go (Zip) 개발 환경 [1]
12723정성태7/21/20217477오류 유형: 740. SharePoint - Alternate access mappings have not been configured 경고
12722정성태7/20/20217307오류 유형: 739. MSVCR110.dll이 없어 exe 실행이 안 되는 경우
12721정성태7/20/20217926오류 유형: 738. The trust relationship between this workstation and the primary domain failed. - 세 번째 이야기
12720정성태7/19/20217279Linux: 43. .NET Core/5+ 응용 프로그램의 Ubuntu (Debian) 패키지 준비
12719정성태7/19/20216457오류 유형: 737. SharePoint 설치 시 "0x800710D8 The object identifier does not represent a valid object." 오류 발생
12718정성태7/19/20217058개발 환경 구성: 581. Windows에서 WSL로 파일 복사 시 root 소유권으로 적용되는 문제파일 다운로드1
12717정성태7/18/20217006Windows: 195. robocopy에서 파일의 ADS(Alternate Data Stream) 정보 복사를 제외하는 방법
12716정성태7/17/20217834개발 환경 구성: 580. msbuild의 Exec Task에 robocopy를 사용하는 방법파일 다운로드1
12715정성태7/17/20219486오류 유형: 736. Windows - MySQL zip 파일 버전의 "mysqld --skip-grant-tables" 실행 시 비정상 종료 [1]
12714정성태7/16/20218251오류 유형: 735. VCRUNTIME140.dll, MSVCP140.dll, VCRUNTIME140.dll, VCRUNTIME140_1.dll이 없어 exe 실행이 안 되는 경우
12713정성태7/16/20218777.NET Framework: 1077. C# - 동기 방식이면서 비동기 규약을 따르게 만드는 Task.FromResult파일 다운로드1
... 31  32  33  34  35  [36]  37  38  39  40  41  42  43  44  45  ...