DelayLoadLibrary.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  1. //-------------------------------------------------------------------------------------------------------
  2. // Copyright (C) Microsoft. All rights reserved.
  3. // Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
  4. //-------------------------------------------------------------------------------------------------------
  5. #include "RuntimeBasePch.h"
  6. #ifdef _CONTROL_FLOW_GUARD
  7. #if !defined(DELAYLOAD_SET_CFG_TARGET)
  8. extern "C"
  9. WINBASEAPI
  10. BOOL
  11. WINAPI
  12. SetProcessValidCallTargets(
  13. _In_ HANDLE hProcess,
  14. _In_ PVOID VirtualAddress,
  15. _In_ SIZE_T RegionSize,
  16. _In_ ULONG NumberOfOffsets,
  17. _In_reads_(NumberOfOffsets) PCFG_CALL_TARGET_INFO OffsetInformation
  18. );
  19. #endif
  20. #endif
  21. namespace Js
  22. {
  23. HRESULT DelayLoadWinRtString::WindowsCreateString(_In_reads_opt_(length) const WCHAR * sourceString, UINT32 length, _Outptr_result_maybenull_ _Result_nullonfailure_ HSTRING * string)
  24. {
  25. if (m_hModule)
  26. {
  27. if (m_pfnWindowsCreateString == nullptr)
  28. {
  29. m_pfnWindowsCreateString = (PFNCWindowsCreateString)GetFunction("WindowsCreateString");
  30. if (m_pfnWindowsCreateString == nullptr)
  31. {
  32. *string = nullptr;
  33. return E_UNEXPECTED;
  34. }
  35. }
  36. Assert(m_pfnWindowsCreateString != nullptr);
  37. return m_pfnWindowsCreateString(sourceString, length, string);
  38. }
  39. *string = nullptr;
  40. return E_NOTIMPL;
  41. }
  42. HRESULT DelayLoadWinRtString::WindowsCreateStringReference(_In_reads_opt_(length + 1) const WCHAR *sourceString, UINT32 length, _Out_ HSTRING_HEADER *hstringHeader, _Outptr_result_maybenull_ _Result_nullonfailure_ HSTRING * string)
  43. {
  44. if (m_hModule)
  45. {
  46. if (m_pfnWindowsCreateStringReference == nullptr)
  47. {
  48. m_pfnWindowsCreateStringReference = (PFNCWindowsCreateStringReference)GetFunction("WindowsCreateStringReference");
  49. if (m_pfnWindowsCreateStringReference == nullptr)
  50. {
  51. *string = nullptr;
  52. return E_UNEXPECTED;
  53. }
  54. }
  55. Assert(m_pfnWindowsCreateStringReference != nullptr);
  56. return m_pfnWindowsCreateStringReference(sourceString, length, hstringHeader, string);
  57. }
  58. *string = nullptr;
  59. return E_NOTIMPL;
  60. }
  61. HRESULT DelayLoadWinRtString::WindowsDeleteString(_In_opt_ HSTRING string)
  62. {
  63. if (m_hModule)
  64. {
  65. if (m_pfnWindowsDeleteString == nullptr)
  66. {
  67. m_pfnWindowsDeleteString = (PFNCWindowsDeleteString)GetFunction("WindowsDeleteString");
  68. if (m_pfnWindowsDeleteString == nullptr)
  69. {
  70. return E_UNEXPECTED;
  71. }
  72. }
  73. Assert(m_pfnWindowsDeleteString != nullptr);
  74. HRESULT hr = m_pfnWindowsDeleteString(string);
  75. Assert(SUCCEEDED(hr));
  76. return hr;
  77. }
  78. return E_NOTIMPL;
  79. }
  80. PCWSTR DelayLoadWinRtString::WindowsGetStringRawBuffer(_In_opt_ HSTRING string, _Out_opt_ UINT32 * length)
  81. {
  82. if (m_hModule)
  83. {
  84. if (m_pfWindowsGetStringRawBuffer == nullptr)
  85. {
  86. m_pfWindowsGetStringRawBuffer = (PFNCWindowsGetStringRawBuffer)GetFunction("WindowsGetStringRawBuffer");
  87. if (m_pfWindowsGetStringRawBuffer == nullptr)
  88. {
  89. if (length)
  90. {
  91. *length = 0;
  92. }
  93. return L"\0";
  94. }
  95. }
  96. Assert(m_pfWindowsGetStringRawBuffer != nullptr);
  97. return m_pfWindowsGetStringRawBuffer(string, length);
  98. }
  99. if (length)
  100. {
  101. *length = 0;
  102. }
  103. return L"\0";
  104. }
  105. HRESULT DelayLoadWinRtString::WindowsCompareStringOrdinal(_In_opt_ HSTRING string1, _In_opt_ HSTRING string2, _Out_ INT32 * result)
  106. {
  107. if (m_hModule)
  108. {
  109. if (m_pfnWindowsCompareStringOrdinal == nullptr)
  110. {
  111. m_pfnWindowsCompareStringOrdinal = (PFNCWindowsCompareStringOrdinal)GetFunction("WindowsCompareStringOrdinal");
  112. if (m_pfnWindowsCompareStringOrdinal == nullptr)
  113. {
  114. return E_UNEXPECTED;
  115. }
  116. }
  117. Assert(m_pfnWindowsCompareStringOrdinal != nullptr);
  118. return m_pfnWindowsCompareStringOrdinal(string1,string2,result);
  119. }
  120. return E_NOTIMPL;
  121. }
  122. HRESULT DelayLoadWinRtString::WindowsDuplicateString(_In_opt_ HSTRING original, _Outptr_result_maybenull_ _Result_nullonfailure_ HSTRING *newString)
  123. {
  124. if(m_hModule)
  125. {
  126. if(m_pfnWindowsDuplicateString == nullptr)
  127. {
  128. m_pfnWindowsDuplicateString = (PFNCWindowsDuplicateString)GetFunction("WindowsDuplicateString");
  129. if(m_pfnWindowsDuplicateString == nullptr)
  130. {
  131. *newString = nullptr;
  132. return E_UNEXPECTED;
  133. }
  134. }
  135. Assert(m_pfnWindowsDuplicateString != nullptr);
  136. return m_pfnWindowsDuplicateString(original, newString);
  137. }
  138. *newString = nullptr;
  139. return E_NOTIMPL;
  140. }
  141. HRESULT DelayLoadWinRtTypeResolution::RoParseTypeName(__in HSTRING typeName, __out DWORD *partsCount, __RPC__deref_out_ecount_full_opt(*partsCount) HSTRING **typeNameParts)
  142. {
  143. if (m_hModule)
  144. {
  145. if (m_pfnRoParseTypeName == nullptr)
  146. {
  147. m_pfnRoParseTypeName = (PFNCWRoParseTypeName)GetFunction("RoParseTypeName");
  148. if (m_pfnRoParseTypeName == nullptr)
  149. {
  150. return E_UNEXPECTED;
  151. }
  152. }
  153. Assert(m_pfnRoParseTypeName != nullptr);
  154. return m_pfnRoParseTypeName(typeName, partsCount, typeNameParts);
  155. }
  156. return E_NOTIMPL;
  157. }
  158. bool DelayLoadWindowsGlobalization::HasGlobalizationDllLoaded()
  159. {
  160. return this->hasGlobalizationDllLoaded;
  161. }
  162. HRESULT DelayLoadWindowsGlobalization::DllGetActivationFactory(
  163. __in HSTRING activatibleClassId,
  164. __out IActivationFactory** factory)
  165. {
  166. if (m_hModule)
  167. {
  168. if (m_pfnFNCWDllGetActivationFactory == nullptr)
  169. {
  170. m_pfnFNCWDllGetActivationFactory = (PFNCWDllGetActivationFactory)GetFunction("DllGetActivationFactory");
  171. if (m_pfnFNCWDllGetActivationFactory == nullptr)
  172. {
  173. return E_UNEXPECTED;
  174. }
  175. }
  176. Assert(m_pfnFNCWDllGetActivationFactory != nullptr);
  177. return m_pfnFNCWDllGetActivationFactory(activatibleClassId, factory);
  178. }
  179. return E_NOTIMPL;
  180. }
  181. HRESULT DelayLoadWinRtFoundation::RoGetActivationFactory(
  182. __in HSTRING activatibleClassId,
  183. __in REFIID iid,
  184. __out IActivationFactory** factory)
  185. {
  186. if (m_hModule)
  187. {
  188. if (m_pfnFNCWRoGetActivationFactory == nullptr)
  189. {
  190. m_pfnFNCWRoGetActivationFactory = (PFNCWRoGetActivationFactory)GetFunction("RoGetActivationFactory");
  191. if (m_pfnFNCWRoGetActivationFactory == nullptr)
  192. {
  193. return E_UNEXPECTED;
  194. }
  195. }
  196. Assert(m_pfnFNCWRoGetActivationFactory != nullptr);
  197. return m_pfnFNCWRoGetActivationFactory(activatibleClassId, iid, factory);
  198. }
  199. return E_NOTIMPL;
  200. }
  201. HRESULT DelayLoadWinRtTypeResolution::RoResolveNamespace(
  202. __in_opt const HSTRING namespaceName,
  203. __in_opt const HSTRING windowsMetaDataPath,
  204. __in const DWORD packageGraphPathsCount,
  205. __in_opt const HSTRING *packageGraphPaths,
  206. __out DWORD *metaDataFilePathsCount,
  207. HSTRING **metaDataFilePaths,
  208. __out DWORD *subNamespacesCount,
  209. HSTRING **subNamespaces)
  210. {
  211. if (m_hModule)
  212. {
  213. if (m_pfnRoResolveNamespace == nullptr)
  214. {
  215. m_pfnRoResolveNamespace = (PFNCRoResolveNamespace)GetFunction("RoResolveNamespace");
  216. if (m_pfnRoResolveNamespace == nullptr)
  217. {
  218. return E_UNEXPECTED;
  219. }
  220. }
  221. Assert(m_pfnRoResolveNamespace != nullptr);
  222. return m_pfnRoResolveNamespace(namespaceName, windowsMetaDataPath, packageGraphPathsCount, packageGraphPaths,
  223. metaDataFilePathsCount, metaDataFilePaths, subNamespacesCount, subNamespaces);
  224. }
  225. return E_NOTIMPL;
  226. }
  227. void DelayLoadWindowsGlobalization::Ensure(Js::DelayLoadWinRtString *winRTStringLibrary)
  228. {
  229. if (!this->m_isInit)
  230. {
  231. DelayLoadLibrary::EnsureFromSystemDirOnly();
  232. #if DBG
  233. // This unused variable is to allow one to see the value of lastError in case both LoadLibrary (DelayLoadLibrary::Ensure has one) fail.
  234. // As the issue might be with the first one, as opposed to the second
  235. DWORD errorWhenLoadingBluePlus = GetLastError();
  236. Unused(errorWhenLoadingBluePlus);
  237. #endif
  238. //Perform a check to see if Windows.Globalization.dll was loaded; if not try loading jsIntl.dll as we are on Win7.
  239. if (m_hModule == nullptr)
  240. {
  241. m_hModule = LoadLibraryEx(GetWin7LibraryName(), nullptr, LOAD_LIBRARY_SEARCH_SYSTEM32);
  242. }
  243. // Set the flag depending on Windows.globalization.dll or jsintl.dll was loaded successfully or not
  244. if (m_hModule != nullptr)
  245. {
  246. hasGlobalizationDllLoaded = true;
  247. }
  248. this->winRTStringLibrary = winRTStringLibrary;
  249. this->winRTStringsPresent = GetFunction("WindowsDuplicateString") != nullptr;
  250. }
  251. }
  252. HRESULT DelayLoadWindowsGlobalization::WindowsCreateString(_In_reads_opt_(length) const WCHAR * sourceString, UINT32 length, _Outptr_result_maybenull_ _Result_nullonfailure_ HSTRING * string)
  253. {
  254. //If winRtStringLibrary isn't nullptr, that means it is available and we are on Win8+
  255. if(!winRTStringsPresent && winRTStringLibrary->IsAvailable())
  256. {
  257. return winRTStringLibrary->WindowsCreateString(sourceString, length, string);
  258. }
  259. return DelayLoadWinRtString::WindowsCreateString(sourceString, length, string);
  260. }
  261. HRESULT DelayLoadWindowsGlobalization::WindowsCreateStringReference(_In_reads_opt_(length + 1) const WCHAR * sourceString, UINT32 length, _Out_ HSTRING_HEADER * header, _Outptr_result_maybenull_ _Result_nullonfailure_ HSTRING * string)
  262. {
  263. //First, we attempt to use the WinStringRT api encapsulated in the globalization dll; if it is available then it is a downlevel dll.
  264. //Otherwise; we might run into an error where we are using the Win8 (because testing is being done for instance) with the downlevel dll, and that would cause errors.
  265. if(!winRTStringsPresent && winRTStringLibrary->IsAvailable())
  266. {
  267. return winRTStringLibrary->WindowsCreateStringReference(sourceString, length, header, string);
  268. }
  269. return DelayLoadWinRtString::WindowsCreateStringReference(sourceString, length, header, string);
  270. }
  271. HRESULT DelayLoadWindowsGlobalization::WindowsDeleteString(_In_opt_ HSTRING string)
  272. {
  273. //First, we attempt to use the WinStringRT api encapsulated in the globalization dll; if it is available then it is a downlevel dll.
  274. //Otherwise; we might run into an error where we are using the Win8 (because testing is being done for instance) with the downlevel dll, and that would cause errors.
  275. if(!winRTStringsPresent && winRTStringLibrary->IsAvailable())
  276. {
  277. return winRTStringLibrary->WindowsDeleteString(string);
  278. }
  279. return DelayLoadWinRtString::WindowsDeleteString(string);
  280. }
  281. PCWSTR DelayLoadWindowsGlobalization::WindowsGetStringRawBuffer(_In_opt_ HSTRING string, _Out_opt_ UINT32 * length)
  282. {
  283. //First, we attempt to use the WinStringRT api encapsulated in the globalization dll; if it is available then it is a downlevel dll.
  284. //Otherwise; we might run into an error where we are using the Win8 (because testing is being done for instance) with the downlevel dll, and that would cause errors.
  285. if(!winRTStringsPresent && winRTStringLibrary->IsAvailable())
  286. {
  287. return winRTStringLibrary->WindowsGetStringRawBuffer(string, length);
  288. }
  289. return DelayLoadWinRtString::WindowsGetStringRawBuffer(string, length);
  290. }
  291. HRESULT DelayLoadWindowsGlobalization::WindowsCompareStringOrdinal(_In_opt_ HSTRING string1, _In_opt_ HSTRING string2, _Out_ INT32 * result)
  292. {
  293. //First, we attempt to use the WinStringRT api encapsulated in the globalization dll; if it is available then it is a downlevel dll.
  294. //Otherwise; we might run into an error where we are using the Win8 (because testing is being done for instance) with the downlevel dll, and that would cause errors.
  295. if(!winRTStringsPresent && winRTStringLibrary->IsAvailable())
  296. {
  297. return winRTStringLibrary->WindowsCompareStringOrdinal(string1, string2, result);
  298. }
  299. return DelayLoadWinRtString::WindowsCompareStringOrdinal(string1, string2, result);
  300. }
  301. HRESULT DelayLoadWindowsGlobalization::WindowsDuplicateString(_In_opt_ HSTRING original, _Outptr_result_maybenull_ _Result_nullonfailure_ HSTRING *newString)
  302. {
  303. //First, we attempt to use the WinStringRT api encapsulated in the globalization dll; if it is available then it is a downlevel dll.
  304. //Otherwise; we might run into an error where we are using the Win8 (because testing is being done for instance) with the downlevel dll, and that would cause errors.
  305. if(!winRTStringsPresent && winRTStringLibrary->IsAvailable())
  306. {
  307. return winRTStringLibrary->WindowsDuplicateString(original, newString);
  308. }
  309. return DelayLoadWinRtString::WindowsDuplicateString(original, newString);
  310. }
  311. #ifdef ENABLE_PROJECTION
  312. HRESULT DelayLoadWinRtError::RoClearError()
  313. {
  314. if (m_hModule)
  315. {
  316. if (m_pfnRoClearError == nullptr)
  317. {
  318. m_pfnRoClearError = (PFNCRoClearError)GetFunction("RoClearError");
  319. if (m_pfnRoClearError == nullptr)
  320. {
  321. return E_UNEXPECTED;
  322. }
  323. }
  324. Assert(m_pfnRoClearError != nullptr);
  325. m_pfnRoClearError();
  326. return S_OK;
  327. }
  328. return E_NOTIMPL;
  329. }
  330. BOOL DelayLoadWinRtError::RoOriginateLanguageException(__in HRESULT error, __in_opt HSTRING message, __in IUnknown * languageException)
  331. {
  332. if (m_hModule)
  333. {
  334. if (m_pfnRoOriginateLanguageException == nullptr)
  335. {
  336. m_pfnRoOriginateLanguageException = (PFNCRoOriginateLanguageException)GetFunction("RoOriginateLanguageException");
  337. if (m_pfnRoOriginateLanguageException == nullptr)
  338. {
  339. return FALSE;
  340. }
  341. }
  342. Assert(m_pfnRoOriginateLanguageException != nullptr);
  343. return m_pfnRoOriginateLanguageException(error, message, languageException);
  344. }
  345. return FALSE;
  346. }
  347. #endif
  348. #ifdef _CONTROL_FLOW_GUARD
  349. // Note. __declspec(guard(nocf)) causes the CFG check to be removed
  350. // inside this function, and is needed only for test binaries (chk and FRETEST)
  351. #if defined(DELAYLOAD_SET_CFG_TARGET)
  352. DECLSPEC_GUARDNOCF
  353. #endif
  354. BOOL DelayLoadWinCoreMemory::SetProcessCallTargets(_In_ HANDLE hProcess,
  355. _In_ PVOID VirtualAddress,
  356. _In_ SIZE_T RegionSize,
  357. _In_ ULONG NumberOfOffsets,
  358. _In_reads_(NumberOfOffsets) PCFG_CALL_TARGET_INFO OffsetInformation)
  359. {
  360. #if defined(DELAYLOAD_SET_CFG_TARGET)
  361. if (m_hModule)
  362. {
  363. if (m_pfnSetProcessValidCallTargets == nullptr)
  364. {
  365. m_pfnSetProcessValidCallTargets = (PFNCSetProcessValidCallTargets) GetFunction("SetProcessValidCallTargets");
  366. if (m_pfnSetProcessValidCallTargets == nullptr)
  367. {
  368. return FALSE;
  369. }
  370. }
  371. Assert(m_pfnSetProcessValidCallTargets != nullptr);
  372. return m_pfnSetProcessValidCallTargets(hProcess, VirtualAddress, RegionSize, NumberOfOffsets, OffsetInformation);
  373. }
  374. return FALSE;
  375. #else
  376. return SetProcessValidCallTargets(hProcess, VirtualAddress, RegionSize, NumberOfOffsets, OffsetInformation);
  377. #endif
  378. }
  379. #endif
  380. BOOL DelayLoadWinCoreProcessThreads::GetMitigationPolicyForProcess(
  381. __in HANDLE hProcess,
  382. __in PROCESS_MITIGATION_POLICY MitigationPolicy,
  383. __out_bcount(nLength) PVOID lpBuffer,
  384. __in SIZE_T nLength
  385. )
  386. {
  387. #if defined(DELAYLOAD_SET_CFG_TARGET)
  388. if (m_hModule)
  389. {
  390. if (m_pfnGetProcessMitigationPolicy == nullptr)
  391. {
  392. m_pfnGetProcessMitigationPolicy = (PFNCGetMitigationPolicyForProcess) GetFunction("GetProcessMitigationPolicy");
  393. if (m_pfnGetProcessMitigationPolicy == nullptr)
  394. {
  395. return FALSE;
  396. }
  397. }
  398. Assert(m_pfnGetProcessMitigationPolicy != nullptr);
  399. return m_pfnGetProcessMitigationPolicy(hProcess, MitigationPolicy, lpBuffer, nLength);
  400. }
  401. return FALSE;
  402. #else
  403. return BinaryFeatureControl::GetMitigationPolicyForProcess(hProcess, MitigationPolicy, lpBuffer, nLength);
  404. #endif // ENABLE_DEBUG_CONFIG_OPTIONS
  405. }
  406. BOOL DelayLoadWinCoreProcessThreads::GetProcessInformation(
  407. __in HANDLE hProcess,
  408. __in PROCESS_INFORMATION_CLASS ProcessInformationClass,
  409. __out_bcount(nLength) PVOID lpBuffer,
  410. __in SIZE_T nLength
  411. )
  412. {
  413. #if defined(DELAYLOAD_SET_CFG_TARGET) || defined(_M_ARM)
  414. if (m_hModule)
  415. {
  416. if (m_pfnGetProcessInformation == nullptr)
  417. {
  418. m_pfnGetProcessInformation = (PFNCGetProcessInformation) GetFunction("GetProcessInformation");
  419. if (m_pfnGetProcessInformation == nullptr)
  420. {
  421. return FALSE;
  422. }
  423. }
  424. Assert(m_pfnGetProcessInformation != nullptr);
  425. return m_pfnGetProcessInformation(hProcess, ProcessInformationClass, lpBuffer, nLength);
  426. }
  427. #endif
  428. return FALSE;
  429. }
  430. // Implement this function inlined so that WinRT.lib can be used without the runtime.
  431. HRESULT DelayLoadWinType::RoGetMetaDataFile(
  432. _In_ const HSTRING name,
  433. _In_opt_ IMetaDataDispenserEx *metaDataDispenser,
  434. _Out_opt_ HSTRING *metaDataFilePath,
  435. _Outptr_opt_ IMetaDataImport2 **metaDataImport,
  436. _Out_opt_ mdTypeDef *typeDefToken)
  437. {
  438. if (m_hModule)
  439. {
  440. if (m_pfnRoGetMetadataFile == nullptr)
  441. {
  442. m_pfnRoGetMetadataFile = (PFNCWRoGetMetadataFile)GetFunction("RoGetMetaDataFile");
  443. if (m_pfnRoGetMetadataFile == nullptr)
  444. {
  445. return E_UNEXPECTED;
  446. }
  447. }
  448. Assert(m_pfnRoGetMetadataFile != nullptr);
  449. return m_pfnRoGetMetadataFile(name, metaDataDispenser, metaDataFilePath, metaDataImport, typeDefToken);
  450. }
  451. return E_NOTIMPL;
  452. }
  453. }