From nobody Sun Nov 24 15:08:22 2024 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; dmarc=pass(p=none dis=none) header.from=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1722925627; cv=none; d=zohomail.com; s=zohoarc; b=nb/Kb0s8F0mhZAFj+mIPWqphqQVmb5dsw5qXy+mUk5VYHYE0+H5dO38nSoUcVeQrJ0GFQp8Gjg70ygii0x/mpqhSnyCA1ZI33w2156BrY/gxf8wBbNu1MgbhDapVohGy/2aVMwnwia8mzslgMKCqqc/8qpyXtdikItM4bt6EmL8= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1722925627; 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=rIl02MTGpKMjmvTqt9QTISYxUzrPTliJWcVLJpIrITo=; b=WWXhRAlftM+0XHiuXiouoXMBXn4I/xpBVsaZbiqSBnyE2CF9zxVN6Wb9ph5sOdmRCgDPTVybsEBcR7tMDZxa4kAE6VacPNJJx/ApUasOxqRGmcXBs9jueRvQKxqEgyb3MgiebnsQ3QoJ87vCIvyjUkZVoNZMifjUP4UsaBUIUrk= 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; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1722925627216274.1490450084365; Mon, 5 Aug 2024 23:27:07 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1sbDe9-0003Ud-Dg; Tue, 06 Aug 2024 02:26:13 -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 1sbDe8-0003QA-0J for qemu-devel@nongnu.org; Tue, 06 Aug 2024 02:26:12 -0400 Received: from mail-pl1-x633.google.com ([2607:f8b0:4864:20::633]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1sbDe5-0003yx-S6 for qemu-devel@nongnu.org; Tue, 06 Aug 2024 02:26:11 -0400 Received: by mail-pl1-x633.google.com with SMTP id d9443c01a7336-1fd69e44596so3678765ad.1 for ; Mon, 05 Aug 2024 23:26:09 -0700 (PDT) Received: from toolbox.alistair23.me (2403-580b-97e8-0-82ce-f179-8a79-69f4.ip6.aussiebb.net. [2403:580b:97e8:0:82ce:f179:8a79:69f4]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-1ff5927f161sm79464975ad.232.2024.08.05.23.26.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 05 Aug 2024 23:26:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1722925568; x=1723530368; 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=rIl02MTGpKMjmvTqt9QTISYxUzrPTliJWcVLJpIrITo=; b=Tm0mLU3f8U7J3Sr21hd4InKIXhTc9Dh1GI4LDjdHd/Aj4J7KVhhB24eZQ8/23nfnS0 /Tt7blz1DSaIGVYzO51tQ+ZKlxP90N+s0XRGoHNktm61Tn0jdHI2N6k9S4dT7lYUOyrP 4V5H39+Hj/VCvjtf31dGMEPjoXD0Xg1EjpQLIs4bMQlKiM0bTos/H5njUx6VSEGXrSMe GN1IN2S/5r/jKupkIu7mOsNXDBlvNXTzheXiQxf0Pfxm/KbwnpIBbhCfBz6cOsRV274n 6XpkK6oV9eGPxDbnP1CqFHcHT8NBT5XRguCH9fb05PWg6xxJJCOfmaURrDsM7e+3mDu9 lrig== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722925568; x=1723530368; 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=rIl02MTGpKMjmvTqt9QTISYxUzrPTliJWcVLJpIrITo=; b=QDm4zEhu0RhDMC5gf5wZMfRmSpNd021Gio3VicZ9xNS+56FL5TjJR2iGhKD9zd+/ww A+mlwk1XqXN2zTluIQ/SRMh6bZX8bW6hMFHAAMI6a9YFU1OGVDe8IsaeqSnnV0drVUeb F6PMDJMIY9qB8BmTOHM60ouVMsP1v6yXpLj5LXfQXasgTbn4fiMxG6vZX0BTIXmb5Ob8 BoLpzGzmHpLILT1IvmSg0x4OFGCK6SGwXsXbtP0fBmP7k2W1jNikkKmERgwDzCAcEwp9 LXfUfvTj/Q1mhkI1MR6Gx8IkZLRc7MpN+wFSAKPHzYp4O3hV4a59ANt0RPZcBO9DBnhV 1SZw== X-Gm-Message-State: AOJu0YyQgMy5uCrIl9T1wkyK4yo7Qkgl/YV+hacfvvMq3rOUYHJdxDS2 WXVjfIEoMAGCsE66OA61CeeT7MqdYnmuhLqLPeRwTEfT7TNxGbgv+yMQt1Pj X-Google-Smtp-Source: AGHT+IF2StyMO+TE6pXy0EkMoCl3aVVqXH7bU7UWvt/7od5BX1lNAvKJ9ZJYokYxPGiegAJObMjr7Q== X-Received: by 2002:a17:902:dac3:b0:1fa:2b11:657d with SMTP id d9443c01a7336-1ff57b827c9mr222497785ad.10.1722925567929; Mon, 05 Aug 2024 23:26:07 -0700 (PDT) From: Alistair Francis X-Google-Original-From: Alistair Francis To: qemu-devel@nongnu.org Cc: alistair23@gmail.com, Atish Patra , Peter Maydell , Alistair Francis Subject: [PULL 4/5] target/riscv: Add asserts for out-of-bound access Date: Tue, 6 Aug 2024 16:25:44 +1000 Message-ID: <20240806062545.1250910-5-alistair.francis@wdc.com> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20240806062545.1250910-1-alistair.francis@wdc.com> References: <20240806062545.1250910-1-alistair.francis@wdc.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::633; envelope-from=alistair23@gmail.com; helo=mail-pl1-x633.google.com X-Spam_score_int: -17 X-Spam_score: -1.8 X-Spam_bar: - X-Spam_report: (-1.8 / 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, FREEMAIL_ENVFROM_END_DIGIT=0.25, FREEMAIL_FROM=0.001, 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 @gmail.com) X-ZM-MESSAGEID: 1722925628910116600 Content-Type: text/plain; charset="utf-8" From: Atish Patra Coverity complained about the possible out-of-bounds access with counter_virt/counter_virt_prev because these two arrays are accessed with privilege mode. However, these two arrays are accessed only when virt is enabled. Thus, the privilege mode can't be M mode. Add the asserts anyways to detect any wrong usage of these arrays in the future. Suggested-by: Peter Maydell Signed-off-by: Atish Patra Fixes: Coverity CID 1558459 Fixes: Coverity CID 1558462 Reviewed-by: Alistair Francis Message-ID: <20240724-fixes-v1-1-4a64596b0d64@rivosinc.com> Signed-off-by: Alistair Francis --- target/riscv/pmu.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/target/riscv/pmu.c b/target/riscv/pmu.c index 3cc0b3648c..e05ab067d2 100644 --- a/target/riscv/pmu.c +++ b/target/riscv/pmu.c @@ -204,6 +204,7 @@ static void riscv_pmu_icount_update_priv(CPURISCVState = *env, } =20 if (env->virt_enabled) { + g_assert(env->priv <=3D PRV_S); counter_arr =3D env->pmu_fixed_ctrs[1].counter_virt; snapshot_prev =3D env->pmu_fixed_ctrs[1].counter_virt_prev; } else { @@ -212,6 +213,7 @@ static void riscv_pmu_icount_update_priv(CPURISCVState = *env, } =20 if (new_virt) { + g_assert(newpriv <=3D PRV_S); snapshot_new =3D env->pmu_fixed_ctrs[1].counter_virt_prev; } else { snapshot_new =3D env->pmu_fixed_ctrs[1].counter_prev; @@ -242,6 +244,7 @@ static void riscv_pmu_cycle_update_priv(CPURISCVState *= env, } =20 if (env->virt_enabled) { + g_assert(env->priv <=3D PRV_S); counter_arr =3D env->pmu_fixed_ctrs[0].counter_virt; snapshot_prev =3D env->pmu_fixed_ctrs[0].counter_virt_prev; } else { @@ -250,6 +253,7 @@ static void riscv_pmu_cycle_update_priv(CPURISCVState *= env, } =20 if (new_virt) { + g_assert(newpriv <=3D PRV_S); snapshot_new =3D env->pmu_fixed_ctrs[0].counter_virt_prev; } else { snapshot_new =3D env->pmu_fixed_ctrs[0].counter_prev; --=20 2.45.2