Skip to content

Commit 80fb00f

Browse files
kgjkotas
andauthored
[interp] Make InterpreterStub handle being called on threads without a Thread object (dotnet#119906)
Make InterpreterStub handle being called on threads without a Thread object Fixes Interop\GCBridge\BridgeTest --------- Co-authored-by: Jan Kotas <jkotas@microsoft.com>
1 parent 843b197 commit 80fb00f

5 files changed

Lines changed: 34 additions & 11 deletions

File tree

src/coreclr/vm/amd64/AsmHelpers.asm

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

7-
Thread_GetInterpThreadContext TEXTEQU <?GetInterpThreadContext@Thread@@QEAAPEAUInterpThreadContext@@XZ>
8-
97
extern PInvokeImportWorker:proc
108
extern ThePreStub:proc
119
extern ProfileEnter:proc
@@ -15,7 +13,7 @@ extern OnHijackWorker:proc
1513
extern JIT_RareDisableHelperWorker:proc
1614
ifdef FEATURE_INTERPRETER
1715
extern ExecuteInterpretedMethod:proc
18-
extern Thread_GetInterpThreadContext:proc
16+
extern GetInterpThreadContextWithPossiblyMissingThread:proc
1917
endif
2018

2119
extern g_pPollGC:QWORD
@@ -564,13 +562,16 @@ NESTED_ENTRY InterpreterStub, _TEXT
564562
mov rbx, METHODDESC_REGISTER
565563

566564
INLINE_GETTHREAD r10; thrashes rax and r11
565+
test r10, r10
566+
jz NoManagedThread
567567

568568
mov rax, qword ptr [r10 + OFFSETOF__Thread__m_pInterpThreadContext]
569569
test rax, rax
570570
jnz HaveInterpThreadContext
571571

572+
NoManagedThread:
572573
mov rcx, r10
573-
call Thread_GetInterpThreadContext
574+
call GetInterpThreadContextWithPossiblyMissingThread
574575
RESTORE_ARGUMENT_REGISTERS __PWTB_ArgumentRegisters
575576
RESTORE_FLOAT_ARGUMENT_REGISTERS __PWTB_FloatArgumentRegisters
576577

src/coreclr/vm/amd64/asmhelpers.S

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -443,13 +443,16 @@ NESTED_ENTRY InterpreterStub, _TEXT, NoHandler
443443

444444
INLINE_GETTHREAD // result in rax, it can thrash all argument registers as it can call a helper
445445
mov r10, rax
446+
test rax, rax
447+
jz LOCAL_LABEL(NoManagedThread)
446448

447449
mov rax, qword ptr [r10 + OFFSETOF__Thread__m_pInterpThreadContext]
448450
test rax, rax
449451
jnz LOCAL_LABEL(HaveInterpThreadContext)
450452

453+
LOCAL_LABEL(NoManagedThread):
451454
mov rcx, r10
452-
call C_FUNC(_ZN6Thread22GetInterpThreadContextEv) // Thread::GetInterpThreadContext
455+
call C_FUNC(GetInterpThreadContextWithPossiblyMissingThread)
453456

454457
LOCAL_LABEL(HaveInterpThreadContext):
455458
mov r10, qword ptr [rax + OFFSETOF__InterpThreadContext__pStackPointer]
@@ -1708,7 +1711,7 @@ NESTED_ENTRY CallJittedMethodRetVoid, _TEXT, NoHandler
17081711
push_nonvol_reg rbp
17091712
mov rbp, rsp
17101713
alloc_stack 0x10
1711-
save_reg_postrsp r10, 0
1714+
save_reg_postrsp r10, 0
17121715
END_PROLOGUE
17131716
sub rsp, rcx // total stack space
17141717
mov r11, rdi // The routines list

src/coreclr/vm/arm64/asmhelpers.S

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -694,12 +694,14 @@ NESTED_ENTRY InterpreterStub, _TEXT, NoHandler
694694
mov x21, x0
695695
#endif
696696
INLINE_GETTHREAD x20 // thrashes x0 on Apple OSes (and possibly other arg registers on other Unixes)
697+
cbz x20, LOCAL_LABEL(NoManagedThread)
697698

698699
ldr x11, [x20, #OFFSETOF__Thread__m_pInterpThreadContext]
699700
cbnz x11, LOCAL_LABEL(HaveInterpThreadContext)
700701

702+
LOCAL_LABEL(NoManagedThread):
701703
#ifdef TARGET_APPLE
702-
// There Thread::GetInterpThreadContext can destroy all argument registers, so we
704+
// GetInterpThreadContextWithPossiblyMissingThread can destroy all argument registers, so we
703705
// need to save them. For non-Apple, they have been already saved in the PROLOG_WITH_TRANSITION_BLOCK
704706
// Restore x0 thrashed by the INLINE_GETTHREAD
705707
mov x0, x21
@@ -708,7 +710,7 @@ NESTED_ENTRY InterpreterStub, _TEXT, NoHandler
708710
#endif
709711

710712
mov x0, x20
711-
bl C_FUNC(_ZN6Thread22GetInterpThreadContextEv) // Thread::GetInterpThreadContext
713+
bl C_FUNC(GetInterpThreadContextWithPossiblyMissingThread)
712714
mov x11, x0
713715

714716
#ifndef TARGET_APPLE

src/coreclr/vm/arm64/asmhelpers.asm

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,7 @@
2424
IMPORT HijackHandler
2525
IMPORT ThrowControlForThread
2626
#ifdef FEATURE_INTERPRETER
27-
SETALIAS Thread_GetInterpThreadContext, ?GetInterpThreadContext@Thread@@QEAAPEAUInterpThreadContext@@XZ
28-
IMPORT $Thread_GetInterpThreadContext
27+
IMPORT GetInterpThreadContextWithPossiblyMissingThread
2928
IMPORT ExecuteInterpretedMethod
3029
#endif
3130

@@ -1065,12 +1064,14 @@ JIT_PollGCRarePath
10651064
PROLOG_WITH_TRANSITION_BLOCK
10661065

10671066
INLINE_GETTHREAD x20, x19
1067+
cbz x20, NoManagedThread
10681068

10691069
ldr x11, [x20, #OFFSETOF__Thread__m_pInterpThreadContext]
10701070
cbnz x11, HaveInterpThreadContext
10711071

1072+
NoManagedThread
10721073
mov x0, x20
1073-
bl $Thread_GetInterpThreadContext
1074+
bl GetInterpThreadContextWithPossiblyMissingThread
10741075
mov x11, x0
10751076
RESTORE_ARGUMENT_REGISTERS sp, __PWTB_ArgumentRegisters
10761077
RESTORE_FLOAT_ARGUMENT_REGISTERS sp, __PWTB_FloatArgumentRegisters

src/coreclr/vm/threads.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7702,6 +7702,22 @@ InterpThreadContext* Thread::GetInterpThreadContext()
77027702

77037703
return m_pInterpThreadContext;
77047704
}
7705+
7706+
extern "C" InterpThreadContext* STDCALL GetInterpThreadContextWithPossiblyMissingThread(Thread *pThread)
7707+
{
7708+
CONTRACTL
7709+
{
7710+
THROWS;
7711+
}
7712+
CONTRACTL_END;
7713+
7714+
if (pThread == nullptr)
7715+
{
7716+
pThread = SetupThread();
7717+
}
7718+
7719+
return pThread->GetInterpThreadContext();
7720+
}
77057721
#endif // FEATURE_INTERPRETER
77067722

77077723
/* static */

0 commit comments

Comments
 (0)