CommonPal.h 11 KB

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