dotnet / runtime

.NET is a cross-platform runtime for cloud, mobile, desktop, and IoT apps.
https://docs.microsoft.com/dotnet/core/
MIT License
14.98k stars 4.66k forks source link

AccessViolation in SystemDomain::LoadBaseSystemClasses in Single File Application #80943

Open nickwhaley opened 1 year ago

nickwhaley commented 1 year ago

Description

When a single file, extracted, self contained application is run in the same folder as another self-contained application that uses a different TFM, it will crash during startup.

Reproduction Steps

  1. Create console application ConsoleApp1.
  2. Set TFM net6.0, RuntimeIdentifier win-x64, SelfContained true
  3. Create console application ConsoleApp2.
  4. Set TFM net7.0, RuntimeIdentifier win-x64, SelfContained true, PublishSingleFile true, IncludeAllContentForSelfExtract true
  5. Publish both projects to the same Folder.
  6. Run ConsoleApp2.

Expected behavior

Program to run successfully and output 'Hello, World!'.

Actual behavior

Crash with 'Fatal error. Internal CLR error. (0x80131506)'.

Regression?

No response

Known Workarounds

Deleting the System.Private.CoreLib.dll file in the current directory corrects the issue but breaks the other application. Disabling IncludeAllContentForSelfExtract also corrects the issue since the file is never extracted.

Configuration

Dotnet 7.0.102 Windows 10.0.22000 x64

Other information

I think it may be because it is attempting to load System.Private.CoreLib.dll from current directory rather than the extracted contents, even though the file version is wrong. The version of CoreLib in current directory is 6.0.1222.56807 and the extracted version in DOTNET_BUNDLE_EXTRACT_BASE_DIR is 7.0.122.56804. TRUSTED_PLATFORM_ASSEMBLIES does contain the full and correct path to the extracted CoreLib, which you would think would load first, but it does not.

Stack trace on the crash:

00 singlefilehost!MethodDesc::GetMethodDescIndex(void) (Inline Function @ 00007ff6`96ec4273) 01 singlefilehost!MethodDesc::GetMethodDescChunk(void) (Inline Function @ 00007ff6`96ec4273) 02 singlefilehost!MethodDesc::GetMethodTable_NoLogging(void) (Inline Function @ 00007ff6`96ec4273) 03 singlefilehost!MethodDesc::GetMethodTable(void) (Inline Function @ 00007ff6`96ec4273) 04 singlefilehost!MethodDesc::DoPrestub(class MethodTable pDispatchingMT = 0x00000000`00000000, CallerGCMode callerGCMode = Unknown (0n0))+0x43 05 singlefilehost!ECall::PopulateManagedCastHelpers(void)+0x1a5 06 singlefilehost!SystemDomain::LoadBaseSystemClasses(void)+0x3f7 07 singlefilehost!SystemDomain::Init(void)+0x1cf 08 singlefilehost!EEStartupHelper(void)+0x1130 09 singlefilehost!EEStartup(void)+0x51 0a singlefilehost!EnsureEEStarted(void)+0xa4 (Inline Function @ 00007ff6`96e4bfb8) 0b singlefilehost!CorHost2::Start(void)+0x138 0c singlefilehost!coreclr_initialize(char exePath = 0x00000205`6f895800 "..snip..\bin\publish\Debug\net7.0\win-x64\LPRCore_Installer_Native.exe", char * appDomainFriendlyName = 0x00007ff6`974e3d80 "clrhost", int propertyCount = 0n21, char propertyKeys = 0xffffffff`fffce4d0, char propertyValues = 0x00000205`6f8c0910, void hostHandle = 0x000000c1`8abff1d0, unsigned int domainId = 0x000000c1`8abff164)+0x29b 0d singlefilehost!coreclr_t::create(char exe_path = 0x00000205`6f895800 "..snip..\bin\publish\Debug\net7.0\win-x64\LPRCore_Installer_Native.exe", char app_domain_friendly_name = 0x00007ff6`974e3d80 "clrhost", class coreclr_property_bag_t properties = 0x00000205`6f895700, class std::unique_ptr<coreclr_t,std::default_delete > inst = 0x00000205`6f895740 empty, class std::basic_string<wchar_t,std::char_traits,std::allocator > libcoreclr_path = )+0x2d8 0e singlefilehost!`anonymous namespace'::create_coreclr(void)+0x154 0f singlefilehost!corehost_main(int argc = 0n1, wchar_t argv = 0x00000205`6f876210)+0xf2

Windbg output:

ModLoad: 00007ffe`a5350000 00007ffe`a5382000 C:\Windows\System32\IMM32.DLL ModLoad: 00007ffe`a4a50000 00007ffe`a4ad2000 C:\Windows\System32\bcryptPrimitives.dll (3aa0.22a4): Unknown exception - code 04242420 (first chance) ModLoad: 00007ffd`ebee0000 00007ffd`ec903000 ..snip..\bin\publish\Debug\net7.0\win-x64\System.Private.CoreLib.dll (3aa0.22a4): Access violation - code c0000005 (first chance) First chance exceptions are reported before any exception handling. This exception may be expected and handled. *** WARNING: Unable to verify checksum for singlefilehost.exe singlefilehost!MethodDesc::GetMethodDescIndex [inlined in singlefilehost!MethodDesc::DoPrestub+0x43]: 00007ff6`96ec4273 0fb64102 movzx eax,byte ptr [rcx+2] ds:00000000`00000002=??

strace also confirms the System.Private.CoreLib.dll is being read from current directory immediately before crash (same issue on linux).

DONET_TRACE does not report anything erroneous, but I can provide the output if needed.

ghost commented 1 year ago

Tagging subscribers to this area: @agocke, @vitek-karas, @vsadov See info in area-owners.md if you want to be subscribed.

