@@ -490,8 +490,9 @@ endif
490
490
INLINE_THREAD_UNHIJACK rdx , rcx , r9 ;; Thread in rdx, trashes rcx and r9
491
491
492
492
mov rcx , [ rsp + rsp_offsetof_arguments + 18h ] ;; rcx <- current ExInfo *
493
+ mov r10 , [ r8 + OFFSETOF__REGDISPLAY__IP ] ;; r10 <- original IP value
493
494
mov r8 , [ r8 + OFFSETOF__REGDISPLAY__SP ] ;; r8 <- resume SP value
494
- xor r9d , r9d ;; r9 <- 0
495
+ xor r9 , r9 ;; r9 <- 0
495
496
496
497
@@: mov rcx , [ rcx + OFFSETOF__ExInfo__m_pPrevExInfo ] ;; rcx <- next ExInfo
497
498
cmp rcx , r9
@@ -501,6 +502,20 @@ endif
501
502
502
503
@@: mov [ rdx + OFFSETOF__Thread__m_pExInfoStackHead ], rcx ;; store the new head on the Thread
503
504
505
+ ;; Sanity check: if we have shadow stack, it should agree with what we have in rsp
506
+ LOCAL_STACK_USE equ 118h
507
+ ifdef _DEBUG
508
+ rdsspq r9
509
+ test r9 , r9
510
+ jz @f
511
+ mov r9 , [ r9 ]
512
+ cmp [ rsp + LOCAL_STACK_USE ], r9
513
+ je @f
514
+ int 3
515
+ @@:
516
+ xor r9 , r9 ;; r9 <- 0
517
+ endif
518
+
504
519
test [ RhpTrapThreads ], TrapThreadsFlags_AbortInProgress
505
520
jz @f
506
521
@@ -511,12 +526,28 @@ endif
511
526
;; It was the ThreadAbortException, so rethrow it
512
527
mov rcx , STATUS_REDHAWK_THREAD_ABORT
513
528
mov rdx , rax ;; rdx <- continuation address as exception RIP
514
- mov rsp , r8 ;; reset the SP to resume SP value
515
- jmp RhpThrowHwEx ;; Throw the ThreadAbortException as a special kind of hardware exception
529
+ mov rax , RhpThrowHwEx ;; Throw the ThreadAbortException as a special kind of hardware exception
516
530
517
- ;; reset RSP and jump to the continuation address
531
+ ;; reset RSP and jump to RAX
518
532
@@: mov rsp , r8 ;; reset the SP to resume SP value
519
- jmp rax
533
+
534
+ ;; if have shadow stack, then we need to reconcile it with the rsp change we have just made
535
+ rdsspq r9
536
+ test r9 , r9
537
+ jz NoSSP
538
+
539
+ ;; Find the shadow stack pointer for the frame we are going to restore to.
540
+ ;; The SSP we search is pointing to the return address of the frame represented
541
+ ;; by the passed in context. So we search for the instruction pointer from
542
+ ;; the context and return one slot up from there.
543
+ ;; (Same logic as in GetSSPForFrameOnCurrentStack)
544
+ xor r11 , r11
545
+ @@: inc r11
546
+ cmp [ r9 + r11 * 8 - 8 ], r10
547
+ jne @b
548
+
549
+ incsspq r11
550
+ NoSSP: jmp rax
520
551
521
552
522
553
NESTED_END RhpCallCatchFunclet , _TEXT
0 commit comments