Skip to content

Commit bdd12cc

Browse files
authored
Fix embedded coreclr detection in corhost.cpp (#80294)
The `CORECLR_EMBEDDED` define that is used in corhost.cpp to detect whether the current host has coreclr and some other native libraries embedded or not doesn't work. The reason is that corhost.cpp is not compiled separately for the cases of embedded and non-embedded coreclr. The proper way is to check the `g_coreclr_embedded` global variable that is defined in the ceemain.cpp, which is compiled separately for those two cases. While we could also make the corhost.cpp build twice, it would be a waste of time.
1 parent 87312f7 commit bdd12cc

File tree

3 files changed

+24
-21
lines changed

3 files changed

+24
-21
lines changed

src/coreclr/dlls/mscoree/exports.cpp

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include <utilcode.h>
1515
#include <corhost.h>
1616
#include <configuration.h>
17+
#include "../../vm/ceemain.h"
1718
#ifdef FEATURE_GDBJIT
1819
#include "../../vm/gdbjithelpers.h"
1920
#endif // FEATURE_GDBJIT
@@ -27,12 +28,6 @@
2728
// Holder for const wide strings
2829
typedef NewArrayHolder<const WCHAR> ConstWStringHolder;
2930

30-
// Specifies whether coreclr is embedded or standalone
31-
extern bool g_coreclr_embedded;
32-
33-
// Specifies whether hostpolicy is embedded in executable or standalone
34-
extern bool g_hostpolicy_embedded;
35-
3631
// Holder for array of wide strings
3732
class ConstWStringArrayHolder : public NewArrayHolder<LPCWSTR>
3833
{

src/coreclr/vm/ceemain.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,5 +54,10 @@ INT32 GetLatchedExitCode (void);
5454
// Stronger than IsGCHeapInitialized
5555
BOOL IsGarbageCollectorFullyInitialized();
5656

57+
// Specifies whether coreclr is embedded or standalone
58+
extern bool g_coreclr_embedded;
59+
60+
// Specifies whether hostpolicy is embedded in executable or standalone
61+
extern bool g_hostpolicy_embedded;
5762

5863
#endif

src/coreclr/vm/corhost.cpp

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -630,23 +630,26 @@ HRESULT CorHost2::CreateAppDomainWithManager(
630630
sAppPaths));
631631
}
632632

633-
#if defined(TARGET_UNIX) && !defined(CORECLR_EMBEDDED)
634-
// Check if the current code is executing in the single file host or in libcoreclr.so. The libSystem.Native is linked
635-
// into the single file host, so we need to check only when this code is in libcoreclr.so.
636-
// Preload the libSystem.Native.so/dylib to detect possible problems with loading it early
637-
EX_TRY
638-
{
639-
NativeLibrary::LoadLibraryByName(W("libSystem.Native"), SystemDomain::SystemAssembly(), FALSE, 0, TRUE);
640-
}
641-
EX_HOOK
633+
#if defined(TARGET_UNIX)
634+
if (!g_coreclr_embedded)
642635
{
643-
Exception *ex = GET_EXCEPTION();
644-
SString err;
645-
ex->GetMessage(err);
646-
LogErrorToHost("Error message: %s", err.GetUTF8());
636+
// Check if the current code is executing in the single file host or in libcoreclr.so. The libSystem.Native is linked
637+
// into the single file host, so we need to check only when this code is in libcoreclr.so.
638+
// Preload the libSystem.Native.so/dylib to detect possible problems with loading it early
639+
EX_TRY
640+
{
641+
NativeLibrary::LoadLibraryByName(W("libSystem.Native"), SystemDomain::SystemAssembly(), FALSE, 0, TRUE);
642+
}
643+
EX_HOOK
644+
{
645+
Exception *ex = GET_EXCEPTION();
646+
SString err;
647+
ex->GetMessage(err);
648+
LogErrorToHost("Error message: %s", err.GetUTF8());
649+
}
650+
EX_END_HOOK;
647651
}
648-
EX_END_HOOK;
649-
#endif // TARGET_UNIX && !CORECLR_EMBEDDED
652+
#endif // TARGET_UNIX
650653

651654
*pAppDomainID=DefaultADID;
652655

0 commit comments

Comments
 (0)