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