* [PATCH] Mask x86 segment registers in Windows gdbserver
@ 2023-12-19 14:48 Tom Tromey
2023-12-20 22:03 ` John Baldwin
0 siblings, 1 reply; 5+ messages in thread
From: Tom Tromey @ 2023-12-19 14:48 UTC (permalink / raw)
To: gdb-patches; +Cc: Tom Tromey
A test internal to AdaCore prints the segment registers. When run
using gdbserver, it shows:
(gdb) print /x $gs
$6 = 0x2b0000
However, the segment registers are only 16 bits -- so this has some
invalid bits.
gdb's windows-nat.c has long had a fix for this problem. This patch
applies the fix to gdbserver as well.
---
gdbserver/win32-i386-low.cc | 26 ++++++++++++++++++++++++++
1 file changed, 26 insertions(+)
diff --git a/gdbserver/win32-i386-low.cc b/gdbserver/win32-i386-low.cc
index f78e0120678..d60ff83c15d 100644
--- a/gdbserver/win32-i386-low.cc
+++ b/gdbserver/win32-i386-low.cc
@@ -35,6 +35,12 @@ using namespace windows_nat;
#define FCS_REGNUM 27
#define FOP_REGNUM 31
+#define I386_CS_REGNUM 10
+#define I386_GS_REGNUM 15
+
+#define AMD64_CS_REGNUM 18
+#define AMD64_GS_REGNUM 23
+
#define FLAG_TRACE_BIT 0x100
static struct x86_debug_reg_state debug_reg_state;
@@ -459,6 +465,18 @@ static const int amd64_mappings[] =
#endif /* __x86_64__ */
+/* Return true if R is a segment register. */
+static bool
+is_segment_register (int r)
+{
+#ifdef __x86_64__
+ if (!windows_process.wow64_process)
+ return r >= AMD64_CS_REGNUM && r <= AMD64_GS_REGNUM;
+ else
+#endif
+ return r >= I386_CS_REGNUM && r <= I386_GS_REGNUM;
+}
+
/* Fetch register from gdbserver regcache data. */
static void
i386_fetch_inferior_register (struct regcache *regcache,
@@ -491,6 +509,14 @@ i386_fetch_inferior_register (struct regcache *regcache,
l = (*((long *) context_offset) >> 16) & ((1 << 11) - 1);
supply_register (regcache, r, (char *) &l);
}
+ else if (is_segment_register (r))
+ {
+ /* GDB treats segment registers as 32bit registers, but they are
+ in fact only 16 bits long. Make sure we do not read extra
+ bits from our source buffer. */
+ l = *((long *) context_offset) & 0xffff;
+ supply_register (regcache, r, (char *) &l);
+ }
else
supply_register (regcache, r, context_offset);
}
--
2.43.0
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH] Mask x86 segment registers in Windows gdbserver
2023-12-19 14:48 [PATCH] Mask x86 segment registers in Windows gdbserver Tom Tromey
@ 2023-12-20 22:03 ` John Baldwin
2023-12-21 20:29 ` Tom Tromey
0 siblings, 1 reply; 5+ messages in thread
From: John Baldwin @ 2023-12-20 22:03 UTC (permalink / raw)
To: Tom Tromey, gdb-patches
On 12/19/23 6:48 AM, Tom Tromey wrote:
> A test internal to AdaCore prints the segment registers. When run
> using gdbserver, it shows:
>
> (gdb) print /x $gs
> $6 = 0x2b0000
>
> However, the segment registers are only 16 bits -- so this has some
> invalid bits.
>
> gdb's windows-nat.c has long had a fix for this problem. This patch
> applies the fix to gdbserver as well.
LGTM. Treating the x86 seg regs as 32-bits has required workarounds in
various places, but not easily fixable unfortunately.
Reviewed-By: John Baldwin <jhb@FreeBSD.org>
--
John Baldwin
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH] Mask x86 segment registers in Windows gdbserver
2023-12-20 22:03 ` John Baldwin
@ 2023-12-21 20:29 ` Tom Tromey
2024-01-18 18:51 ` Tom Tromey
0 siblings, 1 reply; 5+ messages in thread
From: Tom Tromey @ 2023-12-21 20:29 UTC (permalink / raw)
To: John Baldwin; +Cc: Tom Tromey, gdb-patches
>> A test internal to AdaCore prints the segment registers. When run
>> using gdbserver, it shows:
>> (gdb) print /x $gs
>> $6 = 0x2b0000
>> However, the segment registers are only 16 bits -- so this has some
>> invalid bits.
>> gdb's windows-nat.c has long had a fix for this problem. This patch
>> applies the fix to gdbserver as well.
John> LGTM. Treating the x86 seg regs as 32-bits has required workarounds in
John> various places, but not easily fixable unfortunately.
Well, to my surprise, this patch caused regressions in our internal
tester.
It works again if I explicitly exclude the 'ss' register, like:
if (!windows_process.wow64_process)
return r >= AMD64_CS_REGNUM && r <= AMD64_GS_REGNUM && r != 19;
Now, this seems "ok" to do in some sense. However, I can't explain why
native gdb works fine, as it has the same logic in
amd64_windows_segment_register_p.
I'd welcome any thoughts you (or anybody) might have.
Tom
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH] Mask x86 segment registers in Windows gdbserver
2023-12-21 20:29 ` Tom Tromey
@ 2024-01-18 18:51 ` Tom Tromey
2024-01-22 16:57 ` John Baldwin
0 siblings, 1 reply; 5+ messages in thread
From: Tom Tromey @ 2024-01-18 18:51 UTC (permalink / raw)
To: Tom Tromey; +Cc: John Baldwin, gdb-patches
John> LGTM. Treating the x86 seg regs as 32-bits has required workarounds in
John> various places, but not easily fixable unfortunately.
Tom> Well, to my surprise, this patch caused regressions in our internal
Tom> tester.
Tom> It works again if I explicitly exclude the 'ss' register, like:
Tom> if (!windows_process.wow64_process)
Tom> return r >= AMD64_CS_REGNUM && r <= AMD64_GS_REGNUM && r != 19;
Tom> Now, this seems "ok" to do in some sense. However, I can't explain why
Tom> native gdb works fine, as it has the same logic in
Tom> amd64_windows_segment_register_p.
Tom> I'd welcome any thoughts you (or anybody) might have.
I finally figured it out. The Windows CONTEXT structure uses 16-bit
types for these registers' fields. So, gdbserver is actually
reading/writing past the end of the field here.
My patch correctly truncates the value, but the truncated value is
preserved and written back -- overwriting other fields in the thread
context.
I have a new patch that fixes this problem and some other related things
as well. I'll send it once it's been through a bit more testing.
Tom
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH] Mask x86 segment registers in Windows gdbserver
2024-01-18 18:51 ` Tom Tromey
@ 2024-01-22 16:57 ` John Baldwin
0 siblings, 0 replies; 5+ messages in thread
From: John Baldwin @ 2024-01-22 16:57 UTC (permalink / raw)
To: Tom Tromey; +Cc: gdb-patches
On 1/18/24 10:51 AM, Tom Tromey wrote:
> John> LGTM. Treating the x86 seg regs as 32-bits has required workarounds in
> John> various places, but not easily fixable unfortunately.
>
> Tom> Well, to my surprise, this patch caused regressions in our internal
> Tom> tester.
>
> Tom> It works again if I explicitly exclude the 'ss' register, like:
>
> Tom> if (!windows_process.wow64_process)
> Tom> return r >= AMD64_CS_REGNUM && r <= AMD64_GS_REGNUM && r != 19;
>
> Tom> Now, this seems "ok" to do in some sense. However, I can't explain why
> Tom> native gdb works fine, as it has the same logic in
> Tom> amd64_windows_segment_register_p.
>
> Tom> I'd welcome any thoughts you (or anybody) might have.
>
> I finally figured it out. The Windows CONTEXT structure uses 16-bit
> types for these registers' fields. So, gdbserver is actually
> reading/writing past the end of the field here.
>
> My patch correctly truncates the value, but the truncated value is
> preserved and written back -- overwriting other fields in the thread
> context.
>
> I have a new patch that fixes this problem and some other related things
> as well. I'll send it once it's been through a bit more testing.
Ouch, good find.
--
John Baldwin
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2024-01-22 16:57 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-12-19 14:48 [PATCH] Mask x86 segment registers in Windows gdbserver Tom Tromey
2023-12-20 22:03 ` John Baldwin
2023-12-21 20:29 ` Tom Tromey
2024-01-18 18:51 ` Tom Tromey
2024-01-22 16:57 ` John Baldwin
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).