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),
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)
79 const char *_deviceapi_directory = getenv("DeviceAPIDirectory");
80 const char *_runtime_directory = getenv("RuntimeDirectory");
81 if (_deviceapi_directory != nullptr)
82 DeviceAPIDirectory = _deviceapi_directory;
83 if (_runtime_directory != nullptr)
84 RuntimeDirectory = _runtime_directory;
86 #ifdef USE_MANAGED_LAUNCHER
87 const char *_launcher_assembly = getenv("LauncherAssembly");
88 if (_launcher_assembly != nullptr)
89 LauncherAssembly = _launcher_assembly;
93 if (DeviceAPIDirectory.empty())
95 _ERR("Empty Device API Directory");
100 DeviceAPIDirectory = AbsolutePath(DeviceAPIDirectory);
103 if (RuntimeDirectory.empty())
105 _ERR("Empty Runtime Directory");
110 RuntimeDirectory = AbsolutePath(RuntimeDirectory);
113 #ifdef USE_MANAGED_LAUNCHER
114 if (LauncherAssembly.empty())
116 _ERR("Empty Launcher Assembly");
121 LauncherAssembly = AbsolutePath(LauncherAssembly);
125 std::string libcoreclr(ConcatPath(RuntimeDirectory, "libcoreclr.so"));
127 _DBG("libcoreclr : %s", libcoreclr.c_str());
129 coreclrLib = dlopen(libcoreclr.c_str(), RTLD_NOW | RTLD_LOCAL);
130 if (coreclrLib == nullptr)
132 char *err = dlerror();
133 _ERR("dlopen failed to open libcoreclr.so with error %s", err);
137 #define CORELIB_RETURN_IF_NOSYM(type, variable, name) \
138 do { variable = (type)dlsym(coreclrLib, name); \
139 if (variable == nullptr) { \
140 _ERR(name " is not found in the libcoreclr.so"); \
144 CORELIB_RETURN_IF_NOSYM(coreclr_initialize_ptr, InitializeClr, "coreclr_initialize");
145 CORELIB_RETURN_IF_NOSYM(coreclr_execute_assembly_ptr, ExecuteAssembly, "coreclr_execute_assembly");
146 CORELIB_RETURN_IF_NOSYM(coreclr_shutdown_ptr, Shutdown, "coreclr_shutdown");
147 CORELIB_RETURN_IF_NOSYM(coreclr_create_delegate_ptr, CreateDelegate, "coreclr_create_delegate");
149 #undef CORELIB_RETURN_IF_NOSYM
151 _DBG("libcoreclr dlopen and dlsym success");
153 _DBG("this addr : %x", this);
154 _DBG("coreclr_initialize : %x", InitializeClr);
159 bool CoreRuntime::InitializeCoreClr(const char* app_id,
160 const char* assembly_probe_paths,
161 const char* pinvoke_probe_paths,
162 const char* tpa_list)
164 const char *propertyKeys[] =
166 "TRUSTED_PLATFORM_ASSEMBLIES",
169 "NATIVE_DLL_SEARCH_DIRECTORIES",
170 "AppDomainCompatSwitch"
173 const char *propertyValues[] =
176 assembly_probe_paths,
177 assembly_probe_paths,
179 "UseLatestBehaviorWhenTFMNotSpecified"
182 std::string selfPath = ReadSelfPath();
184 int st = InitializeClr(
187 sizeof(propertyKeys) / sizeof(propertyKeys[0]),
195 _ERR("initialize core clr fail! (0x%08x)", st);
199 _DBG("Initialize core clr success");
203 int CoreRuntime::RunManagedLauncher(const char* app_id, const char* app_base, const char* tpa_list)
205 if (FileNotExist(LauncherAssembly))
207 _ERR("Launcher assembly is not exist in %s", LauncherAssembly.c_str());
211 if (!InitializeCoreClr(app_id, app_base, app_base, tpa_list))
213 _ERR("Failed to initialize coreclr");
217 #ifdef USE_MANAGED_LAUNCHER
218 void *preparedFunctionDelegate;
219 int st = CreateDelegate(hostHandle, domainId,
220 "Tizen.Runtime.Coreclr",
221 "Tizen.Runtime.Coreclr.AssemblyManager",
222 "Prepared", &preparedFunctionDelegate);
225 _ERR("Create delegate for Launch prepared function is fail (0x%08x)", st);
228 PreparedFunction = reinterpret_cast<PreparedFunctionPtr>(preparedFunctionDelegate);
230 if(PreparedFunction != nullptr)
235 void *launchFunctionDelegate;
236 st = CreateDelegate(hostHandle, domainId,
237 "Tizen.Runtime.Coreclr",
238 "Tizen.Runtime.Coreclr.AssemblyManager",
239 "Launch", &launchFunctionDelegate);
242 _ERR("Create delegate for Launch managed function is fail! (0x%08x)", st);
245 LaunchFunction = reinterpret_cast<LaunchFunctionPtr>(launchFunctionDelegate);
250 void CoreRuntime::Dispose()
252 if (hostHandle != nullptr)
254 int st = Shutdown(hostHandle, domainId);
257 _ERR("shutdown core clr fail! (0x%08x)", st);
261 if (dlclose(coreclrLib) != 0)
263 _ERR("libcoreclr.so close failed");
265 coreclrLib = nullptr;
267 _DBG("Dotnet runtime disposed");
270 int CoreRuntime::Launch(const char* app_id, const char* root, const char* path, int argc, char* argv[])
274 _ERR("executable path is null");
278 if (FileNotExist(path))
280 _ERR("File not exist : %s", path);
284 std::vector<std::string> searchDirectories = {
285 RuntimeDirectory, DeviceAPIDirectory
286 #ifdef USE_MANAGED_LAUNCHER
287 , Basename(LauncherAssembly)
291 //std::string trusted_directories = JoinStrings(searchDirectories, ":");
293 AssembliesInDirectory(searchDirectories, tpa);
295 std::string appRoot = root;
296 std::string appBin = ConcatPath(appRoot, "bin");
297 std::string appLib = ConcatPath(appRoot, "lib");
298 std::string probePath = appBin + ":" + appLib + ":" + NativeLibDirectory;
300 #ifdef USE_MANAGED_LAUNCHER
301 RunManagedLauncher(app_id, probePath.c_str(), tpa.c_str());
303 bool success = false;
304 if (LaunchFunction != nullptr)
306 std::string cpppath(path);
308 if (IsManagedAssembly(cpppath) && !IsNativeImage(cpppath))
310 size_t extindex = cpppath.size() - 4;
311 cpppath = cpppath.substr(0, extindex) + ".ni" + cpppath.substr(extindex, 4);
312 if (!FileNotExist(cpppath))
314 path = cpppath.c_str();
318 success = LaunchFunction(root, path, argc, argv);
321 _ERR("Failed to launch Application %s", path);
323 return success ? 0 : 1;
327 _ERR("Failed to find launch function");
331 int st = InitializeCoreClr(app_id, probePath.c_str(), probePath.c_str(), tpa.c_str());
332 unsigned int ret = 0;
333 st = ExecuteAssembly(hostHandle, domainId, argc, (const char**)argv, path, &ret);
336 _ERR("Failed to Execute Assembly %s (0x%08x)", path, st);
342 } // namespace dotnetcore
343 } // namespace runtime