Merge pull request #5197 from bylaws/rwxp5

Windows: Improve handling of RWX memory
This commit is contained in:
Ryan Houdek authored and GitHub committed 2026-01-05 13:12:01 -08:00
commit d592e2afb0
4 files changed
+137 -36

No files matched your search

+31 -1
View File
@@ -107,6 +107,10 @@ struct TLS {
}
};
struct FrontendThreadData {
bool InLockedRWXRead {};
};
class WowSyscallHandler;
namespace {
@@ -142,6 +146,10 @@ TLS GetTLS() {
return TLS {NtCurrentTeb()};
}
FrontendThreadData* GetFrontendThreadData(FEXCore::Core::InternalThreadState* Thread) {
return static_cast<FrontendThreadData*>(Thread->FrontendPtr);
}
uint64_t GetWowTEB(void* TEB) {
static constexpr size_t WowTEBOffsetMemberOffset {0x180c};
return static_cast<uint64_t>(
@@ -610,6 +618,8 @@ void BTCpuThreadInit() {
TLS.ThreadState() = Thread;
TLS.ControlWord().fetch_or(ControlBits::WOW_CPU_AREA_DIRTY, std::memory_order::relaxed);
Thread->FrontendPtr = new FrontendThreadData();
auto ThreadTID = GetCurrentThreadId();
Threads.emplace(ThreadTID, Thread);
if (StatAllocHandler) {
@@ -656,6 +666,8 @@ void BTCpuThreadTerm(HANDLE Thread, LONG ExitCode) {
}
auto ThreadState = TLS.ThreadState();
delete GetFrontendThreadData(ThreadState);
// GDT and LDT are mirrored, only free one.
delete[] ThreadState->CurrentFrame->State.segment_arrays[FEXCore::Core::CPUState::SEGMENT_ARRAY_INDEX_GDT];
@@ -1004,7 +1016,25 @@ void BTCpuNotifyUnmapViewOfSection(void* Address, BOOL After, ULONG Status) {
}
}
void BTCpuNotifyReadFile(HANDLE Handle, void* Address, SIZE_T Size, BOOL After, NTSTATUS Status) {}
void BTCpuNotifyReadFile(HANDLE Handle, void* Address, SIZE_T Size, BOOL After, NTSTATUS Status) {
auto& InLockedRWXRead = GetFrontendThreadData(GetTLS().ThreadState())->InLockedRWXRead;
if (!After) {
ThreadCreationMutex.lock();
CTX->GetCodeInvalidationMutex().lock();
if (InvalidationTracker->BeginUntrackedWriteLocked(reinterpret_cast<uint64_t>(Address), static_cast<uint64_t>(Size))) {
InLockedRWXRead = true;
} else {
CTX->GetCodeInvalidationMutex().unlock();
ThreadCreationMutex.unlock();
}
} else {
if (InLockedRWXRead) {
InLockedRWXRead = false;
CTX->GetCodeInvalidationMutex().unlock();
ThreadCreationMutex.unlock();
}
}
}
BOOLEAN WINAPI BTCpuIsProcessorFeaturePresent(UINT Feature) {
return CPUFeatures->IsFeaturePresent(Feature) ? TRUE : FALSE;