From nobody Tue Feb 10 19:50:08 2026 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=1690793109; cv=none; d=zohomail.com; s=zohoarc; b=n6sbc4PxlxRcJ290gzp2TWDshbkA0783t+n7igqVE1nF882QrdGzpINqS5fenIdahv29gOqOjyAse81l0ivyN3L4ZtesUWf29NsntGAkEcdQKutBQJXeiebcwT8RbEYXs3/AYPf4muF1Ds8eMBo+3a9lFYwoyD/kaUPa+ICDs3U= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1690793109; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject; bh=Rv8ecp5MNNYaYzflXkXYlG6pC6Fau5qFV5g6norDKWY=; b=B44AiWOdEiuqduFmkKKJvQNSuf+o86w4zYFaA5pAXY4pYCk4zDoMoCgPKSGm/ZfeQy62dpjvFqfBzW5tSQeoJyx+TO+cvZbUH81K7q0nik8esPXupZGjRMPVHNGdStT4rVMhEHQLMt17Ggc5LxvsJJe0DWSPsKi6U17Q3/fzc+k= 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 1690793109169477.16695627181525; Mon, 31 Jul 2023 01:45:09 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qQOW3-0007G6-Re; Mon, 31 Jul 2023 04:44:35 -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 1qQOVv-00079D-JW for qemu-devel@nongnu.org; Mon, 31 Jul 2023 04:44:28 -0400 Received: from mail-pf1-x434.google.com ([2607:f8b0:4864:20::434]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1qQOVs-0004SZ-Kr for qemu-devel@nongnu.org; Mon, 31 Jul 2023 04:44:27 -0400 Received: by mail-pf1-x434.google.com with SMTP id d2e1a72fcca58-686f94328a4so2338323b3a.0 for ; Mon, 31 Jul 2023 01:44:24 -0700 (PDT) Received: from alarm.. ([157.82.204.253]) by smtp.gmail.com with ESMTPSA id u19-20020aa78493000000b00666e649ca46sm7075563pfn.101.2023.07.31.01.44.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 31 Jul 2023 01:44:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=daynix-com.20221208.gappssmtp.com; s=20221208; t=1690793063; x=1691397863; 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=Rv8ecp5MNNYaYzflXkXYlG6pC6Fau5qFV5g6norDKWY=; b=PZDp+BBAvc0KSB56DsQmUvjKSFua+VZ9zJv0Qx2cAelbx6oWm2A0BCqnTznDCYOsGT 9C/ylIUMVvvzeIZPr6sUP2C5NV7YBwxDzj0GMLnazLIdJWgEv+zt1Amv29lD1aGuK/io tLXNLpawYWT5S2pS2V0ngHDeqkjhqHr6WUd3TXkSg8jsX2VItf1/yPkZwJALZ2T54ZX0 1DfHgW94Ua/0Msj3wxgIeAW9i8Wr0Ks7WlQavTsyENI2dm5luHnIlNv7CpHebGGWoCHz PhY6U71DsRup2LP0zN7VAoz+34qvXpMw7DAh4zvlZXGVb0Rgz1uf1XnarZOmdA3Qt11m PP9A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1690793063; x=1691397863; 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=Rv8ecp5MNNYaYzflXkXYlG6pC6Fau5qFV5g6norDKWY=; b=UcfbaE/lrvnRU/E5uV49hcE08+98Ml7o5o24lDxODJYeLPQlJhoB3Zilol6C6J93q/ 2OFP9ugWExAuJXdlK9hlndnbRvseBlCWWoBvMoCSRfK1nMBXgWxVa0DKBPit5xoiWZcu ymRUm4Cbji8xjWbs6OGAhGyGrEcDR5bwyT8bGc1V4oGvPopxz39i53FF07QMdfva0l56 FSDzC1/jkJCBdDTFRPO6ZBk+B+6Ycxy5P/GGAmUoVZ7vMOV3c65Xt+rBG9r31uXUqGaQ FeP3Rov7a+u3fwWlS6n9IxIGDXVwzCF8STz/UvW2+cg3R/JDWz1/+e71Yxl++x/Nd3eo w6bA== X-Gm-Message-State: ABy/qLYHz/7QYTbMQMbVPHn6paSXxwDzwuIkJvnZEz+TmfEbnzpMC3Mc Y6hit98YWr/HJujMKtHzgaHMPw== X-Google-Smtp-Source: APBJJlGS/uBh757PghIh1LcVXhYRgj49gAZ0Q+WNPNFD5s7fgIOIJxRogywBE58O/HDzE59o2ynz1A== X-Received: by 2002:a05:6a20:160b:b0:133:bf18:ef7b with SMTP id l11-20020a056a20160b00b00133bf18ef7bmr10943503pzj.24.1690793063098; Mon, 31 Jul 2023 01:44:23 -0700 (PDT) From: Akihiko Odaki To: Cc: Paolo Bonzini , =?UTF-8?q?Alex=20Benn=C3=A9e?= , Thomas Huth , Alexandre Iooss , Mahmoud Mandour , Eduardo Habkost , Marcel Apfelbaum , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Yanan Wang , Richard Henderson , =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= , =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= , John Snow , Cleber Rosa , Peter Maydell , Michael Rolnik , "Edgar E. Iglesias" , Brian Cain , Song Gao , Xiaojuan Yang , Laurent Vivier , Aurelien Jarno , Jiaxun Yang , Aleksandar Rikalo , Chris Wulff , Marek Vasut , Stafford Horne , Daniel Henrique Barboza , =?UTF-8?q?C=C3=A9dric=20Le=20Goater?= , David Gibson , Greg Kurz , Nicholas Piggin , Palmer Dabbelt , Alistair Francis , Bin Meng , Weiwei Li , Liu Zhiwei , Yoshinori Sato , David Hildenbrand , Ilya Leoshkevich , Mark Cave-Ayland , Artyom Tarasenko , Bastian Koppelmann , Max Filippov , qemu-devel@nongnu.org, qemu-arm@nongnu.org, qemu-ppc@nongnu.org, qemu-riscv@nongnu.org, qemu-s390x@nongnu.org, Akihiko Odaki Subject: [RFC PATCH 01/24] contrib/plugins: Use GRWLock in execlog Date: Mon, 31 Jul 2023 17:43:28 +0900 Message-ID: <20230731084354.115015-2-akihiko.odaki@daynix.com> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20230731084354.115015-1-akihiko.odaki@daynix.com> References: <20230731084354.115015-1-akihiko.odaki@daynix.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: none client-ip=2607:f8b0:4864:20::434; envelope-from=akihiko.odaki@daynix.com; helo=mail-pf1-x434.google.com X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_NONE=0.001, T_SCC_BODY_TEXT_LINE=-0.01 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+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @daynix-com.20221208.gappssmtp.com) X-ZM-MESSAGEID: 1690793111834100003 Content-Type: text/plain; charset="utf-8" execlog had the following comment: > As we could have multiple threads trying to do this we need to > serialise the expansion under a lock. Threads accessing already > created entries can continue without issue even if the ptr array > gets reallocated during resize. However, when the ptr array gets reallocated, the other threads may have a stale reference to the old buffer. This results in use-after-free. Use GRWLock to properly fix this issue. Fixes: 3d7caf145e ("contrib/plugins: add execlog to log instruction executi= on and memory access") Signed-off-by: Akihiko Odaki Reviewed-by: Alex Benn=C3=A9e --- contrib/plugins/execlog.c | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/contrib/plugins/execlog.c b/contrib/plugins/execlog.c index 7129d526f8..ce67acf145 100644 --- a/contrib/plugins/execlog.c +++ b/contrib/plugins/execlog.c @@ -19,7 +19,7 @@ QEMU_PLUGIN_EXPORT int qemu_plugin_version =3D QEMU_PLUGI= N_VERSION; =20 /* Store last executed instruction on each vCPU as a GString */ static GPtrArray *last_exec; -static GMutex expand_array_lock; +static GRWLock expand_array_lock; =20 static GPtrArray *imatches; static GArray *amatches; @@ -28,18 +28,16 @@ static GArray *amatches; * Expand last_exec array. * * As we could have multiple threads trying to do this we need to - * serialise the expansion under a lock. Threads accessing already - * created entries can continue without issue even if the ptr array - * gets reallocated during resize. + * serialise the expansion under a lock. */ static void expand_last_exec(int cpu_index) { - g_mutex_lock(&expand_array_lock); + g_rw_lock_writer_unlock(&expand_array_lock); while (cpu_index >=3D last_exec->len) { GString *s =3D g_string_new(NULL); g_ptr_array_add(last_exec, s); } - g_mutex_unlock(&expand_array_lock); + g_rw_lock_writer_unlock(&expand_array_lock); } =20 /** @@ -51,8 +49,10 @@ static void vcpu_mem(unsigned int cpu_index, qemu_plugin= _meminfo_t info, GString *s; =20 /* Find vCPU in array */ + g_rw_lock_reader_lock(&expand_array_lock); g_assert(cpu_index < last_exec->len); s =3D g_ptr_array_index(last_exec, cpu_index); + g_rw_lock_reader_unlock(&expand_array_lock); =20 /* Indicate type of memory access */ if (qemu_plugin_mem_is_store(info)) { @@ -80,10 +80,14 @@ static void vcpu_insn_exec(unsigned int cpu_index, void= *udata) GString *s; =20 /* Find or create vCPU in array */ + g_rw_lock_reader_lock(&expand_array_lock); if (cpu_index >=3D last_exec->len) { + g_rw_lock_reader_unlock(&expand_array_lock); expand_last_exec(cpu_index); + g_rw_lock_reader_lock(&expand_array_lock); } s =3D g_ptr_array_index(last_exec, cpu_index); + g_rw_lock_reader_unlock(&expand_array_lock); =20 /* Print previous instruction in cache */ if (s->len) { --=20 2.41.0