Microsoft MVP성태의 닷넷 이야기
글쓴 사람
정성태 (techsharer at outlook.com)
홈페이지
첨부 파일
 
(연관된 글이 1개 있습니다.)

C# - 일부러 GC Heap을 깨뜨려 GC 수행 시 비정상 종료시키는 예제

물론, 일부러 그럴 필요는 없지만 ^^ 그래도 이것이 얼마나 간단할 수 있는지 살펴보겠습니다. 지난 글에 설명했듯이,

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

관리 힙에 있는 객체는 메모리 구조가 OBJECT Header와 Method Table 주소 및 각 타입의 필드로 이뤄집니다.

int[] test = new int[1];
test[0] = 0xff;

Console.WriteLine("int [] ================== ");
fixed (int* p1 = &test[0])
{
    Console.WriteLine("OBJECT Header(SyncBlock Index): " + (*(p1 - 3)).ToString("x"));
    Console.WriteLine("MethodTable Address: " + (*(p1 - 2)).ToString("x"));
    Console.WriteLine("배열 요소 크기: " + (*(p1 - 1)).ToString("x"));
    Console.WriteLine("0번째 배열 값: " + (*(p1 - 0)).ToString("x"));
}

그래서, 만약 관리 힙의 시작 주소를 알 수 있다면 그 이후로 Method Table 주소를 이용해 해당 타입의 필드 정보를 보고 객체가 소유한 메모리의 크기를 구해 다음 객체가 할당된 시작점으로 이동할 수 있습니다. 실제로 GC는 이런 식으로 GC Heap을 열람하면서 객체 정리를 해나갑니다.

그렇다면, 당연히 "Method Table 주소"를 구할 수 없다면 GC는 해당 객체의 크기를 알 수 없어 패닉(?)에 빠지게 될 것입니다. ^^; 이것을 다음과 같이 간단한 코드로 테스트할 수 있습니다.

using System;

class Program
{
    static unsafe void Main(string[] args)
    {
        int[] test = new int[1];
        test[0] = 0xff;

        Console.WriteLine("int [] ================== ");
        fixed (int* p1 = &test[0])
        {
            Console.WriteLine("OBJECT Header(SyncBlock Index): " + (*(p1 - 3)).ToString("x"));
            Console.WriteLine("MethodTable Address: " + (*(p1 - 2)).ToString("x"));

            *(p1 - 2) = 0; // GC의 MethodTable 주소 정보를 제거

            Console.WriteLine("배열 요소 크기: " + (*(p1 - 1)).ToString("x"));
            Console.WriteLine("0번째 배열 값: " + (*(p1 - 0)).ToString("x"));
        }

        GC.Collect(); // 여기서 비정상 종료

        Console.WriteLine("GCed!"); // 이 코드는 절대 수행되지 않음
    }
}

이때 발생하는 이벤트 로그를 보면 다음과 같습니다.

Log Name:      Application
Source:        .NET Runtime
Date:          2019-11-15 오후 10:45:32
Event ID:      1023
Task Category: None
Level:         Error
Keywords:      Classic
User:          N/A
Computer:      TESTPC
Description:
Application: ConsoleApp1.exe
Framework Version: v4.0.30319
Description: The process was terminated due to an internal error in the .NET Runtime at IP 709E21D1 (70900000) with exit code 80131506.

Log Name:      Application
Source:        Application Error
Date:          2019-11-15 오후 10:45:32
Event ID:      1000
Task Category: (100)
Level:         Error
Keywords:      Classic
User:          N/A
Computer:      TESTPC
Description:
Faulting application name: ConsoleApp1.exe, version: 1.0.0.0, time stamp: 0xf39545aa
Faulting module name: clr.dll, version: 4.8.4042.0, time stamp: 0x5d7a9e00
Exception code: 0xc0000005
Fault offset: 0x000e21d1
Faulting process id: 0x5f1c
Faulting application start time: 0x01d59b565e367a5c
Faulting application path: c:\temp\ConsoleApp1\bin\Debug\ConsoleApp1.exe
Faulting module path: C:\Windows\Microsoft.NET\Framework\v4.0.30319\clr.dll
Report Id: b790601d-4a9d-470c-b8a9-144beabee7ab
Faulting package full name: 
Faulting package-relative application ID: 

