-
Notifications
You must be signed in to change notification settings - Fork 4.9k
/
Copy pathclrnt.h
504 lines (398 loc) · 14.1 KB
/
clrnt.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
#ifndef CLRNT_H_
#define CLRNT_H_
#include "staticcontract.h"
#include "cfi.h"
//
// ALL PLATFORMS
//
#define STATUS_INVALID_PARAMETER_3 ((NTSTATUS)0xC00000F1L)
#define STATUS_INVALID_PARAMETER_4 ((NTSTATUS)0xC00000F2L)
#define STATUS_UNSUCCESSFUL ((NTSTATUS)0xC0000001L)
#define STATUS_SUCCESS ((NTSTATUS)0x00000000L)
#ifndef STATUS_UNWIND
#define STATUS_UNWIND ((NTSTATUS)0x80000027L)
#endif
#ifndef DBG_PRINTEXCEPTION_C
#define DBG_PRINTEXCEPTION_C ((DWORD)0x40010006L)
#endif
#ifndef STATUS_UNWIND_CONSOLIDATE
#define STATUS_UNWIND_CONSOLIDATE ((NTSTATUS)0x80000029L)
#endif
#ifndef STATUS_LONGJUMP
#define STATUS_LONGJUMP ((NTSTATUS)0x80000026L)
#endif
#ifndef LOCALE_NAME_MAX_LENGTH
#define LOCALE_NAME_MAX_LENGTH 85
#endif // !LOCALE_NAME_MAX_LENGTH
#ifndef IMAGE_FILE_MACHINE_RISCV64
#define IMAGE_FILE_MACHINE_RISCV64 0x5064 // RISCV64
#endif // !IMAGE_FILE_MACHINE_RISCV64
#ifndef __out_xcount_opt
#define __out_xcount_opt(var) __out
#endif
#ifndef __encoded_pointer
#define __encoded_pointer
#endif
#ifndef __range
#define __range(min, man)
#endif
#ifndef __field_bcount
#define __field_bcount(size)
#endif
#ifndef __field_ecount_opt
#define __field_ecount_opt(nFields)
#endif
#ifndef __field_ecount
#define __field_ecount(EHCount)
#endif
#undef _Ret_bytecap_
#define _Ret_bytecap_(_Size)
#ifndef NT_SUCCESS
#define NT_SUCCESS(Status) (((NTSTATUS)(Status)) >= 0)
#endif
#define ARGUMENT_PRESENT(ArgumentPointer) (\
(CHAR *)(ArgumentPointer) != (CHAR *)(NULL) )
#define EXCEPTION_CHAIN_END ((PEXCEPTION_REGISTRATION_RECORD)-1)
typedef signed char SCHAR;
typedef SCHAR *PSCHAR;
typedef LONG NTSTATUS;
#ifdef HOST_WINDOWS
#define TLS_EXPANSION_SLOTS 1024
// Included for TEB::ReservedForOle, TlsSlots, TlsExpansionSlots
#include <winternl.h>
// Alias for TEB::ThreadLocalStoragePointer
#define ThreadLocalStoragePointer Reserved1[11]
#endif
#if !defined(TARGET_X86)
typedef enum _FUNCTION_TABLE_TYPE {
RF_SORTED,
RF_UNSORTED,
RF_CALLBACK
} FUNCTION_TABLE_TYPE;
typedef struct _DYNAMIC_FUNCTION_TABLE {
LIST_ENTRY Links;
PT_RUNTIME_FUNCTION FunctionTable;
LARGE_INTEGER TimeStamp;
#ifdef TARGET_ARM
ULONG MinimumAddress;
ULONG MaximumAddress;
ULONG BaseAddress;
#else
ULONG64 MinimumAddress;
ULONG64 MaximumAddress;
ULONG64 BaseAddress;
#endif
PGET_RUNTIME_FUNCTION_CALLBACK Callback;
PVOID Context;
PWSTR OutOfProcessCallbackDll;
FUNCTION_TABLE_TYPE Type;
ULONG EntryCount;
} DYNAMIC_FUNCTION_TABLE, *PDYNAMIC_FUNCTION_TABLE;
#endif // !TARGET_X86
//
// AMD64
//
#ifdef TARGET_AMD64
#define RUNTIME_FUNCTION__BeginAddress(prf) (prf)->BeginAddress
#define RUNTIME_FUNCTION__SetBeginAddress(prf,address) ((prf)->BeginAddress = (address))
#define RUNTIME_FUNCTION__EndAddress(prf, ImageBase) (prf)->EndAddress
#define RUNTIME_FUNCTION__GetUnwindInfoAddress(prf) (prf)->UnwindData
#define RUNTIME_FUNCTION__SetUnwindInfoAddress(prf,address) do { (prf)->UnwindData = (address); } while (0)
#define OFFSETOF__RUNTIME_FUNCTION__UnwindInfoAddress offsetof(T_RUNTIME_FUNCTION, UnwindData)
#include "win64unwind.h"
typedef
PEXCEPTION_ROUTINE
(RtlVirtualUnwindFn) (
IN ULONG HandlerType,
IN ULONG64 ImageBase,
IN ULONG64 ControlPc,
IN PT_RUNTIME_FUNCTION FunctionEntry,
IN OUT PCONTEXT ContextRecord,
OUT PVOID *HandlerData,
OUT PULONG64 EstablisherFrame,
IN OUT PKNONVOLATILE_CONTEXT_POINTERS ContextPointers OPTIONAL
);
#ifndef HOST_UNIX
extern RtlVirtualUnwindFn* RtlVirtualUnwind_Unsafe;
#else // !HOST_UNIX
PEXCEPTION_ROUTINE
RtlVirtualUnwind_Unsafe(
IN ULONG HandlerType,
IN ULONG64 ImageBase,
IN ULONG64 ControlPc,
IN PT_RUNTIME_FUNCTION FunctionEntry,
IN OUT PCONTEXT ContextRecord,
OUT PVOID *HandlerData,
OUT PULONG64 EstablisherFrame,
IN OUT PKNONVOLATILE_CONTEXT_POINTERS ContextPointers OPTIONAL
);
#endif // !HOST_UNIX
#endif // TARGET_AMD64
//
// X86
//
#ifdef TARGET_X86
#ifndef HOST_UNIX
//
// x86 ABI does not define RUNTIME_FUNCTION. Define our own to allow unification between x86 and other platforms.
//
#ifdef HOST_X86
typedef struct _RUNTIME_FUNCTION {
DWORD BeginAddress;
// NOTE: R2R doesn't include EndAddress (see docs/design/coreclr/botr/readytorun-format.md).
// NativeAOT does include the EndAddress because the Microsoft linker expects it. In NativeAOT
// the info is generated in the managed ObjectWriter, so the structures don't have to match.
// DWORD EndAddress;
DWORD UnwindData;
} RUNTIME_FUNCTION, *PRUNTIME_FUNCTION;
typedef struct _DISPATCHER_CONTEXT {
_EXCEPTION_REGISTRATION_RECORD* RegistrationPointer;
} DISPATCHER_CONTEXT, *PDISPATCHER_CONTEXT;
#endif // HOST_X86
#endif // !HOST_UNIX
#define RUNTIME_FUNCTION__BeginAddress(prf) (prf)->BeginAddress
#define RUNTIME_FUNCTION__SetBeginAddress(prf,addr) ((prf)->BeginAddress = (addr))
#include "win64unwind.h"
#include "daccess.h"
FORCEINLINE
DWORD
RtlpGetFunctionEndAddress (
_In_ PT_RUNTIME_FUNCTION FunctionEntry,
_In_ TADDR ImageBase
)
{
PUNWIND_INFO pUnwindInfo = (PUNWIND_INFO)(ImageBase + FunctionEntry->UnwindData);
return FunctionEntry->BeginAddress + pUnwindInfo->FunctionLength;
}
#define RUNTIME_FUNCTION__EndAddress(prf, ImageBase) RtlpGetFunctionEndAddress(prf, ImageBase)
#define RUNTIME_FUNCTION__GetUnwindInfoAddress(prf) (prf)->UnwindData
#define RUNTIME_FUNCTION__SetUnwindInfoAddress(prf, addr) do { (prf)->UnwindData = (addr); } while(0)
#ifdef HOST_X86
PEXCEPTION_ROUTINE
RtlVirtualUnwind (
_In_ DWORD HandlerType,
_In_ DWORD ImageBase,
_In_ DWORD ControlPc,
_In_ PRUNTIME_FUNCTION FunctionEntry,
__inout PT_CONTEXT ContextRecord,
_Out_ PVOID *HandlerData,
_Out_ PDWORD EstablisherFrame,
__inout_opt PT_KNONVOLATILE_CONTEXT_POINTERS ContextPointers
);
#endif // HOST_X86
#endif // TARGET_X86
#ifdef TARGET_ARM
#include "daccess.h"
//
// Define unwind information flags.
//
#define UNW_FLAG_NHANDLER 0x0 /* any handler */
#define UNW_FLAG_EHANDLER 0x1 /* filter handler */
#define UNW_FLAG_UHANDLER 0x2 /* unwind handler */
// This function returns the length of a function using the new unwind info on arm.
// Taken from minkernel\ntos\rtl\arm\ntrtlarm.h.
FORCEINLINE
ULONG
RtlpGetFunctionEndAddress (
_In_ PT_RUNTIME_FUNCTION FunctionEntry,
_In_ TADDR ImageBase
)
{
ULONG FunctionLength;
FunctionLength = FunctionEntry->UnwindData;
if ((FunctionLength & 3) != 0) {
FunctionLength = (FunctionLength >> 2) & 0x7ff;
} else {
FunctionLength = *(PTR_ULONG)(ImageBase + FunctionLength) & 0x3ffff;
}
return FunctionEntry->BeginAddress + 2 * FunctionLength;
}
#define RUNTIME_FUNCTION__BeginAddress(FunctionEntry) ThumbCodeToDataPointer<DWORD,DWORD>((FunctionEntry)->BeginAddress)
#define RUNTIME_FUNCTION__SetBeginAddress(FunctionEntry,address) ((FunctionEntry)->BeginAddress = DataPointerToThumbCode<DWORD,DWORD>(address))
#define RUNTIME_FUNCTION__EndAddress(FunctionEntry, ImageBase) ThumbCodeToDataPointer<DWORD,DWORD>(RtlpGetFunctionEndAddress(FunctionEntry, ImageBase))
#define RUNTIME_FUNCTION__SetUnwindInfoAddress(prf,address) do { (prf)->UnwindData = (address); } while (0)
typedef struct _UNWIND_INFO {
// dummy
} UNWIND_INFO, *PUNWIND_INFO;
#if defined(HOST_UNIX) || defined(HOST_X86)
EXTERN_C
NTSYSAPI
PEXCEPTION_ROUTINE
NTAPI
RtlVirtualUnwind (
_In_ DWORD HandlerType,
_In_ DWORD ImageBase,
_In_ DWORD ControlPc,
_In_ PT_RUNTIME_FUNCTION FunctionEntry,
__inout PT_CONTEXT ContextRecord,
_Out_ PVOID *HandlerData,
_Out_ PDWORD EstablisherFrame,
__inout_opt PT_KNONVOLATILE_CONTEXT_POINTERS ContextPointers
);
#endif // HOST_UNIX || HOST_X86
#define UNW_FLAG_NHANDLER 0x0
#endif // TARGET_ARM
#ifdef TARGET_ARM64
#include "daccess.h"
#define UNW_FLAG_NHANDLER 0x0 /* any handler */
#define UNW_FLAG_EHANDLER 0x1 /* filter handler */
#define UNW_FLAG_UHANDLER 0x2 /* unwind handler */
// This function returns the RVA of the end of the function (exclusive, so one byte after the actual end)
// using the unwind info on ARM64. (see ExternalAPIs\Win9CoreSystem\inc\winnt.h)
FORCEINLINE
ULONG64
RtlpGetFunctionEndAddress (
_In_ PT_RUNTIME_FUNCTION FunctionEntry,
_In_ ULONG64 ImageBase
)
{
ULONG64 FunctionLength;
FunctionLength = FunctionEntry->UnwindData;
if ((FunctionLength & 3) != 0)
{
// Compact form pdata.
if ((FunctionLength & 7) == 3)
{
// Long branch pdata, by standard this is 3 so size is 12.
FunctionLength = 3;
}
else
{
FunctionLength = (FunctionLength >> 2) & 0x7ff;
}
}
else
{
// Get from the xdata record.
FunctionLength = *(PTR_ULONG64)(ImageBase + FunctionLength) & 0x3ffff;
}
return FunctionEntry->BeginAddress + 4 * FunctionLength;
}
#define RUNTIME_FUNCTION__BeginAddress(FunctionEntry) ((FunctionEntry)->BeginAddress)
#define RUNTIME_FUNCTION__SetBeginAddress(FunctionEntry,address) ((FunctionEntry)->BeginAddress = (address))
#define RUNTIME_FUNCTION__EndAddress(FunctionEntry, ImageBase) (RtlpGetFunctionEndAddress(FunctionEntry, (ULONG64)(ImageBase)))
#define RUNTIME_FUNCTION__SetUnwindInfoAddress(prf,address) do { (prf)->UnwindData = (address); } while (0)
typedef struct _UNWIND_INFO {
// dummy
} UNWIND_INFO, *PUNWIND_INFO;
EXTERN_C
NTSYSAPI
PEXCEPTION_ROUTINE
NTAPI
RtlVirtualUnwind(
IN ULONG HandlerType,
IN ULONG64 ImageBase,
IN ULONG64 ControlPc,
IN PRUNTIME_FUNCTION FunctionEntry,
IN OUT PCONTEXT ContextRecord,
OUT PVOID *HandlerData,
OUT PULONG64 EstablisherFrame,
IN OUT PKNONVOLATILE_CONTEXT_POINTERS ContextPointers OPTIONAL
);
// Mirror the XSTATE_ARM64_SVE flags from winnt.h
#ifndef XSTATE_ARM64_SVE
#define XSTATE_ARM64_SVE (2)
#endif // XSTATE_ARM64_SVE
#ifndef XSTATE_MASK_ARM64_SVE
#define XSTATE_MASK_ARM64_SVE (1ui64 << (XSTATE_ARM64_SVE))
#endif // XSTATE_MASK_ARM64_SVE
#ifndef CONTEXT_ARM64_XSTATE
#define CONTEXT_ARM64_XSTATE (CONTEXT_ARM64 | 0x20L)
#endif // CONTEXT_ARM64_XSTATE
#ifndef CONTEXT_XSTATE
#define CONTEXT_XSTATE CONTEXT_ARM64_XSTATE
#endif // CONTEXT_XSTATE
#endif
#ifdef TARGET_LOONGARCH64
#include "daccess.h"
#define UNW_FLAG_NHANDLER 0x0 /* any handler */
#define UNW_FLAG_EHANDLER 0x1 /* filter handler */
#define UNW_FLAG_UHANDLER 0x2 /* unwind handler */
// This function returns the RVA of the end of the function (exclusive, so one byte after the actual end)
// using the unwind info on LOONGARCH64. (see ExternalAPIs\Win9CoreSystem\inc\winnt.h)
FORCEINLINE
ULONG64
RtlpGetFunctionEndAddress (
_In_ PT_RUNTIME_FUNCTION FunctionEntry,
_In_ ULONG64 ImageBase
)
{
ULONG64 FunctionLength;
FunctionLength = FunctionEntry->UnwindData;
if ((FunctionLength & 3) != 0) {
FunctionLength = (FunctionLength >> 2) & 0x7ff;
} else {
FunctionLength = *(PTR_ULONG64)(ImageBase + FunctionLength) & 0x3ffff;
}
return FunctionEntry->BeginAddress + 4 * FunctionLength;
}
#define RUNTIME_FUNCTION__BeginAddress(FunctionEntry) ((FunctionEntry)->BeginAddress)
#define RUNTIME_FUNCTION__SetBeginAddress(FunctionEntry,address) ((FunctionEntry)->BeginAddress = (address))
#define RUNTIME_FUNCTION__EndAddress(FunctionEntry, ImageBase) (RtlpGetFunctionEndAddress(FunctionEntry, (ULONG64)(ImageBase)))
#define RUNTIME_FUNCTION__SetUnwindInfoAddress(prf,address) do { (prf)->UnwindData = (address); } while (0)
typedef struct _UNWIND_INFO {
// dummy
} UNWIND_INFO, *PUNWIND_INFO;
EXTERN_C
NTSYSAPI
PEXCEPTION_ROUTINE
NTAPI
RtlVirtualUnwind(
IN ULONG HandlerType,
IN ULONG64 ImageBase,
IN ULONG64 ControlPc,
IN PRUNTIME_FUNCTION FunctionEntry,
IN OUT PCONTEXT ContextRecord,
OUT PVOID *HandlerData,
OUT PULONG64 EstablisherFrame,
IN OUT PKNONVOLATILE_CONTEXT_POINTERS ContextPointers OPTIONAL
);
#endif // TARGET_LOONGARCH64
#ifdef TARGET_RISCV64
#include "daccess.h"
#define UNW_FLAG_NHANDLER 0x0 /* any handler */
#define UNW_FLAG_EHANDLER 0x1 /* filter handler */
#define UNW_FLAG_UHANDLER 0x2 /* unwind handler */
// This function returns the RVA of the end of the function (exclusive, so one byte after the actual end)
// using the unwind info on ARM64. (see ExternalAPIs\Win9CoreSystem\inc\winnt.h)
FORCEINLINE
ULONG64
RtlpGetFunctionEndAddress (
_In_ PT_RUNTIME_FUNCTION FunctionEntry,
_In_ ULONG64 ImageBase
)
{
ULONG64 FunctionLength;
FunctionLength = FunctionEntry->UnwindData;
if ((FunctionLength & 3) != 0) {
FunctionLength = (FunctionLength >> 2) & 0x7ff;
} else {
FunctionLength = *(PTR_ULONG64)(ImageBase + FunctionLength) & 0x3ffff;
}
return FunctionEntry->BeginAddress + 4 * FunctionLength;
}
#define RUNTIME_FUNCTION__BeginAddress(FunctionEntry) ((FunctionEntry)->BeginAddress)
#define RUNTIME_FUNCTION__SetBeginAddress(FunctionEntry,address) ((FunctionEntry)->BeginAddress = (address))
#define RUNTIME_FUNCTION__EndAddress(FunctionEntry, ImageBase) (RtlpGetFunctionEndAddress(FunctionEntry, (ULONG64)(ImageBase)))
#define RUNTIME_FUNCTION__SetUnwindInfoAddress(prf,address) do { (prf)->UnwindData = (address); } while (0)
typedef struct _UNWIND_INFO {
// dummy
} UNWIND_INFO, *PUNWIND_INFO;
EXTERN_C
NTSYSAPI
PEXCEPTION_ROUTINE
NTAPI
RtlVirtualUnwind(
IN ULONG HandlerType,
IN ULONG64 ImageBase,
IN ULONG64 ControlPc,
IN PRUNTIME_FUNCTION FunctionEntry,
IN OUT PCONTEXT ContextRecord,
OUT PVOID *HandlerData,
OUT PULONG64 EstablisherFrame,
IN OUT PKNONVOLATILE_CONTEXT_POINTERS ContextPointers OPTIONAL
);
#endif // TARGET_RISCV64
#endif // CLRNT_H_