Skip to content

Commit

Permalink
Allow R2R for images provided via external_assembly_probe (#112934)
Browse files Browse the repository at this point in the history
Allow R2R for images provided via `external_assembly_probe` This re-uses the existing mechanism from single-file with R2R, where we load the image by copying it. This is obviously inefficient, but allows a first version of functioning R2R for Android.
  • Loading branch information
elinor-fung authored Feb 28, 2025
1 parent 949ad0f commit 77115c3
Showing 1 changed file with 20 additions and 5 deletions.
25 changes: 20 additions & 5 deletions src/coreclr/vm/peimagelayout.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,20 @@ extern "C"
}
#endif

static bool AllowR2RForImage(PEImage* pOwner)
{
// Allow R2R for files
if (pOwner->IsFile())
return true;

// Allow R2R for externally provided images
INT64 size;
if (pOwner->GetExternalData(&size) != NULL && size > 0)
return true;

return false;
}

#ifndef DACCESS_COMPILE
PEImageLayout* PEImageLayout::CreateFromByteArray(PEImage* pOwner, const BYTE* array, COUNT_T size)
{
Expand Down Expand Up @@ -70,9 +84,11 @@ PEImageLayout* PEImageLayout::LoadConverted(PEImage* pOwner, bool disableMapping
pFlat = (FlatImageLayout*)pOwner->GetFlatLayout();
pFlat->AddRef();
}
else if (pOwner->IsFile())
else if (AllowR2RForImage(pOwner))
{
// We only expect to be converting images that aren't already opened in the R2R composite case
pFlat = new FlatImageLayout(pOwner);
_ASSERTE(pFlat->HasReadyToRunHeader());
}

if (pFlat == NULL || !pFlat->CheckILOnlyFormat())
Expand All @@ -88,9 +104,8 @@ PEImageLayout* PEImageLayout::LoadConverted(PEImage* pOwner, bool disableMapping
_ASSERTE(!pOwner->IsFile() || !pFlat->HasReadyToRunHeader() || disableMapping);
#endif

// ignore R2R if the image is not a file.
if ((pFlat->HasReadyToRunHeader() && pOwner->IsFile()) ||
pFlat->HasWriteableSections())
if ((pFlat->HasReadyToRunHeader() && AllowR2RForImage(pOwner))
|| pFlat->HasWriteableSections())
{
return new ConvertedImageLayout(pFlat, disableMapping);
}
Expand Down Expand Up @@ -462,7 +477,7 @@ ConvertedImageLayout::ConvertedImageLayout(FlatImageLayout* source, bool disable

IfFailThrow(Init(loadedImage));

if (m_pOwner->IsFile() && IsNativeMachineFormat())
if (AllowR2RForImage(m_pOwner) && IsNativeMachineFormat())
{
// Do base relocation and exception hookup, if necessary.
// otherwise R2R will be disabled for this image.
Expand Down

0 comments on commit 77115c3

Please sign in to comment.