LibraryLoader.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. #include "il2cpp-config.h"
  2. #if IL2CPP_TARGET_WINDOWS
  3. #include "il2cpp-runtime-metadata.h"
  4. #include "os/LibraryLoader.h"
  5. #include "os/Image.h"
  6. #include "utils/StringUtils.h"
  7. #include "WindowsHelpers.h"
  8. #include "Evntprov.h"
  9. #define WINNT // All functions in Evntrace.h are under this define.. Why? I have no idea!
  10. #include "Evntrace.h"
  11. #if IL2CPP_TARGET_WINDOWS_GAMES
  12. #include "os/WindowsGames/Win32ApiWindowsGamesEmulation.h"
  13. #endif
  14. #include <bcrypt.h>
  15. namespace il2cpp
  16. {
  17. namespace os
  18. {
  19. const HardcodedPInvokeDependencyFunction kAdvapiFunctions[] =
  20. {
  21. HARDCODED_DEPENDENCY_FUNCTION(EnumerateTraceGuidsEx),
  22. HARDCODED_DEPENDENCY_FUNCTION(EventActivityIdControl),
  23. HARDCODED_DEPENDENCY_FUNCTION(EventRegister),
  24. HARDCODED_DEPENDENCY_FUNCTION(EventSetInformation),
  25. HARDCODED_DEPENDENCY_FUNCTION(EventUnregister),
  26. HARDCODED_DEPENDENCY_FUNCTION(EventWrite),
  27. HARDCODED_DEPENDENCY_FUNCTION(EventWriteEx),
  28. HARDCODED_DEPENDENCY_FUNCTION(EventWriteString),
  29. HARDCODED_DEPENDENCY_FUNCTION(EventWriteTransfer),
  30. };
  31. const HardcodedPInvokeDependencyFunction kKernel32Functions[] =
  32. {
  33. HARDCODED_DEPENDENCY_FUNCTION(FormatMessageW),
  34. HARDCODED_DEPENDENCY_FUNCTION(GetCurrentProcessId),
  35. HARDCODED_DEPENDENCY_FUNCTION(GetDynamicTimeZoneInformation),
  36. HARDCODED_DEPENDENCY_FUNCTION(GetNativeSystemInfo),
  37. HARDCODED_DEPENDENCY_FUNCTION(GetTimeZoneInformation),
  38. HARDCODED_DEPENDENCY_FUNCTION(GetFullPathNameW),
  39. HARDCODED_DEPENDENCY_FUNCTION(GetFileAttributesExW),
  40. HARDCODED_DEPENDENCY_FUNCTION(CreateDirectoryW),
  41. HARDCODED_DEPENDENCY_FUNCTION(CloseHandle),
  42. HARDCODED_DEPENDENCY_FUNCTION(CreateFileW),
  43. HARDCODED_DEPENDENCY_FUNCTION(DeleteFileW),
  44. HARDCODED_DEPENDENCY_FUNCTION(FindFirstFileExW),
  45. HARDCODED_DEPENDENCY_FUNCTION(FindNextFileW),
  46. HARDCODED_DEPENDENCY_FUNCTION(MoveFileExW),
  47. HARDCODED_DEPENDENCY_FUNCTION(MultiByteToWideChar),
  48. HARDCODED_DEPENDENCY_FUNCTION(RemoveDirectoryW),
  49. HARDCODED_DEPENDENCY_FUNCTION(ReplaceFileW),
  50. HARDCODED_DEPENDENCY_FUNCTION(SetFileAttributesW),
  51. HARDCODED_DEPENDENCY_FUNCTION(SetFileInformationByHandle),
  52. HARDCODED_DEPENDENCY_FUNCTION(GetFileInformationByHandleEx),
  53. HARDCODED_DEPENDENCY_FUNCTION(CopyFile2),
  54. HARDCODED_DEPENDENCY_FUNCTION(SetThreadErrorMode),
  55. HARDCODED_DEPENDENCY_FUNCTION(GetCurrentThread),
  56. HARDCODED_DEPENDENCY_FUNCTION(GetThreadDescription),
  57. HARDCODED_DEPENDENCY_FUNCTION(CopyFileExW),
  58. HARDCODED_DEPENDENCY_FUNCTION(DeleteVolumeMountPointW),
  59. HARDCODED_DEPENDENCY_FUNCTION(GetLogicalDrives),
  60. HARDCODED_DEPENDENCY_FUNCTION(LocalAlloc),
  61. HARDCODED_DEPENDENCY_FUNCTION(LocalReAlloc),
  62. HARDCODED_DEPENDENCY_FUNCTION(LocalFree),
  63. };
  64. const HardcodedPInvokeDependencyFunction kBCryptFunctions[] =
  65. {
  66. HARDCODED_DEPENDENCY_FUNCTION(BCryptGenRandom),
  67. };
  68. const HardcodedPInvokeDependencyFunction kiphlpapiFunctions[] =
  69. {
  70. HARDCODED_DEPENDENCY_FUNCTION(GetNetworkParams),
  71. HARDCODED_DEPENDENCY_FUNCTION(GetAdaptersAddresses),
  72. HARDCODED_DEPENDENCY_FUNCTION(GetIfEntry),
  73. };
  74. #if !IL2CPP_TARGET_WINDOWS_GAMES
  75. const HardcodedPInvokeDependencyFunction kTimezoneFunctions[] =
  76. {
  77. HARDCODED_DEPENDENCY_FUNCTION(EnumDynamicTimeZoneInformation),
  78. HARDCODED_DEPENDENCY_FUNCTION(GetDynamicTimeZoneInformation),
  79. HARDCODED_DEPENDENCY_FUNCTION(GetDynamicTimeZoneInformationEffectiveYears),
  80. HARDCODED_DEPENDENCY_FUNCTION(GetTimeZoneInformationForYear),
  81. };
  82. const HardcodedPInvokeDependencyFunction kWinTypesFunctions[] =
  83. {
  84. HARDCODED_DEPENDENCY_FUNCTION(RoGetBufferMarshaler)
  85. };
  86. #endif
  87. // All these come without ".dll" extension!
  88. const HardcodedPInvokeDependencyLibrary kHardcodedPInvokeDependencies[] =
  89. {
  90. #if IL2CPP_TARGET_WINDOWS_GAMES
  91. HARDCODED_DEPENDENCY_LIBRARY(L"bcrypt", kBCryptFunctions),
  92. #else
  93. HARDCODED_DEPENDENCY_LIBRARY(L"advapi32", kAdvapiFunctions),
  94. HARDCODED_DEPENDENCY_LIBRARY(L"api-ms-win-core-timezone-l1-1-0", kTimezoneFunctions),
  95. HARDCODED_DEPENDENCY_LIBRARY(L"kernel32", kKernel32Functions),
  96. HARDCODED_DEPENDENCY_LIBRARY(L"iphlpapi", kiphlpapiFunctions),
  97. HARDCODED_DEPENDENCY_LIBRARY(L"wintypes", kWinTypesFunctions),
  98. HARDCODED_DEPENDENCY_LIBRARY(L"bcrypt", kBCryptFunctions),
  99. #endif
  100. };
  101. const HardcodedPInvokeDependencyLibrary* LibraryLoader::HardcodedPInvokeDependencies = kHardcodedPInvokeDependencies;
  102. const size_t LibraryLoader::HardcodedPInvokeDependenciesCount = ARRAYSIZE(kHardcodedPInvokeDependencies);
  103. Baselib_DynamicLibrary_Handle LibraryLoader::ProbeForLibrary(const Il2CppNativeChar* libraryName, const size_t /*libraryNameLength*/, std::string& detailedError)
  104. {
  105. return TryOpeningLibrary(libraryName, detailedError);
  106. }
  107. Baselib_DynamicLibrary_Handle LibraryLoader::OpenProgramHandle(Baselib_ErrorState& errorState, bool& needsClosing)
  108. {
  109. needsClosing = false;
  110. return Baselib_DynamicLibrary_FromNativeHandle(reinterpret_cast<uint64_t>(Image::GetImageBase()), Baselib_DynamicLibrary_WinApiHMODULE, &errorState);
  111. }
  112. bool LibraryLoader::EntryNameMatches(const il2cpp::utils::StringView<char>& hardcodedEntryPoint, const il2cpp::utils::StringView<char>& entryPoint)
  113. {
  114. // Handle windows mapping generic to unicode methods. e.g. MoveFileEx -> MoveFileExW
  115. if (hardcodedEntryPoint.Length() == entryPoint.Length() || (hardcodedEntryPoint.Length() - 1 == entryPoint.Length() && hardcodedEntryPoint[hardcodedEntryPoint.Length() - 1] == 'W'))
  116. {
  117. return strncmp(hardcodedEntryPoint.Str(), entryPoint.Str(), entryPoint.Length()) == 0;
  118. }
  119. return false;
  120. }
  121. }
  122. }
  123. #endif