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"
46 #include "r2r_checker.h"
51 #define LOG_TAG "DOTNET_INSTALLER_PLUGIN"
54 #define __STR(x) __XSTR(x)
55 #if defined(__arm__) || defined(__aarch64__)
56 static const char* __NATIVE_LIB_DIR = __STR(NATIVE_LIB_DIR);
58 static const char* __DOTNET_DIR = __STR(DOTNET_DIR);
59 static const char* __READ_ONLY_APP_UPDATE_DIR = __STR(READ_ONLY_APP_UPDATE_DIR);
61 #ifdef UNIQUE_DEFAULT_BASE_ADDR_SUPPORT
62 static const char* __SYSTEM_BASE_FILE = __STR(SYSTEM_BASE_FILE);
68 static std::string CORERUN_CMD = "/usr/share/dotnet.tizen/netcoreapp/corerun";
69 static std::string CROSSGEN2_PATH = "/usr/share/dotnet.tizen/netcoreapp/crossgen2/crossgen2.dll";
70 static std::string CLRJIT_PATH = "/usr/share/dotnet.tizen/netcoreapp/libclrjit.so";
71 static const char* CROSSGEN_OPT_JITPATH = "--jitpath";
72 static const char* CROSSGEN_OPT_TARGET_ARCH = "--targetarch";
73 static const char* CROSSGEN_OPT_OUT_NEAR_INPUT = "--out-near-input";
74 static const char* CROSSGEN_OPT_SINGLE_FILE_COMPILATION = "--single-file-compilation";
75 //static const char* CROSSGEN_OPT_PARALLELISM = "--parallelism";
76 //static const char* CROSSGEN_OPT_PARALLELISM_COUNT = "5";
77 static const char* CROSSGEN_OPT_RESILIENT = "--resilient";
78 //static const char* CROSSGEN_OPT_OPTIMIZE = "-O";
79 static const char* CROSSGEN_OPT_OPTIMIZE_TIME = "--Ot";
80 static const char* CROSSGEN_OPT_INPUTBUBBLE = "--inputbubble";
81 static const char* CROSSGEN_OPT_COMPILE_BUBBLE_GENERICS = "--compilebubblegenerics";
82 static const char* CROSSGEN_OPT_VERBOSE = "--verbose";
83 static std::vector<std::string> REF_VECTOR;
84 static std::vector<std::string> INPUTBUBBLE_REF_VECTOR;
85 static std::vector<std::string> MIBC_VECTOR;
87 static int __interval = 0;
88 static PathManager* __pm = nullptr;
90 static NIOption* __ni_option = nullptr;
93 NIOption* getNIOption()
95 if (__ni_option == nullptr) {
96 __ni_option = (NIOption*)calloc(sizeof(NIOption), 1);
97 if (__ni_option == nullptr) {
98 _SERR("Fail to create NIOption");
104 static void waitInterval()
106 // by the recommand, ignore small value for performance.
107 if (__interval > 10000) {
108 _SOUT("sleep %d usec", __interval);
113 #ifdef UNIQUE_DEFAULT_BASE_ADDR_SUPPORT
114 // Get next base address to be used for system ni image from file
115 // __SYSTEM_BASE_FILE should be checked for existance before calling this function
116 static uintptr_t getNextBaseAddrFromFile()
118 FILE *pFile = fopen(__SYSTEM_BASE_FILE, "r");
120 _SERR("Failed to open %s", __SYSTEM_BASE_FILE);
127 while (fscanf(pFile, "%" SCNxPTR " %" SCNuPTR "", &addr, &size) != EOF) {
135 // Get next base address to be used for system ni image
136 static uintptr_t getNextBaseAddr()
138 uintptr_t baseAddr = 0;
140 if (!isFile(__SYSTEM_BASE_FILE)) {
141 // This is the starting address for all default base addresses
142 baseAddr = DEFAULT_BASE_ADDR_START;
144 baseAddr = getNextBaseAddrFromFile();
146 // Round to a multple of 64K (see ZapImage::CalculateZapBaseAddress in CoreCLR)
147 uintptr_t BASE_ADDRESS_ALIGNMENT = 0xffff;
148 baseAddr = (baseAddr + BASE_ADDRESS_ALIGNMENT) & ~BASE_ADDRESS_ALIGNMENT;
154 // Save base address of system ni image to file
155 static void updateBaseAddrFile(const std::string& absNIPath, uintptr_t baseAddr)
157 uintptr_t niSize = getSizeOfImage(absNIPath);
159 _SERR("File %s doesn't exist", absNIPath.c_str());
163 // Write new entry to the file
164 FILE *pFile = fopen(__SYSTEM_BASE_FILE, "a");
166 _SERR("Failed to open %s", __SYSTEM_BASE_FILE);
170 fprintf(pFile, "%" PRIxPTR " %" PRIuPTR "\n", baseAddr, niSize);
174 // check if dll is listed in TPA
175 static bool isTPADll(const std::string& dllPath)
177 std::string absPath = getBaseName(getAbsolutePath(dllPath));
179 std::vector<std::string> paths = __pm->getPlatformAssembliesPaths();
180 for (unsigned int i = 0; i < paths.size(); i++) {
181 if (paths[i].find(getBaseName(absPath)) != std::string::npos) {
191 * @brief create the directory including parents directory, and
192 * copy ownership and smack labels to the created directory.
193 * @param[in] target directory path
194 * @param[in] source directory path to get ownership and smack label
195 * @return if directory created successfully, return true otherwise false
197 static bool createDirsAndCopyOwnerShip(std::string& target_path, const std::string& source)
200 mode_t mode = S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH;
202 for (std::string::iterator iter = target_path.begin(); iter != target_path.end();) {
203 std::string::iterator newIter = std::find(iter, target_path.end(), '/');
204 std::string newPath = std::string(target_path.begin(), newIter);
206 if (!newPath.empty()) {
207 if (stat(newPath.c_str(), &st) != 0) {
208 if (mkdir(newPath.c_str(), mode) != 0 && errno != EEXIST) {
209 _SERR("Fail to create app ni directory (%s)", newPath.c_str());
212 if (!source.empty()) {
213 copySmackAndOwnership(source, newPath);
216 if (!S_ISDIR(st.st_mode)) {
217 _SERR("Fail. path is not a dir (%s)", newPath.c_str());
223 if(newIter != target_path.end()) {
231 static std::string getNIFilePath(const std::string& dllPath)
233 size_t index = dllPath.find_last_of(".");
234 if (index == std::string::npos) {
235 _SERR("File doesnot contain extension. fail to get NI file name");
238 std::string fName = dllPath.substr(0, index);
239 std::string fExt = dllPath.substr(index, dllPath.length());
241 // crossgen generate file with lower case extension only
242 std::transform(fExt.begin(), fExt.end(), fExt.begin(), ::tolower);
243 std::string niPath = fName + ".ni" + fExt;
248 static std::string getAppNIFilePath(const std::string& absDllPath, NIOption* opt)
250 std::string niDirPath;
251 std::string prevPath;
253 prevPath = getBaseName(absDllPath);
254 niDirPath = concatPath(prevPath, APP_NI_SUB_DIR);
256 if (opt->flags & NI_FLAGS_APP_UNDER_RO_AREA) {
257 niDirPath = replaceAll(niDirPath, getBaseName(__pm->getAppRootPath()), __READ_ONLY_APP_UPDATE_DIR);
258 _SERR("App is installed in RO area. Change NI path to RW area(%s).", niDirPath.c_str());
259 _ERR("App is installed in RO area. Change NI path to RW area(%s).", niDirPath.c_str());
262 if (!isDirectory(niDirPath)) {
263 if (!createDirsAndCopyOwnerShip(niDirPath, prevPath)) {
264 niDirPath = prevPath;
265 _SERR("fail to create dir (%s)", niDirPath.c_str());
269 return getNIFilePath(concatPath(niDirPath, getFileName(absDllPath)));
272 static bool checkNIExistence(const std::string& absDllPath)
274 std::string absNIPath = getNIFilePath(absDllPath);
275 if (absNIPath.empty()) {
279 if (isFile(absNIPath)) {
283 // native image of System.Private.CoreLib.dll should have to overwrite
284 // original file to support new coreclr
285 if (absDllPath.find("System.Private.CoreLib.dll") != std::string::npos) {
286 return isR2RImage(absDllPath);
292 static bool checkAppNIExistence(const std::string& absDllPath, NIOption* opt)
294 std::string absNIPath = getAppNIFilePath(absDllPath, opt);
295 if (absNIPath.empty()) {
299 if (isFile(absNIPath)) {
306 static bool checkDllExistInDir(const std::string& path)
309 auto func = [&ret](const std::string& f_path, const std::string& f_name) {
310 if (isManagedAssembly(f_name) || isNativeImage(f_name)) {
315 scanFilesInDirectory(path, func, 0);
321 * Get the list of managed files in the specific directory
322 * Absolute paths of managed files are stored at the result list.
323 * If native image already exist in the same directory, managed file is ignored.
325 static ni_error_e getTargetDllList(const std::string& path, std::vector<std::string>& fileList)
327 if (!isDirectory(path)) {
328 return NI_ERROR_INVALID_PARAMETER;
331 auto func = [&fileList](const std::string& f_path, const std::string& f_name) {
332 if (isManagedAssembly(f_path) && !checkNIExistence(f_path)) {
333 fileList.push_back(getAbsolutePath(f_path));
337 scanFilesInDirectory(path, func, 0);
339 return NI_ERROR_NONE;
343 * Get the list of managed files in the specific directory of Application
344 * Absolute paths of managed files are stored at the result list.
345 * If native image already exist in the .native_image directory, managed file is ignored.
348 static ni_error_e getAppTargetDllList(const std::string& path, std::vector<std::string>& fileList, NIOption *opt)
350 if (!isDirectory(path)) {
351 return NI_ERROR_INVALID_PARAMETER;
354 auto func = [&fileList, opt](const std::string& f_path, const std::string& f_name) {
355 if (isManagedAssembly(f_path) && !checkAppNIExistence(f_path, opt)) {
356 fileList.push_back(getAbsolutePath(f_path));
360 scanFilesInDirectory(path, func, 0);
362 return NI_ERROR_NONE;
365 static void makeArgs(std::vector<const char*>& args, const std::vector<std::string>& refPaths, NIOption* opt)
367 args.push_back(CORERUN_CMD.c_str());
368 if (CROSSGEN2_PATH != "") {
369 args.push_back(CROSSGEN2_PATH.c_str());
371 args.push_back(CROSSGEN_OPT_JITPATH);
372 args.push_back(CLRJIT_PATH.c_str());
373 args.push_back(CROSSGEN_OPT_TARGET_ARCH);
374 args.push_back(ARCHITECTURE_IDENTIFIER);
376 //args.push_back(OPT_PARALLELISM);
377 //args.push_back(OPT_PARALLELISM_COUNT);
378 args.push_back(CROSSGEN_OPT_RESILIENT);
380 args.push_back(CROSSGEN_OPT_OPTIMIZE_TIME);
382 if (opt->flags & NI_FLAGS_INPUT_BUBBLE) {
383 args.push_back(CROSSGEN_OPT_INPUTBUBBLE);
384 args.push_back(CROSSGEN_OPT_COMPILE_BUBBLE_GENERICS);
386 INPUTBUBBLE_REF_VECTOR.clear();
387 for (const auto &path : opt->inputBubbleRefFiles) {
388 INPUTBUBBLE_REF_VECTOR.push_back("--inputbubbleref:" + path);
390 for (const auto &path : INPUTBUBBLE_REF_VECTOR) {
391 if (find(args.begin(), args.end(), path) == args.end()) {
392 args.push_back(path.c_str());
397 if (opt->flags & NI_FLAGS_MIBC) {
399 for (const auto &path : opt->mibcPath) {
400 MIBC_VECTOR.push_back("--mibc:" + path);
402 for (const auto &path : MIBC_VECTOR) {
403 if (find(args.begin(), args.end(), path) == args.end()) {
404 args.push_back(path.c_str());
409 if (opt->flags & NI_FLAGS_VERBOSE) {
410 args.push_back(CROSSGEN_OPT_VERBOSE);
415 // set reference path
416 for (const auto &path : opt->refFiles) {
417 REF_VECTOR.push_back("-r:" + path);
420 std::vector<std::string> paths = __pm->getPlatformAssembliesPaths();
421 for (const auto &path : paths) {
422 if (checkDllExistInDir(path)) {
423 REF_VECTOR.push_back("-r:" + path + "/*.dll");
427 if (opt->flags & NI_FLAGS_EXTRA_REF) {
428 for (const auto &erPath : opt->extraRefPath) {
429 std::string path = getAbsolutePath(erPath);
430 if (checkDllExistInDir(path)) {
431 REF_VECTOR.push_back("-r:" + path + "/*.dll");
436 for (const auto &path : refPaths) {
437 if (checkDllExistInDir(path)) {
438 REF_VECTOR.push_back("-r:" + path + "/*.dll");
442 for (const auto &path : REF_VECTOR) {
443 if (find(args.begin(), args.end(), path) == args.end()) {
444 args.push_back(path.c_str());
449 static void clearArgs(std::vector<const char*>& args)
455 static ni_error_e makePdbSymlinkForNI(std::string dllPath, std::string niPath)
457 std::string pdbPath = changeExtension(dllPath, ".dll", ".pdb");
459 if (exist(pdbPath)) {
460 std::string targetPDBPath = changeExtension(niPath, ".ni.dll", ".pdb");
461 if (!exist(targetPDBPath)) {
462 bf::create_symlink(pdbPath, targetPDBPath);
463 copySmackAndOwnership(pdbPath, targetPDBPath, true);
466 } catch (const bf::filesystem_error& error) {
467 _SERR("Fail to create symlink for %s", pdbPath.c_str());
468 return NI_ERROR_UNKNOWN;
471 return NI_ERROR_NONE;
474 static ni_error_e crossgen2PostAction(const std::string& dllPath, const std::string& niPath, NIOption* opt) {
475 std::string outFile = niPath;
476 if (!exist(outFile)) {
477 return NI_ERROR_NO_SUCH_FILE;
479 copySmackAndOwnership(dllPath, outFile);
481 // if AppNI then move ni.dll file to .native_image and copy pdb to .native_image
482 if (opt->flags & NI_FLAGS_APPNI) {
483 outFile = getAppNIFilePath(dllPath, opt);
484 makePdbSymlinkForNI(dllPath, outFile);
486 if (opt->flags & NI_FLAGS_INPUT_BUBBLE && opt->flags & NI_FLAGS_NO_PIPELINE) {
487 outFile = outFile + ".tmp";
490 if (niPath != outFile) {
491 moveFile(niPath, outFile);
495 if (!(opt->flags & NI_FLAGS_INPUT_BUBBLE && opt->flags & NI_FLAGS_NO_PIPELINE)) {
496 _SOUT("Native image %s generated successfully.", outFile.c_str());
499 return NI_ERROR_NONE;
502 static ni_error_e crossgen2PipeLine(const std::vector<std::string>& dllList, const std::vector<std::string>& refPaths, NIOption* opt)
507 return NI_ERROR_UNKNOWN;
511 waitpid(pid, &status, 0);
512 if (WIFEXITED(status)) {
513 for (auto& dllPath: dllList) {
514 ni_error_e ret = crossgen2PostAction(dllPath, changeExtension(dllPath, ".dll", ".ni.dll"), opt);
515 if (ret != NI_ERROR_NONE) {
520 _SERR("Failed. Forked process terminated abnormally");
521 return NI_ERROR_ABNORMAL_PROCESS_TERMINATION;
524 std::vector<const char*> argv;
525 makeArgs(argv, refPaths, opt);
526 argv.push_back(CROSSGEN_OPT_OUT_NEAR_INPUT);
527 argv.push_back(CROSSGEN_OPT_SINGLE_FILE_COMPILATION);
529 // add input files at the end of parameter
530 for (const auto &input : dllList) {
531 argv.push_back(input.c_str());
532 _SOUT("+ %s", input.c_str());
536 argv.push_back(nullptr);
539 if (opt->flags & NI_FLAGS_PRINT_CMD) {
540 _SOUT("==================== NI Commands =========================");
541 for (auto &arg: argv) _SOUT("+ %s", arg);
544 execv(CORERUN_CMD.c_str(), const_cast<char* const*>(argv.data()));
550 return NI_ERROR_NONE;
553 static ni_error_e crossgen2NoPipeLine(const std::vector<std::string>& dllList, const std::vector<std::string>& refPaths, NIOption* opt)
555 for (auto& dllPath : dllList) {
557 if (opt->flags & NI_FLAGS_APPNI) {
558 niPath = getAppNIFilePath(dllPath, opt);
560 niPath = getNIFilePath(dllPath);
562 #ifdef UNIQUE_DEFAULT_BASE_ADDR_SUPPORT
563 uintptr_t baseAddr = 0;
564 if (isTPADll(dllPath)) {
565 baseAddr = getNextBaseAddr();
568 if (opt->flags & NI_FLAGS_INPUT_BUBBLE) {
575 return NI_ERROR_UNKNOWN;
579 waitpid(pid, &status, 0);
580 if (WIFEXITED(status)) {
581 ni_error_e ret = crossgen2PostAction(dllPath, niPath, opt);
582 if (ret != NI_ERROR_NONE) {
585 #ifdef UNIQUE_DEFAULT_BASE_ADDR_SUPPORT
587 updateBaseAddrFile(niPath, baseAddr);
591 _SERR("Failed. Forked process terminated abnormally");
592 _SERR("Crossgen2 was terminated by the OOM killer. Please check the system.");
593 removeFile(changeExtension(niPath, ".ni.dll", ".ni.dll.tmp"));
594 return NI_ERROR_ABNORMAL_PROCESS_TERMINATION;
597 std::vector<const char*> argv;
598 makeArgs(argv, refPaths, opt);
600 #ifdef UNIQUE_DEFAULT_BASE_ADDR_SUPPORT
601 std::string baseAddrString;
603 argv.push_back("--imagebase");
604 std::stringstream ss;
605 ss << "0x" << std::hex << baseAddr;
606 baseAddrString = ss.str();
607 argv.push_back(baseAddrString.c_str());
610 argv.push_back("-o");
611 argv.push_back(niPath.c_str());
613 argv.push_back(dllPath.c_str());
614 _SOUT("+ %s", dllPath.c_str());
617 argv.push_back(nullptr);
620 if (opt->flags & NI_FLAGS_PRINT_CMD) {
621 _SOUT("==================== NI Commands =========================");
622 for (auto &arg: argv) _SOUT("+ %s", arg);
625 execv(CORERUN_CMD.c_str(), const_cast<char* const*>(argv.data()));
634 return NI_ERROR_NONE;
637 static ni_error_e createCoreLibNI(NIOption* opt)
639 std::string coreLib = concatPath(__pm->getRuntimePath(), "System.Private.CoreLib.dll");
640 std::string niCoreLib = concatPath(__pm->getRuntimePath(), "System.Private.CoreLib.ni.dll");
641 std::string niTmpCoreLib = concatPath(__pm->getRuntimePath(), "System.Private.CoreLib.ni.dll.tmp");
642 std::string coreLibBackup = concatPath(__pm->getRuntimePath(), "System.Private.CoreLib.dll.Backup");
644 std::vector<std::string> dllList;
645 std::vector<std::string> refPaths;
646 dllList.push_back(getAbsolutePath(coreLib));
648 if (!isFile(coreLibBackup) && !isR2RImage(coreLib)) {
649 if (crossgen2NoPipeLine(dllList, refPaths, opt) == NI_ERROR_NONE) {
650 if (rename(coreLib.c_str(), coreLibBackup.c_str())) {
651 _SERR("Failed to rename from System.Private.CoreLib.dll to System.Private.CoreLib.dll.Backup");
652 return NI_ERROR_CORE_NI_FILE;
654 if (opt->flags & NI_FLAGS_INPUT_BUBBLE) {
655 if (rename(niTmpCoreLib.c_str(), coreLib.c_str())) {
656 _SERR("Failed to rename from System.Private.CoreLib.ni.dll.tmp to Private.CoreLib.dll");
657 return NI_ERROR_CORE_NI_FILE;
660 if (rename(niCoreLib.c_str(), coreLib.c_str())) {
661 _SERR("Failed to rename from System.Private.CoreLib.ni.dll to Private.CoreLib.dll");
662 return NI_ERROR_CORE_NI_FILE;
666 _SERR("Failed to create native image for %s", coreLib.c_str());
667 return NI_ERROR_CORE_NI_FILE;
670 return NI_ERROR_NONE;
673 static ni_error_e doAOTList(std::vector<std::string>& dllList, const std::string& refPaths, NIOption* opt)
675 ni_error_e ret = NI_ERROR_NONE;
677 if (dllList.empty()) {
678 return NI_ERROR_INVALID_PARAMETER;
680 // When performing AOT for one Dll, an error is returned when an error occurs.
681 // However, when processing multiple dlls at once, only the log for errors is output and skipped.
683 std::string coreLib = concatPath(__pm->getRuntimePath(), "System.Private.CoreLib.dll");
686 std::vector<std::string> niList;
687 for (auto it = dllList.begin(); it != dllList.end(); it++) {
690 _SERR("dll file is not exist : %s", f.c_str());
693 if (!isManagedAssembly(f)) {
694 _SERR("Input file is not a dll file : %s", f.c_str());
697 // handle System.Private.CoreLib.dll separately.
698 // dllList and path manager contain absolute path. So, there is no need to change path to absolute path
703 niList.push_back(changeExtension(f, ".dll", ".ni.dll"));
707 // In the case of SPC, post-processing is required to change the name of the native image.
708 // In order to avoid repeatedly checking whether the generated native image is an SPC,
709 // the SPC native image generation is performed separately.
711 ret = createCoreLibNI(opt);
712 if (ret != NI_ERROR_NONE) {
717 // if there is no proper input after processing dll list
718 if (dllList.empty()) {
722 return NI_ERROR_INVALID_PARAMETER;
726 std::vector<std::string> paths;
727 splitPath(refPaths, paths);
729 if (opt->flags & NI_FLAGS_NO_PIPELINE) {
730 ret = crossgen2NoPipeLine(dllList, paths, opt);
732 std::vector<std::string> notCompiled;
733 ret = crossgen2PipeLine(dllList, paths, opt);
734 if (ret != NI_ERROR_NONE) {
735 _SERR("Crossgen2 is abnormally terminated. Regenerate native images that failed while running crossgen2.");
736 for (auto &dll : dllList) {
737 std::string tFile = changeExtension(dll, ".dll", ".ni.dll");
738 if (opt->flags & NI_FLAGS_INPUT_BUBBLE) {
742 notCompiled.push_back(dll);
745 _SERR("Retry running crossgen2 with --no-pipeline mode to avoid termination by OOM.");
746 ret = crossgen2NoPipeLine(notCompiled, paths, opt);
750 if (ret == NI_ERROR_NONE && opt->flags & NI_FLAGS_INPUT_BUBBLE) {
751 for (auto &dll : dllList) {
754 if (opt->flags & NI_FLAGS_APPNI) {
755 niFile = getAppNIFilePath(dll, opt);
757 niFile = getNIFilePath(dll);
759 tmpFile = niFile + ".tmp";
761 if (exist(tmpFile)) {
762 moveFile(tmpFile, niFile);
763 _SOUT("Native image %s generated successfully.", niFile.c_str());
771 static ni_error_e doAOTFile(const std::string& dllFile, const std::string& refPaths, NIOption* opt)
773 if (!isFile(dllFile)) {
774 _SERR("dll file is not exist : %s", dllFile.c_str());
775 return NI_ERROR_NO_SUCH_FILE;
778 if (!isManagedAssembly(dllFile)) {
779 _SERR("Failed. Input parameter is not managed dll (%s)\n", dllFile.c_str());
780 return NI_ERROR_INVALID_PARAMETER;
783 if (checkNIExistence(dllFile)) {
784 _SERR("Native image file is already exist : %s", dllFile.c_str());
785 return NI_ERROR_ALREADY_EXIST;
788 std::vector<std::string> dllList;
789 dllList.push_back(getAbsolutePath(dllFile));
790 return doAOTList(dllList, refPaths, opt);
793 // callback function of "pkgmgrinfo_appinfo_metadata_filter_foreach"
794 static int appAotCb(pkgmgrinfo_appinfo_h handle, void *userData)
798 NIOption **pOptions = (NIOption**)userData;
800 if ((*pOptions)->flags & NI_FLAGS_SKIP_RO_APP) {
801 bool isSystem = false;
802 int ret = pkgmgrinfo_appinfo_is_system(handle, &isSystem);
803 if (ret != PMINFO_R_OK) {
804 _SERR("Failed to check that app is System or not\n");
812 ret = pkgmgrinfo_appinfo_get_pkgid(handle, &pkgId);
813 if (ret != PMINFO_R_OK) {
814 _SERR("Failed to get pkgid");
818 if (removeNIUnderPkgRoot(pkgId) != NI_ERROR_NONE) {
819 _SERR("Failed to remove previous dlls from [%s]", pkgId);
823 if (createNIUnderPkgRoot(pkgId, *pOptions) != NI_ERROR_NONE) {
824 _SERR("Failed to generate NI file [%s]", pkgId);
827 _SOUT("Complete make application to native image");
833 ni_error_e initNICommon()
835 #if defined(__arm__) || defined(__aarch64__)
838 env = getenv("MIC_CROSSGEN2_ENABLED");
839 if (env != nullptr && !strcmp(env, "1")) {
840 CORERUN_CMD = std::string("/opt/usr/dotnet/mic/crossgen2");
842 CLRJIT_PATH = std::string("/opt/usr/dotnet/mic/libclrjit_unix_") + ARCHITECTURE_IDENTIFIER + std::string("_x64.so");
845 // get interval value
846 const static std::string intervalFile = concatPath(__NATIVE_LIB_DIR, "crossgen_interval.txt");
847 std::ifstream inFile(intervalFile);
849 _SOUT("crossgen_interval.txt is found");
850 inFile >> __interval;
853 if (initializePluginManager("normal")) {
854 _SERR("Fail to initialize PluginManager");
855 return NI_ERROR_UNKNOWN;
859 __pm = new PathManager();
860 } catch (const std::exception& e) {
861 _SERR("Failed to create PathManager");
862 return NI_ERROR_UNKNOWN;
865 char* pluginDllPaths = pluginGetDllPath();
866 if (pluginDllPaths && pluginDllPaths[0] != '\0') {
867 __pm->addPlatformAssembliesPaths(pluginDllPaths, true);
870 char* pluginNativePaths = pluginGetNativeDllSearchingPath();
871 if (pluginNativePaths && pluginNativePaths[0] != '\0') {
872 __pm->addNativeDllSearchingPaths(pluginNativePaths, true);
875 return NI_ERROR_NONE;
877 _SERR("crossgen supports arm/arm64 architecture only. skip ni file generation");
878 return NI_ERROR_NOT_SUPPORTED;
882 void finalizeNICommon()
886 finalizePluginManager();
893 __ni_option = nullptr;
897 ni_error_e createNIPlatform(std::string& extraInputs, NIOption* opt)
899 extraInputs += ":" + __pm->getRuntimePath();
900 extraInputs += ":" + __pm->getTizenFXPath();
902 return createNIUnderDirs(extraInputs, opt);
905 ni_error_e createNIDll(const std::string& dllPath, NIOption* opt)
907 return doAOTFile(dllPath, std::string(), opt);
910 ni_error_e createNIUnderTAC(const std::string& targetPath, const std::string& refPaths, NIOption* opt)
914 bool isAppNI = false;
915 if (opt->flags & NI_FLAGS_APPNI) {
919 if (opt->flags & NI_FLAGS_INPUT_BUBBLE) {
920 std::vector<std::string> refs;
921 splitPath(refPaths, refs);
922 for (auto &p: refs) {
923 if (isDirectory(p) && checkDllExistInDir(p)) {
924 opt->inputBubbleRefFiles.push_back(p + "/*.dll");
929 // get managed file list from targetPath
930 std::vector<std::string> dllList;
931 ret = getTargetDllList(targetPath, dllList);
932 if (ret != NI_ERROR_NONE) {
936 std::vector<std::string> needNIList;
937 std::vector<std::string> niList;
939 for (auto &dll : dllList) {
940 if (!checkNIExistence(dll)) {
941 needNIList.push_back(dll);
943 niList.push_back(getNIFilePath(dll));
946 if (!needNIList.empty()) {
947 // NI fils of TAC-related dlls under /opt/usr/dotnet should not be created under .native_image directory.
948 // So, unset NI_FLAGS_APPNI temporally and restore it after running AOT.
949 opt->flags &= ~NI_FLAGS_APPNI;
950 ret = doAOTList(needNIList, refPaths, opt);
952 opt->flags |= NI_FLAGS_APPNI;
954 if (ret != NI_ERROR_NONE) {
960 for (auto &niPath : niList) {
962 std::string symNIPath = concatPath(targetPath, getFileName(niPath));
963 if (!exist(symNIPath)) {
964 bf::create_symlink(niPath, symNIPath);
965 copySmackAndOwnership(targetPath.c_str(), symNIPath.c_str(), true);
966 _SOUT("%s symbolic link file generated successfully.", symNIPath.c_str());
967 _INFO("%s symbolic link file generated successfully.", symNIPath.c_str());
973 return NI_ERROR_NONE;
977 ni_error_e createNIUnderDirs(const std::string& rootPaths, NIOption* opt)
979 ni_error_e ret = NI_ERROR_NONE;
981 std::vector<std::string> fileList;
982 std::vector<std::string> paths;
983 splitPath(rootPaths, paths);
985 if (opt->flags & NI_FLAGS_INPUT_BUBBLE) {
986 for (auto &p: paths) {
987 if (isDirectory(p) && checkDllExistInDir(p)) {
988 opt->inputBubbleRefFiles.push_back(p + "/*.dll");
993 for (const auto &path : paths) {
998 if (path.find(TAC_SYMLINK_SUB_DIR) != std::string::npos) {
999 ret = createNIUnderTAC(path, rootPaths, opt);
1000 if (ret != NI_ERROR_NONE) {
1003 } else if (opt->flags & NI_FLAGS_APPNI) {
1004 ret = getAppTargetDllList(path, fileList, opt);
1005 if (ret != NI_ERROR_NONE) {
1009 ret = getTargetDllList(path, fileList);
1010 if (ret != NI_ERROR_NONE) {
1016 if (fileList.empty()) {
1017 return NI_ERROR_NONE;
1020 return doAOTList(fileList, rootPaths, opt);
1023 ni_error_e createNIUnderPkgRoot(const std::string& pkgId, NIOption* opt)
1025 if (!isR2RImage(concatPath(__pm->getRuntimePath(), "System.Private.CoreLib.dll"))) {
1026 _SERR("The native image of System.Private.CoreLib does not exist.\n"
1027 "Run the command to create the native image\n"
1028 "# dotnettool --ni-dll /usr/share/dotnet.tizen/netcoreapp/System.Private.CoreLib.dll");
1029 return NI_ERROR_CORE_NI_FILE;
1032 std::string rootPath = getRootPath(pkgId);
1033 if (rootPath.empty()) {
1034 _SERR("Failed to get root path from [%s]", pkgId.c_str());
1035 return NI_ERROR_INVALID_PACKAGE;
1038 __pm->setAppRootPath(rootPath);
1040 char* extraDllPaths = pluginGetExtraDllPath();
1041 if (extraDllPaths && extraDllPaths[0] != '\0') {
1042 opt->flags |= NI_FLAGS_EXTRA_REF;
1043 splitPath(extraDllPaths, opt->extraRefPath);
1046 opt->flags |= NI_FLAGS_APPNI;
1048 if (isReadOnlyArea(rootPath)) {
1049 opt->flags |= NI_FLAGS_APP_UNDER_RO_AREA;
1050 opt->flags |= NI_FLAGS_NO_PIPELINE;
1051 _SERR("Only no-pipeline mode supported for RO app. Set no-pipeline option forcibly");
1053 opt->flags &= ~NI_FLAGS_APP_UNDER_RO_AREA;
1056 // create native image under bin and lib directory
1057 // tac directory is skipped in the createNIUnderDirs.
1058 return createNIUnderDirs(__pm->getAppPaths(), opt);
1061 void removeNIPlatform()
1063 std::string coreLib = concatPath(__pm->getRuntimePath(), "System.Private.CoreLib.dll");
1064 std::string coreLibBackup = concatPath(__pm->getRuntimePath(), "System.Private.CoreLib.dll.Backup");
1066 if (isR2RImage(coreLib)) {
1067 if (!isFile(coreLibBackup)) {
1071 if (remove(coreLib.c_str())) {
1072 _SERR("Failed to remove System.Private.CoreLib native image file");
1074 if (rename(coreLibBackup.c_str(), coreLib.c_str())) {
1075 _SERR("Failed to rename System.Private.CoreLib.Backup to origin");
1079 #ifdef UNIQUE_DEFAULT_BASE_ADDR_SUPPORT
1080 if (isFile(__SYSTEM_BASE_FILE)) {
1081 if (remove(__SYSTEM_BASE_FILE)) {
1082 _SERR("Failed to remove %s", __SYSTEM_BASE_FILE);
1087 removeNIUnderDirs(__pm->getRuntimePath() + ":" + __pm->getTizenFXPath());
1090 void removeNIUnderDirs(const std::string& rootPaths)
1092 auto convert = [](const std::string& path, const std::string& filename) {
1093 if (isNativeImage(path)) {
1094 if (remove(path.c_str())) {
1095 _SERR("Failed to remove %s", path.c_str());
1100 std::vector<std::string> paths;
1101 splitPath(rootPaths, paths);
1102 for (const auto &path : paths) {
1103 scanFilesInDirectory(path, convert, -1);
1107 ni_error_e removeNIUnderPkgRoot(const std::string& pkgId)
1109 std::string rootPath = getRootPath(pkgId);
1110 if (rootPath.empty()) {
1111 _SERR("Failed to get root path from [%s]", pkgId.c_str());
1112 return NI_ERROR_INVALID_PACKAGE;
1115 __pm->setAppRootPath(rootPath);
1117 // getAppNIPaths returns bin/.native_image, lib/.native_image and .tac_symlink.
1118 std::string appNIPaths = __pm->getAppNIPaths();
1119 std::vector<std::string> paths;
1120 splitPath(appNIPaths, paths);
1121 for (const auto &path : paths) {
1122 if (!isReadOnlyArea(path)) {
1123 // Only the native image inside the TAC should be removed.
1124 if (strstr(path.c_str(), TAC_SYMLINK_SUB_DIR) != NULL) {
1125 removeNIUnderDirs(path);
1127 if (isDirectory(path)) {
1128 if (!removeAll(path.c_str())) {
1129 _SERR("Failed to remove app ni dir [%s]", path.c_str());
1136 // In special cases, the ni file may exist in the dll location.
1137 // The code below is to avoid this exceptional case.
1138 std::string appPaths = __pm->getAppPaths();
1139 splitPath(appPaths, paths);
1140 for (const auto &path : paths) {
1141 if (isDirectory(path)) {
1142 removeNIUnderDirs(path);
1146 return NI_ERROR_NONE;
1149 ni_error_e regenerateAppNI(NIOption* opt)
1152 pkgmgrinfo_appinfo_metadata_filter_h handle;
1154 ret = pkgmgrinfo_appinfo_metadata_filter_create(&handle);
1155 if (ret != PMINFO_R_OK)
1156 return NI_ERROR_UNKNOWN;
1158 ret = pkgmgrinfo_appinfo_metadata_filter_add(handle, AOT_METADATA_KEY, METADATA_VALUE);
1159 if (ret != PMINFO_R_OK) {
1160 pkgmgrinfo_appinfo_metadata_filter_destroy(handle);
1161 return NI_ERROR_UNKNOWN;
1164 ret = pkgmgrMDFilterForeach(handle, appAotCb, &opt);
1166 pkgmgrinfo_appinfo_metadata_filter_destroy(handle);
1167 return NI_ERROR_UNKNOWN;
1170 pkgmgrinfo_appinfo_metadata_filter_destroy(handle);
1171 return NI_ERROR_NONE;
1174 // callback function of "pkgmgrinfo_appinfo_metadata_filter_foreach"
1175 static int regenTacCb(pkgmgrinfo_appinfo_h handle, void *userData)
1178 NIOption **pOpt = (NIOption**)userData;
1180 int ret = pkgmgrinfo_appinfo_get_pkgid(handle, &pkgId);
1181 if (ret != PMINFO_R_OK || pkgId == NULL) {
1182 _SERR("Failed to get pkgid");
1186 sqlite3 *tac_db = openDB(TAC_APP_LIST_DB);
1188 _SERR("Sqlite open error");
1191 sqlite3_exec(tac_db, "BEGIN;", NULL, NULL, NULL);
1193 char *sql = sqlite3_mprintf("SELECT * FROM TAC WHERE PKGID = %Q;", pkgId);
1194 std::vector<std::string> nugets = selectDB(tac_db, sql);
1202 std::string nugetPaths;
1203 for (const auto &nuget : nugets) {
1204 if (!nugetPaths.empty()) {
1207 nugetPaths += concatPath(__DOTNET_DIR, nuget);
1210 for (auto& nuget : nugets) {
1211 createNIUnderTAC(concatPath(__DOTNET_DIR, nuget), nugetPaths, *pOpt);
1217 ni_error_e regenerateTACNI(NIOption* opt)
1219 removeNIUnderDirs(__DOTNET_DIR);
1221 pkgmgrinfo_appinfo_metadata_filter_h handle;
1222 int ret = pkgmgrinfo_appinfo_metadata_filter_create(&handle);
1223 if (ret != PMINFO_R_OK) {
1224 return NI_ERROR_UNKNOWN;
1227 ret = pkgmgrinfo_appinfo_metadata_filter_add(handle, TAC_METADATA_KEY, METADATA_VALUE);
1228 if (ret != PMINFO_R_OK) {
1229 pkgmgrinfo_appinfo_metadata_filter_destroy(handle);
1230 return NI_ERROR_UNKNOWN;
1233 ret = pkgmgrMDFilterForeach(handle, regenTacCb, &opt);
1235 pkgmgrinfo_appinfo_metadata_filter_destroy(handle);
1236 return NI_ERROR_UNKNOWN;
1239 pkgmgrinfo_appinfo_metadata_filter_destroy(handle);
1241 return NI_ERROR_NONE;