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"
39 #include "ni_common.h"
40 #include "db_manager.h"
41 #include "tac_common.h"
42 #include "path_manager.h"
43 #include "plugin_manager.h"
48 #define LOG_TAG "DOTNET_INSTALLER_PLUGIN"
51 #error "CROSSGEN_PATH is missed"
55 #define __STR(x) __XSTR(x)
56 static const char* __NATIVE_LIB_DIR = __STR(NATIVE_LIB_DIR);
57 static const char* __CROSSGEN_PATH = __STR(CROSSGEN_PATH);
58 static const char* __DOTNET_DIR = __STR(DOTNET_DIR);
60 #ifdef UNIQUE_DEFAULT_BASE_ADDR_SUPPORT
61 static const char* __SYSTEM_BASE_FILE = __STR(SYSTEM_BASE_FILE);
67 static int __interval = 0;
68 static PathManager* __pm = nullptr;
70 static void waitInterval()
72 // by the recommand, ignore small value for performance.
73 if (__interval > 10000) {
74 fprintf(stdout, "sleep %d usec\n", __interval);
79 static std::string getNIFilePath(const std::string& dllPath)
81 size_t index = dllPath.find_last_of(".");
82 if (index == std::string::npos) {
83 fprintf(stderr, "File doesnot contain extension. fail to get NI file name\n");
86 std::string fName = dllPath.substr(0, index);
87 std::string fExt = dllPath.substr(index, dllPath.length());
89 // crossgen generate file with lower case extension only
90 std::transform(fExt.begin(), fExt.end(), fExt.begin(), ::tolower);
91 std::string niPath = fName + ".ni" + fExt;
96 static std::string getAppNIFilePath(const std::string& niPath)
99 std::string niDirPath;
100 std::string prevPath;
102 size_t index = niPath.find_last_of("/");
103 if (index != std::string::npos) {
104 prevPath = niPath.substr(0, index);
105 fileName = niPath.substr(index + 1, niPath.length());
111 niDirPath = concatPath(prevPath, APP_NI_SUB_DIR);
113 if (!isFile(niDirPath)) {
114 if (mkdir(niDirPath.c_str(), 0755) == 0) {
115 copySmackAndOwnership(prevPath, niDirPath);
117 fprintf(stderr, "Fail to create app ni directory (%s)\n", niDirPath.c_str());
121 return concatPath(niDirPath, fileName);
124 static bool checkNIExistence(const std::string& path)
126 std::string f = getNIFilePath(path);
135 // native image of System.Private.CoreLib.dll should have to overwrite
136 // original file to support new coreclr
137 if (path.find("System.Private.CoreLib.dll") != std::string::npos) {
138 std::string coreLibBackup = path + ".Backup";
139 if (isFile(coreLibBackup)) {
147 #ifdef UNIQUE_DEFAULT_BASE_ADDR_SUPPORT
148 static uintptr_t getFileSize(const std::string& path)
152 if (stat(path.c_str(), &sb) == 0) {
159 // Get next base address to be used for system ni image from file
160 // __SYSTEM_BASE_FILE should be checked for existance before calling this function
161 static uintptr_t getNextBaseAddrFromFile()
163 FILE *pFile = fopen(__SYSTEM_BASE_FILE, "r");
165 fprintf(stderr, "Failed to open %s\n", __SYSTEM_BASE_FILE);
172 while (fscanf(pFile, "%u %u", &addr, &size) != EOF) {
180 // Get next base address to be used for system ni image
181 static uintptr_t getNextBaseAddr()
183 uintptr_t baseAddr = 0;
185 if (!isFile(__SYSTEM_BASE_FILE)) {
186 // This is the starting address for all default base addresses
187 baseAddr = DEFAULT_BASE_ADDR_START;
189 baseAddr = getNextBaseAddrFromFile();
191 // Round to a multple of 64K (see ZapImage::CalculateZapBaseAddress in CoreCLR)
192 uintptr_t BASE_ADDRESS_ALIGNMENT = 0xffff;
193 baseAddr = (baseAddr + BASE_ADDRESS_ALIGNMENT) & ~BASE_ADDRESS_ALIGNMENT;
199 // Save base address of system ni image to file
200 static void updateBaseAddrFile(const std::string& absNIPath, uintptr_t baseAddr)
202 uintptr_t niSize = getFileSize(absNIPath);
204 fprintf(stderr, "File %s doesn't exist\n", absNIPath.c_str());
208 // Write new entry to the file
209 FILE *pFile = fopen(__SYSTEM_BASE_FILE, "a");
211 fprintf(stderr, "Failed to open %s\n", __SYSTEM_BASE_FILE);
215 fprintf(pFile, "%u %u\n", baseAddr, niSize);
219 // check if dll is listed in TPA
220 static bool isTPADll(const std::string& dllPath)
222 std::string absPath = getBaseName(getAbsolutePath(dllPath));
224 std::vector<std::string> paths = __pm->getPlatformAssembliesPaths();
225 for (unsigned int i = 0; i < paths.size(); i++) {
226 if (paths[i].find(getBaseName(absPath)) != std::string::npos) {
235 // baseAddr should be checked in file before getting here
236 static ni_error_e crossgen(const std::string& dllPath, const std::string& appPath, DWORD flags)
238 if (!isFile(dllPath)) {
239 fprintf(stderr, "dll file is not exist : %s\n", dllPath.c_str());
240 return NI_ERROR_NO_SUCH_FILE;
243 if (!isManagedAssembly(dllPath)) {
244 //fprintf(stderr, "Input file is not a dll file : %s\n", dllPath.c_str());
245 return NI_ERROR_INVALID_PARAMETER;
248 if (checkNIExistence(dllPath)) {
249 //fprintf(stderr, "Already ni file is exist for %s\n", dllPath.c_str());
250 return NI_ERROR_ALREADY_EXIST;
253 std::string absDllPath = getAbsolutePath(dllPath);
254 std::string absNIPath = getNIFilePath(dllPath);
256 if (absNIPath.empty()) {
257 fprintf(stderr, "Fail to get ni file name\n");
258 return NI_ERROR_UNKNOWN;
261 bool isAppNI = flags & NI_FLAGS_APPNI;
262 if (isAppNI && strstr(absNIPath.c_str(), __DOTNET_DIR) == NULL) {
263 absNIPath = getAppNIFilePath(absNIPath);
266 #ifdef UNIQUE_DEFAULT_BASE_ADDR_SUPPORT
267 uintptr_t baseAddr = 0;
269 if (isTPADll(dllPath)) {
270 baseAddr = getNextBaseAddr();
276 return NI_ERROR_UNKNOWN;
280 waitpid(pid, &status, 0);
281 if (WIFEXITED(status)) {
282 // Do not use checkNIExistence() function to check whether ni file created or not.
283 // checkNIExistence() return false for System.Private.Corelib.dll
284 if (isFile(absNIPath)) {
285 copySmackAndOwnership(absDllPath, absNIPath);
286 std::string absPdbPath = replaceAll(absDllPath, ".dll", ".pdb");
287 std::string pdbFilePath = replaceAll(absNIPath, ".ni.dll", ".pdb");
288 if (isFile(absPdbPath) && (absPdbPath != pdbFilePath)) {
289 if (!copyFile(absPdbPath, pdbFilePath)) {
290 fprintf(stderr, "Failed to copy a .pdb file\n");
292 copySmackAndOwnership(absPdbPath, pdbFilePath);
295 #ifdef UNIQUE_DEFAULT_BASE_ADDR_SUPPORT
297 updateBaseAddrFile(absNIPath, baseAddr);
300 return NI_ERROR_NONE;
302 fprintf(stderr, "Fail to create native image for %s\n", dllPath.c_str());
303 return NI_ERROR_NO_SUCH_FILE;
307 std::string jitPath = __pm->getRuntimePath() + "/libclrjit.so";
308 std::vector<const char*> argv = {
311 "/JITPath", jitPath.c_str()
314 bool compat = flags & NI_FLAGS_COMPATIBILITY;
315 argv.push_back(compat ? "/Platform_Assemblies_Pathes" : "/p");
316 std::vector<std::string> paths = __pm->getPlatformAssembliesPaths();
317 std::string platformAssembliesPaths;
318 for (const auto &path : paths) {
319 if (!platformAssembliesPaths.empty()) {
320 platformAssembliesPaths += ":";
322 platformAssembliesPaths += path;
324 argv.push_back(platformAssembliesPaths.c_str());
326 bool enableR2R = flags & NI_FLAGS_ENABLER2R;
328 argv.push_back("/FragileNonVersionable");
331 if (flags & NI_FLAGS_VERBOSE) {
332 argv.push_back("/verbose");
335 if (flags & NI_FLAGS_INSTRUMENT) {
336 argv.push_back("/Tuning");
339 #ifdef UNIQUE_DEFAULT_BASE_ADDR_SUPPORT
341 argv.push_back("/BaseAddress");
342 argv.push_back(std::to_string(baseAddr).c_str());
346 argv.push_back("/App_Paths");
347 std::string absAppPath;
348 if (!appPath.empty()) {
349 absAppPath = appPath;
351 absAppPath = getBaseName(absDllPath);
353 argv.push_back(absAppPath.c_str());
355 argv.push_back("/out");
356 argv.push_back(absNIPath.c_str());
358 argv.push_back(absDllPath.c_str());
359 argv.push_back(nullptr);
361 fprintf(stdout, "+ %s (%s)\n", absDllPath.c_str(), enableR2R ? "R2R" : "FNV");
363 execv(__CROSSGEN_PATH, const_cast<char* const*>(argv.data()));
367 return NI_ERROR_NONE;
370 // callback function of "pkgmgrinfo_appinfo_metadata_filter_foreach"
371 static int appAotCb(pkgmgrinfo_appinfo_h handle, void *userData)
375 DWORD *pFlags = (DWORD*)userData;
377 ret = pkgmgrinfo_appinfo_get_pkgid(handle, &pkgId);
378 if (ret != PMINFO_R_OK) {
379 fprintf(stderr, "Failed to get pkgid\n");
383 if (removeNIUnderPkgRoot(pkgId) != NI_ERROR_NONE) {
384 fprintf(stderr, "Failed to remove previous dlls from [%s]\n", pkgId);
388 if (createNIUnderPkgRoot(pkgId, *pFlags) != NI_ERROR_NONE) {
389 fprintf(stderr, "Failed to generate NI file [%s]\n", pkgId);
392 fprintf(stdout, "Complete make application to native image\n");
398 static bool isCoreLibPrepared(DWORD flags)
400 if (flags & NI_FLAGS_ENABLER2R) {
404 std::string coreLibBackup = concatPath(__pm->getRuntimePath(), "System.Private.CoreLib.dll.Backup");
405 if (isFile(coreLibBackup)) {
408 fprintf(stderr, "The native image of System.Private.CoreLib does not exist\n"
409 "Run the command to create the native image\n"
410 "# dotnettool --ni-dll /usr/share/dotnet.tizen/netcoreapp/System.Private.CoreLib.dll\n\n");
415 static bool hasCoreLibNI()
419 std::string ildasm = concatPath(__pm->getRuntimePath(), "ildasm");
420 std::string coreLib = concatPath(__pm->getRuntimePath(), "System.Private.CoreLib.dll");
421 std::string cmd = ildasm + " " + coreLib + " | grep '\\.corflags'";
422 fp = popen(cmd.c_str(), "r");
424 while (fgets(buff, sizeof(buff), fp) != NULL) {
425 buff[strlen(buff) - 1] = '\0';
427 std::string corflag = replaceAll(buff, ".corflags", "");
428 corflag.erase(std::remove(corflag.begin(), corflag.end(), ' '), corflag.end());
429 // CorFlags.ILLibrary=0x00000004 (.ni.dll)
430 if (!strcmp(corflag.substr(0, 10).c_str(), "0x00000004")) {
439 static ni_error_e createCoreLibNI(DWORD flags)
441 std::string coreLib = concatPath(__pm->getRuntimePath(), "System.Private.CoreLib.dll");
442 std::string niCoreLib = concatPath(__pm->getRuntimePath(), "System.Private.CoreLib.ni.dll");
443 std::string coreLibBackup = concatPath(__pm->getRuntimePath(), "System.Private.CoreLib.dll.Backup");
445 if (!isFile(coreLibBackup) && !hasCoreLibNI()) {
446 if (!crossgen(coreLib, std::string(), flags)) {
447 if (rename(coreLib.c_str(), coreLibBackup.c_str())) {
448 fprintf(stderr, "Failed to rename System.Private.CoreLib.dll\n");
449 return NI_ERROR_CORE_NI_FILE;
451 if (rename(niCoreLib.c_str(), coreLib.c_str())) {
452 fprintf(stderr, "Failed to rename System.Private.CoreLib.ni.dll\n");
453 return NI_ERROR_CORE_NI_FILE;
456 fprintf(stderr, "Failed to create native image for %s\n", coreLib.c_str());
457 return NI_ERROR_CORE_NI_FILE;
460 return NI_ERROR_NONE;
463 ni_error_e initNICommon()
465 #if defined(__arm__) || defined(__aarch64__)
466 // get interval value
467 const static std::string intervalFile = concatPath(__NATIVE_LIB_DIR, "crossgen_interval.txt");
468 std::ifstream inFile(intervalFile);
470 fprintf(stdout, "crossgen_interval.txt is found\n");
471 inFile >> __interval;
474 if (initializePluginManager("normal")) {
475 fprintf(stderr, "Fail to initialize PluginManager\n");
476 return NI_ERROR_UNKNOWN;
480 __pm = new PathManager();
481 } catch (const std::exception& e) {
482 fprintf(stderr, "Failed to create PathManager");
483 return NI_ERROR_UNKNOWN;
486 char* pluginDllPaths = pluginGetDllPath();
487 if (pluginDllPaths) {
488 __pm->addPlatformAssembliesPaths(pluginDllPaths);
491 return NI_ERROR_NONE;
493 fprintf(stderr, "crossgen supports arm/arm64 architecture only. skip ni file generation\n");
494 return NI_ERROR_NOT_SUPPORTED;
498 void finalizeNICommon()
502 finalizePluginManager();
508 ni_error_e createNIPlatform(DWORD flags)
510 if (createCoreLibNI(flags) != NI_ERROR_NONE) {
511 return NI_ERROR_CORE_NI_FILE;
514 return createNIUnderDirs(__pm->getRuntimePath() + ":" + __pm->getTizenFXPath(), flags);
517 ni_error_e createNIDll(const std::string& dllPath, DWORD flags)
519 if (dllPath.find("System.Private.CoreLib.dll") != std::string::npos) {
520 return createCoreLibNI(flags);
523 if (!isCoreLibPrepared(flags)) {
524 return NI_ERROR_CORE_NI_FILE;
527 return crossgen(dllPath, std::string(), flags);
530 ni_error_e createNIUnderDirs(const std::string rootPaths, DWORD flags)
532 if (!isCoreLibPrepared(flags)) {
533 return NI_ERROR_CORE_NI_FILE;
536 auto convert = [&rootPaths, flags](const std::string& path, const std::string& filename) {
537 // if path is symlink, donot generate crossgen
538 if (!crossgen(path, rootPaths.c_str(), flags)) {
543 std::vector<std::string> targetPaths;
544 splitPath(rootPaths, targetPaths);
545 for (const auto &path : targetPaths) {
546 // TAC directory should be handled specially because that contains symlink of native image file.
547 if (strstr(path.c_str(), TAC_SYMLINK_SUB_DIR) != NULL) {
548 if (!isDirectory(path)) {
551 // make native image symlink if not exist under tac directory
553 for (auto& symlinkAssembly : bf::recursive_directory_iterator(path)) {
554 std::string symPath = symlinkAssembly.path().string();
555 if (!isManagedAssembly(symPath)) {
559 // if there is symlink and original file for native image, skip generation
560 std::string symNIPath = symPath.substr(0, symPath.rfind(".dll")) + ".ni.dll";
561 if (isFile(symNIPath)) {
565 // if original native image not exist, generate native image
566 std::string originPath = bf::read_symlink(symPath).string();
567 std::string originNIPath = originPath.substr(0, originPath.rfind(".dll")) + ".ni.dll";
568 if (!isFile(originNIPath)) {
569 if (!crossgen(originPath, path.c_str(), flags)) {
574 // if no symlink file exist, create symlink
575 if (!isFile(symNIPath)) {
576 bf::create_symlink(originNIPath, symNIPath);
577 copySmackAndOwnership(symPath.c_str(), symNIPath.c_str(), true);
578 fprintf(stdout, "%s symbolic link file generated successfully.\n", symNIPath.c_str());
579 _INFO("%s symbolic link file generated successfully.", symNIPath.c_str());
582 } catch (const bf::filesystem_error& error) {
583 fprintf(stderr, "Failed to recursive directory: %s\n", error.what());
584 return NI_ERROR_UNKNOWN;
587 scanFilesInDirectory(path, convert, 0);
591 return NI_ERROR_NONE;
594 ni_error_e createNIUnderPkgRoot(const std::string& pkgId, DWORD flags)
596 std::string rootPath = getRootPath(pkgId);
597 if (rootPath.empty()) {
598 fprintf(stderr, "Failed to get root path from [%s]\n", pkgId.c_str());
599 return NI_ERROR_INVALID_PACKAGE;
602 __pm->setAppRootPath(rootPath);
604 flags |= NI_FLAGS_APPNI;
606 // create native image under bin and lib directory
607 // tac directory is skipped in the createNIUnderDirs.
608 return createNIUnderDirs(__pm->getAppPaths(), flags);
611 void removeNIPlatform()
613 std::string coreLib = concatPath(__pm->getRuntimePath(), "System.Private.CoreLib.dll");
614 std::string coreLibBackup = concatPath(__pm->getRuntimePath(), "System.Private.CoreLib.dll.Backup");
616 if (hasCoreLibNI()) {
617 if (!isFile(coreLibBackup)) {
621 if (remove(coreLib.c_str())) {
622 fprintf(stderr, "Failed to remove System.Private.CoreLib native image file\n");
624 if (rename(coreLibBackup.c_str(), coreLib.c_str())) {
625 fprintf(stderr, "Failed to rename System.Private.CoreLib.Backup to origin\n");
629 #ifdef UNIQUE_DEFAULT_BASE_ADDR_SUPPORT
630 if (isFile(__SYSTEM_BASE_FILE)) {
631 if (remove(__SYSTEM_BASE_FILE)) {
632 fprintf(stderr, "Failed to remove %s\n", __SYSTEM_BASE_FILE);
637 removeNIUnderDirs(__pm->getRuntimePath() + ":" + __pm->getTizenFXPath());
640 void removeNIUnderDirs(const std::string rootPaths)
642 auto convert = [](const std::string& path, const std::string& filename) {
643 if (isNativeImage(path)) {
644 if (remove(path.c_str())) {
645 fprintf(stderr, "Failed to remove %s\n", path.c_str());
650 std::vector<std::string> paths;
651 splitPath(rootPaths, paths);
652 for (const auto &path : paths) {
653 scanFilesInDirectory(path, convert, -1);
657 ni_error_e removeNIUnderPkgRoot(const std::string& pkgId)
659 std::string rootPath = getRootPath(pkgId);
660 if (rootPath.empty()) {
661 fprintf(stderr, "Failed to get root path from [%s]\n", pkgId.c_str());
662 return NI_ERROR_INVALID_PACKAGE;
665 __pm->setAppRootPath(rootPath);
667 // getAppNIPaths returns bin/.native_image, lib/.native_image and .tac_symlink.
668 std::string appNIPaths = __pm->getAppNIPaths();
669 std::vector<std::string> paths;
670 splitPath(appNIPaths, paths);
671 for (const auto &path : paths) {
672 // Only the native image inside the TAC should be removed.
673 if (strstr(path.c_str(), TAC_SYMLINK_SUB_DIR) != NULL) {
674 removeNIUnderDirs(path);
676 if (isDirectory(path)) {
677 if (!removeAll(path.c_str())) {
678 fprintf(stderr, "Failed to remove app ni dir [%s]\n", path.c_str());
684 return NI_ERROR_NONE;
687 ni_error_e regenerateAppNI(DWORD flags)
689 if (!isCoreLibPrepared(flags)) {
690 return NI_ERROR_CORE_NI_FILE;
694 pkgmgrinfo_appinfo_metadata_filter_h handle;
696 ret = pkgmgrinfo_appinfo_metadata_filter_create(&handle);
697 if (ret != PMINFO_R_OK)
698 return NI_ERROR_UNKNOWN;
700 ret = pkgmgrinfo_appinfo_metadata_filter_add(handle, AOT_METADATA_KEY, METADATA_VALUE);
701 if (ret != PMINFO_R_OK) {
702 pkgmgrinfo_appinfo_metadata_filter_destroy(handle);
703 return NI_ERROR_UNKNOWN;
706 ret = pkgmgrinfo_appinfo_metadata_filter_foreach(handle, appAotCb, &flags);
707 if (ret != PMINFO_R_OK) {
708 fprintf(stderr, "Failed pkgmgrinfo_appinfo_metadata_filter_foreach\n");
709 pkgmgrinfo_appinfo_metadata_filter_destroy(handle);
710 return NI_ERROR_UNKNOWN;
713 fprintf(stdout, "Success pkgmgrinfo_appinfo_metadata_filter_foreach\n");
715 pkgmgrinfo_appinfo_metadata_filter_destroy(handle);
716 return NI_ERROR_NONE;
719 // callback function of "pkgmgrinfo_appinfo_metadata_filter_foreach"
720 static int regenTacCb(pkgmgrinfo_appinfo_h handle, void *userData)
723 DWORD *pFlags = (DWORD*)userData;
725 int ret = pkgmgrinfo_appinfo_get_pkgid(handle, &pkgId);
726 if (ret != PMINFO_R_OK || pkgId == NULL) {
727 fprintf(stderr, "Failed to get pkgid\n");
731 sqlite3 *tac_db = dbOpen(TAC_APP_LIST_DB);
733 fprintf(stderr, "Sqlite open error\n");
737 char *sql = sqlite3_mprintf("SELECT * FROM TAC WHERE PKGID = %Q;", pkgId);
738 std::vector<std::string> nugets = dbSelect(tac_db, TAC_APP_LIST_DB, sql);
746 std::string nugetPaths;
747 for (const auto &nuget : nugets) {
748 if (!nugetPaths.empty()) {
751 nugetPaths += concatPath(__DOTNET_DIR, nuget);
754 auto convert = [&nugetPaths, pFlags](const std::string& path, const std::string& filename) {
755 if (strstr(path.c_str(), TAC_SHA_256_INFO) != NULL)
757 if (!crossgen(path, nugetPaths.c_str(), *pFlags)) {
762 for (auto& nuget : nugets) {
763 scanFilesInDirectory(concatPath(__DOTNET_DIR, nuget), convert, -1);
769 ni_error_e regenerateTACNI(DWORD flags)
771 if (!isCoreLibPrepared(flags)) {
772 return NI_ERROR_CORE_NI_FILE;
775 removeNIUnderDirs(__DOTNET_DIR);
777 pkgmgrinfo_appinfo_metadata_filter_h handle;
778 int ret = pkgmgrinfo_appinfo_metadata_filter_create(&handle);
779 if (ret != PMINFO_R_OK) {
780 return NI_ERROR_UNKNOWN;
783 ret = pkgmgrinfo_appinfo_metadata_filter_add(handle, TAC_METADATA_KEY, METADATA_VALUE);
784 if (ret != PMINFO_R_OK) {
785 pkgmgrinfo_appinfo_metadata_filter_destroy(handle);
786 return NI_ERROR_UNKNOWN;
789 ret = pkgmgrinfo_appinfo_metadata_filter_foreach(handle, regenTacCb, &flags);
790 if (ret != PMINFO_R_OK) {
791 fprintf(stderr, "Failed pkgmgrinfo_appinfo_metadata_filter_foreach\n");
792 pkgmgrinfo_appinfo_metadata_filter_destroy(handle);
793 return NI_ERROR_UNKNOWN;
795 fprintf(stdout, "Success pkgmgrinfo_appinfo_metadata_filter_foreach\n");
797 pkgmgrinfo_appinfo_metadata_filter_destroy(handle);
799 return NI_ERROR_NONE;