Check the native image with ildasm when using --ni* option of dotnettool (#167)
author최종헌/Common Platform Lab(SR)/Engineer/삼성전자 <j-h.choi@samsung.com>
Thu, 18 Jun 2020 04:10:33 +0000 (13:10 +0900)
committer이형주/Common Platform Lab(SR)/Staff Engineer/삼성전자 <leee.lee@samsung.com>
Thu, 18 Jun 2020 04:10:33 +0000 (13:10 +0900)
Change-Id: I351c1c1978c6daa24a7cb2378ed3206df43c11eb

NativeLauncher/tool/ni_common.cc

index 970bbb2..52ea3ef 100644 (file)
@@ -404,13 +404,37 @@ static bool isCoreLibPrepared(DWORD flags)
        }
 }
 
+static bool hasCoreLibNI()
+{
+       FILE *fp;
+       char buff[1024];
+       std::string ildasm = concatPath(getRuntimeDir(), "ildasm");
+       std::string coreLib = concatPath(getRuntimeDir(), "System.Private.CoreLib.dll");
+       std::string cmd = ildasm + " " + coreLib + " | grep '\\.corflags'";
+       fp = popen(cmd.c_str(), "r");
+       if (fp != NULL) {
+               while (fgets(buff, sizeof(buff), fp) != NULL) {
+                       buff[strlen(buff) - 1] = '\0';
+               }
+               std::string corflag = replaceAll(buff, ".corflags", "");
+               corflag.erase(std::remove(corflag.begin(), corflag.end(), ' '), corflag.end());
+               // CorFlags.ILLibrary=0x00000004 (.ni.dll)
+               if (!strcmp(corflag.substr(0, 10).c_str(), "0x00000004")) {
+                       pclose(fp);
+                       return true;
+               }
+               pclose(fp);
+       }
+       return false;
+}
+
 static ni_error_e createCoreLibNI(DWORD flags)
 {
        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 (!isFile(coreLibBackup)) {
+       if (!isFile(coreLibBackup) && !hasCoreLibNI()) {
                if (!crossgen(coreLib, std::string(), flags)) {
                        if (rename(coreLib.c_str(), coreLibBackup.c_str())) {
                                fprintf(stderr, "Failed to rename System.Private.CoreLib.dll\n");
@@ -690,8 +714,17 @@ void removeNIPlatform()
        std::string coreLib = concatPath(getRuntimeDir(), "System.Private.CoreLib.dll");
        std::string coreLibBackup = concatPath(getRuntimeDir(), "System.Private.CoreLib.dll.Backup");
 
-       if (!isFile(coreLibBackup)) {
-               return;
+       if (hasCoreLibNI()) {
+               if (!isFile(coreLibBackup)) {
+                       return;
+               }
+
+               if (remove(coreLib.c_str())) {
+                       fprintf(stderr, "Failed to remove System.Private.CoreLib native image file\n");
+               }
+               if (rename(coreLibBackup.c_str(), coreLib.c_str())) {
+                       fprintf(stderr, "Failed to rename System.Private.CoreLib.Backup to origin\n");
+               }
        }
 
 #ifdef UNIQUE_DEFAULT_BASE_ADDR_SUPPORT
@@ -702,14 +735,6 @@ void removeNIPlatform()
        }
 #endif
 
-       if (remove(coreLib.c_str())) {
-               fprintf(stderr, "Failed to remove System.Private.CoreLib native image file\n");
-       }
-
-       if (rename(coreLibBackup.c_str(), coreLib.c_str())) {
-               fprintf(stderr, "Failed to rename System.Private.CoreLib.Backup to origin\n");
-       }
-
        const std::string platformDirs[] = {getRuntimeDir(), getTizenFXDir()};
 
        removeNIUnderDirs(platformDirs, 2);