From nobody Thu Jan 23 05:51:09 2025 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org ARC-Seal: i=1; a=rsa-sha256; t=1737485423; cv=none; d=zohomail.com; s=zohoarc; b=kdM89MXgDGZFgRttqRXJtbo3rJJEMUzzDrO9j2GytiJ31D5ls4oAklryByv36t5RyNuXGL3tXJQsC6MB7rzSO/60tokypn3+Fb5tNMpBC7pdCsWy3Ntg4RYYZqtGcHVUO2TJUrOeMsU8yglvBvwRb+KBjo/amYKYx7sbFKs2jCI= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1737485423; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=YvLXXAKDH9ahcunPLK9b2L9XTGiX+9XTb+WMW9ld36E=; b=LP4jY4w6Px6AsQX1503m4EXRGdx6elfd4eOj67T3MuhCQOIiT7BTABSlUZ8sKOtpC/Y0NBUjCvTEcgvcogACkI8L/pdI1kBPGSvLVqpJNYm71PgV0mNZ+rGBwxz48Cu+h9tK/cbYj+4cACWZE1K9L75P8b8Nly1Hq/5HKXwwHqM= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1737485423485723.4345744887295; Tue, 21 Jan 2025 10:50:23 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1taJJR-0005qu-AS; Tue, 21 Jan 2025 13:49:22 -0500 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 1taJJJ-00055j-Nc for qemu-devel@nongnu.org; Tue, 21 Jan 2025 13:49:13 -0500 Received: from mail-pl1-x62d.google.com ([2607:f8b0:4864:20::62d]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1taJJG-0005No-Dx for qemu-devel@nongnu.org; Tue, 21 Jan 2025 13:49:13 -0500 Received: by mail-pl1-x62d.google.com with SMTP id d9443c01a7336-21649a7bcdcso102557235ad.1 for ; Tue, 21 Jan 2025 10:49:10 -0800 (PST) Received: from grind.. ([191.202.238.10]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-21c2ceba56asm81121865ad.98.2025.01.21.10.49.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 21 Jan 2025 10:49:07 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ventanamicro.com; s=google; t=1737485348; x=1738090148; darn=nongnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=YvLXXAKDH9ahcunPLK9b2L9XTGiX+9XTb+WMW9ld36E=; b=eEiEz7aw4F43SF5blxeSRRixrmMbLpfGf+qbRExr9jvY4AjdicL6Q9DghtQVij9FaP B9WUpQAUVG9eBwy8QjO2IOfGXJvYa7q7BlTaWl+Ew8E/gcFFxsqdWZnFDOic9BjFX5Us hkgcfu/Se/kk+Iw2QCgfUylc22+Dg42SV/OmrcXwf6pJbQiZqYr5WcNaorHwLu3qjSnM xQexYo9yr+Y7j+QExt0GLnkIJy3yLEFOFm+SLxQniIlM2gNRbkJQYJiykcb/i3iI8ZGK YO2ci2O0xEA5hS4Om5+PmkY29PX2Qds7mEjRrJmCh7nZgZYSZOLDhACwMc/n6JNHb9fK oS3A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1737485348; x=1738090148; 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=YvLXXAKDH9ahcunPLK9b2L9XTGiX+9XTb+WMW9ld36E=; b=Zt9XTvdZFfeGS7fke97AM8lcN+IDem3CgAAh/Qifioa1xZ0e4sA4xC2zZ098wrQYDa +RmCLxHpfZVSf4K3qrYi5VlT0LK3Zy8VFGYCMROhi379xusF8esaPiGv+k+pSbGv+TzI QfvfCrK7m0391kecOx187Uo5YWbVXEVo+RzGXbgjZ2+7JnHQckmox9xvQokses20USh/ ClF9ahlX9pbBxzBWcGgbDdElJ6zgSg7qFTjsDTovveZfvLHH+LBQG7UPWk0QsQioMboA LOrsOoCjJ9aINcVeVClaFf1dnLmyMbb1QyiR8M5Lxy2sYb9rwp0wX9cyR3NATLAwIPjX x1Jw== X-Gm-Message-State: AOJu0Yxz2LY7Rr8XqEGlp0WbqHeVUjMbfVjMW1LXOwIx9EZyIsOjHj0l /SbOfkN/eUDhUuy+/yoKgYBInmouNwt+vqFx6LoBYOJOE+drIS7IfH9PPtTMT0pfY0ouQxW3plK Q X-Gm-Gg: ASbGncuu2sEfAABfDupjiN8U6Bk8AHRB6wZyjUuhzNDPnQy7HFzc+E525TpHNKiToZN 5s/b1qMNIDIX65dq1WHf65gCb50x2zI5eEOIkaPuw0MT8LbjaASPZv/3OyL2EOiXjbjaJRViryT QkM1JHRumEvNi+G5Ts6NG7RFpQAAhCZtz2pfpdT/DWkw0zoJ6VFjcbjBjHdAQyzrwoGeijGN+5C YwMnlxfVl2p49lgcb+f14jWI3krrVSexju1RndRwv7a1HrEuQf00BULeDZ71vHzEckddwRk X-Google-Smtp-Source: AGHT+IGemy6A+esvt/sUQ6h7P7vlMAEZgDNh/xiJfGyHmsLHZ1zQ/nDOezuNTESLT+1COu1kWoIy3Q== X-Received: by 2002:a17:903:32c1:b0:21b:d126:6d30 with SMTP id d9443c01a7336-21c355c433amr246020075ad.33.1737485348333; Tue, 21 Jan 2025 10:49:08 -0800 (PST) From: Daniel Henrique Barboza To: qemu-devel@nongnu.org Cc: qemu-riscv@nongnu.org, alistair.francis@wdc.com, bmeng@tinylab.org, liwei1518@gmail.com, zhiwei_liu@linux.alibaba.com, palmer@rivosinc.com, peter.maydell@linaro.org, Daniel Henrique Barboza Subject: [PATCH 5/5] target/riscv/cpu_helper.c: fix bad_shift in riscv_cpu_interrupt() Date: Tue, 21 Jan 2025 15:48:47 -0300 Message-ID: <20250121184847.2109128-6-dbarboza@ventanamicro.com> X-Mailer: git-send-email 2.47.1 In-Reply-To: <20250121184847.2109128-1-dbarboza@ventanamicro.com> References: <20250121184847.2109128-1-dbarboza@ventanamicro.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=2607:f8b0:4864:20::62d; envelope-from=dbarboza@ventanamicro.com; helo=mail-pl1-x62d.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 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+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @ventanamicro.com) X-ZM-MESSAGEID: 1737485424209019000 Content-Type: text/plain; charset="utf-8" Coverity reported a BAD_SHIFT issue in the following code: > 2097 >>>> CID 1590355: Integer handling issues (BAD_SHIFT) >>>> In expression "hdeleg >> cause", right shifting by more than 63 bits has undefined behavior. The shift amount, "cause", is at least= 64. > 2098 vsmode_exc =3D env->virt_enabled && (((hdeleg >> cause) & 1= ) || vs_injected); > 2099 /* It is not clear to me how the tool guarantees that '"cause" is at least 64', but indeed there's no guarantees that it would be < 64 in the 'async =3D true' code path. A simple fix to avoid a potential UB is to add a 'cause < 64' guard like 'mode' is already doing right before 'vsmode_exc'. Resolves: Coverity CID 1590355 Fixes: 967760f62c ("target/riscv: Implement Ssdbltrp exception handling") Signed-off-by: Daniel Henrique Barboza --- target/riscv/cpu_helper.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/target/riscv/cpu_helper.c b/target/riscv/cpu_helper.c index e1dfc4ecbf..64d1d68550 100644 --- a/target/riscv/cpu_helper.c +++ b/target/riscv/cpu_helper.c @@ -2095,7 +2095,9 @@ void riscv_cpu_do_interrupt(CPUState *cs) mode =3D env->priv <=3D PRV_S && cause < 64 && (((deleg >> cause) & 1) || s_injected || vs_injected) ? PRV_S : PR= V_M; =20 - vsmode_exc =3D env->virt_enabled && (((hdeleg >> cause) & 1) || vs_inj= ected); + vsmode_exc =3D env->virt_enabled && cause < 64 && + (((hdeleg >> cause) & 1) || vs_injected); + /* * Check double trap condition only if already in S-mode and targeting * S-mode --=20 2.47.1