From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from simark.ca by simark.ca with LMTP id 0bTlF3868mrAxDsAWB0awg (envelope-from ) for ; Mon, 09 Nov 2026 16:26:23 -0500 Received: by simark.ca (Postfix, from userid 112) id 5D6061E06A; Mon, 09 Nov 2026 16:26:23 -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 B0C221E043 for ; Mon, 09 Nov 2026 16:26:22 -0500 (EST) Received: from vm01.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id DB1D24B920F3 for ; Mon, 9 Nov 2026 21:26:21 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org DB1D24B920F3 Received: from mailbackend.panix.com (mailbackend.panix.com [166.84.1.89]) by sourceware.org (Postfix) with ESMTPS id C92354BA2E1C for ; Mon, 9 Nov 2026 21:25:25 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org C92354BA2E1C 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 C92354BA2E1C 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=1794259525; cv=none; b=sJrxq6chTk5wDTaTcnABXOADyXeAl8K+vf0JXIxxyJuszSt6/zEFkfePDgK6Gho2n06ascdzRNgj631jiW9uF/HVGAUUx+KlvFJqDeYek7Zip+NTC23q04pl8qq2t79A+m3bg5YFmF8R9c1jL4+/d/96qKITFE7zViDIL49HQLI= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1794259525; c=relaxed/simple; bh=a9fdTKtQXL2Bl2sCRDSbPoNnuf6xUs263WpGWPp8Jz8=; h=From:To:Subject:Date:Message-Id:MIME-Version; b=NTsU2rUM3E8G44G9/KF3nZDibHlw/PG0QFoc5Va9pRI/+AUvBzsDqBOmlii16/4Vg3WYLQyf00jAR4kZj64FUS4lDyY0ivk5EnWO4RQXHr9cLycDBqKGVvzFGdGuIv0N0YnYnvzaDfIY9CLjzrpXHWuytZ+iypefgpt4aAulMaw= ARC-Authentication-Results: i=1; sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org C92354BA2E1C Received: from doobie (unknown [67.245.74.220]) by mailbackend.panix.com (Postfix) with ESMTPSA id 4jNjVs4J0Pz4Kl2; Mon, 09 Nov 2026 16:25:25 -0500 (EST) Received: by doobie (Postfix, from userid 1000) id 4C2CB28E93A0; Mon, 9 Nov 2026 16:25:25 -0500 (EST) From: Robert Dubner To: gdb-patches@sourceware.org Cc: stephan.rohr@intel.com Subject: [PATCH v2 1/2] gdb: Preserve neighboring bits when writing floating-point fields Date: Mon, 9 Nov 2026 16:24:49 -0500 Message-Id: <20261109212450.3137464-2-rdubner@symas.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20261109212450.3137464-1-rdubner@symas.com> References: <20261109212450.3137464-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 | 88 ++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 81 insertions(+), 7 deletions(-) diff --git a/gdb/target-float.c b/gdb/target-float.c index 31b5ac642a7..8032d4b7738 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 must 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) @@ -2491,3 +2493,75 @@ 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}} + }; + + 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); + } +} + +} /* 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