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()
427 std::string ildasm = concatPath(__pm->getRuntimePath(), "ildasm");
428 std::string coreLib = concatPath(__pm->getRuntimePath(), "System.Private.CoreLib.dll");
429 std::string cmd = ildasm + " " + coreLib + " | grep '\\.corflags'";
430 fp = popen(cmd.c_str(), "r");
432 while (fgets(buff, sizeof(buff), fp) != NULL) {
433 buff[strlen(buff) - 1] = '\0';
435 std::string corflag = replaceAll(buff, ".corflags", "");
436 corflag.erase(std::remove(corflag.begin(), corflag.end(), ' '), corflag.end());
437 // CorFlags.ILLibrary=0x00000004 (.ni.dll)
438 if (!strcmp(corflag.substr(0, 10).c_str(), "0x00000004")) {
447 static ni_error_e createCoreLibNI(DWORD flags)
449 std::string coreLib = concatPath(__pm->getRuntimePath(), "System.Private.CoreLib.dll");
450 std::string niCoreLib = concatPath(__pm->getRuntimePath(), "System.Private.CoreLib.ni.dll");
451 std::string coreLibBackup = concatPath(__pm->getRuntimePath(), "System.Private.CoreLib.dll.Backup");
453 if (!isFile(coreLibBackup) && !hasCoreLibNI()) {
454 if (!crossgen(coreLib, std::string(), flags)) {
455 if (rename(coreLib.c_str(), coreLibBackup.c_str())) {
456 fprintf(stderr, "Failed to rename System.Private.CoreLib.dll\n");
457 return NI_ERROR_CORE_NI_FILE;
459 if (rename(niCoreLib.c_str(), coreLib.c_str())) {
460 fprintf(stderr, "Failed to rename System.Private.CoreLib.ni.dll\n");
461 return NI_ERROR_CORE_NI_FILE;
464 fprintf(stderr, "Failed to create native image for %s\n", coreLib.c_str());
465 return NI_ERROR_CORE_NI_FILE;
468 return NI_ERROR_NONE;
471 ni_error_e initNICommon()
473 #if defined(__arm__) || defined(__aarch64__)
474 // get interval value
475 const static std::string intervalFile = concatPath(__NATIVE_LIB_DIR, "crossgen_interval.txt");
476 std::ifstream inFile(intervalFile);
478 fprintf(stdout, "crossgen_interval.txt is found\n");
479 inFile >> __interval;
482 if (initializePluginManager("normal")) {
483 fprintf(stderr, "Fail to initialize PluginManager\n");
484 return NI_ERROR_UNKNOWN;
488 __pm = new PathManager();
489 } catch (const std::exception& e) {
490 fprintf(stderr, "Failed to create PathManager");
491 return NI_ERROR_UNKNOWN;
494 char* pluginDllPaths = pluginGetDllPath();
495 if (pluginDllPaths) {
496 __pm->addPlatformAssembliesPaths(pluginDllPaths);
499 return NI_ERROR_NONE;
501 fprintf(stderr, "crossgen supports arm/arm64 architecture only. skip ni file generation\n");
502 return NI_ERROR_NOT_SUPPORTED;
506 void finalizeNICommon()
510 finalizePluginManager();
516 ni_error_e createNIPlatform(DWORD flags)
518 if (createCoreLibNI(flags) != NI_ERROR_NONE) {
519 return NI_ERROR_CORE_NI_FILE;
522 return createNIUnderDirs(__pm->getRuntimePath() + ":" + __pm->getTizenFXPath(), flags);
525 ni_error_e createNIDll(const std::string& dllPath, DWORD flags)
527 if (dllPath.find("System.Private.CoreLib.dll") != std::string::npos) {
528 return createCoreLibNI(flags);
531 if (!isCoreLibPrepared(flags)) {
532 return NI_ERROR_CORE_NI_FILE;
535 return crossgen(dllPath, std::string(), flags);
538 ni_error_e createNIUnderDirs(const std::string& rootPaths, DWORD flags)
540 if (!isCoreLibPrepared(flags)) {
541 return NI_ERROR_CORE_NI_FILE;
544 auto convert = [&rootPaths, flags](const std::string& path, const std::string& filename) {
545 // if path is symlink, donot generate crossgen
546 if (!crossgen(path, rootPaths.c_str(), flags)) {
551 std::vector<std::string> targetPaths;
552 splitPath(rootPaths, targetPaths);
553 for (const auto &path : targetPaths) {
554 // TAC directory should be handled specially because that contains symlink of native image file.
555 if (strstr(path.c_str(), TAC_SYMLINK_SUB_DIR) != NULL) {
556 if (!isDirectory(path)) {
559 // make native image symlink if not exist under tac directory
561 for (auto& symlinkAssembly : bf::recursive_directory_iterator(path)) {
562 std::string symPath = symlinkAssembly.path().string();
563 if (!isManagedAssembly(symPath)) {
567 // if there is symlink and original file for native image, skip generation
568 std::string symNIPath = symPath.substr(0, symPath.rfind(".dll")) + ".ni.dll";
569 if (isFile(symNIPath)) {
573 // if original native image not exist, generate native image
574 std::string originPath = bf::read_symlink(symPath).string();
575 std::string originNIPath = originPath.substr(0, originPath.rfind(".dll")) + ".ni.dll";
576 if (!isFile(originNIPath)) {
577 if (!crossgen(originPath, path.c_str(), flags)) {
582 // if no symlink file exist, create symlink
583 if (!isFile(symNIPath)) {
584 bf::create_symlink(originNIPath, symNIPath);
585 copySmackAndOwnership(symPath.c_str(), symNIPath.c_str(), true);
586 fprintf(stdout, "%s symbolic link file generated successfully.\n", symNIPath.c_str());
587 _INFO("%s symbolic link file generated successfully.", symNIPath.c_str());
590 } catch (const bf::filesystem_error& error) {
591 fprintf(stderr, "Failed to recursive directory: %s\n", error.what());
592 return NI_ERROR_UNKNOWN;
595 scanFilesInDirectory(path, convert, 0);
599 return NI_ERROR_NONE;
602 ni_error_e createNIUnderPkgRoot(const std::string& pkgId, DWORD flags)
604 std::string rootPath = getRootPath(pkgId);
605 if (rootPath.empty()) {
606 fprintf(stderr, "Failed to get root path from [%s]\n", pkgId.c_str());
607 return NI_ERROR_INVALID_PACKAGE;
610 __pm->setAppRootPath(rootPath);
612 flags |= NI_FLAGS_APPNI;
614 // create native image under bin and lib directory
615 // tac directory is skipped in the createNIUnderDirs.
616 return createNIUnderDirs(__pm->getAppPaths(), flags);
619 void removeNIPlatform()
621 std::string coreLib = concatPath(__pm->getRuntimePath(), "System.Private.CoreLib.dll");
622 std::string coreLibBackup = concatPath(__pm->getRuntimePath(), "System.Private.CoreLib.dll.Backup");
624 if (hasCoreLibNI()) {
625 if (!isFile(coreLibBackup)) {
629 if (remove(coreLib.c_str())) {
630 fprintf(stderr, "Failed to remove System.Private.CoreLib native image file\n");
632 if (rename(coreLibBackup.c_str(), coreLib.c_str())) {
633 fprintf(stderr, "Failed to rename System.Private.CoreLib.Backup to origin\n");
637 #ifdef UNIQUE_DEFAULT_BASE_ADDR_SUPPORT
638 if (isFile(__SYSTEM_BASE_FILE)) {
639 if (remove(__SYSTEM_BASE_FILE)) {
640 fprintf(stderr, "Failed to remove %s\n", __SYSTEM_BASE_FILE);
645 removeNIUnderDirs(__pm->getRuntimePath() + ":" + __pm->getTizenFXPath());
648 void removeNIUnderDirs(const std::string& rootPaths)
650 auto convert = [](const std::string& path, const std::string& filename) {
651 if (isNativeImage(path)) {
652 if (remove(path.c_str())) {
653 fprintf(stderr, "Failed to remove %s\n", path.c_str());
658 std::vector<std::string> paths;
659 splitPath(rootPaths, paths);
660 for (const auto &path : paths) {
661 scanFilesInDirectory(path, convert, -1);
665 ni_error_e removeNIUnderPkgRoot(const std::string& pkgId)
667 std::string rootPath = getRootPath(pkgId);
668 if (rootPath.empty()) {
669 fprintf(stderr, "Failed to get root path from [%s]\n", pkgId.c_str());
670 return NI_ERROR_INVALID_PACKAGE;
673 __pm->setAppRootPath(rootPath);
675 // getAppNIPaths returns bin/.native_image, lib/.native_image and .tac_symlink.
676 std::string appNIPaths = __pm->getAppNIPaths();
677 std::vector<std::string> paths;
678 splitPath(appNIPaths, paths);
679 for (const auto &path : paths) {
680 // Only the native image inside the TAC should be removed.
681 if (strstr(path.c_str(), TAC_SYMLINK_SUB_DIR) != NULL) {
682 removeNIUnderDirs(path);
684 if (isDirectory(path)) {
685 if (!removeAll(path.c_str())) {
686 fprintf(stderr, "Failed to remove app ni dir [%s]\n", path.c_str());
692 return NI_ERROR_NONE;
695 ni_error_e regenerateAppNI(DWORD flags)
697 if (!isCoreLibPrepared(flags)) {
698 return NI_ERROR_CORE_NI_FILE;
702 pkgmgrinfo_appinfo_metadata_filter_h handle;
704 ret = pkgmgrinfo_appinfo_metadata_filter_create(&handle);
705 if (ret != PMINFO_R_OK)
706 return NI_ERROR_UNKNOWN;
708 ret = pkgmgrinfo_appinfo_metadata_filter_add(handle, AOT_METADATA_KEY, METADATA_VALUE);
709 if (ret != PMINFO_R_OK) {
710 pkgmgrinfo_appinfo_metadata_filter_destroy(handle);
711 return NI_ERROR_UNKNOWN;
714 ret = pkgmgrMDFilterForeach(handle, appAotCb, &flags);
716 pkgmgrinfo_appinfo_metadata_filter_destroy(handle);
717 return NI_ERROR_UNKNOWN;
720 pkgmgrinfo_appinfo_metadata_filter_destroy(handle);
721 return NI_ERROR_NONE;
724 // callback function of "pkgmgrinfo_appinfo_metadata_filter_foreach"
725 static int regenTacCb(pkgmgrinfo_appinfo_h handle, void *userData)
728 DWORD *pFlags = (DWORD*)userData;
730 int ret = pkgmgrinfo_appinfo_get_pkgid(handle, &pkgId);
731 if (ret != PMINFO_R_OK || pkgId == NULL) {
732 fprintf(stderr, "Failed to get pkgid\n");
736 sqlite3 *tac_db = openDB(TAC_APP_LIST_DB);
738 fprintf(stderr, "Sqlite open error\n");
741 sqlite3_exec(tac_db, "BEGIN;", NULL, NULL, NULL);
743 char *sql = sqlite3_mprintf("SELECT * FROM TAC WHERE PKGID = %Q;", pkgId);
744 std::vector<std::string> nugets = selectDB(tac_db, sql);
752 std::string nugetPaths;
753 for (const auto &nuget : nugets) {
754 if (!nugetPaths.empty()) {
757 nugetPaths += concatPath(__DOTNET_DIR, nuget);
760 auto convert = [&nugetPaths, pFlags](const std::string& path, const std::string& filename) {
761 if (strstr(path.c_str(), TAC_SHA_256_INFO) != NULL)
763 if (!crossgen(path, nugetPaths.c_str(), *pFlags)) {
768 for (auto& nuget : nugets) {
769 scanFilesInDirectory(concatPath(__DOTNET_DIR, nuget), convert, -1);
775 ni_error_e regenerateTACNI(DWORD flags)
777 if (!isCoreLibPrepared(flags)) {
778 return NI_ERROR_CORE_NI_FILE;
781 removeNIUnderDirs(__DOTNET_DIR);
783 pkgmgrinfo_appinfo_metadata_filter_h handle;
784 int ret = pkgmgrinfo_appinfo_metadata_filter_create(&handle);
785 if (ret != PMINFO_R_OK) {
786 return NI_ERROR_UNKNOWN;
789 ret = pkgmgrinfo_appinfo_metadata_filter_add(handle, TAC_METADATA_KEY, METADATA_VALUE);
790 if (ret != PMINFO_R_OK) {
791 pkgmgrinfo_appinfo_metadata_filter_destroy(handle);
792 return NI_ERROR_UNKNOWN;
795 ret = pkgmgrMDFilterForeach(handle, regenTacCb, &flags);
797 pkgmgrinfo_appinfo_metadata_filter_destroy(handle);
798 return NI_ERROR_UNKNOWN;
801 pkgmgrinfo_appinfo_metadata_filter_destroy(handle);
803 return NI_ERROR_NONE;