Log Name:      Application
Source:        Windows Error Reporting
Date:          2019-11-15 오후 10:45:35
Event ID:      1001
Task Category: None
Level:         Information
Keywords:      Classic
User:          N/A
Computer:      TESTPC
Description:
Fault bucket 2275602419194088766, type 1
Event Name: APPCRASH
Response: Not available
Cab Id: 0

Problem signature:
P1: ConsoleApp1.exe
P2: 1.0.0.0
P3: f39545aa
P4: clr.dll
P5: 4.8.4042.0
P6: 5d7a9e00
P7: c0000005
P8: 000e21d1
P9: 
P10: 

Attached files:
\\?\C:\ProgramData\Microsoft\Windows\WER\Temp\WER4A26.tmp.mdmp
\\?\C:\ProgramData\Microsoft\Windows\WER\Temp\WER4A95.tmp.WERInternalMetadata.xml
\\?\C:\ProgramData\Microsoft\Windows\WER\Temp\WER4AC5.tmp.xml
\\?\C:\ProgramData\Microsoft\Windows\WER\Temp\WER4AC7.tmp.csv
\\?\C:\ProgramData\Microsoft\Windows\WER\Temp\WER4AE7.tmp.txt

These files may be available here:
\\?\C:\ProgramData\Microsoft\Windows\WER\ReportArchive\AppCrash_ConsoleApp1.exe_205af63bf864821f555164beb5b9ebbca41c_870f1ae3_efc6993a-ca59-44d7-95e8-0749fcc35e9a

Analysis symbol: 
Rechecking for solution: 0
Report Id: b790601d-4a9d-470c-b8a9-144beabee7ab
Report Status: 268435456
Hashed bucket: f856d214730f94afbf949057f6154d3e
Cab Guid: 0

비록 이번에도 "Heap"이 깨지긴 했지만 말 그대로 .NET CLR의 관리 Heap이기 때문에 Native Heap이 깨졌을 때처럼 FaultTolerantHeap과 같은 대우는 받지 못합니다.

어쨌든 이런 식으로 관리 힙이 깨져 비정상 종료하는 경우 메모리 덤프를 받으면, 다음과 같은 콜 스택을 확인할 수 있습니다.

clr!WKS::gc_heap::mark_object_simple+60 
clr!WKS::GCHeap::Promote+a8 
clr!GcEnumObject+37 
clr!EECodeManager::EnumGcRefs+840 
clr!GcStackCrawlCallBack+139 
clr!Thread::StackWalkFramesEx+92 
clr!Thread::StackWalkFrames+9d 
clr!standalone::ScanStackRoots+43 
clr!GCToEEInterface::GcScanRoots+db 
clr!WKS::gc_heap::mark_phase+170 
clr!WKS::gc_heap::gc1+ae 
clr!WKS::gc_heap::garbage_collect+367 
clr!WKS::GCHeap::GarbageCollectGeneration+1bd 
clr!WKS::GCHeap::GarbageCollectTry+71 
clr!WKS::GCHeap::GarbageCollect+ac 
clr!GCInterface::Collect+69 
mscorlib_ni!System.GC.Collect()+31 
mscorlib_ni!System.GC.Collect()+31 
Program.Main(System.String[])+188 
clr!CallDescrWorkerInternal+34 
clr!CallDescrWorkerWithHandler+6b 
clr!MethodDescCallSite::CallTargetWorker+16a 
clr!RunMain+1b3 
clr!Assembly::ExecuteMainMethod+f7 
clr!SystemDomain::ExecuteMainMethod+5ef 
clr!ExecuteEXE+4c 
clr!_CorExeMainInternal+dc 
clr!_CorExeMain+4d 
mscoreei!_CorExeMain+d6 
mscoree!ShellShim__CorExeMain+9e 
mscoree!_CorExeMain_Exported+8 
kernel32!BaseThreadInitThunk+19 
ntdll!__RtlUserThreadStart+2f 
ntdll!_RtlUserThreadStart+1b 
// ServerGC의 경우
clr!SVR::gc_heap::plan_phase+12bb
clr!SVR::gc_heap::gc1+c3
clr!SVR::gc_heap::garbage_collect+f0
clr!SVR::gc_heap::gc_thread_function+74
clr!SVR::gc_heap::gc_thread_stub+9a
clr!<lambda_5d4b8dc0ce3eb05b864e38505727a1c1>::<lambda_invoker_cdecl>+51
kernel32!BaseThreadInitThunk+14
ntdll!RtlUserThreadStart+21

