Modified to support crossgen2 (#330)
[platform/core/dotnet/launcher.git] / NativeLauncher / tool / ni_common.cc
1 /*
2  * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #include <pkgmgr-info.h>
18 #include <pkgmgr_installer_info.h>
19 #include <aul.h>
20 #include <tzplatform_config.h>
21
22 #include "log.h"
23 #include "utils.h"
24 #include "pkgmgr_parser_plugin_interface.h"
25
26 #include <wait.h>
27 #include <dirent.h>
28 #include <sys/stat.h>
29
30 #include <algorithm>
31 #include <string>
32 #include <fstream>
33 #include <sstream>
34
35 #include <pwd.h>
36 #include <grp.h>
37 #include <unistd.h>
38 #include <string.h>
39 #include <sqlite3.h>
40 #include <inttypes.h>
41 #include <errno.h>
42
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"
48
49 #ifdef  LOG_TAG
50 #undef  LOG_TAG
51 #endif
52 #define LOG_TAG "DOTNET_INSTALLER_PLUGIN"
53
54 #ifndef CROSSGEN_PATH
55 #error "CROSSGEN_PATH is missed"
56 #endif
57
58 #define __XSTR(x) #x
59 #define __STR(x) __XSTR(x)
60 #if defined(__arm__) || defined(__aarch64__)
61 static const char* __NATIVE_LIB_DIR = __STR(NATIVE_LIB_DIR);
62 #endif
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);
66
67 #ifdef UNIQUE_DEFAULT_BASE_ADDR_SUPPORT
68 static const char* __SYSTEM_BASE_FILE = __STR(SYSTEM_BASE_FILE);
69 #endif
70
71 #undef __STR
72 #undef __XSTR
73
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;
90
91 static int __interval = 0;
92 static PathManager* __pm = nullptr;
93
94 static bool isCoreLibPrepared(DWORD flags)
95 {
96         if (flags & NI_FLAGS_ENABLER2R) {
97                 return true;
98         }
99
100         std::string coreLibBackup = concatPath(__pm->getRuntimePath(), "System.Private.CoreLib.dll.Backup");
101         if (isFile(coreLibBackup)) {
102                 return true;
103         } else {
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");
107                 return false;
108         }
109 }
110
111 static bool hasCoreLibNI()
112 {
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'";
116
117         FILE *fp;
118         fp = popen(cmd.c_str(), "r");
119         if (fp != NULL) {
120                 char buff[1024];
121                 if (fgets(buff, sizeof(buff), fp) != NULL) {
122                         pclose(fp);
123                         return false;
124                 }
125                 pclose(fp);
126         }
127         return true;
128 }
129
130 static void waitInterval()
131 {
132         // by the recommand, ignore small value for performance.
133         if (__interval > 10000) {
134                 _SOUT("sleep %d usec", __interval);
135                 usleep(__interval);
136         }
137 }
138
139 static std::string getNIFilePath(const std::string& dllPath)
140 {
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");
144                 return "";
145         }
146         std::string fName = dllPath.substr(0, index);
147         std::string fExt = dllPath.substr(index, dllPath.length());
148
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;
152
153         return niPath;
154 }
155
156 /**
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
162  */
163 static bool createDirsAndCopyOwnerShip(std::string& target_path, const std::string& source)
164 {
165         struct stat st;
166         mode_t mode = S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH;
167
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);
171
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());
176                                         return false;
177                                 }
178                                 if (!source.empty()) {
179                                         copySmackAndOwnership(source, newPath);
180                                 }
181                         } else {
182                                 if (!S_ISDIR(st.st_mode)) {
183                                         _SERR("Fail. path is not a dir (%s)", newPath.c_str());
184                                         return false;
185                                 }
186                         }
187                 }
188                 iter = newIter;
189                 if(newIter != target_path.end()) {
190                         ++iter;
191                 }
192         }
193
194         return true;
195 }
196
197 static std::string getAppNIFilePath(const std::string& absDllPath, DWORD flags)
198 {
199         std::string niDirPath;
200         std::string prevPath;
201
202         prevPath = getBaseName(absDllPath);
203         niDirPath = concatPath(prevPath, APP_NI_SUB_DIR);
204
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());
209         }
210
211         if (!isDirectory(niDirPath)) {
212                 if (!createDirsAndCopyOwnerShip(niDirPath, prevPath)) {
213                         niDirPath = prevPath;
214                         _SERR("fail to create dir (%s)", niDirPath.c_str());
215                 }
216         }
217
218         return getNIFilePath(concatPath(niDirPath, getFileName(absDllPath)));
219 }
220
221 static bool checkNIExistence(const std::string& absDllPath)
222 {
223         std::string absNIPath = getNIFilePath(absDllPath);
224         if (absNIPath.empty()) {
225                 return false;
226         }
227
228         if (isFile(absNIPath)) {
229                 return true;
230         }
231
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()) {
236                         return true;
237                 }
238         }
239
240         return false;
241 }
242
243 static bool checkDllExistInDir(const std::string& path)
244 {
245         bool ret = false;
246         auto func = [&ret](const std::string& f_path, const std::string& f_name) {
247                 if (isManagedAssembly(f_name) || isNativeImage(f_name)) {
248                         ret = true;
249                 }
250         };
251
252         scanFilesInDirectory(path, func, 0);
253
254         return ret;
255 }
256
257 /*
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.
261  *
262  * Note: System.Private.CoreLib is excluded from the result list.
263  */
264 static ni_error_e getTargetDllList(const std::string& path, std::vector<std::string>& fileList)
265 {
266         if (!isDirectory(path)) {
267                 return NI_ERROR_INVALID_PARAMETER;
268         }
269
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));
274                         }
275                 }
276         };
277
278         scanFilesInDirectory(path, func, 0);
279
280         return NI_ERROR_NONE;
281 }
282
283 #ifdef UNIQUE_DEFAULT_BASE_ADDR_SUPPORT
284 static uintptr_t getFileSize(const std::string& path)
285 {
286         struct stat sb;
287
288         if (stat(path.c_str(), &sb) == 0) {
289                 return sb.st_size;
290         }
291
292         return 0;
293 }
294
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()
298 {
299         FILE *pFile = fopen(__SYSTEM_BASE_FILE, "r");
300         if (pFile == NULL) {
301                 _SERR("Failed to open %s", __SYSTEM_BASE_FILE);
302                 return 0;
303         }
304
305         uintptr_t addr = 0;
306         uintptr_t size = 0;
307
308         while (fscanf(pFile, "%" SCNxPTR " %" SCNuPTR "", &addr, &size) != EOF) {
309         }
310
311         fclose(pFile);
312
313         return addr + size;
314 }
315
316 // Get next base address to be used for system ni image
317 static uintptr_t getNextBaseAddr()
318 {
319         uintptr_t baseAddr = 0;
320
321         if (!isFile(__SYSTEM_BASE_FILE)) {
322                 // This is the starting address for all default base addresses
323                 baseAddr = DEFAULT_BASE_ADDR_START;
324         } else {
325                 baseAddr = getNextBaseAddrFromFile();
326
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;
330         }
331
332         return baseAddr;
333 }
334
335 // Save base address of system ni image to file
336 static void updateBaseAddrFile(const std::string& absNIPath, uintptr_t baseAddr)
337 {
338         uintptr_t niSize = getFileSize(absNIPath);
339         if (niSize == 0) {
340                 _SERR("File %s doesn't exist", absNIPath.c_str());
341                 return;
342         }
343
344         // Write new entry to the file
345         FILE *pFile = fopen(__SYSTEM_BASE_FILE, "a");
346         if (pFile == NULL) {
347                 _SERR("Failed to open %s", __SYSTEM_BASE_FILE);
348                 return;
349         }
350
351         fprintf(pFile, "%" PRIxPTR " %" PRIuPTR "\n", baseAddr, niSize);
352         fclose(pFile);
353 }
354
355 // check if dll is listed in TPA
356 static bool isTPADll(const std::string& dllPath)
357 {
358         std::string absPath = getBaseName(getAbsolutePath(dllPath));
359
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) {
363                         return true;
364                 }
365         }
366
367         return false;
368 }
369 #endif
370
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)
373 {
374         std::string absDllPath = getAbsolutePath(dllPath);
375         std::string absNIPath;
376
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;
382                 }
383         } else {
384                 absNIPath = getNIFilePath(absDllPath);
385                 if (checkNIExistence(absDllPath)) {
386                         return NI_ERROR_ALREADY_EXIST;
387                 }
388         }
389
390         if (absNIPath.empty()) {
391                 _SERR("Fail to get ni file name");
392                 return NI_ERROR_UNKNOWN;
393         }
394
395 #ifdef UNIQUE_DEFAULT_BASE_ADDR_SUPPORT
396         uintptr_t baseAddr = 0;
397
398         if (isTPADll(dllPath)) {
399                 baseAddr = getNextBaseAddr();
400         }
401 #endif
402
403         pid_t pid = fork();
404         if (pid == -1)
405                 return NI_ERROR_UNKNOWN;
406
407         if (pid > 0) {
408                 int status;
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");
420                                         } else {
421                                                 copySmackAndOwnership(absPdbPath, pdbFilePath);
422                                         }
423                                 }
424 #ifdef UNIQUE_DEFAULT_BASE_ADDR_SUPPORT
425                                 if (baseAddr != 0) {
426                                         updateBaseAddrFile(absNIPath, baseAddr);
427                                 }
428 #endif
429                                 return NI_ERROR_NONE;
430                         } else {
431                                 _SERR("Fail to create native image for %s", dllPath.c_str());
432                                 return NI_ERROR_NO_SUCH_FILE;
433                         }
434                 }
435         } else {
436                 std::string jitPath = __pm->getRuntimePath() + "/libclrjit.so";
437                 std::vector<const char*> argv = {
438                         __CROSSGEN_PATH,
439                         "/nologo",
440                         "/JITPath", jitPath.c_str()
441                 };
442
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 += ":";
450                         }
451                         platformAssembliesPaths += path;
452                 }
453                 argv.push_back(platformAssembliesPaths.c_str());
454
455                 bool enableR2R = flags & NI_FLAGS_ENABLER2R;
456                 if (!enableR2R) {
457                         argv.push_back("/FragileNonVersionable");
458                 }
459
460                 if (flags & NI_FLAGS_VERBOSE) {
461                         argv.push_back("/verbose");
462                 }
463
464                 if (flags & NI_FLAGS_INSTRUMENT) {
465                         argv.push_back("/Tuning");
466                 }
467
468 #ifdef UNIQUE_DEFAULT_BASE_ADDR_SUPPORT
469                 std::string baseAddrString;
470                 if (baseAddr != 0) {
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());
476                 }
477 #endif
478
479                 argv.push_back("/App_Paths");
480                 std::string absAppPath;
481                 if (!appPath.empty()) {
482                         absAppPath = appPath;
483                 } else {
484                         absAppPath = getBaseName(absDllPath);
485                 }
486                 argv.push_back(absAppPath.c_str());
487
488                 argv.push_back("/out");
489                 argv.push_back(absNIPath.c_str());
490
491                 argv.push_back(absDllPath.c_str());
492                 argv.push_back(nullptr);
493
494                 _SOUT("+ %s (%s)", absDllPath.c_str(), enableR2R ? "R2R" : "FNV");
495
496                 execv(__CROSSGEN_PATH, const_cast<char* const*>(argv.data()));
497                 exit(0);
498         }
499
500         return NI_ERROR_NONE;
501 }
502
503 static void makeArgs(std::vector<const char*>& args, const std::vector<std::string>& refPaths, DWORD flags)
504 {
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);
514         }
515         //args.push_back(OPT_PARALLELISM);
516         //args.push_back(OPT_PARALLELISM_COUNT);
517         args.push_back(CROSSGEN_OPT_RESILIENT);
518
519         args.push_back(CROSSGEN_OPT_OPTIMIZE);
520
521         if (flags & NI_FLAGS_INPUT_BUBBLE) {
522                 args.push_back(CROSSGEN_OPT_INPUTBUBBLE);
523                 args.push_back(CROSSGEN_OPT_COMPILE_BUBBLE_GENERICS);
524         }
525
526         if (flags & NI_FLAGS_VERBOSE) {
527                 args.push_back(CROSSGEN_OPT_VERBOSE);
528         }
529
530         if (flags & NI_FLAGS_INSTRUMENT) {
531                 args.push_back(CROSSGEN_OPT_TUNING);
532         }
533
534         REF_VECTOR.clear();
535
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");
540         }
541
542         for (const auto &path : refPaths) {
543                 if (checkDllExistInDir(path)) {
544                         REF_VECTOR.push_back("-r:" + path + "/*.dll");
545                 }
546         }
547
548         for (const auto &path : REF_VECTOR) {
549                 args.push_back(path.c_str());
550         }
551 }
552
553 static void clearArgs(std::vector<const char*>& args)
554 {
555         REF_VECTOR.clear();
556         args.clear();
557 }
558
559 static ni_error_e makePdbSymlinkForNI(std::string dllPath, std::string niPath)
560 {
561         std::string pdbPath = changeExtension(dllPath, ".dll", ".pdb");
562         try {
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);
568                         }
569                 }
570         } catch (const bf::filesystem_error& error) {
571                 _SERR("Fail to create symlink for %s", pdbPath.c_str());
572                 return NI_ERROR_UNKNOWN;
573         }
574
575         return NI_ERROR_NONE;
576 }
577
578 static ni_error_e crossgen2PipeLine(const std::vector<std::string>& dllList, const std::vector<std::string>& refPaths, DWORD flags)
579 {
580         // fork crossgen2
581         pid_t pid = fork();
582         if (pid == -1)
583                 return NI_ERROR_UNKNOWN;
584
585         if (pid > 0) {
586                 int status;
587                 waitpid(pid, &status, 0);
588                 if (WIFEXITED(status)) {
589                         for (auto& dllPath: dllList) {
590                                 std::string niPath = changeExtension(dllPath, ".dll", ".ni.dll");
591
592                                 if (!exist(niPath)) {
593                                         _SERR("Fail to create native image for %s", dllPath.c_str());
594                                         return NI_ERROR_NO_SUCH_FILE;
595                                 }
596
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);
603                                         niPath = appNIPath;
604                                 }
605                                 _SOUT("Native image %s generated successfully.", niPath.c_str());
606                         }
607                 }
608         } else {
609                 std::vector<const char*> argv;
610
611                 makeArgs(argv, refPaths, flags);
612
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());
617                 }
618
619                 argv.push_back(nullptr);
620
621                 // print cmd
622                 //for (auto &arg: argv) _SOUT("+ %s", arg);
623
624                 execv(CORERUN_CMD, const_cast<char* const*>(argv.data()));
625
626                 clearArgs(argv);
627                 exit(0);
628         }
629
630         return NI_ERROR_NONE;
631 }
632
633 static ni_error_e crossgen2NoPipeLine(const std::vector<std::string>& dllList, const std::vector<std::string>& refPaths, DWORD flags)
634 {
635         for (auto& dllPath : dllList) {
636                 std::string niPath;
637                 if (flags & NI_FLAGS_APPNI) {
638                         niPath = getAppNIFilePath(dllPath, flags);
639                 } else {
640                         niPath = getNIFilePath(dllPath);
641                 }
642
643                 // fork crossgen2
644                 pid_t pid = fork();
645                 if (pid == -1)
646                         return NI_ERROR_UNKNOWN;
647
648                 if (pid > 0) {
649                         int status;
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;
655                                 }
656
657                                 copySmackAndOwnership(dllPath, niPath);
658                                 if (flags & NI_FLAGS_APPNI) {
659                                         makePdbSymlinkForNI(dllPath, niPath);
660                                 }
661                                 _SOUT("Native image %s generated successfully.", niPath.c_str());
662                         }
663                 } else {
664                         std::vector<const char*> argv;
665                         makeArgs(argv, refPaths, flags);
666
667                         argv.push_back("-o");
668                         argv.push_back(niPath.c_str());
669
670                         argv.push_back(dllPath.c_str());
671                         _SOUT("+ %s", dllPath.c_str());
672
673                         // end param
674                         argv.push_back(nullptr);
675
676                         // print cmd
677                         //for (auto &arg: argv) _SOUT("+ %s", arg);
678
679                         execv(CORERUN_CMD, const_cast<char* const*>(argv.data()));
680
681                         clearArgs(argv);
682                         exit(0);
683                 }
684
685                 waitInterval();
686         }
687
688         return NI_ERROR_NONE;
689 }
690
691
692 static ni_error_e doAOTList(std::vector<std::string>& dllList, const std::string& refPaths, DWORD flags)
693 {
694         if (dllList.empty()) {
695                 return NI_ERROR_INVALID_PARAMETER;
696         }
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.
699
700         for (auto it = dllList.begin(); it != dllList.end(); it++) {
701                 std::string f = *it;
702                 if (!isFile(f)) {
703                         _SERR("dll file is not exist : %s", f.c_str());
704                         dllList.erase(it--);
705                 }
706                 if (!isManagedAssembly(f)) {
707                         _SERR("Input file is not a dll file : %s", f.c_str());
708                         dllList.erase(it--);
709                 }
710         }
711
712         if (!isFile(concatPath(__pm->getRuntimePath(), "crossgen"))) {
713                 _SERR("crossgen1 doesnot supported. use crossgen2 forcibily");
714                 flags |= NI_FLAGS_USE_CROSSGEN2;
715         }
716
717         if (flags & NI_FLAGS_USE_CROSSGEN2) {           // crossgen2
718                 std::vector<std::string> paths;
719                 splitPath(refPaths, paths);
720
721                 if (flags & NI_FLAGS_NO_PIPELINE) {
722                         crossgen2NoPipeLine(dllList, paths, flags);
723                 } else {
724                         crossgen2PipeLine(dllList, paths, flags);
725                 }
726         } else {                                                                        // crossgen1
727                 for (auto& dll : dllList) {
728                         crossgen(dll, refPaths, flags);
729                         waitInterval();
730                 }
731         }
732
733         return NI_ERROR_NONE;
734 }
735
736 static ni_error_e doAOTFile(const std::string& dllFile, const std::string& refPaths, DWORD flags)
737 {
738         if (!isFile(dllFile)) {
739                 _SERR("dll file is not exist : %s", dllFile.c_str());
740                 return NI_ERROR_NO_SUCH_FILE;
741         }
742
743         if (!isManagedAssembly(dllFile)) {
744                 _SERR("Failed. Input parameter is not managed dll (%s)\n", dllFile.c_str());
745                 return NI_ERROR_INVALID_PARAMETER;
746         }
747
748         std::vector<std::string> dllList;
749         dllList.push_back(getAbsolutePath(dllFile));
750         return doAOTList(dllList, refPaths, flags);
751 }
752
753 // callback function of "pkgmgrinfo_appinfo_metadata_filter_foreach"
754 static int appAotCb(pkgmgrinfo_appinfo_h handle, void *userData)
755 {
756         char *pkgId = NULL;
757         int ret = 0;
758         DWORD *pFlags = (DWORD*)userData;
759
760         ret = pkgmgrinfo_appinfo_get_pkgid(handle, &pkgId);
761         if (ret != PMINFO_R_OK) {
762                 _SERR("Failed to get pkgid");
763                 return -1;
764         }
765
766         if (removeNIUnderPkgRoot(pkgId) != NI_ERROR_NONE) {
767                 _SERR("Failed to remove previous dlls from [%s]", pkgId);
768                 return -1;
769         }
770
771         if (createNIUnderPkgRoot(pkgId, *pFlags) != NI_ERROR_NONE) {
772                 _SERR("Failed to generate NI file [%s]", pkgId);
773                 return -1;
774         } else {
775                 _SOUT("Complete make application to native image");
776         }
777
778         return 0;
779 }
780
781 static ni_error_e createCoreLibNI(DWORD flags)
782 {
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");
786
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;
792                         }
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;
796                         }
797                 } else {
798                         _SERR("Failed to create native image for %s", coreLib.c_str());
799                         return NI_ERROR_CORE_NI_FILE;
800                 }
801         }
802         return NI_ERROR_NONE;
803 }
804
805 ni_error_e initNICommon()
806 {
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);
811         if (inFile) {
812                 _SOUT("crossgen_interval.txt is found");
813                 inFile >> __interval;
814         }
815
816         if (initializePluginManager("normal")) {
817                 _SERR("Fail to initialize PluginManager");
818                 return NI_ERROR_UNKNOWN;
819         }
820
821         try {
822                 __pm = new PathManager();
823         } catch (const std::exception& e) {
824                 _SERR("Failed to create PathManager");
825                 return NI_ERROR_UNKNOWN;
826         }
827
828         char* pluginDllPaths = pluginGetDllPath();
829         if (pluginDllPaths && pluginDllPaths[0] != '\0') {
830                 __pm->addPlatformAssembliesPaths(pluginDllPaths, true);
831         }
832
833         char* pluginNativePaths = pluginGetNativeDllSearchingPath();
834         if (pluginNativePaths && pluginNativePaths[0] != '\0') {
835                 __pm->addNativeDllSearchingPaths(pluginNativePaths, true);
836         }
837
838         return NI_ERROR_NONE;
839 #else
840         _SERR("crossgen supports arm/arm64 architecture only. skip ni file generation");
841         return NI_ERROR_NOT_SUPPORTED;
842 #endif
843 }
844
845 void finalizeNICommon()
846 {
847         __interval = 0;
848
849         finalizePluginManager();
850
851         delete(__pm);
852         __pm = nullptr;
853 }
854
855 ni_error_e createNIPlatform(DWORD flags)
856 {
857         ni_error_e ret = createCoreLibNI(flags);
858         if (ret != NI_ERROR_NONE) {
859                 return ret;
860         }
861
862         return createNIUnderDirs(__pm->getRuntimePath() + ":" + __pm->getTizenFXPath(), flags);
863 }
864
865 ni_error_e createNIDll(const std::string& dllPath, DWORD flags)
866 {
867         if (dllPath.find("System.Private.CoreLib.dll") != std::string::npos) {
868                 return createCoreLibNI(flags);
869         }
870
871         if (!isCoreLibPrepared(flags)) {
872                 return NI_ERROR_CORE_NI_FILE;
873         }
874
875         return doAOTFile(dllPath, std::string(), flags);
876 }
877
878 ni_error_e createNIUnderTAC(const std::string& targetPath, const std::string& refPaths, DWORD flags)
879 {
880         ni_error_e ret;
881
882         // get managed file list from targetPath
883         std::vector<std::string> dllList;
884         ret = getTargetDllList(targetPath, dllList);
885         if (ret != NI_ERROR_NONE) {
886                 return ret;
887         }
888
889         std::vector<std::string> needNIList;
890         std::vector<std::string> niList;
891
892         for (auto &dll : dllList) {
893                 if (!checkNIExistence(dll)) {
894                         needNIList.push_back(dll);
895                 }
896                 niList.push_back(getNIFilePath(dll));
897         }
898
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) {
906                         return ret;
907                 }
908         }
909
910         for (auto &niPath : niList) {
911                 if (exist(niPath)) {
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());
918                         }
919                 }
920         }
921
922         return NI_ERROR_NONE;
923 }
924
925
926 ni_error_e createNIUnderDirs(const std::string& rootPaths, DWORD flags)
927 {
928         ni_error_e ret = NI_ERROR_NONE;
929         if (!isCoreLibPrepared(flags)) {
930                 return NI_ERROR_CORE_NI_FILE;
931         }
932
933         std::vector<std::string> fileList;
934         std::vector<std::string> paths;
935         splitPath(rootPaths, paths);
936
937         for (const auto &path : paths) {
938                 if (!exist(path)) {
939                         continue;
940                 }
941
942                 if (path.find(TAC_SYMLINK_SUB_DIR) != std::string::npos) {
943                         ret = createNIUnderTAC(path, rootPaths, flags);
944                         if (ret != NI_ERROR_NONE) {
945                                 return ret;
946                         }
947                 } else {
948                         ret = getTargetDllList(path, fileList);
949                         if (ret != NI_ERROR_NONE) {
950                                 return ret;
951                         }
952                 }
953         }
954
955         if (fileList.empty()) {
956                 return NI_ERROR_NONE;
957         }
958
959         return doAOTList(fileList, rootPaths, flags);
960 }
961
962 ni_error_e createNIUnderPkgRoot(const std::string& pkgId, DWORD flags)
963 {
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;
968         }
969
970         __pm->setAppRootPath(rootPath);
971
972         flags |= NI_FLAGS_APPNI;
973
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");
978         } else {
979                 flags &= ~NI_FLAGS_APP_UNDER_RO_AREA;
980         }
981
982         // create native image under bin and lib directory
983         // tac directory is skipped in the createNIUnderDirs.
984         return createNIUnderDirs(__pm->getAppPaths(), flags);
985 }
986
987 void removeNIPlatform()
988 {
989         std::string coreLib = concatPath(__pm->getRuntimePath(), "System.Private.CoreLib.dll");
990         std::string coreLibBackup = concatPath(__pm->getRuntimePath(), "System.Private.CoreLib.dll.Backup");
991
992         if (hasCoreLibNI()) {
993                 if (!isFile(coreLibBackup)) {
994                         return;
995                 }
996
997                 if (remove(coreLib.c_str())) {
998                         _SERR("Failed to remove System.Private.CoreLib native image file");
999                 }
1000                 if (rename(coreLibBackup.c_str(), coreLib.c_str())) {
1001                         _SERR("Failed to rename System.Private.CoreLib.Backup to origin");
1002                 }
1003         }
1004
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);
1009                 }
1010         }
1011 #endif
1012
1013         removeNIUnderDirs(__pm->getRuntimePath() + ":" + __pm->getTizenFXPath());
1014 }
1015
1016 void removeNIUnderDirs(const std::string& rootPaths)
1017 {
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());
1022                         }
1023                 }
1024         };
1025
1026         std::vector<std::string> paths;
1027         splitPath(rootPaths, paths);
1028         for (const auto &path : paths) {
1029                 scanFilesInDirectory(path, convert, -1);
1030         }
1031 }
1032
1033 ni_error_e removeNIUnderPkgRoot(const std::string& pkgId)
1034 {
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;
1039         }
1040
1041         __pm->setAppRootPath(rootPath);
1042
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);
1052                         } else {
1053                                 if (isDirectory(path)) {
1054                                         if (!removeAll(path.c_str())) {
1055                                                 _SERR("Failed to remove app ni dir [%s]", path.c_str());
1056                                         }
1057                                 }
1058                         }
1059                 }
1060         }
1061
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);
1069                 }
1070         }
1071
1072         return NI_ERROR_NONE;
1073 }
1074
1075 ni_error_e regenerateAppNI(DWORD flags)
1076 {
1077         if (!isCoreLibPrepared(flags)) {
1078                 return NI_ERROR_CORE_NI_FILE;
1079         }
1080
1081         int ret = 0;
1082         pkgmgrinfo_appinfo_metadata_filter_h handle;
1083
1084         ret = pkgmgrinfo_appinfo_metadata_filter_create(&handle);
1085         if (ret != PMINFO_R_OK)
1086                 return NI_ERROR_UNKNOWN;
1087
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;
1092         }
1093
1094         ret = pkgmgrMDFilterForeach(handle, appAotCb, &flags);
1095         if (ret != 0) {
1096                 pkgmgrinfo_appinfo_metadata_filter_destroy(handle);
1097                 return NI_ERROR_UNKNOWN;
1098         }
1099
1100         pkgmgrinfo_appinfo_metadata_filter_destroy(handle);
1101         return NI_ERROR_NONE;
1102 }
1103
1104 // callback function of "pkgmgrinfo_appinfo_metadata_filter_foreach"
1105 static int regenTacCb(pkgmgrinfo_appinfo_h handle, void *userData)
1106 {
1107         char *pkgId = NULL;
1108         DWORD *pFlags = (DWORD*)userData;
1109
1110         int ret = pkgmgrinfo_appinfo_get_pkgid(handle, &pkgId);
1111         if (ret != PMINFO_R_OK || pkgId == NULL) {
1112                 _SERR("Failed to get pkgid");
1113                 return -1;
1114         }
1115
1116         sqlite3 *tac_db = openDB(TAC_APP_LIST_DB);
1117         if (!tac_db) {
1118                 _SERR("Sqlite open error");
1119                 return -1;
1120         }
1121         sqlite3_exec(tac_db, "BEGIN;", NULL, NULL, NULL);
1122
1123         char *sql = sqlite3_mprintf("SELECT * FROM TAC WHERE PKGID = %Q;", pkgId);
1124         std::vector<std::string> nugets = selectDB(tac_db, sql);
1125         sqlite3_free(sql);
1126
1127         if (tac_db) {
1128                 closeDB(tac_db);
1129                 tac_db = NULL;
1130         }
1131
1132         std::string nugetPaths;
1133         for (const auto &nuget : nugets) {
1134                 if (!nugetPaths.empty()) {
1135                         nugetPaths += ":";
1136                 }
1137                 nugetPaths += concatPath(__DOTNET_DIR, nuget);
1138         }
1139
1140         for (auto& nuget : nugets) {
1141                 createNIUnderTAC(concatPath(__DOTNET_DIR, nuget), nugetPaths, *pFlags);
1142         }
1143
1144         return 0;
1145 }
1146
1147 ni_error_e regenerateTACNI(DWORD flags)
1148 {
1149         if (!isCoreLibPrepared(flags)) {
1150                 return NI_ERROR_CORE_NI_FILE;
1151         }
1152
1153         removeNIUnderDirs(__DOTNET_DIR);
1154
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;
1159         }
1160
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;
1165         }
1166
1167         ret = pkgmgrMDFilterForeach(handle, regenTacCb, &flags);
1168         if (ret != 0) {
1169                 pkgmgrinfo_appinfo_metadata_filter_destroy(handle);
1170                 return NI_ERROR_UNKNOWN;
1171         }
1172
1173         pkgmgrinfo_appinfo_metadata_filter_destroy(handle);
1174
1175         return NI_ERROR_NONE;
1176 }
1177
1178 static std::vector<uid_t> getUserIds()
1179 {
1180         std::vector<uid_t> list;
1181
1182         while (true) {
1183                 errno = 0; // so we can distinguish errors from no more entries
1184                 passwd* entry = getpwent();
1185                 if (!entry) {
1186                         if (errno) {
1187                                 _SERR("Error while getting userIDs");
1188                                 list.clear();
1189                                 return list;
1190                         }
1191                         break;
1192                 }
1193                 list.push_back(entry->pw_uid);
1194         }
1195         endpwent();
1196
1197         return list;
1198 }
1199
1200 static std::string getAppDataPath(const std::string& pkgId, uid_t uid)
1201 {
1202         std::string pDataFile;
1203
1204         tzplatform_set_user(uid);
1205
1206         const char* tzUserApp = tzplatform_getenv(TZ_USER_APP);
1207         if (tzUserApp != NULL) {
1208                 pDataFile = std::string(tzUserApp) + "/" + pkgId + "/data/";
1209         }
1210
1211         tzplatform_reset_user();
1212
1213         return pDataFile;
1214 }
1215
1216 ni_error_e removeAppProfileData(const std::string& pkgId)
1217 {
1218         if (pkgId.empty()) {
1219                 return NI_ERROR_INVALID_PARAMETER;
1220         }
1221
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;
1228
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;
1233                                 }
1234                                 _SOUT("Profile data (%s) is removed successfully", pDataFile.c_str());
1235                         }
1236                 }
1237         }
1238
1239         return NI_ERROR_NONE;
1240 }
1241
1242 static int appTypeListCb(pkgmgrinfo_appinfo_h handle, void *user_data)
1243 {
1244         char *pkgId = NULL;
1245         int ret = pkgmgrinfo_appinfo_get_pkgid(handle, &pkgId);
1246         if (ret != PMINFO_R_OK || pkgId == NULL) {
1247                 _SERR("Fail to get pkgid");
1248                 return 0;
1249         }
1250
1251         if (removeAppProfileData(pkgId) != NI_ERROR_NONE) {
1252                 _SERR("Fail to remove profile data for (%s)", pkgId);
1253         }
1254
1255         return 0;
1256 }
1257
1258 static ni_error_e removeAppProfileByAppType(const char* type)
1259 {
1260         int ret;
1261
1262         pkgmgrinfo_appinfo_filter_h filter;
1263
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;
1268         }
1269
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;
1275         }
1276
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;
1282         }
1283
1284         pkgmgrinfo_appinfo_filter_destroy(filter);
1285
1286         return NI_ERROR_NONE;
1287 }
1288
1289 void removeAllAppProfileData()
1290 {
1291         std::vector<const char*> appTypeList = {"dotnet", "dotnet-nui", "dotnet-inhouse"};
1292
1293         for (auto& type : appTypeList) {
1294                 if (removeAppProfileByAppType(type) != NI_ERROR_NONE) {
1295                         _SERR("Fail to removeAppProfileByAppType for type (%s)", type);
1296                 }
1297         }
1298 }