From: Robert Dubner <rdubner@symas.com>
To: gdb-patches@sourceware.org
Cc: stephan.rohr@intel.com
Subject: [PATCH v4 5/5] gdb: Avoid oversized shifts when writing floating-point fields
Date: Wed, 11 Nov 2026 12:42:47 -0500 [thread overview]
Message-ID: <20261111174247.3382098-6-rdubner@symas.com> (raw)
In-Reply-To: <20261111174247.3382098-1-rdubner@symas.com>
put_field can write fields wider than unsigned long, such as the
112-bit binary128 fraction field used when encoding infinities and
NaNs. Its loop can shift stuff_to_put by its width or more, which is
undefined even when the value is zero.
Use zero once the shift reaches the width of unsigned long. Apply
the guarded value to both full-byte and partial-byte writes,
preserving the existing neighboring-bit masks.
Add unit tests in target-float.c covering wide fields, alignment,
both byte orders, neighboring bits, buffer guards, and binary128
infinity and NaN encodings.
Reviewed-By: Rohr, Stephan <stephan.rohr@intel.com>
---
gdb/target-float.c | 102 +++++++++++++++++++++++++++++++++++++++++++--
1 file changed, 99 insertions(+), 3 deletions(-)
diff --git a/gdb/target-float.c b/gdb/target-float.c
index fc0b7931860..068310932f0 100644
--- a/gdb/target-float.c
+++ b/gdb/target-float.c
@@ -245,6 +245,7 @@ put_field (unsigned char *data, enum floatformat_byteorders order,
{
unsigned int cur_byte;
int cur_bitshift;
+ constexpr int value_bits = sizeof (stuff_to_put) * FLOATFORMAT_CHAR_BIT;
/* Caller must byte-swap words before calling this routine. */
gdb_assert (order == floatformat_little || order == floatformat_big);
@@ -282,17 +283,21 @@ put_field (unsigned char *data, enum floatformat_byteorders order,
/* Move towards the most significant part of the field. */
while (cur_bitshift < len)
{
+ /* Fields such as a binary128 fraction can be wider than STUFF_TO_PUT.
+ Zero-extend rather than shifting by the width of unsigned long or
+ more. */
+ unsigned long shifted
+ = cur_bitshift < value_bits ? stuff_to_put >> cur_bitshift : 0;
if (len - cur_bitshift < FLOATFORMAT_CHAR_BIT)
{
/* This is the last byte. Make sure we don't OR in extra high-order
bits when STUFF_TO_PUT violates the LEN restriction. */
unsigned int mask = (1U << (len - cur_bitshift)) - 1;
*(data + cur_byte) &= ~mask;
- *(data + cur_byte) |= (stuff_to_put >> cur_bitshift) & mask;
+ *(data + cur_byte) |= shifted & mask;
}
else
- *(data + cur_byte) = ((stuff_to_put >> cur_bitshift)
- & ((1 << FLOATFORMAT_CHAR_BIT) - 1));
+ *(data + cur_byte) = shifted & ((1 << FLOATFORMAT_CHAR_BIT) - 1);
cur_bitshift += FLOATFORMAT_CHAR_BIT;
if (order == floatformat_little)
++cur_byte;
@@ -2927,6 +2932,93 @@ signed_zero_test ()
}
}
+/* Check zero extension when a field is wider than unsigned long. Build
+ expected bytes with arbitrary-precision integer operations, independently
+ of put_field and get_field, and preserve neighboring bits and guards. */
+
+static void
+put_field_wide_test ()
+{
+ constexpr unsigned int value_bits
+ = sizeof (unsigned long) * FLOATFORMAT_CHAR_BIT;
+ struct test_case
+ {
+ unsigned int start;
+ unsigned int len;
+ };
+ const test_case tests[] =
+ {
+ {0, 128},
+ {16, 112}, /* The binary128 fraction field. */
+ {15, 112},
+ {12, 113},
+ {8, value_bits},
+ {7, value_bits},
+ {7, value_bits + 1},
+ {0, value_bits + 8}
+ };
+
+ for (auto order : {floatformat_big, floatformat_little})
+ for (const test_case &test : tests)
+ for (unsigned long value : {0UL, 1UL, 0xa5UL, ~0UL,
+ 1UL << (value_bits - 1)})
+ for (gdb_byte initial : {0x00, 0xa5, 0xff})
+ {
+ gdb_byte data[18];
+ memset (data, initial, sizeof (data));
+ data[0] = 0x3c;
+ data[17] = 0xc3;
+ std::vector<gdb_byte> expected (16, initial);
+ bfd_endian byte_order = order == floatformat_big
+ ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
+ unsigned int shift = 128 - test.start - test.len;
+ gdb_mpz image;
+ image.read (expected, byte_order, true);
+ gdb_mpz mask = ((gdb_mpz (1) << test.len) - gdb_mpz (1)) << shift;
+ mask.complement ();
+ image = (image & mask) | (gdb_mpz (value) << shift);
+ image.write (expected, byte_order, true);
+
+ put_field (data + 1, order, 128, test.start, test.len, value);
+ SELF_CHECK (data[0] == 0x3c);
+ SELF_CHECK (std::equal (expected.begin (), expected.end (),
+ data + 1));
+ SELF_CHECK (data[17] == 0xc3);
+ }
+}
+
+/* The MPFR encoder writes a complete binary128 fraction for infinities and
+ NaNs. Exercise that caller as well as the field writer itself. */
+
+static void
+wide_special_test ()
+{
+ mpfr_float_ops ops;
+ type_allocator alloc (get_current_arch ());
+ for (const floatformat *fmt : {&floatformat_ieee_quad_big,
+ &floatformat_ieee_quad_little})
+ {
+ const floatformat *pair[] = {fmt, fmt};
+ type *ftype = init_float_type (alloc, -1, "selftest_quad", pair,
+ BFD_ENDIAN_BIG);
+ bfd_endian order = fmt->byteorder == floatformat_big
+ ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
+ for (const char *text : {"inf", "-inf", "nan"})
+ {
+ gdb_mpz bits = gdb_mpz (0x7fff) << 112;
+ if (text[0] == '-')
+ bits = bits | (gdb_mpz (1) << 127);
+ else if (text[0] == 'n')
+ bits += 1;
+ std::vector<gdb_byte> expected (16);
+ bits.write (expected, order, true);
+ std::vector<gdb_byte> actual (16, 0xa5);
+ SELF_CHECK (ops.from_string (actual.data (), ftype, text));
+ SELF_CHECK (actual == expected);
+ }
+ }
+}
+
} /* namespace selftests */
#endif /* GDB_SELF_TEST */
@@ -2937,6 +3029,10 @@ INIT_GDB_FILE (target_float)
selftests::put_field_test);
selftests::register_test ("target-float-signed-zero",
selftests::signed_zero_test);
+ selftests::register_test ("target-float-put-field-wide",
+ selftests::put_field_wide_test);
+ selftests::register_test ("target-float-wide-specials",
+ selftests::wide_special_test);
selftests::register_test ("target-float-subnormal",
selftests::subnormal_test);
selftests::register_test ("target-float-subnormal-rounding",
--
2.34.1
next prev parent reply other threads:[~2026-11-11 17:43 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-11 17:42 [PATCH v4 0/5] gdb: Fix target floating-point encoding and signed-zero negation Robert Dubner
2026-11-11 17:42 ` [PATCH v4 1/5] gdb: Preserve neighboring bits when writing floating-point fields Robert Dubner
2026-11-12 9:04 ` Rohr, Stephan
2026-11-11 17:42 ` [PATCH v4 2/5] gdb: Preserve subnormal values in target floating-point operations Robert Dubner
2026-11-12 9:25 ` Rohr, Stephan
2026-11-11 17:42 ` [PATCH v4 3/5] gdb: Preserve signed zero in MPFR target conversions Robert Dubner
2026-11-12 9:30 ` Rohr, Stephan
2026-11-11 17:42 ` [PATCH v4 4/5] gdb: Preserve signed zero in floating-point unary negation Robert Dubner
2026-11-12 9:32 ` Rohr, Stephan
2026-11-11 17:42 ` Robert Dubner [this message]
2026-11-12 9:48 ` [PATCH v4 5/5] gdb: Avoid oversized shifts when writing floating-point fields Rohr, Stephan
2026-11-12 17:05 ` Robert Dubner
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=20261111174247.3382098-6-rdubner@symas.com \
--to=rdubner@symas.com \
--cc=gdb-patches@sourceware.org \
--cc=stephan.rohr@intel.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