Changed API to check internet privilege
[platform/core/dotnet/launcher.git] / NativeLauncher / tool / ni_common.cc
index c4b535a..54e1c0b 100644 (file)
@@ -17,7 +17,6 @@
 #include <pkgmgr-info.h>
 #include <pkgmgr_installer_info.h>
 #include <aul.h>
-#include <tzplatform_config.h>
 
 #include "log.h"
 #include "utils.h"
@@ -32,7 +31,6 @@
 #include <fstream>
 #include <sstream>
 
-#include <pwd.h>
 #include <grp.h>
 #include <unistd.h>
 #include <string.h>
@@ -77,7 +75,8 @@ static const char* CROSSGEN_OPT_SINGLE_FILE_COMPILATION = "--single-file-compila
 //static const char* CROSSGEN_OPT_PARALLELISM = "--parallelism";
 //static const char* CROSSGEN_OPT_PARALLELISM_COUNT = "5";
 static const char* CROSSGEN_OPT_RESILIENT = "--resilient";
-static const char* CROSSGEN_OPT_OPTIMIZE = "-O";
+//static const char* CROSSGEN_OPT_OPTIMIZE = "-O";
+static const char* CROSSGEN_OPT_OPTIMIZE_TIME = "--Ot";
 static const char* CROSSGEN_OPT_INPUTBUBBLE = "--inputbubble";
 static const char* CROSSGEN_OPT_COMPILE_BUBBLE_GENERICS = "--compilebubblegenerics";
 static const char* CROSSGEN_OPT_VERBOSE = "--verbose";
@@ -111,26 +110,86 @@ static void waitInterval()
        }
 }
 
