Microsoft MVP성태의 닷넷 이야기
.NET Framework: 1130. C# - ELEMENT_TYPE_INTERNAL 유형의 사용 예 [링크 복사], [링크+제목 복사]
조회: 7147
글쓴 사람
정성태 (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)
13501정성태12/25/20232087개발 환경 구성: 700. WSL + uwsgi - IPv6로 바인딩하는 방법
13500정성태12/24/20232178디버깅 기술: 194. Windbg - x64 가상 주소를 물리 주소로 변환
13498정성태12/23/20232822닷넷: 2186. 한국투자증권 KIS Developers OpenAPI의 C# 래퍼 버전 - eFriendOpenAPI NuGet 패키지
13497정성태12/22/20232294오류 유형: 885. Visual Studiio - error : Could not connect to the remote system. Please verify your connection settings, and that your machine is on the network and reachable.
13496정성태12/21/20232307Linux: 66. 리눅스 - 실행 중인 프로세스 내부의 환경변수 설정을 구하는 방법 (gdb)
13495정성태12/20/20232318Linux: 65. clang++로 공유 라이브러리의 -static 옵션 빌드가 가능할까요?
13494정성태12/20/20232500Linux: 64. Linux 응용 프로그램의 (C++) so 의존성 줄이기(ReleaseMinDependency) - 두 번째 이야기
13493정성태12/19/20232548닷넷: 2185. C# - object를 QueryString으로 직렬화하는 방법
13492정성태12/19/20232262개발 환경 구성: 699. WSL에 nopCommerce 예제 구성
13491정성태12/19/20232232Linux: 63. 리눅스 - 다중 그룹 또는 사용자를 리소스에 권한 부여
13490정성태12/19/20232345개발 환경 구성: 698. Golang - GLIBC 의존을 없애는 정적 빌드 방법
13489정성태12/19/20232133개발 환경 구성: 697. GoLand에서 ldflags 지정 방법
13488정성태12/18/20232067오류 유형: 884. HTTP 500.0 - 명령행에서 실행한 ASP.NET Core 응용 프로그램을 실행하는 방법
13487정성태12/16/20232381개발 환경 구성: 696. C# - 리눅스용 AOT 빌드를 docker에서 수행 [1]
13486정성태12/15/20232195개발 환경 구성: 695. Nuget config 파일에 값 설정/삭제 방법
13485정성태12/15/20232088오류 유형: 883. dotnet build/restore - error : Root element is missing
13484정성태12/14/20232161개발 환경 구성: 694. Windows 디렉터리 경로를 WSL의 /mnt 포맷으로 구하는 방법
13483정성태12/14/20232298닷넷: 2184. C# - 하나의 resource 파일을 여러 프로그램에서 (AOT 시에도) 사용하는 방법파일 다운로드1
13482정성태12/13/20232821닷넷: 2183. C# - eFriend Expert OCX 예제를 .NET Core/5+ Console App에서 사용하는 방법 [2]파일 다운로드1
13481정성태12/13/20232267개발 환경 구성: 693. msbuild - .NET Core/5+ 프로젝트에서 resgen을 이용한 리소스 파일 생성 방법파일 다운로드1
13480정성태12/12/20232605개발 환경 구성: 692. Windows WSL 2 + Chrome 웹 브라우저 설치
13479정성태12/11/20232301개발 환경 구성: 691. WSL 2 (Ubuntu) + nginx 환경 설정
13477정성태12/8/20232482닷넷: 2182. C# - .NET 7부터 추가된 Int128, UInt128 [1]파일 다운로드1
13476정성태12/8/20232206닷넷: 2181. C# - .NET 8 JsonStringEnumConverter의 AOT를 위한 개선파일 다운로드1
13475정성태12/7/20232279닷넷: 2180. .NET 8 - 함수 포인터에 대한 Reflection 정보 조회파일 다운로드1
13474정성태12/6/20232133개발 환경 구성: 690. 닷넷 코어/5+ 버전의 ilasm/ildasm 실행 파일 구하는 방법 - 두 번째 이야기
1  2  3  4  [5]  6  7  8  9  10  11  12  13  14  15  ...