|
13 | 13 |
|
14 | 14 | #define WASM_STRINGIFY_HELPER(value) #value |
15 | 15 | #define WASM_STRINGIFY(value) WASM_STRINGIFY_HELPER(value) |
| 16 | +#define INLINED_PINVOKE_FROM_R2R 1 |
16 | 17 |
|
17 | 18 | void ExecuteInterpretedMethodWithArgs_PortableEntryPoint(PCODE portableEntrypoint, TransitionBlock* block, size_t argsSize, int8_t* retBuff); |
18 | 19 |
|
@@ -491,13 +492,22 @@ void InlinedCallFrame::UpdateRegDisplay_Impl(const PREGDISPLAY pRD, bool updateF |
491 | 492 | return; |
492 | 493 | } |
493 | 494 |
|
494 | | - pRD->pCurrentContext->InterpreterIP = *(DWORD *)&m_pCallerReturnAddress; |
495 | | - |
496 | 495 | pRD->IsCallerContextValid = FALSE; |
497 | 496 |
|
498 | | - pRD->pCurrentContext->InterpreterSP = *(DWORD *)&m_pCallSiteSP; |
499 | | - pRD->pCurrentContext->InterpreterFP = *(DWORD *)&m_pCalleeSavedFP; |
500 | | - |
| 497 | + if (m_pCallerReturnAddress == INLINED_PINVOKE_FROM_R2R) |
| 498 | + { |
| 499 | + pRD->pCurrentContext->InterpreterSP = (TADDR)m_pCallSiteSP; |
| 500 | + pRD->pCurrentContext->InterpreterIP = GetWasmVirtualIPFromStackPointer((TADDR)m_pCallSiteSP); |
| 501 | + _ASSERTE(pRD->pCurrentContext->InterpreterIP != 0); // We should be in RyuJit compiled code here |
| 502 | + pRD->pCurrentContext->InterpreterFP = GetWasmFramePointerFromStackPointer((TADDR)m_pCallSiteSP, (PCODE)pRD->pCurrentContext->InterpreterIP); |
| 503 | + } |
| 504 | + else |
| 505 | + { |
| 506 | + pRD->pCurrentContext->InterpreterIP = *(DWORD *)&m_pCallerReturnAddress; |
| 507 | + pRD->pCurrentContext->InterpreterSP = *(DWORD *)&m_pCallSiteSP; |
| 508 | + pRD->pCurrentContext->InterpreterFP = *(DWORD *)&m_pCalleeSavedFP; |
| 509 | + } |
| 510 | + |
501 | 511 | SyncRegDisplayToCurrentContext(pRD); |
502 | 512 |
|
503 | 513 | #ifdef FEATURE_INTERPRETER |
@@ -681,15 +691,88 @@ extern "C" void STDCALL GenericPInvokeCalliHelper(void) |
681 | 691 | PORTABILITY_ASSERT("GenericPInvokeCalliHelper is not implemented on wasm"); |
682 | 692 | } |
683 | 693 |
|
684 | | -EXTERN_C void JIT_PInvokeBegin(InlinedCallFrame* pFrame) |
| 694 | +// Does the pinvoke frame transition; the naked wrappers below have already set the wasm |
| 695 | +// __stack_pointer global to sp so it is safe to run native code here. |
| 696 | +EXTERN_C void JIT_PInvokeBeginImpl(void* sp, InlinedCallFrame* pFrame) |
| 697 | +{ |
| 698 | + Thread* pThread = GetThread(); |
| 699 | + |
| 700 | + // Initialize the JIT-provided frame storage, deriving its state from sp/pep since wasm |
| 701 | + // has no machine registers to read the caller SP / return address from. |
| 702 | + ::new ((void*)pFrame) InlinedCallFrame(); |
| 703 | + pFrame->m_pCallSiteSP = sp; |
| 704 | + pFrame->m_pCallerReturnAddress = INLINED_PINVOKE_FROM_R2R; // When this is true, UpdateRegDisplay_Impl derives state from m_pCallSiteSP. |
| 705 | + pFrame->m_pCalleeSavedFP = 0; |
| 706 | + pFrame->m_pThread = pThread; |
| 707 | + |
| 708 | + // Link the frame and transition to preemptive GC mode for the native call. |
| 709 | + pFrame->Push(); |
| 710 | + pThread->EnablePreemptiveGC(); |
| 711 | +} |
| 712 | + |
| 713 | +// R2R keeps its shadow SP in a local and leaves the __stack_pointer global stale, so publish |
| 714 | +// the incoming sp to __stack_pointer before any native code runs and leave it there so the |
| 715 | +// subsequent native pinvoke target is also safe. |
| 716 | +extern "C" __attribute__((naked)) void JIT_PInvokeBegin(void* sp, InlinedCallFrame* pFrame, PCODE pep) |
| 717 | +{ |
| 718 | + asm("local.get 0\n" /* sp */ |
| 719 | + "global.set __stack_pointer\n" /* __stack_pointer = sp before any native code runs */ |
| 720 | + "local.get 0\n" /* sp */ |
| 721 | + "local.get 1\n" /* pFrame */ |
| 722 | + "call %0\n" |
| 723 | + "return" ::"i"(JIT_PInvokeBeginImpl)); |
| 724 | +} |
| 725 | + |
| 726 | +extern "C" VOID JIT_PInvokeEndRarePath(); |
| 727 | + |
| 728 | +#ifdef DEBUG |
| 729 | +// Debug variant of these apis tests that sp and __stack_pointer are in sync |
| 730 | +EXTERN_C void JIT_PInvokeEndImpl(TADDR sp, TADDR stack_pointer_global_value, InlinedCallFrame* pFrame) |
685 | 731 | { |
686 | | - PORTABILITY_ASSERT("JIT_PInvokeBegin is not implemented on wasm"); |
| 732 | + _ASSERTE(sp == stack_pointer_global_value); |
| 733 | + Thread* pThread = (Thread*)pFrame->m_pThread; |
| 734 | + |
| 735 | + pThread->m_fPreemptiveGCDisabled.StoreWithoutBarrier(1); |
| 736 | + if (g_TrapReturningThreads) |
| 737 | + { |
| 738 | + JIT_PInvokeEndRarePath(); |
| 739 | + } |
| 740 | + else |
| 741 | + { |
| 742 | + pFrame->Pop(); |
| 743 | + } |
687 | 744 | } |
688 | 745 |
|
689 | | -EXTERN_C void JIT_PInvokeEnd(InlinedCallFrame* pFrame) |
| 746 | +extern "C" __attribute__((naked)) void JIT_PInvokeEnd(void* sp, InlinedCallFrame* pFrame, PCODE pep) |
| 747 | +{ |
| 748 | + asm( |
| 749 | + "local.get 0\n" /* sp */ |
| 750 | + "global.get __stack_pointer\n" /* __stack_pointer */ |
| 751 | + "local.get 1\n" /* pFrame */ |
| 752 | + "local.get 0\n" /* sp */ |
| 753 | + "global.set __stack_pointer\n" /* __stack_pointer = sp before any native code runs, set this here, so that if the assumption around sp == __stack_pointer is wrong the assert logic will work correctly. */ |
| 754 | + "call %0\n" |
| 755 | + "return" ::"i"(JIT_PInvokeEndImpl)); |
| 756 | +} |
| 757 | +#else |
| 758 | +extern "C" void JIT_PInvokeEnd(void* sp, InlinedCallFrame* pFrame, PCODE pep) |
690 | 759 | { |
691 | | - PORTABILITY_ASSERT("JIT_PInvokeEnd is not implemented on wasm"); |
| 760 | + UNREFERENCED_PARAMETER(sp); |
| 761 | + UNREFERENCED_PARAMETER(pep); |
| 762 | + |
| 763 | + Thread* pThread = (Thread*)pFrame->m_pThread; |
| 764 | + |
| 765 | + pThread->m_fPreemptiveGCDisabled.StoreWithoutBarrier(1); |
| 766 | + if (g_TrapReturningThreads) |
| 767 | + { |
| 768 | + JIT_PInvokeEndRarePath(); |
| 769 | + } |
| 770 | + else |
| 771 | + { |
| 772 | + pFrame->Pop(); |
| 773 | + } |
692 | 774 | } |
| 775 | +#endif |
693 | 776 |
|
694 | 777 | extern "C" void STDCALL JIT_StackProbe() |
695 | 778 | { |
|
0 commit comments