Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix R2R files doublemap on UNIX #113082

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

Conversation

t-mustafin
Copy link
Contributor

Runtime creates both layouts for each dll: FlatLayout and LoadedLayout. Non-R2R smaps does not has difference after secondary LoadedLayout, but for R2R secondary LoadedLayout leads to additional smap containing Shared_Clean memory.

This patch changes first FlatLayout creation to LoadedLayout eliminating this additional smap.

Smaps before this patch:

7f100d490000-7f100d491000 r--p 00000000 103:01 26091497                  /home/runtime/installed_x64/clean_478cfe2/main_releaseO0_478cfe2/hello/readline.ni.dll
7f100d4a0000-7f100d4a1000 r-xp 00000000 103:01 26091497                  /home/runtime/installed_x64/clean_478cfe2/main_releaseO0_478cfe2/hello/readline.ni.dll
7f100d4c1000-7f100d4c2000 rw-p 00001000 103:01 26091497                  /home/runtime/installed_x64/clean_478cfe2/main_releaseO0_478cfe2/hello/readline.ni.dll
7f100d4e1000-7f100d4e2000 r--p 00001000 103:01 26091497                  /home/runtime/installed_x64/clean_478cfe2/main_releaseO0_478cfe2/hello/readline.ni.dll
7f108807d000-7f108807f000 r--s 00000000 103:01 26091497                  /home/runtime/installed_x64/clean_478cfe2/main_releaseO0_478cfe2/hello/readline.ni.dll

Smaps with this patch:

7fc50ff40000-7fc50ff41000 r--p 00000000 103:01 27937953                  /home/runtime/installed_x64/clean_478cfe2/main_releaseO0_PR_74c544a/hello/readline.ni.dll
7fc50ff50000-7fc50ff51000 r-xp 00000000 103:01 27937953                  /home/runtime/installed_x64/clean_478cfe2/main_releaseO0_PR_74c544a/hello/readline.ni.dll
7fc50ff71000-7fc50ff72000 rw-p 00001000 103:01 27937953                  /home/runtime/installed_x64/clean_478cfe2/main_releaseO0_PR_74c544a/hello/readline.ni.dll
7fc50ff91000-7fc50ff92000 r--p 00001000 103:01 27937953                  /home/runtime/installed_x64/clean_478cfe2/main_releaseO0_PR_74c544a/hello/readline.ni.dll
Additional smap
7f108807d000-7f108807f000 r--s 00000000 103:01 26091497                  /home/runtime/installed_x64/clean_478cfe2/main_releaseO0_478cfe2/hello/readline.ni.dll
Size:                  8 kB
KernelPageSize:        4 kB
MMUPageSize:           4 kB
Rss:                   8 kB
Pss:                   3 kB
Shared_Clean:          8 kB
Shared_Dirty:          0 kB
Private_Clean:         0 kB
Private_Dirty:         0 kB
Referenced:            8 kB
Anonymous:             0 kB
LazyFree:              0 kB
AnonHugePages:         0 kB
ShmemPmdMapped:        0 kB
FilePmdMapped:        0 kB
Shared_Hugetlb:        0 kB
Private_Hugetlb:       0 kB
Swap:                  0 kB
SwapPss:               0 kB
Locked:                0 kB
THPeligible:        0
VmFlags: rd mr me ms sd

Question about this problem: #112901
cc @gbalykov

@dotnet-policy-service dotnet-policy-service bot added the community-contribution Indicates that the PR has been added by a community member label Mar 3, 2025
Copy link
Contributor

Tagging subscribers to this area: @mangod9
See info in area-owners.md if you want to be subscribed.

@VSadov
Copy link
Member

VSadov commented Mar 3, 2025

As I remember the reason for preferring Flat layout by default is that Flat layout is very cheap to load (basically a direct map of a file) and sufficient for many uses - reflection, JIT-compiling, etc. The actual wired memory that is consumed by the map will depend on the use of the file. If it is only used by reflection, for example, only metadata locations may be needed to be paged in.

