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);
56 #ifdef UNIQUE_DEFAULT_BASE_ADDR_SUPPORT
57 static const char* __SYSTEM_BASE_FILE = __STR(SYSTEM_BASE_FILE);
63 static int __interval = 0;
64 static std::string __tpa;
66 static void waitInterval()
68 // by the recommand, ignore small value for performance.
69 if (__interval > 10000) {
70 fprintf(stderr, "sleep %d usec\n", __interval);
75 static std::string getNiFilePath(const std::string& dllPath)
77 size_t index = dllPath.find_last_of(".");
78 if (index == std::string::npos) {
79 fprintf(stderr, "File doesnot contain extension. fail to get NI file name\n");
82 std::string fName = dllPath.substr(0, index);
83 std::string fExt = dllPath.substr(index, dllPath.length());
85 // crossgen generate file with lower case extension only
86 std::transform(fExt.begin(), fExt.end(), fExt.begin(), ::tolower);
87 std::string niPath = fName + ".ni" + fExt;
92 static std::string getAppNIPath(const std::string& niPath)
95 std::string niDirPath;
98 size_t index = niPath.find_last_of("/");
99 if (index != std::string::npos) {
100 prevPath = niPath.substr(0, index);
101 fileName = niPath.substr(index + 1, niPath.length());
107 niDirPath = concatPath(prevPath, APP_NI_SUB_DIR);
109 if (!isFileExist(niDirPath)) {
110 if (mkdir(niDirPath.c_str(), 0755) == 0) {
111 updateAssemblyInfo(prevPath, niDirPath);
113 fprintf(stderr, "Fail to create app ni directory (%s)\n", niDirPath.c_str());
117 return concatPath(niDirPath, fileName);
120 static bool niExist(const std::string& path)
122 std::string f = getNiFilePath(path);
127 if (isFileExist(f)) {
131 // native image of System.Private.CoreLib.dll should have to overwrite
132 // original file to support new coreclr
133 if (path.find("System.Private.CoreLib.dll") != std::string::npos) {
134 std::string coreLibBackup = path + ".Backup";
135 if (isFileExist(coreLibBackup)) {
143 #ifdef UNIQUE_DEFAULT_BASE_ADDR_SUPPORT
144 // Get next base address to be used for system ni image from file
145 // __SYSTEM_BASE_FILE should be checked for existance before calling this function
146 static uintptr_t getNextBaseAddrFromFile()
148 FILE *pFile = fopen(__SYSTEM_BASE_FILE, "r");
150 fprintf(stderr, "Failed to open %s\n", __SYSTEM_BASE_FILE);
157 while (fscanf(pFile, "%u %u", &addr, &size) != EOF) {
165 // Get next base address to be used for system ni image
166 static uintptr_t getNextBaseAddr()
168 uintptr_t baseAddr = 0;
170 if (!isFileExist(__SYSTEM_BASE_FILE)) {
171 // This is the starting address for all default base addresses
172 baseAddr = DEFAULT_BASE_ADDR_START;
174 baseAddr = getNextBaseAddrFromFile();
176 // Round to a multple of 64K (see ZapImage::CalculateZapBaseAddress in CoreCLR)
177 uintptr_t BASE_ADDRESS_ALIGNMENT = 0xffff;
178 baseAddr = (baseAddr + BASE_ADDRESS_ALIGNMENT) & ~BASE_ADDRESS_ALIGNMENT;
184 // Save base address of system ni image to file
185 static void updateBaseAddrFile(const std::string &absNiPath, uintptr_t baseAddr)
187 uintptr_t niSize = getFileSize(absNiPath);
189 fprintf(stderr, "File %s doesn't exist\n", absNiPath.c_str());
193 // Write new entry to the file
194 FILE *pFile = fopen(__SYSTEM_BASE_FILE, "a");
196 fprintf(stderr, "Failed to open %s\n", __SYSTEM_BASE_FILE);
200 fprintf(pFile, "%u %u\n", baseAddr, niSize);
204 // check if dll is listed in TPA
205 static bool isTPADll(const std::string &dllPath)
207 std::string absDllPath = absolutePath(dllPath);
209 if (__tpa.find(absDllPath) != std::string::npos) {
217 // baseAddr should be checked in file before getting here
218 static ni_error_e crossgen(const std::string& dllPath, const std::string& appPath, bool enableR2R, bool isAppNI = false)
220 if (!isFileExist(dllPath)) {
221 fprintf(stderr, "dll file is not exist : %s\n", dllPath.c_str());
222 return NI_ERROR_NO_SUCH_FILE;
225 if (!isManagedAssembly(dllPath)) {
226 fprintf(stderr, "Input file is not a dll file : %s\n", dllPath.c_str());
227 return NI_ERROR_INVALID_PARAMETER;
230 if (niExist(dllPath)) {
231 fprintf(stderr, "Already ni file is exist for %s\n", dllPath.c_str());
232 return NI_ERROR_ALREADY_EXIST;
235 std::string absDllPath = absolutePath(dllPath);
236 std::string absNiPath = getNiFilePath(dllPath);
237 if (absNiPath.empty()) {
238 fprintf(stderr, "Fail to get ni file name\n");
239 return NI_ERROR_UNKNOWN;
243 absNiPath = getAppNIPath(absNiPath);
246 #ifdef UNIQUE_DEFAULT_BASE_ADDR_SUPPORT
247 uintptr_t baseAddr = 0;
249 if (isTPADll(dllPath)) {
250 baseAddr = getNextBaseAddr();
256 return NI_ERROR_UNKNOWN;
260 waitpid(pid, &status, 0);
261 if (WIFEXITED(status)) {
262 // Do not use niExist() function to check whether ni file created or not.
263 // niEixst() return false for System.Private.Corelib.dll
264 if (isFileExist(absNiPath)) {
265 updateAssemblyInfo(absDllPath, absNiPath);
266 #ifdef UNIQUE_DEFAULT_BASE_ADDR_SUPPORT
268 updateBaseAddrFile(absNiPath, baseAddr);
271 return NI_ERROR_NONE;
273 fprintf(stderr, "Fail to create native image for %s\n", dllPath.c_str());
274 return NI_ERROR_NO_SUCH_FILE;
278 std::string jitPath = getRuntimeDir() + "/libclrjit.so";
279 std::vector<const char*> argv = {
282 "/Trusted_Platform_Assemblies", __tpa.c_str(),
283 "/JITPath", jitPath.c_str()
287 argv.push_back("/FragileNonVersionable");
290 #ifdef UNIQUE_DEFAULT_BASE_ADDR_SUPPORT
292 argv.push_back("/BaseAddress");
293 argv.push_back(std::to_string(baseAddr).c_str());
297 argv.push_back("/App_Paths");
298 std::string absAppPath;
299 if (!appPath.empty()) {
300 absAppPath = appPath;
302 absAppPath = baseName(absDllPath);
304 argv.push_back(absAppPath.c_str());
306 argv.push_back("/out");
307 argv.push_back(absNiPath.c_str());
309 argv.push_back(absDllPath.c_str());
310 argv.push_back(nullptr);
312 fprintf(stderr, "+ %s (%s)\n", absDllPath.c_str(), enableR2R ? "R2R" : "FNV");
314 execv(__CROSSGEN_PATH, const_cast<char* const*>(argv.data()));
318 return NI_ERROR_NONE;
321 // callback function of "pkgmgrinfo_appinfo_metadata_filter_foreach"
322 static int appAotCb(pkgmgrinfo_appinfo_h handle, void *userData)
326 bool* enableR2R = (bool*)userData;
328 ret = pkgmgrinfo_appinfo_get_pkgid(handle, &pkgId);
329 if (ret != PMINFO_R_OK) {
330 fprintf(stderr, "Failed to get pkgid\n");
334 if (removeNiUnderPkgRoot(pkgId) != NI_ERROR_NONE) {
335 fprintf(stderr, "Failed to remove previous dlls from [%s]\n", pkgId);
339 if (resetTACPackage(pkgId) != TAC_ERROR_NONE) {
340 fprintf(stderr, "Failed to remove symlink for given package [%s]\n", pkgId);
344 // Regenerate ni files with R2R mode forcibiliy. (there is no way to now which option is used)
345 if (createNiUnderPkgRoot(pkgId, *enableR2R) != NI_ERROR_NONE) {
346 fprintf(stderr, "Failed to generate NI file [%s]\n", pkgId);
349 fprintf(stderr, "Complete make application to native image\n");
352 if (createTACPackage(pkgId) != TAC_ERROR_NONE) {
353 fprintf(stderr, "Failed to generate symbolic link file [%s]\n", pkgId);
356 fprintf(stderr, "Complete make symbolic link file to tac\n");
362 static void createCoreLibNI(bool enableR2R)
364 std::string coreLib = concatPath(getRuntimeDir(), "System.Private.CoreLib.dll");
365 std::string niCoreLib = concatPath(getRuntimeDir(), "System.Private.CoreLib.ni.dll");
366 std::string coreLibBackup = concatPath(getRuntimeDir(), "System.Private.CoreLib.dll.Backup");
368 if (!isFileExist(coreLibBackup)) {
370 if (!crossgen(coreLib, std::string(), enableR2R)) {
371 if (rename(coreLib.c_str(), coreLibBackup.c_str())) {
372 fprintf(stderr, "Failed to rename System.Private.CoreLib.dll\n");
374 if (rename(niCoreLib.c_str(), coreLib.c_str())) {
375 fprintf(stderr, "Failed to rename System.Private.CoreLib.ni.dll\n");
378 fprintf(stderr, "Failed to create native image for %s\n", coreLib.c_str());
383 ni_error_e initNICommon(NiCommonOption* option)
386 // get interval value
387 const char* intervalFile = "/usr/share/dotnet.tizen/lib/crossgen_interval.txt";
388 std::ifstream inFile(intervalFile);
390 fprintf(stderr, "crossgen_interval.txt is found\n");
391 inFile >> __interval;
394 if (initializePluginManager("normal")) {
395 fprintf(stderr, "Fail to initialize plugin manager\n");
396 return NI_ERROR_UNKNOWN;
398 if (initializePathManager(option->runtimeDir, option->tizenFXDir, option->extraDirs)) {
399 fprintf(stderr, "Fail to initialize path manager\n");
400 return NI_ERROR_UNKNOWN;
405 return NI_ERROR_NONE;
407 fprintf(stderr, "crossgen supports arm architecture only. skip ni file generation\n");
408 return NI_ERROR_NOT_SUPPORTED;
412 void finalizeNICommon()
416 finalizePluginManager();
417 finalizePathManager();
423 void createNiPlatform(bool enableR2R)
425 const std::string platformDirs[] = {getRuntimeDir(), getTizenFXDir()};
426 createNiUnderDirs(platformDirs, 2, enableR2R);
429 ni_error_e createNiDll(const std::string& dllPath, bool enableR2R)
431 createCoreLibNI(enableR2R);
432 // System.Private.CoreLib.dll is generated in the createCoreLibNI function.
433 // Skip if input dll is System.Private.CoreLib.dll
434 if (dllPath.find("System.Private.CoreLib.dll") != std::string::npos) {
435 return NI_ERROR_NONE;
438 ni_error_e status = crossgen(dllPath, std::string(), enableR2R);
443 void createNiUnderDirs(const std::string rootPaths[], int count, bool enableR2R, bool isAppNI)
445 createCoreLibNI(enableR2R);
447 std::string appPaths;
448 for (int i = 0; i < count; i++) {
449 appPaths += rootPaths[i];
453 if (appPaths.back() == ':')
456 auto convert = [&appPaths, enableR2R, isAppNI](const std::string& path, const char* name) {
457 if (!crossgen(path, appPaths.c_str(), enableR2R, isAppNI)) {
462 for (int i = 0; i < count; i++) {
463 scanFilesInDir(rootPaths[i], convert, 1);
467 ni_error_e createNiUnderPkgRoot(const std::string& pkgId, bool enableR2R)
470 if (getRootPath(pkgId, pkgRoot) < 0) {
471 return NI_ERROR_INVALID_PACKAGE;
474 std::string binDir = concatPath(pkgRoot, "bin");
475 std::string libDir = concatPath(pkgRoot, "lib");
476 std::string tacDir = concatPath(binDir, TAC_SYMLINK_SUB_DIR);
477 std::string paths[] = {binDir, libDir, tacDir};
479 createNiUnderDirs(paths, 3, enableR2R, true);
481 return NI_ERROR_NONE;
484 ni_error_e createNiDllUnderPkgRoot(const std::string& pkgId, const std::string& dllPath, bool enableR2R)
487 if (getRootPath(pkgId, pkgRoot) < 0) {
488 return NI_ERROR_INVALID_PACKAGE;
491 std::string binDir = concatPath(pkgRoot, "bin");
492 std::string libDir = concatPath(pkgRoot, "lib");
493 std::string tacDir = concatPath(binDir, TAC_SYMLINK_SUB_DIR);
494 std::string paths = binDir + ":" + libDir + ":" + tacDir;
496 if (bf::is_symlink(dllPath)) {
497 if (bf::exists(tacDir)) {
498 if (!isNativeImage(dllPath)) {
499 std::string originPath = bf::read_symlink(dllPath).string();
500 std::string originNiPath = originPath.substr(0, originPath.rfind(".dll")) + ".ni.dll";
501 if (!bf::exists(originNiPath)) {
502 if(createNiDll(originPath, false) != NI_ERROR_NONE) {
503 fprintf(stderr, "Failed to create NI file [%s]\n", originPath.c_str());
504 return NI_ERROR_UNKNOWN;
507 std::string setNiPath = dllPath.substr(0, dllPath.rfind(".dll")) + ".ni.dll";
508 if (!bf::exists(setNiPath)) {
509 bf::create_symlink(originNiPath, setNiPath);
510 fprintf(stderr, "%s symbolic link file generated successfully.\n", setNiPath.c_str());
511 updateAssemblyInfo(tacDir.c_str(), setNiPath.c_str(), true);
515 return NI_ERROR_NONE;
517 return crossgen(dllPath, paths, enableR2R, true);
521 void removeNiPlatform()
523 std::string coreLib = concatPath(getRuntimeDir(), "System.Private.CoreLib.dll");
524 std::string coreLibBackup = concatPath(getRuntimeDir(), "System.Private.CoreLib.dll.Backup");
526 if (!isFileExist(coreLibBackup)) {
530 #ifdef UNIQUE_DEFAULT_BASE_ADDR_SUPPORT
531 if (isFileExist(__SYSTEM_BASE_FILE)) {
532 if (remove(__SYSTEM_BASE_FILE)) {
533 fprintf(stderr, "Failed to remove %s\n", __SYSTEM_BASE_FILE);
538 if (remove(coreLib.c_str())) {
539 fprintf(stderr, "Failed to remove System.Private.CoreLib native image file\n");
542 if (rename(coreLibBackup.c_str(), coreLib.c_str())) {
543 fprintf(stderr, "Failed to rename System.Private.CoreLib.Backup to origin\n");
546 const std::string platformDirs[] = {getRuntimeDir(), getTizenFXDir()};
548 removeNiUnderDirs(platformDirs, 2);
551 void removeNiUnderDirs(const std::string rootPaths[], int count)
553 auto convert = [](const std::string& path, std::string name) {
554 if (isNativeImage(path)) {
555 if (remove(path.c_str())) {
556 fprintf(stderr, "Failed to remove %s\n", path.c_str());
561 for (int i = 0; i < count; i++)
562 scanFilesInDir(rootPaths[i], convert, -1);
565 ni_error_e removeNiUnderPkgRoot(const std::string& pkgId)
568 if (getRootPath(pkgId, pkgRoot) < 0) {
569 return NI_ERROR_INVALID_PACKAGE;
572 std::string binDir = concatPath(pkgRoot, "bin");
573 std::string libDir = concatPath(pkgRoot, "lib");
574 std::string paths[] = {binDir, libDir};
576 removeNiUnderDirs(paths, 2);
578 std::string binNIDir = concatPath(binDir, APP_NI_SUB_DIR);
579 if (isFileExist(binNIDir)) {
580 if (rmdir(binNIDir.c_str()) != 0) {
581 fprintf(stderr, "Failed to remove app ni dir [%s]\n", binNIDir.c_str());
585 std::string libNIDir = concatPath(libDir, APP_NI_SUB_DIR);
586 if (isFileExist(libNIDir)) {
587 if (rmdir(libNIDir.c_str()) != 0) {
588 fprintf(stderr, "Failed to remove app ni dir [%s]\n", libNIDir.c_str());
592 return NI_ERROR_NONE;
595 ni_error_e regenerateAppNI(bool enableR2R)
598 pkgmgrinfo_appinfo_metadata_filter_h handle;
600 ret = pkgmgrinfo_appinfo_metadata_filter_create(&handle);
601 if (ret != PMINFO_R_OK)
602 return NI_ERROR_UNKNOWN;
604 ret = pkgmgrinfo_appinfo_metadata_filter_add(handle, AOT_METADATA_KEY, METADATA_VALUE);
605 if (ret != PMINFO_R_OK) {
606 pkgmgrinfo_appinfo_metadata_filter_destroy(handle);
607 return NI_ERROR_UNKNOWN;
610 ret = pkgmgrinfo_appinfo_metadata_filter_foreach(handle, appAotCb, &enableR2R);
611 if (ret != PMINFO_R_OK) {
612 fprintf(stderr, "Failed pkgmgrinfo_appinfo_metadata_filter_foreach\n");
613 pkgmgrinfo_appinfo_metadata_filter_destroy(handle);
614 return NI_ERROR_UNKNOWN;
617 fprintf(stderr, "Success pkgmgrinfo_appinfo_metadata_filter_foreach\n");
619 pkgmgrinfo_appinfo_metadata_filter_destroy(handle);
620 return NI_ERROR_NONE;