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.
26 #include <sys/types.h>
33 #include "dotnet_launcher.h"
35 #define PLUGIN_PATH "/usr/share/dotnet.tizen/lib/libdotnet_plugin.so"
39 namespace dotnetcore {
41 #if defined (__aarch64__)
42 #define ARCHITECTURE_IDENTIFIER "arm64"
43 const static std::vector<std::string> RID_FALLBACK_GRAPH =
44 {"linux-arm64", "linux", "unix-arm64", "unix", "any", "base"};
46 #elif defined (__arm__)
47 #define ARCHITECTURE_IDENTIFIER "arm"
48 const static std::vector<std::string> RID_FALLBACK_GRAPH =
49 {"tizen.4.0.0-armel", "tizen.4.0.0", "tizen-armel", "tizen", "linux-armel", "linux", "unix-armel", "unix", "any", "base"};
51 #elif defined (__x86_64__)
52 #define ARCHITECTURE_IDENTIFIER "x64"
53 const static std::vector<std::string> RID_FALLBACK_GRAPH =
54 {"linux-x64", "linux", "unix-x64", "unix", "any", "base"};
56 #elif defined (__i386__)
57 #define ARCHITECTURE_IDENTIFIER "x86"
58 const static std::vector<std::string> RID_FALLBACK_GRAPH =
59 {"linux-x86", "linux", "unix-x86", "unix", "any", "base"};
62 #error "Unknown target"
65 static std::string getExtraNativeLibDirs(const std::string& appRoot)
67 std::string candidate;
68 for (unsigned int i = 0; i < RID_FALLBACK_GRAPH.size(); i++) {
69 if(!candidate.empty()) {
72 candidate += concatPath(appRoot, "bin/runtimes/" + RID_FALLBACK_GRAPH[i] + "/native");
75 candidate = candidate + ":" + concatPath(appRoot, "lib/" ARCHITECTURE_IDENTIFIER);
76 if (!strncmp(ARCHITECTURE_IDENTIFIER, "arm64", 5)) {
77 candidate = candidate + ":" + concatPath(appRoot, "lib/aarch64");
83 CoreRuntime::CoreRuntime(const char* mode) :
84 initializeClr(nullptr),
85 executeAssembly(nullptr),
87 createDelegate(nullptr),
88 __coreclrLib(nullptr),
89 __hostHandle(nullptr),
91 preparedFunction(nullptr),
92 launchFunction(nullptr),
94 pluginInitialize(nullptr),
95 pluginPreload(nullptr),
96 pluginSetAppInfo(nullptr),
97 pluginSetCoreclrInfo(nullptr),
98 pluginGetDllPath(nullptr),
99 pluginBeforeExecute(nullptr),
100 pluginFinalize(nullptr),
105 #define __STR(x) __XSTR(x)
107 #ifdef DEVICE_API_DIR
108 __deviceAPIDirectory = __STR(DEVICE_API_DIR);
111 __runtimeDirectory = __STR(RUNTIME_DIR);
113 #ifdef NATIVE_LIB_DIR
114 __nativeLibDirectory = __STR(NATIVE_LIB_DIR);
120 // support launcher plugin
121 if (!fileNotExist(PLUGIN_PATH)) {
122 __pluginLib = dlopen(PLUGIN_PATH, RTLD_NOW | RTLD_LOCAL);
124 pluginInitialize = (plugin_initialize_ptr)dlsym(__pluginLib, "plugin_initialize");
125 pluginPreload = (plugin_preload_ptr)dlsym(__pluginLib, "plugin_preload");
126 pluginSetAppInfo = (plugin_set_app_info_ptr)dlsym(__pluginLib, "plugin_set_app_info");
127 pluginSetCoreclrInfo = (plugin_set_coreclr_info_ptr)dlsym(__pluginLib, "plugin_set_coreclr_info");
128 pluginGetDllPath = (plugin_get_dll_path_ptr)dlsym(__pluginLib, "plugin_get_dll_path");
129 pluginBeforeExecute = (plugin_before_execute_ptr)dlsym(__pluginLib, "plugin_before_execute");
130 pluginFinalize = (plugin_finalize_ptr)dlsym(__pluginLib, "plugin_finalize");
134 if (pluginInitialize)
135 pluginInitialize(mode);
137 _DBG("Constructor called!!");
140 CoreRuntime::~CoreRuntime()
145 int CoreRuntime::initialize(bool standalone)
148 // libunwind library is used to unwind stack frame, but libunwind for ARM
149 // does not support ARM vfpv3/NEON registers in DWARF format correctly.
150 // Therefore let's disable stack unwinding using DWARF information
151 // See https://github.com/dotnet/coreclr/issues/6698
153 // libunwind use following methods to unwind stack frame.
154 // UNW_ARM_METHOD_ALL 0xFF
155 // UNW_ARM_METHOD_DWARF 0x01
156 // UNW_ARM_METHOD_FRAME 0x02
157 // UNW_ARM_METHOD_EXIDX 0x04
158 putenv(const_cast<char *>("UNW_ARM_UNWIND_METHOD=6"));
161 if (__deviceAPIDirectory.empty()) {
162 _ERR("Empty Device API Directory");
165 __deviceAPIDirectory = absolutePath(__deviceAPIDirectory);
168 if (__runtimeDirectory.empty()) {
169 _ERR("Empty Runtime Directory");
172 __runtimeDirectory = absolutePath(__runtimeDirectory);
175 // set Reference API directory
176 __refAPIDirectory = __deviceAPIDirectory + "/ref";
178 std::string libCoreclr(concatPath(__runtimeDirectory, "libcoreclr.so"));
180 _DBG("libcoreclr : %s", libCoreclr.c_str());
182 __coreclrLib = dlopen(libCoreclr.c_str(), RTLD_NOW | RTLD_LOCAL);
183 if (__coreclrLib == nullptr) {
184 char *err = dlerror();
185 _ERR("dlopen failed to open libcoreclr.so with error %s", err);
189 #define CORELIB_RETURN_IF_NOSYM(type, variable, name) \
191 variable = (type)dlsym(__coreclrLib, name); \
192 if (variable == nullptr) { \
193 _ERR(name " is not found in the libcoreclr.so"); \
198 CORELIB_RETURN_IF_NOSYM(coreclr_initialize_ptr, initializeClr, "coreclr_initialize");
199 CORELIB_RETURN_IF_NOSYM(coreclr_execute_assembly_ptr, executeAssembly, "coreclr_execute_assembly");
200 CORELIB_RETURN_IF_NOSYM(coreclr_shutdown_ptr, shutdown, "coreclr_shutdown");
201 CORELIB_RETURN_IF_NOSYM(coreclr_create_delegate_ptr, createDelegate, "coreclr_create_delegate");
203 #undef CORELIB_RETURN_IF_NOSYM
205 _DBG("libcoreclr dlopen and dlsym success");
207 if (!standalone && pluginPreload)
210 fd = open("/proc/self", O_DIRECTORY);
211 std::string path_tmp = std::string("/proc/self/fd/") + std::to_string(fd);
213 std::string appRoot = path_tmp;
214 std::string appBin = concatPath(appRoot, "bin");
215 std::string appLib = concatPath(appRoot, "lib");
216 std::string probePath = appBin + ":" + appLib;
219 std::vector<std::string> searchDirectories;
220 searchDirectories.push_back(__runtimeDirectory);
221 searchDirectories.push_back(__deviceAPIDirectory);
222 searchDirectories.push_back(__refAPIDirectory);
224 if (pluginGetDllPath) {
225 std::string pluginPath = pluginGetDllPath();
226 if (!pluginPath.empty()) {
227 splitPath(pluginPath, searchDirectories);
231 assembliesInDirectory(searchDirectories, tpa);
233 std::string nativeLibPath;
234 nativeLibPath = getExtraNativeLibDirs(appRoot) + ":" + appBin + ":" + appLib + ":" + __nativeLibDirectory;
236 std::string appName = std::string("dotnet-launcher-") + std::to_string(getpid());
237 if (!initializeCoreClr(appName.c_str(), probePath.c_str(), nativeLibPath.c_str(), tpa.c_str())) {
238 _ERR("Failed to initialize coreclr");
245 bool CoreRuntime::initializeCoreClr(const char* appId,
246 const char* assemblyProbePaths,
247 const char* pinvokeProbePaths,
250 const char *propertyKeys[] = {
251 "TRUSTED_PLATFORM_ASSEMBLIES",
254 "NATIVE_DLL_SEARCH_DIRECTORIES",
255 "AppDomainCompatSwitch"
258 const char *propertyValues[] = {
263 "UseLatestBehaviorWhenTFMNotSpecified"
266 std::string selfPath = readSelfPath();
268 int st = initializeClr(selfPath.c_str(),
270 sizeof(propertyKeys) / sizeof(propertyKeys[0]),
277 _ERR("initialize core clr fail! (0x%08x)", st);
281 if (pluginSetCoreclrInfo)
282 pluginSetCoreclrInfo(__hostHandle, __domainId, createDelegate);
284 _DBG("Initialize core clr success");
288 void CoreRuntime::dispose()
290 if (__hostHandle != nullptr) {
291 int st = shutdown(__hostHandle, __domainId);
293 _ERR("shutdown core clr fail! (0x%08x)", st);
296 if (dlclose(__coreclrLib) != 0)
297 _ERR("libcoreclr.so close failed");
299 __coreclrLib = nullptr;
304 if (__pluginLib != nullptr) {
305 if (dlclose(__pluginLib) != 0)
306 _ERR("libdotnet_plugin.so close failed");
308 __pluginLib = nullptr;
309 pluginInitialize = nullptr;
310 pluginPreload = nullptr;
311 pluginSetAppInfo = nullptr;
312 pluginSetCoreclrInfo = nullptr;
313 pluginGetDllPath = nullptr;
314 pluginBeforeExecute = nullptr;
315 pluginFinalize = nullptr;
318 _DBG("Dotnet runtime disposed");
321 int CoreRuntime::launch(const char* appId, const char* root, const char* path, int argc, char* argv[])
323 if (runLoggingThread() < 0) {
324 _ERR("Failed to create logging thread");
327 if (path == nullptr) {
328 _ERR("executable path is null");
332 if (fileNotExist(path)) {
333 _ERR("File not exist : %s", path);
337 if (pluginSetAppInfo)
338 pluginSetAppInfo(appId, path);
340 int fd2 = open(root, O_DIRECTORY);
341 dup3(fd2, fd, O_CLOEXEC);
345 if (pluginBeforeExecute)
346 pluginBeforeExecute();
348 if (runLoggingThread() < 0) {
349 _ERR("Failed to create logging thread");
352 unsigned int ret = 0;
353 int st = executeAssembly(__hostHandle, __domainId, argc, (const char**)argv, path, &ret);
355 _ERR("Failed to Execute Assembly %s (0x%08x)", path, st);
359 } // namespace dotnetcore
360 } // namespace runtime