Only when we need to execute binary code in R2R sections is when we need loaded layout.
Loaded layout is more complex as sections must be mapped with appropriate offsets, perhaps relocated, memory protection must be tweaked for readable/writeable/executable. Sometimes it may require copying (I do not remember all details).

The reason why we prefer Loaded layout on Windows is that on Windows the OS loader has numerous optimizations for loading/caching/sharing PE images. Note that we do not prefer loaded even on windows, if the assembly is in a bundle - because OS loader does not know about bundles, only can load separate physical files.

The first thing I would wonder about is how this impacts the startup time of an app with many assemblies. Especially if the app is published as a singlefile.

Also - is this just an observation that files are loaded twice in different layouts or it was causing some kind of issues?

@VSadov
Copy link
Member

VSadov commented Mar 3, 2025

I think if we have/pass some extra information that would indicate that the assembly will be inevitably executed, then loading "Loaded" right away might be an interesting optimization.
Just defaulting to "Loaded" might have scenarios where it is not a good idea.

@jkotas
Copy link
Member

jkotas commented Mar 4, 2025

I think if we have/pass some extra information that would indicate that the assembly will be inevitably executed

I think we would have look at the first page of the assembly and then decide whether on the layout. If it is R2R image, use Loaded layout and Flat layout otherwise.

@t-mustafin
Copy link
Contributor Author

I think if we have/pass some extra information that would indicate that the assembly will be inevitably executed

I think we would have look at the first page of the assembly and then decide whether on the layout. If it is R2R image, use Loaded layout and Flat layout otherwise.

6.0 runtime created layout in that way. Force-pushed this version.

Also - is this just an observation that files are loaded twice in different layouts or it was causing some kind of issues?

This additional smap leads to memory consumption increase, it is the issue.

Only when we need to execute binary code in R2R sections is when we need loaded layout.

For non-R2R dlls PEImage::GetOrCreateLayoutInternal called twice: first call with imageLayoutMask=0x15 - LAYOUT_ANY and second call with mask 0x4 - LAYOUT_LOADED.

GetOrCreateLayoutInternal call stacks
Thread 1 "corerun" hit Breakpoint 1, PEImage::GetOrCreateLayoutInternal (this=this@entry=0x55e9f12a8680, imageLayoutMask=imageLayoutMask@entry=15) at /home/runtime/src/coreclr/vm/peimage.cpp:693
693         PTR_PEImageLayout pRetVal=GetExistingLayoutInternal(imageLayoutMask);
$7 = (WCHAR *) 0x55e9f12b6290 u"/home/runtime/installed_x64/clean_478cfe2/main_releaseO0_checkByFlat_6bc6115/System.Runtime.dll"
(gdb) bt
#0  PEImage::GetOrCreateLayoutInternal (this=this@entry=0x55e9f12a8680, imageLayoutMask=imageLayoutMask@entry=15)
    at /home/runtime/src/coreclr/vm/peimage.cpp:693
#1  0x00007fb91b0817e5 in PEImage::GetOrCreateLayout (this=0x55e9f12a8680, imageLayoutMask=15) at /home/runtime/src/coreclr/vm/peimage.cpp:659
#2  0x00007fb91b106102 in BinderAcquireImport (pPEImage=0x55e9f12a8680, ppIAssemblyMetaDataImport=0x7fffaef23010, pdwPAFlags=0x7fffaef23238)
    at /home/runtime/src/coreclr/vm/coreassemblyspec.cpp:122
#3  0x00007fb91b34a2e3 in BINDER_SPACE::AssemblyName::Init (this=0x55e9f12aad10, pPEImage=0x421c380a7e60b900)
    at /home/runtime/src/coreclr/binder/assemblyname.cpp:62
#4  0x00007fb91b344fed in BINDER_SPACE::Assembly::Init (this=0x55e9f12b6250, pPEImage=0x55e9f12a8680, fIsInTPA=1)
    at /home/runtime/src/coreclr/binder/assembly.cpp:44
