Microsoft MVP성태의 닷넷 이야기
.NET Framework: 2069. .NET 7 - AOT(ahead-of-time) 컴파일 [링크 복사], [링크+제목 복사],
조회: 15870
글쓴 사람
정성태 (techsharer at outlook.com)
홈페이지
첨부 파일
 
(연관된 글이 4개 있습니다.)
(시리즈 글이 7개 있습니다.)
개발 환경 구성: 386. .NET Framework Native compiler 프리뷰 버전 사용법
; https://www.sysnet.pe.kr/2/0/11563

.NET Framework: 2069. .NET 7 - AOT(ahead-of-time) 컴파일
; https://www.sysnet.pe.kr/2/0/13162

닷넷: 2175. C# - DllImport 메서드의 AOT 지원을 위한 LibraryImport 옵션
; https://www.sysnet.pe.kr/2/0/13466

닷넷: 2184. C# - 하나의 resource 파일을 여러 프로그램에서 (AOT 시에도) 사용하는 방법
; https://www.sysnet.pe.kr/2/0/13483

개발 환경 구성: 696. C# - 리눅스용 AOT 빌드를 docker에서 수행
; https://www.sysnet.pe.kr/2/0/13487

닷넷: 2202. C# - PublishAot의 glibc에 대한 정적 링킹하는 방법
; https://www.sysnet.pe.kr/2/0/13529

오류 유형: 913. C# - AOT StaticExecutable 정적 링킹 시 빌드 오류
; https://www.sysnet.pe.kr/2/0/13669




.NET 7 - AOT(ahead-of-time) 컴파일

여기서 잠깐, 닷넷 AOT 역사에 대해 잠깐 훑어볼까요? ^^

Conversation about crossgen2
; https://devblogs.microsoft.com/dotnet/conversation-about-crossgen2/

가장 처음에, ".NET Framework"의 NGen이 있었습니다.

Ngen.exe (Native Image Generator)
; https://learn.microsoft.com/en-us/dotnet/framework/tools/ngen-exe-native-image-generator

NGen은 윈도우 플랫폼 종속적이었고, 비록 native 코드를 생성하긴 했지만 그래도 managed 모듈을 필요로 했습니다.

이후 "Windows Phone"이 나와 크로스-플랫폼을 지원해야 하면서 별도로 AOT 코드 생성기가 개발됩니다. 얼마 안 돼 윈도우 폰은 사라졌지만, 다행히 그 역사를 .NET Core의 CrossGen으로 세대를 이어갑니다. 이후 닷넷 6에 포함된 CrossGen2는 이전 CrossGen을 (몇몇 기능 향상과 함께) C# 언어로 포팅한 것에 해당합니다.

하지만, NGen부터 CrossGen2까지의 공통점이 있다면, 바로 native + managed 모듈이 함께 필요하다는 점입니다.




자, 그리고 이제 .NET 7에서 진정한 네이티브 코드 생성기인 AOT 컴파일 기능이 공식적으로 포함됐습니다.

Native AOT Deployment
; https://learn.microsoft.com/en-us/dotnet/core/deploying/native-aot/

실제로 테스트를 해볼까요? ^^

간단한 닷넷 7 콘솔 애플리케이션을 만들고, csproj에 PublishAot 옵션을 추가합니다.

<Project Sdk="Microsoft.NET.Sdk">

    <PropertyGroup>
        <OutputType>Exe</OutputType>
        <TargetFramework>net7.0</TargetFramework>
        <ImplicitUsings>enable</ImplicitUsings>
        <Nullable>enable</Nullable>

        <PublishAot>true</PublishAot>

        <IsAotCompatible>true</IsAotCompatible> 
        <!-- IsTrimmable, EnableTrimAnalyzer, EnableSingleFileAnalyzer, EnableAotAnalyzer 4개의 값을 true로 설정한 효과 (.NET 8부터 지원) ---->
        
    </PropertyGroup>

</Project>

이후 "dotnet publish" 해주면 ".\bin\Debug\net7.0\win-x64\publish" 디렉터리에 실행 파일과 PDB 파일이 생성됩니다.

// 디버그 빌드 기준
net7_pubone_sample.exe, 약 4.69MB
net7_pubone_sample.pdb, 약 15.9MB

// 릴리스 빌드 기준
net7_pubone_sample.exe, 약 2.85MB
net7_pubone_sample.pdb, 약 12.1MB

당연히, net7_pubone_sample.exe만 (닷넷 런타임이 설치되지 않은) 다른 PC에 복사해 실행할 수 있습니다.




이렇게 완전히 AOT로 빌드된 모듈은 역어셈블이 가능할까요?

