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 CoreRuntime::CoreRuntime() :
42 initializeClr(nullptr),
43 executeAssembly(nullptr),
45 createDelegate(nullptr),
46 __coreclrLib(nullptr),
47 __hostHandle(nullptr),
49 preparedFunction(nullptr),
50 launchFunction(nullptr),
52 pluginInitialize(nullptr),
53 pluginPreload(nullptr),
54 pluginSetAppInfo(nullptr),
55 pluginSetCoreclrInfo(nullptr),
56 pluginGetDllPath(nullptr),
57 pluginBeforeExecute(nullptr),
58 pluginFinalize(nullptr),
62 #define __STR(x) __XSTR(x)
65 __deviceAPIDirectory = __STR(DEVICE_API_DIR);
68 __runtimeDirectory = __STR(RUNTIME_DIR);
71 __nativeLibDirectory = __STR(NATIVE_LIB_DIR);
77 // support launcher plugin
78 if (!fileNotExist(PLUGIN_PATH)) {
79 __pluginLib = dlopen(PLUGIN_PATH, RTLD_NOW | RTLD_LOCAL);
81 pluginInitialize = (plugin_initialize_ptr)dlsym(__pluginLib, "plugin_initialize");
82 pluginPreload = (plugin_preload_ptr)dlsym(__pluginLib, "plugin_preload");
83 pluginSetAppInfo = (plugin_set_app_info_ptr)dlsym(__pluginLib, "plugin_set_app_info");
84 pluginSetCoreclrInfo = (plugin_set_coreclr_info_ptr)dlsym(__pluginLib, "plugin_set_coreclr_info");
85 pluginGetDllPath = (plugin_get_dll_path_ptr)dlsym(__pluginLib, "plugin_get_dll_path");
86 pluginBeforeExecute = (plugin_before_execute_ptr)dlsym(__pluginLib, "plugin_before_execute");
87 pluginFinalize = (plugin_finalize_ptr)dlsym(__pluginLib, "plugin_finalize");
94 _DBG("Constructor called!!");
97 CoreRuntime::~CoreRuntime()
102 int CoreRuntime::initialize(bool standalone)
105 // libunwind library is used to unwind stack frame, but libunwind for ARM
106 // does not support ARM vfpv3/NEON registers in DWARF format correctly.
107 // Therefore let's disable stack unwinding using DWARF information
108 // See https://github.com/dotnet/coreclr/issues/6698
110 // libunwind use following methods to unwind stack frame.
111 // UNW_ARM_METHOD_ALL 0xFF
112 // UNW_ARM_METHOD_DWARF 0x01
113 // UNW_ARM_METHOD_FRAME 0x02
114 // UNW_ARM_METHOD_EXIDX 0x04
115 putenv(const_cast<char *>("UNW_ARM_UNWIND_METHOD=6"));
118 if (__deviceAPIDirectory.empty()) {
119 _ERR("Empty Device API Directory");
122 __deviceAPIDirectory = absolutePath(__deviceAPIDirectory);
125 if (__runtimeDirectory.empty()) {
126 _ERR("Empty Runtime Directory");
129 __runtimeDirectory = absolutePath(__runtimeDirectory);
132 // set Reference API directory
133 __refAPIDirectory = __deviceAPIDirectory + "/ref";
135 std::string libCoreclr(concatPath(__runtimeDirectory, "libcoreclr.so"));
137 _DBG("libcoreclr : %s", libCoreclr.c_str());
139 __coreclrLib = dlopen(libCoreclr.c_str(), RTLD_NOW | RTLD_LOCAL);
140 if (__coreclrLib == nullptr) {
141 char *err = dlerror();
142 _ERR("dlopen failed to open libcoreclr.so with error %s", err);
146 #define CORELIB_RETURN_IF_NOSYM(type, variable, name) \
148 variable = (type)dlsym(__coreclrLib, name); \
149 if (variable == nullptr) { \
150 _ERR(name " is not found in the libcoreclr.so"); \
155 CORELIB_RETURN_IF_NOSYM(coreclr_initialize_ptr, initializeClr, "coreclr_initialize");
156 CORELIB_RETURN_IF_NOSYM(coreclr_execute_assembly_ptr, executeAssembly, "coreclr_execute_assembly");
157 CORELIB_RETURN_IF_NOSYM(coreclr_shutdown_ptr, shutdown, "coreclr_shutdown");
158 CORELIB_RETURN_IF_NOSYM(coreclr_create_delegate_ptr, createDelegate, "coreclr_create_delegate");
160 #undef CORELIB_RETURN_IF_NOSYM
162 _DBG("libcoreclr dlopen and dlsym success");
164 if (!standalone && pluginPreload)
167 fd = open("/proc/self", O_DIRECTORY);
168 std::string path_tmp = std::string("/proc/self/fd/") + std::to_string(fd);
170 std::string appRoot = path_tmp;
171 std::string appBin = concatPath(appRoot, "bin");
172 std::string appLib = concatPath(appRoot, "lib");
173 std::string probePath = appBin + ":" + appLib;
176 std::vector<std::string> searchDirectories;
177 searchDirectories.push_back(__runtimeDirectory);
178 searchDirectories.push_back(__deviceAPIDirectory);
179 searchDirectories.push_back(__refAPIDirectory);
181 if (pluginGetDllPath) {
182 std::string pluginPath = pluginGetDllPath();
183 if (!pluginPath.empty()) {
184 searchDirectories.push_back(pluginPath);
188 assembliesInDirectory(searchDirectories, tpa);
190 std::string nativeLibPath;
191 nativeLibPath = appLib + ":" + appBin + ":" + __nativeLibDirectory;
193 std::string appName = std::string("dotnet-launcher-") + std::to_string(getpid());
194 if (!initializeCoreClr(appName.c_str(), probePath.c_str(), nativeLibPath.c_str(), tpa.c_str())) {
195 _ERR("Failed to initialize coreclr");
202 bool CoreRuntime::initializeCoreClr(const char* appId,
203 const char* assemblyProbePaths,
204 const char* pinvokeProbePaths,
207 const char *propertyKeys[] = {
208 "TRUSTED_PLATFORM_ASSEMBLIES",
211 "NATIVE_DLL_SEARCH_DIRECTORIES",
212 "AppDomainCompatSwitch"
215 const char *propertyValues[] = {
220 "UseLatestBehaviorWhenTFMNotSpecified"
223 std::string selfPath = readSelfPath();
225 int st = initializeClr(selfPath.c_str(),
227 sizeof(propertyKeys) / sizeof(propertyKeys[0]),
234 _ERR("initialize core clr fail! (0x%08x)", st);
238 if (pluginSetCoreclrInfo)
239 pluginSetCoreclrInfo(__hostHandle, __domainId);
241 _DBG("Initialize core clr success");
245 void CoreRuntime::dispose()
247 if (__hostHandle != nullptr) {
248 int st = shutdown(__hostHandle, __domainId);
250 _ERR("shutdown core clr fail! (0x%08x)", st);
253 if (dlclose(__coreclrLib) != 0)
254 _ERR("libcoreclr.so close failed");
256 __coreclrLib = nullptr;
261 if (__pluginLib != nullptr) {
262 if (dlclose(__pluginLib) != 0)
263 _ERR("libdotnet_plugin.so close failed");
265 __pluginLib = nullptr;
266 pluginInitialize = nullptr;
267 pluginPreload = nullptr;
268 pluginSetAppInfo = nullptr;
269 pluginSetCoreclrInfo = nullptr;
270 pluginGetDllPath = nullptr;
271 pluginBeforeExecute = nullptr;
272 pluginFinalize = nullptr;
275 _DBG("Dotnet runtime disposed");
278 int CoreRuntime::launch(const char* appId, const char* root, const char* path, int argc, char* argv[])
280 if (path == nullptr) {
281 _ERR("executable path is null");
285 if (fileNotExist(path)) {
286 _ERR("File not exist : %s", path);
290 if (pluginSetAppInfo)
291 pluginSetAppInfo(appId, path);
293 int fd2 = open(root, O_DIRECTORY);
294 dup3(fd2, fd, O_CLOEXEC);
297 if (pluginBeforeExecute)
298 pluginBeforeExecute();
300 if (runLoggingThread() < 0) {
301 _ERR("Failed to create logging thread");
304 unsigned int ret = 0;
305 int st = executeAssembly(__hostHandle, __domainId, argc, (const char**)argv, path, &ret);
307 _ERR("Failed to Execute Assembly %s (0x%08x)", path, st);
311 } // namespace dotnetcore
312 } // namespace runtime