From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from simark.ca by simark.ca with LMTP id 8bT+FXIL9mo6mAYAWB0awg (envelope-from ) for ; Thu, 12 Nov 2026 13:54:42 -0500 Received: by simark.ca (Postfix, from userid 112) id 535B51E0BC; Thu, 12 Nov 2026 13:54:42 -0500 (EST) X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-25) on simark.ca X-Spam-Level: X-Spam-Status: No, score=-5.3 required=5.0 tests=ARC_SIGNED,ARC_VALID,BAYES_00, MAILING_LIST_MULTI,RCVD_IN_DNSWL_MED autolearn=ham autolearn_force=no version=4.0.1 Received: from vm01.sourceware.org (vm01.sourceware.org [IPv6:2620:52:6:3111::32]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange x25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by simark.ca (Postfix) with ESMTPS id 509CE1E091 for ; Thu, 12 Nov 2026 13:54:41 -0500 (EST) Received: from vm01.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 498334BA9004 for ; Thu, 12 Nov 2026 18:54:40 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 498334BA9004 Received: from mailbackend.panix.com (mailbackend.panix.com [166.84.1.89]) by sourceware.org (Postfix) with ESMTPS id 65C1A4BA2E2C for ; Thu, 12 Nov 2026 18:53:06 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 65C1A4BA2E2C Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=symas.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=dubner.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 65C1A4BA2E2C Authentication-Results: sourceware.org; arc=none smtp.remote-ip=166.84.1.89 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1794509586; cv=none; b=waNZseofYaSPubL/rTzY+Yibj7FPeFo4Kmpn05d0fmEqrlAMV4DeF9O5b8+9FY+aqfGNu6XhEeI5y0oGbGieOQGEJyU89B521RAzX5KDeZamNqWgt14H2dblfgPODBEaZiLQwadVdbGzfUi7azwEnYaNLfULZFJeBZ1vW0F0zLg= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1794509586; c=relaxed/simple; bh=nj3QrteFpqFnbYRmSezCnf0yeAW7NqGx5/uuXnXMuyI=; h=From:To:Subject:Date:Message-Id:MIME-Version; b=cFArusX9rvCtInzk1AQyQXhNNI16VNf4kBmDjss11xsvXpcYoyyy9x1qVRRH8E/0p8/wcVPKy6wS63C9qRHEKRYMuwhchJYnipN0lycl5ppsogQxTa4kVS+j8riqqtHczDcFg4Q3CJImgakp+JMUlAXXQkjgGzPTeKrV2txZyPM= ARC-Authentication-Results: i=1; sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 65C1A4BA2E2C Received: from doobie (unknown [67.245.74.220]) by mailbackend.panix.com (Postfix) with ESMTPSA id 4jQTzj1RcBz4HhR; Thu, 12 Nov 2026 13:53:05 -0500 (EST) Received: by doobie (Postfix, from userid 1000) id E9FE428E7A26; Thu, 12 Nov 2026 13:53:04 -0500 (EST) From: Robert Dubner To: gdb-patches@sourceware.org Cc: stephan.rohr@intel.com Subject: [PATCH v5 5/5] gdb: Avoid oversized shifts when writing floating-point fields Date: Thu, 12 Nov 2026 13:52:51 -0500 Message-Id: <20261112185251.1617554-6-rdubner@symas.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20261112185251.1617554-1-rdubner@symas.com> References: <20261112185251.1617554-1-rdubner@symas.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-BeenThere: gdb-patches@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gdb-patches-bounces~public-inbox=simark.ca@sourceware.org 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 --- gdb/target-float.c | 104 ++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 97 insertions(+), 7 deletions(-) diff --git a/gdb/target-float.c b/gdb/target-float.c index 1d7fe55fa3b..e21e905f038 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; @@ -2853,13 +2858,10 @@ signed_zero_test () {&floatformat_vax_f, 1} }; - type_allocator alloc (get_current_arch ()); std::vector types; for (const test_case &test : tests) { - const floatformat *pair[] = {test.fmt, test.fmt}; - types.push_back (init_float_type (alloc, -1, "selftest_float", pair, - BFD_ENDIAN_BIG)); + types.push_back (subnormal_type(test.fmt)); } mpfr_float_ops ops; @@ -2926,6 +2928,90 @@ 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 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; + for (const floatformat *fmt : {&floatformat_ieee_quad_big, + &floatformat_ieee_quad_little}) + { + type *ftype = subnormal_type(fmt); + 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 expected (16); + bits.write (expected, order, true); + std::vector actual (16, 0xa5); + SELF_CHECK (ops.from_string (actual.data (), ftype, text)); + SELF_CHECK (actual == expected); + } + } +} + } /* namespace selftests */ #endif /* GDB_SELF_TEST */ @@ -2936,6 +3022,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