2 * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the License);
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an AS IS BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
27 #include "dotnet_launcher.h"
29 #define PLUGIN_PATH "/usr/share/dotnet.tizen/lib/libdotnet_plugin.so"
33 namespace dotnetcore {
35 CoreRuntime::CoreRuntime() :
36 initializeClr(nullptr),
37 executeAssembly(nullptr),
39 createDelegate(nullptr),
40 __coreclrLib(nullptr),
41 __hostHandle(nullptr),
43 preparedFunction(nullptr),
44 launchFunction(nullptr),
46 pluginInitialize(nullptr),
47 pluginPreload(nullptr),
48 pluginSetAppInfo(nullptr),
49 pluginSetCoreclrInfo(nullptr),
50 pluginGetDllPath(nullptr),
51 pluginBeforeExecute(nullptr),
52 pluginFinalize(nullptr)
55 #define __STR(x) __XSTR(x)
58 __deviceAPIDirectory = __STR(DEVICE_API_DIR);
61 __runtimeDirectory = __STR(RUNTIME_DIR);
64 __nativeLibDirectory = __STR(NATIVE_LIB_DIR);
67 #ifdef USE_MANAGED_LAUNCHER
68 #ifdef CORECLR_LAUNCHER_ASSEMBLY_PATH
69 __launcherAssembly = __STR(CORECLR_LAUNCHER_ASSEMBLY_PATH);
76 // support launcher plugin
77 if (!fileNotExist(PLUGIN_PATH)) {
78 __pluginLib = dlopen(PLUGIN_PATH, RTLD_NOW | RTLD_LOCAL);
80 pluginInitialize = (plugin_initialize_ptr)dlsym(__pluginLib, "plugin_initialize");
81 pluginPreload = (plugin_preload_ptr)dlsym(__pluginLib, "plugin_preload");
82 pluginSetAppInfo = (plugin_set_app_info_ptr)dlsym(__pluginLib, "plugin_set_app_info");
83 pluginSetCoreclrInfo = (plugin_set_coreclr_info_ptr)dlsym(__pluginLib, "plugin_set_coreclr_info");
84 pluginGetDllPath = (plugin_get_dll_path_ptr)dlsym(__pluginLib, "plugin_get_dll_path");
85 pluginBeforeExecute = (plugin_before_execute_ptr)dlsym(__pluginLib, "plugin_before_execute");
86 pluginFinalize = (plugin_finalize_ptr)dlsym(__pluginLib, "plugin_finalize");
93 _DBG("Constructor called!!");
96 CoreRuntime::~CoreRuntime()
101 int CoreRuntime::initialize(bool standalone)
104 // libunwind library is used to unwind stack frame, but libunwind for ARM
105 // does not support ARM vfpv3/NEON registers in DWARF format correctly.
106 // Therefore let's disable stack unwinding using DWARF information
107 // See https://github.com/dotnet/coreclr/issues/6698
109 // libunwind use following methods to unwind stack frame.
110 // UNW_ARM_METHOD_ALL 0xFF
111 // UNW_ARM_METHOD_DWARF 0x01
112 // UNW_ARM_METHOD_FRAME 0x02
113 // UNW_ARM_METHOD_EXIDX 0x04
114 putenv(const_cast<char *>("UNW_ARM_UNWIND_METHOD=6"));
117 if (__deviceAPIDirectory.empty()) {
118 _ERR("Empty Device API Directory");
121 __deviceAPIDirectory = absolutePath(__deviceAPIDirectory);
124 if (__runtimeDirectory.empty()) {
125 _ERR("Empty Runtime Directory");
128 __runtimeDirectory = absolutePath(__runtimeDirectory);
131 // set Reference API directory
132 __refAPIDirectory = __deviceAPIDirectory + "/ref";
134 #ifdef USE_MANAGED_LAUNCHER
135 if (__launcherAssembly.empty()) {
136 _ERR("Empty Launcher Assembly");
139 __launcherAssembly = absolutePath(__launcherAssembly);
143 std::string libCoreclr(concatPath(__runtimeDirectory, "libcoreclr.so"));
145 _DBG("libcoreclr : %s", libCoreclr.c_str());
147 __coreclrLib = dlopen(libCoreclr.c_str(), RTLD_NOW | RTLD_LOCAL);
148 if (__coreclrLib == nullptr) {
149 char *err = dlerror();
150 _ERR("dlopen failed to open libcoreclr.so with error %s", err);
154 #define CORELIB_RETURN_IF_NOSYM(type, variable, name) \
156 variable = (type)dlsym(__coreclrLib, name); \
157 if (variable == nullptr) { \
158 _ERR(name " is not found in the libcoreclr.so"); \
163 CORELIB_RETURN_IF_NOSYM(coreclr_initialize_ptr, initializeClr, "coreclr_initialize");
164 CORELIB_RETURN_IF_NOSYM(coreclr_execute_assembly_ptr, executeAssembly, "coreclr_execute_assembly");
165 CORELIB_RETURN_IF_NOSYM(coreclr_shutdown_ptr, shutdown, "coreclr_shutdown");
166 CORELIB_RETURN_IF_NOSYM(coreclr_create_delegate_ptr, createDelegate, "coreclr_create_delegate");
168 #undef CORELIB_RETURN_IF_NOSYM
170 _DBG("libcoreclr dlopen and dlsym success");
172 if (!standalone && pluginPreload)
178 bool CoreRuntime::initializeCoreClr(const char* appId,
179 const char* assemblyProbePaths,
180 const char* pinvokeProbePaths,
183 const char *propertyKeys[] = {
184 "TRUSTED_PLATFORM_ASSEMBLIES",
187 "NATIVE_DLL_SEARCH_DIRECTORIES",
188 "AppDomainCompatSwitch"
191 const char *propertyValues[] = {
196 "UseLatestBehaviorWhenTFMNotSpecified"
199 std::string selfPath = readSelfPath();
201 int st = initializeClr(selfPath.c_str(),
203 sizeof(propertyKeys) / sizeof(propertyKeys[0]),
210 _ERR("initialize core clr fail! (0x%08x)", st);
214 if (pluginSetCoreclrInfo)
215 pluginSetCoreclrInfo(__hostHandle, __domainId);
217 _DBG("Initialize core clr success");
221 int CoreRuntime::runManagedLauncher(const char* appId, const char* appBase, const char* tpaList)
223 if (fileNotExist(__launcherAssembly)) {
224 _ERR("Launcher assembly is not exist in %s", __launcherAssembly.c_str());
228 if (!initializeCoreClr(appId, appBase, appBase, tpaList)) {
229 _ERR("Failed to initialize coreclr");
233 #ifdef USE_MANAGED_LAUNCHER
234 void *preparedFunctionDelegate;
235 int st = createDelegate(__hostHandle, __domainId,
237 "Tizen.Runtime.Coreclr.AssemblyManager",
238 "Prepared", &preparedFunctionDelegate);
240 _ERR("Create delegate for Launch prepared function is fail (0x%08x)", st);
243 preparedFunction = reinterpret_cast<PreparedFunctionPtr>(preparedFunctionDelegate);
245 if (preparedFunction != nullptr)
248 void *launchFunctionDelegate;
249 st = createDelegate(__hostHandle, __domainId,
251 "Tizen.Runtime.Coreclr.AssemblyManager",
252 "Launch", &launchFunctionDelegate);
254 _ERR("Create delegate for Launch managed function is fail! (0x%08x)", st);
257 launchFunction = reinterpret_cast<LaunchFunctionPtr>(launchFunctionDelegate);
262 void CoreRuntime::dispose()
264 if (__hostHandle != nullptr) {
265 int st = shutdown(__hostHandle, __domainId);
267 _ERR("shutdown core clr fail! (0x%08x)", st);
270 if (dlclose(__coreclrLib) != 0)
271 _ERR("libcoreclr.so close failed");
273 __coreclrLib = nullptr;
278 if (__pluginLib != nullptr) {
279 if (dlclose(__pluginLib) != 0)
280 _ERR("libdotnet_plugin.so close failed");
282 __pluginLib = nullptr;
283 pluginInitialize = nullptr;
284 pluginPreload = nullptr;
285 pluginSetAppInfo = nullptr;
286 pluginSetCoreclrInfo = nullptr;
287 pluginGetDllPath = nullptr;
288 pluginBeforeExecute = nullptr;
289 pluginFinalize = nullptr;
292 _DBG("Dotnet runtime disposed");
295 int CoreRuntime::launch(const char* appId, const char* root, const char* path, int argc, char* argv[])
297 if (path == nullptr) {
298 _ERR("executable path is null");
302 if (fileNotExist(path)) {
303 _ERR("File not exist : %s", path);
307 if (pluginSetAppInfo)
308 pluginSetAppInfo(appId, path);
311 std::string appRoot = root;
312 std::string appBin = concatPath(appRoot, "bin");
313 std::string appLib = concatPath(appRoot, "lib");
314 std::string probePath = appBin + ":" + appLib + ":" + __nativeLibDirectory;
316 std::vector<std::string> searchDirectories;
317 searchDirectories.push_back(appBin);
318 searchDirectories.push_back(appLib);
319 if (pluginGetDllPath) {
320 std::string pluginPath = pluginGetDllPath();
321 if (!pluginPath.empty()) {
322 probePath = probePath + ":" + pluginPath;
323 searchDirectories.push_back(pluginPath);
326 searchDirectories.push_back(__runtimeDirectory);
327 searchDirectories.push_back(__deviceAPIDirectory);
328 searchDirectories.push_back(__refAPIDirectory);
329 #ifdef USE_MANAGED_LAUNCHER
330 searchDirectories.push_back(baseName(__launcherAssembly));
333 assembliesInDirectory(searchDirectories, tpa);
335 if (pluginBeforeExecute)
336 pluginBeforeExecute();
338 if (runLoggingThread() < 0) {
339 _ERR("Failed to create logging thread");
342 #ifdef USE_MANAGED_LAUNCHER
343 runManagedLauncher(appId, probePath.c_str(), tpa.c_str());
345 bool success = false;
346 if (launchFunction != nullptr) {
347 success = launchFunction(root, path, argc, argv);
349 _ERR("Failed to launch Application %s", path);
350 return success ? 0 : 1;
352 _ERR("Failed to find launch function");
356 int st = initializeCoreClr(appId, probePath.c_str(), probePath.c_str(), tpa.c_str());
357 unsigned int ret = 0;
358 st = executeAssembly(__hostHandle, __domainId, argc, (const char**)argv, path, &ret);
360 _ERR("Failed to Execute Assembly %s (0x%08x)", path, st);
365 } // namespace dotnetcore
366 } // namespace runtime