[PATCH v2 2/2] ntdll: Report SegDs to be identical to SegSs in x86_64 exception handlers.

Zebediah Figura z.figura12 at gmail.com
Sun Mar 28 17:08:31 CDT 2021


Based on a patch by Dávid Török.

Wine-Bug: https://bugs.winehq.org/show_bug.cgi?id=47970
Signed-off-by: Zebediah Figura <z.figura12 at gmail.com>
---
v2: Massage the context after returning to user mode; we may need it untouched
for exceptions in 32-bit code.

Actually, the behaviour of segment registers on x86_64 Windows is more
complicated:

* By default, %ss == %ds == %es == %gs == 0x2b; %cs == 0x33; %fs == 0x53.

* NtGetContextThread() does not capture the actual values, but instead returns
  the default values.

* All system calls restore all segments to their default values, including
  NtContinue().

* RtlRestoreContext() also restores all segments to their default values.

* SegDs, SegEs, SegFs, SegGs all restore default values in exception handlers;
  SegSs returns the actual value of %ss.

* RtlCaptureContext() does capture the actual values.

I did not test:

* whether %cs is correctly captured or restored in any functions;

* whether segment registers are restored from exception handlers (either to
  the default values or to the values set in the context).

We could, to a point, emulate the above, but it gets tricky. According to the
Intel 64 and IA-32 Architectures Software Developer's Manual:

* %es, %ds, %ss can point to any segment—the entire contents of the descriptor
  table are effectively ignored (if I'm reading Volume 3 §3.4.4 right). However,
  they have to point to a *valid* segment (cf. Volume 1 §3.4.2.1).

* %cs is more restricted—the attributes in the descriptor table are also
  validated (Volume 3 §5.2.1).

* I don't even know about %fs and %gs; the manual is too vague and confusing.

 dlls/ntdll/signal_x86_64.c   | 4 ++++
 dlls/ntdll/tests/exception.c | 2 +-
 2 files changed, 5 insertions(+), 1 deletion(-)

diff --git a/dlls/ntdll/signal_x86_64.c b/dlls/ntdll/signal_x86_64.c
index b5be4f35758..d88f7ae553a 100644
--- a/dlls/ntdll/signal_x86_64.c
+++ b/dlls/ntdll/signal_x86_64.c
@@ -567,6 +567,10 @@ NTSTATUS WINAPI dispatch_exception( EXCEPTION_RECORD *rec, CONTEXT *context )
               context->R12, context->R13, context->R14, context->R15 );
     }
 
+    /* Legends of Runeterra depends on having SegDs == SegSs in an exception
+     * handler. */
+    context->SegDs = context->SegSs;
+
     if (call_vectored_handlers( rec, context ) == EXCEPTION_CONTINUE_EXECUTION)
         NtContinue( context, FALSE );
 
diff --git a/dlls/ntdll/tests/exception.c b/dlls/ntdll/tests/exception.c
index 7c75606dd32..cdf3cdf8201 100644
--- a/dlls/ntdll/tests/exception.c
+++ b/dlls/ntdll/tests/exception.c
@@ -3020,7 +3020,7 @@ static DWORD WINAPI handler( EXCEPTION_RECORD *rec, ULONG64 frame,
         "%u: Unexpected exception address %p/%p\n", entry,
         rec->ExceptionAddress, (char*)context->Rip );
 
-    todo_wine ok( context->SegDs == context->SegSs,
+    ok( context->SegDs == context->SegSs,
         "%u: ds %#x does not match ss %#x\n", entry, context->SegDs, context->SegSs );
     todo_wine ok( context->SegEs == context->SegSs,
         "%u: es %#x does not match ss %#x\n", entry, context->SegEs, context->SegSs );
-- 
2.30.2




More information about the wine-devel mailing list