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.
31 #include <sys/types.h>
34 #include <linux/limits.h>
38 #include <app_common.h>
40 #include "injection.h"
44 #include "dotnet_launcher.h"
45 #include "plugin_manager.h"
46 #include "path_manager.h"
47 #include "log_manager.h"
50 #define __STR(x) __XSTR(x)
51 static const char* __FRAMEWORK_DIR = __STR(FRAMEWORK_DIR);
57 namespace dotnetcore {
59 #if defined (__aarch64__)
60 #define ARCHITECTURE_IDENTIFIER "arm64"
62 #elif defined (__arm__)
63 #define ARCHITECTURE_IDENTIFIER "armel"
65 #elif defined (__x86_64__)
66 #define ARCHITECTURE_IDENTIFIER "x64"
68 #elif defined (__i386__)
69 #define ARCHITECTURE_IDENTIFIER "x86"
72 #error "Unknown target"
75 static const char* __TIZEN_RID_VERSION_KEY = "db/dotnet/tizen_rid_version";
77 // The sequence of RID_FALLBACK graphs must be:
78 // 1. Tizen + Version + Architecture
80 // 3. OS(tizen, linux, unix) + Architecture
81 // 4. OS(tizen, linux, unix)
83 static std::string getExtraNativeLibDirs(const std::string& appRoot)
85 std::vector<std::string> RID_FALLBACK_GRAPH;
86 std::vector<std::string> RID_FALLBACK_TIZEN;
87 char* tizen_rid = vconf_get_str(__TIZEN_RID_VERSION_KEY);
89 std::vector<std::string> version;
90 splitPath(tizen_rid, version);
91 std::reverse(std::begin(version), std::end(version));
92 for (unsigned int i = 0; i < version.size(); i++) {
93 RID_FALLBACK_TIZEN.push_back(std::string("tizen." + version[i] + "-" + ARCHITECTURE_IDENTIFIER));
94 RID_FALLBACK_TIZEN.push_back(std::string("tizen." + version[i]));
99 std::vector<std::string> RID_FALLBACK_OS = {"tizen", "linux", "unix"};
100 for (unsigned int i = 0; i < RID_FALLBACK_OS.size(); i++) {
101 RID_FALLBACK_GRAPH.push_back(std::string(RID_FALLBACK_OS[i] + "-" + ARCHITECTURE_IDENTIFIER));
102 RID_FALLBACK_GRAPH.push_back(std::string(RID_FALLBACK_OS[i]));
104 RID_FALLBACK_GRAPH.push_back("any");
105 RID_FALLBACK_GRAPH.push_back("base");
107 std::string candidate;
108 for (unsigned int i = 0; i < RID_FALLBACK_GRAPH.size(); i++) {
109 if (!candidate.empty()) {
112 candidate += concatPath(appRoot, "bin/runtimes/" + RID_FALLBACK_GRAPH[i] + "/native");
115 candidate = candidate + ":" + concatPath(appRoot, "lib/" ARCHITECTURE_IDENTIFIER);
116 if (!strncmp(ARCHITECTURE_IDENTIFIER, "arm64", 5)) {
117 candidate = candidate + ":" + concatPath(appRoot, "lib/aarch64");
118 } else if (!strncmp(ARCHITECTURE_IDENTIFIER, "armel", 5)) {
119 candidate = candidate + ":" + concatPath(appRoot, "lib/arm");
126 static std::vector<std::string> __envList;
128 static void setEnvFromFile()
131 std::ifstream inFile(ENV_FILE_PATH);
136 _INFO("coreclr_env.list is found");
139 while (std::getline(inFile, token, '\n')) {
140 if (!token.empty()) {
141 __envList.push_back(token);
145 for (unsigned int i = 0; i < __envList.size(); i++) {
146 putenv(const_cast<char *>(__envList[i].c_str()));
149 _INFO("coreclr_env.list file is not found. skip");
153 #define _unused(x) ((void)(x))
155 struct sigaction sig_abrt_new;
156 struct sigaction sig_abrt_old;
158 static bool checkOnSigabrt = false;
159 static bool checkOnTerminate = false;
161 static void onSigabrt(int signum)
163 // use unused variable to avoid build warning
164 ssize_t ret = write(STDERR_FILENO, "onSigabrt called\n", 17);
166 if (checkOnTerminate) {
167 ret = write(STDERR_FILENO, "onSigabrt called while terminate. go to exit\n", 45);
172 if (checkOnSigabrt) {
173 ret = write(STDERR_FILENO, "onSigabrt called again. go to exit\n", 35);
178 checkOnSigabrt = true;
179 if (sigaction(SIGABRT, &sig_abrt_old, NULL) == 0) {
180 if (raise(signum) < 0) {
181 ret = write(STDERR_FILENO, "Fail to raise SIGABRT\n", 22);
184 ret = write(STDERR_FILENO, "Fail to set original SIGABRT handler\n", 37);
189 static void registerSigHandler()
191 sig_abrt_new.sa_handler = onSigabrt;
192 if (sigemptyset(&sig_abrt_new.sa_mask) != 0) {
193 _ERR("Fail to sigemptyset");
196 if (sigaction(SIGABRT, &sig_abrt_new, &sig_abrt_old) < 0) {
197 _ERR("Fail to add sig handler");
201 static bool storage_cb(int id, storage_type_e type, storage_state_e state, const char *path, void *user_data)
203 int* tmp = (int*)user_data;
204 if (type == STORAGE_TYPE_INTERNAL)
213 static void initEnvForSpecialFolder()
219 error = storage_foreach_device_supported(storage_cb, &storageId);
220 if (error != STORAGE_ERROR_NONE) {
224 error = storage_get_directory(storageId, STORAGE_DIRECTORY_IMAGES, &path);
225 if (error == STORAGE_ERROR_NONE && path != NULL) {
226 setenv("XDG_PICTURES_DIR", const_cast<char *>(path), 1);
231 error = storage_get_directory(storageId, STORAGE_DIRECTORY_MUSIC, &path);
232 if (error == STORAGE_ERROR_NONE && path != NULL) {
233 setenv("XDG_MUSIC_DIR", const_cast<char *>(path), 1);
238 error = storage_get_directory(storageId, STORAGE_DIRECTORY_VIDEOS, &path);
239 if (error == STORAGE_ERROR_NONE && path != NULL) {
240 setenv("XDG_VIDEOS_DIR", const_cast<char *>(path), 1);
246 void CoreRuntime::preloadTypes()
248 const static std::string initDllPath = concatPath(__FRAMEWORK_DIR, "Tizen.Init.dll");
249 if (!isFileExist(initDllPath)) {
250 _ERR("Failed to locate Tizen.Init.dll");
254 typedef void (*InitDelegate)();
255 InitDelegate initDelegate;
257 int ret = createDelegate(__hostHandle,
260 "Tizen.Init.TypeLoader",
262 (void**)&initDelegate);
265 _ERR("Failed to create delegate for PreloadTypes (0x%08x)", ret);
271 CoreRuntime::CoreRuntime(const char* mode) :
272 initializeClr(nullptr),
273 executeAssembly(nullptr),
275 createDelegate(nullptr),
276 setEnvironmentVariable(nullptr),
277 __coreclrLib(nullptr),
278 __hostHandle(nullptr),
281 __initialized(false),
282 __isProfileMode(false)
284 _INFO("Constructor called!!");
287 env = getenv("CORECLR_ENABLE_PROFILING");
288 if (env != nullptr && !strcmp(env, "1")) {
289 _INFO("profiling mode on");
290 __isProfileMode = true;
293 // plugin initialize should be called before start loader mainloop.
294 // In case of VD plugins, attaching secure zone is done in the plugin_initialize().
295 // When attaching to a secure zone, if there is a created thread, it will failed.
296 // So, plugin initialize should be called before mainloop start.
297 if (initializePluginManager(mode) < 0) {
298 _ERR("Failed to initialize PluginManager");
302 CoreRuntime::~CoreRuntime()
304 // workaround : to prevent crash while process terminate on profiling mode,
305 // kill process immediately.
306 // see https://github.com/dotnet/coreclr/issues/26687
307 if (__isProfileMode) {
308 _INFO("shutdown process immediately.");
315 int CoreRuntime::initialize(bool standalone, bool useDlog)
317 // checkInjection checks dotnet-launcher run mode
318 // At the moment, this mechanism is used only when the Memory Profiler is started.
319 int res = checkInjection();
321 _ERR("Failed to initnialize Memory Profiler");
325 #define __STR(x) __XSTR(x)
327 #ifdef NATIVE_LIB_DIR
328 __nativeLibDirectory = __STR(NATIVE_LIB_DIR);
335 // libunwind library is used to unwind stack frame, but libunwind for ARM
336 // does not support ARM vfpv3/NEON registers in DWARF format correctly.
337 // Therefore let's disable stack unwinding using DWARF information
338 // See https://github.com/dotnet/coreclr/issues/6698
340 // libunwind use following methods to unwind stack frame.
341 // UNW_ARM_METHOD_ALL 0xFF
342 // UNW_ARM_METHOD_DWARF 0x01
343 // UNW_ARM_METHOD_FRAME 0x02
344 // UNW_ARM_METHOD_EXIDX 0x04
345 putenv(const_cast<char *>("UNW_ARM_UNWIND_METHOD=6"));
348 // Disable debug pipes and semaphores creation in case of non-standlone mode
350 putenv(const_cast<char *>("COMPlus_EnableDiagnostics=0"));
352 // Write Debug.WriteLine to stderr
353 putenv(const_cast<char *>("COMPlus_DebugWriteToStdErr=1"));
355 #ifdef USE_DEFAULT_BASE_ADDR
356 putenv(const_cast<char *>("COMPlus_UseDefaultBaseAddr=1"));
357 #endif // USE_DEFAULT_BASE_ADDR
359 // read string from external file and set them to environment value.
362 if (initializePathManager(std::string(), std::string(), std::string()) < 0) {
363 _ERR("Failed to initialize PathManager");
367 if (useDlog && !pluginHasLogControl()) {
368 if (initializeLogManager() < 0) {
369 _ERR("Failed to initnialize LogManager");
374 std::string libCoreclr(concatPath(getRuntimeDir(), "libcoreclr.so"));
376 __coreclrLib = dlopen(libCoreclr.c_str(), RTLD_NOW | RTLD_LOCAL);
377 if (__coreclrLib == nullptr) {
378 char *err = dlerror();
379 _ERR("dlopen failed to open libcoreclr.so with error %s", err);
380 if (access(libCoreclr.c_str(), R_OK) == -1)
381 _ERR("access '%s': %s\n", libCoreclr.c_str(), strerror(errno));
385 #define CORELIB_RETURN_IF_NOSYM(type, variable, name) \
387 variable = (type)dlsym(__coreclrLib, name); \
388 if (variable == nullptr) { \
389 _ERR(name " is not found in the libcoreclr.so"); \
394 CORELIB_RETURN_IF_NOSYM(coreclr_initialize_ptr, initializeClr, "coreclr_initialize");
395 CORELIB_RETURN_IF_NOSYM(coreclr_execute_assembly_ptr, executeAssembly, "coreclr_execute_assembly");
396 CORELIB_RETURN_IF_NOSYM(coreclr_shutdown_ptr, shutdown, "coreclr_shutdown");
397 CORELIB_RETURN_IF_NOSYM(coreclr_create_delegate_ptr, createDelegate, "coreclr_create_delegate");
399 #undef CORELIB_RETURN_IF_NOSYM
401 _INFO("libcoreclr dlopen and dlsym success");
407 // Set environment for System.Environment.SpecialFolder
408 // Below function creates dbus connection by callging storage API.
409 // If dbus connection is created bofere fork(), forked process cannot use dbus.
410 // To avoid gdbus blocking issue, below function should be called after fork()
411 initEnvForSpecialFolder();
413 fd = open("/proc/self", O_DIRECTORY);
414 std::string appRoot = std::string("/proc/self/fd/") + std::to_string(fd);
415 std::string appBin = concatPath(appRoot, "bin");
416 std::string appLib = concatPath(appRoot, "lib");
417 std::string appTac = concatPath(appBin, TAC_SYMLINK_SUB_DIR);
418 std::string probePath = appBin + ":" + appLib + ":" + appTac;
419 std::string NIprobePath = concatPath(appBin, APP_NI_SUB_DIR) + ":" + concatPath(appLib, APP_NI_SUB_DIR) + ":" + appTac;
420 std::string tpa = getTPA();
421 std::string runtimeDir = getRuntimeDir();
422 std::string nativeLibPath = getExtraNativeLibDirs(appRoot) + ":" + appBin + ":" + appLib + ":" + __nativeLibDirectory + ":" + runtimeDir;
423 std::string appName = std::string("dotnet-launcher-") + std::to_string(getpid());
425 if (!initializeCoreClr(appName.c_str(), probePath.c_str(), NIprobePath.c_str(), nativeLibPath.c_str(), tpa.c_str())) {
426 _ERR("Failed to initialize coreclr");
430 int st = createDelegate(__hostHandle, __domainId, "Dotnet.Launcher", "Dotnet.Launcher.Environment", "SetEnvironmentVariable", (void**)&setEnvironmentVariable);
431 if (st < 0 || setEnvironmentVariable == nullptr) {
432 _ERR("Create delegate for Dotnet.Launcher.dll -> Dotnet.Launcher.Environment -> SetEnvironmentVariable failed (0x%08x)", st);
436 __initialized = true;
439 preloadTypes(); // Preload common managed code
442 _INFO("CoreRuntime initialize success");
447 bool CoreRuntime::initializeCoreClr(const char* appId,
448 const char* assemblyProbePaths,
449 const char* NIProbePaths,
450 const char* pinvokeProbePaths,
453 const char *propertyKeys[] = {
454 "TRUSTED_PLATFORM_ASSEMBLIES",
457 "NATIVE_DLL_SEARCH_DIRECTORIES",
458 "AppDomainCompatSwitch"
461 const char *propertyValues[] = {
466 "UseLatestBehaviorWhenTFMNotSpecified"
469 std::string selfPath = readSelfPath();
471 int st = initializeClr(selfPath.c_str(),
473 sizeof(propertyKeys) / sizeof(propertyKeys[0]),
480 _ERR("initialize core clr fail! (0x%08x)", st);
484 pluginSetCoreclrInfo(__hostHandle, __domainId, createDelegate);
486 _INFO("Initialize core clr success");
490 void CoreRuntime::dispose()
492 // call plugin finalize function to notify finalize to plugin
493 // dlclose shoud be done after coreclr shutdown to avoid breaking signal chain
496 // ignore the signal generated by an exception that occurred during shutdown
497 checkOnTerminate = true;
499 if (__hostHandle != nullptr) {
500 int st = shutdown(__hostHandle, __domainId);
502 _ERR("shutdown core clr fail! (0x%08x)", st);
503 __hostHandle = nullptr;
506 if (__coreclrLib != nullptr) {
507 if (dlclose(__coreclrLib) != 0) {
508 _ERR("libcoreclr.so close failed");
511 __coreclrLib = nullptr;
514 finalizePluginManager();
515 finalizePathManager();
519 _INFO("Dotnet runtime disposed");
522 int CoreRuntime::launch(const char* appId, const char* root, const char* path, int argc, char* argv[])
524 if (!__initialized) {
525 _ERR("Runtime is not initialized");
529 if (path == nullptr) {
530 _ERR("executable path is null");
534 if (!isFileExist(path)) {
535 _ERR("File not exist : %s", path);
539 // launchpad override stdout and stderr to journalctl before launch application.
540 // we have to re-override that to input pipe for logging thread.
541 // if LogManager is not initialized, below redirectFD will return 0;
542 if (redirectFD() < 0) {
543 _ERR("Failed to redirect FD");
547 // VD has their own signal handler.
548 if (!pluginHasLogControl()) {
549 registerSigHandler();
552 pluginSetAppInfo(appId, path);
554 int fd2 = open(root, O_DIRECTORY);
555 dup3(fd2, fd, O_CLOEXEC);
559 // set application data path to coreclr environment.
560 // application data path can be changed by owner. So, we have to set data path just before launching.
561 char* localDataPath = app_get_data_path();
562 if (localDataPath != nullptr) {
563 setEnvironmentVariable("XDG_DATA_HOME", localDataPath);
567 pluginBeforeExecute();
569 _INFO("execute assembly : %s", path);
571 unsigned int ret = 0;
572 int st = executeAssembly(__hostHandle, __domainId, argc, (const char**)argv, path, &ret);
574 _ERR("Failed to Execute Assembly %s (0x%08x)", path, st);
578 } // namespace dotnetcore
579 } // namespace runtime