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, bool enableR2R, bool isAppNI = false)
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 if (isAppNI && strstr(absNiPath.c_str(), __TAC_DIR) == NULL) {
244 absNiPath = getAppNIPath(absNiPath);
247 #ifdef UNIQUE_DEFAULT_BASE_ADDR_SUPPORT
248 uintptr_t baseAddr = 0;
250 if (isTPADll(dllPath)) {
251 baseAddr = getNextBaseAddr();
257 return NI_ERROR_UNKNOWN;
261 waitpid(pid, &status, 0);
262 if (WIFEXITED(status)) {
263 // Do not use niExist() function to check whether ni file created or not.
264 // niEixst() return false for System.Private.Corelib.dll
265 if (isFileExist(absNiPath)) {
266 updateAssemblyInfo(absDllPath, absNiPath);
267 #ifdef UNIQUE_DEFAULT_BASE_ADDR_SUPPORT
269 updateBaseAddrFile(absNiPath, baseAddr);
272 return NI_ERROR_NONE;
274 fprintf(stderr, "Fail to create native image for %s\n", dllPath.c_str());
275 return NI_ERROR_NO_SUCH_FILE;
279 std::string jitPath = getRuntimeDir() + "/libclrjit.so";
280 std::vector<const char*> argv = {
284 "/JITPath", jitPath.c_str()
288 argv.push_back("/FragileNonVersionable");
291 #ifdef UNIQUE_DEFAULT_BASE_ADDR_SUPPORT
293 argv.push_back("/BaseAddress");
294 argv.push_back(std::to_string(baseAddr).c_str());
298 argv.push_back("/App_Paths");
299 std::string absAppPath;
300 if (!appPath.empty()) {
301 absAppPath = appPath;
303 absAppPath = baseName(absDllPath);
305 argv.push_back(absAppPath.c_str());
307 argv.push_back("/out");
308 argv.push_back(absNiPath.c_str());
310 argv.push_back(absDllPath.c_str());
311 argv.push_back(nullptr);
313 fprintf(stderr, "+ %s (%s)\n", absDllPath.c_str(), enableR2R ? "R2R" : "FNV");
315 execv(__CROSSGEN_PATH, const_cast<char* const*>(argv.data()));
319 return NI_ERROR_NONE;
322 // callback function of "pkgmgrinfo_appinfo_metadata_filter_foreach"
323 static int appAotCb(pkgmgrinfo_appinfo_h handle, void *userData)
327 bool* enableR2R = (bool*)userData;
329 ret = pkgmgrinfo_appinfo_get_pkgid(handle, &pkgId);
330 if (ret != PMINFO_R_OK) {
331 fprintf(stderr, "Failed to get pkgid\n");
335 if (removeNiUnderPkgRoot(pkgId) != NI_ERROR_NONE) {
336 fprintf(stderr, "Failed to remove previous dlls from [%s]\n", pkgId);
340 if (resetTACPackage(pkgId) != TAC_ERROR_NONE) {
341 fprintf(stderr, "Failed to remove symlink for given package [%s]\n", pkgId);
345 // Regenerate ni files with R2R mode forcibiliy. (there is no way to now which option is used)
346 if (createNiUnderPkgRoot(pkgId, *enableR2R) != NI_ERROR_NONE) {
347 fprintf(stderr, "Failed to generate NI file [%s]\n", pkgId);
350 fprintf(stderr, "Complete make application to native image\n");
353 if (createTACPackage(pkgId) != TAC_ERROR_NONE) {
354 fprintf(stderr, "Failed to generate symbolic link file [%s]\n", pkgId);
357 fprintf(stderr, "Complete make symbolic link file to tac\n");
363 static void createCoreLibNI(bool enableR2R)
365 std::string coreLib = concatPath(getRuntimeDir(), "System.Private.CoreLib.dll");
366 std::string niCoreLib = concatPath(getRuntimeDir(), "System.Private.CoreLib.ni.dll");
367 std::string coreLibBackup = concatPath(getRuntimeDir(), "System.Private.CoreLib.dll.Backup");
369 if (!isFileExist(coreLibBackup)) {
371 if (!crossgen(coreLib, std::string(), enableR2R)) {
372 if (rename(coreLib.c_str(), coreLibBackup.c_str())) {
373 fprintf(stderr, "Failed to rename System.Private.CoreLib.dll\n");
375 if (rename(niCoreLib.c_str(), coreLib.c_str())) {
376 fprintf(stderr, "Failed to rename System.Private.CoreLib.ni.dll\n");
379 fprintf(stderr, "Failed to create native image for %s\n", coreLib.c_str());
384 ni_error_e initNICommon(NiCommonOption* option)
387 // get interval value
388 const char* intervalFile = "/usr/share/dotnet.tizen/lib/crossgen_interval.txt";
389 std::ifstream inFile(intervalFile);
391 fprintf(stderr, "crossgen_interval.txt is found\n");
392 inFile >> __interval;
395 if (initializePluginManager("normal")) {
396 fprintf(stderr, "Fail to initialize PluginManager\n");
397 return NI_ERROR_UNKNOWN;
399 if (initializePathManager(option->runtimeDir, option->tizenFXDir, option->extraDirs)) {
400 fprintf(stderr, "Fail to initialize PathManager\n");
401 return NI_ERROR_UNKNOWN;
406 return NI_ERROR_NONE;
408 fprintf(stderr, "crossgen supports arm architecture only. skip ni file generation\n");
409 return NI_ERROR_NOT_SUPPORTED;
413 void finalizeNICommon()
417 finalizePluginManager();
418 finalizePathManager();
424 void createNiPlatform(bool enableR2R)
426 const std::string platformDirs[] = {getRuntimeDir(), getTizenFXDir()};
427 createNiUnderDirs(platformDirs, 2, enableR2R);
430 ni_error_e createNiDll(const std::string& dllPath, bool enableR2R)
432 createCoreLibNI(enableR2R);
433 // System.Private.CoreLib.dll is generated in the createCoreLibNI function.
434 // Skip if input dll is System.Private.CoreLib.dll
435 if (dllPath.find("System.Private.CoreLib.dll") != std::string::npos) {
436 return NI_ERROR_NONE;
439 ni_error_e status = crossgen(dllPath, std::string(), enableR2R);
444 void createNiUnderTAC(const std::string rootPaths[], int count)
446 std::string appPaths;
448 for (auto& nuget : bf::recursive_directory_iterator(__TAC_DIR)) {
449 std::string nugetPath = nuget.path().string();
450 if (bf::is_directory(nugetPath)) {
451 appPaths += nugetPath;
455 if (appPaths.back() == ':') {
459 auto convert = [&appPaths](const std::string& path, const char* name) {
460 if (strstr(path.c_str(), TAC_APP_LIST_DB) != NULL ||
461 strstr(path.c_str(), TAC_APP_LIST_RESTORE_DB) != NULL ||
462 strstr(path.c_str(), TAC_SHA_256_INFO) != NULL)
464 if (!crossgen(path, appPaths.c_str(), false)) {
469 for (int i = 0; i < count; i++) {
470 scanFilesInDir(rootPaths[i], convert, -1);
472 } catch (const bf::filesystem_error& error) {
473 fprintf(stderr, "Failed to recursive directory: %s", error.what());
478 void createNiUnderDirs(const std::string rootPaths[], int count, bool enableR2R, bool isAppNI)
480 createCoreLibNI(enableR2R);
482 std::string appPaths;
483 for (int i = 0; i < count; i++) {
484 appPaths += rootPaths[i];
488 if (appPaths.back() == ':')
491 std::vector<std::string> tpaAssemblies;
492 splitPath(__tpa, tpaAssemblies);
494 auto convert = [&appPaths, enableR2R, isAppNI, tpaAssemblies](const std::string& path, const char* name) {
496 std::string assembly = path.substr(path.rfind('/') + 1);
497 bool isExist = false;
498 for (auto& tpa : tpaAssemblies) {
499 if (!strcmp(replaceAll(tpa, ".ni.dll", ".dll").c_str(), assembly.c_str())) {
505 fprintf(stderr, "%s present in the TPA list skips generation of NI file.\n", path.c_str());
509 if (!crossgen(path, appPaths.c_str(), enableR2R, isAppNI)) {
514 for (int i = 0; i < count; i++) {
515 scanFilesInDir(rootPaths[i], convert, 1);
518 tpaAssemblies.clear();
521 ni_error_e createNiUnderPkgRoot(const std::string& pkgId, bool enableR2R)
524 if (getRootPath(pkgId, pkgRoot) < 0) {
525 return NI_ERROR_INVALID_PACKAGE;
528 std::string binDir = concatPath(pkgRoot, "bin");
529 std::string libDir = concatPath(pkgRoot, "lib");
530 std::string tacDir = concatPath(binDir, TAC_SYMLINK_SUB_DIR);
531 std::string paths[] = {binDir, libDir, tacDir};
533 createNiUnderDirs(paths, 3, enableR2R, true);
535 return NI_ERROR_NONE;
538 ni_error_e createNiDllUnderPkgRoot(const std::string& pkgId, const std::string& dllPath, bool enableR2R)
541 if (getRootPath(pkgId, pkgRoot) < 0) {
542 return NI_ERROR_INVALID_PACKAGE;
545 std::string binDir = concatPath(pkgRoot, "bin");
546 std::string libDir = concatPath(pkgRoot, "lib");
547 std::string tacDir = concatPath(binDir, TAC_SYMLINK_SUB_DIR);
548 std::string paths = binDir + ":" + libDir + ":" + tacDir;
550 if (bf::is_symlink(dllPath)) {
551 if (bf::exists(tacDir)) {
552 if (!isNativeImage(dllPath)) {
553 std::string originPath = bf::read_symlink(dllPath).string();
554 std::string originNiPath = originPath.substr(0, originPath.rfind(".dll")) + ".ni.dll";
555 if (!bf::exists(originNiPath)) {
556 if(createNiDll(originPath, false) != NI_ERROR_NONE) {
557 fprintf(stderr, "Failed to create NI file [%s]\n", originPath.c_str());
558 return NI_ERROR_UNKNOWN;
561 std::string setNiPath = dllPath.substr(0, dllPath.rfind(".dll")) + ".ni.dll";
562 if (!bf::exists(setNiPath)) {
563 bf::create_symlink(originNiPath, setNiPath);
564 fprintf(stderr, "%s symbolic link file generated successfully.\n", setNiPath.c_str());
565 updateAssemblyInfo(tacDir.c_str(), setNiPath.c_str(), true);
569 return NI_ERROR_NONE;
571 std::string assembly = dllPath.substr(dllPath.rfind('/') + 1);
572 std::vector<std::string> tpaAssemblies;
573 splitPath(__tpa, tpaAssemblies);
574 bool isExist = false;
575 for (auto& tpa : tpaAssemblies) {
576 if (!strcmp(replaceAll(tpa, ".ni.dll", ".dll").c_str(), assembly.c_str())) {
581 tpaAssemblies.clear();
583 fprintf(stderr, "%s present in the TPA list skips generation of NI file.\n", dllPath.c_str());
584 return NI_ERROR_NONE;
586 return crossgen(dllPath, paths, enableR2R, true);
591 void removeNiPlatform()
593 std::string coreLib = concatPath(getRuntimeDir(), "System.Private.CoreLib.dll");
594 std::string coreLibBackup = concatPath(getRuntimeDir(), "System.Private.CoreLib.dll.Backup");
596 if (!isFileExist(coreLibBackup)) {
600 #ifdef UNIQUE_DEFAULT_BASE_ADDR_SUPPORT
601 if (isFileExist(__SYSTEM_BASE_FILE)) {
602 if (remove(__SYSTEM_BASE_FILE)) {
603 fprintf(stderr, "Failed to remove %s\n", __SYSTEM_BASE_FILE);
608 if (remove(coreLib.c_str())) {
609 fprintf(stderr, "Failed to remove System.Private.CoreLib native image file\n");
612 if (rename(coreLibBackup.c_str(), coreLib.c_str())) {
613 fprintf(stderr, "Failed to rename System.Private.CoreLib.Backup to origin\n");
616 const std::string platformDirs[] = {getRuntimeDir(), getTizenFXDir()};
618 removeNiUnderDirs(platformDirs, 2);
621 void removeNiUnderDirs(const std::string rootPaths[], int count)
623 auto convert = [](const std::string& path, std::string name) {
624 if (isNativeImage(path)) {
625 if (remove(path.c_str())) {
626 fprintf(stderr, "Failed to remove %s\n", path.c_str());
631 for (int i = 0; i < count; i++)
632 scanFilesInDir(rootPaths[i], convert, -1);
635 ni_error_e removeNiUnderPkgRoot(const std::string& pkgId)
638 if (getRootPath(pkgId, pkgRoot) < 0) {
639 return NI_ERROR_INVALID_PACKAGE;
642 std::string binDir = concatPath(pkgRoot, "bin");
643 std::string libDir = concatPath(pkgRoot, "lib");
644 std::string paths[] = {binDir, libDir};
646 removeNiUnderDirs(paths, 2);
648 std::string binNIDir = concatPath(binDir, APP_NI_SUB_DIR);
649 if (isFileExist(binNIDir)) {
650 if (rmdir(binNIDir.c_str()) != 0) {
651 fprintf(stderr, "Failed to remove app ni dir [%s]\n", binNIDir.c_str());
655 std::string libNIDir = concatPath(libDir, APP_NI_SUB_DIR);
656 if (isFileExist(libNIDir)) {
657 if (rmdir(libNIDir.c_str()) != 0) {
658 fprintf(stderr, "Failed to remove app ni dir [%s]\n", libNIDir.c_str());
662 return NI_ERROR_NONE;
665 ni_error_e regenerateAppNI(bool enableR2R)
668 pkgmgrinfo_appinfo_metadata_filter_h handle;
670 ret = pkgmgrinfo_appinfo_metadata_filter_create(&handle);
671 if (ret != PMINFO_R_OK)
672 return NI_ERROR_UNKNOWN;
674 ret = pkgmgrinfo_appinfo_metadata_filter_add(handle, AOT_METADATA_KEY, METADATA_VALUE);
675 if (ret != PMINFO_R_OK) {
676 pkgmgrinfo_appinfo_metadata_filter_destroy(handle);
677 return NI_ERROR_UNKNOWN;
680 ret = pkgmgrinfo_appinfo_metadata_filter_foreach(handle, appAotCb, &enableR2R);
681 if (ret != PMINFO_R_OK) {
682 fprintf(stderr, "Failed pkgmgrinfo_appinfo_metadata_filter_foreach\n");
683 pkgmgrinfo_appinfo_metadata_filter_destroy(handle);
684 return NI_ERROR_UNKNOWN;
687 fprintf(stderr, "Success pkgmgrinfo_appinfo_metadata_filter_foreach\n");
689 pkgmgrinfo_appinfo_metadata_filter_destroy(handle);
690 return NI_ERROR_NONE;