C# - PublishSingleFile로 배포한 이미지의 역어셈블 가능 여부 (난독화 필요성)
; https://www.sysnet.pe.kr/2/0/13161

위의 글에서 살펴본 .NET 6와는 달리, 이제 Windbg는 mscordaccore.dll을 로드하려고 해도,

// c:\temp\mscordaccore.dll이 있는 경우,

0:004> .cordll -lp c:\temp
CLR DLL status: No load attempts

실패합니다. 즉, 이제는 대상 프로세스를 .NET 프로세스로 볼 수 없는 것입니다. 비록 관리 Heap을 사용해 GC는 발생하겠지만 그 외의 모든 면에서 일반 native 프로세스와 동일한데요, 달리 말하면, 그냥 여러분들이 C/C++로 GC 기능을 구현한 다음 그것을 활용해 코딩한 것과 같은 것입니다.

따라서, AOT로 빌드한 경우라면 이제 native 모드로 디버깅을 해야 하는데,

0:005> ~
   0  Id: 2df8.1424 Suspend: 1 Teb: 00000036`a2077000 Unfrozen
   1  Id: 2df8.2144 Suspend: 1 Teb: 00000036`a2079000 Unfrozen
   2  Id: 2df8.21c8 Suspend: 1 Teb: 00000036`a207b000 Unfrozen
   3  Id: 2df8.2210 Suspend: 1 Teb: 00000036`a207d000 Unfrozen
   4  Id: 2df8.2e44 Suspend: 1 Teb: 00000036`a207f000 Unfrozen
.  5  Id: 2df8.1ddc Suspend: 1 Teb: 00000036`a2081000 Unfrozen

0:005> ~0s
rax=0000000000000006 rbx=00000036a22ff748 rcx=0000000000000050
rdx=0000000000000000 rsi=0000000000000050 rdi=0000000000000000
rip=00007ffabe40d184 rsp=00000036a22ff478 rbp=00000036a22ff5c0
 r8=0000000000000020  r9=0000000000000001 r10=0000000000000000
r11=00000188b1aeffe0 r12=0000000000000000 r13=0000000000000000
r14=0000000000000000 r15=0000000000000000
iopl=0         nv up ei pl zr na po nc
cs=0033  ss=002b  ds=002b  es=002b  fs=0053  gs=002b             efl=00000244
ntdll!NtReadFile+0x14:
00007ffa`be40d184 c3              ret

0:000> k
 # Child-SP          RetAddr               Call Site
00 00000036`a22ff478 00007ffa`bbaf5593     ntdll!NtReadFile+0x14
01 00000036`a22ff480 00007ff6`5f259a0d     KERNELBASE!ReadFile+0x73
02 00000036`a22ff500 00007ff6`5f25953f     net7_pubone_sample!System_Console_Interop_Kernel32___ReadFile_g____PInvoke_43_0+0x7d
03 00000036`a22ff5d0 00007ff6`5f25ad36     net7_pubone_sample!System_Console_Interop_Kernel32__ReadFile+0x5f [/_/src/libraries/System.Console/src/Microsoft.Interop.LibraryImportGenerator/Microsoft.Interop.LibraryImportGenerator/LibraryImports.g.cs @ 412] 
04 00000036`a22ff640 00007ff6`5f25ab6b     net7_pubone_sample!System_Console_System_ConsolePal_WindowsConsoleStream__ReadFileNative+0xc6 [/_/src/libraries/System.Console/src/System/ConsolePal.Windows.cs @ 1156] 
05 00000036`a22ff6f0 00007ff6`5f25e86c     net7_pubone_sample!System_Console_System_ConsolePal_WindowsConsoleStream__Read+0x6b [/_/src/libraries/System.Console/src/System/ConsolePal.Windows.cs @ 1114] 
06 00000036`a22ff770 00007ff6`5f1dcbef     net7_pubone_sample!System_Console_System_IO_ConsoleStream__Read+0x8c [/_/src/libraries/System.Console/src/System/IO/ConsoleStream.cs @ 76] 
07 00000036`a22ff7e0 00007ff6`5f1dcd6d     net7_pubone_sample!S_P_CoreLib_System_IO_StreamReader__ReadBuffer+0x1af [/_/src/libraries/System.Private.CoreLib/src/System/IO/StreamReader.cs @ 616] 
08 00000036`a22ff8a0 00007ff6`5f25ee63     net7_pubone_sample!S_P_CoreLib_System_IO_StreamReader__ReadLine+0x7d [/_/src/libraries/System.Private.CoreLib/src/System/IO/StreamReader.cs @ 785] 
09 00000036`a22ff960 00007ff6`5f25a1f5     net7_pubone_sample!System_Console_System_IO_SyncTextReader__ReadLine+0x63 [/_/src/libraries/System.Console/src/System/IO/SyncTextReader.cs @ 77] 
0a 00000036`a22ff9c0 00007ff6`5f25f688     net7_pubone_sample!System_Console_System_Console__ReadLine+0x35 [/_/src/libraries/System.Console/src/System/Console.cs @ 722] 
0b 00000036`a22ffa10 00007ff6`5f2ceaa7     net7_pubone_sample!net7_pubone_sample_net7_pubone_sample_Program__Main+0x28 [E:\net7_pubone_sample\net7_pubone_sample\Program.cs @ 8] 
0c 00000036`a22ffa50 00007ff6`5f2ceb3a     net7_pubone_sample!net7_pubone_sample__Module___MainMethodWrapper+0x17
0d 00000036`a22ffa80 00007ff6`5f0ceb6e     net7_pubone_sample!net7_pubone_sample__Module___StartupCodeMain+0x8a
0e 00000036`a22ffae0 00007ff6`5f0bdf54     net7_pubone_sample!wmain+0xae [D:\a\_work\1\s\src\coreclr\nativeaot\Bootstrap\main.cpp @ 205] 
0f (Inline Function) --------`--------     net7_pubone_sample!invoke_main+0x22 [D:\a\_work\1\s\src\vctools\crt\vcstartup\src\startup\exe_common.inl @ 90] 
10 00000036`a22ffb30 00007ffa`bcba74b4     net7_pubone_sample!__scrt_common_main_seh+0x10c [D:\a\_work\1\s\src\vctools\crt\vcstartup\src\startup\exe_common.inl @ 288] 
11 00000036`a22ffb70 00007ffa`be3c26a1     KERNEL32!BaseThreadInitThunk+0x14
12 00000036`a22ffba0 00000000`00000000     ntdll!RtlUserThreadStart+0x21

