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.
17 #include <pkgmgr-info.h>
18 #include <pkgmgr_installer_info.h>
23 #include "pkgmgr_parser_plugin_interface.h"
38 #include "ni_common.h"
39 #include "tac_common.h"
40 #include "path_manager.h"
41 #include "plugin_manager.h"
46 #define LOG_TAG "DOTNET_INSTALLER_PLUGIN"
49 #error "CROSSGEN_PATH is missed"
53 #define __STR(x) __XSTR(x)
54 static const char* __CROSSGEN_PATH = __STR(CROSSGEN_PATH);
56 #ifdef UNIQUE_DEFAULT_BASE_ADDR_SUPPORT
57 static const char* __SYSTEM_BASE_FILE = __STR(SYSTEM_BASE_FILE);
63 static int __interval = 0;
64 static std::string __tpa;
66 static void waitInterval()
68 // by the recommand, ignore small value for performance.
69 if (__interval > 10000) {
70 fprintf(stderr, "sleep %d usec\n", __interval);
75 static std::string getNiFilePath(const std::string& dllPath)
77 size_t index = dllPath.find_last_of(".");
78 if (index == std::string::npos) {
79 fprintf(stderr, "File doesnot contain extension. fail to get NI file name\n");
82 std::string fName = dllPath.substr(0, index);
83 std::string fExt = dllPath.substr(index, dllPath.length());
85 // crossgen generate file with lower case extension only
86 std::transform(fExt.begin(), fExt.end(), fExt.begin(), ::tolower);
87 std::string niPath = fName + ".ni" + fExt;
92 static std::string getAppNIPath(const std::string& niPath)
95 std::string niDirPath;
98 size_t index = niPath.find_last_of("/");
99 if (index != std::string::npos) {
100 prevPath = niPath.substr(0, index);
101 fileName = niPath.substr(index + 1, niPath.length());
107 niDirPath = concatPath(prevPath, APP_NI_SUB_DIR);
109 if (!isFileExist(niDirPath)) {
110 if (mkdir(niDirPath.c_str(), 0755) == 0) {
111 updateAssemblyInfo(prevPath, niDirPath);
113 fprintf(stderr, "Fail to create app ni directory (%s)\n", niDirPath.c_str());
117 return concatPath(niDirPath, fileName);
120 static bool niExist(const std::string& path)
122 std::string f = getNiFilePath(path);
127 if (isFileExist(f)) {
131 // native image of System.Private.CoreLib.dll should have to overwrite
132 // original file to support new coreclr
133 if (path.find("System.Private.CoreLib.dll") != std::string::npos) {
134 std::string coreLibBackup = path + ".Backup";
135 if (isFileExist(coreLibBackup)) {
143 #ifdef UNIQUE_DEFAULT_BASE_ADDR_SUPPORT
144 // Get next base address to be used for system ni image from file
145 // __SYSTEM_BASE_FILE should be checked for existance before calling this function
146 static uintptr_t getNextBaseAddrFromFile()
148 FILE *pFile = fopen(__SYSTEM_BASE_FILE, "r");
150 fprintf(stderr, "Failed to open %s\n", __SYSTEM_BASE_FILE);
157 while (fscanf(pFile, "%u %u", &addr, &size) != EOF) {
165 // Get next base address to be used for system ni image
166 static uintptr_t getNextBaseAddr()
168 uintptr_t baseAddr = 0;
170 if (!isFileExist(__SYSTEM_BASE_FILE)) {
171 // This is the starting address for all default base addresses
172 baseAddr = DEFAULT_BASE_ADDR_START;
174 baseAddr = getNextBaseAddrFromFile();
176 // Round to a multple of 64K (see ZapImage::CalculateZapBaseAddress in CoreCLR)
177 uintptr_t BASE_ADDRESS_ALIGNMENT = 0xffff;
178 baseAddr = (baseAddr + BASE_ADDRESS_ALIGNMENT) & ~BASE_ADDRESS_ALIGNMENT;
184 // Save base address of system ni image to file
185 static void updateBaseAddrFile(const std::string &dllPath, uintptr_t baseAddr)
187 std::string absNiPath = getNiFilePath(dllPath);
188 if (absNiPath.empty()) {
189 fprintf(stderr, "Failed to get ni file path for %s\n", dllPath.c_str());
193 uintptr_t niSize = getFileSize(absNiPath);
195 fprintf(stderr, "File %s doesn't exist\n", absNiPath.c_str());
199 // Write new entry to the file
200 FILE *pFile = fopen(__SYSTEM_BASE_FILE, "a");
202 fprintf(stderr, "Failed to open %s\n", __SYSTEM_BASE_FILE);
206 fprintf(pFile, "%u %u\n", baseAddr, niSize);
210 // check if dll is listed in TPA
211 static bool isTPADll(const std::string &dllPath)
213 std::string absDllPath = absolutePath(dllPath);
215 if (__tpa.find(absDllPath) != std::string::npos) {
223 // baseAddr should be checked in file before getting here
224 static ni_error_e crossgen(const std::string& dllPath, const std::string& appPath, bool enableR2R, uintptr_t baseAddr = 0, bool isAppNI = false)
226 if (!isFileExist(dllPath)) {
227 fprintf(stderr, "dll file is not exist : %s\n", dllPath.c_str());
228 return NI_ERROR_NO_SUCH_FILE;
231 if (!isManagedAssembly(dllPath)) {
232 fprintf(stderr, "Input file is not a dll file : %s\n", dllPath.c_str());
233 return NI_ERROR_INVALID_PARAMETER;
236 if (niExist(dllPath)) {
237 fprintf(stderr, "Already ni file is exist for %s\n", dllPath.c_str());
238 return NI_ERROR_ALREADY_EXIST;
241 std::string absDllPath = absolutePath(dllPath);
242 std::string absNiPath = getNiFilePath(dllPath);
243 if (absNiPath.empty()) {
244 fprintf(stderr, "Fail to get ni file name\n");
245 return NI_ERROR_UNKNOWN;
249 absNiPath = getAppNIPath(absNiPath);
254 return NI_ERROR_UNKNOWN;
258 waitpid(pid, &status, 0);
259 if (WIFEXITED(status)) {
260 // Do not use niExist() function to check whether ni file created or not.
261 // niEixst() return false for System.Private.Corelib.dll
262 if (isFileExist(absNiPath)) {
263 updateAssemblyInfo(absDllPath, absNiPath);
264 return NI_ERROR_NONE;
266 fprintf(stderr, "Fail to create native image for %s\n", dllPath.c_str());
267 return NI_ERROR_NO_SUCH_FILE;
271 std::string jitPath = getRuntimeDir() + "/libclrjit.so";
272 std::vector<const char*> argv = {
275 "/Trusted_Platform_Assemblies", __tpa.c_str(),
276 "/JITPath", jitPath.c_str()
280 argv.push_back("/FragileNonVersionable");
283 #ifdef UNIQUE_DEFAULT_BASE_ADDR_SUPPORT
285 argv.push_back("/BaseAddress");
286 argv.push_back(std::to_string(baseAddr).c_str());
290 argv.push_back("/App_Paths");
291 std::string absAppPath;
292 if (!appPath.empty()) {
293 absAppPath = appPath;
295 absAppPath = baseName(absDllPath);
297 argv.push_back(absAppPath.c_str());
299 argv.push_back("/out");
300 argv.push_back(absNiPath.c_str());
302 argv.push_back(absDllPath.c_str());
303 argv.push_back(nullptr);
305 fprintf(stderr, "+ %s (%s)\n", absDllPath.c_str(), enableR2R ? "R2R" : "FNV");
307 execv(__CROSSGEN_PATH, const_cast<char* const*>(argv.data()));
311 return NI_ERROR_NONE;
314 // callback function of "pkgmgrinfo_appinfo_metadata_filter_foreach"
315 static int appAotCb(pkgmgrinfo_appinfo_h handle, void *userData)
319 bool* enableR2R = (bool*)userData;
321 ret = pkgmgrinfo_appinfo_get_pkgid(handle, &pkgId);
322 if (ret != PMINFO_R_OK) {
323 fprintf(stderr, "Failed to get pkgid\n");
327 if (removeNiUnderPkgRoot(pkgId) != NI_ERROR_NONE) {
328 fprintf(stderr, "Failed to remove previous dlls from [%s]\n", pkgId);
332 if (resetTACPackage(pkgId) != TAC_ERROR_NONE) {
333 fprintf(stderr, "Failed to remove symlink for given package [%s]\n", pkgId);
337 // Regenerate ni files with R2R mode forcibiliy. (there is no way to now which option is used)
338 if (createNiUnderPkgRoot(pkgId, *enableR2R) != NI_ERROR_NONE) {
339 fprintf(stderr, "Failed to generate NI file [%s]\n", pkgId);
342 fprintf(stderr, "Complete make application to native image\n");
345 if (createTACPackage(pkgId) != TAC_ERROR_NONE) {
346 fprintf(stderr, "Failed to generate symbolic link file [%s]\n", pkgId);
349 fprintf(stderr, "Complete make symbolic link file to tac\n");
355 static void createCoreLibNI(bool enableR2R, bool doGenUniqueBaseSystem)
357 std::string coreLib = concatPath(getRuntimeDir(), "System.Private.CoreLib.dll");
358 std::string niCoreLib = concatPath(getRuntimeDir(), "System.Private.CoreLib.ni.dll");
359 std::string coreLibBackup = concatPath(getRuntimeDir(), "System.Private.CoreLib.dll.Backup");
361 if (!isFileExist(coreLibBackup)) {
362 uintptr_t baseAddr = 0;
364 #ifdef UNIQUE_DEFAULT_BASE_ADDR_SUPPORT
365 if (doGenUniqueBaseSystem) {
366 baseAddr = getNextBaseAddr();
370 if (!crossgen(coreLib, std::string(), enableR2R, baseAddr)) {
371 #ifdef UNIQUE_DEFAULT_BASE_ADDR_SUPPORT
372 if (doGenUniqueBaseSystem && baseAddr != 0) {
373 updateBaseAddrFile(coreLib, baseAddr);
377 if (rename(coreLib.c_str(), coreLibBackup.c_str())) {
378 fprintf(stderr, "Failed to rename System.Private.CoreLib.dll\n");
380 if (rename(niCoreLib.c_str(), coreLib.c_str())) {
381 fprintf(stderr, "Failed to rename System.Private.CoreLib.ni.dll\n");
384 fprintf(stderr, "Failed to create native image for %s\n", coreLib.c_str());
389 ni_error_e initNICommon(NiCommonOption* option)
392 // get interval value
393 const char* intervalFile = "/usr/share/dotnet.tizen/lib/crossgen_interval.txt";
394 std::ifstream inFile(intervalFile);
396 fprintf(stderr, "crossgen_interval.txt is found\n");
397 inFile >> __interval;
400 if (initializePluginManager("normal")) {
401 fprintf(stderr, "Fail to initialize plugin manager\n");
402 return NI_ERROR_UNKNOWN;
404 if (initializePathManager(option->runtimeDir, option->tizenFXDir, option->extraDirs)) {
405 fprintf(stderr, "Fail to initialize path manager\n");
406 return NI_ERROR_UNKNOWN;
411 return NI_ERROR_NONE;
413 fprintf(stderr, "crossgen supports arm architecture only. skip ni file generation\n");
414 return NI_ERROR_NOT_SUPPORTED;
418 void finalizeNICommon()
422 finalizePluginManager();
423 finalizePathManager();
429 void createNiPlatform(bool enableR2R, bool doGenUniqueBaseSystem)
431 const std::string platformDirs[] = {getRuntimeDir(), getTizenFXDir()};
432 createNiUnderDirs(platformDirs, 2, enableR2R, doGenUniqueBaseSystem);
435 ni_error_e createNiDll(const std::string& dllPath, bool enableR2R, bool doGenUniqueBaseSystem)
437 createCoreLibNI(enableR2R, doGenUniqueBaseSystem);
438 // System.Private.CoreLib.dll is generated in the createCoreLibNI function.
439 // Skip if input dll is System.Private.CoreLib.dll
440 if (dllPath.find("System.Private.CoreLib.dll") != std::string::npos) {
441 return NI_ERROR_NONE;
444 uintptr_t baseAddr = 0;
446 #ifdef UNIQUE_DEFAULT_BASE_ADDR_SUPPORT
447 if (doGenUniqueBaseSystem && isTPADll(dllPath)) {
448 baseAddr = getNextBaseAddr();
452 ni_error_e status = crossgen(dllPath, std::string(), enableR2R, baseAddr);
454 #ifdef UNIQUE_DEFAULT_BASE_ADDR_SUPPORT
455 if (doGenUniqueBaseSystem && baseAddr != 0) {
456 updateBaseAddrFile(dllPath, baseAddr);
463 void createNiUnderDirs(const std::string rootPaths[], int count, bool enableR2R, bool doGenUniqueBaseSystem, bool isAppNI)
465 createCoreLibNI(enableR2R, doGenUniqueBaseSystem);
467 std::string appPaths;
468 for (int i = 0; i < count; i++) {
469 appPaths += rootPaths[i];
473 if (appPaths.back() == ':')
476 auto convert = [&appPaths, enableR2R, doGenUniqueBaseSystem, isAppNI](const std::string& path, const char* name) {
477 uintptr_t baseAddr = 0;
479 #ifdef UNIQUE_DEFAULT_BASE_ADDR_SUPPORT
480 if (doGenUniqueBaseSystem && !isAppNI && isTPADll(path)) {
481 baseAddr = getNextBaseAddr();
485 if (!crossgen(path, appPaths.c_str(), enableR2R, baseAddr, isAppNI)) {
486 #ifdef UNIQUE_DEFAULT_BASE_ADDR_SUPPORT
487 if (doGenUniqueBaseSystem && !isAppNI && baseAddr != 0) {
488 updateBaseAddrFile(path, baseAddr);
496 for (int i = 0; i < count; i++) {
497 scanFilesInDir(rootPaths[i], convert, 1);
501 ni_error_e createNiUnderPkgRoot(const std::string& pkgId, bool enableR2R)
504 if (getRootPath(pkgId, pkgRoot) < 0) {
505 return NI_ERROR_INVALID_PACKAGE;
508 std::string binDir = concatPath(pkgRoot, "bin");
509 std::string libDir = concatPath(pkgRoot, "lib");
510 std::string tacDir = concatPath(binDir, TAC_SYMLINK_SUB_DIR);
511 std::string paths[] = {binDir, libDir, tacDir};
513 createNiUnderDirs(paths, 3, enableR2R, false, true);
515 return NI_ERROR_NONE;
518 ni_error_e createNiDllUnderPkgRoot(const std::string& pkgId, const std::string& dllPath, bool enableR2R)
521 if (getRootPath(pkgId, pkgRoot) < 0) {
522 return NI_ERROR_INVALID_PACKAGE;
525 std::string binDir = concatPath(pkgRoot, "bin");
526 std::string libDir = concatPath(pkgRoot, "lib");
527 std::string tacDir = concatPath(binDir, TAC_SYMLINK_SUB_DIR);
528 std::string paths = binDir + ":" + libDir + ":" + tacDir;
530 if (bf::is_symlink(dllPath)) {
531 if (bf::exists(tacDir)) {
532 if (!isNativeImage(dllPath)) {
533 std::string originPath = bf::read_symlink(dllPath).string();
534 std::string originNiPath = originPath.substr(0, originPath.rfind(".dll")) + ".ni.dll";
535 if (!bf::exists(originNiPath)) {
536 if(createNiDll(originPath, false, false) != NI_ERROR_NONE) {
537 fprintf(stderr, "Failed to create NI file [%s]\n", originPath.c_str());
538 return NI_ERROR_UNKNOWN;
541 std::string setNiPath = dllPath.substr(0, dllPath.rfind(".dll")) + ".ni.dll";
542 if (!bf::exists(setNiPath)) {
543 bf::create_symlink(originNiPath, setNiPath);
544 fprintf(stderr, "%s symbolic link file generated successfully.\n", setNiPath.c_str());
545 updateAssemblyInfo(tacDir.c_str(), setNiPath.c_str(), true);
549 return NI_ERROR_NONE;
551 return crossgen(dllPath, paths, enableR2R, 0, true);
555 void removeNiPlatform()
557 std::string coreLib = concatPath(getRuntimeDir(), "System.Private.CoreLib.dll");
558 std::string coreLibBackup = concatPath(getRuntimeDir(), "System.Private.CoreLib.dll.Backup");
560 if (!isFileExist(coreLibBackup)) {
564 #ifdef UNIQUE_DEFAULT_BASE_ADDR_SUPPORT
565 if (isFileExist(__SYSTEM_BASE_FILE)) {
566 if (remove(__SYSTEM_BASE_FILE)) {
567 fprintf(stderr, "Failed to remove %s\n", __SYSTEM_BASE_FILE);
572 if (remove(coreLib.c_str())) {
573 fprintf(stderr, "Failed to remove System.Private.CoreLib native image file\n");
576 if (rename(coreLibBackup.c_str(), coreLib.c_str())) {
577 fprintf(stderr, "Failed to rename System.Private.CoreLib.Backup to origin\n");
580 const std::string platformDirs[] = {getRuntimeDir(), getTizenFXDir()};
582 removeNiUnderDirs(platformDirs, 2);
585 void removeNiUnderDirs(const std::string rootPaths[], int count)
587 auto convert = [](const std::string& path, std::string name) {
588 if (isNativeImage(path)) {
589 if (remove(path.c_str())) {
590 fprintf(stderr, "Failed to remove %s\n", path.c_str());
595 for (int i = 0; i < count; i++)
596 scanFilesInDir(rootPaths[i], convert, -1);
599 ni_error_e removeNiUnderPkgRoot(const std::string& pkgId)
602 if (getRootPath(pkgId, pkgRoot) < 0) {
603 return NI_ERROR_INVALID_PACKAGE;
606 std::string binDir = concatPath(pkgRoot, "bin");
607 std::string libDir = concatPath(pkgRoot, "lib");
608 std::string paths[] = {binDir, libDir};
610 removeNiUnderDirs(paths, 2);
612 std::string binNIDir = concatPath(binDir, APP_NI_SUB_DIR);
613 if (isFileExist(binNIDir)) {
614 if (rmdir(binNIDir.c_str()) != 0) {
615 fprintf(stderr, "Failed to remove app ni dir [%s]\n", binNIDir.c_str());
619 std::string libNIDir = concatPath(libDir, APP_NI_SUB_DIR);
620 if (isFileExist(libNIDir)) {
621 if (rmdir(libNIDir.c_str()) != 0) {
622 fprintf(stderr, "Failed to remove app ni dir [%s]\n", libNIDir.c_str());
626 return NI_ERROR_NONE;
629 ni_error_e regenerateAppNI(bool enableR2R)
632 pkgmgrinfo_appinfo_metadata_filter_h handle;
634 ret = pkgmgrinfo_appinfo_metadata_filter_create(&handle);
635 if (ret != PMINFO_R_OK)
636 return NI_ERROR_UNKNOWN;
638 ret = pkgmgrinfo_appinfo_metadata_filter_add(handle, AOT_METADATA_KEY, METADATA_VALUE);
639 if (ret != PMINFO_R_OK) {
640 pkgmgrinfo_appinfo_metadata_filter_destroy(handle);
641 return NI_ERROR_UNKNOWN;
644 ret = pkgmgrinfo_appinfo_metadata_filter_foreach(handle, appAotCb, &enableR2R);
645 if (ret != PMINFO_R_OK) {
646 fprintf(stderr, "Failed pkgmgrinfo_appinfo_metadata_filter_foreach\n");
647 pkgmgrinfo_appinfo_metadata_filter_destroy(handle);
648 return NI_ERROR_UNKNOWN;
651 fprintf(stderr, "Success pkgmgrinfo_appinfo_metadata_filter_foreach\n");
653 pkgmgrinfo_appinfo_metadata_filter_destroy(handle);
654 return NI_ERROR_NONE;