From patchwork Fri Mar 3 21:12:02 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tom Tromey X-Patchwork-Id: 65994 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id C30FB384FB49 for ; Fri, 3 Mar 2023 21:13:23 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org C30FB384FB49 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1677878003; bh=+n4ofZB/vWJB2OJNIceOMRaVsjl6U+y3wfrGXcWS+xE=; h=To:Cc:Subject:Date:In-Reply-To:References:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From:Reply-To:From; b=M+yDb03FREDRqbYdv9koQld9L3VZxdcNsfxQdYfMXDQn8YmPtP/ZEy8OnKPNBvhX9 8ueHyV5vhbDQDUgsEfDnkbSBWuYW74kUyUZyM5U6oCSObcTijgA1NHa4AFjnD7eJ2n hjs2D1pct1DdzqMZGVrIpD773UUCpeucBgFnGfG0= X-Original-To: gdb-patches@sourceware.org Delivered-To: gdb-patches@sourceware.org Received: from mail-io1-xd2e.google.com (mail-io1-xd2e.google.com [IPv6:2607:f8b0:4864:20::d2e]) by sourceware.org (Postfix) with ESMTPS id A301D3858C5E for ; Fri, 3 Mar 2023 21:12:21 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org A301D3858C5E Received: by mail-io1-xd2e.google.com with SMTP id 76so1519695iou.9 for ; Fri, 03 Mar 2023 13:12:21 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1677877940; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=+n4ofZB/vWJB2OJNIceOMRaVsjl6U+y3wfrGXcWS+xE=; b=l8Ztky9oMxe4GQcnpZaK3bQ0dLX0/L6kLkgT7fMpmgfCdYXjCvU9FWW4jW170KqBCP nXJ7AM1xvtxwCgwISypuyhE3gNLyK6p01VgLbxXngn+KYzY+TYJU+FhhoJi3a+cJJl95 ucIa1GgiN2KbY5Oti5VrV9yzlf6eKoFE2EZRUrS9fuB2i86sy9tF4GuBFoKj0CWFQ7YC 8s/GdFBN1+5zMVPQeGzPuXC45qVzHE9ZROuQDttyAFOuL/Y7EBfYtbjGxWpEuUpAoLbN 42vlehcBvyjb7Tb06lcWA9YLTSxuXXMX3zhkkf1Kzn8qnbm4skSwyEU82PaeYra1QdaK xQrA== X-Gm-Message-State: AO0yUKWpmJIE5E5qau9v52e1Bdvwd978uNJhEhfOfPCKhID80UsuV4Xy XXu24+Ea2THst7rB4kaaNEefEj3bKsqHdLwh6iA= X-Google-Smtp-Source: AK7set8wsSPixARlKrugZxcWAG8ICZRDlf8mlln4vUZPjd2oOsmi/P3GWDJH2TrIJizgOCnXarIBJw== X-Received: by 2002:a6b:600f:0:b0:704:b286:64c3 with SMTP id r15-20020a6b600f000000b00704b28664c3mr1803562iog.16.1677877940570; Fri, 03 Mar 2023 13:12:20 -0800 (PST) Received: from localhost.localdomain (75-166-130-93.hlrn.qwest.net. [75.166.130.93]) by smtp.gmail.com with ESMTPSA id g8-20020a925208000000b00313b281ecd2sm867988ilb.70.2023.03.03.13.12.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 03 Mar 2023 13:12:20 -0800 (PST) To: gdb-patches@sourceware.org Cc: Tom Tromey Subject: [PATCH 3/8] Add truncation mode to gdb_mpz Date: Fri, 3 Mar 2023 14:12:02 -0700 Message-Id: <20230303211207.1053037-4-tromey@adacore.com> X-Mailer: git-send-email 2.39.1 In-Reply-To: <20230303211207.1053037-1-tromey@adacore.com> References: <20230303211207.1053037-1-tromey@adacore.com> MIME-Version: 1.0 X-Spam-Status: No, score=-11.6 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gdb-patches@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Tom Tromey via Gdb-patches From: Tom Tromey Reply-To: Tom Tromey Errors-To: gdb-patches-bounces+patchwork=sourceware.org@sourceware.org Sender: "Gdb-patches" This renames gdb_mpz::safe_export to export_bits, and adds a new flag to export a truncated value. This is needed by value arithmetic. --- gdb/gmp-utils.c | 93 ++++++++++++++++++++++++++++--------------------- gdb/gmp-utils.h | 29 +++++++++++---- 2 files changed, 76 insertions(+), 46 deletions(-) diff --git a/gdb/gmp-utils.c b/gdb/gmp-utils.c index 57f5f9766b9..0afa344781b 100644 --- a/gdb/gmp-utils.c +++ b/gdb/gmp-utils.c @@ -66,18 +66,8 @@ gdb_mpz::read (gdb::array_view buf, enum bfd_endian byte_order, /* See gmp-utils.h. */ void -gdb_mpz::write (gdb::array_view buf, enum bfd_endian byte_order, - bool unsigned_p) const -{ - this->safe_export - (buf, byte_order == BFD_ENDIAN_BIG ? 1 : -1 /* endian */, unsigned_p); -} - -/* See gmp-utils.h. */ - -void -gdb_mpz::safe_export (gdb::array_view buf, - int endian, bool unsigned_p) const +gdb_mpz::export_bits (gdb::array_view buf, int endian, bool unsigned_p, + bool safe) const { gdb_assert (buf.size () > 0); @@ -92,36 +82,39 @@ gdb_mpz::safe_export (gdb::array_view buf, return; } - /* Determine the maximum range of values that our buffer can hold, - and verify that VAL is within that range. */ - - gdb_mpz lo, hi; - const size_t max_usable_bits = buf.size () * HOST_CHAR_BIT; - if (unsigned_p) - { - lo = 0; - - mpz_ui_pow_ui (hi.m_val, 2, max_usable_bits); - mpz_sub_ui (hi.m_val, hi.m_val, 1); - } - else + if (safe) { - mpz_ui_pow_ui (lo.m_val, 2, max_usable_bits - 1); - mpz_neg (lo.m_val, lo.m_val); - - mpz_ui_pow_ui (hi.m_val, 2, max_usable_bits - 1); - mpz_sub_ui (hi.m_val, hi.m_val, 1); + /* Determine the maximum range of values that our buffer can + hold, and verify that VAL is within that range. */ + + gdb_mpz lo, hi; + const size_t max_usable_bits = buf.size () * HOST_CHAR_BIT; + if (unsigned_p) + { + lo = 0; + + mpz_ui_pow_ui (hi.m_val, 2, max_usable_bits); + mpz_sub_ui (hi.m_val, hi.m_val, 1); + } + else + { + mpz_ui_pow_ui (lo.m_val, 2, max_usable_bits - 1); + mpz_neg (lo.m_val, lo.m_val); + + mpz_ui_pow_ui (hi.m_val, 2, max_usable_bits - 1); + mpz_sub_ui (hi.m_val, hi.m_val, 1); + } + + if (mpz_cmp (m_val, lo.m_val) < 0 || mpz_cmp (m_val, hi.m_val) > 0) + error (_("Cannot export value %s as %zu-bits %s integer" + " (must be between %s and %s)"), + this->str ().c_str (), + max_usable_bits, + unsigned_p ? _("unsigned") : _("signed"), + lo.str ().c_str (), + hi.str ().c_str ()); } - if (mpz_cmp (m_val, lo.m_val) < 0 || mpz_cmp (m_val, hi.m_val) > 0) - error (_("Cannot export value %s as %zu-bits %s integer" - " (must be between %s and %s)"), - this->str ().c_str (), - max_usable_bits, - unsigned_p ? _("unsigned") : _("signed"), - lo.str ().c_str (), - hi.str ().c_str ()); - const gdb_mpz *exported_val = this; gdb_mpz un_signed; if (sign < 0) @@ -134,6 +127,28 @@ gdb_mpz::safe_export (gdb::array_view buf, exported_val = &un_signed; } + /* If the value is too large, truncate it. */ + if (!safe + && mpz_sizeinbase (exported_val->m_val, 2) > buf.size () * HOST_CHAR_BIT) + { + /* If we don't already have a copy, make it now. */ + if (exported_val != &un_signed) + { + un_signed = *exported_val; + exported_val = &un_signed; + } + + un_signed.mask (buf.size () * HOST_CHAR_BIT); + } + + /* It's possible that one of the above results in zero, which has to + be handled specially. */ + if (exported_val->sgn () == 0) + { + memset (buf.data (), 0, buf.size ()); + return; + } + /* Do the export into a buffer allocated by GMP itself; that way, we can detect cases where BUF is not large enough to export our value, and thus avoid a buffer overlow. Normally, this should diff --git a/gdb/gmp-utils.h b/gdb/gmp-utils.h index d4e5015e345..d378499d287 100644 --- a/gdb/gmp-utils.h +++ b/gdb/gmp-utils.h @@ -137,7 +137,20 @@ struct gdb_mpz UNSIGNED_P indicates whether the number has an unsigned type. */ void write (gdb::array_view buf, enum bfd_endian byte_order, - bool unsigned_p) const; + bool unsigned_p) const + { + export_bits (buf, byte_order == BFD_ENDIAN_BIG ? 1 : -1 /* endian */, + unsigned_p, true /* safe */); + } + + /* Like write, but truncates the value to the desired number of + bytes. */ + void truncate (gdb::array_view buf, enum bfd_endian byte_order, + bool unsigned_p) const + { + export_bits (buf, byte_order == BFD_ENDIAN_BIG ? 1 : -1 /* endian */, + unsigned_p, false /* safe */); + } /* Return a string containing VAL. */ std::string str () const { return gmp_string_printf ("%Zd", m_val); } @@ -337,10 +350,11 @@ struct gdb_mpz . -1 for least significant byte first; or . 0 for native endianness. - An error is raised if BUF is not large enough to contain the value - being exported. */ - void safe_export (gdb::array_view buf, - int endian, bool unsigned_p) const; + If SAFE is true, an error is raised if BUF is not large enough to + contain the value being exported. If SAFE is false, the value is + truncated to fit in BUF. */ + void export_bits (gdb::array_view buf, int endian, bool unsigned_p, + bool safe) const; friend struct gdb_mpq; friend struct gdb_mpf; @@ -590,9 +604,10 @@ gdb_mpz::as_integer () const { T result; - this->safe_export ({(gdb_byte *) &result, sizeof (result)}, + this->export_bits ({(gdb_byte *) &result, sizeof (result)}, 0 /* endian (0 = native) */, - !std::is_signed::value /* unsigned_p */); + !std::is_signed::value /* unsigned_p */, + true /* safe */); return result; }