sos 확장의 도움 없이 닷넷 프로그램을 문제 분석하는 경험은... 그다지 유쾌하진 않을 것입니다. ^^ 참고로, dotnet-dump 역시 Native AOT로 빌드된 프로세스는 인식하지 못합니다.

c:\temp> dotnet-dump ps
No supported .NET processes were found

(그래도, GC Heap 관련 정보는 디버깅할 수 있는 방법을 제공해줘야 하지 않을까요? ^^;)




문서에 나오지만,

Limitations of Native AOT deployment

  • No dynamic loading (for example, Assembly.LoadFile)
  • No runtime code generation (for example, System.Reflection.Emit)
  • No C++/CLI
  • No built-in COM (only applies to Windows)
  • Requires trimming, which has limitations
  • Implies compilation into a single file, which has known incompatibilities
  • Apps include required runtime libraries (just like self-contained apps, increasing their size, as compared to framework-dependent apps)

당연히 .NET Reflection과 같은 기능들은 AOT 지원이 안 됩니다. 그리고 .NET 7의 BCL 중에서도 위와 같은 조건에 걸리는 타입들이 많기 때문에 아직은 AOT가 WPF/WinForm 등의 응용 프로그램은 지원하지 못합니다. 사실 저런 조건들이 AOT 입장에서 제약이지, WPF/WinForm 등의 개발 프레임워크를 만들 때는 유용하게 쓸 수 있는 기술들이어서 제법 광범위하게 사용했던 것입니다. 그런 이유에서, Native AOT가 과연 "아직" WPF/WinForm을 지원하지 못한 것인지, "영원히" 지원하지 못할 것인지는 두고 봐야 알 수 있을 것입니다. ^^

결국 현재의 닷넷 7 AOT는 "Console Application"을 대상으로 한 프로젝트만 지원하고 WPF/WinForm 등의 프로젝트에서 PublishAot 옵션을 주면 "error NETSDK1168: WPF is not supported or recommended with trimming enabled. Please go to https://aka.ms/dotnet-illink/wpf for more details." 빌드 오류가 발생합니다.




참고로, AOT 빌드는 UnmanagedCallersOnlyAttribute 특성과 어우러져 Native API(윈도우의 경우 Win32 DLL)로 동작하는 라이브러리를 만들기에는 아주 이상적인 환경을 제공합니다.

Build native libraries
; https://learn.microsoft.com/en-us/dotnet/core/deploying/native-aot/#build-native-libraries

관련 예제 코드
; https://github.com/dotnet/samples/tree/main/core/nativeaot/NativeLibrary




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

[연관 글]






[최초 등록일: ]
[최종 수정일: 12/22/2023]

