ReactOS 0.4.16-dev-1946-g52006dd
internal_shared.h
Go to the documentation of this file.
1//
2// internal_shared.h
3//
4// Copyright (c) 2024 Timo Kreuzer
5//
6// Header for definitions shared between vcruntime and UCRT.
7//
8// SPDX-License-Identifier: MIT
9//
10
11#pragma once
12
13#include <corecrt_startup.h>
14#include <crtdbg.h>
15#include <windef.h>
16#include <winbase.h>
17
18#ifdef __cplusplus
19extern "C" {
20#endif // __cplusplus
21
22#if defined(_MSC_VER)
23#define _CRTALLOC(x) __declspec(allocate(x))
24#else
25#define _CRTALLOC(x) __attribute__((section(x)))
26#endif
27
28#ifdef _MSC_VER
29#pragma section(".CRT$XIC", long, read)
30#pragma section(".CRT$XPX", long, read)
31#pragma section(".CRT$XPXA", long, read)
32
33#pragma section(".CRT$XIA", long, read)
34#pragma section(".CRT$XIAA", long, read) // CRT startup
35#pragma section(".CRT$XIZ", long, read)
36#pragma section(".CRT$XCA", long, read)
37#pragma section(".CRT$XCZ", long, read)
38#pragma section(".CRT$XPA", long, read)
39#pragma section(".CRT$XPZ", long, read)
40#pragma section(".CRT$XTA", long, read)
41#pragma section(".CRT$XTZ", long, read)
42
43
44#pragma section(".CRT$XDA",long,read)
45#pragma section(".CRT$XDC",long,read)
46#pragma section(".CRT$XDZ",long,read)
47
48#pragma section(".CRT$XLA",long,read) // TLS callback start
49#pragma section(".CRT$XLC",long,read) // TLS constructors
50#pragma section(".CRT$XLD",long,read) // TLS destructors
51#pragma section(".CRT$XLZ",long,read) // TLS callback end
52
53#pragma section(".tls",long,read,write)
54#pragma section(".tls$AAA",long,read,write)
55#pragma section(".tls$ZZZ",long,read,write)
56#endif
57
58extern _PIFV __xi_a[];
59extern _PIFV __xi_z[];
60extern _PVFV __xc_a[];
61extern _PVFV __xc_z[];
62extern _PVFV __xp_a[];
63extern _PVFV __xp_z[];
64extern _PVFV __xt_a[];
65extern _PVFV __xt_z[];
66
67extern char __ImageBase;
68
70
71#define CRT_WARNING_DISABLE_PUSH(wn, message) \
72 __pragma(warning(push)) \
73 __pragma(warning(disable: wn))
74
75#define CRT_WARNING_POP \
76 __pragma(warning(pop))
77
78#define _BEGIN_SECURE_CRT_DEPRECATION_DISABLE \
79 __pragma(warning(push)) \
80 __pragma(warning(disable: 4996))
81
82#define _END_SECURE_CRT_DEPRECATION_DISABLE \
83 __pragma(warning(pop))
84
85#define _CRT_DEBUGGER_IGNORE -1
86#define _CRT_DEBUGGER_GSFAILURE 1
87#define _CRT_DEBUGGER_INVALIDPARAMETER 2
88#define _CRT_DEBUGGER_ABORT 3
89
90#define _CRT_DEBUGGER_HOOK(x)
91
92#define _CRT_SECURITYCRITICAL_ATTRIBUTE
93#define _CRT_SECURITYSAFECRITICAL_ATTRIBUTE
94
95//-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
96//
97// Parameter validation macros
98// Partly duplicated in corecrt_internal_strtox.h
99//
100//-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
101
102#ifdef _DEBUG
103 #define _INVALID_PARAMETER(expr) _invalid_parameter(expr, __FUNCTIONW__, __FILEW__, __LINE__, 0)
104#else
105 #define _INVALID_PARAMETER(expr) _invalid_parameter_noinfo()
106#endif
107
108#define _VALIDATE_RETURN(expr, errorcode, retexpr) \
109 { \
110 int _Expr_val = !!(expr); \
111 _ASSERT_EXPR((_Expr_val), _CRT_WIDE(#expr)); \
112 if (!(_Expr_val)) \
113 { \
114 *_errno() = (errorcode); \
115 _INVALID_PARAMETER(_CRT_WIDE(#expr)); \
116 return (retexpr); \
117 } \
118 }
119
120#define _VALIDATE_RETURN_VOID(expr, errorcode) \
121 { \
122 int _Expr_val = !!(expr); \
123 _ASSERT_EXPR((_Expr_val), _CRT_WIDE(#expr)); \
124 if (!(_Expr_val)) \
125 { \
126 *_errno() = (errorcode); \
127 _INVALID_PARAMETER(_CRT_WIDE(#expr)); \
128 return; \
129 } \
130 }
131
132#define _VALIDATE_RETURN_NOERRNO(expr, retexpr) \
133 { \
134 int _Expr_val = !!(expr); \
135 _ASSERT_EXPR((_Expr_val), _CRT_WIDE(#expr)); \
136 if (!(_Expr_val)) \
137 { \
138 _INVALID_PARAMETER(_CRT_WIDE(#expr)); \
139 return (retexpr); \
140 } \
141 }
142
143#define _VALIDATE_RETURN_ERRCODE(expr, errorcode) \
144 _VALIDATE_RETURN(expr, errorcode, errorcode)
145
146#define _VALIDATE_CLEAR_OSSERR_RETURN(expr, errorcode, retexpr) \
147 { \
148 int _Expr_val = !!(expr); \
149 _ASSERT_EXPR((_Expr_val), _CRT_WIDE(#expr)); \
150 if (!(_Expr_val)) \
151 { \
152 *__doserrno() = 0L; \
153 *_errno() = errorcode; \
154 _INVALID_PARAMETER(_CRT_WIDE(#expr) ); \
155 return (retexpr); \
156 } \
157 }
158
159#define _VALIDATE_CLEAR_OSSERR_RETURN_ERRCODE(expr, errorcode) \
160 _VALIDATE_CLEAR_OSSERR_RETURN(expr, errorcode, errorcode)
161
162#define _VALIDATE_RETURN_NOEXC(expr, errorcode, retexpr) \
163 { \
164 if (!(expr)) \
165 { \
166 *_errno() = errorcode; \
167 return (retexpr); \
168 } \
169 }
170
171#define _VALIDATE_RETURN_ERRCODE_NOEXC(expr, errorcode) \
172 _VALIDATE_RETURN_NOEXC(expr, errorcode, errorcode)
173
174#define _malloc_crt malloc
175#define _free_crt free
176#define _calloc_crt calloc
177#define _recalloc_crt _recalloc
178#define _malloca_crt _malloca
179#define _freea_crt _freea
180
181#ifdef __cplusplus
182} // extern "C"
183
184template<typename T>
185__forceinline
186T __crt_fast_encode_pointer(T ptr)
187{
188 return reinterpret_cast<T>(
189 reinterpret_cast<uintptr_t>(ptr) ^ __security_cookie);
190}
191
192// Workaround for nullptr encoding.
193// Casting nullptr_t to uinttptr_t is not allowed in C++. Using a union compiles,
194// but GCC will optimize it to a constant zero, which is not what we want.
195class encoded_nullptr_t
196{
197 void* m_ptr;
198public:
199 inline encoded_nullptr_t(void* P) : m_ptr(P) { };
200 template<typename T> operator T*() { return (T*)m_ptr; }
201};
202
203__forceinline
204encoded_nullptr_t __crt_fast_encode_pointer(decltype(nullptr) ptr)
205{
206 void* encoded_null = __crt_fast_encode_pointer((void*)ptr);
207 return encoded_nullptr_t(encoded_null);
208}
209
210template<typename T>
211__forceinline
212T __crt_fast_decode_pointer(T ptr)
213{
214 return reinterpret_cast<T>(
215 reinterpret_cast<uintptr_t>(ptr) ^ __security_cookie);
216}
217
218template<typename T>
219T __crt_interlocked_read(const T volatile* ptr)
220{
221 return *ptr;
222}
223
224template<typename T>
225T __crt_interlocked_read_pointer(const T volatile* ptr)
226{
227 return *ptr;
228}
229
230template<typename T>
231T __crt_interlocked_exchange_pointer(T *ptr, void* value)
232{
233 return (T)_InterlockedExchangePointer((void* volatile*)ptr, value);
234}
235
236template<typename T>
237struct __crt_seh_guarded_call
238{
239 template<typename Init, typename Action, typename Cleanup>
240 T operator()(Init init, Action action, Cleanup cleanup)
241 {
242 T result;
243 init();
244 __try
245 {
246 result = action();
247 }
249 {
250 cleanup();
251 }
253 return result;
254 }
255};
256
257template<>
258struct __crt_seh_guarded_call<void>
259{
260 template<typename Init, typename Action, typename Cleanup>
261 void operator()(Init init, Action action, Cleanup cleanup)
262 {
263 init();
264 __try
265 {
266 action();
267 }
269 {
270 cleanup();
271 }
273 }
274};
275
276template<typename FuncPtr>
277FuncPtr __crt_get_proc_address(HMODULE module, const char* name)
278{
279 return (FuncPtr)GetProcAddress(module, name);
280}
281
282template<typename Action, typename Cleanup>
283void __crt_call_and_cleanup(Action action, Cleanup cleanup)
284{
285 action();
286 cleanup();
287}
288
289struct __crt_malloc_free_policy
290{
291 template<typename T>
292 void operator()(T* const ptr) throw()
293 {
294 _free_crt((void*)ptr);
295 }
296};
297
298struct __crt_public_free_policy
299{
300 template<typename T>
301 void operator()(T* const ptr) throw()
302 {
303 _free_crt((void*)ptr);
304 }
305};
306
307struct __crt_malloca_free_policy
308{
309 void operator()(void* const ptr) throw()
310 {
312 }
313};
314
315template <typename T, typename FreePolicy = __crt_malloc_free_policy>
316class __crt_unique_heap_ptr
317{
318 T* _ptr;
319public:
320 __crt_unique_heap_ptr() : _ptr(nullptr) {}
321 __crt_unique_heap_ptr(T* ptr) : _ptr(ptr) {}
322 __crt_unique_heap_ptr(__crt_unique_heap_ptr&& from) : _ptr(from._ptr) { from._ptr = nullptr; }
323 ~__crt_unique_heap_ptr() { FreePolicy()(_ptr); }
324 void attach(T* ptr) { FreePolicy()(_ptr); _ptr = ptr; }
325 T* detach() { T* ptr = _ptr; _ptr = nullptr; return ptr; }
326 operator bool() const { return _ptr != nullptr; }
327 T* get() const { return _ptr; }
328 T** get_address_of() { return &_ptr; }
329
330 // Move assignment
331 __crt_unique_heap_ptr& operator=(__crt_unique_heap_ptr&& from)
332 {
333 FreePolicy()(_ptr);
334 _ptr = from._ptr;
335 from._ptr = nullptr;
336 return *this;
337 }
338};
339
340template <typename T>
341using __crt_scoped_stack_ptr = __crt_unique_heap_ptr<T, __crt_malloca_free_policy>;
342
343#define _malloc_crt_t(t, n) (__crt_unique_heap_ptr<t>(static_cast<t*>(_malloc_crt((n) * sizeof(t)))))
344#define _calloc_crt_t(t, n) (__crt_unique_heap_ptr<t>(static_cast<t*>(_calloc_crt((n), sizeof(t)))))
345#define _recalloc_crt_t(t, p, n) (__crt_unique_heap_ptr<t>(static_cast<t*>(_recalloc_crt(p, (n), sizeof(t)))))
346#define _malloca_crt_t(t, n) (__crt_scoped_stack_ptr<t>(static_cast<t*>(_malloca_crt((n) * sizeof(t)))))
347
348#endif // __cplusplus
void get(int argc, const char *argv[])
Definition: cmds.c:480
int(__cdecl * _PIFV)(void)
void(__cdecl * _PVFV)(void)
#define P(row, col)
#define GetProcAddress(x, y)
Definition: compat.h:753
static void cleanup(void)
Definition: main.c:1335
static const WCHAR Cleanup[]
Definition: register.c:80
action
Definition: namespace.c:707
return nullptr
Definition: expand.cpp:78
GLuint64EXT * result
Definition: glext.h:11304
char __ImageBase
_PVFV __xp_z[]
#define _free_crt
_PVFV __xp_a[]
#define _freea_crt
_PVFV __xc_a[]
_PVFV __xc_z[]
_PVFV __xt_a[]
const IMAGE_TLS_DIRECTORY _tls_used
_PVFV __xt_z[]
_PIFV __xi_z[]
_PIFV __xi_a[]
void * _InterlockedExchangePointer(_Interlocked_operand_ void *volatile *_Target, void *_Value)
unsigned int uintptr_t
Definition: intrin.h:47
static PVOID ptr
Definition: dispmode.c:27
#define bool
Definition: nsiface.idl:72
#define __try
Definition: pseh2_64.h:188
#define __endtry
Definition: pseh2_64.h:191
#define __finally
Definition: pseh2_64.h:190
#define T(num)
Definition: thunks.c:311
uintptr_t __security_cookie
Definition: gs_support.c:49
CardRegion * from
Definition: spigame.cpp:19
Definition: name.c:39
Definition: pdh_main.c:96
_In_ WDFIOTARGET _In_ _Strict_type_match_ WDF_IO_TARGET_SENT_IO_ACTION Action
Definition: wdfiotarget.h:510
static int init
Definition: wintirpc.c:33