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"
41 #include "ni_common.h"
42 #include "db_manager.h"
43 #include "tac_common.h"
44 #include "path_manager.h"
45 #include "plugin_manager.h"
50 #define LOG_TAG "DOTNET_INSTALLER_PLUGIN"
53 #error "CROSSGEN_PATH is missed"
57 #define __STR(x) __XSTR(x)
58 #if defined(__arm__) || defined(__aarch64__)
59 static const char* __NATIVE_LIB_DIR = __STR(NATIVE_LIB_DIR);
61 static const char* __CROSSGEN_PATH = __STR(CROSSGEN_PATH);
62 static const char* __DOTNET_DIR = __STR(DOTNET_DIR);
64 #ifdef UNIQUE_DEFAULT_BASE_ADDR_SUPPORT
65 static const char* __SYSTEM_BASE_FILE = __STR(SYSTEM_BASE_FILE);
71 static int __interval = 0;
72 static PathManager* __pm = nullptr;
74 static void waitInterval()
76 // by the recommand, ignore small value for performance.
77 if (__interval > 10000) {
78 fprintf(stdout, "sleep %d usec\n", __interval);
83 static std::string getNIFilePath(const std::string& dllPath)
85 size_t index = dllPath.find_last_of(".");
86 if (index == std::string::npos) {
87 fprintf(stderr, "File doesnot contain extension. fail to get NI file name\n");
90 std::string fName = dllPath.substr(0, index);
91 std::string fExt = dllPath.substr(index, dllPath.length());
93 // crossgen generate file with lower case extension only
94 std::transform(fExt.begin(), fExt.end(), fExt.begin(), ::tolower);
95 std::string niPath = fName + ".ni" + fExt;
100 static std::string getAppNIFilePath(const std::string& niPath)
102 std::string fileName;
103 std::string niDirPath;
104 std::string prevPath;
106 size_t index = niPath.find_last_of("/");
107 if (index != std::string::npos) {
108 prevPath = niPath.substr(0, index);
109 fileName = niPath.substr(index + 1, niPath.length());
115 niDirPath = concatPath(prevPath, APP_NI_SUB_DIR);
117 if (!isFile(niDirPath)) {
118 if (mkdir(niDirPath.c_str(), 0755) == 0) {
119 copySmackAndOwnership(prevPath, niDirPath);
121 fprintf(stderr, "Fail to create app ni directory (%s)\n", niDirPath.c_str());
125 return concatPath(niDirPath, fileName);
128 static bool checkNIExistence(const std::string& path)
130 std::string f = getNIFilePath(path);
139 // native image of System.Private.CoreLib.dll should have to overwrite
140 // original file to support new coreclr
141 if (path.find("System.Private.CoreLib.dll") != std::string::npos) {
142 std::string coreLibBackup = path + ".Backup";
143 if (isFile(coreLibBackup)) {
151 #ifdef UNIQUE_DEFAULT_BASE_ADDR_SUPPORT
152 static uintptr_t getFileSize(const std::string& path)
156 if (stat(path.c_str(), &sb) == 0) {
163 // Get next base address to be used for system ni image from file
164 // __SYSTEM_BASE_FILE should be checked for existance before calling this function
165 static uintptr_t getNextBaseAddrFromFile()
167 FILE *pFile = fopen(__SYSTEM_BASE_FILE, "r");
169 fprintf(stderr, "Failed to open %s\n", __SYSTEM_BASE_FILE);
176 while (fscanf(pFile, "%" SCNxPTR " %" SCNuPTR "", &addr, &size) != EOF) {
184 // Get next base address to be used for system ni image
185 static uintptr_t getNextBaseAddr()
187 uintptr_t baseAddr = 0;
189 if (!isFile(__SYSTEM_BASE_FILE)) {
190 // This is the starting address for all default base addresses
191 baseAddr = DEFAULT_BASE_ADDR_START;
193 baseAddr = getNextBaseAddrFromFile();
195 // Round to a multple of 64K (see ZapImage::CalculateZapBaseAddress in CoreCLR)
196 uintptr_t BASE_ADDRESS_ALIGNMENT = 0xffff;
197 baseAddr = (baseAddr + BASE_ADDRESS_ALIGNMENT) & ~BASE_ADDRESS_ALIGNMENT;
203 // Save base address of system ni image to file
204 static void updateBaseAddrFile(const std::string& absNIPath, uintptr_t baseAddr)
206 uintptr_t niSize = getFileSize(absNIPath);
208 fprintf(stderr, "File %s doesn't exist\n", absNIPath.c_str());
212 // Write new entry to the file
213 FILE *pFile = fopen(__SYSTEM_BASE_FILE, "a");
215 fprintf(stderr, "Failed to open %s\n", __SYSTEM_BASE_FILE);
219 fprintf(pFile, "%" PRIxPTR " %" PRIuPTR "\n", baseAddr, niSize);
223 // check if dll is listed in TPA
224 static bool isTPADll(const std::string& dllPath)
226 std::string absPath = getBaseName(getAbsolutePath(dllPath));
228 std::vector<std::string> paths = __pm->getPlatformAssembliesPaths();
229 for (unsigned int i = 0; i < paths.size(); i++) {
230 if (paths[i].find(getBaseName(absPath)) != std::string::npos) {
239 // baseAddr should be checked in file before getting here
240 static ni_error_e crossgen(const std::string& dllPath, const std::string& appPath, DWORD flags)
242 if (!isFile(dllPath)) {
243 fprintf(stderr, "dll file is not exist : %s\n", dllPath.c_str());
244 return NI_ERROR_NO_SUCH_FILE;
247 if (!isManagedAssembly(dllPath)) {
248 //fprintf(stderr, "Input file is not a dll file : %s\n", dllPath.c_str());
249 return NI_ERROR_INVALID_PARAMETER;
252 if (checkNIExistence(dllPath)) {
253 //fprintf(stderr, "Already ni file is exist for %s\n", dllPath.c_str());
254 return NI_ERROR_ALREADY_EXIST;
257 std::string absDllPath = getAbsolutePath(dllPath);
258 std::string absNIPath = getNIFilePath(dllPath);
260 if (absNIPath.empty()) {
261 fprintf(stderr, "Fail to get ni file name\n");
262 return NI_ERROR_UNKNOWN;
265 bool isAppNI = flags & NI_FLAGS_APPNI;
266 if (isAppNI && strstr(absNIPath.c_str(), __DOTNET_DIR) == NULL) {
267 absNIPath = getAppNIFilePath(absNIPath);
270 #ifdef UNIQUE_DEFAULT_BASE_ADDR_SUPPORT
271 uintptr_t baseAddr = 0;
273 if (isTPADll(dllPath)) {
274 baseAddr = getNextBaseAddr();
280 return NI_ERROR_UNKNOWN;
284 waitpid(pid, &status, 0);
285 if (WIFEXITED(status)) {
286 // Do not use checkNIExistence() function to check whether ni file created or not.
287 // checkNIExistence() return false for System.Private.Corelib.dll
288 if (isFile(absNIPath)) {
289 copySmackAndOwnership(absDllPath, absNIPath);
290 std::string absPdbPath = replaceAll(absDllPath, ".dll", ".pdb");
291 std::string pdbFilePath = replaceAll(absNIPath, ".ni.dll", ".pdb");
292 if (isFile(absPdbPath) && (absPdbPath != pdbFilePath)) {
293 if (!copyFile(absPdbPath, pdbFilePath)) {
294 fprintf(stderr, "Failed to copy a .pdb file\n");
296 copySmackAndOwnership(absPdbPath, pdbFilePath);
299 #ifdef UNIQUE_DEFAULT_BASE_ADDR_SUPPORT
301 updateBaseAddrFile(absNIPath, baseAddr);
304 return NI_ERROR_NONE;
306 fprintf(stderr, "Fail to create native image for %s\n", dllPath.c_str());
307 return NI_ERROR_NO_SUCH_FILE;
311 std::string jitPath = __pm->getRuntimePath() + "/libclrjit.so";
312 std::vector<const char*> argv = {
315 "/JITPath", jitPath.c_str()
318 bool compat = flags & NI_FLAGS_COMPATIBILITY;
319 argv.push_back(compat ? "/Platform_Assemblies_Pathes" : "/p");
320 std::vector<std::string> paths = __pm->getPlatformAssembliesPaths();
321 std::string platformAssembliesPaths;
322 for (const auto &path : paths) {
323 if (!platformAssembliesPaths.empty()) {
324 platformAssembliesPaths += ":";
326 platformAssembliesPaths += path;
328 argv.push_back(platformAssembliesPaths.c_str());
330 bool enableR2R = flags & NI_FLAGS_ENABLER2R;
332 argv.push_back("/FragileNonVersionable");
335 if (flags & NI_FLAGS_VERBOSE) {
336 argv.push_back("/verbose");
339 if (flags & NI_FLAGS_INSTRUMENT) {
340 argv.push_back("/Tuning");
343 #ifdef UNIQUE_DEFAULT_BASE_ADDR_SUPPORT
344 std::string baseAddrString;
346 argv.push_back("/BaseAddress");
347 std::stringstream ss;
348 ss << "0x" << std::hex << baseAddr;
349 baseAddrString = ss.str();
350 argv.push_back(baseAddrString.c_str());
354 argv.push_back("/App_Paths");
355 std::string absAppPath;
356 if (!appPath.empty()) {
357 absAppPath = appPath;
359 absAppPath = getBaseName(absDllPath);
361 argv.push_back(absAppPath.c_str());
363 argv.push_back("/out");
364 argv.push_back(absNIPath.c_str());
366 argv.push_back(absDllPath.c_str());
367 argv.push_back(nullptr);
369 fprintf(stdout, "+ %s (%s)\n", absDllPath.c_str(), enableR2R ? "R2R" : "FNV");
371 execv(__CROSSGEN_PATH, const_cast<char* const*>(argv.data()));
375 return NI_ERROR_NONE;
378 // callback function of "pkgmgrinfo_appinfo_metadata_filter_foreach"
379 static int appAotCb(pkgmgrinfo_appinfo_h handle, void *userData)
383 DWORD *pFlags = (DWORD*)userData;
385 ret = pkgmgrinfo_appinfo_get_pkgid(handle, &pkgId);
386 if (ret != PMINFO_R_OK) {
387 fprintf(stderr, "Failed to get pkgid\n");
391 if (removeNIUnderPkgRoot(pkgId) != NI_ERROR_NONE) {
392 fprintf(stderr, "Failed to remove previous dlls from [%s]\n", pkgId);
396 if (createNIUnderPkgRoot(pkgId, *pFlags) != NI_ERROR_NONE) {
397 fprintf(stderr, "Failed to generate NI file [%s]\n", pkgId);
400 fprintf(stdout, "Complete make application to native image\n");
406 static bool isCoreLibPrepared(DWORD flags)
408 if (flags & NI_FLAGS_ENABLER2R) {
412 std::string coreLibBackup = concatPath(__pm->getRuntimePath(), "System.Private.CoreLib.dll.Backup");
413 if (isFile(coreLibBackup)) {
416 fprintf(stderr, "The native image of System.Private.CoreLib does not exist\n"
417 "Run the command to create the native image\n"
418 "# dotnettool --ni-dll /usr/share/dotnet.tizen/netcoreapp/System.Private.CoreLib.dll\n\n");
423 static bool hasCoreLibNI()
425 std::string ildasm = concatPath(__pm->getRuntimePath(), "ildasm");
426 std::string coreLib = concatPath(__pm->getRuntimePath(), "System.Private.CoreLib.dll");
427 std::string cmd = ildasm + " " + coreLib + " -noil -stats | grep '\\.xdata'";
430 fp = popen(cmd.c_str(), "r");
433 if (fgets(buff, sizeof(buff), fp) != NULL) {
442 static ni_error_e createCoreLibNI(DWORD flags)
444 std::string coreLib = concatPath(__pm->getRuntimePath(), "System.Private.CoreLib.dll");
445 std::string niCoreLib = concatPath(__pm->getRuntimePath(), "System.Private.CoreLib.ni.dll");
446 std::string coreLibBackup = concatPath(__pm->getRuntimePath(), "System.Private.CoreLib.dll.Backup");
448 if (!isFile(coreLibBackup) && !hasCoreLibNI()) {
449 if (!crossgen(coreLib, std::string(), flags)) {
450 if (rename(coreLib.c_str(), coreLibBackup.c_str())) {
451 fprintf(stderr, "Failed to rename System.Private.CoreLib.dll\n");
452 return NI_ERROR_CORE_NI_FILE;
454 if (rename(niCoreLib.c_str(), coreLib.c_str())) {
455 fprintf(stderr, "Failed to rename System.Private.CoreLib.ni.dll\n");
456 return NI_ERROR_CORE_NI_FILE;
459 fprintf(stderr, "Failed to create native image for %s\n", coreLib.c_str());
460 return NI_ERROR_CORE_NI_FILE;
463 return NI_ERROR_NONE;
466 ni_error_e initNICommon()
468 #if defined(__arm__) || defined(__aarch64__)
469 // get interval value
470 const static std::string intervalFile = concatPath(__NATIVE_LIB_DIR, "crossgen_interval.txt");
471 std::ifstream inFile(intervalFile);
473 fprintf(stdout, "crossgen_interval.txt is found\n");
474 inFile >> __interval;
477 if (initializePluginManager("normal")) {
478 fprintf(stderr, "Fail to initialize PluginManager\n");
479 return NI_ERROR_UNKNOWN;
483 __pm = new PathManager();
484 } catch (const std::exception& e) {
485 fprintf(stderr, "Failed to create PathManager");
486 return NI_ERROR_UNKNOWN;
489 char* pluginDllPaths = pluginGetDllPath();
490 if (pluginDllPaths) {
491 __pm->addPlatformAssembliesPaths(pluginDllPaths);
494 return NI_ERROR_NONE;
496 fprintf(stderr, "crossgen supports arm/arm64 architecture only. skip ni file generation\n");
497 return NI_ERROR_NOT_SUPPORTED;
501 void finalizeNICommon()
505 finalizePluginManager();
511 ni_error_e createNIPlatform(DWORD flags)
513 if (createCoreLibNI(flags) != NI_ERROR_NONE) {
514 return NI_ERROR_CORE_NI_FILE;
517 return createNIUnderDirs(__pm->getRuntimePath() + ":" + __pm->getTizenFXPath(), flags);
520 ni_error_e createNIDll(const std::string& dllPath, DWORD flags)
522 if (dllPath.find("System.Private.CoreLib.dll") != std::string::npos) {
523 return createCoreLibNI(flags);
526 if (!isCoreLibPrepared(flags)) {
527 return NI_ERROR_CORE_NI_FILE;
530 return crossgen(dllPath, std::string(), flags);
533 ni_error_e createNIUnderDirs(const std::string& rootPaths, DWORD flags)
535 if (!isCoreLibPrepared(flags)) {
536 return NI_ERROR_CORE_NI_FILE;
539 auto convert = [&rootPaths, flags](const std::string& path, const std::string& filename) {
540 // if path is symlink, donot generate crossgen
541 if (!crossgen(path, rootPaths.c_str(), flags)) {
546 std::vector<std::string> targetPaths;
547 splitPath(rootPaths, targetPaths);
548 for (const auto &path : targetPaths) {
549 // TAC directory should be handled specially because that contains symlink of native image file.
550 if (strstr(path.c_str(), TAC_SYMLINK_SUB_DIR) != NULL) {
551 if (!isDirectory(path)) {
554 // make native image symlink if not exist under tac directory
556 for (auto& symlinkAssembly : bf::recursive_directory_iterator(path)) {
557 std::string symPath = symlinkAssembly.path().string();
558 if (!isManagedAssembly(symPath)) {
562 // if there is symlink and original file for native image, skip generation
563 std::string symNIPath = symPath.substr(0, symPath.rfind(".dll")) + ".ni.dll";
564 if (isFile(symNIPath)) {
568 // if original native image not exist, generate native image
569 std::string originPath = bf::read_symlink(symPath).string();
570 std::string originNIPath = originPath.substr(0, originPath.rfind(".dll")) + ".ni.dll";
571 if (!isFile(originNIPath)) {
572 if (!crossgen(originPath, path.c_str(), flags)) {
577 // if no symlink file exist, create symlink
578 if (!isFile(symNIPath)) {
579 bf::create_symlink(originNIPath, symNIPath);
580 copySmackAndOwnership(symPath.c_str(), symNIPath.c_str(), true);
581 fprintf(stdout, "%s symbolic link file generated successfully.\n", symNIPath.c_str());
582 _INFO("%s symbolic link file generated successfully.", symNIPath.c_str());
585 } catch (const bf::filesystem_error& error) {
586 fprintf(stderr, "Failed to recursive directory: %s\n", error.what());
587 return NI_ERROR_UNKNOWN;
590 scanFilesInDirectory(path, convert, 0);
594 return NI_ERROR_NONE;
597 ni_error_e createNIUnderPkgRoot(const std::string& pkgId, DWORD flags)
599 std::string rootPath = getRootPath(pkgId);
600 if (rootPath.empty()) {
601 fprintf(stderr, "Failed to get root path from [%s]\n", pkgId.c_str());
602 return NI_ERROR_INVALID_PACKAGE;
605 __pm->setAppRootPath(rootPath);
607 flags |= NI_FLAGS_APPNI;
609 // create native image under bin and lib directory
610 // tac directory is skipped in the createNIUnderDirs.
611 return createNIUnderDirs(__pm->getAppPaths(), flags);
614 void removeNIPlatform()
616 std::string coreLib = concatPath(__pm->getRuntimePath(), "System.Private.CoreLib.dll");
617 std::string coreLibBackup = concatPath(__pm->getRuntimePath(), "System.Private.CoreLib.dll.Backup");
619 if (hasCoreLibNI()) {
620 if (!isFile(coreLibBackup)) {
624 if (remove(coreLib.c_str())) {
625 fprintf(stderr, "Failed to remove System.Private.CoreLib native image file\n");
627 if (rename(coreLibBackup.c_str(), coreLib.c_str())) {
628 fprintf(stderr, "Failed to rename System.Private.CoreLib.Backup to origin\n");
632 #ifdef UNIQUE_DEFAULT_BASE_ADDR_SUPPORT
633 if (isFile(__SYSTEM_BASE_FILE)) {
634 if (remove(__SYSTEM_BASE_FILE)) {
635 fprintf(stderr, "Failed to remove %s\n", __SYSTEM_BASE_FILE);
640 removeNIUnderDirs(__pm->getRuntimePath() + ":" + __pm->getTizenFXPath());
643 void removeNIUnderDirs(const std::string& rootPaths)
645 auto convert = [](const std::string& path, const std::string& filename) {
646 if (isNativeImage(path)) {
647 if (remove(path.c_str())) {
648 fprintf(stderr, "Failed to remove %s\n", path.c_str());
653 std::vector<std::string> paths;
654 splitPath(rootPaths, paths);
655 for (const auto &path : paths) {
656 scanFilesInDirectory(path, convert, -1);
660 ni_error_e removeNIUnderPkgRoot(const std::string& pkgId)
662 std::string rootPath = getRootPath(pkgId);
663 if (rootPath.empty()) {
664 fprintf(stderr, "Failed to get root path from [%s]\n", pkgId.c_str());
665 return NI_ERROR_INVALID_PACKAGE;
668 __pm->setAppRootPath(rootPath);
670 // getAppNIPaths returns bin/.native_image, lib/.native_image and .tac_symlink.
671 std::string appNIPaths = __pm->getAppNIPaths();
672 std::vector<std::string> paths;
673 splitPath(appNIPaths, paths);
674 for (const auto &path : paths) {
675 // Only the native image inside the TAC should be removed.
676 if (strstr(path.c_str(), TAC_SYMLINK_SUB_DIR) != NULL) {
677 removeNIUnderDirs(path);
679 if (isDirectory(path)) {
680 if (!removeAll(path.c_str())) {
681 fprintf(stderr, "Failed to remove app ni dir [%s]\n", path.c_str());
687 return NI_ERROR_NONE;
690 ni_error_e regenerateAppNI(DWORD flags)
692 if (!isCoreLibPrepared(flags)) {
693 return NI_ERROR_CORE_NI_FILE;
697 pkgmgrinfo_appinfo_metadata_filter_h handle;
699 ret = pkgmgrinfo_appinfo_metadata_filter_create(&handle);
700 if (ret != PMINFO_R_OK)
701 return NI_ERROR_UNKNOWN;
703 ret = pkgmgrinfo_appinfo_metadata_filter_add(handle, AOT_METADATA_KEY, METADATA_VALUE);
704 if (ret != PMINFO_R_OK) {
705 pkgmgrinfo_appinfo_metadata_filter_destroy(handle);
706 return NI_ERROR_UNKNOWN;
709 ret = pkgmgrMDFilterForeach(handle, appAotCb, &flags);
711 pkgmgrinfo_appinfo_metadata_filter_destroy(handle);
712 return NI_ERROR_UNKNOWN;
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 = openDB(TAC_APP_LIST_DB);
733 fprintf(stderr, "Sqlite open error\n");
736 sqlite3_exec(tac_db, "BEGIN;", NULL, NULL, NULL);
738 char *sql = sqlite3_mprintf("SELECT * FROM TAC WHERE PKGID = %Q;", pkgId);
739 std::vector<std::string> nugets = selectDB(tac_db, sql);
747 std::string nugetPaths;
748 for (const auto &nuget : nugets) {
749 if (!nugetPaths.empty()) {
752 nugetPaths += concatPath(__DOTNET_DIR, nuget);
755 auto convert = [&nugetPaths, pFlags](const std::string& path, const std::string& filename) {
756 if (strstr(path.c_str(), TAC_SHA_256_INFO) != NULL)
758 if (!crossgen(path, nugetPaths.c_str(), *pFlags)) {
763 for (auto& nuget : nugets) {
764 scanFilesInDirectory(concatPath(__DOTNET_DIR, nuget), convert, -1);
770 ni_error_e regenerateTACNI(DWORD flags)
772 if (!isCoreLibPrepared(flags)) {
773 return NI_ERROR_CORE_NI_FILE;
776 removeNIUnderDirs(__DOTNET_DIR);
778 pkgmgrinfo_appinfo_metadata_filter_h handle;
779 int ret = pkgmgrinfo_appinfo_metadata_filter_create(&handle);
780 if (ret != PMINFO_R_OK) {
781 return NI_ERROR_UNKNOWN;
784 ret = pkgmgrinfo_appinfo_metadata_filter_add(handle, TAC_METADATA_KEY, METADATA_VALUE);
785 if (ret != PMINFO_R_OK) {
786 pkgmgrinfo_appinfo_metadata_filter_destroy(handle);
787 return NI_ERROR_UNKNOWN;
790 ret = pkgmgrMDFilterForeach(handle, regenTacCb, &flags);
792 pkgmgrinfo_appinfo_metadata_filter_destroy(handle);
793 return NI_ERROR_UNKNOWN;
796 pkgmgrinfo_appinfo_metadata_filter_destroy(handle);
798 return NI_ERROR_NONE;