2
0

CommonPal.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  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. #pragma once
  6. #ifdef _WIN32
  7. #pragma warning(push)
  8. #pragma warning(disable: 4995) /* 'function': name was marked as #pragma deprecated */
  9. // === Windows Header Files ===
  10. #define INC_OLE2 /* for windows.h */
  11. #define CONST_VTABLE /* for objbase.h */
  12. #include <windows.h>
  13. /* Don't want GetObject and GetClassName to be defined to GetObjectW and GetClassNameW */
  14. #undef GetObject
  15. #undef GetClassName
  16. #undef Yield /* winbase.h defines this but we want to use it for Js::OpCode::Yield; it is Win16 legacy, no harm undef'ing it */
  17. #pragma warning(pop)
  18. typedef wchar_t char16;
  19. // xplat-todo: get a better name for this macro
  20. #define _u(s) L##s
  21. #define INIT_PRIORITY(x)
  22. #define get_cpuid __cpuid
  23. #else // !_WIN32
  24. #define USING_PAL_STDLIB 1
  25. #ifdef PAL_STDCPP_COMPAT
  26. #include <math.h>
  27. #include <time.h>
  28. #include <smmintrin.h>
  29. #include <xmmintrin.h>
  30. #endif
  31. #include "inc/pal.h"
  32. #include "inc/rt/palrt.h"
  33. #include "inc/rt/no_sal2.h"
  34. #include "inc/rt/oaidl.h"
  35. typedef char16_t char16;
  36. #define _u(s) u##s
  37. #define INIT_PRIORITY(x) __attribute__((init_priority(x)))
  38. #ifdef PAL_STDCPP_COMPAT
  39. #define __in
  40. #define __out
  41. #define FILE PAL_FILE
  42. #endif
  43. // xplat-todo: verify below is correct
  44. #include <cpuid.h>
  45. inline int get_cpuid(int cpuInfo[4], int function_id)
  46. {
  47. return __get_cpuid(
  48. static_cast<unsigned int>(function_id),
  49. reinterpret_cast<unsigned int*>(&cpuInfo[0]),
  50. reinterpret_cast<unsigned int*>(&cpuInfo[1]),
  51. reinterpret_cast<unsigned int*>(&cpuInfo[2]),
  52. reinterpret_cast<unsigned int*>(&cpuInfo[3]));
  53. }
  54. inline void DebugBreak()
  55. {
  56. __builtin_trap();
  57. }
  58. #define _BitScanForward BitScanForward
  59. #define _BitScanForward64 BitScanForward64
  60. #define _BitScanReverse BitScanReverse
  61. #define _BitScanReverse64 BitScanReverse64
  62. #define _bittest BitTest
  63. #define _bittestandset BitTestAndSet
  64. #define _interlockedbittestandset InterlockedBitTestAndSet
  65. #define DbgRaiseAssertionFailure() __builtin_trap()
  66. // These are not available in pal
  67. #define fwprintf_s fwprintf
  68. #define wmemcmp wcsncmp
  69. // sprintf_s overloaded in safecrt.h. Not sure why palrt.h redefines sprintf_s.
  70. #undef sprintf_s
  71. // PAL LoadLibraryExW not supported
  72. #define LOAD_LIBRARY_SEARCH_SYSTEM32 0
  73. // winerror.h
  74. #define FACILITY_JSCRIPT 2306
  75. #define JSCRIPT_E_CANTEXECUTE _HRESULT_TYPEDEF_(0x89020001L)
  76. #define DISP_E_UNKNOWNINTERFACE _HRESULT_TYPEDEF_(0x80020001L)
  77. #define DISP_E_MEMBERNOTFOUND _HRESULT_TYPEDEF_(0x80020003L)
  78. #define DISP_E_UNKNOWNNAME _HRESULT_TYPEDEF_(0x80020006L)
  79. #define DISP_E_NONAMEDARGS _HRESULT_TYPEDEF_(0x80020007L)
  80. #define DISP_E_EXCEPTION _HRESULT_TYPEDEF_(0x80020009L)
  81. #define DISP_E_BADINDEX _HRESULT_TYPEDEF_(0x8002000BL)
  82. #define DISP_E_UNKNOWNLCID _HRESULT_TYPEDEF_(0x8002000CL)
  83. #define DISP_E_ARRAYISLOCKED _HRESULT_TYPEDEF_(0x8002000DL)
  84. #define DISP_E_BADPARAMCOUNT _HRESULT_TYPEDEF_(0x8002000EL)
  85. #define DISP_E_PARAMNOTOPTIONAL _HRESULT_TYPEDEF_(0x8002000FL)
  86. #define DISP_E_NOTACOLLECTION _HRESULT_TYPEDEF_(0x80020011L)
  87. #define TYPE_E_DLLFUNCTIONNOTFOUND _HRESULT_TYPEDEF_(0x8002802FL)
  88. #define TYPE_E_TYPEMISMATCH _HRESULT_TYPEDEF_(0x80028CA0L)
  89. #define TYPE_E_OUTOFBOUNDS _HRESULT_TYPEDEF_(0x80028CA1L)
  90. #define TYPE_E_IOERROR _HRESULT_TYPEDEF_(0x80028CA2L)
  91. #define TYPE_E_CANTCREATETMPFILE _HRESULT_TYPEDEF_(0x80028CA3L)
  92. #define TYPE_E_CANTLOADLIBRARY _HRESULT_TYPEDEF_(0x80029C4AL)
  93. #define STG_E_TOOMANYOPENFILES _HRESULT_TYPEDEF_(0x80030004L)
  94. #define STG_E_ACCESSDENIED _HRESULT_TYPEDEF_(0x80030005L)
  95. #define STG_E_INSUFFICIENTMEMORY _HRESULT_TYPEDEF_(0x80030008L)
  96. #define STG_E_NOMOREFILES _HRESULT_TYPEDEF_(0x80030012L)
  97. #define STG_E_DISKISWRITEPROTECTED _HRESULT_TYPEDEF_(0x80030013L)
  98. #define STG_E_READFAULT _HRESULT_TYPEDEF_(0x8003001EL)
  99. #define STG_E_SHAREVIOLATION _HRESULT_TYPEDEF_(0x80030020L)
  100. #define STG_E_LOCKVIOLATION _HRESULT_TYPEDEF_(0x80030021L)
  101. #define STG_E_MEDIUMFULL _HRESULT_TYPEDEF_(0x80030070L)
  102. #define STG_E_INVALIDNAME _HRESULT_TYPEDEF_(0x800300FCL)
  103. #define STG_E_INUSE _HRESULT_TYPEDEF_(0x80030100L)
  104. #define STG_E_NOTCURRENT _HRESULT_TYPEDEF_(0x80030101L)
  105. #define STG_E_CANTSAVE _HRESULT_TYPEDEF_(0x80030103L)
  106. #define REGDB_E_CLASSNOTREG _HRESULT_TYPEDEF_(0x80040154L)
  107. #define MK_E_UNAVAILABLE _HRESULT_TYPEDEF_(0x800401E3L)
  108. #define MK_E_INVALIDEXTENSION _HRESULT_TYPEDEF_(0x800401E6L)
  109. #define MK_E_CANTOPENFILE _HRESULT_TYPEDEF_(0x800401EAL)
  110. #define CO_E_APPNOTFOUND _HRESULT_TYPEDEF_(0x800401F5L)
  111. #define CO_E_APPDIDNTREG _HRESULT_TYPEDEF_(0x800401FEL)
  112. #define GetScode(hr) ((SCODE) (hr))
  113. // activscp.h
  114. #define SCRIPT_E_RECORDED _HRESULT_TYPEDEF_(0x86664004L)
  115. typedef
  116. enum tagBREAKPOINT_STATE
  117. {
  118. BREAKPOINT_DELETED = 0,
  119. BREAKPOINT_DISABLED = 1,
  120. BREAKPOINT_ENABLED = 2
  121. } BREAKPOINT_STATE;
  122. typedef
  123. enum tagBREAKRESUME_ACTION
  124. {
  125. BREAKRESUMEACTION_ABORT = 0,
  126. BREAKRESUMEACTION_CONTINUE = ( BREAKRESUMEACTION_ABORT + 1 ) ,
  127. BREAKRESUMEACTION_STEP_INTO = ( BREAKRESUMEACTION_CONTINUE + 1 ) ,
  128. BREAKRESUMEACTION_STEP_OVER = ( BREAKRESUMEACTION_STEP_INTO + 1 ) ,
  129. BREAKRESUMEACTION_STEP_OUT = ( BREAKRESUMEACTION_STEP_OVER + 1 ) ,
  130. BREAKRESUMEACTION_IGNORE = ( BREAKRESUMEACTION_STEP_OUT + 1 ) ,
  131. BREAKRESUMEACTION_STEP_DOCUMENT = ( BREAKRESUMEACTION_IGNORE + 1 )
  132. } BREAKRESUMEACTION;
  133. // _countof
  134. #if defined _M_X64 || defined _M_ARM || defined _M_ARM64
  135. #define _UNALIGNED __unaligned
  136. #else
  137. #define _UNALIGNED
  138. #endif
  139. #ifdef __cplusplus
  140. extern "C++"
  141. {
  142. template <typename _CountofType, size_t _SizeOfArray>
  143. char(*__countof_helper(_UNALIGNED _CountofType(&_Array)[_SizeOfArray]))[_SizeOfArray];
  144. #define __crt_countof(_Array) (sizeof(*__countof_helper(_Array)) + 0)
  145. }
  146. #else
  147. #define __crt_countof(_Array) (sizeof(_Array) / sizeof(_Array[0]))
  148. #endif
  149. #ifndef _countof
  150. #define _countof __crt_countof
  151. #endif
  152. // _countof
  153. #define ARRAYSIZE(A) _countof(A)
  154. //
  155. // Singly linked list structure. Can be used as either a list head, or
  156. // as link words.
  157. //
  158. typedef struct _SINGLE_LIST_ENTRY {
  159. struct _SINGLE_LIST_ENTRY *Next;
  160. } SINGLE_LIST_ENTRY, *PSINGLE_LIST_ENTRY;
  161. #if defined(_WIN64)
  162. //
  163. // The type SINGLE_LIST_ENTRY is not suitable for use with SLISTs. For
  164. // WIN64, an entry on an SLIST is required to be 16-byte aligned, while a
  165. // SINGLE_LIST_ENTRY structure has only 8 byte alignment.
  166. //
  167. // Therefore, all SLIST code should use the SLIST_ENTRY type instead of the
  168. // SINGLE_LIST_ENTRY type.
  169. //
  170. #pragma warning(push)
  171. #pragma warning(disable:4324) // structure padded due to align()
  172. typedef struct DECLSPEC_ALIGN(16) _SLIST_ENTRY {
  173. struct _SLIST_ENTRY *Next;
  174. } SLIST_ENTRY, *PSLIST_ENTRY;
  175. #pragma warning(pop)
  176. #else
  177. typedef struct _SINGLE_LIST_ENTRY SLIST_ENTRY, *PSLIST_ENTRY;
  178. #endif // _WIN64
  179. #if defined(_AMD64_)
  180. typedef union DECLSPEC_ALIGN(16) _SLIST_HEADER {
  181. struct { // original struct
  182. ULONGLONG Alignment;
  183. ULONGLONG Region;
  184. } DUMMYSTRUCTNAME;
  185. struct { // x64 16-byte header
  186. ULONGLONG Depth : 16;
  187. ULONGLONG Sequence : 48;
  188. ULONGLONG Reserved : 4;
  189. ULONGLONG NextEntry : 60; // last 4 bits are always 0's
  190. } HeaderX64;
  191. } SLIST_HEADER, *PSLIST_HEADER;
  192. #elif defined(_X86_)
  193. typedef union _SLIST_HEADER {
  194. ULONGLONG Alignment;
  195. struct {
  196. SLIST_ENTRY Next;
  197. WORD Depth;
  198. WORD CpuId;
  199. } DUMMYSTRUCTNAME;
  200. } SLIST_HEADER, *PSLIST_HEADER;
  201. #elif defined(_ARM_)
  202. typedef union _SLIST_HEADER {
  203. ULONGLONG Alignment;
  204. struct {
  205. SLIST_ENTRY Next;
  206. WORD Depth;
  207. WORD Reserved;
  208. } DUMMYSTRUCTNAME;
  209. } SLIST_HEADER, *PSLIST_HEADER;
  210. #endif
  211. PALIMPORT VOID PALAPI InitializeSListHead(IN OUT PSLIST_HEADER ListHead);
  212. PALIMPORT PSLIST_ENTRY PALAPI InterlockedPushEntrySList(IN OUT PSLIST_HEADER ListHead, IN OUT PSLIST_ENTRY ListEntry);
  213. PALIMPORT PSLIST_ENTRY PALAPI InterlockedPopEntrySList(IN OUT PSLIST_HEADER ListHead);
  214. template <class T>
  215. inline T InterlockedExchangeAdd(
  216. IN OUT T volatile *Addend,
  217. IN T Value)
  218. {
  219. return __sync_fetch_and_add(Addend, Value);
  220. }
  221. template <class T>
  222. inline T InterlockedExchangeSubtract(
  223. IN OUT T volatile *Addend,
  224. IN T Value)
  225. {
  226. return __sync_fetch_and_sub(Addend, Value);
  227. }
  228. template <class T>
  229. inline T InterlockedIncrement(
  230. IN OUT T volatile *Addend)
  231. {
  232. return __sync_add_and_fetch(Addend, T(1));
  233. }
  234. template <class T>
  235. inline T InterlockedDecrement(
  236. IN OUT T volatile *Addend)
  237. {
  238. return __sync_sub_and_fetch(Addend, T(1));
  239. }
  240. inline __int64 _abs64(__int64 n)
  241. {
  242. return n < 0 ? -n : n;
  243. }
  244. // xplat-todo: implement these for JIT and Concurrent/Partial GC
  245. uintptr_t _beginthreadex(
  246. void *security,
  247. unsigned stack_size,
  248. unsigned ( __stdcall *start_address )( void * ),
  249. void *arglist,
  250. unsigned initflag,
  251. unsigned *thrdaddr);
  252. BOOL WINAPI GetModuleHandleEx(
  253. _In_ DWORD dwFlags,
  254. _In_opt_ LPCTSTR lpModuleName,
  255. _Out_ HMODULE *phModule
  256. );
  257. // xplat-todo: implement this function to get the stack bounds of the current
  258. // thread
  259. // For Linux, we could use pthread_getattr_np to get the stack limit (end)
  260. // and then use the stack size to calculate the stack base
  261. int GetCurrentThreadStackBounds(char** stackBase, char** stackEnd);
  262. errno_t rand_s(unsigned int* randomValue);
  263. errno_t __cdecl _ultow_s(unsigned _Value, WCHAR *_Dst, size_t _SizeInWords, int _Radix);
  264. errno_t __cdecl _ui64tow_s(unsigned __int64 _Value, WCHAR *_Dst, size_t _SizeInWords, int _Radix);
  265. void __cdecl qsort_s(void *base, size_t num, size_t width,
  266. int (__cdecl *compare )(void *, const void *, const void *),
  267. void * context
  268. );
  269. char16* __cdecl wmemset(char16* wcs, char16 wc, size_t n);
  270. DWORD __cdecl CharLowerBuffW(const char16* lpsz, DWORD cchLength);
  271. DWORD __cdecl CharUpperBuffW(const char16* lpsz, DWORD cchLength);
  272. #define MAXUINT32 ((uint32_t)~((uint32_t)0))
  273. #define MAXINT32 ((int32_t)(MAXUINT32 >> 1))
  274. #define BYTE_MAX 0xff
  275. #define USHORT_MAX 0xffff
  276. #ifdef UNICODE
  277. #define StringCchPrintf StringCchPrintfW
  278. #endif
  279. #endif // _WIN32
  280. // Use intsafe.h for internal builds (currently missing some files with stdint.h)
  281. #if defined(_WIN32) && defined(NTBUILD)
  282. #define ENABLE_INTSAFE_SIGNED_FUNCTIONS 1
  283. #include <intsafe.h>
  284. #else
  285. #include <stdint.h>
  286. #endif
  287. #ifdef _MSC_VER
  288. // ms-specific keywords
  289. #define _ABSTRACT abstract
  290. // MSVC2015 does not support C++11 semantics for `typename QualifiedName` declarations
  291. // outside of template code.
  292. #define _TYPENAME
  293. #else
  294. #define _ABSTRACT
  295. #define _TYPENAME typename
  296. #endif
  297. #if (defined(_MSC_VER) && _MSC_VER < 1900) || defined(NTBUILD)
  298. // "noexcept" not supported before VS 2015
  299. #define _NOEXCEPT_ throw()
  300. #else
  301. #define _NOEXCEPT_ noexcept
  302. #endif
  303. #ifdef _MSC_VER
  304. extern "C" PVOID _ReturnAddress(VOID);
  305. #pragma intrinsic(_ReturnAddress)
  306. #else
  307. #define _ReturnAddress() __builtin_return_address(0)
  308. #endif
  309. // xplat-todo: figure out why strsafe.h includes stdio etc
  310. // which prevents me from directly including PAL's strsafe.h
  311. #ifdef __cplusplus
  312. #define _STRSAFE_EXTERN_C extern "C"
  313. #else
  314. #define _STRSAFE_EXTERN_C extern
  315. #endif
  316. // If you do not want to use these functions inline (and instead want to link w/ strsafe.lib), then
  317. // #define STRSAFE_LIB before including this header file.
  318. #if defined(STRSAFE_LIB)
  319. #define STRSAFEAPI _STRSAFE_EXTERN_C HRESULT __stdcall
  320. #pragma comment(lib, "strsafe.lib")
  321. #elif defined(STRSAFE_LIB_IMPL)
  322. #define STRSAFEAPI _STRSAFE_EXTERN_C HRESULT __stdcall
  323. #else
  324. #define STRSAFEAPI __inline HRESULT __stdcall
  325. #define STRSAFE_INLINE
  326. #endif
  327. STRSAFEAPI StringCchPrintfW(WCHAR* pszDest, size_t cchDest, const WCHAR* pszFormat, ...);
  328. template <class TryFunc, class FinallyFunc>
  329. void TryFinally(const TryFunc& tryFunc, const FinallyFunc& finallyFunc)
  330. {
  331. bool hasException = true;
  332. try
  333. {
  334. tryFunc();
  335. hasException = false;
  336. }
  337. catch(...)
  338. {
  339. finallyFunc(hasException);
  340. throw;
  341. }
  342. finallyFunc(hasException);
  343. }
  344. __inline
  345. HRESULT ULongMult(ULONG ulMultiplicand, ULONG ulMultiplier, ULONG* pulResult);