* [PATCH] gdb: fix reg_buffer::raw_supply with std::span
@ 2026-11-21 21:49 Andrew Burgess
2026-11-22 5:42 ` Tom Tromey
0 siblings, 1 reply; 3+ messages in thread
From: Andrew Burgess @ 2026-11-21 21:49 UTC (permalink / raw)
To: gdb-patches; +Cc: Tom Tromey, Andrew Burgess
After the switch to use std::span (where possible), and when building
GDB in C++20 mode with -D_GLIBCXX_DEBUG, I'm seeing failures when
running with the --target_board=native-gdbserver, like this:
/usr/include/c++/16/span:186:
In function:
constexpr std::span<_Type, _Extent>::span(_It, size_type) [with _It =
const unsigned char*; _Type = const unsigned char; long unsigned int
_Extent = 18446744073709551615; size_type = long unsigned int]
Error: function requires a valid iterator range [first, first + count).
Objects involved in the operation:
iterator "=================================================================
==314011==ERROR: AddressSanitizer: global-buffer-overflow on address 0x000007c8a108 at pc 0x7fde522b31b0 bp 0x7ffe9c1e0bc0 sp 0x7ffe9c1e0380
READ of size 9 at 0x000007c8a108 thread T0
<snip>
#5 0x00000040830b in std::span<unsigned char const, 18446744073709551615ul>::span<unsigned char const*>(unsigned char const*, unsigned long) /usr/include/c++/16/span:186
#6 0x00000068b16b in std::span<unsigned char const, 18446744073709551615ul> gdb::make_span<unsigned char const>(unsigned char const*, unsigned long) ../../src/gdb/../gdbsupport/span.h:273
#7 0x0000030f8b0b in reg_buffer::raw_supply(int, void const*) ../../src/gdb/regcache.c:1165
#8 0x0000031eeaf7 in remote_target::process_g_packet(regcache*) ../../src/gdb/remote.c:9434
#9 0x0000031eef66 in remote_target::fetch_registers_using_g(regcache*) ../../src/gdb/remote.c:9446
#10 0x0000031efab2 in remote_target::fetch_registers(regcache*, int) ../../src/gdb/remote.c:9495
<snip>
The problem here is in gdb/regcache.c, in this code:
void
reg_buffer::raw_supply (int regnum, const void *src)
{
assert_regnum (regnum);
int size = m_descr->sizeof_register[regnum];
raw_supply (regnum, gdb::make_span ((const gdb_byte *) src, size));
}
The API for this function allows SRC to be nullptr, this is handled in
the raw_supply overload that the above code forwards to. However, the
size of a register is usually not 0, so this code ends up trying to
create a std::span where SRC is nullptr and SIZE is non-zero; this is
undefined behaviour and triggers the assert we see above.
Solve this by handling the case where SRC is nullptr explicitly; create
an empty span for this case. After this the above assert is resolved
and gdb.base/break.exp passes without issue using native-gdbserver
board.
After fixing this I did a quick audit of other regcache code to see if
the problem could be present anywhere else. In reg_buffer_common the
two overloads:
void raw_supply (int regnum, const gdb_byte *src)
void raw_supply (int regnum, const uint64_t *src)
both have a similar pattern; taking a pointer and forwarding to the:
virtual void raw_supply (int regnum, gdb::span<const gdb_byte> src)
member function. I don't know if either of these are ever called with a
nullptr pointer, but if they are they would trigger the same assert. So
to be safe I had these also handle the SRC is nullptr case.
While I was here I have updated the comments in
gdbsupport/common-regcache.h to better document the API.
Finally, I have also added an assert to gdb::make_span which catches
attempts to create a span with a nullptr data pointer and a non-zero
length. This will mean that GDB hits this assert even if it is not
compiled with -D_GLIBCXX_DEBUG, which increases our test coverage.
---
gdb/regcache.c | 6 +++++-
gdb/regcache.h | 3 ++-
gdbsupport/common-regcache.h | 20 +++++++++++++++++---
gdbsupport/span.h | 1 +
4 files changed, 25 insertions(+), 5 deletions(-)
diff --git a/gdb/regcache.c b/gdb/regcache.c
index 0e7e6a85284..c3a5d2c2c38 100644
--- a/gdb/regcache.c
+++ b/gdb/regcache.c
@@ -1162,7 +1162,11 @@ reg_buffer::raw_supply (int regnum, const void *src)
assert_regnum (regnum);
int size = m_descr->sizeof_register[regnum];
- raw_supply (regnum, gdb::make_span ((const gdb_byte *) src, size));
+
+ /* Creating a std::span with nullptr data and non-zero length is undefined. */
+ raw_supply (regnum, (src == nullptr
+ ? gdb::span<const gdb_byte> {}
+ : gdb::make_span ((const gdb_byte *) src, size)));
}
/* See regcache.h. */
diff --git a/gdb/regcache.h b/gdb/regcache.h
index 5e9c2be095e..1aaed9db7aa 100644
--- a/gdb/regcache.h
+++ b/gdb/regcache.h
@@ -222,7 +222,8 @@ class reg_buffer : public reg_buffer_common
/* See gdbsupport/common-regcache.h. */
void raw_supply (int regnum, gdb::span<const gdb_byte> src) override;
- /* Deprecated overload of the above. */
+ /* Deprecated overload of the above. If SRC is nullptr then the register is
+ marked as unavailable. */
void raw_supply (int regnum, const void *src);
void raw_supply (int regnum, const reg_buffer &src)
diff --git a/gdbsupport/common-regcache.h b/gdbsupport/common-regcache.h
index 0c3b2129572..3479ac330e0 100644
--- a/gdbsupport/common-regcache.h
+++ b/gdbsupport/common-regcache.h
@@ -77,18 +77,32 @@ struct reg_buffer_common
virtual int register_size (int regnum) const = 0;
/* Supply register REGNUM, whose contents are stored in SRC, to this register
- buffer. */
+ buffer. If SRC.data() == nullptr then the register is marked unavailable. */
virtual void raw_supply (int regnum, gdb::span<const gdb_byte> src)
= 0;
+ /* Supply register REGNUM, whose contents are *SRC. If SRC == nullptr then the
+ register is marked unavailable. */
void raw_supply (int regnum, const uint64_t *src)
{
+ /* Creating a std::span with nullptr data and non-zero length is undefined. */
raw_supply (regnum,
- gdb::make_span ((const gdb_byte *) src, sizeof (*src)));
+ (src == nullptr
+ ? gdb::span<const gdb_byte> {}
+ : gdb::make_span ((const gdb_byte *) src, sizeof (*src))));
}
+ /* Supply register REGNUM, whose contents are *SRC. There must be at least
+ register_size(REGNUM) bytes available at SRC. If SRC == nullptr then the
+ register is marked unavailable. */
void raw_supply (int regnum, const gdb_byte *src)
- { raw_supply (regnum, gdb::make_span (src, register_size (regnum))); }
+ {
+ /* Creating a std::span with nullptr data and non-zero length is undefined. */
+ raw_supply (regnum,
+ (src == nullptr
+ ? gdb::span<const gdb_byte> {}
+ : gdb::make_span (src, register_size (regnum))));
+ }
/* Supply part of register REGNUM with zeroed value. Start at OFFSET in
the register, with size SIZE. The rest of the register is left
diff --git a/gdbsupport/span.h b/gdbsupport/span.h
index 5288391d2e1..f27dc0a01f7 100644
--- a/gdbsupport/span.h
+++ b/gdbsupport/span.h
@@ -270,6 +270,7 @@ template<typename U>
constexpr inline span<U>
make_span (U *array, size_t size) noexcept
{
+ gdb_assert (array != nullptr || size == 0);
return {array, size};
}
base-commit: 0c4864d8879dced5d9178a96b7c3b78f0f5a1580
--
2.56.1
^ permalink raw reply [flat|nested] 3+ messages in thread* Re: [PATCH] gdb: fix reg_buffer::raw_supply with std::span
2026-11-21 21:49 [PATCH] gdb: fix reg_buffer::raw_supply with std::span Andrew Burgess
@ 2026-11-22 5:42 ` Tom Tromey
2026-11-22 23:21 ` Andrew Burgess
0 siblings, 1 reply; 3+ messages in thread
From: Tom Tromey @ 2026-11-22 5:42 UTC (permalink / raw)
To: Andrew Burgess; +Cc: gdb-patches, Tom Tromey
>>>>> "Andrew" == Andrew Burgess <aburgess@redhat.com> writes:
Andrew> After the switch to use std::span (where possible), and when building
Andrew> GDB in C++20 mode with -D_GLIBCXX_DEBUG, I'm seeing failures when
Andrew> running with the --target_board=native-gdbserver, like this:
I sent a patch just now but this is ok.
Approved-By: Tom Tromey <tom@tromey.com>
Tom
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH] gdb: fix reg_buffer::raw_supply with std::span
2026-11-22 5:42 ` Tom Tromey
@ 2026-11-22 23:21 ` Andrew Burgess
0 siblings, 0 replies; 3+ messages in thread
From: Andrew Burgess @ 2026-11-22 23:21 UTC (permalink / raw)
To: Tom Tromey; +Cc: gdb-patches, Tom Tromey
Tom Tromey <tom@tromey.com> writes:
>>>>>> "Andrew" == Andrew Burgess <aburgess@redhat.com> writes:
>
> Andrew> After the switch to use std::span (where possible), and when building
> Andrew> GDB in C++20 mode with -D_GLIBCXX_DEBUG, I'm seeing failures when
> Andrew> running with the --target_board=native-gdbserver, like this:
>
> I sent a patch just now but this is ok.
> Approved-By: Tom Tromey <tom@tromey.com>
Pushed.
Thanks,
Andrew
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2026-11-22 23:22 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-21 21:49 [PATCH] gdb: fix reg_buffer::raw_supply with std::span Andrew Burgess
2026-11-22 5:42 ` Tom Tromey
2026-11-22 23:21 ` Andrew Burgess
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox