Skip to content

Commit 52cecdc

Browse files
authored
Ensure proper InterpThreadContext initialization (#117479)
* Ensure proper InterpThreadContext initialization In case an interpreted method was compiled on one thread and then later executed on another thread that never executed any interpreted code before, the InterpThreadContext was not initialized. This change fixes that by calling Thread::GetInterpThreadContext from the InterpreterStub in case it was not initialized for the current thread yet. * Fix Unix Amd64
1 parent f3548ef commit 52cecdc

File tree

5 files changed

+67
-24
lines changed

5 files changed

+67
-24
lines changed

src/coreclr/pal/inc/unixasmmacrosarm64.inc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -204,9 +204,9 @@ C_FUNC(\Name\()_End):
204204

205205
__PWTB_TransitionBlock = __PWTB_FloatArgumentRegisters
206206

207-
.if (__PWTB_SaveFPArgs == 1)
208-
__PWTB_TransitionBlock = __PWTB_TransitionBlock + SIZEOF__FloatArgumentRegisters
209-
.endif
207+
// Always reserve space for the FP arg regs, even if they are not saved in the PROLOG_WITH_TRANSITION_BLOCK
208+
// Some code using this macro saves them optionally
209+
__PWTB_TransitionBlock = __PWTB_TransitionBlock + SIZEOF__FloatArgumentRegisters
210210

211211
__PWTB_StackAlloc = __PWTB_TransitionBlock
212212
__PWTB_ArgumentRegisters = __PWTB_StackAlloc + 104

src/coreclr/vm/amd64/AsmHelpers.asm

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
include AsmMacros.inc
55
include asmconstants.inc
66

7+
Thread_GetInterpThreadContext TEXTEQU <?GetInterpThreadContext@Thread@@QEAAPEAUInterpThreadContext@@XZ>
8+
79
extern NDirectImportWorker:proc
810
extern ThePreStub:proc
911
extern ProfileEnter:proc
@@ -13,6 +15,7 @@ extern OnHijackWorker:proc
1315
extern JIT_RareDisableHelperWorker:proc
1416
ifdef FEATURE_INTERPRETER
1517
extern ExecuteInterpretedMethod:proc
18+
extern Thread_GetInterpThreadContext:proc
1619
endif
1720

1821
extern g_pPollGC:QWORD
@@ -565,12 +568,21 @@ NESTED_ENTRY InterpreterStub, _TEXT
565568

566569
INLINE_GETTHREAD r10; thrashes rax and r11
567570

571+
mov rax, qword ptr [r10 + OFFSETOF__Thread__m_pInterpThreadContext]
572+
test rax, rax
573+
jnz HaveInterpThreadContext
574+
575+
mov rcx, r10
576+
call Thread_GetInterpThreadContext
577+
RESTORE_ARGUMENT_REGISTERS __PWTB_ArgumentRegisters
578+
RESTORE_FLOAT_ARGUMENT_REGISTERS __PWTB_FloatArgumentRegisters
579+
580+
HaveInterpThreadContext:
581+
mov r10, qword ptr [rax + OFFSETOF__InterpThreadContext__pStackPointer]
568582
; Load the InterpMethod pointer from the IR bytecode
569583
mov rax, qword ptr [rbx]
570584
mov rax, qword ptr [rax + OFFSETOF__InterpMethod__pCallStub]
571585
lea r11, qword ptr [rax + OFFSETOF__CallStubHeader__Routines]
572-
mov r10, qword ptr [r10 + OFFSETOF__Thread__m_pInterpThreadContext]
573-
mov r10, qword ptr [r10 + OFFSETOF__InterpThreadContext__pStackPointer]
574586
lea rax, [rsp + __PWTB_TransitionBlock]
575587
; Copy the arguments to the interpreter stack, invoke the InterpExecMethod and load the return value
576588
call qword ptr [r11]

src/coreclr/vm/amd64/asmhelpers.S

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -444,6 +444,15 @@ NESTED_ENTRY InterpreterStub, _TEXT, NoHandler
444444
INLINE_GETTHREAD // result in rax, it can thrash all argument registers as it can call a helper
445445
mov r10, rax
446446

447+
mov rax, qword ptr [r10 + OFFSETOF__Thread__m_pInterpThreadContext]
448+
test rax, rax
449+
jnz LOCAL_LABEL(HaveInterpThreadContext)
450+
451+
mov rcx, r10
452+
call C_FUNC(_ZN6Thread22GetInterpThreadContextEv) // Thread::GetInterpThreadContext
453+
454+
LOCAL_LABEL(HaveInterpThreadContext):
455+
mov r10, qword ptr [rax + OFFSETOF__InterpThreadContext__pStackPointer]
447456
// Load the InterpMethod pointer from the IR bytecode
448457
mov rax, qword ptr [rbx]
449458
mov rax, qword ptr [rax + OFFSETOF__InterpMethod__pCallStub]
@@ -463,8 +472,6 @@ NESTED_ENTRY InterpreterStub, _TEXT, NoHandler
463472
movsd xmm6, real8 ptr [rsp + __PWTB_FloatArgumentRegisters + 0x60]
464473
movsd xmm7, real8 ptr [rsp + __PWTB_FloatArgumentRegisters + 0x70]
465474
lea r11, qword ptr [rax + OFFSETOF__CallStubHeader__Routines]
466-
mov r10, qword ptr [r10 + OFFSETOF__Thread__m_pInterpThreadContext]
467-
mov r10, qword ptr [r10 + OFFSETOF__InterpThreadContext__pStackPointer]
468475
lea rax, [rsp + __PWTB_TransitionBlock]
469476
// rbx contains IR bytecode address
470477
// Copy the arguments to the interpreter stack, invoke the InterpExecMethod and load the return value

src/coreclr/vm/arm64/asmhelpers.S

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -695,27 +695,40 @@ NESTED_ENTRY InterpreterStub, _TEXT, NoHandler
695695
#endif
696696
INLINE_GETTHREAD x20 // thrashes x0 on Apple OSes (and possibly other arg registers on other Unixes)
697697

698-
// On Apple, the INLINE_GETTHREAD is guaranteed to not to thrash argument registers other than x0
699-
// On other Unixes, there is no such guarantee, so we need to always restore the argument registers
698+
ldr x11, [x20, #OFFSETOF__Thread__m_pInterpThreadContext]
699+
cbnz x11, LOCAL_LABEL(HaveInterpThreadContext)
700+
701+
#ifdef TARGET_APPLE
702+
// There Thread::GetInterpThreadContext can destroy all argument registers, so we
703+
// need to save them. For non-Apple, they have been already saved in the PROLOG_WITH_TRANSITION_BLOCK
704+
// Restore x0 thrashed by the INLINE_GETTHREAD
705+
mov x0, x21
706+
SAVE_ARGUMENT_REGISTERS sp, __PWTB_ArgumentRegisters
707+
SAVE_FLOAT_ARGUMENT_REGISTERS sp, __PWTB_FloatArgumentRegisters
708+
#endif
709+
710+
mov x0, x20
711+
bl C_FUNC(_ZN6Thread22GetInterpThreadContextEv) // Thread::GetInterpThreadContext
712+
mov x11, x0
713+
700714
#ifndef TARGET_APPLE
701-
ldp x0, x1, [sp, #__PWTB_ArgumentRegisters + 8]
702-
ldp x2, x3, [sp, #__PWTB_ArgumentRegisters + 0x18]
703-
ldp x4, x5, [sp, #__PWTB_ArgumentRegisters + 0x28]
704-
ldp x6, x7, [sp, #__PWTB_ArgumentRegisters + 0x38]
705-
ldp q0, q1, [sp, #__PWTB_FloatArgumentRegisters]
706-
ldp q2, q3, [sp, #__PWTB_FloatArgumentRegisters + 0x20]
707-
ldp q4, q5, [sp, #__PWTB_FloatArgumentRegisters + 0x40]
708-
ldp q6, q7, [sp, #__PWTB_FloatArgumentRegisters + 0x60]
709-
#else // !TARGET_APPLE
710-
// Restore the thrashed x0
715+
LOCAL_LABEL(HaveInterpThreadContext):
716+
#endif
717+
718+
RESTORE_ARGUMENT_REGISTERS sp, __PWTB_ArgumentRegisters
719+
RESTORE_FLOAT_ARGUMENT_REGISTERS sp, __PWTB_FloatArgumentRegisters
720+
721+
#ifdef TARGET_APPLE
722+
LOCAL_LABEL(HaveInterpThreadContext):
723+
// On Apple, the INLINE_GETTHREAD is guaranteed to not to thrash argument registers other than x0
724+
// So we restore just the x0
711725
mov x0, x21
712-
#endif // !TARGET_APPLE
726+
#endif // TARGET_APPLE
713727

714728
ldr x9, [x19] // InterpMethod*
715729
ldr x9, [x9, #OFFSETOF__InterpMethod__pCallStub]
716730
add x10, x9, #OFFSETOF__CallStubHeader__Routines
717-
ldr x9, [x20, #OFFSETOF__Thread__m_pInterpThreadContext]
718-
ldr x9, [x9, #OFFSETOF__InterpThreadContext__pStackPointer]
731+
ldr x9, [x11, #OFFSETOF__InterpThreadContext__pStackPointer]
719732
// x19 contains IR bytecode address
720733
// Copy the arguments to the interpreter stack, invoke the InterpExecMethod and load the return value
721734
ldr x11, [x10], #8

src/coreclr/vm/arm64/asmhelpers.asm

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
IMPORT HijackHandler
2525
IMPORT ThrowControlForThread
2626
#ifdef FEATURE_INTERPRETER
27+
SETALIAS Thread_GetInterpThreadContext, ?GetInterpThreadContext@Thread@@QEAAPEAUInterpThreadContext@@XZ
28+
IMPORT $Thread_GetInterpThreadContext
2729
IMPORT ExecuteInterpretedMethod
2830
#endif
2931

@@ -1064,13 +1066,22 @@ JIT_PollGCRarePath
10641066

10651067
INLINE_GETTHREAD x20, x19
10661068

1069+
ldr x11, [x20, #OFFSETOF__Thread__m_pInterpThreadContext]
1070+
cbnz x11, HaveInterpThreadContext
1071+
1072+
mov x0, x20
1073+
bl $Thread_GetInterpThreadContext
1074+
mov x11, x0
1075+
RESTORE_ARGUMENT_REGISTERS sp, __PWTB_ArgumentRegisters
1076+
RESTORE_FLOAT_ARGUMENT_REGISTERS sp, __PWTB_FloatArgumentRegisters
1077+
1078+
HaveInterpThreadContext
10671079
; IR bytecode address
10681080
mov x19, METHODDESC_REGISTER
10691081
ldr x9, [METHODDESC_REGISTER]
10701082
ldr x9, [x9, #OFFSETOF__InterpMethod__pCallStub]
10711083
add x10, x9, #OFFSETOF__CallStubHeader__Routines
1072-
ldr x9, [x20, #OFFSETOF__Thread__m_pInterpThreadContext]
1073-
ldr x9, [x9, #OFFSETOF__InterpThreadContext__pStackPointer]
1084+
ldr x9, [x11, #OFFSETOF__InterpThreadContext__pStackPointer]
10741085
; x19 contains IR bytecode address
10751086
; Copy the arguments to the interpreter stack, invoke the InterpExecMethod and load the return value
10761087
ldr x11, [x10], #8

0 commit comments

Comments
 (0)