#5  0x00007fb91b346514 in BINDER_SPACE::AssemblyBinderCommon::GetAssembly (assemblyPath=..., fIsInTPA=fIsInTPA@entry=1, 
    ppAssembly=ppAssembly@entry=0x7fffaef23400, bundleFileLocation=...) at /home/runtime/src/coreclr/binder/assemblybindercommon.cpp:1020
#6  0x00007fb91b3477b3 in BINDER_SPACE::AssemblyBinderCommon::BindByTpaList (pApplicationContext=pApplicationContext@entry=0x55e9f1274160, 
    pRequestedAssemblyName=pRequestedAssemblyName@entry=0x55e9f129abd0, excludeAppPaths=false, pBindResult=pBindResult@entry=0x7fffaef23908)
    at /home/runtime/src/coreclr/binder/assemblybindercommon.cpp:910
#7  0x00007fb91b346b62 in BINDER_SPACE::AssemblyBinderCommon::BindLocked (pApplicationContext=0x55e9f12a8680, 
    pApplicationContext@entry=0x55e9f1274160, pAssemblyName=pAssemblyName@entry=0x55e9f129abd0, skipVersionCompatibilityCheck=false, 
    excludeAppPaths=false, pBindResult=pBindResult@entry=0x7fffaef23908) at /home/runtime/src/coreclr/binder/assemblybindercommon.cpp:510
#8  0x00007fb91b345854 in BINDER_SPACE::AssemblyBinderCommon::BindByName (pApplicationContext=pApplicationContext@entry=0x55e9f1274160, 
    pAssemblyName=pAssemblyName@entry=0x55e9f129abd0, skipFailureCaching=false, skipVersionCompatibilityCheck=false, excludeAppPaths=false, 
    pBindResult=pBindResult@entry=0x7fffaef23908) at /home/runtime/src/coreclr/binder/assemblybindercommon.cpp:430
#9  0x00007fb91b3453e8 in BINDER_SPACE::AssemblyBinderCommon::BindAssembly (pBinder=0x55e9f1274140, pAssemblyName=0xf, 
    excludeAppPaths=<optimized out>, ppAssembly=0x7fffaef23b30) at /home/runtime/src/coreclr/binder/assemblybindercommon.cpp:211
#10 0x00007fb91b34d1ef in DefaultAssemblyBinder::BindAssemblyByNameWorker (this=0x55e9f1274140, pAssemblyName=0x55e9f129abd0, 
    ppCoreCLRFoundAssembly=0x7fffaef23b30, excludeAppPaths=false) at /home/runtime/src/coreclr/binder/defaultassemblybinder.cpp:26
#11 DefaultAssemblyBinder::BindUsingAssemblyName (this=0x55e9f12a8680, pAssemblyName=0xf, ppAssembly=0x7fffaef23c20)
    at /home/runtime/src/coreclr/binder/defaultassemblybinder.cpp:51
#12 0x00007fb91afdde18 in AssemblyBinder::BindAssemblyByName (this=0x55e9f1274140, pAssemblyNameData=<optimized out>, ppAssembly=0x7fffaef23c20)
    at /home/runtime/src/coreclr/vm/assemblybinder.cpp:23
#13 0x00007fb91b105b10 in AssemblySpec::Bind (this=0x7fffaef246b0, pAppDomain=<optimized out>, ppAssembly=0x7fffaef240f8)
    at /home/runtime/src/coreclr/vm/coreassemblyspec.cpp:66
#14 0x00007fb91afd2566 in AppDomain::BindAssemblySpec (this=0x55e9f1271c40, pSpec=0x7fffaef246b0, fThrowOnFileNotFound=1)
    at /home/runtime/src/coreclr/vm/appdomain.cpp:3176
#15 0x00007fb91b080076 in PEAssembly::LoadAssembly (this=0x55e9f12b5f20, kAssemblyRef=587202561)
    at /home/runtime/src/coreclr/vm/peassembly.cpp:492
#16 0x00007fb91afea083 in Module::LoadAssemblyImpl (this=0x7fb89d05a420, kAssemblyRef=587202561)
    at /home/runtime/src/coreclr/vm/ceeload.cpp:2415
