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>
20 #include <tzplatform_config.h>
24 #include "pkgmgr_parser_plugin_interface.h"
43 #include "ni_common.h"
44 #include "db_manager.h"
45 #include "tac_common.h"
46 #include "path_manager.h"
47 #include "plugin_manager.h"
52 #define LOG_TAG "DOTNET_INSTALLER_PLUGIN"
55 #error "CROSSGEN_PATH is missed"
59 #define __STR(x) __XSTR(x)
60 #if defined(__arm__) || defined(__aarch64__)
61 static const char* __NATIVE_LIB_DIR = __STR(NATIVE_LIB_DIR);
63 static const char* __CROSSGEN_PATH = __STR(CROSSGEN_PATH);
64 static const char* __DOTNET_DIR = __STR(DOTNET_DIR);
65 static const char* __READ_ONLY_APP_UPDATE_DIR = __STR(READ_ONLY_APP_UPDATE_DIR);
67 #ifdef UNIQUE_DEFAULT_BASE_ADDR_SUPPORT
68 static const char* __SYSTEM_BASE_FILE = __STR(SYSTEM_BASE_FILE);
74 static const char* CORERUN_CMD = "/usr/bin/dotnet-inhouse";
75 static const char* CROSSGEN2_PATH = "/usr/share/dotnet.tizen/netcoreapp/crossgen2/crossgen2.dll";
76 static const char* CLRJIT_PATH = "/usr/share/dotnet.tizen/netcoreapp/libclrjit.so";
77 static const char* CROSSGEN_OPT_JITPATH = "--jitpath";
78 static const char* CROSSGEN_OPT_TARGET_ARCH = "--targetarch";
79 static const char* CROSSGEN_OPT_OUT_NEAR_INPUT = "--out-near-input";
80 static const char* CROSSGEN_OPT_SINGLE_FILE_COMPILATION = "--single-file-compilation";
81 //static const char* CROSSGEN_OPT_PARALLELISM = "--parallelism";
82 //static const char* CROSSGEN_OPT_PARALLELISM_COUNT = "5";
83 static const char* CROSSGEN_OPT_RESILIENT = "--resilient";
84 static const char* CROSSGEN_OPT_OPTIMIZE = "-O";
85 static const char* CROSSGEN_OPT_INPUTBUBBLE = "--inputbubble";
86 static const char* CROSSGEN_OPT_COMPILE_BUBBLE_GENERICS = "--compilebubblegenerics";
87 static const char* CROSSGEN_OPT_VERBOSE = "--verbose";
88 static const char* CROSSGEN_OPT_TUNING = "--tuning";
89 static std::vector<std::string> REF_VECTOR;
91 static int __interval = 0;
92 static PathManager* __pm = nullptr;
94 static bool isCoreLibPrepared(DWORD flags)
96 if (flags & NI_FLAGS_ENABLER2R) {
100 std::string coreLibBackup = concatPath(__pm->getRuntimePath(), "System.Private.CoreLib.dll.Backup");
101 if (isFile(coreLibBackup)) {
104 _SERR("The native image of System.Private.CoreLib does not exist\n"
105 "Run the command to create the native image\n"
106 "# dotnettool --ni-dll /usr/share/dotnet.tizen/netcoreapp/System.Private.CoreLib.dll\n");
111 static bool hasCoreLibNI()
113 std::string ildasm = concatPath(__pm->getRuntimePath(), "ildasm");
114 std::string coreLib = concatPath(__pm->getRuntimePath(), "System.Private.CoreLib.dll");
115 std::string cmd = ildasm + " " + coreLib + " -noil -stats | grep '.rsrc'";
118 fp = popen(cmd.c_str(), "r");
121 if (fgets(buff, sizeof(buff), fp) != NULL) {
130 static void waitInterval()
132 // by the recommand, ignore small value for performance.
133 if (__interval > 10000) {
134 _SOUT("sleep %d usec", __interval);
139 static std::string getNIFilePath(const std::string& dllPath)
141 size_t index = dllPath.find_last_of(".");
142 if (index == std::string::npos) {
143 _SERR("File doesnot contain extension. fail to get NI file name");
146 std::string fName = dllPath.substr(0, index);
147 std::string fExt = dllPath.substr(index, dllPath.length());
149 // crossgen generate file with lower case extension only
150 std::transform(fExt.begin(), fExt.end(), fExt.begin(), ::tolower);
151 std::string niPath = fName + ".ni" + fExt;
157 * @brief create the directory including parents directory, and
158 * copy ownership and smack labels to the created directory.
159 * @param[in] target directory path
160 * @param[in] source directory path to get ownership and smack label
161 * @return if directory created successfully, return true otherwise false
163 static bool createDirsAndCopyOwnerShip(std::string& target_path, const std::string& source)
166 mode_t mode = S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH;
168 for (std::string::iterator iter = target_path.begin(); iter != target_path.end();) {
169 std::string::iterator newIter = std::find(iter, target_path.end(), '/');
170 std::string newPath = std::string(target_path.begin(), newIter);
172 if (!newPath.empty()) {
173 if (stat(newPath.c_str(), &st) != 0) {
174 if (mkdir(newPath.c_str(), mode) != 0 && errno != EEXIST) {
175 _SERR("Fail to create app ni directory (%s)", newPath.c_str());
178 if (!source.empty()) {
179 copySmackAndOwnership(source, newPath);
182 if (!S_ISDIR(st.st_mode)) {
183 _SERR("Fail. path is not a dir (%s)", newPath.c_str());
189 if(newIter != target_path.end()) {
197 static std::string getAppNIFilePath(const std::string& absDllPath, DWORD flags)
199 std::string niDirPath;
200 std::string prevPath;
202 prevPath = getBaseName(absDllPath);
203 niDirPath = concatPath(prevPath, APP_NI_SUB_DIR);
205 if (flags & NI_FLAGS_APP_UNDER_RO_AREA) {
206 niDirPath = replaceAll(niDirPath, getBaseName(__pm->getAppRootPath()), __READ_ONLY_APP_UPDATE_DIR);
207 _SERR("App is installed in RO area. Change NI path to RW area(%s).", niDirPath.c_str());
208 _ERR("App is installed in RO area. Change NI path to RW area(%s).", niDirPath.c_str());
211 if (!isDirectory(niDirPath)) {
212 if (!createDirsAndCopyOwnerShip(niDirPath, prevPath)) {
213 niDirPath = prevPath;
214 _SERR("fail to create dir (%s)", niDirPath.c_str());
218 return getNIFilePath(concatPath(niDirPath, getFileName(absDllPath)));
221 static bool checkNIExistence(const std::string& absDllPath)
223 std::string absNIPath = getNIFilePath(absDllPath);
224 if (absNIPath.empty()) {
228 if (isFile(absNIPath)) {
232 // native image of System.Private.CoreLib.dll should have to overwrite
233 // original file to support new coreclr
234 if (absDllPath.find("System.Private.CoreLib.dll") != std::string::npos) {
235 if (hasCoreLibNI()) {
243 static bool checkDllExistInDir(const std::string& path)
246 auto func = [&ret](const std::string& f_path, const std::string& f_name) {
247 if (isManagedAssembly(f_name) || isNativeImage(f_name)) {
252 scanFilesInDirectory(path, func, 0);
258 * Get the list of managed files in the specific directory
259 * Absolute paths of managed files are stored at the result list.
260 * If native image already exist in the same directory, managed file is ignored.
262 * Note: System.Private.CoreLib is excluded from the result list.
264 static ni_error_e getTargetDllList(const std::string& path, std::vector<std::string>& fileList)
266 if (!isDirectory(path)) {
267 return NI_ERROR_INVALID_PARAMETER;
270 auto func = [&fileList](const std::string& f_path, const std::string& f_name) {
271 if (isManagedAssembly(f_path) && !checkNIExistence(f_path)) {
272 if (f_path.find("System.Private.CoreLib") == std::string::npos) {
273 fileList.push_back(getAbsolutePath(f_path));
278 scanFilesInDirectory(path, func, 0);
280 return NI_ERROR_NONE;
283 #ifdef UNIQUE_DEFAULT_BASE_ADDR_SUPPORT
284 static uintptr_t getFileSize(const std::string& path)
288 if (stat(path.c_str(), &sb) == 0) {
295 // Get next base address to be used for system ni image from file
296 // __SYSTEM_BASE_FILE should be checked for existance before calling this function
297 static uintptr_t getNextBaseAddrFromFile()
299 FILE *pFile = fopen(__SYSTEM_BASE_FILE, "r");
301 _SERR("Failed to open %s", __SYSTEM_BASE_FILE);
308 while (fscanf(pFile, "%" SCNxPTR " %" SCNuPTR "", &addr, &size) != EOF) {
316 // Get next base address to be used for system ni image
317 static uintptr_t getNextBaseAddr()
319 uintptr_t baseAddr = 0;
321 if (!isFile(__SYSTEM_BASE_FILE)) {
322 // This is the starting address for all default base addresses
323 baseAddr = DEFAULT_BASE_ADDR_START;
325 baseAddr = getNextBaseAddrFromFile();
327 // Round to a multple of 64K (see ZapImage::CalculateZapBaseAddress in CoreCLR)
328 uintptr_t BASE_ADDRESS_ALIGNMENT = 0xffff;
329 baseAddr = (baseAddr + BASE_ADDRESS_ALIGNMENT) & ~BASE_ADDRESS_ALIGNMENT;
335 // Save base address of system ni image to file
336 static void updateBaseAddrFile(const std::string& absNIPath, uintptr_t baseAddr)
338 uintptr_t niSize = getFileSize(absNIPath);
340 _SERR("File %s doesn't exist", absNIPath.c_str());
344 // Write new entry to the file
345 FILE *pFile = fopen(__SYSTEM_BASE_FILE, "a");
347 _SERR("Failed to open %s", __SYSTEM_BASE_FILE);
351 fprintf(pFile, "%" PRIxPTR " %" PRIuPTR "\n", baseAddr, niSize);
355 // check if dll is listed in TPA
356 static bool isTPADll(const std::string& dllPath)
358 std::string absPath = getBaseName(getAbsolutePath(dllPath));
360 std::vector<std::string> paths = __pm->getPlatformAssembliesPaths();
361 for (unsigned int i = 0; i < paths.size(); i++) {
362 if (paths[i].find(getBaseName(absPath)) != std::string::npos) {
371 // baseAddr should be checked in file before getting here
372 static ni_error_e crossgen(const std::string& dllPath, const std::string& appPath, DWORD flags)
374 std::string absDllPath = getAbsolutePath(dllPath);
375 std::string absNIPath;
377 bool isAppNI = flags & NI_FLAGS_APPNI;
378 if (isAppNI && strstr(absDllPath.c_str(), __DOTNET_DIR) == NULL) {
379 absNIPath = getAppNIFilePath(absDllPath, flags);
380 if (exist(absNIPath)) {
381 return NI_ERROR_ALREADY_EXIST;
384 absNIPath = getNIFilePath(absDllPath);
385 if (checkNIExistence(absDllPath)) {
386 return NI_ERROR_ALREADY_EXIST;
390 if (absNIPath.empty()) {
391 _SERR("Fail to get ni file name");
392 return NI_ERROR_UNKNOWN;
395 #ifdef UNIQUE_DEFAULT_BASE_ADDR_SUPPORT
396 uintptr_t baseAddr = 0;
398 if (isTPADll(dllPath)) {
399 baseAddr = getNextBaseAddr();
405 return NI_ERROR_UNKNOWN;
409 waitpid(pid, &status, 0);
410 if (WIFEXITED(status)) {
411 // Do not use checkNIExistence() function to check whether ni file created or not.
412 // checkNIExistence() return false for System.Private.Corelib.dll
413 if (isFile(absNIPath)) {
414 copySmackAndOwnership(absDllPath, absNIPath);
415 std::string absPdbPath = replaceAll(absDllPath, ".dll", ".pdb");
416 std::string pdbFilePath = replaceAll(absNIPath, ".ni.dll", ".pdb");
417 if (isFile(absPdbPath) && (absPdbPath != pdbFilePath)) {
418 if (!copyFile(absPdbPath, pdbFilePath)) {
419 _SERR("Failed to copy a .pdb file");
421 copySmackAndOwnership(absPdbPath, pdbFilePath);
424 #ifdef UNIQUE_DEFAULT_BASE_ADDR_SUPPORT
426 updateBaseAddrFile(absNIPath, baseAddr);
429 return NI_ERROR_NONE;
431 _SERR("Fail to create native image for %s", dllPath.c_str());
432 return NI_ERROR_NO_SUCH_FILE;
436 std::string jitPath = __pm->getRuntimePath() + "/libclrjit.so";
437 std::vector<const char*> argv = {
440 "/JITPath", jitPath.c_str()
443 bool compat = flags & NI_FLAGS_COMPATIBILITY;
444 argv.push_back(compat ? "/Platform_Assemblies_Pathes" : "/p");
445 std::vector<std::string> paths = __pm->getPlatformAssembliesPaths();
446 std::string platformAssembliesPaths;
447 for (const auto &path : paths) {
448 if (!platformAssembliesPaths.empty()) {
449 platformAssembliesPaths += ":";
451 platformAssembliesPaths += path;
453 argv.push_back(platformAssembliesPaths.c_str());
455 bool enableR2R = flags & NI_FLAGS_ENABLER2R;
457 argv.push_back("/FragileNonVersionable");
460 if (flags & NI_FLAGS_VERBOSE) {
461 argv.push_back("/verbose");
464 if (flags & NI_FLAGS_INSTRUMENT) {
465 argv.push_back("/Tuning");
468 #ifdef UNIQUE_DEFAULT_BASE_ADDR_SUPPORT
469 std::string baseAddrString;
471 argv.push_back("/BaseAddress");
472 std::stringstream ss;
473 ss << "0x" << std::hex << baseAddr;
474 baseAddrString = ss.str();
475 argv.push_back(baseAddrString.c_str());
479 argv.push_back("/App_Paths");
480 std::string absAppPath;
481 if (!appPath.empty()) {
482 absAppPath = appPath;
484 absAppPath = getBaseName(absDllPath);
486 argv.push_back(absAppPath.c_str());
488 argv.push_back("/out");
489 argv.push_back(absNIPath.c_str());
491 argv.push_back(absDllPath.c_str());
492 argv.push_back(nullptr);
494 _SOUT("+ %s (%s)", absDllPath.c_str(), enableR2R ? "R2R" : "FNV");
496 execv(__CROSSGEN_PATH, const_cast<char* const*>(argv.data()));
500 return NI_ERROR_NONE;
503 static void makeArgs(std::vector<const char*>& args, const std::vector<std::string>& refPaths, DWORD flags)
505 args.push_back(CORERUN_CMD);
506 args.push_back(CROSSGEN2_PATH);
507 args.push_back(CROSSGEN_OPT_JITPATH);
508 args.push_back(CLRJIT_PATH);
509 args.push_back(CROSSGEN_OPT_TARGET_ARCH);
510 args.push_back(ARCHITECTURE_IDENTIFIER);
511 if (!(flags & NI_FLAGS_NO_PIPELINE)) {
512 args.push_back(CROSSGEN_OPT_OUT_NEAR_INPUT);
513 args.push_back(CROSSGEN_OPT_SINGLE_FILE_COMPILATION);
515 //args.push_back(OPT_PARALLELISM);
516 //args.push_back(OPT_PARALLELISM_COUNT);
517 args.push_back(CROSSGEN_OPT_RESILIENT);
519 args.push_back(CROSSGEN_OPT_OPTIMIZE);
521 if (flags & NI_FLAGS_INPUT_BUBBLE) {
522 args.push_back(CROSSGEN_OPT_INPUTBUBBLE);
523 args.push_back(CROSSGEN_OPT_COMPILE_BUBBLE_GENERICS);
526 if (flags & NI_FLAGS_VERBOSE) {
527 args.push_back(CROSSGEN_OPT_VERBOSE);
530 if (flags & NI_FLAGS_INSTRUMENT) {
531 args.push_back(CROSSGEN_OPT_TUNING);
536 // set reference path
537 std::vector<std::string> paths = __pm->getPlatformAssembliesPaths();
538 for (const auto &path : paths) {
539 REF_VECTOR.push_back("-r:" + path + "/*.dll");
542 for (const auto &path : refPaths) {
543 if (checkDllExistInDir(path)) {
544 REF_VECTOR.push_back("-r:" + path + "/*.dll");
548 for (const auto &path : REF_VECTOR) {
549 args.push_back(path.c_str());
553 static void clearArgs(std::vector<const char*>& args)
559 static ni_error_e makePdbSymlinkForNI(std::string dllPath, std::string niPath)
561 std::string pdbPath = changeExtension(dllPath, ".dll", ".pdb");
563 if (exist(pdbPath)) {
564 std::string targetPDBPath = changeExtension(niPath, ".ni.dll", ".pdb");
565 if (!exist(targetPDBPath)) {
566 bf::create_symlink(pdbPath, targetPDBPath);
567 copySmackAndOwnership(pdbPath, targetPDBPath, true);
570 } catch (const bf::filesystem_error& error) {
571 _SERR("Fail to create symlink for %s", pdbPath.c_str());
572 return NI_ERROR_UNKNOWN;
575 return NI_ERROR_NONE;
578 static ni_error_e crossgen2PipeLine(const std::vector<std::string>& dllList, const std::vector<std::string>& refPaths, DWORD flags)
583 return NI_ERROR_UNKNOWN;
587 waitpid(pid, &status, 0);
588 if (WIFEXITED(status)) {
589 for (auto& dllPath: dllList) {
590 std::string niPath = changeExtension(dllPath, ".dll", ".ni.dll");
592 if (!exist(niPath)) {
593 _SERR("Fail to create native image for %s", dllPath.c_str());
594 return NI_ERROR_NO_SUCH_FILE;
597 copySmackAndOwnership(dllPath, niPath);
598 // if AppNI then move ni.dll file to .native_image and copy pdb to .native_image
599 if (flags & NI_FLAGS_APPNI) {
600 std::string appNIPath = getAppNIFilePath(dllPath, flags);
601 moveFile(niPath, appNIPath);
602 makePdbSymlinkForNI(dllPath, appNIPath);
605 _SOUT("Native image %s generated successfully.", niPath.c_str());
609 std::vector<const char*> argv;
611 makeArgs(argv, refPaths, flags);
613 // add input files at the end of parameter
614 for (const auto &input : dllList) {
615 argv.push_back(input.c_str());
616 _SOUT("+ %s", input.c_str());
619 argv.push_back(nullptr);
622 //for (auto &arg: argv) _SOUT("+ %s", arg);
624 execv(CORERUN_CMD, const_cast<char* const*>(argv.data()));
630 return NI_ERROR_NONE;
633 static ni_error_e crossgen2NoPipeLine(const std::vector<std::string>& dllList, const std::vector<std::string>& refPaths, DWORD flags)
635 for (auto& dllPath : dllList) {
637 if (flags & NI_FLAGS_APPNI) {
638 niPath = getAppNIFilePath(dllPath, flags);
640 niPath = getNIFilePath(dllPath);
646 return NI_ERROR_UNKNOWN;
650 waitpid(pid, &status, 0);
651 if (WIFEXITED(status)) {
652 if (!exist(niPath)) {
653 _SERR("Fail to create native image for %s", dllPath.c_str());
654 return NI_ERROR_NO_SUCH_FILE;
657 copySmackAndOwnership(dllPath, niPath);
658 if (flags & NI_FLAGS_APPNI) {
659 makePdbSymlinkForNI(dllPath, niPath);
661 _SOUT("Native image %s generated successfully.", niPath.c_str());
664 std::vector<const char*> argv;
665 makeArgs(argv, refPaths, flags);
667 argv.push_back("-o");
668 argv.push_back(niPath.c_str());
670 argv.push_back(dllPath.c_str());
671 _SOUT("+ %s", dllPath.c_str());
674 argv.push_back(nullptr);
677 //for (auto &arg: argv) _SOUT("+ %s", arg);
679 execv(CORERUN_CMD, const_cast<char* const*>(argv.data()));
688 return NI_ERROR_NONE;
692 static ni_error_e doAOTList(std::vector<std::string>& dllList, const std::string& refPaths, DWORD flags)
694 if (dllList.empty()) {
695 return NI_ERROR_INVALID_PARAMETER;
697 // When performing AOT for one Dll, an error is returned when an error occurs.
698 // However, when processing multiple dlls at once, only the log for errors is output and skipped.
700 for (auto it = dllList.begin(); it != dllList.end(); it++) {
703 _SERR("dll file is not exist : %s", f.c_str());
706 if (!isManagedAssembly(f)) {
707 _SERR("Input file is not a dll file : %s", f.c_str());
712 if (!isFile(concatPath(__pm->getRuntimePath(), "crossgen"))) {
713 _SERR("crossgen1 doesnot supported. use crossgen2 forcibily");
714 flags |= NI_FLAGS_USE_CROSSGEN2;
717 if (flags & NI_FLAGS_USE_CROSSGEN2) { // crossgen2
718 std::vector<std::string> paths;
719 splitPath(refPaths, paths);
721 if (flags & NI_FLAGS_NO_PIPELINE) {
722 crossgen2NoPipeLine(dllList, paths, flags);
724 crossgen2PipeLine(dllList, paths, flags);
726 } else { // crossgen1
727 for (auto& dll : dllList) {
728 crossgen(dll, refPaths, flags);
733 return NI_ERROR_NONE;
736 static ni_error_e doAOTFile(const std::string& dllFile, const std::string& refPaths, DWORD flags)
738 if (!isFile(dllFile)) {
739 _SERR("dll file is not exist : %s", dllFile.c_str());
740 return NI_ERROR_NO_SUCH_FILE;
743 if (!isManagedAssembly(dllFile)) {
744 _SERR("Failed. Input parameter is not managed dll (%s)\n", dllFile.c_str());
745 return NI_ERROR_INVALID_PARAMETER;
748 std::vector<std::string> dllList;
749 dllList.push_back(getAbsolutePath(dllFile));
750 return doAOTList(dllList, refPaths, flags);
753 // callback function of "pkgmgrinfo_appinfo_metadata_filter_foreach"
754 static int appAotCb(pkgmgrinfo_appinfo_h handle, void *userData)
758 DWORD *pFlags = (DWORD*)userData;
760 ret = pkgmgrinfo_appinfo_get_pkgid(handle, &pkgId);
761 if (ret != PMINFO_R_OK) {
762 _SERR("Failed to get pkgid");
766 if (removeNIUnderPkgRoot(pkgId) != NI_ERROR_NONE) {
767 _SERR("Failed to remove previous dlls from [%s]", pkgId);
771 if (createNIUnderPkgRoot(pkgId, *pFlags) != NI_ERROR_NONE) {
772 _SERR("Failed to generate NI file [%s]", pkgId);
775 _SOUT("Complete make application to native image");
781 static ni_error_e createCoreLibNI(DWORD flags)
783 std::string coreLib = concatPath(__pm->getRuntimePath(), "System.Private.CoreLib.dll");
784 std::string niCoreLib = concatPath(__pm->getRuntimePath(), "System.Private.CoreLib.ni.dll");
785 std::string coreLibBackup = concatPath(__pm->getRuntimePath(), "System.Private.CoreLib.dll.Backup");
787 if (!isFile(coreLibBackup) && !hasCoreLibNI()) {
788 if (doAOTFile(coreLib, std::string(), flags) == NI_ERROR_NONE) {
789 if (rename(coreLib.c_str(), coreLibBackup.c_str())) {
790 _SERR("Failed to rename System.Private.CoreLib.dll");
791 return NI_ERROR_CORE_NI_FILE;
793 if (rename(niCoreLib.c_str(), coreLib.c_str())) {
794 _SERR("Failed to rename System.Private.CoreLib.ni.dll");
795 return NI_ERROR_CORE_NI_FILE;
798 _SERR("Failed to create native image for %s", coreLib.c_str());
799 return NI_ERROR_CORE_NI_FILE;
802 return NI_ERROR_NONE;
805 ni_error_e initNICommon()
807 #if defined(__arm__) || defined(__aarch64__)
808 // get interval value
809 const static std::string intervalFile = concatPath(__NATIVE_LIB_DIR, "crossgen_interval.txt");
810 std::ifstream inFile(intervalFile);
812 _SOUT("crossgen_interval.txt is found");
813 inFile >> __interval;
816 if (initializePluginManager("normal")) {
817 _SERR("Fail to initialize PluginManager");
818 return NI_ERROR_UNKNOWN;
822 __pm = new PathManager();
823 } catch (const std::exception& e) {
824 _SERR("Failed to create PathManager");
825 return NI_ERROR_UNKNOWN;
828 char* pluginDllPaths = pluginGetDllPath();
829 if (pluginDllPaths && pluginDllPaths[0] != '\0') {
830 __pm->addPlatformAssembliesPaths(pluginDllPaths, true);
833 char* pluginNativePaths = pluginGetNativeDllSearchingPath();
834 if (pluginNativePaths && pluginNativePaths[0] != '\0') {
835 __pm->addNativeDllSearchingPaths(pluginNativePaths, true);
838 return NI_ERROR_NONE;
840 _SERR("crossgen supports arm/arm64 architecture only. skip ni file generation");
841 return NI_ERROR_NOT_SUPPORTED;
845 void finalizeNICommon()
849 finalizePluginManager();
855 ni_error_e createNIPlatform(DWORD flags)
857 ni_error_e ret = createCoreLibNI(flags);
858 if (ret != NI_ERROR_NONE) {
862 return createNIUnderDirs(__pm->getRuntimePath() + ":" + __pm->getTizenFXPath(), flags);
865 ni_error_e createNIDll(const std::string& dllPath, DWORD flags)
867 if (dllPath.find("System.Private.CoreLib.dll") != std::string::npos) {
868 return createCoreLibNI(flags);
871 if (!isCoreLibPrepared(flags)) {
872 return NI_ERROR_CORE_NI_FILE;
875 return doAOTFile(dllPath, std::string(), flags);
878 ni_error_e createNIUnderTAC(const std::string& targetPath, const std::string& refPaths, DWORD flags)
882 // get managed file list from targetPath
883 std::vector<std::string> dllList;
884 ret = getTargetDllList(targetPath, dllList);
885 if (ret != NI_ERROR_NONE) {
889 std::vector<std::string> needNIList;
890 std::vector<std::string> niList;
892 for (auto &dll : dllList) {
893 if (!checkNIExistence(dll)) {
894 needNIList.push_back(dll);
896 niList.push_back(getNIFilePath(dll));
899 if (!needNIList.empty()) {
900 // NI fils of TAC-related dlls under /opt/usr/dotnet should not be created under .native_image directory.
901 // So, unset NI_FLAGS_APPNI temporally and restore it after running AOT.
902 flags &= ~NI_FLAGS_APPNI;
903 ret = doAOTList(needNIList, refPaths, flags);
904 flags |= NI_FLAGS_APPNI;
905 if (ret != NI_ERROR_NONE) {
910 for (auto &niPath : niList) {
912 std::string symNIPath = concatPath(targetPath, getFileName(niPath));
913 if (!exist(symNIPath)) {
914 bf::create_symlink(niPath, symNIPath);
915 copySmackAndOwnership(targetPath.c_str(), symNIPath.c_str(), true);
916 _SOUT("%s symbolic link file generated successfully.", symNIPath.c_str());
917 _INFO("%s symbolic link file generated successfully.", symNIPath.c_str());
922 return NI_ERROR_NONE;
926 ni_error_e createNIUnderDirs(const std::string& rootPaths, DWORD flags)
928 ni_error_e ret = NI_ERROR_NONE;
929 if (!isCoreLibPrepared(flags)) {
930 return NI_ERROR_CORE_NI_FILE;
933 std::vector<std::string> fileList;
934 std::vector<std::string> paths;
935 splitPath(rootPaths, paths);
937 for (const auto &path : paths) {
942 if (path.find(TAC_SYMLINK_SUB_DIR) != std::string::npos) {
943 ret = createNIUnderTAC(path, rootPaths, flags);
944 if (ret != NI_ERROR_NONE) {
948 ret = getTargetDllList(path, fileList);
949 if (ret != NI_ERROR_NONE) {
955 if (fileList.empty()) {
956 return NI_ERROR_NONE;
959 return doAOTList(fileList, rootPaths, flags);
962 ni_error_e createNIUnderPkgRoot(const std::string& pkgId, DWORD flags)
964 std::string rootPath = getRootPath(pkgId);
965 if (rootPath.empty()) {
966 _SERR("Failed to get root path from [%s]", pkgId.c_str());
967 return NI_ERROR_INVALID_PACKAGE;
970 __pm->setAppRootPath(rootPath);
972 flags |= NI_FLAGS_APPNI;
974 if (isReadOnlyArea(rootPath)) {
975 flags |= NI_FLAGS_APP_UNDER_RO_AREA;
976 flags |= NI_FLAGS_NO_PIPELINE;
977 _SERR("Only no-pipeline mode supported for RO app. Set no-pipeline option forcibly");
979 flags &= ~NI_FLAGS_APP_UNDER_RO_AREA;
982 // create native image under bin and lib directory
983 // tac directory is skipped in the createNIUnderDirs.
984 return createNIUnderDirs(__pm->getAppPaths(), flags);
987 void removeNIPlatform()
989 std::string coreLib = concatPath(__pm->getRuntimePath(), "System.Private.CoreLib.dll");
990 std::string coreLibBackup = concatPath(__pm->getRuntimePath(), "System.Private.CoreLib.dll.Backup");
992 if (hasCoreLibNI()) {
993 if (!isFile(coreLibBackup)) {
997 if (remove(coreLib.c_str())) {
998 _SERR("Failed to remove System.Private.CoreLib native image file");
1000 if (rename(coreLibBackup.c_str(), coreLib.c_str())) {
1001 _SERR("Failed to rename System.Private.CoreLib.Backup to origin");
1005 #ifdef UNIQUE_DEFAULT_BASE_ADDR_SUPPORT
1006 if (isFile(__SYSTEM_BASE_FILE)) {
1007 if (remove(__SYSTEM_BASE_FILE)) {
1008 _SERR("Failed to remove %s", __SYSTEM_BASE_FILE);
1013 removeNIUnderDirs(__pm->getRuntimePath() + ":" + __pm->getTizenFXPath());
1016 void removeNIUnderDirs(const std::string& rootPaths)
1018 auto convert = [](const std::string& path, const std::string& filename) {
1019 if (isNativeImage(path)) {
1020 if (remove(path.c_str())) {
1021 _SERR("Failed to remove %s", path.c_str());
1026 std::vector<std::string> paths;
1027 splitPath(rootPaths, paths);
1028 for (const auto &path : paths) {
1029 scanFilesInDirectory(path, convert, -1);
1033 ni_error_e removeNIUnderPkgRoot(const std::string& pkgId)
1035 std::string rootPath = getRootPath(pkgId);
1036 if (rootPath.empty()) {
1037 _SERR("Failed to get root path from [%s]", pkgId.c_str());
1038 return NI_ERROR_INVALID_PACKAGE;
1041 __pm->setAppRootPath(rootPath);
1043 // getAppNIPaths returns bin/.native_image, lib/.native_image and .tac_symlink.
1044 std::string appNIPaths = __pm->getAppNIPaths();
1045 std::vector<std::string> paths;
1046 splitPath(appNIPaths, paths);
1047 for (const auto &path : paths) {
1048 if (!isReadOnlyArea(path)) {
1049 // Only the native image inside the TAC should be removed.
1050 if (strstr(path.c_str(), TAC_SYMLINK_SUB_DIR) != NULL) {
1051 removeNIUnderDirs(path);
1053 if (isDirectory(path)) {
1054 if (!removeAll(path.c_str())) {
1055 _SERR("Failed to remove app ni dir [%s]", path.c_str());
1062 // In special cases, the ni file may exist in the dll location.
1063 // The code below is to avoid this exceptional case.
1064 std::string appPaths = __pm->getAppPaths();
1065 splitPath(appPaths, paths);
1066 for (const auto &path : paths) {
1067 if (isDirectory(path)) {
1068 removeNIUnderDirs(path);
1072 return NI_ERROR_NONE;
1075 ni_error_e regenerateAppNI(DWORD flags)
1077 if (!isCoreLibPrepared(flags)) {
1078 return NI_ERROR_CORE_NI_FILE;
1082 pkgmgrinfo_appinfo_metadata_filter_h handle;
1084 ret = pkgmgrinfo_appinfo_metadata_filter_create(&handle);
1085 if (ret != PMINFO_R_OK)
1086 return NI_ERROR_UNKNOWN;
1088 ret = pkgmgrinfo_appinfo_metadata_filter_add(handle, AOT_METADATA_KEY, METADATA_VALUE);
1089 if (ret != PMINFO_R_OK) {
1090 pkgmgrinfo_appinfo_metadata_filter_destroy(handle);
1091 return NI_ERROR_UNKNOWN;
1094 ret = pkgmgrMDFilterForeach(handle, appAotCb, &flags);
1096 pkgmgrinfo_appinfo_metadata_filter_destroy(handle);
1097 return NI_ERROR_UNKNOWN;
1100 pkgmgrinfo_appinfo_metadata_filter_destroy(handle);
1101 return NI_ERROR_NONE;
1104 // callback function of "pkgmgrinfo_appinfo_metadata_filter_foreach"
1105 static int regenTacCb(pkgmgrinfo_appinfo_h handle, void *userData)
1108 DWORD *pFlags = (DWORD*)userData;
1110 int ret = pkgmgrinfo_appinfo_get_pkgid(handle, &pkgId);
1111 if (ret != PMINFO_R_OK || pkgId == NULL) {
1112 _SERR("Failed to get pkgid");
1116 sqlite3 *tac_db = openDB(TAC_APP_LIST_DB);
1118 _SERR("Sqlite open error");
1121 sqlite3_exec(tac_db, "BEGIN;", NULL, NULL, NULL);
1123 char *sql = sqlite3_mprintf("SELECT * FROM TAC WHERE PKGID = %Q;", pkgId);
1124 std::vector<std::string> nugets = selectDB(tac_db, sql);
1132 std::string nugetPaths;
1133 for (const auto &nuget : nugets) {
1134 if (!nugetPaths.empty()) {
1137 nugetPaths += concatPath(__DOTNET_DIR, nuget);
1140 for (auto& nuget : nugets) {
1141 createNIUnderTAC(concatPath(__DOTNET_DIR, nuget), nugetPaths, *pFlags);
1147 ni_error_e regenerateTACNI(DWORD flags)
1149 if (!isCoreLibPrepared(flags)) {
1150 return NI_ERROR_CORE_NI_FILE;
1153 removeNIUnderDirs(__DOTNET_DIR);
1155 pkgmgrinfo_appinfo_metadata_filter_h handle;
1156 int ret = pkgmgrinfo_appinfo_metadata_filter_create(&handle);
1157 if (ret != PMINFO_R_OK) {
1158 return NI_ERROR_UNKNOWN;
1161 ret = pkgmgrinfo_appinfo_metadata_filter_add(handle, TAC_METADATA_KEY, METADATA_VALUE);
1162 if (ret != PMINFO_R_OK) {
1163 pkgmgrinfo_appinfo_metadata_filter_destroy(handle);
1164 return NI_ERROR_UNKNOWN;
1167 ret = pkgmgrMDFilterForeach(handle, regenTacCb, &flags);
1169 pkgmgrinfo_appinfo_metadata_filter_destroy(handle);
1170 return NI_ERROR_UNKNOWN;
1173 pkgmgrinfo_appinfo_metadata_filter_destroy(handle);
1175 return NI_ERROR_NONE;
1178 static std::vector<uid_t> getUserIds()
1180 std::vector<uid_t> list;
1183 errno = 0; // so we can distinguish errors from no more entries
1184 passwd* entry = getpwent();
1187 _SERR("Error while getting userIDs");
1193 list.push_back(entry->pw_uid);
1200 static std::string getAppDataPath(const std::string& pkgId, uid_t uid)
1202 std::string pDataFile;
1204 tzplatform_set_user(uid);
1206 const char* tzUserApp = tzplatform_getenv(TZ_USER_APP);
1207 if (tzUserApp != NULL) {
1208 pDataFile = std::string(tzUserApp) + "/" + pkgId + "/data/";
1211 tzplatform_reset_user();
1216 ni_error_e removeAppProfileData(const std::string& pkgId)
1218 if (pkgId.empty()) {
1219 return NI_ERROR_INVALID_PARAMETER;
1222 std::vector<uid_t> uidList = getUserIds();
1223 for (auto& uid : uidList) {
1224 // get data path from pkgid
1225 std::string dataPath = getAppDataPath(pkgId, uid);
1226 if (!dataPath.empty() && exist(dataPath)) {
1227 std::string pDataFile = dataPath + PROFILE_BASENAME;
1229 if (exist(pDataFile)) {
1230 if (!removeFile(pDataFile)) {
1231 _SERR("Fail to remove profile data file (%s).", pDataFile.c_str());
1232 return NI_ERROR_UNKNOWN;
1234 _SOUT("Profile data (%s) is removed successfully", pDataFile.c_str());
1239 return NI_ERROR_NONE;
1242 static int appTypeListCb(pkgmgrinfo_appinfo_h handle, void *user_data)
1245 int ret = pkgmgrinfo_appinfo_get_pkgid(handle, &pkgId);
1246 if (ret != PMINFO_R_OK || pkgId == NULL) {
1247 _SERR("Fail to get pkgid");
1251 if (removeAppProfileData(pkgId) != NI_ERROR_NONE) {
1252 _SERR("Fail to remove profile data for (%s)", pkgId);
1258 static ni_error_e removeAppProfileByAppType(const char* type)
1262 pkgmgrinfo_appinfo_filter_h filter;
1264 ret = pkgmgrinfo_appinfo_filter_create(&filter);
1265 if (ret != PMINFO_R_OK) {
1266 _SERR("Fail to create appinfo filter");
1267 return NI_ERROR_UNKNOWN;
1270 ret = pkgmgrinfo_appinfo_filter_add_string(filter, PMINFO_APPINFO_PROP_APP_TYPE, type);
1271 if (ret != PMINFO_R_OK) {
1272 pkgmgrinfo_appinfo_filter_destroy(filter);
1273 _SERR("Fail to add appinfo filter (%s)", type);
1274 return NI_ERROR_UNKNOWN;
1277 ret = pkgmgrinfo_appinfo_filter_foreach_appinfo(filter, appTypeListCb, NULL);
1278 if (ret != PMINFO_R_OK) {
1279 _SERR("Fail to pkgmgrinfo_pkginfo_filter_foreach_pkginfo");
1280 pkgmgrinfo_appinfo_filter_destroy(filter);
1281 return NI_ERROR_UNKNOWN;
1284 pkgmgrinfo_appinfo_filter_destroy(filter);
1286 return NI_ERROR_NONE;
1289 void removeAllAppProfileData()
1291 std::vector<const char*> appTypeList = {"dotnet", "dotnet-nui", "dotnet-inhouse"};
1293 for (auto& type : appTypeList) {
1294 if (removeAppProfileByAppType(type) != NI_ERROR_NONE) {
1295 _SERR("Fail to removeAppProfileByAppType for type (%s)", type);