-
Notifications
You must be signed in to change notification settings - Fork 4.6k
/
ExceptionHandling.asm
480 lines (362 loc) · 17.8 KB
/
ExceptionHandling.asm
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
;; Licensed to the .NET Foundation under one or more agreements.
;; The .NET Foundation licenses this file to you under the MIT license.
.586
.model flat
option casemap:none
.code
include AsmMacros.inc
RhpCallFunclet equ @RhpCallFunclet@0
RhpThrowHwEx equ @RhpThrowHwEx@0
extern RhpCallFunclet : proc
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;; RhpThrowHwEx
;;
;; INPUT: ECX: exception code of fault
;; EDX: faulting RIP
;;
;; OUTPUT:
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
FASTCALL_FUNC RhpThrowHwEx, 0
esp_offsetof_ExInfo textequ %0
esp_offsetof_Context textequ %SIZEOF__ExInfo
push edx ; make it look like we were called by pushing the faulting IP like a return address
push ebp
mov ebp, esp
lea eax, [esp+8] ;; calculate the RSP of the throw site
;; edx already contains the throw site IP
;; struct PAL_LIMITED_CONTEXT
;; {
push ebx
push eax
push esi
push edi
mov ebx, [ebp]
push ebx ;; 'faulting' Rbp
push eax ;; 'faulting' Rsp
push edx ;; 'faulting' IP
;; };
sub esp, SIZEOF__ExInfo
INLINE_GETTHREAD eax, edx ;; eax <- thread, edx <- trashed
lea edx, [esp + esp_offsetof_ExInfo] ;; edx <- ExInfo*
xor esi, esi
mov [edx + OFFSETOF__ExInfo__m_exception], esi ;; init the exception object to null
mov byte ptr [edx + OFFSETOF__ExInfo__m_passNumber], 1 ;; init to the first pass
mov dword ptr [edx + OFFSETOF__ExInfo__m_idxCurClause], 0FFFFFFFFh
mov byte ptr [edx + OFFSETOF__ExInfo__m_kind], 2 ;; ExKind.HardwareFault
;; link the ExInfo into the thread's ExInfo chain
mov ebx, [eax + OFFSETOF__Thread__m_pExInfoStackHead]
mov [edx + OFFSETOF__ExInfo__m_pPrevExInfo], ebx ;; pExInfo->m_pPrevExInfo = m_pExInfoStackHead
mov [eax + OFFSETOF__Thread__m_pExInfoStackHead], edx ;; m_pExInfoStackHead = pExInfo
;; set the exception context field on the ExInfo
lea ebx, [esp + esp_offsetof_Context] ;; ebx <- PAL_LIMITED_CONTEXT*
mov [edx + OFFSETOF__ExInfo__m_pExContext], ebx ;; init ExInfo.m_pExContext
;; ecx still contains the exception code
;; edx contains the address of the ExInfo
call RhThrowHwEx
EXPORT_POINTER_TO_ADDRESS _PointerToRhpThrowHwEx2
;; no return
int 3
FASTCALL_ENDFUNC
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;; RhpThrowEx
;;
;; INPUT: ECX: exception object
;;
;; OUTPUT:
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
FASTCALL_FUNC RhpThrowEx, 0
esp_offsetof_ExInfo textequ %0
esp_offsetof_Context textequ %SIZEOF__ExInfo
push ebp
mov ebp, esp
lea eax, [esp+8] ;; calculate the RSP of the throw site
mov edx, [esp+4] ;; get the throw site IP via the return address
;; struct PAL_LIMITED_CONTEXT
;; {
push ebx
push eax
push esi
push edi
mov ebx, [ebp]
push ebx ;; 'faulting' Rbp
push eax ;; 'faulting' Rsp
push edx ;; 'faulting' IP
;; };
sub esp, SIZEOF__ExInfo
;; -------------------------
lea ebx, [eax-4] ;; ebx <- addr of return address
INLINE_GETTHREAD eax, edx ;; eax <- thread, edx <- trashed
;; There is runtime C# code that can tail call to RhpThrowEx using a binder intrinsic. So the return
;; address could have been hijacked when we were in that C# code and we must remove the hijack and
;; reflect the correct return address in our exception context record. The other throw helpers don't
;; need this because they cannot be tail-called from C#.
INLINE_THREAD_UNHIJACK eax, esi, edx ;; trashes esi, edx
mov edx, [ebx] ;; edx <- return address
mov [esp + esp_offsetof_Context + OFFSETOF__PAL_LIMITED_CONTEXT__IP], edx ;; set 'faulting' IP after unhijack
lea edx, [esp + esp_offsetof_ExInfo] ;; edx <- ExInfo*
xor esi, esi
mov [edx + OFFSETOF__ExInfo__m_exception], esi ;; init the exception object to null
mov byte ptr [edx + OFFSETOF__ExInfo__m_passNumber], 1 ;; init to the first pass
mov dword ptr [edx + OFFSETOF__ExInfo__m_idxCurClause], 0FFFFFFFFh
mov byte ptr [edx + OFFSETOF__ExInfo__m_kind], 1 ;; ExKind.Throw
;; link the ExInfo into the thread's ExInfo chain
mov ebx, [eax + OFFSETOF__Thread__m_pExInfoStackHead]
mov [edx + OFFSETOF__ExInfo__m_pPrevExInfo], ebx ;; pExInfo->m_pPrevExInfo = m_pExInfoStackHead
mov [eax + OFFSETOF__Thread__m_pExInfoStackHead], edx ;; m_pExInfoStackHead = pExInfo
;; set the exception context field on the ExInfo
lea ebx, [esp + esp_offsetof_Context] ;; ebx <- PAL_LIMITED_CONTEXT*
mov [edx + OFFSETOF__ExInfo__m_pExContext], ebx ;; init ExInfo.m_pExContext
;; ecx still contains the exception object
;; edx contains the address of the ExInfo
call RhThrowEx
EXPORT_POINTER_TO_ADDRESS _PointerToRhpThrowEx2
;; no return
int 3
FASTCALL_ENDFUNC
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;; void FASTCALL RhpRethrow()
;;
;; SUMMARY: Similar to RhpThrowEx, except that it passes along the currently active ExInfo
;;
;; INPUT:
;;
;; OUTPUT:
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
FASTCALL_FUNC RhpRethrow, 0
esp_offsetof_ExInfo textequ %0
esp_offsetof_Context textequ %SIZEOF__ExInfo
push ebp
mov ebp, esp
lea eax, [esp+8] ;; calculate the RSP of the throw site
mov edx, [esp+4] ;; get the throw site IP via the return address
;; struct PAL_LIMITED_CONTEXT
;; {
push ebx
push eax
push esi
push edi
mov ebx, [ebp]
push ebx ;; 'faulting' Rbp
push eax ;; 'faulting' Rsp
push edx ;; 'faulting' IP
;; };
sub esp, SIZEOF__ExInfo
;; -------------------------
lea ebx, [eax-4] ;; ebx <- addr of return address
INLINE_GETTHREAD eax, edx ;; eax <- thread, edx <- trashed
lea edx, [esp + esp_offsetof_ExInfo] ;; edx <- ExInfo*
xor esi, esi
mov [edx + OFFSETOF__ExInfo__m_exception], esi ;; init the exception object to null
mov byte ptr [edx + OFFSETOF__ExInfo__m_passNumber], 1 ;; init to the first pass
mov dword ptr [edx + OFFSETOF__ExInfo__m_idxCurClause], 0FFFFFFFFh
mov byte ptr [edx + OFFSETOF__ExInfo__m_kind], 0 ;; init to a deterministic value (ExKind.None)
;; link the ExInfo into the thread's ExInfo chain
mov ecx, [eax + OFFSETOF__Thread__m_pExInfoStackHead] ;; ecx <- currently active ExInfo
mov [edx + OFFSETOF__ExInfo__m_pPrevExInfo], ecx ;; pExInfo->m_pPrevExInfo = m_pExInfoStackHead
mov [eax + OFFSETOF__Thread__m_pExInfoStackHead], edx ;; m_pExInfoStackHead = pExInfo
;; set the exception context field on the ExInfo
lea ebx, [esp + esp_offsetof_Context] ;; ebx <- PAL_LIMITED_CONTEXT*
mov [edx + OFFSETOF__ExInfo__m_pExContext], ebx ;; init ExInfo.m_pExContext
;; ecx contains the currently active ExInfo
;; edx contains the address of the new ExInfo
call RhRethrow
EXPORT_POINTER_TO_ADDRESS _PointerToRhpRethrow2
;; no return
int 3
FASTCALL_ENDFUNC
;;
;; Prologue of all funclet calling helpers (RhpCallXXXXFunclet)
;;
FUNCLET_CALL_PROLOGUE macro localsCount
push ebp
mov ebp, esp
push ebx ;; save preserved registers (for the stackwalker)
push esi ;;
push edi ;;
stack_alloc_size = localsCount * 4
if stack_alloc_size ne 0
sub esp, stack_alloc_size
endif
endm
;;
;; Epilogue of all funclet calling helpers (RhpCallXXXXFunclet)
;;
FUNCLET_CALL_EPILOGUE macro
if stack_alloc_size ne 0
add esp, stack_alloc_size
endif
pop edi
pop esi
pop ebx
pop ebp
endm
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;; void* FASTCALL RhpCallCatchFunclet(RtuObjectRef exceptionObj, void* pHandlerIP, REGDISPLAY* pRegDisplay,
;; ExInfo* pExInfo)
;;
;; INPUT: ECX: exception object
;; EDX: handler funclet address
;; [ESP + 4]: REGDISPLAY*
;; [ESP + 8]: ExInfo*
;;
;; OUTPUT:
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
FASTCALL_FUNC RhpCallCatchFunclet, 0
FUNCLET_CALL_PROLOGUE 2
esp_offsetof_ResumeIP textequ %00h ;; [esp + 00h]: continuation address
esp_offsetof_is_handling_thread_abort textequ %04h ;; [esp + 04h]: set if we are handling ThreadAbortException
;; [esp + 08h]: edi save
;; [esp + 0ch]: esi save
;; [esp + 10h]: ebx save
esp_offsetof_PrevEBP textequ %14h ;; [esp + 14h]: prev ebp
esp_offsetof_RetAddr textequ %18h ;; [esp + 18h]: return address
esp_offsetof_RegDisplay textequ %1ch ;; [esp + 1Ch]: REGDISPLAY*
esp_offsetof_ExInfo textequ %20h ;; [esp + 20h]: ExInfo*
;; Clear the DoNotTriggerGc state before calling out to our managed catch funclet.
INLINE_GETTHREAD eax, ebx ;; eax <- Thread*, ebx is trashed
lock and dword ptr [eax + OFFSETOF__Thread__m_ThreadStateFlags], NOT TSF_DoNotTriggerGc
cmp ecx, [eax + OFFSETOF__Thread__m_threadAbortException]
setz byte ptr [esp + esp_offsetof_is_handling_thread_abort]
mov edi, [esp + esp_offsetof_RegDisplay] ;; edi <- REGDISPLAY *
mov eax, [edi + OFFSETOF__REGDISPLAY__pRbx]
mov ebx, [eax]
mov eax, [edi + OFFSETOF__REGDISPLAY__pRbp]
mov eax, [eax]
push eax ; save the funclet's EBP value for later
mov eax, [edi + OFFSETOF__REGDISPLAY__pRsi]
mov esi, [eax]
mov eax, [edi + OFFSETOF__REGDISPLAY__pRdi]
mov edi, [eax]
pop eax ; get the funclet's EBP value
;; ECX still contains the exception object
;; EDX: funclet IP
;; EAX: funclet EBP
call RhpCallFunclet
EXPORT_POINTER_TO_ADDRESS _PointerToRhpCallCatchFunclet2
;; eax: resume IP
mov [esp + esp_offsetof_ResumeIP], eax ;; save for later
INLINE_GETTHREAD edx, ecx ;; edx <- Thread*, trash ecx
;; We must unhijack the thread at this point because the section of stack where the hijack is applied
;; may go dead. If it does, then the next time we try to unhijack the thread, it will corrupt the stack.
INLINE_THREAD_UNHIJACK edx, ecx, eax ;; Thread in edx, trashes ecx and eax
mov ecx, [esp + esp_offsetof_ExInfo] ;; ecx <- current ExInfo *
mov eax, [esp + esp_offsetof_RegDisplay] ;; eax <- REGDISPLAY*
mov eax, [eax + OFFSETOF__REGDISPLAY__SP] ;; eax <- resume SP value
@@: mov ecx, [ecx + OFFSETOF__ExInfo__m_pPrevExInfo] ;; ecx <- next ExInfo
cmp ecx, 0
je @F ;; we're done if it's null
cmp ecx, eax
jl @B ;; keep looping if it's lower than the new SP
@@: mov [edx + OFFSETOF__Thread__m_pExInfoStackHead], ecx ;; store the new head on the Thread
test [RhpTrapThreads], TrapThreadsFlags_AbortInProgress
jz @f
;; test if the exception handled by the catch was the ThreadAbortException
cmp byte ptr [esp + esp_offsetof_is_handling_thread_abort], 0
je @f
;; RhpCallFunclet preserved our local EBP value, so let's fetch the correct one for the resume address
mov ecx, [esp + esp_offsetof_RegDisplay] ;; ecx <- REGDISPLAY *
mov ecx, [ecx + OFFSETOF__REGDISPLAY__pRbp]
mov ebp, [ecx]
;; It was the ThreadAbortException, so rethrow it
mov ecx, STATUS_REDHAWK_THREAD_ABORT
mov edx, [esp + esp_offsetof_ResumeIP]
mov esp, eax ;; reset the SP to resume SP value
jmp RhpThrowHwEx ;; Throw the ThreadAbortException as a special kind of hardware exception
@@:
;; RhpCallFunclet preserved our local EBP value, so let's fetch the correct one for the resume address
mov ecx, [esp + esp_offsetof_RegDisplay] ;; ecx <- REGDISPLAY *
mov ecx, [ecx + OFFSETOF__REGDISPLAY__pRbp]
mov ebp, [ecx]
;; reset ESP and jump to the continuation address
mov ecx, [esp + esp_offsetof_ResumeIP]
mov esp, eax
jmp ecx
FASTCALL_ENDFUNC
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;; void FASTCALL RhpCallFinallyFunclet(void* pHandlerIP, REGDISPLAY* pRegDisplay)
;;
;; INPUT: ECX: handler funclet address
;; EDX: REGDISPLAY*
;;
;; OUTPUT:
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
FASTCALL_FUNC RhpCallFinallyFunclet, 0
FUNCLET_CALL_PROLOGUE 0
push edx ;; save REGDISPLAY*
;; Clear the DoNotTriggerGc state before calling out to our managed catch funclet.
INLINE_GETTHREAD eax, ebx ;; eax <- Thread*, ebx is trashed
lock and dword ptr [eax + OFFSETOF__Thread__m_ThreadStateFlags], NOT TSF_DoNotTriggerGc
;;
;; load preserved registers for funclet
;;
mov eax, [edx + OFFSETOF__REGDISPLAY__pRbx]
mov ebx, [eax]
mov eax, [edx + OFFSETOF__REGDISPLAY__pRsi]
mov esi, [eax]
mov eax, [edx + OFFSETOF__REGDISPLAY__pRdi]
mov edi, [eax]
mov eax, [edx + OFFSETOF__REGDISPLAY__pRbp]
mov eax, [eax]
mov edx, ecx
;; ECX: not used
;; EDX: funclet IP
;; EAX: funclet EBP
call RhpCallFunclet
EXPORT_POINTER_TO_ADDRESS _PointerToRhpCallFinallyFunclet2
pop edx ;; restore REGDISPLAY*
;;
;; save preserved registers from funclet
;;
mov eax, [edx + OFFSETOF__REGDISPLAY__pRbx]
mov [eax], ebx
mov eax, [edx + OFFSETOF__REGDISPLAY__pRsi]
mov [eax], esi
mov eax, [edx + OFFSETOF__REGDISPLAY__pRdi]
mov [eax], edi
INLINE_GETTHREAD eax, ebx ;; eax <- Thread*, ebx is trashed
lock or dword ptr [eax + OFFSETOF__Thread__m_ThreadStateFlags], TSF_DoNotTriggerGc
FUNCLET_CALL_EPILOGUE
ret
FASTCALL_ENDFUNC
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;; void* FASTCALL RhpCallFilterFunclet(RtuObjectRef exceptionObj, void* pFilterIP, REGDISPLAY* pRegDisplay)
;;
;; INPUT: ECX: exception object
;; EDX: filter funclet address
;; [ESP + 4]: REGDISPLAY*
;;
;; OUTPUT:
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
FASTCALL_FUNC RhpCallFilterFunclet, 0
FUNCLET_CALL_PROLOGUE 0
push edx ;; save filter funclet address
;;
;; load preserved registers for funclet
;;
mov edx, [ebp + 8]
mov eax, [edx + OFFSETOF__REGDISPLAY__pRbp]
mov eax, [eax]
;; ECX still contains exception object
;; EAX contains the funclet EBP value
mov edx, [esp + 0] ;; reload filter funclet address
call RhpCallFunclet
EXPORT_POINTER_TO_ADDRESS _PointerToRhpCallFilterFunclet2
;; EAX contains the result of the filter execution
mov edx, [ebp + 8]
pop ecx ;; pop scratch slot
FUNCLET_CALL_EPILOGUE
ret
FASTCALL_ENDFUNC
end