Modify the problem that the original assembly is deleted (#80)
[platform/core/dotnet/launcher.git] / NativeLauncher / tool / ni_common.cc
index d2b7ec3..309f1cd 100644 (file)
 
 #include <algorithm>
 #include <string>
+#include <fstream>
 
 #include <pwd.h>
 #include <grp.h>
-#include <sys/stat.h>
 #include <unistd.h>
 #include <string.h>
 
-#include <fstream>
-#include <sys/smack.h>
-
 #include "ni_common.h"
+#include "tac_common.h"
 #include "path_manager.h"
 #include "plugin_manager.h"
 
 #ifdef  LOG_TAG
 #undef  LOG_TAG
 #endif
-#define LOG_TAG "NETCORE_INSTALLER_PLUGIN"
+#define LOG_TAG "DOTNET_INSTALLER_PLUGIN"
 
 #ifndef CROSSGEN_PATH
 #error "CROSSGEN_PATH is missed"
 #define __XSTR(x) #x
 #define __STR(x) __XSTR(x)
 static const char* __CROSSGEN_PATH = __STR(CROSSGEN_PATH);
+
+#ifdef UNIQUE_DEFAULT_BASE_ADDR_SUPPORT
+static const char* __SYSTEM_BASE_FILE = __STR(SYSTEM_BASE_FILE);
+#endif
+
 #undef __STR
 #undef __XSTR
 
@@ -69,26 +72,6 @@ static void waitInterval()
        }
 }
 
