From patchwork Tue Jan 21 18:48:43 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Daniel Henrique Barboza X-Patchwork-Id: 13946486 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id E1D51C02182 for ; Tue, 21 Jan 2025 18:49:12 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1taJJB-0004K4-NU; Tue, 21 Jan 2025 13:49:05 -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 1taJJ8-0004De-DZ for qemu-devel@nongnu.org; Tue, 21 Jan 2025 13:49:02 -0500 Received: from mail-pl1-x62e.google.com ([2607:f8b0:4864:20::62e]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1taJJ6-0005Ku-Eb for qemu-devel@nongnu.org; Tue, 21 Jan 2025 13:49:02 -0500 Received: by mail-pl1-x62e.google.com with SMTP id d9443c01a7336-2156e078563so79622775ad.2 for ; Tue, 21 Jan 2025 10:48:59 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ventanamicro.com; s=google; t=1737485337; x=1738090137; 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=jwNZlRoqWyY0SU1aH3L8QCTZcSYByfezJsd2zpb077c=; b=NkmqG7FytJyuq5TqE22RQpo7umxkpjAluo4Xlx5rvMaVaFARAKNxLyYxN80o8gobsO HojZLV5/aDOZp2tUj1maUHZ/UXhXjPJsrGZznMxv7DYG8UFVQ+OpAu5Txa9UF19RvBaY LGo0NEcYt+wJktfBk/pBpw9xEecW8a4e8CwJaM7i4t9WciVoR33DRAfjqNS3Ne8vL6pN HAhSAKP8XlH0Tp8f4i++PEogSMPiqlcmv/19bXPM55Os6AWaTBAfOTUSGGFSuhvTfAae k64l/XDQPv1vdLw634zbLZAvUYqM3BrkykqL1v21NTIvqSquzGIlQRzZJN7KTyJ4n4gw 7Ftw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1737485337; x=1738090137; 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=jwNZlRoqWyY0SU1aH3L8QCTZcSYByfezJsd2zpb077c=; b=p2/4/X4M88g49GrcPISO/5asJGrQ5z2+UYOz7WXH69XzntcEQPNn357Q/Uh1PCkZu1 x1CnCQsts4cO4Nkmb6HgP+cSb+f7+RM0sEbY19Pzk9MGP5heFIuFZKaTDlUD9hAvjyot LZVPTIr14PCap3futtsEQ5T0kv7QWhXL0YSlsKhSFlLGkYN8RaoxzEmzbBMiajLT4duN 67mDAAF9TUxsSS5XvkLw+WuRHp5ygljk5bBDhbQSl02OkeQEl5xiAPi7l4gT7a+1oV0+ Sp2SMEyYP9V1WFFVy3Cj0TL/s/v8MQbQqY0/ab2z5NAVgqyL1S6RfDv/PKZwSeQ+D2Cx 6uOg== X-Gm-Message-State: AOJu0Yy/BaOayu6QhgzSZb4oD8Yp+OpAoCtou+tzt2XuAalPiwhSNuZn YBg15iPhvMTvTBuFFs+2DXlo0ZYeTv983Y6ZuNgAtrowsoZryFeWfYqvDOGEB+6j5y6vpklQMf3 I X-Gm-Gg: ASbGncu+lVKjNDY/gz9+LgjG+q/yH5S7kmI6A7Oefa0C9bzGDsUFozpjJYYju/9QuNy TMIlqdCQTaxVoMgrDmanPvgGw7uCxTrvg/ZgYQ9PUI1zsxvYIg6dCZqgS5JPHk3c39wWabLWG5X bHPiLitgDv0BquPqmA1N7rYdsv3EksMwGOllcq7UdcPQ9107aeZX7qSpTw0QFJwuJCTN9NIeDDt OBLswK/PlEyIoZ2CSgraWVJXQTWh1NnPXIzD8U0btMDa89UHYIw4xj59kZsDLfSint3eTF1 X-Google-Smtp-Source: AGHT+IES+x/bOnHhe+f7tnLZ4Vm12kVcBrWDBsxY62avdwz7s7sHRLU2wB1OQTetrXulnmxObeO32A== X-Received: by 2002:a17:902:d511:b0:215:75ca:6a0 with SMTP id d9443c01a7336-21c3555b09emr318630425ad.29.1737485335959; Tue, 21 Jan 2025 10:48:55 -0800 (PST) Received: from grind.. ([191.202.238.10]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-21c2ceba56asm81121865ad.98.2025.01.21.10.48.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 21 Jan 2025 10:48:55 -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 1/5] target/riscv/csr.c: fix deadcode in rmw_xireg() Date: Tue, 21 Jan 2025 15:48:43 -0300 Message-ID: <20250121184847.2109128-2-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 Received-SPF: pass client-ip=2607:f8b0:4864:20::62e; envelope-from=dbarboza@ventanamicro.com; helo=mail-pl1-x62e.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+qemu-devel=archiver.kernel.org@nongnu.org Sender: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Coverity found a DEADCODE issue in rmw_xireg() claiming that we can't reach 'RISCV_EXCP_VIRT_INSTRUCTION_FAULT' at the 'done' label: done: if (ret) { return (env->virt_enabled && virt) ? RISCV_EXCP_VIRT_INSTRUCTION_FAULT : RISCV_EXCP_ILLEGAL_INST; } return RISCV_EXCP_NONE; This happens because the 'virt' flag, which is only used by 'done', is set to 'false' and it will always remain 'false' in any condition where we'll jump to 'done': switch (csrno) { (...) case CSR_VSIREG: isel = env->vsiselect; virt = true; break; default: goto done; }; 'virt = true' will never reach 'done' because we have a if/else-if/else block right before the label that will always return: if (xiselect_aia_range(isel)) { return ... } else if (...) { return ... } else { return RISCV_EXCP_ILLEGAL_INST; } All this means that we can preserve the current logic by reducing the 'done' label to: done: if (ret) { return RISCV_EXCP_ILLEGAL_INST; } return RISCV_EXCP_NONE; The flag 'virt' is now unused. Remove it. Fix the 'goto done' identation while we're at it. Resolves: Coverity CID 1590359 Fixes: dc0280723d ("target/riscv: Decouple AIA processing from xiselect and xireg") Signed-off-by: Daniel Henrique Barboza --- target/riscv/csr.c | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/target/riscv/csr.c b/target/riscv/csr.c index afb7544f07..ab209d0cda 100644 --- a/target/riscv/csr.c +++ b/target/riscv/csr.c @@ -2658,7 +2658,6 @@ static RISCVException rmw_xireg(CPURISCVState *env, int csrno, target_ulong *val, target_ulong new_val, target_ulong wr_mask) { - bool virt = false; int ret = -EINVAL; target_ulong isel; @@ -2680,10 +2679,9 @@ static RISCVException rmw_xireg(CPURISCVState *env, int csrno, break; case CSR_VSIREG: isel = env->vsiselect; - virt = true; break; default: - goto done; + goto done; }; /* @@ -2705,8 +2703,7 @@ static RISCVException rmw_xireg(CPURISCVState *env, int csrno, done: if (ret) { - return (env->virt_enabled && virt) ? - RISCV_EXCP_VIRT_INSTRUCTION_FAULT : RISCV_EXCP_ILLEGAL_INST; + return RISCV_EXCP_ILLEGAL_INST; } return RISCV_EXCP_NONE; } From patchwork Tue Jan 21 18:48:44 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Daniel Henrique Barboza X-Patchwork-Id: 13946487 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 4A0F2C0218C for ; Tue, 21 Jan 2025 18:49:24 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1taJJC-0004Nl-Px; Tue, 21 Jan 2025 13:49:06 -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 1taJJA-0004J1-Cj for qemu-devel@nongnu.org; Tue, 21 Jan 2025 13:49:05 -0500 Received: from mail-pl1-x62b.google.com ([2607:f8b0:4864:20::62b]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1taJJ8-0005Lf-Da for qemu-devel@nongnu.org; Tue, 21 Jan 2025 13:49:03 -0500 Received: by mail-pl1-x62b.google.com with SMTP id d9443c01a7336-21a7ed0155cso97277105ad.3 for ; Tue, 21 Jan 2025 10:49:01 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ventanamicro.com; s=google; t=1737485340; x=1738090140; 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=z7p6XN2M1MHDl+uW3Q8gV1olbYzbSDmoR29h4KhBMQc=; b=Ik+7h1TjTvfHqJrzrQmdPA/q7PFJzbPaw7Eql9kxBaYXRZC21JW9QDSfHJblvosq2X z+kJxtNhqIR2jNuisuWhC/xdLWy3jh277Rh6JkwFA8ATDbqWPYeTuJaU+aFvrhZnDh+0 CJoQREkdsGPrnWOBGtot9BgIn4AfxldwRRiZ9WF5eTxtEGqjot/qkAwKm8FzQLuZd5Lc jsScc+7WKMzRLSRG/Fmdy9EdxlHWeteZfpqSF+mIaGpxtyCyk3AqAT+/5osR/po09qRs O16L9Jw5aDkxnbUBR4gzcf1f8mmnNc1if/Ev7Y44e2nWubhLUDO6S9nlzEiXatTwrTfj PXkw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1737485340; x=1738090140; 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=z7p6XN2M1MHDl+uW3Q8gV1olbYzbSDmoR29h4KhBMQc=; b=GG+i/CqIUAzYVUQKQc2IBx3PjG80jjS07kFnE/hWgD6EhIh9/Bjq9HS7spDPSRgHOq kR/geR/NqmJuqAEdDaGFb8LVOkv2uCROmlFu8fKF/0VxIYOyYzPJD0DZJl5ibHrVMMjz QFKnN0LFiXQ9eB6IRI2Hy4VGpztZL9MmFNuDIMwyxZ1V+7tDAVg2FzSOBcxIBUpdu2Tb N4yyw8RkOJIB3hd4cIUmZJ91dwSDcMargW/MnJ+GKFB4bPW6lx9MD8WCpi+EARkT/kby ElRkZGR54AyMaPeydTeI4SX4o5socXg0j2EQt/tDVr/3dWHVPn0hNvXd80yfElseygxc kpDw== X-Gm-Message-State: AOJu0YyTBcNqxtBcfSAPVqJY7GSkCxGMT+kpONBUnJnLT7inPKiOE8bO doH6YACC2AgDdc9VFUvT7xASPTHPdbaDgNke4m4V58VafD2F+xFb9TKjzYWw42D2bbdhLPrU3lx 1 X-Gm-Gg: ASbGncug48RRacjq2qa1uok9haRubBN2gX+dKr3HkrmK4RSdS7/rK0oYN2KH9J2enxU SGFeV3GyoQFad+qDX0kcQAbkQccZzhfaS1Oyb2AiFucEwXhXTThuf3SKeTmxfLLOhD8psAAdGDB XYUusLWyjUf8jQtdzwyWI4ei2eaYIgASlCGeDY1MmNtOWzSpKDnXkhm4HDWn37Kf8GGG1JObsSL 1W2iT12+X0Pf/kex+WBG4GghPrZxZwf8gG9XMPkDj/bcX0k9GfZ+nP4OR+jlGzrnUx7wFr9 X-Google-Smtp-Source: AGHT+IGjZINNCRqYFGKCPRTPV2ftktRkwXOddQC1o0pW7jBlTkWZL76wtIGdjPotYqX8nqo/7itOSg== X-Received: by 2002:a17:902:da87:b0:216:501e:e314 with SMTP id d9443c01a7336-21c3540d181mr266638255ad.20.1737485339030; Tue, 21 Jan 2025 10:48:59 -0800 (PST) Received: from grind.. ([191.202.238.10]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-21c2ceba56asm81121865ad.98.2025.01.21.10.48.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 21 Jan 2025 10:48:58 -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 2/5] target/riscv/csr.c: fix 'ret' deadcode in rmw_xireg() Date: Tue, 21 Jan 2025 15:48:44 -0300 Message-ID: <20250121184847.2109128-3-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 Received-SPF: pass client-ip=2607:f8b0:4864:20::62b; envelope-from=dbarboza@ventanamicro.com; helo=mail-pl1-x62b.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=unavailable 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+qemu-devel=archiver.kernel.org@nongnu.org Sender: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Coverity found a second DEADCODE issue in rmw_xireg() claiming that we can't reach 'RISCV_EXCP_NONE' at the 'done' label: > 2706 done: > 2707 if (ret) { > 2708 return (env->virt_enabled && virt) ? > 2709 RISCV_EXCP_VIRT_INSTRUCTION_FAULT : RISCV_EXCP_ILLEGAL_INST; > 2710 } >>>> CID 1590356: Control flow issues (DEADCODE) >>>> Execution cannot reach this statement: "return RISCV_EXCP_NONE;". > 2711 return RISCV_EXCP_NONE; Our label is now reduced after fixing another deadcode in the previous patch but the problem reported here still remains: done: if (ret) { return RISCV_EXCP_ILLEGAL_INST; } return RISCV_EXCP_NONE; This happens because 'ret' changes only once at the start of the function: ret = smstateen_acc_ok(env, 0, SMSTATEEN0_SVSLCT); if (ret != RISCV_EXCP_NONE) { return ret; } So it's a guarantee that ret will be RISCV_EXCP_NONE (-1) if we ever reach the label, i.e. "if (ret)" will always be true, and the label can be even further reduced to: done: return RISCV_EXCP_ILLEGAL_INST; To make a better use of the label, remove the 'else' from the xiselect_aia_range() chain and let it fall-through to the 'done' label since they are now both returning RISCV_EXCP_ILLEGAL_INST. Resolves: Coverity CID 1590356 Fixes: dc0280723d ("target/riscv: Decouple AIA processing from xiselect and xireg") Signed-off-by: Daniel Henrique Barboza --- target/riscv/csr.c | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/target/riscv/csr.c b/target/riscv/csr.c index ab209d0cda..0e83c3b045 100644 --- a/target/riscv/csr.c +++ b/target/riscv/csr.c @@ -2697,15 +2697,10 @@ static RISCVException rmw_xireg(CPURISCVState *env, int csrno, } else if (riscv_cpu_cfg(env)->ext_smcsrind || riscv_cpu_cfg(env)->ext_sscsrind) { return rmw_xireg_csrind(env, csrno, isel, val, new_val, wr_mask); - } else { - return RISCV_EXCP_ILLEGAL_INST; } done: - if (ret) { - return RISCV_EXCP_ILLEGAL_INST; - } - return RISCV_EXCP_NONE; + return RISCV_EXCP_ILLEGAL_INST; } static RISCVException rmw_xtopei(CPURISCVState *env, int csrno, From patchwork Tue Jan 21 18:48:45 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Daniel Henrique Barboza X-Patchwork-Id: 13946488 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id F11EBC0218B for ; Tue, 21 Jan 2025 18:49:26 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1taJJH-0004ky-Iy; Tue, 21 Jan 2025 13:49:11 -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 1taJJD-0004Tu-Ek for qemu-devel@nongnu.org; Tue, 21 Jan 2025 13:49:07 -0500 Received: from mail-pl1-x634.google.com ([2607:f8b0:4864:20::634]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1taJJA-0005MX-LS for qemu-devel@nongnu.org; Tue, 21 Jan 2025 13:49:07 -0500 Received: by mail-pl1-x634.google.com with SMTP id d9443c01a7336-2163b0c09afso109645405ad.0 for ; Tue, 21 Jan 2025 10:49:04 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ventanamicro.com; s=google; t=1737485343; x=1738090143; 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=Rvu1CgqxV7eAphnSN54+tDhlM3TA+qR/gcxjkFnEWjw=; b=LawOKZ8bYvT3S9S4oniFLOjamUu9VCrSOAdyl7RNzeR4BWeWqbnoNNLx+4kJSagZIW mK7A+9Sk6wkkkAGZ95SXomlC4X9CDmqIyFuwzc6q2jwfNVvownhiqCpLJqs890b0g4P6 AonuO10IpvE+A7v+lcQufQeEmtD4WGIabUoO61vxHTvIoay3BL2iXmdDBOtQkj5YFqea H1F/DHmmhd3jBkhaDIq7KqwqYbrGX/UbW9sS7K8MYR7QwB+t1YTiEVptIktgvRpOhdtn Rx3N+qyeuS7W/EjYwhNp8/vj49HUhQ2eprhJLhKrlEtn4vhgKd83OkcYk/MNAv2k9sHZ At/A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1737485343; x=1738090143; 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=Rvu1CgqxV7eAphnSN54+tDhlM3TA+qR/gcxjkFnEWjw=; b=uBdCRFVK/MEer2of4HNlx/t39M83OAWn2a+fnXkCQ2Ufw+VsApZS8sVyjd32naFNGQ WDoyBXmDkC/kVjNj5HWJr/aorBMDWEK5pUmN1m/VA7n/CQRn0OwyKeIkQQqFnNRlC6QQ x3VpO7YnoS82EzlxGb+yXg4BYkfONZBrJA3QMHVhHJUsKlAWLOWR1i6ftKyFhu96SSZC SPto7CJQRRYW+63PoRiL5BV+R3WcfebXrz9itR3flAK3PVtG7CSsUpusMgMY2GbdOta7 m9hT0EUZRlzkSJlg8hl57j9qlAFzaojmDqP45g/DcJWC3bocwoQyCEpFLqzvC0EblsHH q5dw== X-Gm-Message-State: AOJu0YyotD1xsdHIBrIbH4PF3/sZI6HzUzRml5TEvNbhLFK0uI1+oh5M XCiBI1YJ8ArgVNiKgX+ya0zEdYXYwt+3Sgi60DG9RDh8bHoJwyhdF/OfyVVfxcNtQjxipINB1pc w X-Gm-Gg: ASbGncsvWmPw5xGBSLSWjAejhDZKxbxdR4Jkz0adyZIFxmRI+ZCR2A9UD11SxbEtQLN x8a23/SH8EsJDL0V1yBZUskFc/xFCORJCviMRtrBcBLflswQ4lBb8DRC/0SR4v7bFwb9okiW5fu Om0k5DqenjAWiWahn+TdcIxf93nl03P/MdlN76v9Y1mdsZq1ylMRroI6gL/Tdy129aWxkAv74Qc qSJcW/+ClHdRYXwnBvOtTDfoOFb7FqoAlMRUwKeuA9tav/tK3hZYeJvXsN/duAJeMbd17t6 X-Google-Smtp-Source: AGHT+IESP4i3tbzPGF4yXpQslSMUUd/AhTFYAxFzW9c0AJWvrEVtRb0L/dJKlBg3b8stpphikzKpZw== X-Received: by 2002:a17:902:d501:b0:20c:9821:6998 with SMTP id d9443c01a7336-21c352c798emr324610145ad.10.1737485342075; Tue, 21 Jan 2025 10:49:02 -0800 (PST) Received: from grind.. ([191.202.238.10]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-21c2ceba56asm81121865ad.98.2025.01.21.10.48.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 21 Jan 2025 10:49:01 -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 3/5] target/riscv/csr.c: fix deadcode in rmw_xiregi() Date: Tue, 21 Jan 2025 15:48:45 -0300 Message-ID: <20250121184847.2109128-4-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 Received-SPF: pass client-ip=2607:f8b0:4864:20::634; envelope-from=dbarboza@ventanamicro.com; helo=mail-pl1-x634.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=unavailable 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+qemu-devel=archiver.kernel.org@nongnu.org Sender: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Coverity found a DEADCODE issue in rmw_xiregi() claiming that we can't reach 'RISCV_EXCP_VIRT_INSTRUCTION_FAULT' at the 'done' label: > 2652 done: >>>> CID 1590357: Control flow issues (DEADCODE) >>>> Execution cannot reach the expression "RISCV_EXCP_VIRT_INSTRUCTION_FAULT" inside this statement: "return (env->virt_enabled &...". > 2653 return (env->virt_enabled && virt) ? > 2654 RISCV_EXCP_VIRT_INSTRUCTION_FAULT : RISCV_EXCP_ILLEGAL_INST; This happens because 'virt' is being set to 'false' and it will remain as 'false' in any code path where 'done' will be called. The label can be safely reduced to: done: return RISCV_EXCP_ILLEGAL_INST; And that will leave us with the following usage of a 'goto' skipping a single 'return' to do another single 'return': } else { goto done; } return rmw_xireg_csrind(env, csrno, isel, val, new_val, wr_mask); done: return RISCV_EXCP_ILLEGAL_INST; Which we will eliminate it and just do 'return RISCV_EXCP_ILLEGAL_INST' instead. Resolves: Coverity CID 1590357 Fixes: 5e33a20827 ("target/riscv: Support generic CSR indirect access") Signed-off-by: Daniel Henrique Barboza --- target/riscv/csr.c | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/target/riscv/csr.c b/target/riscv/csr.c index 0e83c3b045..75f21ccabb 100644 --- a/target/riscv/csr.c +++ b/target/riscv/csr.c @@ -2621,7 +2621,6 @@ static int rmw_xireg_csrind(CPURISCVState *env, int csrno, static int rmw_xiregi(CPURISCVState *env, int csrno, target_ulong *val, target_ulong new_val, target_ulong wr_mask) { - bool virt = false; int ret = -EINVAL; target_ulong isel; @@ -2642,16 +2641,11 @@ static int rmw_xiregi(CPURISCVState *env, int csrno, target_ulong *val, } else if (CSR_VSIREG <= csrno && csrno <= CSR_VSIREG6 && csrno != CSR_VSIREG4 - 1) { isel = env->vsiselect; - virt = true; } else { - goto done; + return RISCV_EXCP_ILLEGAL_INST; } return rmw_xireg_csrind(env, csrno, isel, val, new_val, wr_mask); - -done: - return (env->virt_enabled && virt) ? - RISCV_EXCP_VIRT_INSTRUCTION_FAULT : RISCV_EXCP_ILLEGAL_INST; } static RISCVException rmw_xireg(CPURISCVState *env, int csrno, From patchwork Tue Jan 21 18:48:46 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Daniel Henrique Barboza X-Patchwork-Id: 13946494 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id E1EB0C0218B for ; Tue, 21 Jan 2025 18:52:34 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1taJJP-0005Zb-5l; Tue, 21 Jan 2025 13:49:19 -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 1taJJH-0004nm-CZ for qemu-devel@nongnu.org; Tue, 21 Jan 2025 13:49:12 -0500 Received: from mail-pl1-x62a.google.com ([2607:f8b0:4864:20::62a]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1taJJF-0005NC-1E for qemu-devel@nongnu.org; Tue, 21 Jan 2025 13:49:10 -0500 Received: by mail-pl1-x62a.google.com with SMTP id d9443c01a7336-216426b0865so103696615ad.0 for ; Tue, 21 Jan 2025 10:49:06 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ventanamicro.com; s=google; t=1737485345; x=1738090145; 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=aFnox9paCwopK+iw70x8l4RBRO8z4SdTp83xgjmiXGE=; b=P39n/vObDA5bl+HO+C8vdgx5MYNbfVNJNrVOeaYzR2F9DjlP+4XqrvIhHYKxDu/OtA nYLJ/wpIHmk+QgBbZ35hn4C75XrkDM8q6zAQDPMFeWpywodEr1Zf4HGBKcBq4dsfTfSZ lrJqh2k/UTnHgOv+0Wb55kfQoqAe/iSg+pS07+kMv3AIx/6wsE8OlLEjDCyXb6/kM36p IvKa/ATZkkhkiuGq5yLtLJIuuxnR5egYLApSOjczP9gLcDL3RUvsqHXVQ2g1tAkw380G gCWzQCOc1hEJ3aj79a35z4L4fO2oxf0cebXYacAI+sNUNbbXxfKDHvSQmyAZ4ombmjJf vWOw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1737485345; x=1738090145; 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=aFnox9paCwopK+iw70x8l4RBRO8z4SdTp83xgjmiXGE=; b=RUlpZYGQ/hzw3eqEPswo4WE3IquNCWbh0PFWwyqwGZdMc/HA+v2XBVkP21+trEC2jE 7dCEfP/c7NX5wUITT58k2gGXyR90nOmiM13RIDm6jiHDxhaJs2M6UT6IA3JshFFXVvfL a2DqK4wEBaiDCKtsL8mNrQBO4vcZzZwPzqOjWIFwkeyu/jKf6MTJ1Jil0nfIZ+Q59t2D Ucoz4W63jPdw4n2UQDgppvBJL4q2JKlxY9LtsxrhcQ+hVRTsVreWkI1n3ixA12GppDc9 TJZkWC/zOXHHDkkQobTbeQBeMrXmqQ6f61hH5/nmJSgksvV1jRicDoOobKOMGJ3NuFK0 qxdA== X-Gm-Message-State: AOJu0YwLQ3yzLmmoo9OmQ0BgrqbNR1eHL2ldUAJOVjPfh/IhLUXXrKro q7Gy7ROWPfbtnFsv/n+Mk5ZlleefbQBlm4TsPjFmihPH34ZZH5D1oOF+9+DzaZPjPVyNKeaTNcv d X-Gm-Gg: ASbGncs7r2CgLH4Ld7ZJvqz1td0c2S1HcX3Me5s7NL12qZtW9lgjA+lPzYlTiAuvHpP 5ndbK6L3T51ZfjrKgzT07azUUfxMxAR8zcmQP590oyvJyqCdmwPFSs8C/onx6y6reBEK+vNdmLq wB9YkboB3J6mJN/X5D9VCR7A5uDQNY9qHy6rGXAyeSncgYeiAIhd751C67ON7g9ZLSCfBv30qrv ebcS305ND5LInpUuEQ8lqBlkmPiXE2NZwuGKcrwKvF23CiCoQxUZEm26UuW9zPlpTkTjbsQ X-Google-Smtp-Source: AGHT+IHbpsnBVrUf1PnyjrEd5diSaZNG27segl+kTh6f+rQJ0fRpX3TLg88OcpnqyW8y0MJkhlRmRQ== X-Received: by 2002:a17:902:ea10:b0:21a:8716:faab with SMTP id d9443c01a7336-21c352ec1f0mr259659385ad.16.1737485345158; Tue, 21 Jan 2025 10:49:05 -0800 (PST) Received: from grind.. ([191.202.238.10]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-21c2ceba56asm81121865ad.98.2025.01.21.10.49.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 21 Jan 2025 10:49:04 -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 4/5] target/riscv/csr.c: fix deadcode in aia_smode32() Date: Tue, 21 Jan 2025 15:48:46 -0300 Message-ID: <20250121184847.2109128-5-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 Received-SPF: pass client-ip=2607:f8b0:4864:20::62a; envelope-from=dbarboza@ventanamicro.com; helo=mail-pl1-x62a.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=unavailable 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+qemu-devel=archiver.kernel.org@nongnu.org Sender: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Coverity reported a DEADCODE ticket in this function, as follows: >>>> CID 1590358: Control flow issues (DEADCODE) >>>> Execution cannot reach this statement: "return ret;". > 380 return ret; > 381 } The cause is that the 'if (ret != RISCV_EXCP_NONE)' conditional is duplicated: ret = smstateen_acc_ok(env, 0, SMSTATEEN0_AIA); if (ret != RISCV_EXCP_NONE) { return ret; } if (ret != RISCV_EXCP_NONE) { return ret; } Remove the duplication to fix the deadcode. Resolves: Coverity CID 1590358 Fixes: dbcb6e1ccf ("target/riscv: Enable S*stateen bits for AIA") Signed-off-by: Daniel Henrique Barboza --- target/riscv/csr.c | 4 ---- 1 file changed, 4 deletions(-) diff --git a/target/riscv/csr.c b/target/riscv/csr.c index 75f21ccabb..dc0a88a0f0 100644 --- a/target/riscv/csr.c +++ b/target/riscv/csr.c @@ -376,10 +376,6 @@ static RISCVException aia_smode32(CPURISCVState *env, int csrno) return ret; } - if (ret != RISCV_EXCP_NONE) { - return ret; - } - return smode32(env, csrno); } From patchwork Tue Jan 21 18:48:47 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Daniel Henrique Barboza X-Patchwork-Id: 13946489 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 9626BC02182 for ; Tue, 21 Jan 2025 18:50:22 +0000 (UTC) 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) 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) 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) 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 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+qemu-devel=archiver.kernel.org@nongnu.org Sender: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org 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 = 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 = 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 = env->priv <= PRV_S && cause < 64 && (((deleg >> cause) & 1) || s_injected || vs_injected) ? PRV_S : PRV_M; - vsmode_exc = env->virt_enabled && (((hdeleg >> cause) & 1) || vs_injected); + vsmode_exc = env->virt_enabled && cause < 64 && + (((hdeleg >> cause) & 1) || vs_injected); + /* * Check double trap condition only if already in S-mode and targeting * S-mode