Microsoft MVP성태의 닷넷 이야기
닷넷: 2210. C# - Native 메모리에 .NET 개체를 생성 [링크 복사], [링크+제목 복사],
조회: 2544
글쓴 사람
정성태 (techsharer at outlook.com)
홈페이지
첨부 파일

(시리즈 글이 3개 있습니다.)
.NET Framework: 1023. C# - GC 힙이 아닌 Native 힙에 인스턴스 생성 - 0SuperComicLib.LowLevel 라이브러리 소개
; https://www.sysnet.pe.kr/2/0/12538

닷넷: 2210. C# - Native 메모리에 .NET 개체를 생성
; https://www.sysnet.pe.kr/2/0/13537

닷넷: 2211. C# - NonGC(FOH) 영역에 .NET 개체를 생성
; https://www.sysnet.pe.kr/2/0/13538




C# - Native 메모리에 .NET 개체를 생성

예전에 소개한 라이브러리가 있는데요,

C# - GC 힙이 아닌 Native 힙에 인스턴스 생성 - 0SuperComicLib.LowLevel 라이브러리 소개
; https://www.sysnet.pe.kr/2/0/12538

저것이 가능한 이유는, .NET 개체에 대한 메모리 구조를 그대로 Native 메모리에 적용해서 닷넷 런타임으로 하여금 Managed 개체처럼 동일하게 접근할 수 있도록 만들었기 때문입니다.

이 과정을 간단하게 코드로 알아볼까요?

예전에 언급했던 것처럼,

C#에서 확인해 보는 관리 힙의 인스턴스 구조
; https://www.sysnet.pe.kr/2/0/1176

참조 개체의 메모리 형식은 다음과 같은 유형의 메모리로 구성됩니다.

[일반 개체인 경우]

Object Header (CPU Word 크기)
Method Table (CPU Word 크기)
...[멤버 field]...


[배열 개체인 경우]

Object Header (CPU Word 크기)
Method Table (CPU Word 크기)
# of elements (배열 요소 크기)
N 개의 요소 (연속된 배열 요소)

따라서, 우리가 만약 Int32 필드를 가진 다음과 같은 개체를 정의했다면,

public class MyObject
{
    public int Value { get; set; }
}

아래와 같이 24바이트만 채워주면 CLR은 그것을 MyObject로 인식할 수 있는 것입니다.

| object header | method table   | field Value    |
0000000000000000 0000000000000000 0000000000000000

재미 삼아 값을 하나씩 채워볼까요? 우선, object header는 0, field value는 초깃값을 주면 되므로 어렵지 않습니다. 남은 것은 Method Table인데요, 이에 대해서는 전에도 한 번 언급한 적이 있습니다.

Method Table
; https://www.sysnet.pe.kr/2/0/12142#method_table

그러니까, TypeHandle을 통해서도 구할 수 있고,

var pMethodTable = typeof(MyObject).TypeHandle.Value;

인스턴스화된 개체를 통해서도 구할 수 있습니다.

MyObject obj = new MyObject();

TypedReference tr = __makeref(obj);
nint objectPtr = **(nint**)(&tr);
IntPtr pMethodTable = *(nint*)objectPtr;

이것을 종합해, 특정 개체를 네이티브 메모리에 그대로 복제하려면 다음과 같은 식으로 작성할 수 있습니다.

using System.Runtime.InteropServices;

namespace ConsoleApp1;

internal class Program
{
    static unsafe void Main(string[] args)
    {
        MyObject obj = new MyObject() { Value = 10 };

        IntPtr allocated = Marshal.AllocHGlobal(MyObject.GetObjectSize());
        MyObject objOnNative = MyObject.WriteObject(allocated, obj);
        objOnNative.Value++;

        Console.WriteLine(obj); // 출력 결과: 10
        Console.WriteLine(objOnNative); // 출력 결과: 11

        Marshal.FreeHGlobal(allocated);
    }
}

public class MyObject
{
    public int Value { get; set; }

    public override string ToString()
    {
        return $"{Value}";
    }

    public unsafe static int GetObjectSize()
    {
        // return 24; // 0x18

        var pMethodTable = typeof(MyObject).TypeHandle.Value;
        var methodTable = *(MethodTable*)pMethodTable;
        return methodTable.BaseSize;
    }

    public unsafe static MyObject WriteObject(IntPtr ptr, MyObject obj)
    {
        *(nint*)ptr = 0; // Object Header를 쓰고,
        ptr += sizeof(nint);
        IntPtr objAddress = ptr;

        var pMethodTable = typeof(MyObject).TypeHandle.Value;
        // TypedReference tr = __makeref(obj);
        // nint objectPtr = **(nint**)(&tr);
        // IntPtr pMethodTable = *(nint*)objectPtr;

        *(nint*)ptr = pMethodTable; // Method Table을 쓰고,
        ptr += sizeof(nint);

        *(int*)ptr = obj.Value; // 멤버 필드 값을 설정

// #pragma warning disable 8500 
        // warning CS8500: This takes the address of, gets the size of, or declares a pointer to a managed type ('MyObject')
        // MyObject objValue = *(MyObject*)&objAddress;
// #pragma warning restore 8500

        MyObject objValue = Unsafe.As<nint, MyObject>(ref objAddress);

        return objValue;
    }
}

// runtime/src/coreclr/vm/methodtable.h
// ; https://github.com/dotnet/runtime/blob/main/src/coreclr/vm/methodtable.h#L584
[StructLayout(LayoutKind.Explicit)]
public struct MethodTable
{
    // Low WORD is component size for array and string types (HasComponentSize() returns true).
    // Used for flags otherwise.
    [FieldOffset(0)]
    public int Flags;

