Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/coreclr/inc/CrstTypes.def
Original file line number Diff line number Diff line change
Expand Up @@ -528,4 +528,10 @@ End

Crst PregeneratedStringThunks
AcquiredBefore UnresolvedClassLock
End

// Guards the de-duplication set used when relocating host-probed webcil (WASM) R2R images.
// It is a leaf that acquires no other Crst while held, so it is Unordered.
Crst WebcilImageRelocation
Unordered
End
7 changes: 5 additions & 2 deletions src/coreclr/inc/crsttypes_generated.h
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,9 @@ enum CrstType
CrstUnwindInfoTablePendingLock = 100,
CrstUnwindInfoTablePublishLock = 101,
CrstVSDIndirectionCellLock = 102,
CrstWrapperTemplate = 103,
kNumberOfCrstTypes = 104
CrstWebcilImageRelocation = 103,
CrstWrapperTemplate = 104,
kNumberOfCrstTypes = 105
};

#endif // __CRST_TYPES_INCLUDED
Expand Down Expand Up @@ -233,6 +234,7 @@ int g_rgCrstLevelMap[] =
2, // CrstUnwindInfoTablePendingLock
3, // CrstUnwindInfoTablePublishLock
3, // CrstVSDIndirectionCellLock
-1, // CrstWebcilImageRelocation
3, // CrstWrapperTemplate
};

Expand Down Expand Up @@ -342,6 +344,7 @@ LPCSTR g_rgCrstNameMap[] =
"CrstUnwindInfoTablePendingLock",
"CrstUnwindInfoTablePublishLock",
"CrstVSDIndirectionCellLock",
"CrstWebcilImageRelocation",
"CrstWrapperTemplate",
};

Expand Down
8 changes: 7 additions & 1 deletion src/coreclr/inc/webcildecoder.h
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,13 @@ class WebcilDecoder
// ------------------------------------------------------------

BOOL HasReadyToRunHeader() const;
BOOL IsComponentAssembly() const { return FALSE; }
BOOL IsComponentAssembly() const
{
// A webcil composite's component assemblies carry the R2R COMPONENT flag; the flat FALSE
// default would make PEImageLayout::IsComponentAssembly() (routed here via DECODER_DISPATCH)
// report every webcil image as non-component, disabling composite R2R load.
return HasReadyToRunHeader() && (GetReadyToRunHeader()->CoreHeader.Flags & READYTORUN_FLAG_COMPONENT) != 0;
}
Comment thread
lewing marked this conversation as resolved.
Resolved
READYTORUN_HEADER *GetReadyToRunHeader() const;
BOOL IsNativeMachineFormat() const { return true; } // This can only be loaded on a Wasm runtime which matches the necessary load environment, which means these are always in the native machine format.
PTR_CVOID GetNativeManifestMetadata(COUNT_T *pSize) const;
Expand Down
9 changes: 9 additions & 0 deletions src/coreclr/vm/nativeimage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,16 @@ namespace
}
}

#ifdef TARGET_WASM
// On WebAssembly the runtime only loads flat webcil composites, which do not expose a named
// "RTR_HEADER" export the way PE R2R images do; obtain the R2R header from the decoder instead.
// PE R2R images (which rely on the export) cannot be loaded on WASM, so the export path is never
// taken here. A genuinely non-R2R image still fails validation below via the NULL header check.
if (peLoadedImage->HasReadyToRunHeader())
*header = peLoadedImage->GetReadyToRunHeader();
#else // TARGET_WASM
*header = (READYTORUN_HEADER *)peLoadedImage->GetExport("RTR_HEADER");
#endif // TARGET_WASM
if (*header == NULL)
{
COMPlusThrowHR(COR_E_BADIMAGEFORMAT);
Expand Down
4 changes: 4 additions & 0 deletions src/coreclr/vm/peimage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ void PEImage::Startup()
s_ijwFixupDataHash = ::new PtrHashMap;
s_ijwFixupDataHash->Init(CompareIJWDataBase, FALSE, &ijwLock);

#ifdef TARGET_WASM
PEImageLayout::Startup();
#endif // TARGET_WASM

RETURN;
}

Expand Down
43 changes: 43 additions & 0 deletions src/coreclr/vm/peimagelayout.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,19 @@ static bool AllowR2RForImage(PEImage* pOwner)
#ifndef DACCESS_COMPILE
extern BOOL g_useDefaultBaseAddr;

#ifdef TARGET_WASM
// Guards s_relocatedWebcilBases in ApplyBaseRelocations so concurrent loads of the same shared
// host-probed webcil buffer cannot relocate it more than once. Initialized by PEImageLayout::Startup.
static CrstStatic s_webcilRelocationCrst;

/*static*/
void PEImageLayout::Startup()
{
WRAPPER_NO_CONTRACT;
s_webcilRelocationCrst.Init(CrstWebcilImageRelocation);
}
#endif // TARGET_WASM

