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