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