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 (createTACPackage(pkgId, *pFlags) != TAC_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", error.what());
491 void createNiUnderDirs(const std::string rootPaths[], int count, DWORD flags)
493 createCoreLibNI(flags);
495 std::string appPaths;
496 for (int i = 0; i < count; i++) {
497 appPaths += rootPaths[i];
501 if (appPaths.back() == ':')
504 std::vector<std::string> tpaAssemblies;
505 splitPath(__tpa, tpaAssemblies);
507 auto convert = [&appPaths, flags, tpaAssemblies](const std::string& path, const char* name) {
508 bool isAppNI = flags & NI_FLAGS_APPNI;
510 std::string assembly = path.substr(path.rfind('/') + 1);
511 bool isExist = false;
512 for (auto& tpa : tpaAssemblies) {
513 if (!strcmp(replaceAll(tpa, ".ni.dll", ".dll").c_str(), assembly.c_str())) {
519 fprintf(stderr, "%s present in the TPA list skips generation of NI file.\n", path.c_str());
523 if (!crossgen(path, appPaths.c_str(), flags)) {
528 for (int i = 0; i < count; i++) {
529 scanFilesInDir(rootPaths[i], convert, 1);
532 tpaAssemblies.clear();
535 ni_error_e createNiUnderPkgRoot(const std::string& pkgId, DWORD flags)
538 if (getRootPath(pkgId, pkgRoot) < 0) {
539 return NI_ERROR_INVALID_PACKAGE;
542 std::string binDir = concatPath(pkgRoot, "bin");
543 std::string libDir = concatPath(pkgRoot, "lib");
544 std::string tacDir = concatPath(binDir, TAC_SYMLINK_SUB_DIR);
545 std::string paths[] = {binDir, libDir, tacDir};
547 flags |= NI_FLAGS_APPNI;
548 createNiUnderDirs(paths, 3, flags);
550 return NI_ERROR_NONE;
553 ni_error_e createNiDllUnderPkgRoot(const std::string& pkgId, const std::string& dllPath, DWORD flags)
556 if (getRootPath(pkgId, pkgRoot) < 0) {
557 return NI_ERROR_INVALID_PACKAGE;
560 std::string binDir = concatPath(pkgRoot, "bin");
561 std::string libDir = concatPath(pkgRoot, "lib");
562 std::string tacDir = concatPath(binDir, TAC_SYMLINK_SUB_DIR);
563 std::string paths = binDir + ":" + libDir + ":" + tacDir;
565 if (bf::is_symlink(dllPath)) {
566 if (bf::exists(tacDir)) {
567 if (!isNativeImage(dllPath)) {
568 std::string originPath = bf::read_symlink(dllPath).string();
569 std::string originNiPath = originPath.substr(0, originPath.rfind(".dll")) + ".ni.dll";
570 if (!bf::exists(originNiPath)) {
571 if(createNiDll(originPath, flags) != NI_ERROR_NONE) {
572 fprintf(stderr, "Failed to create NI file [%s]\n", originPath.c_str());
573 return NI_ERROR_UNKNOWN;
576 std::string setNiPath = dllPath.substr(0, dllPath.rfind(".dll")) + ".ni.dll";
577 if (!bf::exists(setNiPath)) {
578 bf::create_symlink(originNiPath, setNiPath);
579 fprintf(stderr, "%s symbolic link file generated successfully.\n", setNiPath.c_str());
580 updateAssemblyInfo(tacDir.c_str(), setNiPath.c_str(), true);
584 return NI_ERROR_NONE;
586 std::string assembly = dllPath.substr(dllPath.rfind('/') + 1);
587 std::vector<std::string> tpaAssemblies;
588 splitPath(__tpa, tpaAssemblies);
589 bool isExist = false;
590 for (auto& tpa : tpaAssemblies) {
591 if (!strcmp(replaceAll(tpa, ".ni.dll", ".dll").c_str(), assembly.c_str())) {
596 tpaAssemblies.clear();
598 fprintf(stderr, "%s present in the TPA list skips generation of NI file.\n", dllPath.c_str());
599 return NI_ERROR_NONE;
601 flags |= NI_FLAGS_APPNI;
602 return crossgen(dllPath, paths, flags);
607 void removeNiPlatform()
609 std::string coreLib = concatPath(getRuntimeDir(), "System.Private.CoreLib.dll");
610 std::string coreLibBackup = concatPath(getRuntimeDir(), "System.Private.CoreLib.dll.Backup");
612 if (!isFileExist(coreLibBackup)) {
616 #ifdef UNIQUE_DEFAULT_BASE_ADDR_SUPPORT
617 if (isFileExist(__SYSTEM_BASE_FILE)) {
618 if (remove(__SYSTEM_BASE_FILE)) {
619 fprintf(stderr, "Failed to remove %s\n", __SYSTEM_BASE_FILE);
624 if (remove(coreLib.c_str())) {
625 fprintf(stderr, "Failed to remove System.Private.CoreLib native image file\n");
628 if (rename(coreLibBackup.c_str(), coreLib.c_str())) {
629 fprintf(stderr, "Failed to rename System.Private.CoreLib.Backup to origin\n");
632 const std::string platformDirs[] = {getRuntimeDir(), getTizenFXDir()};
634 removeNiUnderDirs(platformDirs, 2);
637 void removeNiUnderDirs(const std::string rootPaths[], int count)
639 auto convert = [](const std::string& path, std::string name) {
640 if (isNativeImage(path)) {
641 if (remove(path.c_str())) {
642 fprintf(stderr, "Failed to remove %s\n", path.c_str());
647 for (int i = 0; i < count; i++)
648 scanFilesInDir(rootPaths[i], convert, -1);
651 ni_error_e removeNiUnderPkgRoot(const std::string& pkgId)
654 if (getRootPath(pkgId, pkgRoot) < 0) {
655 return NI_ERROR_INVALID_PACKAGE;
658 std::string binDir = concatPath(pkgRoot, "bin");
659 std::string libDir = concatPath(pkgRoot, "lib");
660 std::string paths[] = {binDir, libDir};
662 removeNiUnderDirs(paths, 2);
664 std::string binNIDir = concatPath(binDir, APP_NI_SUB_DIR);
665 if (isFileExist(binNIDir)) {
666 if (rmdir(binNIDir.c_str()) != 0) {
667 fprintf(stderr, "Failed to remove app ni dir [%s]\n", binNIDir.c_str());
671 std::string libNIDir = concatPath(libDir, APP_NI_SUB_DIR);
672 if (isFileExist(libNIDir)) {
673 if (rmdir(libNIDir.c_str()) != 0) {
674 fprintf(stderr, "Failed to remove app ni dir [%s]\n", libNIDir.c_str());
678 return NI_ERROR_NONE;
681 ni_error_e regenerateAppNI(DWORD flags)
684 pkgmgrinfo_appinfo_metadata_filter_h handle;
686 ret = pkgmgrinfo_appinfo_metadata_filter_create(&handle);
687 if (ret != PMINFO_R_OK)
688 return NI_ERROR_UNKNOWN;
690 ret = pkgmgrinfo_appinfo_metadata_filter_add(handle, AOT_METADATA_KEY, METADATA_VALUE);
691 if (ret != PMINFO_R_OK) {
692 pkgmgrinfo_appinfo_metadata_filter_destroy(handle);
693 return NI_ERROR_UNKNOWN;
696 ret = pkgmgrinfo_appinfo_metadata_filter_foreach(handle, appAotCb, &flags);
697 if (ret != PMINFO_R_OK) {
698 fprintf(stderr, "Failed pkgmgrinfo_appinfo_metadata_filter_foreach\n");
699 pkgmgrinfo_appinfo_metadata_filter_destroy(handle);
700 return NI_ERROR_UNKNOWN;
703 fprintf(stderr, "Success pkgmgrinfo_appinfo_metadata_filter_foreach\n");
705 pkgmgrinfo_appinfo_metadata_filter_destroy(handle);
706 return NI_ERROR_NONE;