Add --skip-ro-app option
[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 #include "r2r_checker.h"
49
50 #ifdef  LOG_TAG
51 #undef  LOG_TAG
52 #endif
53 #define LOG_TAG "DOTNET_INSTALLER_PLUGIN"
54
55 #define __XSTR(x) #x
56 #define __STR(x) __XSTR(x)
57 #if defined(__arm__) || defined(__aarch64__)
58 static const char* __NATIVE_LIB_DIR = __STR(NATIVE_LIB_DIR);
59 #endif
60 static const char* __DOTNET_DIR = __STR(DOTNET_DIR);
61 static const char* __READ_ONLY_APP_UPDATE_DIR = __STR(READ_ONLY_APP_UPDATE_DIR);
62
63 #ifdef UNIQUE_DEFAULT_BASE_ADDR_SUPPORT
64 static const char* __SYSTEM_BASE_FILE = __STR(SYSTEM_BASE_FILE);
65 #endif
66
67 #undef __STR
68 #undef __XSTR
69
70 static std::string CORERUN_CMD = "/usr/share/dotnet.tizen/netcoreapp/corerun";
71 static std::string CROSSGEN2_PATH = "/usr/share/dotnet.tizen/netcoreapp/crossgen2/crossgen2.dll";
72 static std::string CLRJIT_PATH = "/usr/share/dotnet.tizen/netcoreapp/libclrjit.so";
73 static const char* CROSSGEN_OPT_JITPATH = "--jitpath";
74 static const char* CROSSGEN_OPT_TARGET_ARCH = "--targetarch";
75 static const char* CROSSGEN_OPT_OUT_NEAR_INPUT = "--out-near-input";
76 static const char* CROSSGEN_OPT_SINGLE_FILE_COMPILATION = "--single-file-compilation";
77 //static const char* CROSSGEN_OPT_PARALLELISM = "--parallelism";
78 //static const char* CROSSGEN_OPT_PARALLELISM_COUNT = "5";
79 static const char* CROSSGEN_OPT_RESILIENT = "--resilient";
80 static const char* CROSSGEN_OPT_OPTIMIZE = "-O";
81 static const char* CROSSGEN_OPT_INPUTBUBBLE = "--inputbubble";
82 static const char* CROSSGEN_OPT_COMPILE_BUBBLE_GENERICS = "--compilebubblegenerics";
83 static const char* CROSSGEN_OPT_VERBOSE = "--verbose";
84 static std::vector<std::string> REF_VECTOR;
85 static std::vector<std::string> INPUTBUBBLE_REF_VECTOR;
86 static std::vector<std::string> MIBC_VECTOR;
87
88 static int __interval = 0;
89 static PathManager* __pm = nullptr;
90
91 static NIOption* __ni_option = nullptr;
92
93 // singleton
94 NIOption* getNIOption()
95 {
96         if (__ni_option == nullptr) {
97                 __ni_option = (NIOption*)calloc(sizeof(NIOption), 1);
98                 if (__ni_option == nullptr) {
99                         _SERR("Fail to create NIOption");
100                 }
101         }
102         return __ni_option;
103 }
104
105 static void waitInterval()
106 {
107         // by the recommand, ignore small value for performance.
108         if (__interval > 10000) {
109                 _SOUT("sleep %d usec", __interval);
110                 usleep(__interval);
111         }
112 }
113
114 /**
115  * @brief create the directory including parents directory, and
116  *        copy ownership and smack labels to the created directory.
117  * @param[in] target directory path
118  * @param[in] source directory path to get ownership and smack label
119  * @return if directory created successfully, return true otherwise false
120  */
121 static bool createDirsAndCopyOwnerShip(std::string& target_path, const std::string& source)
122 {
123         struct stat st;
124         mode_t mode = S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH;
125
126         for (std::string::iterator iter = target_path.begin(); iter != target_path.end();) {
127                 std::string::iterator newIter = std::find(iter, target_path.end(), '/');
128                 std::string newPath = std::string(target_path.begin(), newIter);
129
130                 if (!newPath.empty()) {
131                         if (stat(newPath.c_str(), &st) != 0) {
132                                 if (mkdir(newPath.c_str(), mode) != 0 && errno != EEXIST) {
133                                         _SERR("Fail to create app ni directory (%s)", newPath.c_str());
134                                         return false;
135                                 }
136                                 if (!source.empty()) {
137                                         copySmackAndOwnership(source, newPath);
138                                 }
139                         } else {
140                                 if (!S_ISDIR(st.st_mode)) {
141                                         _SERR("Fail. path is not a dir (%s)", newPath.c_str());
142                                         return false;
143                                 }
144                         }
145                 }
146                 iter = newIter;
147                 if(newIter != target_path.end()) {
148                         ++iter;
149                 }
150         }
151
152         return true;
153 }
154
155 static std::string getNIFilePath(const std::string& dllPath)
156 {
157         size_t index = dllPath.find_last_of(".");
158         if (index == std::string::npos) {
159                 _SERR("File doesnot contain extension. fail to get NI file name");
160                 return "";
161         }
162         std::string fName = dllPath.substr(0, index);
163         std::string fExt = dllPath.substr(index, dllPath.length());
164
165         // crossgen generate file with lower case extension only
166         std::transform(fExt.begin(), fExt.end(), fExt.begin(), ::tolower);
167         std::string niPath = fName + ".ni" + fExt;
168
169         return niPath;
170 }
171
172 static std::string getAppNIFilePath(const std::string& absDllPath, NIOption* opt)
173 {
174         std::string niDirPath;
175         std::string prevPath;
176
177         prevPath = getBaseName(absDllPath);
178         niDirPath = concatPath(prevPath, APP_NI_SUB_DIR);
179
180         if (opt->flags & NI_FLAGS_APP_UNDER_RO_AREA) {
181                 niDirPath = replaceAll(niDirPath, getBaseName(__pm->getAppRootPath()), __READ_ONLY_APP_UPDATE_DIR);
182                 _SERR("App is installed in RO area. Change NI path to RW area(%s).", niDirPath.c_str());
183                 _ERR("App is installed in RO area. Change NI path to RW area(%s).", niDirPath.c_str());
184         }
185
186         if (!isDirectory(niDirPath)) {
187                 if (!createDirsAndCopyOwnerShip(niDirPath, prevPath)) {
188                         niDirPath = prevPath;
189                         _SERR("fail to create dir (%s)", niDirPath.c_str());
190                 }
191         }
192
193         return getNIFilePath(concatPath(niDirPath, getFileName(absDllPath)));
194 }
195
196 static bool checkNIExistence(const std::string& absDllPath)
197 {
198         std::string absNIPath = getNIFilePath(absDllPath);
199         if (absNIPath.empty()) {
200                 return false;
201         }
202
203         if (isFile(absNIPath)) {
204                 return true;
205         }
206
207         // native image of System.Private.CoreLib.dll should have to overwrite
208         // original file to support new coreclr
209         if (absDllPath.find("System.Private.CoreLib.dll") != std::string::npos) {
210                 return isR2RImage(absDllPath);
211         }
212
213         return false;
214 }
215
216 static bool checkAppNIExistence(const std::string& absDllPath, NIOption* opt)
217 {
218         std::string absNIPath = getAppNIFilePath(absDllPath, opt);
219         if (absNIPath.empty()) {
220                 return false;
221         }
222
223         if (isFile(absNIPath)) {
224                 return true;
225         }
226
227         return false;
228 }
229
230 static bool checkDllExistInDir(const std::string& path)
231 {
232         bool ret = false;
233         auto func = [&ret](const std::string& f_path, const std::string& f_name) {
234                 if (isManagedAssembly(f_name) || isNativeImage(f_name)) {
235                         ret = true;
236                 }
237         };
238
239         scanFilesInDirectory(path, func, 0);
240
241         return ret;
242 }
243
244 /*
245  * Get the list of managed files in the specific directory
246  * Absolute paths of managed files are stored at the result list.
247  * If native image already exist in the same directory, managed file is ignored.
248  */
249 static ni_error_e getTargetDllList(const std::string& path, std::vector<std::string>& fileList)
250 {
251         if (!isDirectory(path)) {
252                 return NI_ERROR_INVALID_PARAMETER;
253         }
254
255         auto func = [&fileList](const std::string& f_path, const std::string& f_name) {
256                 if (isManagedAssembly(f_path) && !checkNIExistence(f_path)) {
257                         fileList.push_back(getAbsolutePath(f_path));
258                 }
259         };
260
261         scanFilesInDirectory(path, func, 0);
262
263         return NI_ERROR_NONE;
264 }
265
266 /*
267  * Get the list of managed files in the specific directory of Application
268  * Absolute paths of managed files are stored at the result list.
269  * If native image already exist in the .native_image directory, managed file is ignored.
270  *
271  */
272 static ni_error_e getAppTargetDllList(const std::string& path, std::vector<std::string>& fileList, NIOption *opt)
273 {
274         if (!isDirectory(path)) {
275                 return NI_ERROR_INVALID_PARAMETER;
276         }
277
278         auto func = [&fileList, opt](const std::string& f_path, const std::string& f_name) {
279                 if (isManagedAssembly(f_path) && !checkAppNIExistence(f_path, opt)) {
280                         fileList.push_back(getAbsolutePath(f_path));
281                 }
282         };
283
284         scanFilesInDirectory(path, func, 0);
285
286         return NI_ERROR_NONE;
287 }
288
289 static void makeArgs(std::vector<const char*>& args, const std::vector<std::string>& refPaths, NIOption* opt)
290 {
291         args.push_back(CORERUN_CMD.c_str());
292         if (CROSSGEN2_PATH != "") {
293                 args.push_back(CROSSGEN2_PATH.c_str());
294         }
295         args.push_back(CROSSGEN_OPT_JITPATH);
296         args.push_back(CLRJIT_PATH.c_str());
297         args.push_back(CROSSGEN_OPT_TARGET_ARCH);
298         args.push_back(ARCHITECTURE_IDENTIFIER);
299         if (!(opt->flags & NI_FLAGS_NO_PIPELINE)) {
300                 args.push_back(CROSSGEN_OPT_OUT_NEAR_INPUT);
301                 args.push_back(CROSSGEN_OPT_SINGLE_FILE_COMPILATION);
302         }
303         //args.push_back(OPT_PARALLELISM);
304         //args.push_back(OPT_PARALLELISM_COUNT);
305         args.push_back(CROSSGEN_OPT_RESILIENT);
306
307         args.push_back(CROSSGEN_OPT_OPTIMIZE);
308
309         if (opt->flags & NI_FLAGS_INPUT_BUBBLE) {
310                 args.push_back(CROSSGEN_OPT_INPUTBUBBLE);
311                 args.push_back(CROSSGEN_OPT_COMPILE_BUBBLE_GENERICS);
312
313                 if (opt->flags & NI_FLAGS_INPUT_BUBBLE_REF) {
314                         INPUTBUBBLE_REF_VECTOR.clear();
315                         // check inputbubbleref format.
316                         for (const auto &path : opt->inputBubbleRefPath) {
317                                 if (checkDllExistInDir(path)) {
318                                         INPUTBUBBLE_REF_VECTOR.push_back("--inputbubbleref:" + path + "/*.dll");
319                                 }
320                         }
321                         // add ref path to inputbubble ref
322                         for (const auto &path : refPaths) {
323                                 if (checkDllExistInDir(path)) {
324                                         INPUTBUBBLE_REF_VECTOR.push_back("--inputbubbleref:" + path + "/*.dll");
325                                 }
326                         }
327                         for (const auto &path : INPUTBUBBLE_REF_VECTOR) {
328                                 args.push_back(path.c_str());
329                         }
330                 }
331         }
332
333         if (opt->flags & NI_FLAGS_MIBC) {
334                 MIBC_VECTOR.clear();
335                 for (const auto &path : opt->mibcPath) {
336                         MIBC_VECTOR.push_back("--mibc:" + path);
337                 }
338                 for (const auto &path : MIBC_VECTOR) {
339                         args.push_back(path.c_str());
340                 }
341         }
342
343         if (opt->flags & NI_FLAGS_VERBOSE) {
344                 args.push_back(CROSSGEN_OPT_VERBOSE);
345         }
346
347         REF_VECTOR.clear();
348
349         // set reference path
350         if (opt->flags & NI_FLAGS_REF) {
351                 for (const auto &path : opt->refPath) {
352                         REF_VECTOR.push_back("-r:" + path + "/*.dll");
353                 }
354         } else {
355                 std::vector<std::string> paths = __pm->getPlatformAssembliesPaths();
356                 for (const auto &path : paths) {
357                         if (checkDllExistInDir(path)) {
358                                 REF_VECTOR.push_back("-r:" + path + "/*.dll");
359                         }
360                 }
361         }
362
363         if (opt->flags & NI_FLAGS_EXTRA_REF) {
364                 for (const auto &erPath : opt->extraRefPath) {
365                         std::string path = getAbsolutePath(erPath);
366                         if (checkDllExistInDir(path)) {
367                                 REF_VECTOR.push_back("-r:" + path + "/*.dll");
368                         }
369                 }
370         }
371
372         for (const auto &path : refPaths) {
373                 if (checkDllExistInDir(path)) {
374                         REF_VECTOR.push_back("-r:" + path + "/*.dll");
375                 }
376         }
377
378         for (const auto &path : REF_VECTOR) {
379                 args.push_back(path.c_str());
380         }
381 }
382
383 static void clearArgs(std::vector<const char*>& args)
384 {
385         REF_VECTOR.clear();
386         args.clear();
387 }
388
389 static ni_error_e makePdbSymlinkForNI(std::string dllPath, std::string niPath)
390 {
391         std::string pdbPath = changeExtension(dllPath, ".dll", ".pdb");
392         try {
393                 if (exist(pdbPath)) {
394                         std::string targetPDBPath = changeExtension(niPath, ".ni.dll", ".pdb");
395                         if (!exist(targetPDBPath)) {
396                                 bf::create_symlink(pdbPath, targetPDBPath);
397                                 copySmackAndOwnership(pdbPath, targetPDBPath, true);
398                         }
399                 }
400         } catch (const bf::filesystem_error& error) {
401                 _SERR("Fail to create symlink for %s", pdbPath.c_str());
402                 return NI_ERROR_UNKNOWN;
403         }
404
405         return NI_ERROR_NONE;
406 }
407
408 static ni_error_e crossgen2PostAction(const std::string& dllPath, const std::string& niPath, NIOption* opt) {
409         if (!exist(niPath)) {
410                 removeFile(changeExtension(niPath, ".ni.dll", ".ni.dll.tmp"));
411                 _SERR("Fail to create native image for %s", dllPath.c_str());
412                 return NI_ERROR_NO_SUCH_FILE;
413         }
414         copySmackAndOwnership(dllPath, niPath);
415         // if AppNI then move ni.dll file to .native_image and copy pdb to .native_image
416         if (opt->flags & NI_FLAGS_APPNI) {
417                 std::string appNIPath = getAppNIFilePath(dllPath, opt);
418                 moveFile(niPath, appNIPath);
419                 makePdbSymlinkForNI(dllPath, appNIPath);
420                 _SOUT("Native image %s generated successfully.", appNIPath.c_str());
421         } else {
422                 _SOUT("Native image %s generated successfully.", niPath.c_str());
423         }
424         return NI_ERROR_NONE;
425 }
426
427 static ni_error_e crossgen2PipeLine(const std::vector<std::string>& dllList, const std::vector<std::string>& refPaths, NIOption* opt)
428 {
429         // fork crossgen2
430         pid_t pid = fork();
431         if (pid == -1)
432                 return NI_ERROR_UNKNOWN;
433
434         if (pid > 0) {
435                 int status;
436                 waitpid(pid, &status, 0);
437                 if (WIFEXITED(status)) {
438                         for (auto& dllPath: dllList) {
439                                 ni_error_e ret = crossgen2PostAction(dllPath, changeExtension(dllPath, ".dll", ".ni.dll"), opt);
440                                 if (ret != NI_ERROR_NONE) {
441                                         return ret;
442                                 }
443                         }
444                 } else {
445                         _SERR("Failed. Forked process terminated abnormally");
446                         return NI_ERROR_ABNORMAL_PROCESS_TERMINATION;
447                 }
448         } else {
449                 std::vector<const char*> argv;
450                 makeArgs(argv, refPaths, opt);
451
452                 // add input files at the end of parameter
453                 for (const auto &input : dllList) {
454                         argv.push_back(input.c_str());
455                         _SOUT("+ %s", input.c_str());
456                 }
457
458                 // end param
459                 argv.push_back(nullptr);
460
461                 // print cmd
462                 if (opt->flags & NI_FLAGS_PRINT_CMD) {
463                         _SOUT("==================== NI Commands =========================");
464                         for (auto &arg: argv) _SOUT("+ %s", arg);
465                 }
466
467                 execv(CORERUN_CMD.c_str(), const_cast<char* const*>(argv.data()));
468
469                 clearArgs(argv);
470                 exit(0);
471         }
472
473         return NI_ERROR_NONE;
474 }
475
476 static ni_error_e crossgen2NoPipeLine(const std::vector<std::string>& dllList, const std::vector<std::string>& refPaths, NIOption* opt)
477 {
478         for (auto& dllPath : dllList) {
479                 std::string niPath;
480                 if (opt->flags & NI_FLAGS_APPNI) {
481                         niPath = getAppNIFilePath(dllPath, opt);
482                 } else {
483                         niPath = getNIFilePath(dllPath);
484                 }
485
486                 // fork crossgen2
487                 pid_t pid = fork();
488                 if (pid == -1)
489                         return NI_ERROR_UNKNOWN;
490
491                 if (pid > 0) {
492                         int status;
493                         waitpid(pid, &status, 0);
494                         if (WIFEXITED(status)) {
495                                 ni_error_e ret = crossgen2PostAction(dllPath, niPath, opt);
496                                 if (ret != NI_ERROR_NONE) {
497                                         return ret;
498                                 }
499                         } else {
500                                 _SERR("Failed. Forked process terminated abnormally");
501                                 _SERR("Crossgen2 was terminated by the OOM killer. Please check the system.");
502                                 removeFile(changeExtension(niPath, ".ni.dll", ".ni.dll.tmp"));
503                                 return NI_ERROR_ABNORMAL_PROCESS_TERMINATION;
504                         }
505                 } else {
506                         std::vector<const char*> argv;
507                         makeArgs(argv, refPaths, opt);
508
509                         argv.push_back("-o");
510                         argv.push_back(niPath.c_str());
511
512                         argv.push_back(dllPath.c_str());
513                         _SOUT("+ %s", dllPath.c_str());
514
515                         // end param
516                         argv.push_back(nullptr);
517
518                         // print cmd
519                         if (opt->flags & NI_FLAGS_PRINT_CMD) {
520                                 _SOUT("==================== NI Commands =========================");
521                                 for (auto &arg: argv) _SOUT("+ %s", arg);
522                         }
523
524                         execv(CORERUN_CMD.c_str(), const_cast<char* const*>(argv.data()));
525
526                         clearArgs(argv);
527                         exit(0);
528                 }
529
530                 waitInterval();
531         }
532
533         return NI_ERROR_NONE;
534 }
535
536 static ni_error_e createCoreLibNI(NIOption* opt)
537 {
538         std::string coreLib = concatPath(__pm->getRuntimePath(), "System.Private.CoreLib.dll");
539         std::string niCoreLib = concatPath(__pm->getRuntimePath(), "System.Private.CoreLib.ni.dll");
540         std::string coreLibBackup = concatPath(__pm->getRuntimePath(), "System.Private.CoreLib.dll.Backup");
541
542         std::vector<std::string> dllList;
543         std::vector<std::string> refPaths;
544         dllList.push_back(getAbsolutePath(coreLib));
545
546         if (!isFile(coreLibBackup) && !isR2RImage(coreLib)) {
547                 if (crossgen2NoPipeLine(dllList, refPaths, opt) == NI_ERROR_NONE && exist(niCoreLib)) {
548                         if (rename(coreLib.c_str(), coreLibBackup.c_str())) {
549                                 _SERR("Failed to rename System.Private.CoreLib.dll");
550                                 return NI_ERROR_CORE_NI_FILE;
551                         }
552                         if (rename(niCoreLib.c_str(), coreLib.c_str())) {
553                                 _SERR("Failed to rename System.Private.CoreLib.ni.dll");
554                                 return NI_ERROR_CORE_NI_FILE;
555                         }
556                 } else {
557                         _SERR("Failed to create native image for %s", coreLib.c_str());
558                         return NI_ERROR_CORE_NI_FILE;
559                 }
560         }
561         return NI_ERROR_NONE;
562 }
563
564 static ni_error_e doAOTList(std::vector<std::string>& dllList, const std::string& refPaths, NIOption* opt)
565 {
566         ni_error_e ret = NI_ERROR_NONE;
567
568         if (dllList.empty()) {
569                 return NI_ERROR_INVALID_PARAMETER;
570         }
571         // When performing AOT for one Dll, an error is returned when an error occurs.
572         // However, when processing multiple dlls at once, only the log for errors is output and skipped.
573
574         std::string coreLib = concatPath(__pm->getRuntimePath(), "System.Private.CoreLib.dll");
575         bool hasSPC = false;
576
577         std::vector<std::string> niList;
578         for (auto it = dllList.begin(); it != dllList.end(); it++) {
579                 std::string f = *it;
580                 if (!isFile(f)) {
581                         _SERR("dll file is not exist : %s", f.c_str());
582                         dllList.erase(it--);
583                 }
584                 if (!isManagedAssembly(f)) {
585                         _SERR("Input file is not a dll file : %s", f.c_str());
586                         dllList.erase(it--);
587                 }
588                 // handle System.Private.CoreLib.dll separately.
589                 // dllList and path manager contain absolute path. So, there is no need to change path to absolute path
590                 if (f == coreLib) {
591                         hasSPC = true;
592                         dllList.erase(it--);
593                 } else {
594                         niList.push_back(changeExtension(f, ".dll", ".ni.dll"));
595                 }
596         }
597
598         // In the case of SPC, post-processing is required to change the name of the native image.
599         // In order to avoid repeatedly checking whether the generated native image is an SPC,
600         // the SPC native image generation is performed separately.
601         if (hasSPC) {
602                 ret = createCoreLibNI(opt);
603                 if (ret != NI_ERROR_NONE) {
604                         return ret;
605                 }
606         }
607
608         // if there is no proper input after processing dll list
609         if (dllList.empty()) {
610                 if (hasSPC) {
611                         return ret;
612                 } else {
613                         return NI_ERROR_INVALID_PARAMETER;
614                 }
615         }
616
617         std::vector<std::string> paths;
618         splitPath(refPaths, paths);
619
620         if (opt->flags & NI_FLAGS_NO_PIPELINE) {
621                 ret = crossgen2NoPipeLine(dllList, paths, opt);
622         } else {
623                 // When the forked process in the pipeline state is terminated(WIFSIGNALED(status)),
624                 // retry the generation of the native image
625                 // if the number of .dll files and the number of .ni.dll files are different.
626                 for (int callCnt = 0; callCnt < 2; callCnt++) {
627                         // If an error occurs, perform it twice with the same option.
628                         ret = crossgen2PipeLine(dllList, paths, opt);
629                         if (ret != NI_ERROR_NONE) {
630                                 _SERR("Crossgen2 is abnormally terminated. Regenerate native images that failed while running crossgen2.");
631                                 dllList.clear();
632                                 for (auto it = niList.begin(); it != niList.end(); it++) {
633                                         std::string niPath = *it;
634                                         std::string dllPath = changeExtension(niPath, ".ni.dll", ".dll");
635                                         if (crossgen2PostAction(dllPath, niPath, opt) != NI_ERROR_NONE) {
636                                                 dllList.push_back(dllPath);
637                                         } else {
638                                                 niList.erase(it--);
639                                         }
640                                 }
641                         } else {
642                                 break;
643                         }
644                 }
645                 // If an error occurs after two crossgen2PipeLine() attempts,
646                 // try crossgen2NoPipeLine() for the last time.
647                 if (ret != NI_ERROR_NONE) {
648                         _SERR("Retry running crossgen2 with --no-pipeline mode to avoid termination by OOM.");
649                         ret = crossgen2NoPipeLine(dllList, paths, opt);
650                 }
651         }
652
653         return ret;
654 }
655
656 static ni_error_e doAOTFile(const std::string& dllFile, const std::string& refPaths, NIOption* opt)
657 {
658         if (!isFile(dllFile)) {
659                 _SERR("dll file is not exist : %s", dllFile.c_str());
660                 return NI_ERROR_NO_SUCH_FILE;
661         }
662
663         if (!isManagedAssembly(dllFile)) {
664                 _SERR("Failed. Input parameter is not managed dll (%s)\n", dllFile.c_str());
665                 return NI_ERROR_INVALID_PARAMETER;
666         }
667
668         if (checkNIExistence(dllFile)) {
669                 _SERR("Native image file is already exist : %s", dllFile.c_str());
670                 return NI_ERROR_ALREADY_EXIST;
671         }
672
673         std::vector<std::string> dllList;
674         dllList.push_back(getAbsolutePath(dllFile));
675         return doAOTList(dllList, refPaths, opt);
676 }
677
678 // callback function of "pkgmgrinfo_appinfo_metadata_filter_foreach"
679 static int appAotCb(pkgmgrinfo_appinfo_h handle, void *userData)
680 {
681         char *pkgId = NULL;
682         int ret = 0;
683         NIOption **pOptions = (NIOption**)userData;
684
685         if ((*pOptions)->flags & NI_FLAGS_SKIP_RO_APP) {
686                 bool isSystem = false;
687                 int ret = pkgmgrinfo_appinfo_is_system(handle, &isSystem);
688                 if (ret != PMINFO_R_OK) {
689                         _SERR("Failed to check that app is System or not\n");
690                         return -1;
691                 }
692                 if (isSystem) {
693                         return 0;
694                 }
695         }
696
697         ret = pkgmgrinfo_appinfo_get_pkgid(handle, &pkgId);
698         if (ret != PMINFO_R_OK) {
699                 _SERR("Failed to get pkgid");
700                 return -1;
701         }
702
703         if (removeNIUnderPkgRoot(pkgId) != NI_ERROR_NONE) {
704                 _SERR("Failed to remove previous dlls from [%s]", pkgId);
705                 return -1;
706         }
707
708         if (createNIUnderPkgRoot(pkgId, *pOptions) != NI_ERROR_NONE) {
709                 _SERR("Failed to generate NI file [%s]", pkgId);
710                 return -1;
711         } else {
712                 _SOUT("Complete make application to native image");
713         }
714
715         return 0;
716 }
717
718 ni_error_e initNICommon()
719 {
720 #if defined(__arm__) || defined(__aarch64__)
721
722         char *env = nullptr;
723         env = getenv("MIC_CROSSGEN2_ENABLED");
724         if (env != nullptr && !strcmp(env, "1")) {
725                 CORERUN_CMD = std::string("/opt/usr/dotnet/mic/crossgen2");
726                 CROSSGEN2_PATH = "";
727                 CLRJIT_PATH = std::string("/opt/usr/dotnet/mic/libclrjit_unix_") + ARCHITECTURE_IDENTIFIER + std::string("_x64.so");
728         }
729
730         // get interval value
731         const static std::string intervalFile = concatPath(__NATIVE_LIB_DIR, "crossgen_interval.txt");
732         std::ifstream inFile(intervalFile);
733         if (inFile) {
734                 _SOUT("crossgen_interval.txt is found");
735                 inFile >> __interval;
736         }
737
738         if (initializePluginManager("normal")) {
739                 _SERR("Fail to initialize PluginManager");
740                 return NI_ERROR_UNKNOWN;
741         }
742
743         try {
744                 __pm = new PathManager();
745         } catch (const std::exception& e) {
746                 _SERR("Failed to create PathManager");
747                 return NI_ERROR_UNKNOWN;
748         }
749
750         char* pluginDllPaths = pluginGetDllPath();
751         if (pluginDllPaths && pluginDllPaths[0] != '\0') {
752                 __pm->addPlatformAssembliesPaths(pluginDllPaths, true);
753         }
754
755         char* pluginNativePaths = pluginGetNativeDllSearchingPath();
756         if (pluginNativePaths && pluginNativePaths[0] != '\0') {
757                 __pm->addNativeDllSearchingPaths(pluginNativePaths, true);
758         }
759
760         return NI_ERROR_NONE;
761 #else
762         _SERR("crossgen supports arm/arm64 architecture only. skip ni file generation");
763         return NI_ERROR_NOT_SUPPORTED;
764 #endif
765 }
766
767 void finalizeNICommon()
768 {
769         __interval = 0;
770
771         finalizePluginManager();
772
773         delete(__pm);
774         __pm = nullptr;
775
776         if (__ni_option) {
777                 free(__ni_option);
778                 __ni_option = nullptr;
779         }
780 }
781
782 ni_error_e createNIPlatform(NIOption* opt)
783 {
784         ni_error_e ret = createNIUnderDirs(__pm->getRuntimePath(), opt);
785         if (ret != NI_ERROR_NONE) {
786                 return ret;
787         }
788
789         return createNIUnderDirs(__pm->getTizenFXPath(), opt);
790 }
791
792 ni_error_e createNIDll(const std::string& dllPath, NIOption* opt)
793 {
794         return doAOTFile(dllPath, std::string(), opt);
795 }
796
797 ni_error_e createNIUnderTAC(const std::string& targetPath, const std::string& refPaths, NIOption* opt)
798 {
799         ni_error_e ret;
800
801         // get managed file list from targetPath
802         std::vector<std::string> dllList;
803         ret = getTargetDllList(targetPath, dllList);
804         if (ret != NI_ERROR_NONE) {
805                 return ret;
806         }
807
808         std::vector<std::string> needNIList;
809         std::vector<std::string> niList;
810
811         for (auto &dll : dllList) {
812                 if (!checkNIExistence(dll)) {
813                         needNIList.push_back(dll);
814                 }
815                 niList.push_back(getNIFilePath(dll));
816         }
817
818         if (!needNIList.empty()) {
819                 // NI fils of TAC-related dlls under /opt/usr/dotnet should not be created under .native_image directory.
820                 // So, unset NI_FLAGS_APPNI temporally and restore it after running AOT.
821                 opt->flags &= ~NI_FLAGS_APPNI;
822                 ret = doAOTList(needNIList, refPaths, opt);
823                 opt->flags |= NI_FLAGS_APPNI;
824                 if (ret != NI_ERROR_NONE) {
825                         return ret;
826                 }
827         }
828
829         for (auto &niPath : niList) {
830                 if (exist(niPath)) {
831                         std::string symNIPath = concatPath(targetPath, getFileName(niPath));
832                         if (!exist(symNIPath)) {
833                                 bf::create_symlink(niPath, symNIPath);
834                                 copySmackAndOwnership(targetPath.c_str(), symNIPath.c_str(), true);
835                                 _SOUT("%s symbolic link file generated successfully.", symNIPath.c_str());
836                                 _INFO("%s symbolic link file generated successfully.", symNIPath.c_str());
837                         }
838                 }
839         }
840
841         return NI_ERROR_NONE;
842 }
843
844
845 ni_error_e createNIUnderDirs(const std::string& rootPaths, NIOption* opt)
846 {
847         ni_error_e ret = NI_ERROR_NONE;
848
849         std::vector<std::string> fileList;
850         std::vector<std::string> paths;
851         splitPath(rootPaths, paths);
852
853         for (const auto &path : paths) {
854                 if (!exist(path)) {
855                         continue;
856                 }
857
858                 if (path.find(TAC_SYMLINK_SUB_DIR) != std::string::npos) {
859                         ret = createNIUnderTAC(path, rootPaths, opt);
860                         if (ret != NI_ERROR_NONE) {
861                                 return ret;
862                         }
863                 } else if (opt->flags & NI_FLAGS_APPNI) {
864                         ret = getAppTargetDllList(path, fileList, opt);
865                         if (ret != NI_ERROR_NONE) {
866                                 return ret;
867                         }
868                 } else {
869                         ret = getTargetDllList(path, fileList);
870                         if (ret != NI_ERROR_NONE) {
871                                 return ret;
872                         }
873                 }
874         }
875
876         if (fileList.empty()) {
877                 return NI_ERROR_NONE;
878         }
879
880         return doAOTList(fileList, rootPaths, opt);
881 }
882
883 ni_error_e createNIUnderPkgRoot(const std::string& pkgId, NIOption* opt)
884 {
885         if (!isR2RImage(concatPath(__pm->getRuntimePath(), "System.Private.CoreLib.dll"))) {
886                 _SERR("The native image of System.Private.CoreLib does not exist.\n"
887                                 "Run the command to create the native image\n"
888                                 "# dotnettool --ni-dll /usr/share/dotnet.tizen/netcoreapp/System.Private.CoreLib.dll");
889                 return NI_ERROR_CORE_NI_FILE;
890         }
891
892         std::string rootPath = getRootPath(pkgId);
893         if (rootPath.empty()) {
894                 _SERR("Failed to get root path from [%s]", pkgId.c_str());
895                 return NI_ERROR_INVALID_PACKAGE;
896         }
897
898         __pm->setAppRootPath(rootPath);
899
900         char* extraDllPaths = pluginGetExtraDllPath();
901         if (extraDllPaths && extraDllPaths[0] != '\0') {
902                 opt->flags |= NI_FLAGS_EXTRA_REF;
903                 splitPath(extraDllPaths, opt->extraRefPath);
904         }
905
906         opt->flags |= NI_FLAGS_APPNI;
907
908         if (isReadOnlyArea(rootPath)) {
909                 opt->flags |= NI_FLAGS_APP_UNDER_RO_AREA;
910                 opt->flags |= NI_FLAGS_NO_PIPELINE;
911                 _SERR("Only no-pipeline mode supported for RO app. Set no-pipeline option forcibly");
912         } else {
913                 opt->flags &= ~NI_FLAGS_APP_UNDER_RO_AREA;
914         }
915
916         // create native image under bin and lib directory
917         // tac directory is skipped in the createNIUnderDirs.
918         return createNIUnderDirs(__pm->getAppPaths(), opt);
919 }
920
921 void removeNIPlatform()
922 {
923         std::string coreLib = concatPath(__pm->getRuntimePath(), "System.Private.CoreLib.dll");
924         std::string coreLibBackup = concatPath(__pm->getRuntimePath(), "System.Private.CoreLib.dll.Backup");
925
926         if (isR2RImage(coreLib)) {
927                 if (!isFile(coreLibBackup)) {
928                         return;
929                 }
930
931                 if (remove(coreLib.c_str())) {
932                         _SERR("Failed to remove System.Private.CoreLib native image file");
933                 }
934                 if (rename(coreLibBackup.c_str(), coreLib.c_str())) {
935                         _SERR("Failed to rename System.Private.CoreLib.Backup to origin");
936                 }
937         }
938
939 #ifdef UNIQUE_DEFAULT_BASE_ADDR_SUPPORT
940         if (isFile(__SYSTEM_BASE_FILE)) {
941                 if (remove(__SYSTEM_BASE_FILE)) {
942                         _SERR("Failed to remove %s", __SYSTEM_BASE_FILE);
943                 }
944         }
945 #endif
946
947         removeNIUnderDirs(__pm->getRuntimePath() + ":" + __pm->getTizenFXPath());
948 }
949
950 void removeNIUnderDirs(const std::string& rootPaths)
951 {
952         auto convert = [](const std::string& path, const std::string& filename) {
953                 if (isNativeImage(path)) {
954                         if (remove(path.c_str())) {
955                                 _SERR("Failed to remove %s", path.c_str());
956                         }
957                 }
958         };
959
960         std::vector<std::string> paths;
961         splitPath(rootPaths, paths);
962         for (const auto &path : paths) {
963                 scanFilesInDirectory(path, convert, -1);
964         }
965 }
966
967 ni_error_e removeNIUnderPkgRoot(const std::string& pkgId)
968 {
969         std::string rootPath = getRootPath(pkgId);
970         if (rootPath.empty()) {
971                 _SERR("Failed to get root path from [%s]", pkgId.c_str());
972                 return NI_ERROR_INVALID_PACKAGE;
973         }
974
975         __pm->setAppRootPath(rootPath);
976
977         // getAppNIPaths returns bin/.native_image, lib/.native_image and .tac_symlink.
978         std::string appNIPaths = __pm->getAppNIPaths();
979         std::vector<std::string> paths;
980         splitPath(appNIPaths, paths);
981         for (const auto &path : paths) {
982                 if (!isReadOnlyArea(path)) {
983                         // Only the native image inside the TAC should be removed.
984                         if (strstr(path.c_str(), TAC_SYMLINK_SUB_DIR) != NULL) {
985                                 removeNIUnderDirs(path);
986                         } else {
987                                 if (isDirectory(path)) {
988                                         if (!removeAll(path.c_str())) {
989                                                 _SERR("Failed to remove app ni dir [%s]", path.c_str());
990                                         }
991                                 }
992                         }
993                 }
994         }
995
996         // In special cases, the ni file may exist in the dll location.
997         // The code below is to avoid this exceptional case.
998         std::string appPaths = __pm->getAppPaths();
999         splitPath(appPaths, paths);
1000         for (const auto &path : paths) {
1001                 if (isDirectory(path)) {
1002                         removeNIUnderDirs(path);
1003                 }
1004         }
1005
1006         return NI_ERROR_NONE;
1007 }
1008
1009 ni_error_e regenerateAppNI(NIOption* opt)
1010 {
1011         int ret = 0;
1012         pkgmgrinfo_appinfo_metadata_filter_h handle;
1013
1014         ret = pkgmgrinfo_appinfo_metadata_filter_create(&handle);
1015         if (ret != PMINFO_R_OK)
1016                 return NI_ERROR_UNKNOWN;
1017
1018         ret = pkgmgrinfo_appinfo_metadata_filter_add(handle, AOT_METADATA_KEY, METADATA_VALUE);
1019         if (ret != PMINFO_R_OK) {
1020                 pkgmgrinfo_appinfo_metadata_filter_destroy(handle);
1021                 return NI_ERROR_UNKNOWN;
1022         }
1023
1024         ret = pkgmgrMDFilterForeach(handle, appAotCb, &opt);
1025         if (ret != 0) {
1026                 pkgmgrinfo_appinfo_metadata_filter_destroy(handle);
1027                 return NI_ERROR_UNKNOWN;
1028         }
1029
1030         pkgmgrinfo_appinfo_metadata_filter_destroy(handle);
1031         return NI_ERROR_NONE;
1032 }
1033
1034 // callback function of "pkgmgrinfo_appinfo_metadata_filter_foreach"
1035 static int regenTacCb(pkgmgrinfo_appinfo_h handle, void *userData)
1036 {
1037         char *pkgId = NULL;
1038         NIOption **pOpt = (NIOption**)userData;
1039
1040         int ret = pkgmgrinfo_appinfo_get_pkgid(handle, &pkgId);
1041         if (ret != PMINFO_R_OK || pkgId == NULL) {
1042                 _SERR("Failed to get pkgid");
1043                 return -1;
1044         }
1045
1046         sqlite3 *tac_db = openDB(TAC_APP_LIST_DB);
1047         if (!tac_db) {
1048                 _SERR("Sqlite open error");
1049                 return -1;
1050         }
1051         sqlite3_exec(tac_db, "BEGIN;", NULL, NULL, NULL);
1052
1053         char *sql = sqlite3_mprintf("SELECT * FROM TAC WHERE PKGID = %Q;", pkgId);
1054         std::vector<std::string> nugets = selectDB(tac_db, sql);
1055         sqlite3_free(sql);
1056
1057         if (tac_db) {
1058                 closeDB(tac_db);
1059                 tac_db = NULL;
1060         }
1061
1062         std::string nugetPaths;
1063         for (const auto &nuget : nugets) {
1064                 if (!nugetPaths.empty()) {
1065                         nugetPaths += ":";
1066                 }
1067                 nugetPaths += concatPath(__DOTNET_DIR, nuget);
1068         }
1069
1070         for (auto& nuget : nugets) {
1071                 createNIUnderTAC(concatPath(__DOTNET_DIR, nuget), nugetPaths, *pOpt);
1072         }
1073
1074         return 0;
1075 }
1076
1077 ni_error_e regenerateTACNI(NIOption* opt)
1078 {
1079         removeNIUnderDirs(__DOTNET_DIR);
1080
1081         pkgmgrinfo_appinfo_metadata_filter_h handle;
1082         int ret = pkgmgrinfo_appinfo_metadata_filter_create(&handle);
1083         if (ret != PMINFO_R_OK) {
1084                 return NI_ERROR_UNKNOWN;
1085         }
1086
1087         ret = pkgmgrinfo_appinfo_metadata_filter_add(handle, TAC_METADATA_KEY, METADATA_VALUE);
1088         if (ret != PMINFO_R_OK) {
1089                 pkgmgrinfo_appinfo_metadata_filter_destroy(handle);
1090                 return NI_ERROR_UNKNOWN;
1091         }
1092
1093         ret = pkgmgrMDFilterForeach(handle, regenTacCb, &opt);
1094         if (ret != 0) {
1095                 pkgmgrinfo_appinfo_metadata_filter_destroy(handle);
1096                 return NI_ERROR_UNKNOWN;
1097         }
1098
1099         pkgmgrinfo_appinfo_metadata_filter_destroy(handle);
1100
1101         return NI_ERROR_NONE;
1102 }
1103
1104 static std::vector<uid_t> getUserIds()
1105 {
1106         std::vector<uid_t> list;
1107
1108         while (true) {
1109                 errno = 0; // so we can distinguish errors from no more entries
1110                 passwd* entry = getpwent();
1111                 if (!entry) {
1112                         if (errno) {
1113                                 _SERR("Error while getting userIDs");
1114                                 list.clear();
1115                                 return list;
1116                         }
1117                         break;
1118                 }
1119                 list.push_back(entry->pw_uid);
1120         }
1121         endpwent();
1122
1123         return list;
1124 }
1125
1126 static std::string getAppDataPath(const std::string& pkgId, uid_t uid)
1127 {
1128         std::string pDataFile;
1129
1130         tzplatform_set_user(uid);
1131
1132         const char* tzUserApp = tzplatform_getenv(TZ_USER_APP);
1133         if (tzUserApp != NULL) {
1134                 pDataFile = std::string(tzUserApp) + "/" + pkgId + "/data/";
1135         }
1136
1137         tzplatform_reset_user();
1138
1139         return pDataFile;
1140 }
1141
1142 ni_error_e removeAppProfileData(const std::string& pkgId)
1143 {
1144         if (pkgId.empty()) {
1145                 return NI_ERROR_INVALID_PARAMETER;
1146         }
1147
1148         std::vector<uid_t> uidList = getUserIds();
1149         for (auto& uid : uidList) {
1150                 // get data path from pkgid
1151                 std::string dataPath = getAppDataPath(pkgId, uid);
1152                 if (!dataPath.empty() && exist(dataPath)) {
1153                         std::string pDataFile = dataPath + PROFILE_BASENAME;
1154
1155                         if (exist(pDataFile)) {
1156                                 if (!removeFile(pDataFile)) {
1157                                         _SERR("Fail to remove profile data file (%s).", pDataFile.c_str());
1158                                         return NI_ERROR_UNKNOWN;
1159                                 }
1160                                 _SOUT("Profile data (%s) is removed successfully", pDataFile.c_str());
1161                         }
1162                 }
1163         }
1164
1165         return NI_ERROR_NONE;
1166 }
1167
1168 static int appTypeListCb(pkgmgrinfo_appinfo_h handle, void *user_data)
1169 {
1170         char *pkgId = NULL;
1171         int ret = pkgmgrinfo_appinfo_get_pkgid(handle, &pkgId);
1172         if (ret != PMINFO_R_OK || pkgId == NULL) {
1173                 _SERR("Fail to get pkgid");
1174                 return 0;
1175         }
1176
1177         if (removeAppProfileData(pkgId) != NI_ERROR_NONE) {
1178                 _SERR("Fail to remove profile data for (%s)", pkgId);
1179         }
1180
1181         return 0;
1182 }
1183
1184 static ni_error_e removeAppProfileByAppType(const char* type)
1185 {
1186         int ret;
1187
1188         pkgmgrinfo_appinfo_filter_h filter;
1189
1190         ret = pkgmgrinfo_appinfo_filter_create(&filter);
1191         if (ret != PMINFO_R_OK) {
1192                 _SERR("Fail to create appinfo filter");
1193                 return NI_ERROR_UNKNOWN;
1194         }
1195
1196         ret = pkgmgrinfo_appinfo_filter_add_string(filter, PMINFO_APPINFO_PROP_APP_TYPE, type);
1197         if (ret != PMINFO_R_OK) {
1198                 pkgmgrinfo_appinfo_filter_destroy(filter);
1199                 _SERR("Fail to add appinfo filter (%s)", type);
1200                 return NI_ERROR_UNKNOWN;
1201         }
1202
1203         ret = pkgmgrinfo_appinfo_filter_foreach_appinfo(filter, appTypeListCb, NULL);
1204         if (ret != PMINFO_R_OK) {
1205                 _SERR("Fail to pkgmgrinfo_pkginfo_filter_foreach_pkginfo");
1206                 pkgmgrinfo_appinfo_filter_destroy(filter);
1207                 return NI_ERROR_UNKNOWN;
1208         }
1209
1210         pkgmgrinfo_appinfo_filter_destroy(filter);
1211
1212         return NI_ERROR_NONE;
1213 }
1214
1215 void removeAllAppProfileData()
1216 {
1217         std::vector<const char*> appTypeList = {"dotnet", "dotnet-nui", "dotnet-inhouse"};
1218
1219         for (auto& type : appTypeList) {
1220                 if (removeAppProfileByAppType(type) != NI_ERROR_NONE) {
1221                         _SERR("Fail to removeAppProfileByAppType for type (%s)", type);
1222                 }
1223         }
1224 }