11 #include "dotnet_launcher.h"
15 namespace dotnetcore {
17 CoreRuntime::CoreRuntime() :
21 PreparedFunction(nullptr),
22 LaunchFunction(nullptr),
23 InitializeClr(nullptr),
24 ExecuteAssembly(nullptr),
26 CreateDelegate(nullptr)
29 #define __STR(x) __XSTR(x)
32 DeviceAPIDirectory = __STR(DEVICE_API_DIR);
35 RuntimeDirectory = __STR(RUNTIME_DIR);
37 #ifdef CORECLR_LAUNCHER_ASSEMBLY_PATH
38 LauncherAssembly = __STR(CORECLR_LAUNCHER_ASSEMBLY_PATH);
44 _DBG("Constructor called!!");
47 CoreRuntime::~CoreRuntime()
52 int CoreRuntime::Initialize(bool standalone)
57 const char *_deviceapi_directory = getenv("DeviceAPIDirectory");
58 const char *_runtime_directory = getenv("RuntimeDirectory");
59 const char *_launcher_assembly = getenv("LauncherAssembly");
60 if (_deviceapi_directory != nullptr)
61 DeviceAPIDirectory = _deviceapi_directory;
62 if (_runtime_directory != nullptr)
63 RuntimeDirectory = _runtime_directory;
64 if (_launcher_assembly != nullptr)
65 LauncherAssembly = _launcher_assembly;
68 if (DeviceAPIDirectory.empty())
70 _ERR("Empty Device API Directory");
75 DeviceAPIDirectory = AbsolutePath(DeviceAPIDirectory);
77 if (RuntimeDirectory.empty())
79 _ERR("Empty Runtime Directory");
84 RuntimeDirectory = AbsolutePath(RuntimeDirectory);
86 if (LauncherAssembly.empty())
88 _ERR("Empty Launcher Assembly");
93 LauncherAssembly = AbsolutePath(LauncherAssembly);
96 std::string libcoreclr(ConcatPath(RuntimeDirectory, "libcoreclr.so"));
98 _DBG("libcoreclr : %s", libcoreclr.c_str());
100 coreclrLib = dlopen(libcoreclr.c_str(), RTLD_NOW | RTLD_LOCAL);
101 if (coreclrLib == nullptr)
103 char *err = dlerror();
104 _ERR("dlopen failed to open libcoreclr.so with error %s", err);
108 #define CORELIB_RETURN_IF_NOSYM(type, variable, name) \
109 do { variable = (type)dlsym(coreclrLib, name); \
110 if (variable == nullptr) { \
111 _ERR(name " is not found in the libcoreclr.so"); \
115 CORELIB_RETURN_IF_NOSYM(coreclr_initialize_ptr, InitializeClr, "coreclr_initialize");
116 CORELIB_RETURN_IF_NOSYM(coreclr_execute_assembly_ptr, ExecuteAssembly, "coreclr_execute_assembly");
117 CORELIB_RETURN_IF_NOSYM(coreclr_shutdown_ptr, Shutdown, "coreclr_shutdown");
118 CORELIB_RETURN_IF_NOSYM(coreclr_create_delegate_ptr, CreateDelegate, "coreclr_create_delegate");
120 #undef CORELIB_RETURN_IF_NOSYM
122 _DBG("libcoreclr dlopen and dlsym success");
124 _DBG("this addr : %x", this);
125 _DBG("coreclr_initialize : %x", InitializeClr);
130 int CoreRuntime::RunManagedLauncher()
133 unsigned int domainId;
135 if (FileNotExist(LauncherAssembly))
137 _ERR("Launcher assembly is not exist in %s", LauncherAssembly.c_str());
141 std::string launcherDir = Basename(LauncherAssembly);
142 std::vector<std::string> searchDirectories = {
143 RuntimeDirectory, DeviceAPIDirectory, launcherDir
146 std::string trusted_assemblies;
147 AssembliesInDirectory(searchDirectories, trusted_assemblies);
148 std::string trusted_directories = JoinStrings(searchDirectories, ":");
150 _DBG("coreclr_dir : %s", RuntimeDirectory.c_str());
151 _DBG("tpa_dirs : %s", trusted_directories.c_str());
152 _DBG("native_so_search_dir : %s", trusted_directories.c_str());
153 _DBG("launcher_assembly : %s", LauncherAssembly.c_str());
154 _DBG("launcher_dir : %s", launcherDir.c_str());
156 const char *propertyKeys[] =
158 "TRUSTED_PLATFORM_ASSEMBLIES",
161 "NATIVE_DLL_SEARCH_DIRECTORIES",
162 "AppDomainCompatSwitch"
165 const char *propertyValues[] =
167 trusted_assemblies.c_str(),
170 trusted_directories.c_str(),
171 "UseLatestBehaviorWhenTFMNotSpecified"
174 //_DBG("trusted platform assemblies : %s", propertyValues[0]);
175 _DBG("app_path : %s", propertyValues[1]);
176 _DBG("app_ni_path : %s", propertyValues[2]);
177 _DBG("native dll search path : %s", propertyValues[3]);
178 _DBG("app domain compat switch : %s", propertyValues[4]);
180 _DBG("before InitializeClr");
181 _DBG("this addr : %x", this);
182 _DBG("coreclr_initialize : %x", InitializeClr);
184 std::string selfPath = ReadSelfPath();
186 _DBG("self path : %s", selfPath.c_str());
188 _DBG("libcoreclr addr : %x", coreclrLib);
190 int st = InitializeClr(
192 //LauncherAssembly.c_str(),
194 sizeof(propertyKeys) / sizeof(propertyKeys[0]),
202 _ERR("initialize core clr fail! (0x%08x)", st);
206 _DBG("Initialize core clr success");
208 void *preparedFunctionDelegate;
209 st = CreateDelegate(hostHandle, domainId,
210 "Tizen.Runtime.Coreclr",
211 "Tizen.Runtime.Coreclr.AssemblyManager",
212 "Prepared", &preparedFunctionDelegate);
215 _ERR("Create delegate for Launch prepared function is fail (0x%08x)", st);
218 PreparedFunction = reinterpret_cast<PreparedFunctionPtr>(preparedFunctionDelegate);
220 if(PreparedFunction != nullptr)
225 void *launchFunctionDelegate;
226 st = CreateDelegate(hostHandle, domainId,
227 "Tizen.Runtime.Coreclr",
228 "Tizen.Runtime.Coreclr.AssemblyManager",
229 "Launch", &launchFunctionDelegate);
232 _ERR("Create delegate for Launch managed function is fail! (0x%08x)", st);
235 LaunchFunction = reinterpret_cast<LaunchFunctionPtr>(launchFunctionDelegate);
240 void CoreRuntime::Dispose()
242 if (hostHandle != nullptr)
244 int st = Shutdown(hostHandle, domainId);
247 _ERR("shutdown core clr fail! (0x%08x)", st);
251 if (dlclose(coreclrLib) != 0)
253 _ERR("libcoreclr.so close failed");
255 coreclrLib = nullptr;
257 _DBG("Dotnet runtime disposed");
260 int CoreRuntime::Launch(const char* root, const char* path, int argc, char* argv[])
264 _ERR("executable path is null");
268 if (FileNotExist(path))
270 _ERR("File not exist : %s", path);
274 bool success = false;
275 if (LaunchFunction != nullptr)
277 success = LaunchFunction(root, path, argc, argv);
280 _ERR("Failed to launch Application %s", path);
283 return success ? 0 : 1;
286 } // namespace dotnetcore
287 } // namespace runtime