#17 0x00007fb91afda58a in ModuleBase::LoadAssembly (this=0x7fb89d05a420, kAssemblyRef=15) at /home/runtime/src/coreclr/vm/ceeload.h:577
#18 Assembly::FindModuleByTypeRef (pModule=0x7fb89d05a420, tkType=15, loadFlag=Loader::Load, pfNoResolutionScope=0x7fffaef248c4)
    at /home/runtime/src/coreclr/vm/assembly.cpp:878
#19 0x00007fb91affb71f in ClassLoader::LoadTypeDefOrRefThrowing (pModule=<optimized out>, pModule@entry=0x7fb89d05a420, 
    typeDefOrRef=<optimized out>, typeDefOrRef@entry=16777228, fNotFoundAction=fNotFoundAction@entry=ClassLoader::ThrowIfNotFound, 
    fUninstantiated=fUninstantiated@entry=ClassLoader::FailIfUninstDefOrRef, tokenNotToLoad=tokenNotToLoad@entry=0, 
    level=level@entry=CLASS_LOAD_APPROXPARENTS) at /home/runtime/src/coreclr/vm/clsload.cpp:2076
#20 0x00007fb91affbf71 in ClassLoader::LoadApproxTypeThrowing (pModule=pModule@entry=0x7fb89d05a420, tok=16777228, 
    pSigInst=pSigInst@entry=0x7fffaef26b60, pClassTypeContext=pClassTypeContext@entry=0x7fffaef26b20)
    at /home/runtime/src/coreclr/vm/clsload.cpp:2484
#21 0x00007fb91affc344 in ClassLoader::LoadApproxParentThrowing (pModule=0x7fb89d05a420, cl=33554434, pParentInst=0x7fffaef26b60, 
    pClassTypeContext=0x7fffaef26b20) at /home/runtime/src/coreclr/vm/clsload.cpp:2538
#22 0x00007fb91b15907c in ClassLoader::CreateTypeHandleForTypeDefThrowing (pModule=0x7fb89d05a420, cl=15, inst=..., pamTracker=0x7fffaef26e60)
    at /home/runtime/src/coreclr/vm/methodtablebuilder.cpp:12266
#23 0x00007fb91affc9de in ClassLoader::CreateTypeHandleForTypeKey (pKey=pKey@entry=0x7fffaef27158, pamTracker=pamTracker@entry=0x7fffaef26e60)
    at /home/runtime/src/coreclr/vm/typehandle.h:665
#24 0x00007fb91affd795 in ClassLoader::DoIncrementalLoad (pTypeKey=0x7fffaef27158, typeHnd=..., currentLevel=<optimized out>)
    at /home/runtime/src/coreclr/vm/clsload.cpp:2592
#25 ClassLoader::LoadTypeHandleForTypeKey_Body (this=this@entry=0x55e9f129b8e0, pTypeKey=pTypeKey@entry=0x7fffaef27158, typeHnd=..., 
    targetLevel=CLASS_LOAD_EXACTPARENTS) at /home/runtime/src/coreclr/vm/clsload.cpp:3259
#26 0x00007fb91aff9e9e in ClassLoader::LoadTypeHandleForTypeKey (this=0x55e9f129b8e0, pTypeKey=pTypeKey@entry=0x7fffaef27158, typeHnd=..., 
    targetLevel=targetLevel@entry=CLASS_LOADED, pInstContext=pInstContext@entry=0x0) at /home/runtime/src/coreclr/vm/clsload.cpp:2990
#27 0x00007fb91affab1c in ClassLoader::LoadTypeDefThrowing (pModule=0x7fb89d05a420, pModule@entry=0x0, typeDef=typeDef@entry=33554434, 
    fNotFoundAction=fNotFoundAction@entry=ClassLoader::ThrowIfNotFound, 
    fUninstantiated=fUninstantiated@entry=ClassLoader::FailIfUninstDefOrRef, tokenNotToLoad=tokenNotToLoad@entry=0, level=CLASS_LOAD_BEGIN, 
    level@entry=CLASS_LOADED, pTargetInstantiation=0x0) at /home/runtime/src/coreclr/vm/clsload.cpp:1969
