From patchwork Fri Sep 6 05:16:05 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Tokarev X-Patchwork-Id: 825905 Delivered-To: patch@linaro.org Received: by 2002:adf:a345:0:b0:367:895a:4699 with SMTP id d5csp639255wrb; Thu, 5 Sep 2024 22:25:55 -0700 (PDT) X-Forwarded-Encrypted: i=2; AJvYcCVUrSVUzldWt1qkfDWX5g651B7KkAfhroeV/tU8icKvCYudcdx5LggPKbdAlf3I17vlbgSypg==@linaro.org X-Google-Smtp-Source: AGHT+IHj44iHSVAxPfEIFvrBguYQCos4vWx5gxOLGFTCIIj3n300zsrF334x1DD9D+oVnrxbvVL1 X-Received: by 2002:a05:6214:3285:b0:6c3:55be:23a4 with SMTP id 6a1803df08f44-6c5282f9259mr16240536d6.1.1725600354919; Thu, 05 Sep 2024 22:25:54 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1725600354; cv=none; d=google.com; s=arc-20240605; b=ipZvVfhvD401GC+e1nBwIqeeXqiKBl6MtPBxTUHL2PRN+cw5mHc0o31zKRETcmrGog S8kHWlISORjMv3n/T5d5a2+7gCYc/MdV6vEsIX9nXAz7PjKSVJTdnFdnnpLBVPob0Rmz 3/8fggmq5qHsXAeR3IW4YiuXnrlKwhshC7Jla+9G3r5/s0z8ya112XzM5GpuCT6WlnnV DKnBpwmatkPdreKRgdmHRSOXDPZMPsEJ0SlD66q5IEDyy69qOuhcY9lQC3tni7p5nzuK jNdFZ4VKTSreJC0GC8b2svLZIbT/uI9d9bmnZ29d8Fidjrqb1Tl/fh/VKjL8NEMehofW N6MQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20240605; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:cc:to :from; bh=z83USwdbJV8YHV8q41qGAN35o8jholCy4Zzhj/HDo9E=; fh=xJ1URYKcMN3TM0/XAv5v+aCN+5tIbzAdcfBx5UNgoLw=; b=B/X3KVtFAPeldxPOjzroJS6XMlLbBEOJxhi9TrJSwPa3hyazLS11s7xI6tkX/TTvdb iofwEkxPFvYrxmI/FNwfRcJRSnVS4OrAQV4zRCktHlbmJc/jsPee4ZVNHYKv9UgccKHh HVTTQOePiEZtbtPJS1KdEmGzxrsKMP/jQWefu4jYCSpzCz5h5ZZD5x2qyvtpd9hozIws tTfFvhpw6dgyf8pjK86IJSZa2i/ZrQGbwiUo9JHZpxpr2uhsADUSX2g9psbJP00wFkj7 VpZ+KfXykpArf2xmpTNi7yr2N6pM41SHlVPFlxkLB9T4b/e6Mwo5vRwV3JOWjOEx38FL saMA==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org" Return-Path: Received: from lists.gnu.org (lists.gnu.org. [209.51.188.17]) by mx.google.com with ESMTPS id 6a1803df08f44-6c520525c93si36561836d6.598.2024.09.05.22.25.54 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Thu, 05 Sep 2024 22:25:54 -0700 (PDT) Received-SPF: pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; Authentication-Results: mx.google.com; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org" Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1smRM2-0006mB-7w; Fri, 06 Sep 2024 01:17:54 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1smRLw-00063b-6l; Fri, 06 Sep 2024 01:17:48 -0400 Received: from isrv.corpit.ru ([86.62.121.231]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1smRLu-000830-HE; Fri, 06 Sep 2024 01:17:47 -0400 Received: from tsrv.corpit.ru (tsrv.tls.msk.ru [192.168.177.2]) by isrv.corpit.ru (Postfix) with ESMTP id 492828C125; Fri, 6 Sep 2024 08:15:17 +0300 (MSK) Received: from tls.msk.ru (mjt.wg.tls.msk.ru [192.168.177.130]) by tsrv.corpit.ru (Postfix) with SMTP id ED25C13336B; Fri, 6 Sep 2024 08:16:34 +0300 (MSK) Received: (nullmailer pid 10436 invoked by uid 1000); Fri, 06 Sep 2024 05:16:33 -0000 From: Michael Tokarev To: qemu-devel@nongnu.org Cc: qemu-stable@nongnu.org, Peter Maydell , Richard Henderson , Michael Tokarev Subject: [Stable-7.2.14 17/40] target/arm: Avoid shifts by -1 in tszimm_shr() and tszimm_shl() Date: Fri, 6 Sep 2024 08:16:05 +0300 Message-Id: <20240906051633.10288-17-mjt@tls.msk.ru> X-Mailer: git-send-email 2.39.2 In-Reply-To: References: MIME-Version: 1.0 Received-SPF: pass client-ip=86.62.121.231; envelope-from=mjt@tls.msk.ru; helo=isrv.corpit.ru X-Spam_score_int: -68 X-Spam_score: -6.9 X-Spam_bar: ------ X-Spam_report: (-6.9 / 5.0 requ) BAYES_00=-1.9, RCVD_IN_DNSWL_HI=-5, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+patch=linaro.org@nongnu.org Sender: qemu-devel-bounces+patch=linaro.org@nongnu.org From: Peter Maydell The function tszimm_esz() returns a shift amount, or possibly -1 in certain cases that correspond to unallocated encodings in the instruction set. We catch these later in the trans_ functions (generally with an "a-esz < 0" check), but before we do the decodetree-generated code will also call tszimm_shr() or tszimm_sl(), which will use the tszimm_esz() return value as a shift count without checking that it is not negative, which is undefined behaviour. Avoid the UB by checking the return value in tszimm_shr() and tszimm_shl(). Cc: qemu-stable@nongnu.org Resolves: Coverity CID 1547617, 1547694 Signed-off-by: Peter Maydell Reviewed-by: Richard Henderson Message-id: 20240722172957.1041231-4-peter.maydell@linaro.org (cherry picked from commit 76916dfa89e8900639c1055c07a295c06628a0bc) Signed-off-by: Michael Tokarev diff --git a/target/arm/translate-sve.c b/target/arm/translate-sve.c index 7388e1dbc7..034e816491 100644 --- a/target/arm/translate-sve.c +++ b/target/arm/translate-sve.c @@ -61,13 +61,27 @@ static int tszimm_esz(DisasContext *s, int x) static int tszimm_shr(DisasContext *s, int x) { - return (16 << tszimm_esz(s, x)) - x; + /* + * We won't use the tszimm_shr() value if tszimm_esz() returns -1 (the + * trans function will check for esz < 0), so we can return any + * value we like from here in that case as long as we avoid UB. + */ + int esz = tszimm_esz(s, x); + if (esz < 0) { + return esz; + } + return (16 << esz) - x; } /* See e.g. LSL (immediate, predicated). */ static int tszimm_shl(DisasContext *s, int x) { - return x - (8 << tszimm_esz(s, x)); + /* As with tszimm_shr(), value will be unused if esz < 0 */ + int esz = tszimm_esz(s, x); + if (esz < 0) { + return esz; + } + return x - (8 << esz); } /* The SH bit is in bit 8. Extract the low 8 and shift. */