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 bool CoreRuntime::InitializeCoreClr(const char* assembly_probe_paths, const char* pinvoke_probe_paths)
132 std::vector<std::string> platformDirectories = {
133 RuntimeDirectory, DeviceAPIDirectory
136 std::string trusted_assemblies;
137 AssembliesInDirectory(platformDirectories, trusted_assemblies);
139 const char *propertyKeys[] =
141 "TRUSTED_PLATFORM_ASSEMBLIES",
144 "NATIVE_DLL_SEARCH_DIRECTORIES",
145 "AppDomainCompatSwitch"
148 const char *propertyValues[] =
150 trusted_assemblies.c_str(),
151 assembly_probe_paths,
152 assembly_probe_paths,
154 "UseLatestBehaviorWhenTFMNotSpecified"
157 std::string selfPath = ReadSelfPath();
159 int st = InitializeClr(
162 sizeof(propertyKeys) / sizeof(propertyKeys[0]),
170 _ERR("initialize core clr fail! (0x%08x)", st);
174 _DBG("Initialize core clr success");
178 int CoreRuntime::RunManagedLauncher()
180 if (FileNotExist(LauncherAssembly))
182 _ERR("Launcher assembly is not exist in %s", LauncherAssembly.c_str());
186 std::string launcherDir = Basename(LauncherAssembly);
187 std::vector<std::string> searchDirectories = {
188 RuntimeDirectory, DeviceAPIDirectory
191 std::string trusted_directories = JoinStrings(searchDirectories, ":");
193 _DBG("coreclr_dir : %s", RuntimeDirectory.c_str());
194 _DBG("native_so_search_dir : %s", trusted_directories.c_str());
195 _DBG("launcher_assembly : %s", LauncherAssembly.c_str());
196 _DBG("launcher_dir : %s", launcherDir.c_str());
198 if (!InitializeCoreClr(launcherDir.c_str(), launcherDir.c_str()))
200 _ERR("Failed to initialize coreclr");
204 void *preparedFunctionDelegate;
205 int st = CreateDelegate(hostHandle, domainId,
206 "Tizen.Runtime.Coreclr",
207 "Tizen.Runtime.Coreclr.AssemblyManager",
208 "Prepared", &preparedFunctionDelegate);
211 _ERR("Create delegate for Launch prepared function is fail (0x%08x)", st);
214 PreparedFunction = reinterpret_cast<PreparedFunctionPtr>(preparedFunctionDelegate);
216 if(PreparedFunction != nullptr)
221 void *launchFunctionDelegate;
222 st = CreateDelegate(hostHandle, domainId,
223 "Tizen.Runtime.Coreclr",
224 "Tizen.Runtime.Coreclr.AssemblyManager",
225 "Launch", &launchFunctionDelegate);
228 _ERR("Create delegate for Launch managed function is fail! (0x%08x)", st);
231 LaunchFunction = reinterpret_cast<LaunchFunctionPtr>(launchFunctionDelegate);
236 void CoreRuntime::Dispose()
238 if (hostHandle != nullptr)
240 int st = Shutdown(hostHandle, domainId);
243 _ERR("shutdown core clr fail! (0x%08x)", st);
247 if (dlclose(coreclrLib) != 0)
249 _ERR("libcoreclr.so close failed");
251 coreclrLib = nullptr;
253 _DBG("Dotnet runtime disposed");
256 int CoreRuntime::Launch(const char* root, const char* path, int argc, char* argv[])
260 _ERR("executable path is null");
264 if (FileNotExist(path))
266 _ERR("File not exist : %s", path);
270 bool success = false;
271 if (LaunchFunction != nullptr)
273 success = LaunchFunction(root, path, argc, argv);
276 _ERR("Failed to launch Application %s", path);
278 return success ? 0 : 1;
282 std::string appRoot = root;
283 std::string appBin = ConcatPath(appRoot, "bin");
284 std::string appLib = ConcatPath(appRoot, "lib");
285 std::string probePath = appBin + ":" + appLib;
287 int st = InitializeCoreClr(probePath.c_str(), probePath.c_str());
288 unsigned int ret = 0;
289 st = ExecuteAssembly(hostHandle, domainId, argc, (const char**)argv, path, &ret);
292 _ERR("Failed to Execute Assembly %s (0x%08x)", path, st);
298 } // namespace dotnetcore
299 } // namespace runtime