#28 0x00007fb91affb86e in ClassLoader::LoadTypeDefOrRefThrowing (pModule=<optimized out>, typeDefOrRef=<optimized out>, 
    fNotFoundAction=ClassLoader::ThrowIfNotFound, fUninstantiated=ClassLoader::FailIfUninstDefOrRef, tokenNotToLoad=0, level=4046105872)
    at /home/runtime/src/coreclr/vm/clsload.cpp:2138
#29 0x00007fb91afdc794 in Assembly::GetEntryPoint (this=this@entry=0x55e9f12b4d00) at /home/runtime/src/coreclr/vm/assembly.cpp:1518
#30 0x00007fb91afdc233 in Assembly::ExecuteMainMethod (this=0x55e9f12b4d00, stringArgs=0x7fffaef27860, waitForOtherThreads=1)
    at /home/runtime/src/coreclr/vm/assembly.cpp:1382
#31 0x00007fb91b004b96 in CorHost2::ExecuteAssembly (this=<optimized out>, dwAppDomainId=<optimized out>, pwzAssemblyPath=<optimized out>, 
    argc=0, argv=0x0, pReturnValue=0x7fffaef27a1c) at /home/runtime/src/coreclr/vm/corhost.cpp:349
#32 0x00007fb91afca348 in coreclr_execute_assembly (hostHandle=0x55e9f12a8680, domainId=1, argc=0, argv=<optimized out>, 
    managedAssemblyPath=<optimized out>, exitCode=0x7fffaef27a1c) at /home/runtime/src/coreclr/dlls/mscoree/exports.cpp:495
#33 0x000055e9f09eb758 in run (config=...) at /home/runtime/src/coreclr/hosts/corerun/corerun.cpp:431
--Type <RET> for more, q to quit, c to continue without paging--c
#34 main (argc=<optimized out>, argv=<optimized out>) at /home/runtime/src/coreclr/hosts/corerun/corerun.cpp:643
(gdb) c
Continuing.

Thread 1 "corerun" hit Breakpoint 1, PEImage::GetOrCreateLayoutInternal (this=this@entry=0x55e9f12a8680, imageLayoutMask=imageLayoutMask@entry=4) at /home/runtime/src/coreclr/vm/peimage.cpp:693
693         PTR_PEImageLayout pRetVal=GetExistingLayoutInternal(imageLayoutMask);
$8 = (WCHAR *) 0x55e9f12b6290 u"/home/runtime/installed_x64/clean_478cfe2/main_releaseO0_checkByFlat_6bc6115/System.Runtime.dll"
(gdb) bt
#0  PEImage::GetOrCreateLayoutInternal (this=this@entry=0x55e9f12a8680, imageLayoutMask=imageLayoutMask@entry=4)
    at /home/runtime/src/coreclr/vm/peimage.cpp:693
#1  0x00007fb91b0817e5 in PEImage::GetOrCreateLayout (this=0x55e9f12a8680, imageLayoutMask=4) at /home/runtime/src/coreclr/vm/peimage.cpp:659
#2  0x00007fb91b07f5a5 in PEAssembly::EnsureLoaded (this=0x55e9f12b63d0) at /home/runtime/src/coreclr/vm/peassembly.cpp:71
#3  0x00007fb91afd842e in Assembly::Init (this=0x55e9f12ab5d0, pamTracker=pamTracker@entry=0x7fffaef24250)
    at /home/runtime/src/coreclr/vm/assembly.cpp:181
#4  0x00007fb91afd8ee0 in Assembly::Create (pPEAssembly=<optimized out>, pamTracker=0x7fffaef24250, pLoaderAllocator=<optimized out>)
    at /home/runtime/src/coreclr/vm/assembly.cpp:352
#5  0x00007fb91b00c308 in DomainAssembly::DomainAssembly (this=0x55e9f12b4f90, pPEAssembly=0x55e9f12b63d0, 
    pLoaderAllocator=0x7fb91b62d1c0 <g_pSystemDomainMemory+16>, memTracker=0x7fffaef24250) at /home/runtime/src/coreclr/vm/domainassembly.cpp:35