참고로 이때의 예외 유형은 System.ExecutionEngineException입니다.

아무튼, 테스트를 통해 알 수 있겠지만 관리 힙의 데이터 주소를 native 측에 전달하거나, 또는 unsafe/fixed로 다룰 때는 그만큼 주의를 기울여야 합니다. 위의 사례와는 반대로 해당 데이터를 넘어서 쓰게 되면 다음 객체의 헤더를 덮어쓰게 될 것이고 그럴 때에도 GC는 헤어날 수 없는 충격에 빠지게 될 테니까요. ^^




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

[연관 글]






[최초 등록일: ]
[최종 수정일: 5/20/2021]

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)
12843정성태10/3/20216582스크립트: 29. 파이썬 - fork 시 기존 클라이언트 소켓 및 스레드의 동작파일 다운로드1
12842정성태10/1/202124744오류 유형: 763. 파이썬 오류 - AttributeError: type object '...' has no attribute '...'
12841정성태10/1/20218369스크립트: 28. 모든 파이썬 프로세스에 올라오는 특별한 파일 - sitecustomize.py
12840정성태9/30/20218406.NET Framework: 1119. Entity Framework의 Join 사용 시 다중 칼럼에 대한 OR 조건 쿼리파일 다운로드1
12839정성태9/15/20219485.NET Framework: 1118. C# 11 - 제네릭 타입의 특성 적용파일 다운로드1
12838정성태9/13/20219111.NET Framework: 1117. C# - Task에 전달한 Action, Func 유형에 따라 달라지는 async/await 비동기 처리 [2]파일 다운로드1
12837정성태9/11/20218047VC++: 151. Golang - fmt.Errorf, errors.Is, errors.As 설명
12836정성태9/10/20217627Linux: 45. 리눅스 - 실행 중인 다른 프로그램의 출력을 확인하는 방법
12835정성태9/7/20218937.NET Framework: 1116. C# 10 - (15) CallerArgumentExpression 특성 추가 [2]파일 다운로드1
12834정성태9/7/20217310오류 유형: 762. Visual Studio 2019 Build Tools - 'C:\Program' is not recognized as an internal or external command, operable program or batch file.
12833정성태9/6/20216767VC++: 150. Golang - TCP client/server echo 예제 코드파일 다운로드1
12832정성태9/6/20217581VC++: 149. Golang - 인터페이스 포인터가 의미 있을까요?
12831정성태9/6/20216138VC++: 148. Golang - 채널에 따른 다중 작업 처리파일 다운로드1
12830정성태9/6/20218354오류 유형: 761. Internet Explorer에서 파일 다운로드 시 "Your current security settings do not allow this file to be downloaded." 오류
12829정성태9/5/202110002.NET Framework: 1115. C# 10 - (14) 구조체 타입에 기본 생성자 정의 가능파일 다운로드1
12828정성태9/4/20218130.NET Framework: 1114. C# 10 - (13) 단일 파일 내에 적용되는 namespace 선언파일 다운로드1
12827정성태9/4/20218121스크립트: 27. 파이썬 - 웹 페이지 데이터 수집을 위한 scrapy Crawler 사용법 요약
12826정성태9/3/202110367.NET Framework: 1113. C# 10 - (12) 문자열 보간 성능 개선 [1]파일 다운로드1
12825정성태9/3/20217910개발 환경 구성: 603. GoLand - WSL 환경과 연동
12824정성태9/2/202116987오류 유형: 760. 파이썬 tensorflow - Dst tensor is not initialized. 오류 메시지
12823정성태9/2/20216732스크립트: 26. 파이썬 - PyCharm을 이용한 fork 디버그 방법
12822정성태9/1/202111935오류 유형: 759. 파이썬 tensorflow - ValueError: Shapes (...) and (...) are incompatible [2]
12821정성태9/1/20217471.NET Framework: 1112. C# - .NET 6부터 공개된 ISpanFormattable 사용법
12820정성태9/1/20217786VC++: 147. Golang - try/catch에 대응하는 panic/recover [1]파일 다운로드1
12819정성태8/31/20217906.NET Framework: 1111. C# - FormattableString 타입
12818정성태8/31/20217145Windows: 198. 윈도우 - 작업 관리자에서 (tensorflow 등으로 인한) GPU 연산 부하 보는 방법
... [31]  32  33  34  35  36  37  38  39  40  41  42  43  44  45  ...