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