#6  0x00007fb91afd04e1 in AppDomain::LoadAssemblyInternal (this=this@entry=0x55e9f1271c40, pIdentity=pIdentity@entry=0x7fffaef24748, 
    pPEAssembly=pPEAssembly@entry=0x55e9f12b63d0, targetLevel=targetLevel@entry=FILE_LOADED) at /home/runtime/src/coreclr/vm/appdomain.cpp:2467
#7  0x00007fb91afcd57e in AppDomain::LoadAssembly (this=0x55e9f1271c40, pSpec=0x7fffaef24748, pPEAssembly=0x55e9f12b63d0, 
    targetLevel=FILE_LOADED) at /home/runtime/src/coreclr/vm/appdomain.cpp:2378
#8  0x00007fb91afea120 in Module::LoadAssemblyImpl (this=0x7fb89d05a420, kAssemblyRef=587202561)
    at /home/runtime/src/coreclr/vm/ceeload.cpp:2426
#9  0x00007fb91afda58a in ModuleBase::LoadAssembly (this=0x7fb89d05a420, kAssemblyRef=4) at /home/runtime/src/coreclr/vm/ceeload.h:577
#10 Assembly::FindModuleByTypeRef (pModule=0x7fb89d05a420, tkType=4, loadFlag=Loader::Load, pfNoResolutionScope=0x7fffaef248c4)
    at /home/runtime/src/coreclr/vm/assembly.cpp:878
#11 0x00007fb91affb71f in ClassLoader::LoadTypeDefOrRefThrowing (pModule=<optimized out>, pModule@entry=0x7fb89d05a420, 
    typeDefOrRef=<optimized out>, typeDefOrRef@entry=16777228, fNotFoundAction=fNotFoundAction@entry=ClassLoader::ThrowIfNotFound, 
    fUninstantiated=fUninstantiated@entry=ClassLoader::FailIfUninstDefOrRef, tokenNotToLoad=tokenNotToLoad@entry=0, 
    level=level@entry=CLASS_LOAD_APPROXPARENTS) at /home/runtime/src/coreclr/vm/clsload.cpp:2076
#12 0x00007fb91affbf71 in ClassLoader::LoadApproxTypeThrowing (pModule=pModule@entry=0x7fb89d05a420, tok=16777228, 
    pSigInst=pSigInst@entry=0x7fffaef26b60, pClassTypeContext=pClassTypeContext@entry=0x7fffaef26b20)
    at /home/runtime/src/coreclr/vm/clsload.cpp:2484
#13 0x00007fb91affc344 in ClassLoader::LoadApproxParentThrowing (pModule=0x7fb89d05a420, cl=33554434, pParentInst=0x7fffaef26b60, 
    pClassTypeContext=0x7fffaef26b20) at /home/runtime/src/coreclr/vm/clsload.cpp:2538
#14 0x00007fb91b15907c in ClassLoader::CreateTypeHandleForTypeDefThrowing (pModule=0x7fb89d05a420, cl=4, inst=..., pamTracker=0x7fffaef26e60)
    at /home/runtime/src/coreclr/vm/methodtablebuilder.cpp:12266
#15 0x00007fb91affc9de in ClassLoader::CreateTypeHandleForTypeKey (pKey=pKey@entry=0x7fffaef27158, pamTracker=pamTracker@entry=0x7fffaef26e60)
    at /home/runtime/src/coreclr/vm/typehandle.h:665
#16 0x00007fb91affd795 in ClassLoader::DoIncrementalLoad (pTypeKey=0x7fffaef27158, typeHnd=..., currentLevel=<optimized out>)
    at /home/runtime/src/coreclr/vm/clsload.cpp:2592
#17 ClassLoader::LoadTypeHandleForTypeKey_Body (this=this@entry=0x55e9f129b8e0, pTypeKey=pTypeKey@entry=0x7fffaef27158, typeHnd=..., 
    targetLevel=CLASS_LOAD_EXACTPARENTS) at /home/runtime/src/coreclr/vm/clsload.cpp:3259
