From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from simark.ca by simark.ca with LMTP id nuytHjqp9GqQgwIAWB0awg (envelope-from ) for ; Wed, 11 Nov 2026 12:43:22 -0500 Received: by simark.ca (Postfix, from userid 112) id 70A59378002; Wed, 11 Nov 2026 12:43:22 -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 D19871E04F for ; Wed, 11 Nov 2026 12:43:21 -0500 (EST) Received: from vm01.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id D3E9A4BBCD87 for ; Wed, 11 Nov 2026 17:43:20 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org D3E9A4BBCD87 Received: from mailbackend.panix.com (mailbackend.panix.com [166.84.1.89]) by sourceware.org (Postfix) with ESMTPS id A39374B99F6C for ; Wed, 11 Nov 2026 17:42:55 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org A39374B99F6C 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 A39374B99F6C 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=1794418975; cv=none; b=Vzwy7V+onOVmHCeOb7sE2m/+U7B9cMxQpp3I3+8lexn2OTKiXWpXA/4JrC4jZF337nShKnwksmo8spSLLD5tLELN7xKKFwwPHpGkFVe0cziP2HTlJf/qzBxAVc7uoyrp0pq4m9KZ4sNPKBIfjmikvNWBBXoH7iLqwD3QkT9Nlmk= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1794418975; c=relaxed/simple; bh=H2bP8nC8tonkLIDVEMdfbmT7oCglIPBeKrCqw9ImuDI=; h=From:To:Subject:Date:Message-Id:MIME-Version; b=a9dR7JRmSRVn650Nd82zTo7D5qbEfO5xCuqFt80HkbYmMphcN+m5QJCqL9YHzvhnr6IaU6CkqMmICJIgC5YtrhlIIir40zsUG3x/EE6/cjksW93DxIJTKzhxuwUJKOCbagU3uRRNqCSQ1HBBUdq5pIsFbtABGiOV3JT3kx4bYK8= ARC-Authentication-Results: i=1; sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org A39374B99F6C Received: from doobie (unknown [67.245.74.220]) by mailbackend.panix.com (Postfix) with ESMTPSA id 4jPrTC3F1Hz175g; Wed, 11 Nov 2026 12:42:55 -0500 (EST) Received: by doobie (Postfix, from userid 1000) id 290FF28E902A; Wed, 11 Nov 2026 12:42:55 -0500 (EST) From: Robert Dubner 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 Message-Id: <20261111174247.3382098-6-rdubner@symas.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20261111174247.3382098-1-rdubner@symas.com> References: <20261111174247.3382098-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 | 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 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 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 */ @@ -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