-static void updateNiFileInfo(const std::string& dllPath, const std::string& niPath)
-{
-       char* label = NULL;
-
-       // change smack label
-       if (smack_getlabel(dllPath.c_str(), &label, SMACK_LABEL_ACCESS) == 0) {
-               if (smack_setlabel(niPath.c_str(), label, SMACK_LABEL_ACCESS) < 0) {
-                       fprintf(stderr, "Fail to set smack label\n");
-               }
-               free(label);
-       }
-
-       // change owner and groups for generated ni file.
-       struct stat info;
-       if (!stat(dllPath.c_str(), &info)) {
-               if (chown(niPath.c_str(), info.st_uid, info.st_gid) == -1)
-                       fprintf(stderr, "Failed to change owner and group name\n");
-       }
-}
-
 static std::string getNiFilePath(const std::string& dllPath)
 {
        size_t index = dllPath.find_last_of(".");
@@ -108,25 +91,30 @@ static std::string getNiFilePath(const std::string& dllPath)
 
 static std::string getAppNIPath(const std::string& niPath)
 {
+       std::string fileName;
+       std::string niDirPath;
+       std::string prevPath;
+
        size_t index = niPath.find_last_of("/");
-       if (index == std::string::npos) {
-               fprintf(stderr, "dllPath doesnot contains path info\n");
-               return "";
+       if (index != std::string::npos) {
+               prevPath = niPath.substr(0, index);
+               fileName = niPath.substr(index + 1, niPath.length());
+       } else {
+               prevPath = ".";
+               fileName = niPath;
        }
 
-       std::string prevPath = niPath.substr(0, index);
-       std::string fileName = niPath.substr(index, niPath.length());
-       std::string niDirPath = prevPath + APP_NI_SUB_DIR;
+       niDirPath = concatPath(prevPath, APP_NI_SUB_DIR);
 
        if (!isFileExist(niDirPath)) {
                if (mkdir(niDirPath.c_str(), 0755) == 0) {
-                       updateNiFileInfo(prevPath, niDirPath);
+                       updateAssemblyInfo(prevPath, niDirPath);
                } else {
                        fprintf(stderr, "Fail to create app ni directory (%s)\n", niDirPath.c_str());
                }
        }
 
-       return niDirPath + fileName;
+       return concatPath(niDirPath, fileName);
 }
 
 static bool niExist(const std::string& path)
@@ -152,7 +140,88 @@ static bool niExist(const std::string& path)
        return false;
 }
 
-static ni_error_e crossgen(const std::string& dllPath, const std::string& appPath, bool enableR2R, bool isAppNI = false)
+#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()
+{
+       FILE *pFile = fopen(__SYSTEM_BASE_FILE, "r");
+       if (pFile == NULL) {
+               fprintf(stderr, "Failed to open %s\n", __SYSTEM_BASE_FILE);
+               return 0;
+       }
+
+       uintptr_t addr = 0;
+       uintptr_t size = 0;
+
+       while (fscanf(pFile, "%u %u", &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 (!isFileExist(__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 &dllPath, uintptr_t baseAddr)
+{
+       std::string absNiPath = getNiFilePath(dllPath);
+       if (absNiPath.empty()) {
+               fprintf(stderr, "Failed to get ni file path for %s\n", dllPath.c_str());
+               return;
+       }
+
+       uintptr_t niSize = getFileSize(absNiPath);
+       if (niSize == 0) {
+               fprintf(stderr, "File %s doesn't exist\n", absNiPath.c_str());
+               return;
+       }
+
+       // Write new entry to the file
+       FILE *pFile = fopen(__SYSTEM_BASE_FILE, "a");
+       if (pFile == NULL) {
+               fprintf(stderr, "Failed to open %s\n", __SYSTEM_BASE_FILE);
+               return;
+       }
+
+       fprintf(pFile, "%u %u\n", baseAddr, niSize);
+       fclose(pFile);
+}
+
+// check if dll is listed in TPA
+static bool isTPADll(const std::string &dllPath)
+{
+       std::string absDllPath = absolutePath(dllPath);
+
+       if (__tpa.find(absDllPath) != std::string::npos) {
+               return true;
+       }
+
+       return false;
+}
+#endif
+
+// baseAddr should be checked in file before getting here
+static ni_error_e crossgen(const std::string& dllPath, const std::string& appPath, bool enableR2R, uintptr_t baseAddr = 0, bool isAppNI = false)
 {
        if (!isFileExist(dllPath)) {
                fprintf(stderr, "dll file is not exist : %s\n", dllPath.c_str());
@@ -191,7 +260,7 @@ static ni_error_e crossgen(const std::string& dllPath, const std::string& appPat
                        // Do not use niExist() function to check whether ni file created or not.
                        // niEixst() return false for System.Private.Corelib.dll
                        if (isFileExist(absNiPath)) {
-                               updateNiFileInfo(absDllPath, absNiPath);
+                               updateAssemblyInfo(absDllPath, absNiPath);
                                return NI_ERROR_NONE;
                        } else {
                                fprintf(stderr, "Fail to create native image for %s\n", dllPath.c_str());
@@ -211,6 +280,13 @@ static ni_error_e crossgen(const std::string& dllPath, const std::string& appPat
                        argv.push_back("/FragileNonVersionable");
                }
 
+#ifdef UNIQUE_DEFAULT_BASE_ADDR_SUPPORT
+               if (baseAddr != 0) {
+                       argv.push_back("/BaseAddress");
+                       argv.push_back(std::to_string(baseAddr).c_str());
+               }
+#endif
+
                argv.push_back("/App_Paths");
                std::string absAppPath;
                if (!appPath.empty()) {
@@ -235,40 +311,6 @@ static ni_error_e crossgen(const std::string& dllPath, const std::string& appPat
        return NI_ERROR_NONE;
 }
 
-static ni_error_e getRootPath(std::string pkgId, std::string& rootPath)
-{
-       int ret = 0;
-       char *path = 0;
-
-       uid_t uid = 0;
-
-       if (pkgmgr_installer_info_get_target_uid(&uid) < 0) {
-               _ERR("Failed to get UID");
-               return NI_ERROR_UNKNOWN;
-       }
-
-       pkgmgrinfo_pkginfo_h handle;
-       if (uid == 0) {
-               ret = pkgmgrinfo_pkginfo_get_pkginfo(pkgId.c_str(), &handle);
-               if (ret != PMINFO_R_OK)
-                       return NI_ERROR_UNKNOWN;
-       } else {
-               ret = pkgmgrinfo_pkginfo_get_usr_pkginfo(pkgId.c_str(), uid, &handle);
-               if (ret != PMINFO_R_OK)
-                       return NI_ERROR_UNKNOWN;
-       }
-
-       ret = pkgmgrinfo_pkginfo_get_root_path(handle, &path);
-       if (ret != PMINFO_R_OK) {
-               pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
-               return NI_ERROR_UNKNOWN;
-       }
-       rootPath = path;
-       pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
-
-       return NI_ERROR_NONE;
-}
-
 // callback function of "pkgmgrinfo_appinfo_metadata_filter_foreach"
 static int appAotCb(pkgmgrinfo_appinfo_h handle, void *userData)
 {
@@ -282,30 +324,56 @@ static int appAotCb(pkgmgrinfo_appinfo_h handle, void *userData)
                return -1;
        }
 
-       if (removeNiUnderPkgRoot(pkgId) != 0) {
+       if (removeNiUnderPkgRoot(pkgId) != NI_ERROR_NONE) {
                fprintf(stderr, "Failed to remove previous dlls from [%s]\n", pkgId);
                return -1;
        }
 
+       if (resetTACPackage(pkgId) != TAC_ERROR_NONE) {
+               fprintf(stderr, "Failed to remove symlink for given package [%s]\n", pkgId);
+               return -1;
+       }
+
        // Regenerate ni files with R2R mode forcibiliy. (there is no way to now which option is used)
-       if (createNiUnderPkgRoot(pkgId, *enableR2R) != 0) {
-               fprintf(stderr, "Failed to get root path from [%s]\n", pkgId);
+       if (createNiUnderPkgRoot(pkgId, *enableR2R) != NI_ERROR_NONE) {
+               fprintf(stderr, "Failed to generate NI file [%s]\n", pkgId);
                return -1;
        } else {
                fprintf(stderr, "Complete make application to native image\n");
        }
 
+       if (createTACPackage(pkgId) != TAC_ERROR_NONE) {
+               fprintf(stderr, "Failed to generate symbolic link file [%s]\n", pkgId);
+               return -1;
+       }else {
+               fprintf(stderr, "Complete make symbolic link file to tac\n");
+       }
+
        return 0;
 }
 
-static void createCoreLibNI(bool enableR2R)
+static void createCoreLibNI(bool enableR2R, bool doGenUniqueBaseSystem)
 {
        std::string coreLib = concatPath(getRuntimeDir(), "System.Private.CoreLib.dll");
        std::string niCoreLib = concatPath(getRuntimeDir(), "System.Private.CoreLib.ni.dll");
        std::string coreLibBackup = concatPath(getRuntimeDir(), "System.Private.CoreLib.dll.Backup");
 
        if (!isFileExist(coreLibBackup)) {
-               if (!crossgen(coreLib, std::string(), enableR2R)) {
+               uintptr_t baseAddr = 0;
+
+#ifdef UNIQUE_DEFAULT_BASE_ADDR_SUPPORT
+               if (doGenUniqueBaseSystem) {
+                       baseAddr = getNextBaseAddr();
+               }
+#endif
+
+               if (!crossgen(coreLib, std::string(), enableR2R, baseAddr)) {
+#ifdef UNIQUE_DEFAULT_BASE_ADDR_SUPPORT
+                       if (doGenUniqueBaseSystem && baseAddr != 0) {
+                               updateBaseAddrFile(coreLib, baseAddr);
+                       }
+#endif
+
                        if (rename(coreLib.c_str(), coreLibBackup.c_str())) {
                                fprintf(stderr, "Failed to rename System.Private.CoreLib.dll\n");
                        }
@@ -358,27 +426,43 @@ void finalizeNICommon()
 }
 
 
-void createNiPlatform(bool enableR2R)
+void createNiPlatform(bool enableR2R, bool doGenUniqueBaseSystem)
 {
        const std::string platformDirs[] = {getRuntimeDir(), getTizenFXDir()};
-       createNiUnderDirs(platformDirs, 2, enableR2R);
+       createNiUnderDirs(platformDirs, 2, enableR2R, doGenUniqueBaseSystem);
 }
 
-ni_error_e createNiDll(const std::string& dllPath, bool enableR2R)
+ni_error_e createNiDll(const std::string& dllPath, bool enableR2R, bool doGenUniqueBaseSystem)
 {
-       createCoreLibNI(enableR2R);
+       createCoreLibNI(enableR2R, doGenUniqueBaseSystem);
        // System.Private.CoreLib.dll is generated in the createCoreLibNI function.
        // Skip if input dll is System.Private.CoreLib.dll
        if (dllPath.find("System.Private.CoreLib.dll") != std::string::npos) {
                return NI_ERROR_NONE;
        }
 
-       return crossgen(dllPath, std::string(), enableR2R);
+       uintptr_t baseAddr = 0;
+
+#ifdef UNIQUE_DEFAULT_BASE_ADDR_SUPPORT
+       if (doGenUniqueBaseSystem && isTPADll(dllPath)) {
+               baseAddr = getNextBaseAddr();
+       }
+#endif
+
+       ni_error_e status = crossgen(dllPath, std::string(), enableR2R, baseAddr);
+
+#ifdef UNIQUE_DEFAULT_BASE_ADDR_SUPPORT
+       if (doGenUniqueBaseSystem && baseAddr != 0) {
+               updateBaseAddrFile(dllPath, baseAddr);
+       }
+#endif
+
+       return status;
 }
 
-void createNiUnderDirs(const std::string rootPaths[], int count, bool enableR2R, bool isAppNI)
+void createNiUnderDirs(const std::string rootPaths[], int count, bool enableR2R, bool doGenUniqueBaseSystem, bool isAppNI)
 {
-       createCoreLibNI(enableR2R);
+       createCoreLibNI(enableR2R, doGenUniqueBaseSystem);
 
        std::string appPaths;
        for (int i = 0; i < count; i++) {
@@ -389,8 +473,22 @@ void createNiUnderDirs(const std::string rootPaths[], int count, bool enableR2R,
        if (appPaths.back() == ':')
                appPaths.pop_back();
 
-       auto convert = [&appPaths, enableR2R, isAppNI](const std::string& path, const char* name) {
-               if (!crossgen(path, appPaths.c_str(), enableR2R, isAppNI)) {
+       auto convert = [&appPaths, enableR2R, doGenUniqueBaseSystem, isAppNI](const std::string& path, const char* name) {
+               uintptr_t baseAddr = 0;
+
+#ifdef UNIQUE_DEFAULT_BASE_ADDR_SUPPORT
+               if (doGenUniqueBaseSystem && !isAppNI && isTPADll(path)) {
+                       baseAddr = getNextBaseAddr();
+               }
+#endif
+
+               if (!crossgen(path, appPaths.c_str(), enableR2R, baseAddr, isAppNI)) {
+#ifdef UNIQUE_DEFAULT_BASE_ADDR_SUPPORT
+                       if (doGenUniqueBaseSystem && !isAppNI && baseAddr != 0) {
+                               updateBaseAddrFile(path, baseAddr);
+                       }
+#endif
+
                        waitInterval();
                }
        };
@@ -403,17 +501,16 @@ void createNiUnderDirs(const std::string rootPaths[], int count, bool enableR2R,
 ni_error_e createNiUnderPkgRoot(const std::string& pkgId, bool enableR2R)
 {
        std::string pkgRoot;
-       if (getRootPath(pkgId, pkgRoot) != NI_ERROR_NONE) {
-               fprintf(stderr, "Failed to get root path from [%s]\n", pkgId.c_str());
+       if (getRootPath(pkgId, pkgRoot) < 0) {
                return NI_ERROR_INVALID_PACKAGE;
        }
 
        std::string binDir = concatPath(pkgRoot, "bin");
        std::string libDir = concatPath(pkgRoot, "lib");
-       std::string appTAC = concatPath(binDir, ".TAC.Release");
-       std::string paths[] = {binDir, libDir, appTAC};
+       std::string tacDir = concatPath(binDir, TAC_SYMLINK_SUB_DIR);
+       std::string paths[] = {binDir, libDir, tacDir};
 
-       createNiUnderDirs(paths, 3, enableR2R, true);
+       createNiUnderDirs(paths, 3, enableR2R, false, true);
 
        return NI_ERROR_NONE;
 }
@@ -422,15 +519,37 @@ ni_error_e createNiDllUnderPkgRoot(const std::string& pkgId, const std::string&
 {
        std::string pkgRoot;
        if (getRootPath(pkgId, pkgRoot) < 0) {
-               fprintf(stderr, "Failed to get root path from [%s]\n", pkgId.c_str());
                return NI_ERROR_INVALID_PACKAGE;
        }
 
        std::string binDir = concatPath(pkgRoot, "bin");
        std::string libDir = concatPath(pkgRoot, "lib");
-       std::string paths = binDir + ":" + libDir;
-
-       return crossgen(dllPath, paths, enableR2R, true);
+       std::string tacDir = concatPath(binDir, TAC_SYMLINK_SUB_DIR);
+       std::string paths = binDir + ":" + libDir + ":" + tacDir;
+
+       if (bf::is_symlink(dllPath)) {
+               if (bf::exists(tacDir)) {
+                       if (!isNativeImage(dllPath)) {
+                               std::string originPath = bf::read_symlink(dllPath).string();
+                               std::string originNiPath = originPath.substr(0, originPath.rfind(".dll")) + ".ni.dll";
+                               if (!bf::exists(originNiPath)) {
+                                       if(createNiDll(originPath, false, false) != NI_ERROR_NONE) {
+                                               fprintf(stderr, "Failed to create NI file [%s]\n", originPath.c_str());
+                                               return NI_ERROR_UNKNOWN;
+                                       }
+                               }
+                               std::string setNiPath = dllPath.substr(0, dllPath.rfind(".dll")) + ".ni.dll";
+                               if (!bf::exists(setNiPath)) {
+                                       bf::create_symlink(originNiPath, setNiPath);
+                                       fprintf(stderr, "%s symbolic link file generated successfully.\n", setNiPath.c_str());
+                                       updateAssemblyInfo(tacDir.c_str(), setNiPath.c_str(), true);
+                               }
+                       }
+               }
+               return NI_ERROR_NONE;
+       } else {
+               return crossgen(dllPath, paths, enableR2R, 0, true);
+       }
 }
 
 void removeNiPlatform()
@@ -442,6 +561,14 @@ void removeNiPlatform()
                return;
        }
 
+#ifdef UNIQUE_DEFAULT_BASE_ADDR_SUPPORT
+       if (isFileExist(__SYSTEM_BASE_FILE)) {
+               if (remove(__SYSTEM_BASE_FILE)) {
+                       fprintf(stderr, "Failed to remove %s\n", __SYSTEM_BASE_FILE);
+               }
+       }
+#endif
+
        if (remove(coreLib.c_str())) {
                fprintf(stderr, "Failed to remove System.Private.CoreLib native image file\n");
        }
@@ -473,7 +600,6 @@ ni_error_e removeNiUnderPkgRoot(const std::string& pkgId)
 {
        std::string pkgRoot;
        if (getRootPath(pkgId, pkgRoot) < 0) {
-               fprintf(stderr, "Failed to get root path from [%s]\n", pkgId.c_str());
                return NI_ERROR_INVALID_PACKAGE;
        }
 
@@ -483,14 +609,14 @@ ni_error_e removeNiUnderPkgRoot(const std::string& pkgId)
 
        removeNiUnderDirs(paths, 2);
 
-       std::string binNIDir = binDir + APP_NI_SUB_DIR;
+       std::string binNIDir = concatPath(binDir, APP_NI_SUB_DIR);
        if (isFileExist(binNIDir)) {
                if (rmdir(binNIDir.c_str()) != 0) {
                        fprintf(stderr, "Failed to remove app ni dir [%s]\n", binNIDir.c_str());
                }
        }
 
-       std::string libNIDir = libDir + APP_NI_SUB_DIR;
+       std::string libNIDir = concatPath(libDir, APP_NI_SUB_DIR);
        if (isFileExist(libNIDir)) {
                if (rmdir(libNIDir.c_str()) != 0) {
                        fprintf(stderr, "Failed to remove app ni dir [%s]\n", libNIDir.c_str());
@@ -509,7 +635,7 @@ ni_error_e regenerateAppNI(bool enableR2R)
        if (ret != PMINFO_R_OK)
                return NI_ERROR_UNKNOWN;
 
-       ret = pkgmgrinfo_appinfo_metadata_filter_add(handle, AOT_METADATA_KEY, AOT_METADATA_VALUE);
+       ret = pkgmgrinfo_appinfo_metadata_filter_add(handle, AOT_METADATA_KEY, METADATA_VALUE);
        if (ret != PMINFO_R_OK) {
                pkgmgrinfo_appinfo_metadata_filter_destroy(handle);
                return NI_ERROR_UNKNOWN;