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 "dotnet_launcher.h"
31 namespace dotnetcore {
33 CoreRuntime::CoreRuntime() :
34 initializeClr(nullptr),
35 executeAssembly(nullptr),
37 createDelegate(nullptr),
38 __coreclrLib(nullptr),
39 __hostHandle(nullptr),
41 preparedFunction(nullptr),
42 launchFunction(nullptr)
45 #define __STR(x) __XSTR(x)
48 __deviceAPIDirectory = __STR(DEVICE_API_DIR);
51 __runtimeDirectory = __STR(RUNTIME_DIR);
54 __nativeLibDirectory = __STR(NATIVE_LIB_DIR);
57 #ifdef USE_MANAGED_LAUNCHER
58 #ifdef CORECLR_LAUNCHER_ASSEMBLY_PATH
59 __launcherAssembly = __STR(CORECLR_LAUNCHER_ASSEMBLY_PATH);
66 _DBG("Constructor called!!");
69 CoreRuntime::~CoreRuntime()
74 int CoreRuntime::initialize(bool standalone)
77 const char *deviceApiDirectory = getenv("__deviceAPIDirectory");
78 const char *runtimeDirectory = getenv("__runtimeDirectory");
79 if (deviceApiDirectory != nullptr)
80 __deviceAPIDirectory = deviceApiDirectory;
81 if (runtimeDirectory != nullptr)
82 __runtimeDirectory = runtimeDirectory;
84 #ifdef USE_MANAGED_LAUNCHER
85 const char *launcherAssembly = getenv("__launcherAssembly");
86 if (launcherAssembly != nullptr)
87 __launcherAssembly = launcherAssembly;
91 if (__deviceAPIDirectory.empty()) {
92 _ERR("Empty Device API Directory");
95 __deviceAPIDirectory = absolutePath(__deviceAPIDirectory);
98 if (__runtimeDirectory.empty()) {
99 _ERR("Empty Runtime Directory");
102 __runtimeDirectory = absolutePath(__runtimeDirectory);
105 #ifdef USE_MANAGED_LAUNCHER
106 if (__launcherAssembly.empty()) {
107 _ERR("Empty Launcher Assembly");
110 __launcherAssembly = absolutePath(__launcherAssembly);
114 std::string libCoreclr(concatPath(__runtimeDirectory, "libcoreclr.so"));
116 _DBG("libcoreclr : %s", libCoreclr.c_str());
118 __coreclrLib = dlopen(libCoreclr.c_str(), RTLD_NOW | RTLD_LOCAL);
119 if (__coreclrLib == nullptr) {
120 char *err = dlerror();
121 _ERR("dlopen failed to open libcoreclr.so with error %s", err);
125 #define CORELIB_RETURN_IF_NOSYM(type, variable, name) \
127 variable = (type)dlsym(__coreclrLib, name); \
128 if (variable == nullptr) { \
129 _ERR(name " is not found in the libcoreclr.so"); \
134 CORELIB_RETURN_IF_NOSYM(coreclr_initialize_ptr, initializeClr, "coreclr_initialize");
135 CORELIB_RETURN_IF_NOSYM(coreclr_execute_assembly_ptr, executeAssembly, "coreclr_execute_assembly");
136 CORELIB_RETURN_IF_NOSYM(coreclr_shutdown_ptr, shutdown, "coreclr_shutdown");
137 CORELIB_RETURN_IF_NOSYM(coreclr_create_delegate_ptr, createDelegate, "coreclr_create_delegate");
139 #undef CORELIB_RETURN_IF_NOSYM
141 _DBG("libcoreclr dlopen and dlsym success");
142 _DBG("this addr : %x", this);
143 _DBG("coreclr_initialize : %x", initializeClr);
148 bool CoreRuntime::initializeCoreClr(const char* appId,
149 const char* assemblyProbePaths,
150 const char* pinvokeProbePaths,
153 const char *propertyKeys[] = {
154 "TRUSTED_PLATFORM_ASSEMBLIES",
157 "NATIVE_DLL_SEARCH_DIRECTORIES",
158 "AppDomainCompatSwitch"
161 const char *propertyValues[] = {
166 "UseLatestBehaviorWhenTFMNotSpecified"
169 std::string selfPath = readSelfPath();
171 int st = initializeClr(selfPath.c_str(),
173 sizeof(propertyKeys) / sizeof(propertyKeys[0]),
180 _ERR("initialize core clr fail! (0x%08x)", st);
184 _DBG("Initialize core clr success");
188 int CoreRuntime::runManagedLauncher(const char* appId, const char* appBase, const char* tpaList)
190 if (fileNotExist(__launcherAssembly)) {
191 _ERR("Launcher assembly is not exist in %s", __launcherAssembly.c_str());
195 if (!initializeCoreClr(appId, appBase, appBase, tpaList)) {
196 _ERR("Failed to initialize coreclr");
200 #ifdef USE_MANAGED_LAUNCHER
201 void *preparedFunctionDelegate;
202 int st = createDelegate(__hostHandle, __domainId,
204 "Tizen.Runtime.Coreclr.AssemblyManager",
205 "Prepared", &preparedFunctionDelegate);
207 _ERR("Create delegate for Launch prepared function is fail (0x%08x)", st);
210 preparedFunction = reinterpret_cast<PreparedFunctionPtr>(preparedFunctionDelegate);
212 if (preparedFunction != nullptr)
215 void *launchFunctionDelegate;
216 st = createDelegate(__hostHandle, __domainId,
218 "Tizen.Runtime.Coreclr.AssemblyManager",
219 "Launch", &launchFunctionDelegate);
221 _ERR("Create delegate for Launch managed function is fail! (0x%08x)", st);
224 launchFunction = reinterpret_cast<LaunchFunctionPtr>(launchFunctionDelegate);
229 void CoreRuntime::dispose()
231 if (__hostHandle != nullptr) {
232 int st = shutdown(__hostHandle, __domainId);
234 _ERR("shutdown core clr fail! (0x%08x)", st);
237 if (dlclose(__coreclrLib) != 0)
238 _ERR("libcoreclr.so close failed");
240 __coreclrLib = nullptr;
242 _DBG("Dotnet runtime disposed");
245 int CoreRuntime::launch(const char* appId, const char* root, const char* path, int argc, char* argv[])
247 if (path == nullptr) {
248 _ERR("executable path is null");
252 if (fileNotExist(path)) {
253 _ERR("File not exist : %s", path);
258 std::string appRoot = root;
259 std::string appBin = concatPath(appRoot, "bin");
260 std::string appLib = concatPath(appRoot, "lib");
261 std::string probePath = appBin + ":" + appLib + ":" + __nativeLibDirectory;
263 std::vector<std::string> searchDirectories;
264 searchDirectories.push_back(appBin);
265 searchDirectories.push_back(appLib);
266 searchDirectories.push_back(__runtimeDirectory);
267 searchDirectories.push_back(__deviceAPIDirectory);
268 #ifdef USE_MANAGED_LAUNCHER
269 searchDirectories.push_back(baseName(__launcherAssembly));
272 assembliesInDirectory(searchDirectories, tpa);
274 #ifdef USE_MANAGED_LAUNCHER
275 runManagedLauncher(appId, probePath.c_str(), tpa.c_str());
277 bool success = false;
278 if (launchFunction != nullptr) {
279 std::string cppPath(path);
281 if (isManagedAssembly(cppPath) && !isNativeImage(cppPath)) {
282 size_t extindex = cppPath.size() - 4;
283 cppPath = cppPath.substr(0, extindex) + ".ni" + cppPath.substr(extindex, 4);
284 if (!fileNotExist(cppPath))
285 path = cppPath.c_str();
288 success = launchFunction(root, path, argc, argv);
290 _ERR("Failed to launch Application %s", path);
291 return success ? 0 : 1;
293 _ERR("Failed to find launch function");
297 int st = initializeCoreClr(appId, probePath.c_str(), probePath.c_str(), tpa.c_str());
298 unsigned int ret = 0;
299 st = executeAssembly(__hostHandle, __domainId, argc, (const char**)argv, path, &ret);
301 _ERR("Failed to Execute Assembly %s (0x%08x)", path, st);
306 } // namespace dotnetcore
307 } // namespace runtime