From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from simark.ca by simark.ca with LMTP id yfBDNU4L9mo6mAYAWB0awg (envelope-from ) for ; Thu, 12 Nov 2026 13:54:06 -0500 Received: by simark.ca (Postfix, from userid 112) id D1C451E0BC; Thu, 12 Nov 2026 13:54:06 -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 35B4B1E087 for ; Thu, 12 Nov 2026 13:54:06 -0500 (EST) Received: from vm01.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id F2F5E4BA9021 for ; Thu, 12 Nov 2026 18:54:03 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org F2F5E4BA9021 Received: from mailbackend.panix.com (mailbackend.panix.com [166.84.1.89]) by sourceware.org (Postfix) with ESMTPS id 172B14BA2E09 for ; Thu, 12 Nov 2026 18:53:04 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 172B14BA2E09 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 172B14BA2E09 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=1794509584; cv=none; b=hBdUyQQud3UEoXrzwzmo0dQ677UT0TUh6RXJ1R0bYB91+7VTFTXJ02EULGzEye+6c0mcMb13DeJbpoHghLLQBgoAY0LA1Jr7aweyqVDv499ViP4BQlrSJeRJAvz4EAP+Lwd3z3T54Sx5jjnZXsulyNvqtGWaxJ8hl9gc6yohCuQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1794509584; c=relaxed/simple; bh=uwTts0p6GNuf/2oLmjGQf2u01N6appIsjm/b9lu3stk=; h=From:To:Subject:Date:Message-Id:MIME-Version; b=YbgRkmUcFuQtcz0FBc+JQJ8KBVvyB63RTodOTNO+DZEbqnG1rIsvi+zClZFF2L39U1tbtU+zNx7RwNOoM4Zt9SYU6DcjP1XJIPvVbjX9xi3e7DQIteCNVNMdmDmo76qhsb9R5QQyVQzV5egNQtBJZlU8orHuiWt7gQiljAMsCh8= ARC-Authentication-Results: i=1; sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 172B14BA2E09 Received: from doobie (unknown [67.245.74.220]) by mailbackend.panix.com (Postfix) with ESMTPSA id 4jQTzg1WJfz4J3x; Thu, 12 Nov 2026 13:53:03 -0500 (EST) Received: by doobie (Postfix, from userid 1000) id E37DE28E7A26; Thu, 12 Nov 2026 13:53:02 -0500 (EST) From: Robert Dubner To: gdb-patches@sourceware.org Cc: stephan.rohr@intel.com Subject: [PATCH v5 1/5] gdb: Preserve neighboring bits when writing floating-point fields Date: Thu, 12 Nov 2026 13:52:47 -0500 Message-Id: <20261112185251.1617554-2-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 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. Reviewed-By: Rohr, Stephan Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=34927 --- gdb/target-float.c | 118 ++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 107 insertions(+), 11 deletions(-) diff --git a/gdb/target-float.c b/gdb/target-float.c index 31b5ac642a7..89a95067f2f 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,96 @@ 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 STUFF_TO_PUT does not change bits outside the field. */ + {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