-static std::string getNIFilePath(const std::string& dllPath)
+#ifdef UNIQUE_DEFAULT_BASE_ADDR_SUPPORT
+// Get next base address to be used for system ni image from file
+// __SYSTEM_BASE_FILE should be checked for existance before calling this function
+static uintptr_t getNextBaseAddrFromFile()
 {
-       size_t index = dllPath.find_last_of(".");
-       if (index == std::string::npos) {
-               _SERR("File doesnot contain extension. fail to get NI file name");
-               return "";
+       FILE *pFile = fopen(__SYSTEM_BASE_FILE, "r");
+       if (pFile == NULL) {
+               _SERR("Failed to open %s", __SYSTEM_BASE_FILE);
+               return 0;
        }
-       std::string fName = dllPath.substr(0, index);
-       std::string fExt = dllPath.substr(index, dllPath.length());
 
-       // crossgen generate file with lower case extension only
-       std::transform(fExt.begin(), fExt.end(), fExt.begin(), ::tolower);
-       std::string niPath = fName + ".ni" + fExt;
+       uintptr_t addr = 0;
+       uintptr_t size = 0;
 
-       return niPath;
+       while (fscanf(pFile, "%" SCNxPTR " %" SCNuPTR "", &addr, &size) != EOF) {
+       }
+
+       fclose(pFile);
+
+       return addr + size;
+}
+
+// Get next base address to be used for system ni image
+static uintptr_t getNextBaseAddr()
+{
+       uintptr_t baseAddr = 0;
+
+       if (!isFile(__SYSTEM_BASE_FILE)) {
+               // This is the starting address for all default base addresses
+               baseAddr = DEFAULT_BASE_ADDR_START;
+       } else {
+               baseAddr = getNextBaseAddrFromFile();
+
+               // Round to a multple of 64K (see ZapImage::CalculateZapBaseAddress in CoreCLR)
+               uintptr_t BASE_ADDRESS_ALIGNMENT = 0xffff;
+               baseAddr = (baseAddr + BASE_ADDRESS_ALIGNMENT) & ~BASE_ADDRESS_ALIGNMENT;
+       }
+
+       return baseAddr;
+}
+
+// Save base address of system ni image to file
+static void updateBaseAddrFile(const std::string& absNIPath, uintptr_t baseAddr)
+{
+       uintptr_t niSize = getSizeOfImage(absNIPath);
+       if (niSize == 0) {
+               _SERR("File %s doesn't exist", absNIPath.c_str());
+               return;
+       }
+
+       // Write new entry to the file
+       FILE *pFile = fopen(__SYSTEM_BASE_FILE, "a");
+       if (pFile == NULL) {
+               _SERR("Failed to open %s", __SYSTEM_BASE_FILE);
+               return;
+       }
+
+       fprintf(pFile, "%" PRIxPTR " %" PRIuPTR "\n", baseAddr, niSize);
+       fclose(pFile);
 }
 
+// check if dll is listed in TPA
+static bool isTPADll(const std::string& dllPath)
+{
+       std::string absPath = getBaseName(getAbsolutePath(dllPath));
+
+       std::vector<std::string> paths = __pm->getPlatformAssembliesPaths();
+       for (unsigned int i = 0; i < paths.size(); i++) {
+               if (paths[i].find(getBaseName(absPath)) != std::string::npos) {
+                       return true;
+               }
+       }
+
+       return false;
+}
+#endif
+
 /**
  * @brief create the directory including parents directory, and
- *        copy ownership and smack labels to the created directory.
+ *          copy ownership and smack labels to the created directory.
  * @param[in] target directory path
  * @param[in] source directory path to get ownership and smack label
  * @return if directory created successfully, return true otherwise false
@@ -169,6 +228,23 @@ static bool createDirsAndCopyOwnerShip(std::string& target_path, const std::stri
        return true;
 }
 
+static std::string getNIFilePath(const std::string& dllPath)
+{
+       size_t index = dllPath.find_last_of(".");
+       if (index == std::string::npos) {
+               _SERR("File doesnot contain extension. fail to get NI file name");
+               return "";
+       }
+       std::string fName = dllPath.substr(0, index);
+       std::string fExt = dllPath.substr(index, dllPath.length());
+
+       // crossgen generate file with lower case extension only
+       std::transform(fExt.begin(), fExt.end(), fExt.begin(), ::tolower);
+       std::string niPath = fName + ".ni" + fExt;
+
+       return niPath;
+}
+
 static std::string getAppNIFilePath(const std::string& absDllPath, NIOption* opt)
 {
        std::string niDirPath;
@@ -213,6 +289,20 @@ static bool checkNIExistence(const std::string& absDllPath)
        return false;
 }
 
+static bool checkAppNIExistence(const std::string& absDllPath, NIOption* opt)
+{
+       std::string absNIPath = getAppNIFilePath(absDllPath, opt);
+       if (absNIPath.empty()) {
+               return false;
+       }
+
+       if (isFile(absNIPath)) {
+               return true;
+       }
+
+       return false;
+}
+
 static bool checkDllExistInDir(const std::string& path)
 {
        bool ret = false;
@@ -249,6 +339,29 @@ static ni_error_e getTargetDllList(const std::string& path, std::vector<std::str
        return NI_ERROR_NONE;
 }
 
+/*
+ * Get the list of managed files in the specific directory of Application
+ * Absolute paths of managed files are stored at the result list.
+ * If native image already exist in the .native_image directory, managed file is ignored.
+ *
+ */
+static ni_error_e getAppTargetDllList(const std::string& path, std::vector<std::string>& fileList, NIOption *opt)
+{
+       if (!isDirectory(path)) {
+               return NI_ERROR_INVALID_PARAMETER;
+       }
+
+       auto func = [&fileList, opt](const std::string& f_path, const std::string& f_name) {
+               if (isManagedAssembly(f_path) && !checkAppNIExistence(f_path, opt)) {
+                       fileList.push_back(getAbsolutePath(f_path));
+               }
+       };
+
+       scanFilesInDirectory(path, func, 0);
+
+       return NI_ERROR_NONE;
+}
+
 static void makeArgs(std::vector<const char*>& args, const std::vector<std::string>& refPaths, NIOption* opt)
 {
        args.push_back(CORERUN_CMD.c_str());
@@ -259,35 +372,23 @@ static void makeArgs(std::vector<const char*>& args, const std::vector<std::stri
        args.push_back(CLRJIT_PATH.c_str());
        args.push_back(CROSSGEN_OPT_TARGET_ARCH);
        args.push_back(ARCHITECTURE_IDENTIFIER);
-       if (!(opt->flags & NI_FLAGS_NO_PIPELINE)) {
-               args.push_back(CROSSGEN_OPT_OUT_NEAR_INPUT);
-               args.push_back(CROSSGEN_OPT_SINGLE_FILE_COMPILATION);
-       }
+
        //args.push_back(OPT_PARALLELISM);
        //args.push_back(OPT_PARALLELISM_COUNT);
        args.push_back(CROSSGEN_OPT_RESILIENT);
 
-       args.push_back(CROSSGEN_OPT_OPTIMIZE);
+       args.push_back(CROSSGEN_OPT_OPTIMIZE_TIME);
 
        if (opt->flags & NI_FLAGS_INPUT_BUBBLE) {
                args.push_back(CROSSGEN_OPT_INPUTBUBBLE);
                args.push_back(CROSSGEN_OPT_COMPILE_BUBBLE_GENERICS);
 
-               if (opt->flags & NI_FLAGS_INPUT_BUBBLE_REF) {
-                       INPUTBUBBLE_REF_VECTOR.clear();
-                       // check inputbubbleref format.
-                       for (const auto &path : opt->inputBubbleRefPath) {
-                               if (checkDllExistInDir(path)) {
-                                       INPUTBUBBLE_REF_VECTOR.push_back("--inputbubbleref:" + path + "/*.dll");
-                               }
-                       }
-                       // add ref path to inputbubble ref
-                       for (const auto &path : refPaths) {
-                               if (checkDllExistInDir(path)) {
-                                       INPUTBUBBLE_REF_VECTOR.push_back("--inputbubbleref:" + path + "/*.dll");
-                               }
-                       }
-                       for (const auto &path : INPUTBUBBLE_REF_VECTOR) {
+               INPUTBUBBLE_REF_VECTOR.clear();
+               for (const auto &path : opt->inputBubbleRefFiles) {
+                       INPUTBUBBLE_REF_VECTOR.push_back("--inputbubbleref:" + path);
+               }
+               for (const auto &path : INPUTBUBBLE_REF_VECTOR) {
+                       if (find(args.begin(), args.end(), path) == args.end()) {
                                args.push_back(path.c_str());
                        }
                }
@@ -299,7 +400,9 @@ static void makeArgs(std::vector<const char*>& args, const std::vector<std::stri
                        MIBC_VECTOR.push_back("--mibc:" + path);
                }
                for (const auto &path : MIBC_VECTOR) {
-                       args.push_back(path.c_str());
+                       if (find(args.begin(), args.end(), path) == args.end()) {
+                               args.push_back(path.c_str());
+                       }
                }
        }
 
@@ -310,13 +413,20 @@ static void makeArgs(std::vector<const char*>& args, const std::vector<std::stri
        REF_VECTOR.clear();
 
        // set reference path
-       if (opt->flags & NI_FLAGS_REF) {
-               for (const auto &path : opt->refPath) {
+       for (const auto &path : opt->refFiles) {
+               REF_VECTOR.push_back("-r:" + path);
+       }
+
+       std::vector<std::string> paths = __pm->getPlatformAssembliesPaths();
+       for (const auto &path : paths) {
+               if (checkDllExistInDir(path)) {
                        REF_VECTOR.push_back("-r:" + path + "/*.dll");
                }
-       } else {
-               std::vector<std::string> paths = __pm->getPlatformAssembliesPaths();
-               for (const auto &path : paths) {
+       }
+
+       if (opt->flags & NI_FLAGS_EXTRA_REF) {
+               for (const auto &erPath : opt->extraRefPath) {
+                       std::string path = getAbsolutePath(erPath);
                        if (checkDllExistInDir(path)) {
                                REF_VECTOR.push_back("-r:" + path + "/*.dll");
                        }
@@ -330,7 +440,9 @@ static void makeArgs(std::vector<const char*>& args, const std::vector<std::stri
        }
 
        for (const auto &path : REF_VECTOR) {
-               args.push_back(path.c_str());
+               if (find(args.begin(), args.end(), path) == args.end()) {
+                       args.push_back(path.c_str());
+               }
        }
 }
 
@@ -359,6 +471,40 @@ static ni_error_e makePdbSymlinkForNI(std::string dllPath, std::string niPath)
        return NI_ERROR_NONE;
 }
 
+static ni_error_e crossgen2PostAction(const std::string& dllPath, const std::string& niPath, NIOption* opt) {
+       std::string outFile = niPath;
+       if (!exist(outFile)) {
+               return NI_ERROR_NO_SUCH_FILE;
+       }
+       copySmackAndOwnership(dllPath, outFile);
+
+       // if AppNI then move ni.dll file to .native_image and copy pdb to .native_image
+       if (opt->flags & NI_FLAGS_APPNI) {
+               outFile = getAppNIFilePath(dllPath, opt);
+               makePdbSymlinkForNI(dllPath, outFile);
+
+               if (opt->flags & NI_FLAGS_INPUT_BUBBLE && opt->flags & NI_FLAGS_NO_PIPELINE) {
+                       outFile = outFile + ".tmp";
+               }
+
+               if (niPath != outFile) {
+                       moveFile(niPath, outFile);
+               }
+       }
+
+       if (opt->flags & NI_FLAGS_RM_ORIGIN_AFTER_NI) {
+               if (!removeFile(dllPath)) {
+                       _SERR("Fail to remove original file : %s", dllPath.c_str());
+               }
+       }
+
+       if (!(opt->flags & NI_FLAGS_INPUT_BUBBLE && opt->flags & NI_FLAGS_NO_PIPELINE)) {
+               _SOUT("Native image %s generated successfully.", outFile.c_str());
+       }
+
+       return NI_ERROR_NONE;
+}
+
 static ni_error_e crossgen2PipeLine(const std::vector<std::string>& dllList, const std::vector<std::string>& refPaths, NIOption* opt)
 {
        // fork crossgen2
@@ -371,30 +517,20 @@ static ni_error_e crossgen2PipeLine(const std::vector<std::string>& dllList, con
                waitpid(pid, &status, 0);
                if (WIFEXITED(status)) {
                        for (auto& dllPath: dllList) {
-                               std::string niPath = changeExtension(dllPath, ".dll", ".ni.dll");
-
-                               if (!exist(niPath)) {
-                                       _SERR("Fail to create native image for %s", dllPath.c_str());
-                                       return NI_ERROR_NO_SUCH_FILE;
-                               }
-
-                               copySmackAndOwnership(dllPath, niPath);
-                               // if AppNI then move ni.dll file to .native_image and copy pdb to .native_image
-                               if (opt->flags & NI_FLAGS_APPNI) {
-                                       std::string appNIPath = getAppNIFilePath(dllPath, opt);
-                                       moveFile(niPath, appNIPath);
-                                       makePdbSymlinkForNI(dllPath, appNIPath);
-                                       niPath = appNIPath;
+                               ni_error_e ret = crossgen2PostAction(dllPath, changeExtension(dllPath, ".dll", ".ni.dll"), opt);
+                               if (ret != NI_ERROR_NONE) {
+                                       return ret;
                                }
-
-                               _SOUT("Native image %s generated successfully.", niPath.c_str());
                        }
                } else {
                        _SERR("Failed. Forked process terminated abnormally");
+                       return NI_ERROR_ABNORMAL_PROCESS_TERMINATION;
                }
        } else {
                std::vector<const char*> argv;
                makeArgs(argv, refPaths, opt);
+               argv.push_back(CROSSGEN_OPT_OUT_NEAR_INPUT);
+               argv.push_back(CROSSGEN_OPT_SINGLE_FILE_COMPILATION);
 
                // add input files at the end of parameter
                for (const auto &input : dllList) {
@@ -429,6 +565,15 @@ static ni_error_e crossgen2NoPipeLine(const std::vector<std::string>& dllList, c
                } else {
                        niPath = getNIFilePath(dllPath);
                }
+#ifdef UNIQUE_DEFAULT_BASE_ADDR_SUPPORT
+               uintptr_t baseAddr = 0;
+               if (isTPADll(dllPath)) {
+                       baseAddr = getNextBaseAddr();
+               }
+#endif
+               if (opt->flags & NI_FLAGS_INPUT_BUBBLE) {
+                       niPath += ".tmp";
+               }
 
                // fork crossgen2
                pid_t pid = fork();
@@ -439,24 +584,35 @@ static ni_error_e crossgen2NoPipeLine(const std::vector<std::string>& dllList, c
                        int status;
                        waitpid(pid, &status, 0);
                        if (WIFEXITED(status)) {
-                               if (!exist(niPath)) {
-                                       _SERR("Fail to create native image for %s", dllPath.c_str());
-                                       return NI_ERROR_NO_SUCH_FILE;
+                               ni_error_e ret = crossgen2PostAction(dllPath, niPath, opt);
+                               if (ret != NI_ERROR_NONE) {
+                                       return ret;
                                }
-
-                               copySmackAndOwnership(dllPath, niPath);
-                               if (opt->flags & NI_FLAGS_APPNI) {
-                                       makePdbSymlinkForNI(dllPath, niPath);
+#ifdef UNIQUE_DEFAULT_BASE_ADDR_SUPPORT
+                               if (baseAddr != 0) {
+                                       updateBaseAddrFile(niPath, baseAddr);
                                }
-
-                               _SOUT("Native image %s generated successfully.", niPath.c_str());
+#endif
                        } else {
                                _SERR("Failed. Forked process terminated abnormally");
+                               _SERR("Crossgen2 was terminated by the OOM killer. Please check the system.");
+                               removeFile(changeExtension(niPath, ".ni.dll", ".ni.dll.tmp"));
+                               return NI_ERROR_ABNORMAL_PROCESS_TERMINATION;
                        }
                } else {
                        std::vector<const char*> argv;
                        makeArgs(argv, refPaths, opt);
 
+#ifdef UNIQUE_DEFAULT_BASE_ADDR_SUPPORT
+                       std::string baseAddrString;
+                       if (baseAddr != 0) {
+                               argv.push_back("--imagebase");
+                               std::stringstream ss;
+                               ss << "0x" << std::hex << baseAddr;
+                               baseAddrString = ss.str();
+                               argv.push_back(baseAddrString.c_str());
+                       }
+#endif
                        argv.push_back("-o");
                        argv.push_back(niPath.c_str());
 
@@ -488,6 +644,7 @@ static ni_error_e createCoreLibNI(NIOption* opt)
 {
        std::string coreLib = concatPath(__pm->getRuntimePath(), "System.Private.CoreLib.dll");
        std::string niCoreLib = concatPath(__pm->getRuntimePath(), "System.Private.CoreLib.ni.dll");
+       std::string niTmpCoreLib = concatPath(__pm->getRuntimePath(), "System.Private.CoreLib.ni.dll.tmp");
        std::string coreLibBackup = concatPath(__pm->getRuntimePath(), "System.Private.CoreLib.dll.Backup");
 
        std::vector<std::string> dllList;
@@ -495,14 +652,29 @@ static ni_error_e createCoreLibNI(NIOption* opt)
        dllList.push_back(getAbsolutePath(coreLib));
 
        if (!isFile(coreLibBackup) && !isR2RImage(coreLib)) {
-               if (crossgen2NoPipeLine(dllList, refPaths, opt) == NI_ERROR_NONE && exist(niCoreLib)) {
-                       if (rename(coreLib.c_str(), coreLibBackup.c_str())) {
-                               _SERR("Failed to rename System.Private.CoreLib.dll");
+               if (crossgen2NoPipeLine(dllList, refPaths, opt) == NI_ERROR_NONE) {
+                       if (opt->flags & NI_FLAGS_RM_ORIGIN_AFTER_NI) {
+                               std::ofstream output(coreLibBackup);
+                               if (!exist(coreLibBackup)) {
+                                       _SERR("Failed to create System.Private.CoreLib.dll.Backup");
+                                       return NI_ERROR_CORE_NI_FILE;
+                               }
+                               copySmackAndOwnership(__pm->getRuntimePath(), coreLibBackup, false);
+                               output.close();
+                       } else if (rename(coreLib.c_str(), coreLibBackup.c_str())) {
+                               _SERR("Failed to rename from System.Private.CoreLib.dll to System.Private.CoreLib.dll.Backup");
                                return NI_ERROR_CORE_NI_FILE;
                        }
-                       if (rename(niCoreLib.c_str(), coreLib.c_str())) {
-                               _SERR("Failed to rename System.Private.CoreLib.ni.dll");
-                               return NI_ERROR_CORE_NI_FILE;
+                       if (opt->flags & NI_FLAGS_INPUT_BUBBLE) {
+                               if (rename(niTmpCoreLib.c_str(), coreLib.c_str())) {
+                                       _SERR("Failed to rename from System.Private.CoreLib.ni.dll.tmp to Private.CoreLib.dll");
+                                       return NI_ERROR_CORE_NI_FILE;
+                               }                               
+                       } else {
+                               if (rename(niCoreLib.c_str(), coreLib.c_str())) {
+                                       _SERR("Failed to rename from System.Private.CoreLib.ni.dll to Private.CoreLib.dll");
+                                       return NI_ERROR_CORE_NI_FILE;
+                               }
                        }
                } else {
                        _SERR("Failed to create native image for %s", coreLib.c_str());
@@ -531,13 +703,13 @@ static ni_error_e doAOTList(std::vector<std::string>& dllList, const std::string
                        _SERR("dll file is not exist : %s", f.c_str());
                        dllList.erase(it--);
                }
-               if (!isManagedAssembly(f)) {
+               else if (!isManagedAssembly(f)) {
                        _SERR("Input file is not a dll file : %s", f.c_str());
                        dllList.erase(it--);
                }
                // handle System.Private.CoreLib.dll separately.
                // dllList and path manager contain absolute path. So, there is no need to change path to absolute path
-               if (f == coreLib) {
+               else if (f == coreLib) {
                        hasSPC = true;
                        dllList.erase(it--);
                }
@@ -568,7 +740,40 @@ static ni_error_e doAOTList(std::vector<std::string>& dllList, const std::string
        if (opt->flags & NI_FLAGS_NO_PIPELINE) {
                ret = crossgen2NoPipeLine(dllList, paths, opt);
        } else {
+               std::vector<std::string> notCompiled;
                ret = crossgen2PipeLine(dllList, paths, opt);
+               if (ret != NI_ERROR_NONE) {
+                       _SERR("Crossgen2 is abnormally terminated. Regenerate native images that failed while running crossgen2.");
+                       for (auto &dll : dllList) {
+                               std::string tFile = changeExtension(dll, ".dll", ".ni.dll");
+                               if (opt->flags & NI_FLAGS_INPUT_BUBBLE) {
+                                       tFile += ".tmp";
+                               }
+                               if (!exist(tFile)) {
+                                       notCompiled.push_back(dll);
+                               }
+                       }
+                       _SERR("Retry running crossgen2 with --no-pipeline mode to avoid termination by OOM.");
+                       ret = crossgen2NoPipeLine(notCompiled, paths, opt);
+               }
+       }
+
+       if (ret == NI_ERROR_NONE && opt->flags & NI_FLAGS_INPUT_BUBBLE) {
+               for (auto &dll : dllList) {
+                       std::string tmpFile;
+                       std::string niFile;
+                       if (opt->flags & NI_FLAGS_APPNI) {
+                               niFile = getAppNIFilePath(dll, opt);
+                       } else {
+                               niFile = getNIFilePath(dll);
+                       }
+                       tmpFile = niFile + ".tmp";
+
+                       if (exist(tmpFile)) {
+                               moveFile(tmpFile, niFile);
+                               _SOUT("Native image %s generated successfully.", niFile.c_str());
+                       }
+               }
        }
 
        return ret;
@@ -603,6 +808,18 @@ static int appAotCb(pkgmgrinfo_appinfo_h handle, void *userData)
        int ret = 0;
        NIOption **pOptions = (NIOption**)userData;
 
+       if ((*pOptions)->flags & NI_FLAGS_SKIP_RO_APP) {
+               bool isSystem = false;
+               int ret = pkgmgrinfo_appinfo_is_system(handle, &isSystem);
+               if (ret != PMINFO_R_OK) {
+                       _SERR("Failed to check that app is System or not\n");
+                       return -1;
+               }
+               if (isSystem) {
+                       return 0;
+               }
+       }
+
        ret = pkgmgrinfo_appinfo_get_pkgid(handle, &pkgId);
        if (ret != PMINFO_R_OK) {
                _SERR("Failed to get pkgid");
@@ -688,14 +905,12 @@ void finalizeNICommon()
        }
 }
 
-ni_error_e createNIPlatform(NIOption* opt)
+ni_error_e createNIPlatform(std::string& extraInputs, NIOption* opt)
 {
-       ni_error_e ret = createNIUnderDirs(__pm->getRuntimePath(), opt);
-       if (ret != NI_ERROR_NONE) {
-               return ret;
-       }
+       extraInputs += ":" + __pm->getRuntimePath();
+       extraInputs += ":" + __pm->getTizenFXPath();
 
-       return createNIUnderDirs(__pm->getTizenFXPath(), opt);
+       return createNIUnderDirs(extraInputs, opt);
 }
 
 ni_error_e createNIDll(const std::string& dllPath, NIOption* opt)
@@ -707,6 +922,21 @@ ni_error_e createNIUnderTAC(const std::string& targetPath, const std::string& re
 {
        ni_error_e ret;
 
+       bool isAppNI = false;
+       if (opt->flags & NI_FLAGS_APPNI) {
+               isAppNI = true;
+       }
+
+       if (opt->flags & NI_FLAGS_INPUT_BUBBLE) {
+               std::vector<std::string> refs;
+               splitPath(refPaths, refs);
+               for (auto &p: refs) {
+                       if (isDirectory(p) && checkDllExistInDir(p)) {
+                               opt->inputBubbleRefFiles.push_back(p + "/*.dll");
+                       }
+               }
+       }
+
        // get managed file list from targetPath
        std::vector<std::string> dllList;
        ret = getTargetDllList(targetPath, dllList);
@@ -729,20 +959,24 @@ ni_error_e createNIUnderTAC(const std::string& targetPath, const std::string& re
                // So, unset NI_FLAGS_APPNI temporally and restore it after running AOT.
                opt->flags &= ~NI_FLAGS_APPNI;
                ret = doAOTList(needNIList, refPaths, opt);
-               opt->flags |= NI_FLAGS_APPNI;
+               if (isAppNI) {
+                       opt->flags |= NI_FLAGS_APPNI;
+               }
                if (ret != NI_ERROR_NONE) {
                        return ret;
                }
        }
 
-       for (auto &niPath : niList) {
-               if (exist(niPath)) {
-                       std::string symNIPath = concatPath(targetPath, getFileName(niPath));
-                       if (!exist(symNIPath)) {
-                               bf::create_symlink(niPath, symNIPath);
-                               copySmackAndOwnership(targetPath.c_str(), symNIPath.c_str(), true);
-                               _SOUT("%s symbolic link file generated successfully.", symNIPath.c_str());
-                               _INFO("%s symbolic link file generated successfully.", symNIPath.c_str());
+       if (isAppNI) {
+               for (auto &niPath : niList) {
+                       if (exist(niPath)) {
+                               std::string symNIPath = concatPath(targetPath, getFileName(niPath));
+                               if (!exist(symNIPath)) {
+                                       bf::create_symlink(niPath, symNIPath);
+                                       copySmackAndOwnership(targetPath.c_str(), symNIPath.c_str(), true);
+                                       _SOUT("%s symbolic link file generated successfully.", symNIPath.c_str());
+                                       _INFO("%s symbolic link file generated successfully.", symNIPath.c_str());
+                               }
                        }
                }
        }
@@ -759,6 +993,14 @@ ni_error_e createNIUnderDirs(const std::string& rootPaths, NIOption* opt)
        std::vector<std::string> paths;
        splitPath(rootPaths, paths);
 
+       if (opt->flags & NI_FLAGS_INPUT_BUBBLE) {
+               for (auto &p: paths) {
+                       if (isDirectory(p) && checkDllExistInDir(p)) {
+                               opt->inputBubbleRefFiles.push_back(p + "/*.dll");
+                       }
+               }
+       }
+
        for (const auto &path : paths) {
                if (!exist(path)) {
                        continue;
@@ -769,6 +1011,11 @@ ni_error_e createNIUnderDirs(const std::string& rootPaths, NIOption* opt)
                        if (ret != NI_ERROR_NONE) {
                                return ret;
                        }
+               } else if (opt->flags & NI_FLAGS_APPNI) {
+                       ret = getAppTargetDllList(path, fileList, opt);
+                       if (ret != NI_ERROR_NONE) {
+                               return ret;
+                       }
                } else {
                        ret = getTargetDllList(path, fileList);
                        if (ret != NI_ERROR_NONE) {
@@ -786,6 +1033,13 @@ ni_error_e createNIUnderDirs(const std::string& rootPaths, NIOption* opt)
 
 ni_error_e createNIUnderPkgRoot(const std::string& pkgId, NIOption* opt)
 {
+       if (!isR2RImage(concatPath(__pm->getRuntimePath(), "System.Private.CoreLib.dll"))) {
+               _SERR("The native image of System.Private.CoreLib does not exist.\n"
+                               "Run the command to create the native image\n"
+                               "# dotnettool --ni-dll /usr/share/dotnet.tizen/netcoreapp/System.Private.CoreLib.dll");
+               return NI_ERROR_CORE_NI_FILE;
+       }
+
        std::string rootPath = getRootPath(pkgId);
        if (rootPath.empty()) {
                _SERR("Failed to get root path from [%s]", pkgId.c_str());
@@ -796,7 +1050,8 @@ ni_error_e createNIUnderPkgRoot(const std::string& pkgId, NIOption* opt)
 
        char* extraDllPaths = pluginGetExtraDllPath();
        if (extraDllPaths && extraDllPaths[0] != '\0') {
-               __pm->setExtraDllPaths(extraDllPaths);
+               opt->flags |= NI_FLAGS_EXTRA_REF;
+               splitPath(extraDllPaths, opt->extraRefPath);
        }
 
        opt->flags |= NI_FLAGS_APPNI;
@@ -847,8 +1102,13 @@ void removeNIUnderDirs(const std::string& rootPaths)
 {
        auto convert = [](const std::string& path, const std::string& filename) {
                if (isNativeImage(path)) {
-                       if (remove(path.c_str())) {
-                               _SERR("Failed to remove %s", path.c_str());
+                       std::string assemblyPath = changeExtension(path, ".ni.dll", ".dll");
+                       if (exist(assemblyPath)) {
+                               if (remove(path.c_str())) {
+                                       _SERR("Failed to remove %s", path.c_str());
+                               }
+                       } else {
+                               _SOUT("%s cannot be removed because there is no %s", path.c_str(), assemblyPath.c_str());
                        }
                }
        };
@@ -997,124 +1257,3 @@ ni_error_e regenerateTACNI(NIOption* opt)
        return NI_ERROR_NONE;
 }
 
-static std::vector<uid_t> getUserIds()
-{
-       std::vector<uid_t> list;
-
-       while (true) {
-               errno = 0; // so we can distinguish errors from no more entries
-               passwd* entry = getpwent();
-               if (!entry) {
-                       if (errno) {
-                               _SERR("Error while getting userIDs");
-                               list.clear();
-                               return list;
-                       }
-                       break;
-               }
-               list.push_back(entry->pw_uid);
-       }
-       endpwent();
-
-       return list;
-}
-
-static std::string getAppDataPath(const std::string& pkgId, uid_t uid)
-{
-       std::string pDataFile;
-
-       tzplatform_set_user(uid);
-
-       const char* tzUserApp = tzplatform_getenv(TZ_USER_APP);
-       if (tzUserApp != NULL) {
-               pDataFile = std::string(tzUserApp) + "/" + pkgId + "/data/";
-       }
-
-       tzplatform_reset_user();
-
-       return pDataFile;
-}
-
-ni_error_e removeAppProfileData(const std::string& pkgId)
-{
-       if (pkgId.empty()) {
-               return NI_ERROR_INVALID_PARAMETER;
-       }
-
-       std::vector<uid_t> uidList = getUserIds();
-       for (auto& uid : uidList) {
-               // get data path from pkgid
-               std::string dataPath = getAppDataPath(pkgId, uid);
-               if (!dataPath.empty() && exist(dataPath)) {
-                       std::string pDataFile = dataPath + PROFILE_BASENAME;
-
-                       if (exist(pDataFile)) {
-                               if (!removeFile(pDataFile)) {
-                                       _SERR("Fail to remove profile data file (%s).", pDataFile.c_str());
-                                       return NI_ERROR_UNKNOWN;
-                               }
-                               _SOUT("Profile data (%s) is removed successfully", pDataFile.c_str());
-                       }
-               }
-       }
-
-       return NI_ERROR_NONE;
-}
-
-static int appTypeListCb(pkgmgrinfo_appinfo_h handle, void *user_data)
-{
-       char *pkgId = NULL;
-       int ret = pkgmgrinfo_appinfo_get_pkgid(handle, &pkgId);
-       if (ret != PMINFO_R_OK || pkgId == NULL) {
-               _SERR("Fail to get pkgid");
-               return 0;
-       }
-
-       if (removeAppProfileData(pkgId) != NI_ERROR_NONE) {
-               _SERR("Fail to remove profile data for (%s)", pkgId);
-       }
-
-       return 0;
-}
-
-static ni_error_e removeAppProfileByAppType(const char* type)
-{
-       int ret;
-
-       pkgmgrinfo_appinfo_filter_h filter;
-
-       ret = pkgmgrinfo_appinfo_filter_create(&filter);
-       if (ret != PMINFO_R_OK) {
-               _SERR("Fail to create appinfo filter");
-               return NI_ERROR_UNKNOWN;
-       }
-
-       ret = pkgmgrinfo_appinfo_filter_add_string(filter, PMINFO_APPINFO_PROP_APP_TYPE, type);
-       if (ret != PMINFO_R_OK) {
-               pkgmgrinfo_appinfo_filter_destroy(filter);
-               _SERR("Fail to add appinfo filter (%s)", type);
-               return NI_ERROR_UNKNOWN;
-       }
-
-       ret = pkgmgrinfo_appinfo_filter_foreach_appinfo(filter, appTypeListCb, NULL);
-       if (ret != PMINFO_R_OK) {
-               _SERR("Fail to pkgmgrinfo_pkginfo_filter_foreach_pkginfo");
-               pkgmgrinfo_appinfo_filter_destroy(filter);
-               return NI_ERROR_UNKNOWN;
-       }
-
-       pkgmgrinfo_appinfo_filter_destroy(filter);
-
-       return NI_ERROR_NONE;
-}
-
-void removeAllAppProfileData()
-{
-       std::vector<const char*> appTypeList = {"dotnet", "dotnet-nui", "dotnet-inhouse"};
-
-       for (auto& type : appTypeList) {
-               if (removeAppProfileByAppType(type) != NI_ERROR_NONE) {
-                       _SERR("Fail to removeAppProfileByAppType for type (%s)", type);
-               }
-       }
-}