PEImageLayout* PEImageLayout::CreateFromByteArray(PEImage* pOwner, const BYTE* array, COUNT_T size)
{
STANDARD_VM_CONTRACT;
Expand Down Expand Up @@ -248,6 +261,34 @@ void PEImageLayout::ApplyBaseRelocations(bool relocationMustWriteCopy)
SSIZE_T tableBaseDelta = GetTableBaseOffset();
#endif // FEATURE_WEBCIL

#ifdef TARGET_WASM
// Host-probed webcil R2R images share one in-memory buffer, so the binder can open the same
// image twice (identity probe + load). WASM table-index relocations are additive
// (index += tableBase), so relocating the shared buffer twice doubles tableBase and corrupts
// indirect calls. Track the buffers already relocated and skip re-relocation.
//
// The lock is taken before the Contains check and held for the rest of the method so that, when
// WASM is built with threads (FEATURE_MULTITHREADING), two concurrent loads of the same shared
// buffer cannot both pass the check and relocate it twice.
//
// INTERIM: the intended production fix is to relocate the host-probed buffer once at the point the
// host hands it to the runtime and skip webcil relocation here entirely, removing this set.
typedef SetSHash< TADDR,
Comment thread
lewing marked this conversation as resolved.
Resolved
NoRemoveSHashTraits< NonDacAwareSHashTraits< SetSHashTraits<TADDR> > > > RelocatedWebcilSet;
static RelocatedWebcilSet s_relocatedWebcilBases;
Comment thread
lewing marked this conversation as resolved.
Resolved
CrstHolder relocationGuard(&s_webcilRelocationCrst);
if (IsWebcilFormat())
{
const TADDR webcilBase = (TADDR)GetBase();
if (s_relocatedWebcilBases.Contains(webcilBase))
return;
// Record before applying: once we commit to relocating this shared buffer, no other load may
// additively relocate it again. A failure partway through relocation is unrecoverable for the
// additive model and fails the load regardless, so there is no "safe" point to record instead.
s_relocatedWebcilBases.Add(webcilBase);
}
#endif // TARGET_WASM
Comment thread
lewing marked this conversation as resolved.
Resolved

// Nothing to do - image is loaded at preferred base and no table base offset
if (delta == 0
#ifdef FEATURE_WEBCIL
Expand Down Expand Up @@ -306,6 +347,8 @@ void PEImageLayout::ApplyBaseRelocations(bool relocationMustWriteCopy)

DWORD rva = VAL32(r->VirtualAddress);

// For webcil (wasm-only) this relies on the flat-mapped invariant PointerToRawData ==
// VirtualAddress, so GetBase() + rva is the correct address (equivalent to GetRvaData(rva)).
BYTE * pageAddress = (BYTE *)GetBase() + rva;

// Check whether the page is outside the unprotected region.
Expand Down
4 changes: 4 additions & 0 deletions src/coreclr/vm/peimagelayout.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ class PEImageLayout

public:
#ifndef DACCESS_COMPILE
#ifdef TARGET_WASM
// One-time initialization of the lock guarding webcil relocation de-duplication.
static void Startup();
#endif // TARGET_WASM
static PEImageLayout* CreateFromByteArray(PEImage* pOwner, const BYTE* array, COUNT_T size);
#ifndef TARGET_UNIX
static PEImageLayout* CreateFromHMODULE(HMODULE hModule,PEImage* pOwner);
Expand Down
2 changes: 1 addition & 1 deletion src/coreclr/vm/peimagelayout.inl
Original file line number Diff line number Diff line change
Expand Up @@ -637,7 +637,7 @@ inline PTR_CVOID PEImageLayout::GetNativeManifestMetadata(COUNT_T *pSize) const
inline BOOL PEImageLayout::IsComponentAssembly() const
{
WRAPPER_NO_CONTRACT;
PE_OR_WEBCIL(IsComponentAssembly(), FALSE)
DECODER_DISPATCH(IsComponentAssembly())
}

inline BOOL PEImageLayout::HasReadyToRunHeader() const
Expand Down
9 changes: 8 additions & 1 deletion src/coreclr/vm/readytoruninfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -537,7 +537,14 @@ static NativeImage *AcquireCompositeImage(Module * pModule, PEImageLayout * pLay
return NULL;

LPCUTF8 ownerCompositeExecutableName = NULL;
if (pLayout->IsMapped())
if (pLayout->IsWebcilFormat())
{
// Webcil is wasm-only and flat-mapped by construction (PointerToRawData == VirtualAddress),
// so this is equivalent to GetBase() + virtualAddress; use the decoder's GetRvaData as the
// format-correct idiom for resolving an RVA.
ownerCompositeExecutableName = (LPCUTF8)pLayout->GetRvaData(virtualAddress);
}
else if (pLayout->IsMapped())
{
ownerCompositeExecutableName = (LPCUTF8)pLayout->GetBase() + virtualAddress;
}
Expand Down
Loading