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"
34 #include "plugin_manager.h"
35 #include "path_manager.h"
36 #include "log_manager.h"
38 #define PLUGIN_PATH "/usr/share/dotnet.tizen/lib/libdotnet_plugin.so"
42 namespace dotnetcore {
44 #if defined (__aarch64__)
45 #define ARCHITECTURE_IDENTIFIER "arm64"
46 const static std::vector<std::string> RID_FALLBACK_GRAPH =
47 {"linux-arm64", "linux", "unix-arm64", "unix", "any", "base"};
49 #elif defined (__arm__)
50 #define ARCHITECTURE_IDENTIFIER "arm"
51 const static std::vector<std::string> RID_FALLBACK_GRAPH =
52 {"tizen.4.0.0-armel", "tizen.4.0.0", "tizen-armel", "tizen", "linux-armel", "linux", "unix-armel", "unix", "any", "base"};
54 #elif defined (__x86_64__)
55 #define ARCHITECTURE_IDENTIFIER "x64"
56 const static std::vector<std::string> RID_FALLBACK_GRAPH =
57 {"linux-x64", "linux", "unix-x64", "unix", "any", "base"};
59 #elif defined (__i386__)
60 #define ARCHITECTURE_IDENTIFIER "x86"
61 const static std::vector<std::string> RID_FALLBACK_GRAPH =
62 {"linux-x86", "linux", "unix-x86", "unix", "any", "base"};
65 #error "Unknown target"
68 static std::string getExtraNativeLibDirs(const std::string& appRoot)
70 std::string candidate;
71 for (unsigned int i = 0; i < RID_FALLBACK_GRAPH.size(); i++) {
72 if(!candidate.empty()) {
75 candidate += concatPath(appRoot, "bin/runtimes/" + RID_FALLBACK_GRAPH[i] + "/native");
78 candidate = candidate + ":" + concatPath(appRoot, "lib/" ARCHITECTURE_IDENTIFIER);
79 if (!strncmp(ARCHITECTURE_IDENTIFIER, "arm64", 5)) {
80 candidate = candidate + ":" + concatPath(appRoot, "lib/aarch64");
86 CoreRuntime::CoreRuntime(const char* mode) :
87 initializeClr(nullptr),
88 executeAssembly(nullptr),
90 createDelegate(nullptr),
91 __coreclrLib(nullptr),
92 __hostHandle(nullptr),
98 _DBG("Constructor called!!");
101 CoreRuntime::~CoreRuntime()
106 int CoreRuntime::initialize(bool standalone)
109 #define __STR(x) __XSTR(x)
111 #ifdef NATIVE_LIB_DIR
112 __nativeLibDirectory = __STR(NATIVE_LIB_DIR);
119 // libunwind library is used to unwind stack frame, but libunwind for ARM
120 // does not support ARM vfpv3/NEON registers in DWARF format correctly.
121 // Therefore let's disable stack unwinding using DWARF information
122 // See https://github.com/dotnet/coreclr/issues/6698
124 // libunwind use following methods to unwind stack frame.
125 // UNW_ARM_METHOD_ALL 0xFF
126 // UNW_ARM_METHOD_DWARF 0x01
127 // UNW_ARM_METHOD_FRAME 0x02
128 // UNW_ARM_METHOD_EXIDX 0x04
129 putenv(const_cast<char *>("UNW_ARM_UNWIND_METHOD=6"));
132 if (initializePluginManager(__mode) < 0) {
133 _ERR("Failed to initialize PluginManager");
137 if (initializePathManager(std::string(), std::string(), std::string()) < 0) {
138 _ERR("Failed to initialize PathManager");
142 if (initializeLogManager() < 0) {
143 _ERR("Failed to initnialize LogManager");
147 if (redirectFD() < 0) {
148 _ERR("Failed to redirect FD");
152 if (runLoggingThread() < 0) {
153 _ERR("Failed to create and run logging thread to redicrect log");
157 std::string libCoreclr(concatPath(getRuntimeDir(), "libcoreclr.so"));
159 __coreclrLib = dlopen(libCoreclr.c_str(), RTLD_NOW | RTLD_LOCAL);
160 if (__coreclrLib == nullptr) {
161 char *err = dlerror();
162 _ERR("dlopen failed to open libcoreclr.so with error %s", err);
166 #define CORELIB_RETURN_IF_NOSYM(type, variable, name) \
168 variable = (type)dlsym(__coreclrLib, name); \
169 if (variable == nullptr) { \
170 _ERR(name " is not found in the libcoreclr.so"); \
175 CORELIB_RETURN_IF_NOSYM(coreclr_initialize_ptr, initializeClr, "coreclr_initialize");
176 CORELIB_RETURN_IF_NOSYM(coreclr_execute_assembly_ptr, executeAssembly, "coreclr_execute_assembly");
177 CORELIB_RETURN_IF_NOSYM(coreclr_shutdown_ptr, shutdown, "coreclr_shutdown");
178 CORELIB_RETURN_IF_NOSYM(coreclr_create_delegate_ptr, createDelegate, "coreclr_create_delegate");
180 #undef CORELIB_RETURN_IF_NOSYM
182 _DBG("libcoreclr dlopen and dlsym success");
187 fd = open("/proc/self", O_DIRECTORY);
188 std::string appRoot = std::string("/proc/self/fd/") + std::to_string(fd);
189 std::string appBin = concatPath(appRoot, "bin");
190 std::string appLib = concatPath(appRoot, "lib");
191 std::string probePath = appBin + ":" + appLib;
192 std::string tpa = getTPA();
193 std::string nativeLibPath = getExtraNativeLibDirs(appRoot) + ":" + appBin + ":" + appLib + ":" + __nativeLibDirectory;
194 std::string appName = std::string("dotnet-launcher-") + std::to_string(getpid());
196 if (!initializeCoreClr(appName.c_str(), probePath.c_str(), nativeLibPath.c_str(), tpa.c_str())) {
197 _ERR("Failed to initialize coreclr");
201 __initialized = true;
206 bool CoreRuntime::initializeCoreClr(const char* appId,
207 const char* assemblyProbePaths,
208 const char* pinvokeProbePaths,
211 const char *propertyKeys[] = {
212 "TRUSTED_PLATFORM_ASSEMBLIES",
215 "NATIVE_DLL_SEARCH_DIRECTORIES",
216 "AppDomainCompatSwitch"
219 const char *propertyValues[] = {
224 "UseLatestBehaviorWhenTFMNotSpecified"
227 std::string selfPath = readSelfPath();
229 int st = initializeClr(selfPath.c_str(),
231 sizeof(propertyKeys) / sizeof(propertyKeys[0]),
238 _ERR("initialize core clr fail! (0x%08x)", st);
242 pluginSetCoreclrInfo(__hostHandle, __domainId, createDelegate);
244 _DBG("Initialize core clr success");
248 void CoreRuntime::dispose()
250 if (__hostHandle != nullptr) {
251 int st = shutdown(__hostHandle, __domainId);
253 _ERR("shutdown core clr fail! (0x%08x)", st);
254 __hostHandle = nullptr;
257 if (__coreclrLib != nullptr) {
258 if (dlclose(__coreclrLib) != 0) {
259 _ERR("libcoreclr.so close failed");
262 __coreclrLib = nullptr;
265 finalizePluginManager();
266 finalizePathManager();
268 _DBG("Dotnet runtime disposed");
271 int CoreRuntime::launch(const char* appId, const char* root, const char* path, int argc, char* argv[])
273 if (!__initialized) {
274 _ERR("Runtime is not initialized");
278 if (path == nullptr) {
279 _ERR("executable path is null");
283 if (!isFileExist(path)) {
284 _ERR("File not exist : %s", path);
288 // launchpad override stdout and stderr to journalctl before launch application.
289 // we have to re-override that to input pipe for logging thread.
290 if (redirectFD() < 0) {
291 _ERR("Failed to redirect FD");
295 pluginSetAppInfo(appId, path);
297 int fd2 = open(root, O_DIRECTORY);
298 dup3(fd2, fd, O_CLOEXEC);
302 pluginBeforeExecute();
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