CommonPal.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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 wchar16;
  19. // xplat-todo: get a better name for this macro
  20. #define CH_WSTR(s) L##s
  21. #define INIT_PRIORITY(x)
  22. #define get_cpuid __cpuid
  23. #else // !_WIN32
  24. #define USING_PAL_STDLIB 1
  25. #include "inc/pal.h"
  26. #include "inc/rt/palrt.h"
  27. #include "inc/rt/no_sal2.h"
  28. #include "inc/rt/oaidl.h"
  29. typedef char16_t wchar16;
  30. #define CH_WSTR(s) u##s
  31. #define INIT_PRIORITY(x) __attribute__((init_priority(x)))
  32. // xplat-todo: verify below is correct
  33. #include <cpuid.h>
  34. inline int get_cpuid(int cpuInfo[4], int function_id)
  35. {
  36. return __get_cpuid(
  37. static_cast<unsigned int>(function_id),
  38. reinterpret_cast<unsigned int*>(&cpuInfo[0]),
  39. reinterpret_cast<unsigned int*>(&cpuInfo[1]),
  40. reinterpret_cast<unsigned int*>(&cpuInfo[2]),
  41. reinterpret_cast<unsigned int*>(&cpuInfo[3]));
  42. }
  43. inline void DebugBreak()
  44. {
  45. __builtin_trap();
  46. }
  47. #define _BitScanForward BitScanForward
  48. #define _BitScanForward64 BitScanForward64
  49. #define _BitScanReverse BitScanReverse
  50. #define _BitScanReverse64 BitScanReverse64
  51. #define _bittest BitTest
  52. #define _bittestandset BitTestAndSet
  53. #define _interlockedbittestandset InterlockedBitTestAndSet
  54. #define DbgRaiseAssertionFailure() __builtin_trap()
  55. // These are not available in pal
  56. #define fwprintf_s fwprintf
  57. #define wmemcmp wcsncmp
  58. // sprintf_s overloaded in safecrt.h. Not sure why palrt.h redefines sprintf_s.
  59. #undef sprintf_s
  60. // PAL LoadLibraryExW not supported
  61. #define LOAD_LIBRARY_SEARCH_SYSTEM32 0
  62. // winerror.h
  63. #define FACILITY_JSCRIPT 2306
  64. #define JSCRIPT_E_CANTEXECUTE _HRESULT_TYPEDEF_(0x89020001L)
  65. #define DISP_E_UNKNOWNINTERFACE _HRESULT_TYPEDEF_(0x80020001L)
  66. #define DISP_E_MEMBERNOTFOUND _HRESULT_TYPEDEF_(0x80020003L)
  67. #define DISP_E_UNKNOWNNAME _HRESULT_TYPEDEF_(0x80020006L)
  68. #define DISP_E_NONAMEDARGS _HRESULT_TYPEDEF_(0x80020007L)
  69. #define DISP_E_EXCEPTION _HRESULT_TYPEDEF_(0x80020009L)
  70. #define DISP_E_BADINDEX _HRESULT_TYPEDEF_(0x8002000BL)
  71. #define DISP_E_UNKNOWNLCID _HRESULT_TYPEDEF_(0x8002000CL)
  72. #define DISP_E_ARRAYISLOCKED _HRESULT_TYPEDEF_(0x8002000DL)
  73. #define DISP_E_BADPARAMCOUNT _HRESULT_TYPEDEF_(0x8002000EL)
  74. #define DISP_E_PARAMNOTOPTIONAL _HRESULT_TYPEDEF_(0x8002000FL)
  75. #define DISP_E_NOTACOLLECTION _HRESULT_TYPEDEF_(0x80020011L)
  76. #define TYPE_E_DLLFUNCTIONNOTFOUND _HRESULT_TYPEDEF_(0x8002802FL)
  77. #define TYPE_E_TYPEMISMATCH _HRESULT_TYPEDEF_(0x80028CA0L)
  78. #define TYPE_E_OUTOFBOUNDS _HRESULT_TYPEDEF_(0x80028CA1L)
  79. #define TYPE_E_IOERROR _HRESULT_TYPEDEF_(0x80028CA2L)
  80. #define TYPE_E_CANTCREATETMPFILE _HRESULT_TYPEDEF_(0x80028CA3L)
  81. #define TYPE_E_CANTLOADLIBRARY _HRESULT_TYPEDEF_(0x80029C4AL)
  82. #define STG_E_TOOMANYOPENFILES _HRESULT_TYPEDEF_(0x80030004L)
  83. #define STG_E_ACCESSDENIED _HRESULT_TYPEDEF_(0x80030005L)
  84. #define STG_E_INSUFFICIENTMEMORY _HRESULT_TYPEDEF_(0x80030008L)
  85. #define STG_E_NOMOREFILES _HRESULT_TYPEDEF_(0x80030012L)
  86. #define STG_E_DISKISWRITEPROTECTED _HRESULT_TYPEDEF_(0x80030013L)
  87. #define STG_E_READFAULT _HRESULT_TYPEDEF_(0x8003001EL)
  88. #define STG_E_SHAREVIOLATION _HRESULT_TYPEDEF_(0x80030020L)
  89. #define STG_E_LOCKVIOLATION _HRESULT_TYPEDEF_(0x80030021L)
  90. #define STG_E_MEDIUMFULL _HRESULT_TYPEDEF_(0x80030070L)
  91. #define STG_E_INVALIDNAME _HRESULT_TYPEDEF_(0x800300FCL)
  92. #define STG_E_INUSE _HRESULT_TYPEDEF_(0x80030100L)
  93. #define STG_E_NOTCURRENT _HRESULT_TYPEDEF_(0x80030101L)
  94. #define STG_E_CANTSAVE _HRESULT_TYPEDEF_(0x80030103L)
  95. #define REGDB_E_CLASSNOTREG _HRESULT_TYPEDEF_(0x80040154L)
  96. #define MK_E_UNAVAILABLE _HRESULT_TYPEDEF_(0x800401E3L)
  97. #define MK_E_INVALIDEXTENSION _HRESULT_TYPEDEF_(0x800401E6L)
  98. #define MK_E_CANTOPENFILE _HRESULT_TYPEDEF_(0x800401EAL)
  99. #define CO_E_APPNOTFOUND _HRESULT_TYPEDEF_(0x800401F5L)
  100. #define CO_E_APPDIDNTREG _HRESULT_TYPEDEF_(0x800401FEL)
  101. #define GetScode(hr) ((SCODE) (hr))
  102. // activscp.h
  103. #define SCRIPT_E_RECORDED 0x86664004L
  104. // _countof
  105. #if defined _M_X64 || defined _M_ARM || defined _M_ARM64
  106. #define _UNALIGNED __unaligned
  107. #else
  108. #define _UNALIGNED
  109. #endif
  110. #ifdef __cplusplus
  111. extern "C++"
  112. {
  113. template <typename _CountofType, size_t _SizeOfArray>
  114. char(*__countof_helper(_UNALIGNED _CountofType(&_Array)[_SizeOfArray]))[_SizeOfArray];
  115. #define __crt_countof(_Array) (sizeof(*__countof_helper(_Array)) + 0)
  116. }
  117. #else
  118. #define __crt_countof(_Array) (sizeof(_Array) / sizeof(_Array[0]))
  119. #endif
  120. #ifndef _countof
  121. #define _countof __crt_countof
  122. #endif
  123. // _countof
  124. #define ARRAYSIZE(A) _countof(A)
  125. //
  126. // Singly linked list structure. Can be used as either a list head, or
  127. // as link words.
  128. //
  129. typedef struct _SINGLE_LIST_ENTRY {
  130. struct _SINGLE_LIST_ENTRY *Next;
  131. } SINGLE_LIST_ENTRY, *PSINGLE_LIST_ENTRY;
  132. #if defined(_WIN64)
  133. //
  134. // The type SINGLE_LIST_ENTRY is not suitable for use with SLISTs. For
  135. // WIN64, an entry on an SLIST is required to be 16-byte aligned, while a
  136. // SINGLE_LIST_ENTRY structure has only 8 byte alignment.
  137. //
  138. // Therefore, all SLIST code should use the SLIST_ENTRY type instead of the
  139. // SINGLE_LIST_ENTRY type.
  140. //
  141. #pragma warning(push)
  142. #pragma warning(disable:4324) // structure padded due to align()
  143. typedef struct DECLSPEC_ALIGN(16) _SLIST_ENTRY {
  144. struct _SLIST_ENTRY *Next;
  145. } SLIST_ENTRY, *PSLIST_ENTRY;
  146. #pragma warning(pop)
  147. #else
  148. typedef struct _SINGLE_LIST_ENTRY SLIST_ENTRY, *PSLIST_ENTRY;
  149. #endif // _WIN64
  150. #if defined(_AMD64_)
  151. typedef union DECLSPEC_ALIGN(16) _SLIST_HEADER {
  152. struct { // original struct
  153. ULONGLONG Alignment;
  154. ULONGLONG Region;
  155. } DUMMYSTRUCTNAME;
  156. struct { // x64 16-byte header
  157. ULONGLONG Depth : 16;
  158. ULONGLONG Sequence : 48;
  159. ULONGLONG Reserved : 4;
  160. ULONGLONG NextEntry : 60; // last 4 bits are always 0's
  161. } HeaderX64;
  162. } SLIST_HEADER, *PSLIST_HEADER;
  163. #elif defined(_X86_)
  164. typedef union _SLIST_HEADER {
  165. ULONGLONG Alignment;
  166. struct {
  167. SLIST_ENTRY Next;
  168. WORD Depth;
  169. WORD CpuId;
  170. } DUMMYSTRUCTNAME;
  171. } SLIST_HEADER, *PSLIST_HEADER;
  172. #elif defined(_ARM_)
  173. typedef union _SLIST_HEADER {
  174. ULONGLONG Alignment;
  175. struct {
  176. SLIST_ENTRY Next;
  177. WORD Depth;
  178. WORD Reserved;
  179. } DUMMYSTRUCTNAME;
  180. } SLIST_HEADER, *PSLIST_HEADER;
  181. #endif
  182. PALIMPORT VOID PALAPI InitializeSListHead(IN OUT PSLIST_HEADER ListHead);
  183. PALIMPORT PSLIST_ENTRY PALAPI InterlockedPushEntrySList(IN OUT PSLIST_HEADER ListHead, IN OUT PSLIST_ENTRY ListEntry);
  184. PALIMPORT PSLIST_ENTRY PALAPI InterlockedPopEntrySList(IN OUT PSLIST_HEADER ListHead);
  185. // Use overloaded versions of InterlockedExchangeAdd
  186. #define InterlockedExchangeAdd __InterlockedExchangeAdd
  187. inline long InterlockedExchangeAdd(
  188. IN OUT long volatile *Addend,
  189. IN long Value)
  190. {
  191. return __sync_fetch_and_add(Addend, Value);
  192. }
  193. inline unsigned long InterlockedExchangeAdd(
  194. IN OUT unsigned long volatile *Addend,
  195. IN unsigned long Value)
  196. {
  197. return __sync_fetch_and_add(Addend, Value);
  198. }
  199. inline long InterlockedExchangeSubtract(
  200. IN OUT long volatile *Addend,
  201. IN long Value)
  202. {
  203. return __sync_fetch_and_sub(Addend, Value);
  204. }
  205. inline unsigned long InterlockedExchangeSubtract(
  206. IN OUT unsigned long volatile *Addend,
  207. IN unsigned long Value)
  208. {
  209. return __sync_fetch_and_sub(Addend, Value);
  210. }
  211. // xplat-todo: implement these for JIT and Concurrent/Partial GC
  212. uintptr_t _beginthreadex(
  213. void *security,
  214. unsigned stack_size,
  215. unsigned ( __stdcall *start_address )( void * ),
  216. void *arglist,
  217. unsigned initflag,
  218. unsigned *thrdaddr);
  219. BOOL WINAPI GetModuleHandleEx(
  220. _In_ DWORD dwFlags,
  221. _In_opt_ LPCTSTR lpModuleName,
  222. _Out_ HMODULE *phModule
  223. );
  224. // xplat-todo: implement this function to get the stack bounds of the current
  225. // thread
  226. // For Linux, we could use pthread_getattr_np to get the stack limit (end)
  227. // and then use the stack size to calculate the stack base
  228. int GetCurrentThreadStackBounds(char** stackBase, char** stackEnd);
  229. // xplat-todo: cryptographically secure PRNG?
  230. errno_t rand_s(unsigned int* randomValue);
  231. #define MAXUINT32 ((uint32_t)~((uint32_t)0))
  232. #define MAXINT32 ((int32_t)(MAXUINT32 >> 1))
  233. #endif // _WIN32
  234. // Use intsafe.h for internal builds (currently missing some files with stdint.h)
  235. #if defined(_WIN32) && defined(NTBUILD)
  236. #define ENABLE_INTSAFE_SIGNED_FUNCTIONS 1
  237. #include <intsafe.h>
  238. #else
  239. #include <stdint.h>
  240. #endif
  241. #ifdef _MSC_VER
  242. // ms-specific keywords
  243. #define _ABSTRACT abstract
  244. // MSVC2015 does not support C++11 semantics for `typename QualifiedName` declarations
  245. // outside of template code.
  246. #define _TYPENAME
  247. #else
  248. #define _ABSTRACT
  249. #define _TYPENAME typename
  250. #endif
  251. #if defined(_MSC_VER) && _MSC_VER < 1900
  252. // "noexcept" not supported before VS 2015
  253. #define _NOEXCEPT
  254. #else
  255. #define _NOEXCEPT noexcept
  256. #endif
  257. // xplat-todo: can we get rid of this for clang?
  258. // Including xmmintrin.h right now creates a ton of
  259. // compile errors, so temporarily defining this for clang
  260. // to avoid including that header
  261. #ifndef _MSC_VER
  262. #define _MM_HINT_T0 3
  263. #endif
  264. // xplat-todo: figure out why strsafe.h includes stdio etc
  265. // which prevents me from directly including PAL's strsafe.h
  266. #ifdef __cplusplus
  267. #define _STRSAFE_EXTERN_C extern "C"
  268. #else
  269. #define _STRSAFE_EXTERN_C extern
  270. #endif
  271. // If you do not want to use these functions inline (and instead want to link w/ strsafe.lib), then
  272. // #define STRSAFE_LIB before including this header file.
  273. #if defined(STRSAFE_LIB)
  274. #define STRSAFEAPI _STRSAFE_EXTERN_C HRESULT __stdcall
  275. #pragma comment(lib, "strsafe.lib")
  276. #elif defined(STRSAFE_LIB_IMPL)
  277. #define STRSAFEAPI _STRSAFE_EXTERN_C HRESULT __stdcall
  278. #else
  279. #define STRSAFEAPI __inline HRESULT __stdcall
  280. #define STRSAFE_INLINE
  281. #endif
  282. STRSAFEAPI StringCchPrintfW(WCHAR* pszDest, size_t cchDest, const WCHAR* pszFormat, ...);