#18 0x00007fb91aff9e9e in ClassLoader::LoadTypeHandleForTypeKey (this=0x55e9f129b8e0, pTypeKey=pTypeKey@entry=0x7fffaef27158, typeHnd=..., 
    targetLevel=targetLevel@entry=CLASS_LOADED, pInstContext=pInstContext@entry=0x0) at /home/runtime/src/coreclr/vm/clsload.cpp:2990
#19 0x00007fb91affab1c in ClassLoader::LoadTypeDefThrowing (pModule=0x7fb89d05a420, pModule@entry=0x0, typeDef=typeDef@entry=33554434, 
    fNotFoundAction=fNotFoundAction@entry=ClassLoader::ThrowIfNotFound, 
    fUninstantiated=fUninstantiated@entry=ClassLoader::FailIfUninstDefOrRef, tokenNotToLoad=tokenNotToLoad@entry=0, level=CLASS_LOAD_BEGIN, 
    level@entry=CLASS_LOADED, pTargetInstantiation=0x0) at /home/runtime/src/coreclr/vm/clsload.cpp:1969
#20 0x00007fb91affb86e in ClassLoader::LoadTypeDefOrRefThrowing (pModule=<optimized out>, typeDefOrRef=<optimized out>, 
    fNotFoundAction=ClassLoader::ThrowIfNotFound, fUninstantiated=ClassLoader::FailIfUninstDefOrRef, tokenNotToLoad=131072, level=19)
    at /home/runtime/src/coreclr/vm/clsload.cpp:2138
#21 0x00007fb91afdc794 in Assembly::GetEntryPoint (this=this@entry=0x55e9f12b4d00) at /home/runtime/src/coreclr/vm/assembly.cpp:1518
#22 0x00007fb91afdc233 in Assembly::ExecuteMainMethod (this=0x55e9f12b4d00, stringArgs=0x7fffaef27860, waitForOtherThreads=1)
    at /home/runtime/src/coreclr/vm/assembly.cpp:1382
#23 0x00007fb91b004b96 in CorHost2::ExecuteAssembly (this=<optimized out>, dwAppDomainId=<optimized out>, pwzAssemblyPath=<optimized out>, 
    argc=0, argv=0x0, pReturnValue=0x7fffaef27a1c) at /home/runtime/src/coreclr/vm/corhost.cpp:349
#24 0x00007fb91afca348 in coreclr_execute_assembly (hostHandle=0x55e9f12a8680, domainId=1, argc=0, argv=<optimized out>, 
    managedAssemblyPath=<optimized out>, exitCode=0x7fffaef27a1c) at /home/runtime/src/coreclr/dlls/mscoree/exports.cpp:495
#25 0x000055e9f09eb758 in run (config=...) at /home/runtime/src/coreclr/hosts/corerun/corerun.cpp:431
#26 main (argc=<optimized out>, argv=<optimized out>) at /home/runtime/src/coreclr/hosts/corerun/corerun.cpp:643

LoadedLayout creation is called from EnsureLoaded():

// "Module::Create" will initialize R2R support, if there is an R2R header.
// make sure the PE is loaded or R2R will be disabled.
pPEAssembly->EnsureLoaded();

Moreover, LoadedLayout is needed by further checks. I suppose this checks should use GetFlatLayout instead:
if (HasPEImage())
{
return GetLoadedLayout()->HasReadyToRunHeader();

@jkotas
Copy link
Member

jkotas commented Mar 4, 2025

I suppose this checks should use GetFlatLayout instead:

I think it should be GetOrCreateLayout(PEImageLayout::LAYOUT_ANY)

@jkotas
Copy link
Member

jkotas commented Mar 4, 2025

I think it should be GetOrCreateLayout(PEImageLayout::LAYOUT_ANY)

Or introduce a new GetAnyLayout method as a shortcut for this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-VM-coreclr community-contribution Indicates that the PR has been added by a community member
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants