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 pluginGetDllPath(nullptr),
50 pluginBeforeExecute(nullptr),
51 pluginFinalize(nullptr)
54 #define __STR(x) __XSTR(x)
57 __deviceAPIDirectory = __STR(DEVICE_API_DIR);
60 __runtimeDirectory = __STR(RUNTIME_DIR);
63 __nativeLibDirectory = __STR(NATIVE_LIB_DIR);
66 #ifdef USE_MANAGED_LAUNCHER
67 #ifdef CORECLR_LAUNCHER_ASSEMBLY_PATH
68 __launcherAssembly = __STR(CORECLR_LAUNCHER_ASSEMBLY_PATH);
75 // support launcher plugin
76 if (!fileNotExist(PLUGIN_PATH)) {
77 __pluginLib = dlopen(PLUGIN_PATH, RTLD_NOW | RTLD_LOCAL);
79 pluginInitialize = (plugin_initialize_ptr)dlsym(__pluginLib, "plugin_initialize");
80 pluginPreload = (plugin_preload_ptr)dlsym(__pluginLib, "plugin_preload");
81 pluginSetAppInfo = (plugin_set_app_info_ptr)dlsym(__pluginLib, "plugin_set_app_info");
82 pluginGetDllPath = (plugin_get_dll_path_ptr)dlsym(__pluginLib, "plugin_get_dll_path");
83 pluginBeforeExecute = (plugin_before_execute_ptr)dlsym(__pluginLib, "plugin_before_execute");
84 pluginFinalize = (plugin_finalize_ptr)dlsym(__pluginLib, "plugin_finalize");
91 _DBG("Constructor called!!");
94 CoreRuntime::~CoreRuntime()
99 int CoreRuntime::initialize(bool standalone)
102 // libunwind library is used to unwind stack frame, but libunwind for ARM
103 // does not support ARM vfpv3/NEON registers in DWARF format correctly.
104 // Therefore let's disable stack unwinding using DWARF information
105 // See https://github.com/dotnet/coreclr/issues/6698
107 // libunwind use following methods to unwind stack frame.
108 // UNW_ARM_METHOD_ALL 0xFF
109 // UNW_ARM_METHOD_DWARF 0x01
110 // UNW_ARM_METHOD_FRAME 0x02
111 // UNW_ARM_METHOD_EXIDX 0x04
112 putenv(const_cast<char *>("UNW_ARM_UNWIND_METHOD=6"));
115 if (__deviceAPIDirectory.empty()) {
116 _ERR("Empty Device API Directory");
119 __deviceAPIDirectory = absolutePath(__deviceAPIDirectory);
122 if (__runtimeDirectory.empty()) {
123 _ERR("Empty Runtime Directory");
126 __runtimeDirectory = absolutePath(__runtimeDirectory);
129 // set Reference API directory
130 __refAPIDirectory = __deviceAPIDirectory + "/ref";
132 #ifdef USE_MANAGED_LAUNCHER
133 if (__launcherAssembly.empty()) {
134 _ERR("Empty Launcher Assembly");
137 __launcherAssembly = absolutePath(__launcherAssembly);
141 std::string libCoreclr(concatPath(__runtimeDirectory, "libcoreclr.so"));
143 _DBG("libcoreclr : %s", libCoreclr.c_str());
145 __coreclrLib = dlopen(libCoreclr.c_str(), RTLD_NOW | RTLD_LOCAL);
146 if (__coreclrLib == nullptr) {
147 char *err = dlerror();
148 _ERR("dlopen failed to open libcoreclr.so with error %s", err);
152 #define CORELIB_RETURN_IF_NOSYM(type, variable, name) \
154 variable = (type)dlsym(__coreclrLib, name); \
155 if (variable == nullptr) { \
156 _ERR(name " is not found in the libcoreclr.so"); \
161 CORELIB_RETURN_IF_NOSYM(coreclr_initialize_ptr, initializeClr, "coreclr_initialize");
162 CORELIB_RETURN_IF_NOSYM(coreclr_execute_assembly_ptr, executeAssembly, "coreclr_execute_assembly");
163 CORELIB_RETURN_IF_NOSYM(coreclr_shutdown_ptr, shutdown, "coreclr_shutdown");
164 CORELIB_RETURN_IF_NOSYM(coreclr_create_delegate_ptr, createDelegate, "coreclr_create_delegate");
166 #undef CORELIB_RETURN_IF_NOSYM
168 _DBG("libcoreclr dlopen and dlsym success");
170 if (!standalone && pluginPreload)
176 bool CoreRuntime::initializeCoreClr(const char* appId,
177 const char* assemblyProbePaths,
178 const char* pinvokeProbePaths,
181 const char *propertyKeys[] = {
182 "TRUSTED_PLATFORM_ASSEMBLIES",
185 "NATIVE_DLL_SEARCH_DIRECTORIES",
186 "AppDomainCompatSwitch"
189 const char *propertyValues[] = {
194 "UseLatestBehaviorWhenTFMNotSpecified"
197 std::string selfPath = readSelfPath();
199 int st = initializeClr(selfPath.c_str(),
201 sizeof(propertyKeys) / sizeof(propertyKeys[0]),
208 _ERR("initialize core clr fail! (0x%08x)", st);
212 _DBG("Initialize core clr success");
216 int CoreRuntime::runManagedLauncher(const char* appId, const char* appBase, const char* tpaList)
218 if (fileNotExist(__launcherAssembly)) {
219 _ERR("Launcher assembly is not exist in %s", __launcherAssembly.c_str());
223 if (!initializeCoreClr(appId, appBase, appBase, tpaList)) {
224 _ERR("Failed to initialize coreclr");
228 #ifdef USE_MANAGED_LAUNCHER
229 void *preparedFunctionDelegate;
230 int st = createDelegate(__hostHandle, __domainId,
232 "Tizen.Runtime.Coreclr.AssemblyManager",
233 "Prepared", &preparedFunctionDelegate);
235 _ERR("Create delegate for Launch prepared function is fail (0x%08x)", st);
238 preparedFunction = reinterpret_cast<PreparedFunctionPtr>(preparedFunctionDelegate);
240 if (preparedFunction != nullptr)
243 void *launchFunctionDelegate;
244 st = createDelegate(__hostHandle, __domainId,
246 "Tizen.Runtime.Coreclr.AssemblyManager",
247 "Launch", &launchFunctionDelegate);
249 _ERR("Create delegate for Launch managed function is fail! (0x%08x)", st);
252 launchFunction = reinterpret_cast<LaunchFunctionPtr>(launchFunctionDelegate);
257 void CoreRuntime::dispose()
259 if (__hostHandle != nullptr) {
260 int st = shutdown(__hostHandle, __domainId);
262 _ERR("shutdown core clr fail! (0x%08x)", st);
265 if (dlclose(__coreclrLib) != 0)
266 _ERR("libcoreclr.so close failed");
268 __coreclrLib = nullptr;
273 if (__pluginLib != nullptr) {
274 if (dlclose(__pluginLib) != 0)
275 _ERR("libdotnet_plugin.so close failed");
277 __pluginLib = nullptr;
278 pluginInitialize = nullptr;
279 pluginPreload = nullptr;
280 pluginSetAppInfo = nullptr;
281 pluginGetDllPath = nullptr;
282 pluginBeforeExecute = nullptr;
283 pluginFinalize = nullptr;
286 _DBG("Dotnet runtime disposed");
289 int CoreRuntime::launch(const char* appId, const char* root, const char* path, int argc, char* argv[])
291 if (path == nullptr) {
292 _ERR("executable path is null");
296 if (fileNotExist(path)) {
297 _ERR("File not exist : %s", path);
301 if (pluginSetAppInfo)
302 pluginSetAppInfo(appId, path);
305 std::string appRoot = root;
306 std::string appBin = concatPath(appRoot, "bin");
307 std::string appLib = concatPath(appRoot, "lib");
308 std::string probePath = appBin + ":" + appLib + ":" + __nativeLibDirectory;
310 std::vector<std::string> searchDirectories;
311 searchDirectories.push_back(appBin);
312 searchDirectories.push_back(appLib);
313 if (pluginGetDllPath) {
314 std::string pluginPath = pluginGetDllPath();
315 if (!pluginPath.empty()) {
316 probePath = probePath + ":" + pluginPath;
317 searchDirectories.push_back(pluginPath);
320 searchDirectories.push_back(__runtimeDirectory);
321 searchDirectories.push_back(__deviceAPIDirectory);
322 searchDirectories.push_back(__refAPIDirectory);
323 #ifdef USE_MANAGED_LAUNCHER
324 searchDirectories.push_back(baseName(__launcherAssembly));
327 assembliesInDirectory(searchDirectories, tpa);
329 if (pluginBeforeExecute)
330 pluginBeforeExecute();
332 #ifdef USE_MANAGED_LAUNCHER
333 runManagedLauncher(appId, probePath.c_str(), tpa.c_str());
335 bool success = false;
336 if (launchFunction != nullptr) {
337 std::string cppPath(path);
339 if (isManagedAssembly(cppPath) && !isNativeImage(cppPath)) {
340 size_t extindex = cppPath.size() - 4;
341 cppPath = cppPath.substr(0, extindex) + ".ni" + cppPath.substr(extindex, 4);
342 if (!fileNotExist(cppPath))
343 path = cppPath.c_str();
346 success = launchFunction(root, path, argc, argv);
348 _ERR("Failed to launch Application %s", path);
349 return success ? 0 : 1;
351 _ERR("Failed to find launch function");
355 int st = initializeCoreClr(appId, probePath.c_str(), probePath.c_str(), tpa.c_str());
356 unsigned int ret = 0;
357 st = executeAssembly(__hostHandle, __domainId, argc, (const char**)argv, path, &ret);
359 _ERR("Failed to Execute Assembly %s (0x%08x)", path, st);
364 } // namespace dotnetcore
365 } // namespace runtime