From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from simark.ca by simark.ca with LMTP id AUxoMp/j82pG4AAAWB0awg (envelope-from ) for ; Tue, 10 Nov 2026 22:40:15 -0500 Received: by simark.ca (Postfix, from userid 112) id A91A71E06A; Tue, 10 Nov 2026 22:40:15 -0500 (EST) X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-25) on simark.ca X-Spam-Level: X-Spam-Status: No, score=-2.3 required=5.0 tests=ARC_SIGNED,ARC_VALID,BAYES_00, MAILING_LIST_MULTI,RCVD_IN_DNSWL_MED, RCVD_IN_VALIDITY_CERTIFIED_BLOCKED,RCVD_IN_VALIDITY_RPBL_BLOCKED, RCVD_IN_VALIDITY_SAFE_BLOCKED autolearn=ham autolearn_force=no version=4.0.1 Received: from vm01.sourceware.org (vm01.sourceware.org [38.145.34.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 74BBA1E06A for ; Tue, 10 Nov 2026 22:40:13 -0500 (EST) Received: from vm01.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 03D454BA23D8 for ; Wed, 11 Nov 2026 03:40:13 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 03D454BA23D8 Received: from mailbackend.panix.com (mailbackend.panix.com [166.84.1.89]) by sourceware.org (Postfix) with ESMTPS id 376094BA2E21 for ; Wed, 11 Nov 2026 03:39:49 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 376094BA2E21 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 376094BA2E21 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=1794368389; cv=none; b=SA3Ez02Jib/Tk0MAQauCVVpprC0Kexw2Cap53+OK1rQgAKzVfiUrtDJsFIE08DlJfoUuUyisWYYp5rjobXtmtxBvGGfA3CPbKYLl/ujBpge7n9V8iqzzJ7xPbqPRzdH1DLm1M58CCOaMUxu5nyInMILf5oGiXaL5BrcewFlz+vE= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1794368389; c=relaxed/simple; bh=sKjla0pN3EoCEHsy3vWdknR+F/obGbniTqIzZBm6lks=; h=From:To:Subject:Date:Message-Id:MIME-Version; b=lLBnUB4Zx5ZHtMPiPXeK4fMewITTkrk4PlrqRA+SS4PstPynamo2Y+ciseRf4qgtRMEvfNnmALYBR7f3W+W57aFMNfnzGpPKGzDNj5tYpMeeepb43Ztd6avR5fWa12gvsUYNVSALX8AvXNXqKc3aV9t6QM3G3V6UJ+lsm/EWiqQ= ARC-Authentication-Results: i=1; sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 376094BA2E21 Received: from doobie (unknown [67.245.74.220]) by mailbackend.panix.com (Postfix) with ESMTPSA id 4jPTmN6rp0z42rj; Tue, 10 Nov 2026 22:39:48 -0500 (EST) Received: by doobie (Postfix, from userid 1000) id 9C9BC28E902A; Tue, 10 Nov 2026 22:39:48 -0500 (EST) From: Robert Dubner To: gdb-patches@sourceware.org Cc: stephan.rohr@intel.com Subject: [PATCH v3 1/2] gdb: Preserve neighboring bits when writing floating-point fields Date: Tue, 10 Nov 2026 22:39:37 -0500 Message-Id: <20261111033938.3306719-2-rdubner@symas.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20261111033938.3306719-1-rdubner@symas.com> References: <20261111033938.3306719-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 clear bits outside the requested field when writing part of a byte. For IEEE binary16, writing the exponent can clear the adjacent sign bit, turning a negative value positive. Limit both clearing and setting to the requested field width. Derive the mask from cur_bitshift to account for little-endian formats whose total size is not a multiple of eight bits. Correct the function comment to describe bit lengths and the input requirements. Add a unit test in target-float.c covering neighboring-bit preservation in both byte orders, including formats with partial final bytes. Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=34927 --- gdb/target-float.c | 117 ++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 106 insertions(+), 11 deletions(-) diff --git a/gdb/target-float.c b/gdb/target-float.c index 31b5ac642a7..836f79b8e31 100644 --- a/gdb/target-float.c +++ b/gdb/target-float.c @@ -21,6 +21,7 @@ #include "floatformat.h" #include "target-float.h" #include "gdbarch.h" +#include "gdbsupport/selftest.h" /* Target floating-point operations. @@ -229,8 +230,9 @@ get_field (const bfd_byte *data, enum floatformat_byteorders order, return result; } -/* Set a field which starts at START and is LEN bytes long. DATA and - TOTAL_LEN are the thing we are extracting it from, in byteorder ORDER. */ +/* Set a field which starts at START and is LEN bits long. DATA is the + buffer we are storing into, with TOTAL_LEN bits in byteorder ORDER. + STUFF_TO_PUT is trimmed to fit in LEN bits. */ static void put_field (unsigned char *data, enum floatformat_byteorders order, unsigned int total_len, unsigned int start, unsigned int len, @@ -260,11 +262,11 @@ put_field (unsigned char *data, enum floatformat_byteorders order, } if (cur_bitshift > -FLOATFORMAT_CHAR_BIT) { - *(data + cur_byte) &= - ~(((1 << ((start + len) % FLOATFORMAT_CHAR_BIT)) - 1) - << (-cur_bitshift)); - *(data + cur_byte) |= - (stuff_to_put & ((1 << FLOATFORMAT_CHAR_BIT) - 1)) << (-cur_bitshift); + unsigned int bits + = std::min (len, (unsigned int) (FLOATFORMAT_CHAR_BIT + cur_bitshift)); + unsigned int mask = ((1 << bits) - 1) << (-cur_bitshift); + *(data + cur_byte) &= ~mask; + *(data + cur_byte) |= (stuff_to_put << (-cur_bitshift)) & mask; } cur_bitshift += FLOATFORMAT_CHAR_BIT; if (order == floatformat_little) @@ -277,10 +279,11 @@ put_field (unsigned char *data, enum floatformat_byteorders order, { if (len - cur_bitshift < FLOATFORMAT_CHAR_BIT) { - /* This is the last byte. */ - *(data + cur_byte) &= - ~((1 << (len - cur_bitshift)) - 1); - *(data + cur_byte) |= (stuff_to_put >> cur_bitshift); + /* 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; } else *(data + cur_byte) = ((stuff_to_put >> cur_bitshift) @@ -2491,3 +2494,95 @@ target_float_compare (const gdb_byte *x, const struct type *type_x, const target_float_ops *ops = get_target_float_ops (type_x, type_y); return ops->compare (x, type_x, y, type_y); } + +#if GDB_SELF_TEST +namespace selftests +{ + +/* Check that writing a field preserves neighboring bits and buffer guards. + Use explicit expected bytes, independently of get_field and host floating- + point formats. */ + +static void +put_field_test () +{ + struct test_case + { + floatformat_byteorders order; + unsigned int total_len; + unsigned int start; + unsigned int len; + unsigned long value; + unsigned char before[2]; + unsigned char after[2]; + }; + + const test_case tests[] = + { + /* Writing the binary16 exponent must preserve the sign and fraction. + The first pair produces the encoding of -1.5. */ + {floatformat_big, 16, 1, 5, 15, {0x82, 0x00}, {0xbe, 0x00}}, + {floatformat_little, 16, 1, 5, 15, {0x00, 0x82}, {0x00, 0xbe}}, + {floatformat_big, 16, 1, 5, 0, {0xff, 0xff}, {0x83, 0xff}}, + {floatformat_little, 16, 1, 5, 0, {0xff, 0xff}, {0xff, 0x83}}, + {floatformat_big, 16, 1, 5, 31, {0x80, 0x03}, {0xfc, 0x03}}, + {floatformat_little, 16, 1, 5, 31, {0x03, 0x80}, {0x03, 0xfc}}, + + /* Check fields that span bytes and end on a byte boundary. */ + {floatformat_big, 16, 4, 12, 0x123, {0xff, 0xff}, {0xf1, 0x23}}, + {floatformat_little, 16, 4, 12, 0x123, {0xff, 0xff}, {0x23, 0xf1}}, + {floatformat_big, 16, 0, 16, 0xa55a, {0xff, 0xff}, {0xa5, 0x5a}}, + {floatformat_little, 16, 0, 16, 0xa55a, {0xff, 0xff}, {0x5a, 0xa5}}, + + /* A non-byte-sized format also exercises the little-endian excess + adjustment. Preserve its padding and neighboring field bits. */ + {floatformat_big, 13, 1, 3, 0, {0xff, 0xff}, {0x8f, 0xff}}, + {floatformat_little, 13, 1, 3, 0, {0xff, 0xff}, {0xff, 0xf1}}, + {floatformat_big, 13, 1, 3, 5, {0xff, 0xff}, {0xdf, 0xff}}, + {floatformat_little, 13, 1, 3, 5, {0xff, 0xff}, {0xff, 0xfb}}, + {floatformat_big, 13, 1, 5, 0, {0xff, 0xff}, {0x83, 0xff}}, + {floatformat_little, 13, 1, 5, 0, {0xff, 0xff}, {0x7f, 0xf0}}, + + /* Make sure the code forcing stuff_to_put fits in len bits works. */ + {floatformat_big, 16, 6, 10, 0x7ff, {0x00, 0x00}, {0x03, 0xff}}, + {floatformat_little, 16, 6, 10, 0x7ff, {0x00, 0x00}, {0xff, 0x03}}, + }; + + for (const test_case &test : tests) + { + unsigned char data[] = {0xa5, test.before[0], test.before[1], 0x5a}; + put_field (data + 1, test.order, test.total_len, test.start, test.len, + test.value); + SELF_CHECK (data[0] == 0xa5); + SELF_CHECK (data[1] == test.after[0]); + SELF_CHECK (data[2] == test.after[1]); + SELF_CHECK (data[3] == 0x5a); + } + + /* A 32-bit field that starts and ends partway through a byte. */ + for (auto order : {floatformat_big, floatformat_little}) + { + unsigned char data[] = {0x12, 0xa5, 0xa5, 0xa5, 0xa5, 0xa5, 0x34}; + unsigned char expected[] = {0xaf, 0xed, 0xcb, 0xa9, 0x85}; + + if (order == floatformat_little) + std::reverse (expected, expected + sizeof (expected)); + + put_field (data + 1, order, 40, 4, 32, 0xfedcba98UL); + + SELF_CHECK (data[0] == 0x12); + SELF_CHECK (std::equal (expected, expected + sizeof (expected), + data + 1)); + SELF_CHECK (data[6] == 0x34); + } +} +} /* namespace selftests */ +#endif /* GDB_SELF_TEST */ + +INIT_GDB_FILE (target_float) +{ +#if GDB_SELF_TEST + selftests::register_test ("target-float-put-field", + selftests::put_field_test); +#endif +} -- 2.34.1