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);
53 #ifdef CORECLR_LAUNCHER_ASSEMBLY_PATH
54 LauncherAssembly = __STR(CORECLR_LAUNCHER_ASSEMBLY_PATH);
60 _DBG("Constructor called!!");
63 CoreRuntime::~CoreRuntime()
68 int CoreRuntime::Initialize(bool standalone)
73 const char *_deviceapi_directory = getenv("DeviceAPIDirectory");
74 const char *_runtime_directory = getenv("RuntimeDirectory");
75 const char *_launcher_assembly = getenv("LauncherAssembly");
76 if (_deviceapi_directory != nullptr)
77 DeviceAPIDirectory = _deviceapi_directory;
78 if (_runtime_directory != nullptr)
79 RuntimeDirectory = _runtime_directory;
80 if (_launcher_assembly != nullptr)
81 LauncherAssembly = _launcher_assembly;
84 if (DeviceAPIDirectory.empty())
86 _ERR("Empty Device API Directory");
91 DeviceAPIDirectory = AbsolutePath(DeviceAPIDirectory);
93 if (RuntimeDirectory.empty())
95 _ERR("Empty Runtime Directory");
100 RuntimeDirectory = AbsolutePath(RuntimeDirectory);
102 if (LauncherAssembly.empty())
104 _ERR("Empty Launcher Assembly");
109 LauncherAssembly = AbsolutePath(LauncherAssembly);
112 std::string libcoreclr(ConcatPath(RuntimeDirectory, "libcoreclr.so"));
114 _DBG("libcoreclr : %s", libcoreclr.c_str());
116 coreclrLib = dlopen(libcoreclr.c_str(), RTLD_NOW | RTLD_LOCAL);
117 if (coreclrLib == nullptr)
119 char *err = dlerror();
120 _ERR("dlopen failed to open libcoreclr.so with error %s", err);
124 #define CORELIB_RETURN_IF_NOSYM(type, variable, name) \
125 do { variable = (type)dlsym(coreclrLib, name); \
126 if (variable == nullptr) { \
127 _ERR(name " is not found in the libcoreclr.so"); \
131 CORELIB_RETURN_IF_NOSYM(coreclr_initialize_ptr, InitializeClr, "coreclr_initialize");
132 CORELIB_RETURN_IF_NOSYM(coreclr_execute_assembly_ptr, ExecuteAssembly, "coreclr_execute_assembly");
133 CORELIB_RETURN_IF_NOSYM(coreclr_shutdown_ptr, Shutdown, "coreclr_shutdown");
134 CORELIB_RETURN_IF_NOSYM(coreclr_create_delegate_ptr, CreateDelegate, "coreclr_create_delegate");
136 #undef CORELIB_RETURN_IF_NOSYM
138 _DBG("libcoreclr dlopen and dlsym success");
140 _DBG("this addr : %x", this);
141 _DBG("coreclr_initialize : %x", InitializeClr);
146 bool CoreRuntime::InitializeCoreClr(const char* assembly_probe_paths, const char* pinvoke_probe_paths)
148 std::vector<std::string> platformDirectories = {
149 RuntimeDirectory, DeviceAPIDirectory
152 std::string trusted_assemblies;
153 AssembliesInDirectory(platformDirectories, trusted_assemblies);
155 const char *propertyKeys[] =
157 "TRUSTED_PLATFORM_ASSEMBLIES",
160 "NATIVE_DLL_SEARCH_DIRECTORIES",
161 "AppDomainCompatSwitch"
164 const char *propertyValues[] =
166 trusted_assemblies.c_str(),
167 assembly_probe_paths,
168 assembly_probe_paths,
170 "UseLatestBehaviorWhenTFMNotSpecified"
173 std::string selfPath = ReadSelfPath();
175 int st = InitializeClr(
178 sizeof(propertyKeys) / sizeof(propertyKeys[0]),
186 _ERR("initialize core clr fail! (0x%08x)", st);
190 _DBG("Initialize core clr success");
194 int CoreRuntime::RunManagedLauncher()
196 if (FileNotExist(LauncherAssembly))
198 _ERR("Launcher assembly is not exist in %s", LauncherAssembly.c_str());
202 std::string launcherDir = Basename(LauncherAssembly);
203 std::vector<std::string> searchDirectories = {
204 RuntimeDirectory, DeviceAPIDirectory
207 std::string trusted_directories = JoinStrings(searchDirectories, ":");
209 _DBG("coreclr_dir : %s", RuntimeDirectory.c_str());
210 _DBG("native_so_search_dir : %s", trusted_directories.c_str());
211 _DBG("launcher_assembly : %s", LauncherAssembly.c_str());
212 _DBG("launcher_dir : %s", launcherDir.c_str());
214 if (!InitializeCoreClr(launcherDir.c_str(), launcherDir.c_str()))
216 _ERR("Failed to initialize coreclr");
220 void *preparedFunctionDelegate;
221 int st = CreateDelegate(hostHandle, domainId,
222 "Tizen.Runtime.Coreclr",
223 "Tizen.Runtime.Coreclr.AssemblyManager",
224 "Prepared", &preparedFunctionDelegate);
227 _ERR("Create delegate for Launch prepared function is fail (0x%08x)", st);
230 PreparedFunction = reinterpret_cast<PreparedFunctionPtr>(preparedFunctionDelegate);
232 if(PreparedFunction != nullptr)
237 void *launchFunctionDelegate;
238 st = CreateDelegate(hostHandle, domainId,
239 "Tizen.Runtime.Coreclr",
240 "Tizen.Runtime.Coreclr.AssemblyManager",
241 "Launch", &launchFunctionDelegate);
244 _ERR("Create delegate for Launch managed function is fail! (0x%08x)", st);
247 LaunchFunction = reinterpret_cast<LaunchFunctionPtr>(launchFunctionDelegate);
252 void CoreRuntime::Dispose()
254 if (hostHandle != nullptr)
256 int st = Shutdown(hostHandle, domainId);
259 _ERR("shutdown core clr fail! (0x%08x)", st);
263 if (dlclose(coreclrLib) != 0)
265 _ERR("libcoreclr.so close failed");
267 coreclrLib = nullptr;
269 _DBG("Dotnet runtime disposed");
272 int CoreRuntime::Launch(const char* root, const char* path, int argc, char* argv[])
276 _ERR("executable path is null");
280 std::string cpppath(path);
282 if (IsManagedAssembly(cpppath) && !IsNativeImage(cpppath))
284 size_t extindex = cpppath.size() - 4;
285 cpppath = cpppath.substr(0, extindex) + ".ni" + cpppath.substr(extindex, 4);
286 if (!FileNotExist(cpppath))
288 path = cpppath.c_str();
292 if (FileNotExist(path))
294 _ERR("File not exist : %s", path);
298 bool success = false;
299 if (LaunchFunction != nullptr)
301 success = LaunchFunction(root, path, argc, argv);
304 _ERR("Failed to launch Application %s", path);
306 return success ? 0 : 1;
310 std::string appRoot = root;
311 std::string appBin = ConcatPath(appRoot, "bin");
312 std::string appLib = ConcatPath(appRoot, "lib");
313 std::string probePath = appBin + ":" + appLib;
315 int st = InitializeCoreClr(probePath.c_str(), probePath.c_str());
316 unsigned int ret = 0;
317 st = ExecuteAssembly(hostHandle, domainId, argc, (const char**)argv, path, &ret);
320 _ERR("Failed to Execute Assembly %s (0x%08x)", path, st);
326 } // namespace dotnetcore
327 } // namespace runtime