    [FieldOffset(0)]
    public ushort ComponentSize;

    // Base size of instance of this class when allocated on the heap
    [FieldOffset(4)]
    public int BaseSize;
}

이런 식으로 개체를 만드는 것이 재미있긴 하지만, 이전에 언급했던 것처럼 현실적으로 사용하기에는 어려움이 있습니다.

가장 심한 제약이 바로, 저렇게 만든 MyObject는 GC Heap에 있는 참조 개체를 포함해서는 안 된다는 점입니다. 가령, 다음과 같은 식으로 문자열조차도 보관해서는 안 됩니다.

MyObject obj = new MyObject();
obj.Value = 5;
obj.Name = "...."; // GC Heap에 있는 문자열을 보관한 경우 GC 이후 문제 발생 (.NET 8부터는 문자열 리터럴을 FOH에 위치)

public class MyObject
{
    public int Value { get; set; }
    public string Name { get; set; }
}

왜냐하면, "Name" 필드에 할당된 문자열이 GC Heap에 존재하는 경우 Garbage Collection이 동작한 후로는 아예 삭제되거나, 또는 메모리 Compaction 작업으로 인해 위치가 이동할 수 있기 때문입니다. 그렇게 되면, Native Heap에 있던 MyObject의 Name 필드는 GC가 발생하기 전의 위치 그대로를 담고 있으므로 GC 이후에는 쓰레기 위치를 가리키고 있는 것이나 다름없게 됩니다.

대충 어떤 식인지 감이 오시죠? ^^

(첨부 파일은 이 글의 예제 코드를 포함합니다.)





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







[최초 등록일: ]
[최종 수정일: 1/24/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)
13439정성태11/10/20233100닷넷: 2158. C# - 소켓 포트를 미리 시스템에 등록/예약해 사용하는 방법(Port Exclusion Ranges)파일 다운로드1
13438정성태11/9/20232668닷넷: 2157. C# - WinRT 기능을 이용해 윈도우에서 실행 중인 Media App 제어
13437정성태11/8/20232851닷넷: 2156. .NET 7 이상의 콘솔 프로그램을 (dockerfile 없이) 로컬 docker에 배포하는 방법
13436정성태11/7/20233105닷넷: 2155. C# - .NET 8 런타임부터 (Reflection 없이) 특성을 이용해 public이 아닌 멤버 호출 가능
13435정성태11/6/20233003닷넷: 2154. C# - 네이티브 자원을 포함한 관리 개체(예: 스레드)의 GC 정리
13434정성태11/1/20232780스크립트: 62. 파이썬 - class의 정적 함수를 동적으로 교체
13433정성태11/1/20232470스크립트: 61. 파이썬 - 함수 오버로딩 미지원
13432정성태10/31/20232551오류 유형: 878. 탐색기의 WSL 디렉터리 접근 시 "Attempt to access invalid address." 오류 발생
13431정성태10/31/20232932스크립트: 60. 파이썬 - 비동기 FastAPI 앱을 gunicorn으로 호스팅
13430정성태10/30/20232763닷넷: 2153. C# - 사용자가 빌드한 ICU dll 파일을 사용하는 방법
13429정성태10/27/20233052닷넷: 2152. Win32 Interop - C/C++ DLL로부터 이중 포인터 버퍼를 C#으로 받는 예제파일 다운로드1
13428정성태10/25/20233152닷넷: 2151. C# 12 - ref readonly 매개변수
13427정성태10/18/20233340닷넷: 2150. C# 12 - 정적 문맥에서 인스턴스 멤버에 대한 nameof 접근 허용(Allow nameof to always access instance members from static context)
13426정성태10/13/20233488스크립트: 59. 파이썬 - 비동기 호출 함수(run_until_complete, run_in_executor, create_task, run_in_threadpool)
13425정성태10/11/20233256닷넷: 2149. C# - PLinq의 Partitioner<T>를 이용한 사용자 정의 분할파일 다운로드1
13423정성태10/6/20233233스크립트: 58. 파이썬 - async/await 기본 사용법
13422정성태10/5/20233390닷넷: 2148. C# - async 유무에 따른 awaitable 메서드의 병렬 및 예외 처리
13421정성태10/4/20233477닷넷: 2147. C# - 비동기 메서드의 async 예약어 유무에 따른 차이
13420정성태9/26/20235790스크립트: 57. 파이썬 - UnboundLocalError: cannot access local variable '...' where it is not associated with a value
13419정성태9/25/20233286스크립트: 56. 파이썬 - RuntimeError: dictionary changed size during iteration
13418정성태9/25/20233999닷넷: 2146. C# - ConcurrentDictionary 자료 구조의 동기화 방식
13417정성태9/19/20233527닷넷: 2145. C# - 제네릭의 형식 매개변수에 속한 (매개변수를 가진) 생성자를 호출하는 방법
13416정성태9/19/20233299오류 유형: 877. redis-py - MISCONF Redis is configured to save RDB snapshots, ...
13415정성태9/18/20233802닷넷: 2144. C# 12 - 컬렉션 식(Collection Expressions)
13414정성태9/16/20233579디버깅 기술: 193. Windbg - ThreadStatic 필드 값을 조사하는 방법
13413정성태9/14/20233775닷넷: 2143. C# - 시스템 Time Zone 변경 시 이벤트 알림을 받는 방법
1  2  3  4  5  6  7  [8]  9  10  11  12  13  14  15  ...