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 <sys/types.h>
34 #include "dotnet_launcher.h"
35 #include "plugin_manager.h"
36 #include "path_manager.h"
37 #include "log_manager.h"
39 #define PLUGIN_PATH "/usr/share/dotnet.tizen/lib/libdotnet_plugin.so"
40 #define ENV_FILE_PATH "/usr/share/dotnet.tizen/lib/coreclr_env.list"
44 namespace dotnetcore {
46 #if defined (__aarch64__)
47 #define ARCHITECTURE_IDENTIFIER "arm64"
48 const static std::vector<std::string> RID_FALLBACK_GRAPH =
49 {"linux-arm64", "linux", "unix-arm64", "unix", "any", "base"};
51 #elif defined (__arm__)
52 #define ARCHITECTURE_IDENTIFIER "arm"
53 const static std::vector<std::string> RID_FALLBACK_GRAPH =
54 {"tizen.4.0.0-armel", "tizen.4.0.0", "tizen-armel", "tizen", "linux-armel", "linux", "unix-armel", "unix", "any", "base"};
56 #elif defined (__x86_64__)
57 #define ARCHITECTURE_IDENTIFIER "x64"
58 const static std::vector<std::string> RID_FALLBACK_GRAPH =
59 {"linux-x64", "linux", "unix-x64", "unix", "any", "base"};
61 #elif defined (__i386__)
62 #define ARCHITECTURE_IDENTIFIER "x86"
63 const static std::vector<std::string> RID_FALLBACK_GRAPH =
64 {"linux-x86", "linux", "unix-x86", "unix", "any", "base"};
67 #error "Unknown target"
70 static std::string getExtraNativeLibDirs(const std::string& appRoot)
72 std::string candidate;
73 for (unsigned int i = 0; i < RID_FALLBACK_GRAPH.size(); i++) {
74 if(!candidate.empty()) {
77 candidate += concatPath(appRoot, "bin/runtimes/" + RID_FALLBACK_GRAPH[i] + "/native");
80 candidate = candidate + ":" + concatPath(appRoot, "lib/" ARCHITECTURE_IDENTIFIER);
81 if (!strncmp(ARCHITECTURE_IDENTIFIER, "arm64", 5)) {
82 candidate = candidate + ":" + concatPath(appRoot, "lib/aarch64");
89 static std::vector<std::string> __envList;
91 static void setEnvFromFile()
94 std::ifstream inFile(ENV_FILE_PATH);
99 _INFO("coreclr_env.list is found");
102 std::istringstream ss(envList);
105 while (std::getline(ss, token, ':')) {
106 if (!token.empty()) {
107 __envList.push_back(token);
111 for (unsigned int i = 0; i < __envList.size(); i++) {
112 putenv(const_cast<char *>(__envList[i].c_str()));
115 _INFO("coreclr_env.list file is not found. skip");
119 CoreRuntime::CoreRuntime(const char* mode) :
120 initializeClr(nullptr),
121 executeAssembly(nullptr),
123 createDelegate(nullptr),
124 __coreclrLib(nullptr),
125 __hostHandle(nullptr),
130 _INFO("Constructor called!!");
132 // plugin initialize should be called before start loader mainloop.
133 // In case of VD plugins, attaching secure zone is done in the plugin_initialize().
134 // When attaching to a secure zone, if there is a created thread, it will failed.
135 // So, plugin initialize should be called before mainloop start.
136 if (initializePluginManager(mode) < 0) {
137 _ERR("Failed to initialize PluginManager");
140 if (pluginHasLogControl()) {
141 __enableLogManager = false;
143 __enableLogManager = true;
147 CoreRuntime::~CoreRuntime()
152 int CoreRuntime::initialize(bool standalone)
155 #define __STR(x) __XSTR(x)
157 #ifdef NATIVE_LIB_DIR
158 __nativeLibDirectory = __STR(NATIVE_LIB_DIR);
165 // libunwind library is used to unwind stack frame, but libunwind for ARM
166 // does not support ARM vfpv3/NEON registers in DWARF format correctly.
167 // Therefore let's disable stack unwinding using DWARF information
168 // See https://github.com/dotnet/coreclr/issues/6698
170 // libunwind use following methods to unwind stack frame.
171 // UNW_ARM_METHOD_ALL 0xFF
172 // UNW_ARM_METHOD_DWARF 0x01
173 // UNW_ARM_METHOD_FRAME 0x02
174 // UNW_ARM_METHOD_EXIDX 0x04
175 putenv(const_cast<char *>("UNW_ARM_UNWIND_METHOD=6"));
178 // read string from external file and set them to environment value.
181 if (initializePathManager(std::string(), std::string(), std::string()) < 0) {
182 _ERR("Failed to initialize PathManager");
186 if (__enableLogManager) {
187 if (initializeLogManager() < 0) {
188 _ERR("Failed to initnialize LogManager");
192 if (redirectFD() < 0) {
193 _ERR("Failed to redirect FD");
197 if (runLoggingThread() < 0) {
198 _ERR("Failed to create and run logging thread to redicrect log");
203 std::string libCoreclr(concatPath(getRuntimeDir(), "libcoreclr.so"));
205 __coreclrLib = dlopen(libCoreclr.c_str(), RTLD_NOW | RTLD_LOCAL);
206 if (__coreclrLib == nullptr) {
207 char *err = dlerror();
208 _ERR("dlopen failed to open libcoreclr.so with error %s", err);
212 #define CORELIB_RETURN_IF_NOSYM(type, variable, name) \
214 variable = (type)dlsym(__coreclrLib, name); \
215 if (variable == nullptr) { \
216 _ERR(name " is not found in the libcoreclr.so"); \
221 CORELIB_RETURN_IF_NOSYM(coreclr_initialize_ptr, initializeClr, "coreclr_initialize");
222 CORELIB_RETURN_IF_NOSYM(coreclr_execute_assembly_ptr, executeAssembly, "coreclr_execute_assembly");
223 CORELIB_RETURN_IF_NOSYM(coreclr_shutdown_ptr, shutdown, "coreclr_shutdown");
224 CORELIB_RETURN_IF_NOSYM(coreclr_create_delegate_ptr, createDelegate, "coreclr_create_delegate");
226 #undef CORELIB_RETURN_IF_NOSYM
228 _INFO("libcoreclr dlopen and dlsym success");
233 fd = open("/proc/self", O_DIRECTORY);
234 std::string appRoot = std::string("/proc/self/fd/") + std::to_string(fd);
235 std::string appBin = concatPath(appRoot, "bin");
236 std::string appLib = concatPath(appRoot, "lib");
237 std::string probePath = appBin + ":" + appLib;
238 std::string tpa = getTPA();
239 std::string nativeLibPath = getExtraNativeLibDirs(appRoot) + ":" + appBin + ":" + appLib + ":" + __nativeLibDirectory;
240 std::string appName = std::string("dotnet-launcher-") + std::to_string(getpid());
242 if (!initializeCoreClr(appName.c_str(), probePath.c_str(), nativeLibPath.c_str(), tpa.c_str())) {
243 _ERR("Failed to initialize coreclr");
247 __initialized = true;
249 _INFO("CoreRuntime initialize success");
254 bool CoreRuntime::initializeCoreClr(const char* appId,
255 const char* assemblyProbePaths,
256 const char* pinvokeProbePaths,
259 const char *propertyKeys[] = {
260 "TRUSTED_PLATFORM_ASSEMBLIES",
263 "NATIVE_DLL_SEARCH_DIRECTORIES",
264 "AppDomainCompatSwitch"
267 const char *propertyValues[] = {
272 "UseLatestBehaviorWhenTFMNotSpecified"
275 std::string selfPath = readSelfPath();
277 int st = initializeClr(selfPath.c_str(),
279 sizeof(propertyKeys) / sizeof(propertyKeys[0]),
286 _ERR("initialize core clr fail! (0x%08x)", st);
290 pluginSetCoreclrInfo(__hostHandle, __domainId, createDelegate);
292 _INFO("Initialize core clr success");
296 void CoreRuntime::dispose()
298 if (__hostHandle != nullptr) {
299 int st = shutdown(__hostHandle, __domainId);
301 _ERR("shutdown core clr fail! (0x%08x)", st);
302 __hostHandle = nullptr;
305 if (__coreclrLib != nullptr) {
306 if (dlclose(__coreclrLib) != 0) {
307 _ERR("libcoreclr.so close failed");
310 __coreclrLib = nullptr;
313 finalizePluginManager();
314 finalizePathManager();
318 _INFO("Dotnet runtime disposed");
321 int CoreRuntime::launch(const char* appId, const char* root, const char* path, int argc, char* argv[])
323 if (!__initialized) {
324 _ERR("Runtime is not initialized");
328 if (path == nullptr) {
329 _ERR("executable path is null");
333 if (!isFileExist(path)) {
334 _ERR("File not exist : %s", path);
338 if (__enableLogManager) {
339 // launchpad override stdout and stderr to journalctl before launch application.
340 // we have to re-override that to input pipe for logging thread.
341 if (redirectFD() < 0) {
342 _ERR("Failed to redirect FD");
347 pluginSetAppInfo(appId, path);
349 int fd2 = open(root, O_DIRECTORY);
350 dup3(fd2, fd, O_CLOEXEC);
354 pluginBeforeExecute();
356 _INFO("execute assembly : %s", path);
358 unsigned int ret = 0;
359 int st = executeAssembly(__hostHandle, __domainId, argc, (const char**)argv, path, &ret);
361 _ERR("Failed to Execute Assembly %s (0x%08x)", path, st);
365 } // namespace dotnetcore
366 } // namespace runtime