Skip to content

Commit

Permalink
initialize runtime at start of reverse PInvoke, unix dlopen successful (
Browse files Browse the repository at this point in the history
  • Loading branch information
tonerdo committed Aug 5, 2017
1 parent a159b9b commit b096c3d
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 24 deletions.
89 changes: 65 additions & 24 deletions src/Native/Bootstrap/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#include "gcenv.structs.h"
#include "gcenv.base.h"

#include <stdlib.h>
#include <stdlib.h>

#ifndef CPPCODEGEN

Expand Down Expand Up @@ -258,7 +258,13 @@ static const pfn c_classlibFunctions[] = {

#endif // !CPPCODEGEN

static bool RUNTIME_INITIALIZED = false;
extern "C" void InitializeModules(void* osModule, void ** modules, int count, void ** pClasslibFunctions, int nClasslibFunctions);
extern "C" int InitializeRuntime();

#ifdef CPPCODEGEN
ReversePInvokeFrame frame;
#endif

#ifdef CORERT_DLL
#if defined(_WIN32)
Expand All @@ -269,13 +275,60 @@ extern "C" void InitializeModules(void* osModule, void ** modules, int count, vo
#endif // CORERT_DLL

#if defined(_WIN32)
#ifndef CORERT_DLL
extern "C" int __managed__Main(int argc, wchar_t* argv[]);
#else
extern "C" int __managed__Startup(int argc, wchar_t* argv[]);
#endif // CORERT_DLL
int wmain(int argc, wchar_t* argv[])
#else
#ifndef CORERT_DLL
extern "C" int __managed__Main(int argc, char* argv[]);
#else
extern "C" int __managed__Startup(int argc, char* argv[]);
#endif // CORERT_DLL
int main(int argc, char* argv[])
#endif
{
int initval = InitializeRuntime();
if (initval != 0)
return initval;

int retval;
#ifdef CPPCODEGEN
try
#endif
{
#ifndef CORERT_DLL
retval = __managed__Main(argc, argv);
#else
retval = 0;
#endif // CORERT_DLL
}
#ifdef CPPCODEGEN
catch (const char* &e)
{
printf("Call to an unimplemented runtime method; execution cannot continue.\n");
printf("Method: %s\n", e);
retval = -1;
}
#endif

#ifdef CPPCODEGEN
__reverse_pinvoke_return(&frame);
#endif

RhpShutdown();
RUNTIME_INITIALIZED = false;

return retval;
}

extern "C" int InitializeRuntime()
{
if (RUNTIME_INITIALIZED)
return 0;

if (!RhInitialize())
return -1;

Expand All @@ -284,7 +337,11 @@ int main(int argc, char* argv[])
#endif // CPPCODEGEN

#ifndef CPPCODEGEN
#ifndef CORERT_DLL
void * osModule = PalGetModuleHandleFromPointer((void*)&__managed__Main);
#else
void * osModule = PalGetModuleHandleFromPointer((void*)&__managed__Startup);
#endif // CORERT_DLL
if (!RhRegisterOSModule(
osModule,
(void*)&__managedcode_a, (uint32_t)((char *)&__managedcode_z - (char*)&__managedcode_a),
Expand All @@ -294,8 +351,9 @@ int main(int argc, char* argv[])
}
#endif // !CPPCODEGEN

RUNTIME_INITIALIZED = true;

#ifdef CPPCODEGEN
ReversePInvokeFrame frame;
__reverse_pinvoke(&frame);
#endif

Expand All @@ -305,27 +363,10 @@ int main(int argc, char* argv[])
InitializeModules(nullptr, (void**)RtRHeaderWrapper(), 2, nullptr, 0);
#endif // !CPPCODEGEN

int retval;
#ifdef CPPCODEGEN
try
#endif
{
retval = __managed__Main(argc, argv);
}
#ifdef CPPCODEGEN
catch (const char* &e)
{
printf("Call to an unimplemented runtime method; execution cannot continue.\n");
printf("Method: %s\n", e);
retval = -1;
}
#endif

#ifdef CPPCODEGEN
__reverse_pinvoke_return(&frame);
#endif

RhpShutdown();
#ifdef CORERT_DLL
// Run startup method immediately for a native library
return __managed__Startup(0, NULL);
#endif // CORERT_DLL

return retval;
return 0;
}
6 changes: 6 additions & 0 deletions src/Native/Runtime/thread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1268,9 +1268,15 @@ EXTERN_C NOINLINE void FASTCALL RhpReversePInvokeAttachOrTrapThread2(ReversePInv
// PInvoke
//

extern "C" int InitializeRuntime();

// Standard calling convention variant of RhpReversePInvoke
COOP_PINVOKE_HELPER(void, RhpReversePInvoke2, (ReversePInvokeFrame * pFrame))
{
// Entrypoint for reverse PInvoke
// ensure runtime is initialized
InitializeRuntime();

Thread * pCurThread = ThreadStore::RawGetCurrentThread();
pFrame->m_savedThread = pCurThread;
if (pCurThread->InlineTryFastReversePInvoke(pFrame))
Expand Down

0 comments on commit b096c3d

Please sign in to comment.