2 * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include <pkgmgr-info.h>
18 #include <pkgmgr_installer_info.h>
23 #include "pkgmgr_parser_plugin_interface.h"
38 #include "ni_common.h"
39 #include "tac_common.h"
40 #include "path_manager.h"
41 #include "plugin_manager.h"
46 #define LOG_TAG "DOTNET_INSTALLER_PLUGIN"
49 #error "CROSSGEN_PATH is missed"
53 #define __STR(x) __XSTR(x)
54 static const char* __CROSSGEN_PATH = __STR(CROSSGEN_PATH);
55 static const char* __TAC_DIR = __STR(TAC_DIR);
57 #ifdef UNIQUE_DEFAULT_BASE_ADDR_SUPPORT
58 static const char* __SYSTEM_BASE_FILE = __STR(SYSTEM_BASE_FILE);
64 static int __interval = 0;
65 static std::string __tpa;
67 static void waitInterval()
69 // by the recommand, ignore small value for performance.
70 if (__interval > 10000) {
71 fprintf(stderr, "sleep %d usec\n", __interval);
76 static std::string getNiFilePath(const std::string& dllPath)
78 size_t index = dllPath.find_last_of(".");
79 if (index == std::string::npos) {
80 fprintf(stderr, "File doesnot contain extension. fail to get NI file name\n");
83 std::string fName = dllPath.substr(0, index);
84 std::string fExt = dllPath.substr(index, dllPath.length());
86 // crossgen generate file with lower case extension only
87 std::transform(fExt.begin(), fExt.end(), fExt.begin(), ::tolower);
88 std::string niPath = fName + ".ni" + fExt;
93 static std::string getAppNIPath(const std::string& niPath)
96 std::string niDirPath;
99 size_t index = niPath.find_last_of("/");
100 if (index != std::string::npos) {
101 prevPath = niPath.substr(0, index);
102 fileName = niPath.substr(index + 1, niPath.length());
108 niDirPath = concatPath(prevPath, APP_NI_SUB_DIR);
110 if (!isFileExist(niDirPath)) {
111 if (mkdir(niDirPath.c_str(), 0755) == 0) {
112 updateAssemblyInfo(prevPath, niDirPath);
114 fprintf(stderr, "Fail to create app ni directory (%s)\n", niDirPath.c_str());
118 return concatPath(niDirPath, fileName);
121 static bool niExist(const std::string& path)
123 std::string f = getNiFilePath(path);
128 if (isFileExist(f)) {
132 // native image of System.Private.CoreLib.dll should have to overwrite
133 // original file to support new coreclr
134 if (path.find("System.Private.CoreLib.dll") != std::string::npos) {
135 std::string coreLibBackup = path + ".Backup";
136 if (isFileExist(coreLibBackup)) {
144 #ifdef UNIQUE_DEFAULT_BASE_ADDR_SUPPORT
145 // Get next base address to be used for system ni image from file
146 // __SYSTEM_BASE_FILE should be checked for existance before calling this function
147 static uintptr_t getNextBaseAddrFromFile()
149 FILE *pFile = fopen(__SYSTEM_BASE_FILE, "r");
151 fprintf(stderr, "Failed to open %s\n", __SYSTEM_BASE_FILE);
158 while (fscanf(pFile, "%u %u", &addr, &size) != EOF) {
166 // Get next base address to be used for system ni image
167 static uintptr_t getNextBaseAddr()
169 uintptr_t baseAddr = 0;
171 if (!isFileExist(__SYSTEM_BASE_FILE)) {
172 // This is the starting address for all default base addresses
173 baseAddr = DEFAULT_BASE_ADDR_START;
175 baseAddr = getNextBaseAddrFromFile();
177 // Round to a multple of 64K (see ZapImage::CalculateZapBaseAddress in CoreCLR)
178 uintptr_t BASE_ADDRESS_ALIGNMENT = 0xffff;
179 baseAddr = (baseAddr + BASE_ADDRESS_ALIGNMENT) & ~BASE_ADDRESS_ALIGNMENT;
185 // Save base address of system ni image to file
186 static void updateBaseAddrFile(const std::string &absNiPath, uintptr_t baseAddr)
188 uintptr_t niSize = getFileSize(absNiPath);
190 fprintf(stderr, "File %s doesn't exist\n", absNiPath.c_str());
194 // Write new entry to the file
195 FILE *pFile = fopen(__SYSTEM_BASE_FILE, "a");
197 fprintf(stderr, "Failed to open %s\n", __SYSTEM_BASE_FILE);
201 fprintf(pFile, "%u %u\n", baseAddr, niSize);
205 // check if dll is listed in TPA
206 static bool isTPADll(const std::string &dllPath)
208 std::string absDllPath = absolutePath(dllPath);
210 if (__tpa.find(absDllPath) != std::string::npos) {
218 // baseAddr should be checked in file before getting here
219 static ni_error_e crossgen(const std::string& dllPath, const std::string& appPath, DWORD flags)
221 if (!isFileExist(dllPath)) {
222 fprintf(stderr, "dll file is not exist : %s\n", dllPath.c_str());
223 return NI_ERROR_NO_SUCH_FILE;
226 if (!isManagedAssembly(dllPath)) {
227 fprintf(stderr, "Input file is not a dll file : %s\n", dllPath.c_str());
228 return NI_ERROR_INVALID_PARAMETER;
231 if (niExist(dllPath)) {
232 fprintf(stderr, "Already ni file is exist for %s\n", dllPath.c_str());
233 return NI_ERROR_ALREADY_EXIST;
236 std::string absDllPath = absolutePath(dllPath);
237 std::string absNiPath = getNiFilePath(dllPath);
238 if (absNiPath.empty()) {
239 fprintf(stderr, "Fail to get ni file name\n");
240 return NI_ERROR_UNKNOWN;
243 bool isAppNI = flags & NI_FLAGS_APPNI;
244 if (isAppNI && strstr(absNiPath.c_str(), __TAC_DIR) == NULL) {
245 absNiPath = getAppNIPath(absNiPath);
248 #ifdef UNIQUE_DEFAULT_BASE_ADDR_SUPPORT
249 uintptr_t baseAddr = 0;
251 if (isTPADll(dllPath)) {
252 baseAddr = getNextBaseAddr();
258 return NI_ERROR_UNKNOWN;
262 waitpid(pid, &status, 0);
263 if (WIFEXITED(status)) {
264 // Do not use niExist() function to check whether ni file created or not.
265 // niEixst() return false for System.Private.Corelib.dll
266 if (isFileExist(absNiPath)) {
267 updateAssemblyInfo(absDllPath, absNiPath);
268 #ifdef UNIQUE_DEFAULT_BASE_ADDR_SUPPORT
270 updateBaseAddrFile(absNiPath, baseAddr);
273 return NI_ERROR_NONE;
275 fprintf(stderr, "Fail to create native image for %s\n", dllPath.c_str());
276 return NI_ERROR_NO_SUCH_FILE;
280 std::string jitPath = getRuntimeDir() + "/libclrjit.so";
281 std::vector<const char*> argv = {
284 "/JITPath", jitPath.c_str()
287 bool compat = flags & NI_FLAGS_COMPATIBILITY;
288 argv.push_back(compat ? "/Trusted_Platform_Assemblies" : "/r");
289 argv.push_back(__tpa.c_str());
291 bool enableR2R = flags & NI_FLAGS_ENABLER2R;
293 argv.push_back("/FragileNonVersionable");
296 if (flags & NI_FLAGS_VERBOSE) {
297 argv.push_back("/verbose");
300 if (flags & NI_FLAGS_INSTRUMENT) {
301 argv.push_back("/Tuning");
304 #ifdef UNIQUE_DEFAULT_BASE_ADDR_SUPPORT
306 argv.push_back("/BaseAddress");
307 argv.push_back(std::to_string(baseAddr).c_str());
311 argv.push_back("/App_Paths");
312 std::string absAppPath;
313 if (!appPath.empty()) {
314 absAppPath = appPath;
316 absAppPath = baseName(absDllPath);
318 argv.push_back(absAppPath.c_str());
320 argv.push_back("/out");
321 argv.push_back(absNiPath.c_str());
323 argv.push_back(absDllPath.c_str());
324 argv.push_back(nullptr);
326 fprintf(stderr, "+ %s (%s)\n", absDllPath.c_str(), enableR2R ? "R2R" : "FNV");
328 execv(__CROSSGEN_PATH, const_cast<char* const*>(argv.data()));
332 return NI_ERROR_NONE;
335 // callback function of "pkgmgrinfo_appinfo_metadata_filter_foreach"
336 static int appAotCb(pkgmgrinfo_appinfo_h handle, void *userData)
340 DWORD *pFlags = (DWORD*)userData;
342 ret = pkgmgrinfo_appinfo_get_pkgid(handle, &pkgId);
343 if (ret != PMINFO_R_OK) {
344 fprintf(stderr, "Failed to get pkgid\n");
348 if (removeNiUnderPkgRoot(pkgId) != NI_ERROR_NONE) {
349 fprintf(stderr, "Failed to remove previous dlls from [%s]\n", pkgId);
353 if (resetTACPackage(pkgId) != TAC_ERROR_NONE) {
354 fprintf(stderr, "Failed to remove symlink for given package [%s]\n", pkgId);
358 // Regenerate ni files with R2R mode forcibiliy. (there is no way to now which option is used)
359 if (createNiUnderPkgRoot(pkgId, *pFlags) != NI_ERROR_NONE) {
360 fprintf(stderr, "Failed to generate NI file [%s]\n", pkgId);
363 fprintf(stderr, "Complete make application to native image\n");
366 if (createTACPkgRoot(pkgId, *pFlags) != NI_ERROR_NONE) {
367 fprintf(stderr, "Failed to generate symbolic link file [%s]\n", pkgId);
370 fprintf(stderr, "Complete make symbolic link file to tac\n");
376 static void createCoreLibNI(DWORD flags)
378 std::string coreLib = concatPath(getRuntimeDir(), "System.Private.CoreLib.dll");
379 std::string niCoreLib = concatPath(getRuntimeDir(), "System.Private.CoreLib.ni.dll");
380 std::string coreLibBackup = concatPath(getRuntimeDir(), "System.Private.CoreLib.dll.Backup");
382 if (!isFileExist(coreLibBackup)) {
384 if (!crossgen(coreLib, std::string(), flags)) {
385 if (rename(coreLib.c_str(), coreLibBackup.c_str())) {
386 fprintf(stderr, "Failed to rename System.Private.CoreLib.dll\n");
388 if (rename(niCoreLib.c_str(), coreLib.c_str())) {
389 fprintf(stderr, "Failed to rename System.Private.CoreLib.ni.dll\n");
392 fprintf(stderr, "Failed to create native image for %s\n", coreLib.c_str());
397 ni_error_e initNICommon(NiCommonOption* option)
400 // get interval value
401 const char* intervalFile = "/usr/share/dotnet.tizen/lib/crossgen_interval.txt";
402 std::ifstream inFile(intervalFile);
404 fprintf(stderr, "crossgen_interval.txt is found\n");
405 inFile >> __interval;
408 if (initializePluginManager("normal")) {
409 fprintf(stderr, "Fail to initialize PluginManager\n");
410 return NI_ERROR_UNKNOWN;
412 if (initializePathManager(option->runtimeDir, option->tizenFXDir, option->extraDirs)) {
413 fprintf(stderr, "Fail to initialize PathManager\n");
414 return NI_ERROR_UNKNOWN;
419 return NI_ERROR_NONE;
421 fprintf(stderr, "crossgen supports arm architecture only. skip ni file generation\n");
422 return NI_ERROR_NOT_SUPPORTED;
426 void finalizeNICommon()
430 finalizePluginManager();
431 finalizePathManager();
437 void createNiPlatform(DWORD flags)
439 const std::string platformDirs[] = {getRuntimeDir(), getTizenFXDir()};
440 createNiUnderDirs(platformDirs, 2, flags);
443 ni_error_e createNiDll(const std::string& dllPath, DWORD flags)
445 createCoreLibNI(flags);
446 // System.Private.CoreLib.dll is generated in the createCoreLibNI function.
447 // Skip if input dll is System.Private.CoreLib.dll
448 if (dllPath.find("System.Private.CoreLib.dll") != std::string::npos) {
449 return NI_ERROR_NONE;
452 ni_error_e status = crossgen(dllPath, std::string(), flags);
457 void createNiUnderTAC(const std::string rootPaths[], int count, DWORD flags)
459 std::string appPaths;
461 for (auto& nuget : bf::recursive_directory_iterator(__TAC_DIR)) {
462 std::string nugetPath = nuget.path().string();
463 if (bf::is_directory(nugetPath)) {
464 appPaths += nugetPath;
468 if (appPaths.back() == ':') {
472 auto convert = [&appPaths, flags](const std::string& path, const char* name) {
473 if (strstr(path.c_str(), TAC_APP_LIST_DB) != NULL ||
474 strstr(path.c_str(), TAC_APP_LIST_RESTORE_DB) != NULL ||
475 strstr(path.c_str(), TAC_SHA_256_INFO) != NULL)
477 if (!crossgen(path, appPaths.c_str(), flags)) {
482 for (int i = 0; i < count; i++) {
483 scanFilesInDir(rootPaths[i], convert, -1);
485 } catch (const bf::filesystem_error& error) {
486 fprintf(stderr, "Failed to recursive directory: %s\n", error.what());
491 ni_error_e createTACPkgRoot(const std::string& pkgId, DWORD flags)
494 if (getRootPath(pkgId, pkgRoot) < 0) {
495 return NI_ERROR_INVALID_PACKAGE;
498 std::string binDir = concatPath(pkgRoot, "bin");
499 std::string tacDir = concatPath(binDir, TAC_SYMLINK_SUB_DIR);
500 std::string binNIDir = concatPath(binDir, APP_NI_SUB_DIR);
501 if (bf::exists(tacDir)) {
503 for (auto& symlinkAssembly : bf::recursive_directory_iterator(tacDir)) {
504 std::string symPath = symlinkAssembly.path().string();
505 if (bf::is_symlink(symPath)) {
506 if (!isNativeImage(symPath)) {
507 std::string originPath = bf::read_symlink(symPath).string();
508 std::string originNiPath = originPath.substr(0, originPath.rfind(".dll")) + ".ni.dll";
509 if (!bf::exists(originNiPath)) {
510 if(createNiDll(originPath, flags) != NI_ERROR_NONE) {
511 fprintf(stderr, "Failed to create NI file [%s]\n", originPath.c_str());
512 return NI_ERROR_UNKNOWN;
515 std::string symNIPath = symPath.substr(0, symPath.rfind(".dll")) + ".ni.dll";
516 if (!bf::exists(symNIPath)) {
517 bf::create_symlink(originNiPath, symNIPath);
518 fprintf(stderr, "%s symbolic link file generated successfully.\n", symNIPath.c_str());
519 updateAssemblyInfo(tacDir.c_str(), symNIPath.c_str(), true);
521 std::string NIFileName = symNIPath.substr(symNIPath.rfind('/') + 1);
522 if (!removeFile(concatPath(binNIDir, NIFileName))) {
523 fprintf(stderr, "Failed to remove of %s\n", concatPath(binNIDir, NIFileName).c_str());
524 return NI_ERROR_UNKNOWN;
530 } catch (const bf::filesystem_error& error) {
531 fprintf(stderr, "Failed to recursive directory: %s\n", error.what());
532 return NI_ERROR_UNKNOWN;
535 return NI_ERROR_NONE;
538 void createNiUnderDirs(const std::string rootPaths[], int count, DWORD flags)
540 createCoreLibNI(flags);
542 std::string appPaths;
543 for (int i = 0; i < count; i++) {
544 appPaths += rootPaths[i];
548 if (appPaths.back() == ':')
551 std::vector<std::string> tpaAssemblies;
552 splitPath(__tpa, tpaAssemblies);
554 auto convert = [&appPaths, flags, tpaAssemblies](const std::string& path, const char* name) {
555 bool isAppNI = flags & NI_FLAGS_APPNI;
557 std::string assembly = path.substr(path.rfind('/') + 1);
558 bool isExist = false;
559 for (auto& tpa : tpaAssemblies) {
560 if (!strcmp(replaceAll(tpa, ".ni.dll", ".dll").c_str(), assembly.c_str())) {
566 fprintf(stderr, "%s present in the TPA list skips generation of NI file.\n", path.c_str());
570 if (!crossgen(path, appPaths.c_str(), flags)) {
575 for (int i = 0; i < count; i++) {
576 scanFilesInDir(rootPaths[i], convert, 1);
579 tpaAssemblies.clear();
582 ni_error_e createNiUnderPkgRoot(const std::string& pkgId, DWORD flags)
585 if (getRootPath(pkgId, pkgRoot) < 0) {
586 return NI_ERROR_INVALID_PACKAGE;
589 std::string binDir = concatPath(pkgRoot, "bin");
590 std::string libDir = concatPath(pkgRoot, "lib");
591 std::string tacDir = concatPath(binDir, TAC_SYMLINK_SUB_DIR);
592 std::string paths[] = {binDir, libDir, tacDir};
594 flags |= NI_FLAGS_APPNI;
595 createNiUnderDirs(paths, 3, flags);
597 return NI_ERROR_NONE;
600 ni_error_e createNiDllUnderPkgRoot(const std::string& pkgId, const std::string& dllPath, DWORD flags)
603 if (getRootPath(pkgId, pkgRoot) < 0) {
604 return NI_ERROR_INVALID_PACKAGE;
607 std::string binDir = concatPath(pkgRoot, "bin");
608 std::string libDir = concatPath(pkgRoot, "lib");
609 std::string tacDir = concatPath(binDir, TAC_SYMLINK_SUB_DIR);
610 std::string paths = binDir + ":" + libDir + ":" + tacDir;
612 if (bf::is_symlink(dllPath)) {
613 if (bf::exists(tacDir)) {
614 if (!isNativeImage(dllPath)) {
615 std::string originPath = bf::read_symlink(dllPath).string();
616 std::string originNiPath = originPath.substr(0, originPath.rfind(".dll")) + ".ni.dll";
617 if (!bf::exists(originNiPath)) {
618 if(createNiDll(originPath, flags) != NI_ERROR_NONE) {
619 fprintf(stderr, "Failed to create NI file [%s]\n", originPath.c_str());
620 return NI_ERROR_UNKNOWN;
623 std::string setNiPath = dllPath.substr(0, dllPath.rfind(".dll")) + ".ni.dll";
624 if (!bf::exists(setNiPath)) {
625 bf::create_symlink(originNiPath, setNiPath);
626 fprintf(stderr, "%s symbolic link file generated successfully.\n", setNiPath.c_str());
627 updateAssemblyInfo(tacDir.c_str(), setNiPath.c_str(), true);
631 return NI_ERROR_NONE;
633 std::string assembly = dllPath.substr(dllPath.rfind('/') + 1);
634 std::vector<std::string> tpaAssemblies;
635 splitPath(__tpa, tpaAssemblies);
636 bool isExist = false;
637 for (auto& tpa : tpaAssemblies) {
638 if (!strcmp(replaceAll(tpa, ".ni.dll", ".dll").c_str(), assembly.c_str())) {
643 tpaAssemblies.clear();
645 fprintf(stderr, "%s present in the TPA list skips generation of NI file.\n", dllPath.c_str());
646 return NI_ERROR_NONE;
648 flags |= NI_FLAGS_APPNI;
649 return crossgen(dllPath, paths, flags);
654 void removeNiPlatform()
656 std::string coreLib = concatPath(getRuntimeDir(), "System.Private.CoreLib.dll");
657 std::string coreLibBackup = concatPath(getRuntimeDir(), "System.Private.CoreLib.dll.Backup");
659 if (!isFileExist(coreLibBackup)) {
663 #ifdef UNIQUE_DEFAULT_BASE_ADDR_SUPPORT
664 if (isFileExist(__SYSTEM_BASE_FILE)) {
665 if (remove(__SYSTEM_BASE_FILE)) {
666 fprintf(stderr, "Failed to remove %s\n", __SYSTEM_BASE_FILE);
671 if (remove(coreLib.c_str())) {
672 fprintf(stderr, "Failed to remove System.Private.CoreLib native image file\n");
675 if (rename(coreLibBackup.c_str(), coreLib.c_str())) {
676 fprintf(stderr, "Failed to rename System.Private.CoreLib.Backup to origin\n");
679 const std::string platformDirs[] = {getRuntimeDir(), getTizenFXDir()};
681 removeNiUnderDirs(platformDirs, 2);
684 void removeNiUnderDirs(const std::string rootPaths[], int count)
686 auto convert = [](const std::string& path, std::string name) {
687 if (isNativeImage(path)) {
688 if (remove(path.c_str())) {
689 fprintf(stderr, "Failed to remove %s\n", path.c_str());
694 for (int i = 0; i < count; i++)
695 scanFilesInDir(rootPaths[i], convert, -1);
698 ni_error_e removeNiUnderPkgRoot(const std::string& pkgId)
701 if (getRootPath(pkgId, pkgRoot) < 0) {
702 return NI_ERROR_INVALID_PACKAGE;
705 std::string binDir = concatPath(pkgRoot, "bin");
706 std::string libDir = concatPath(pkgRoot, "lib");
707 std::string paths[] = {binDir, libDir};
709 removeNiUnderDirs(paths, 2);
711 std::string binNIDir = concatPath(binDir, APP_NI_SUB_DIR);
712 if (isFileExist(binNIDir)) {
713 if (rmdir(binNIDir.c_str()) != 0) {
714 fprintf(stderr, "Failed to remove app ni dir [%s]\n", binNIDir.c_str());
718 std::string libNIDir = concatPath(libDir, APP_NI_SUB_DIR);
719 if (isFileExist(libNIDir)) {
720 if (rmdir(libNIDir.c_str()) != 0) {
721 fprintf(stderr, "Failed to remove app ni dir [%s]\n", libNIDir.c_str());
725 return NI_ERROR_NONE;
728 ni_error_e regenerateAppNI(DWORD flags)
731 pkgmgrinfo_appinfo_metadata_filter_h handle;
733 ret = pkgmgrinfo_appinfo_metadata_filter_create(&handle);
734 if (ret != PMINFO_R_OK)
735 return NI_ERROR_UNKNOWN;
737 ret = pkgmgrinfo_appinfo_metadata_filter_add(handle, AOT_METADATA_KEY, METADATA_VALUE);
738 if (ret != PMINFO_R_OK) {
739 pkgmgrinfo_appinfo_metadata_filter_destroy(handle);
740 return NI_ERROR_UNKNOWN;
743 ret = pkgmgrinfo_appinfo_metadata_filter_foreach(handle, appAotCb, &flags);
744 if (ret != PMINFO_R_OK) {
745 fprintf(stderr, "Failed pkgmgrinfo_appinfo_metadata_filter_foreach\n");
746 pkgmgrinfo_appinfo_metadata_filter_destroy(handle);
747 return NI_ERROR_UNKNOWN;
750 fprintf(stderr, "Success pkgmgrinfo_appinfo_metadata_filter_foreach\n");
752 pkgmgrinfo_appinfo_metadata_filter_destroy(handle);
753 return NI_ERROR_NONE;
756 ni_error_e regenerateTACNI(DWORD flags)
758 const std::string tacDir[] = {__TAC_DIR};
759 removeNiUnderDirs(tacDir, 1);
760 createNiUnderTAC(tacDir, 1, flags);
761 return NI_ERROR_NONE;