Issue Details
### Description When a single file, extracted, self contained application is run in the same folder as another self-contained application that uses a different TFM, it will crash during startup. ### Reproduction Steps 1. Create console application ConsoleApp1. 2. Set TFM net6.0, RuntimeIdentifier win-x64, SelfContained true 3. Create console application ConsoleApp2. 4. Set TFM net7.0, RuntimeIdentifier win-x64, SelfContained true, PublishSingleFile true, IncludeAllContentForSelfExtract true 5. Publish both projects to the same Folder. 6. Run ConsoleApp2. ### Expected behavior Program to run successfully and output 'Hello, World!'. ### Actual behavior Crash with 'Fatal error. Internal CLR error. (0x80131506)'. ### Regression? _No response_ ### Known Workarounds _No response_ ### Configuration Dotnet 7.0.102 Windows 10.0.22000 x64 ### Other information I think it may be because it is attempting to load `System.Private.CoreLib.dll` from current directory rather than the extracted contents, even though the file version is wrong. The version of `CoreLib` in current directory is 6.0.1222.56807 and the extracted version in DOTNET_BUNDLE_EXTRACT_BASE_DIR is 7.0.122.56804. TRUSTED_PLATFORM_ASSEMBLIES does contain the full and correct path to the extracted `CoreLib`. Deleting the `System.Private.CoreLib.dll` file in the current directory corrects the issue but breaks the other application. Disabling IncludeAllContentForSelfExtract also corrects the issue since the file is never extracted. Stack trace on the crash: > 00 singlefilehost!MethodDesc::GetMethodDescIndex(void) (Inline Function @ 00007ff6`96ec4273) > 01 singlefilehost!MethodDesc::GetMethodDescChunk(void) (Inline Function @ 00007ff6`96ec4273) > 02 singlefilehost!MethodDesc::GetMethodTable_NoLogging(void) (Inline Function @ 00007ff6`96ec4273) > 03 singlefilehost!MethodDesc::GetMethodTable(void) (Inline Function @ 00007ff6`96ec4273) > 04 singlefilehost!MethodDesc::DoPrestub(class MethodTable * pDispatchingMT = 0x00000000`00000000, CallerGCMode callerGCMode = Unknown (0n0))+0x43 > 05 singlefilehost!ECall::PopulateManagedCastHelpers(void)+0x1a5 > 06 singlefilehost!SystemDomain::LoadBaseSystemClasses(void)+0x3f7 > 07 singlefilehost!SystemDomain::Init(void)+0x1cf > 08 singlefilehost!EEStartupHelper(void)+0x1130 > 09 singlefilehost!EEStartup(void)+0x51 > 0a singlefilehost!EnsureEEStarted(void)+0xa4 (Inline Function @ 00007ff6`96e4bfb8) > 0b singlefilehost!CorHost2::Start(void)+0x138 > 0c singlefilehost!coreclr_initialize(char * exePath = 0x00000205`6f895800 "C:\ElsagGIT\EOC4\CarSystem UI\LPRCore Installer Native\bin\publish\Debug\net7.0\win-x64\LPRCore_Installer_Native.exe", char * appDomainFriendlyName = 0x00007ff6`974e3d80 "clrhost", int propertyCount = 0n21, char ** propertyKeys = 0xffffffff`fffce4d0, char ** propertyValues = 0x00000205`6f8c0910, void ** hostHandle = 0x000000c1`8abff1d0, unsigned int * domainId = 0x000000c1`8abff164)+0x29b > 0d singlefilehost!coreclr_t::create(char * exe_path = 0x00000205`6f895800 "C:\ElsagGIT\EOC4\CarSystem UI\LPRCore Installer Native\bin\publish\Debug\net7.0\win-x64\LPRCore_Installer_Native.exe", char * app_domain_friendly_name = 0x00007ff6`974e3d80 "clrhost", class coreclr_property_bag_t * properties = 0x00000205`6f895700, class std::unique_ptr > * inst = 0x00000205`6f895740 empty, class std::basic_string,std::allocator > * libcoreclr_path = )+0x2d8 > 0e singlefilehost!`anonymous namespace'::create_coreclr(void)+0x154 > 0f singlefilehost!corehost_main(int argc = 0n1, wchar_t ** argv = 0x00000205`6f876210)+0xf2 Windbg output: > ModLoad: 00007ffe`a5350000 00007ffe`a5382000 C:\Windows\System32\IMM32.DLL > ModLoad: 00007ffe`a4a50000 00007ffe`a4ad2000 C:\Windows\System32\bcryptPrimitives.dll > (3aa0.22a4): Unknown exception - code 04242420 (first chance) > ModLoad: 00007ffd`ebee0000 00007ffd`ec903000 ..snip..\bin\publish\Debug\net7.0\win-x64\System.Private.CoreLib.dll > (3aa0.22a4): Access violation - code c0000005 (first chance) > First chance exceptions are reported before any exception handling. > This exception may be expected and handled. > *** WARNING: Unable to verify checksum for singlefilehost.exe > singlefilehost!MethodDesc::GetMethodDescIndex [inlined in singlefilehost!MethodDesc::DoPrestub+0x43]: > 00007ff6`96ec4273 0fb64102 movzx eax,byte ptr [rcx+2] ds:00000000`00000002=?? strace also confirms the `System.Private.CoreLib.dll` is being read from current directory immediately before crash (same issue on linux). DONET_TRACE does not report anything erroneous, but I can provide the output if needed.
Author: nickwhaley
Assignees: -
Labels: `area-Single-File`
Milestone: -
agocke commented 12 months ago

We're no longer recommending IncludeAllContentForSelfExtract, so that is the suggested workaround for now.