Add setup of COMPlus_UseDefaultBaseAddr environment variable
[platform/core/dotnet/launcher.git] / NativeLauncher / launcher / dotnet / dotnet_launcher.cc
index 6a873c2..57b6005 100644 (file)
@@ -45,9 +45,6 @@
 #include "path_manager.h"
 #include "log_manager.h"
 
-#define PLUGIN_PATH "/usr/share/dotnet.tizen/lib/libdotnet_plugin.so"
-#define ENV_FILE_PATH "/usr/share/dotnet.tizen/lib/coreclr_env.list"
-
 namespace tizen {
 namespace runtime {
 namespace dotnetcore {
@@ -224,12 +221,30 @@ static void initEnvForSpecialFolder()
        }
 }
 
-static std::u16string utf8ToUtf16(char* str)
+void CoreRuntime::preloadTypes()
 {
-       std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t> convert;
-       return convert.from_bytes(str);
-}
+       const static std::string initDllPath = "/usr/share/dotnet.tizen/framework/Tizen.Init.dll";
+       if (!isFileExist(initDllPath)) {
+               _ERR("Failed to locate Tizen.Init.dll");
+               return;
+       }
+
+       typedef void (*InitDelegate)();
+       InitDelegate initDelegate;
 
+       int ret = createDelegate(__hostHandle,
+               __domainId,
+               "Tizen.Init",
+               "Tizen.Init.TypeLoader",
+               "PreloadTypes",
+               (void**)&initDelegate);
+
+       if (ret < 0) {
+               _ERR("Failed to create delegate for PreloadTypes (0x%08x)", ret);
+       } else {
+               initDelegate();
+       }
+}
 
 CoreRuntime::CoreRuntime(const char* mode) :
        initializeClr(nullptr),
@@ -267,8 +282,7 @@ CoreRuntime::~CoreRuntime()
 
 int CoreRuntime::initialize(bool standalone)
 {
-       // checkInjection checks dotnet-launcher run mode,
-       // if it contains DOTNET_LAUNCHER_INJECT variable, it injects library.
+       // checkInjection checks dotnet-launcher run mode
        // At the moment, this mechanism is used only when the Memory Profiler is started.
        int res = checkInjection();
        if (res != 0) {
@@ -306,6 +320,10 @@ int CoreRuntime::initialize(bool standalone)
        // Write Debug.WriteLine to stderr
        putenv(const_cast<char *>("COMPlus_DebugWriteToStdErr=1"));
 
+#ifdef USE_DEFAULT_BASE_ADDR
+       putenv(const_cast<char *>("COMPlus_UseDefaultBaseAddr=1"));
+#endif // USE_DEFAULT_BASE_ADDR
+
        // read string from external file and set them to environment value.
        setEnvFromFile();
 
@@ -356,7 +374,6 @@ int CoreRuntime::initialize(bool standalone)
        CORELIB_RETURN_IF_NOSYM(coreclr_execute_assembly_ptr, executeAssembly, "coreclr_execute_assembly");
        CORELIB_RETURN_IF_NOSYM(coreclr_shutdown_ptr, shutdown, "coreclr_shutdown");
        CORELIB_RETURN_IF_NOSYM(coreclr_create_delegate_ptr, createDelegate, "coreclr_create_delegate");
-       CORELIB_RETURN_IF_NOSYM(set_environment_variable_ptr, setEnvironmentVariable, "SetEnvironmentVariableW");
 
 #undef CORELIB_RETURN_IF_NOSYM
 
@@ -369,19 +386,32 @@ int CoreRuntime::initialize(bool standalone)
        std::string appRoot = std::string("/proc/self/fd/") + std::to_string(fd);
        std::string appBin = concatPath(appRoot, "bin");
        std::string appLib = concatPath(appRoot, "lib");
-       std::string appTAC = concatPath(appBin, "TAC.Release");
-       std::string probePath = appBin + ":" + appLib + ":" + appTAC;
+       std::string appTac = concatPath(appBin, TAC_SYMLINK_SUB_DIR);
+       std::string probePath = appBin + ":" + appLib + ":" + appTac;
+       std::string NIprobePath = concatPath(appBin, APP_NI_SUB_DIR) + ":" + concatPath(appLib, APP_NI_SUB_DIR) + ":" + appTac;
        std::string tpa = getTPA();
-       std::string nativeLibPath = getExtraNativeLibDirs(appRoot) + ":" + appBin + ":" + appLib + ":" + __nativeLibDirectory;
+       std::string runtimeDir = getRuntimeDir();
+       std::string nativeLibPath = getExtraNativeLibDirs(appRoot) + ":" + appBin + ":" + appLib + ":" + __nativeLibDirectory + ":" + runtimeDir;
        std::string appName = std::string("dotnet-launcher-") + std::to_string(getpid());
 
-       if (!initializeCoreClr(appName.c_str(), probePath.c_str(), nativeLibPath.c_str(), tpa.c_str())) {
+       if (!initializeCoreClr(appName.c_str(), probePath.c_str(), NIprobePath.c_str(), nativeLibPath.c_str(), tpa.c_str())) {
                _ERR("Failed to initialize coreclr");
                return -1;
        }
 
+       int st = createDelegate(__hostHandle, __domainId, "Dotnet.Launcher", "Dotnet.Launcher.Environment", "SetEnvironmentVariable", (void**)&setEnvironmentVariable);
+       if (st < 0 || setEnvironmentVariable == nullptr) {
+               _ERR("Create delegate for Dotnet.Launcher.dll -> Dotnet.Launcher.Environment -> SetEnvironmentVariable failed (0x%08x)", st);
+               return -1;
+       }
+
        __initialized = true;
 
+       if (!standalone)
+       {
+               preloadTypes();         // Preload common managed code
+       }
+
        _INFO("CoreRuntime initialize success");
 
        return 0;
@@ -389,6 +419,7 @@ int CoreRuntime::initialize(bool standalone)
 
 bool CoreRuntime::initializeCoreClr(const char* appId,
                                                                         const char* assemblyProbePaths,
+                                                                        const char* NIProbePaths,
                                                                         const char* pinvokeProbePaths,
                                                                         const char* tpaList)
 {
@@ -403,7 +434,7 @@ bool CoreRuntime::initializeCoreClr(const char* appId,
        const char *propertyValues[] = {
                tpaList,
                assemblyProbePaths,
-               assemblyProbePaths,
+               NIProbePaths,
                pinvokeProbePaths,
                "UseLatestBehaviorWhenTFMNotSpecified"
        };
@@ -500,12 +531,7 @@ int CoreRuntime::launch(const char* appId, const char* root, const char* path, i
        // application data path can be changed by owner. So, we have to set data path just before launching.
        char* localDataPath = app_get_data_path();
        if (localDataPath != nullptr) {
-               std::u16string envval = utf8ToUtf16(localDataPath);
-
-               if (!setEnvironmentVariable(u"XDG_DATA_HOME", envval.c_str())) {
-                       _ERR("Failed to set XDG_DATA_HOME");
-               }
-
+               setEnvironmentVariable("XDG_DATA_HOME", localDataPath);
                free(localDataPath);
        }