| 709 | } |
| 710 | |
| 711 | NTSTATUS BTCpuSetContext(HANDLE Thread, HANDLE Process, void* Unknown, WOW64_CONTEXT* Context) { |
| 712 | if (!FEX::Windows::ValidateHandleAccess(Thread, THREAD_SET_CONTEXT)) { |
| 713 | return STATUS_ACCESS_DENIED; |
| 714 | } |
| 715 | |
| 716 | auto ThreadDup = FEX::Windows::DupHandle(Thread, THREAD_QUERY_INFORMATION | THREAD_GET_CONTEXT | THREAD_SET_CONTEXT); |
| 717 | auto [Err, TLS] = GetThreadTLS(*ThreadDup); |
| 718 | if (Err) { |
| 719 | return Err; |
| 720 | } |
| 721 | |
| 722 | // Back-up the input context incase we've been passed the CPU area (the flush below would wipe it out otherwise) |
| 723 | WOW64_CONTEXT TmpContext = *Context; |
| 724 | |
| 725 | if (!(TLS.ControlWord().load(std::memory_order::relaxed) & ControlBits::WOW_CPU_AREA_DIRTY)) { |
| 726 | if (Err = Context::FlushThreadStateContext(*ThreadDup); Err) { |
| 727 | return Err; |
| 728 | } |
| 729 | } |
| 730 | |
| 731 | // Merge the input context into the CPU area then pass the full context into the JIT |
| 732 | if (Err = RtlWow64SetThreadContext(*ThreadDup, &TmpContext); Err) { |
| 733 | return Err; |
| 734 | } |
| 735 | |
| 736 | TmpContext.ContextFlags = WOW64_CONTEXT_FULL | WOW64_CONTEXT_EXTENDED_REGISTERS; |
| 737 | |
| 738 | if (Err = RtlWow64GetThreadContext(*ThreadDup, &TmpContext); Err) { |
| 739 | return Err; |
| 740 | } |
| 741 | |
| 742 | if (Thread == GetCurrentThread() && TLS.CachedCallRetSp()) { |
| 743 | TLS.ThreadState()->CurrentFrame->State.callret_sp = TLS.CachedCallRetSp(); |
| 744 | } |
| 745 | |
| 746 | Context::LoadStateFromWowContext(TLS.ThreadState(), GetWowTEB(TLS.TEB), &TmpContext); |
| 747 | return STATUS_SUCCESS; |
| 748 | } |
| 749 | |
| 750 | // .seh_pushframe doesn't restore the frame pointer, so if when unwinding from RtlCaptureContext an operation is used |
| 751 | // that sets SP from FP, the unwound SP value will be incorrect. Wrap RtlCaptureContext so the correct FP is immediately |
no test coverage detected