Creative Commons License
이 저작물은 크리에이티브 커먼즈 코리아 저작자표시-비영리-변경금지 2.0 대한민국 라이센스에 따라 이용하실 수 있습니다.
by SeongTae Jeong, mailto:techsharer at outlook.com

비밀번호

댓글 작성자
 



2024-12-06 09시50분
How Much Memory Do You Need in 2024 to Run 1 Million Concurrent Tasks?
; https://hez2010.github.io/async-runtimes-benchmarks-2024/

100만 개 테스트에서 오히려 rust보다 (AOT 무관하게) C#의 메모리 사용이 더 적었다고. ^^
정성태

... 121  122  123  124  125  126  127  128  129  130  131  [132]  133  134  135  ...
NoWriterDateCnt.TitleFile(s)
1755정성태9/22/201434275오류 유형: 241. Unity Web Player를 설치해도 여전히 설치하라는 화면이 나오는 경우 [4]
1754정성태9/22/201424609VC++: 80. 내 컴퓨터에서 C++ AMP 코드가 실행이 될까요? [1]
1753정성태9/22/201420604오류 유형: 240. Lync로 세미나 참여 시 소리만 들리지 않는 경우 [1]
1752정성태9/21/201441067Windows: 100. 윈도우 8 - RDP 연결을 이용해 VNC처럼 사용자 로그온 화면을 공유하는 방법 [5]
1751정성태9/20/201438933.NET Framework: 464. 프로세스 간 통신 시 소켓 필요 없이 간단하게 Pipe를 열어 통신하는 방법 [1]파일 다운로드1
1750정성태9/20/201423828.NET Framework: 463. PInvoke 호출을 이용한 비동기 파일 작업파일 다운로드1
1749정성태9/20/201423730.NET Framework: 462. 커널 객체를 위한 null DACL 생성 방법파일 다운로드1
1748정성태9/19/201425379개발 환경 구성: 238. [Synergy] 여러 컴퓨터에서 키보드, 마우스 공유
1747정성태9/19/201428428오류 유형: 239. psexec 실행 오류 - The system cannot find the file specified.
1746정성태9/18/201426098.NET Framework: 461. .NET EXE 파일을 닷넷 프레임워크 버전에 상관없이 실행할 수 있을까요? - 두 번째 이야기 [6]파일 다운로드1
1745정성태9/17/201423034개발 환경 구성: 237. 리눅스 Integration Services 버전 업그레이드 하는 방법 [1]
1744정성태9/17/201431052.NET Framework: 460. GetTickCount / GetTickCount64와 0x7FFE0000 주솟값 [4]파일 다운로드1
1743정성태9/16/201420985오류 유형: 238. 설치 오류 - Failed to get size of pseudo bundle
1742정성태8/27/201426958개발 환경 구성: 236. Hyper-V에 설치한 리눅스 VM의 VHD 크기 늘리는 방법 [2]
1741정성태8/26/201421332.NET Framework: 459. GetModuleHandleEx로 알아보는 .NET 메서드의 DLL 모듈 관계파일 다운로드1
1740정성태8/25/201432502.NET Framework: 458. 닷넷 GC가 순환 참조를 해제할 수 있을까요? [2]파일 다운로드1
1739정성태8/24/201426517.NET Framework: 457. 교착상태(Dead-lock) 해결 방법 - Lock Leveling [2]파일 다운로드1
1738정성태8/23/201422042.NET Framework: 456. C# - CAS를 이용한 Lock 래퍼 클래스파일 다운로드1
1737정성태8/20/201419755VS.NET IDE: 93. Visual Studio 2013 동기화 문제
1736정성태8/19/201425568VC++: 79. [부연] CAS Lock 알고리즘은 과연 빠른가? [2]파일 다운로드1
1735정성태8/19/201418162.NET Framework: 455. 닷넷 사용자 정의 예외 클래스의 최소 구현 코드 - 두 번째 이야기
1734정성태8/13/201419825오류 유형: 237. Windows Media Player cannot access the file. The file might be in use, you might not have access to the computer where the file is stored, or your proxy settings might not be correct.
1733정성태8/13/201426351.NET Framework: 454. EmptyWorkingSet Win32 API를 사용하는 C# 예제파일 다운로드1
1732정성태8/13/201434465Windows: 99. INetCache 폴더가 다르게 보이는 이유
1731정성태8/11/201427075개발 환경 구성: 235. 점(.)으로 시작하는 파일명을 탐색기에서 만드는 방법
1730정성태8/11/201422161개발 환경 구성: 234. Royal TS의 터미널(Terminal) 연결에서 한글이 깨지는 현상 해결 방법
... 121  122  123  124  125  126  127  128  129  130  131  [132]  133  134  135  ...