Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
From: Andrew Burgess <aburgess@redhat.com>
To: gdb-patches@sourceware.org
Cc: Tom Tromey <tom@tromey.com>, Andrew Burgess <aburgess@redhat.com>
Subject: [PATCH] gdb: fix reg_buffer::raw_supply with std::span
Date: Sat, 21 Nov 2026 21:49:47 +0000	[thread overview]
Message-ID: <20dd9eefcb195157a8d7860721627150649ed751.1795297721.git.aburgess@redhat.com> (raw)

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



             reply	other threads:[~2026-11-21 21:50 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-21 21:49 Andrew Burgess [this message]
2026-11-22  5:42 ` Tom Tromey
2026-11-22 23:21   ` Andrew Burgess

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20dd9eefcb195157a8d7860721627150649ed751.1795297721.git.aburgess@redhat.com \
    --to=aburgess@redhat.com \
    --cc=gdb-patches@sourceware.org \
    --cc=tom@tromey.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox