From nobody Tue Feb 10 01:54:32 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; dmarc=pass(p=none dis=none) header.from=163.com ARC-Seal: i=1; a=rsa-sha256; t=1692329029; cv=none; d=zohomail.com; s=zohoarc; b=fJM7eFrCOtimQ4ms8HOWzxGvejQn9+tK7FFxZKkMRfjSHm4RIkML1ZNRwlQi8KFFzZ5aS59+QS/slLSRXYNO/RZo0P/nmsRtExriHxX2V1K6pW1xPhRHvmHDk6WpwLT5QQA9WpUg44CZbBtiZ5D7s1I6uIWkJuRIGq6EqK3sP0w= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1692329029; h=Content-Transfer-Encoding:Cc:Date:From:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Sender:Subject:To; bh=31qCjGtUi6Mud1nHsJu2fpGL0YO/zHs8tY6CmlEjMTU=; b=Kb8LYUlFccd0CsG+iWb7meD7OQxcfjJfJJwWMVusSu64+i/T3IDOLYLT6hxaensVzUlXdEcZQ9genzjlOb978Pr3ynYnG4JRl/X0n/5cZ9inEjLMZvrpGeoaEI16RdSLCXJ3w6ofNhfJJGgxo734LNVVgIcLWZWA0EOZWoNuepI= 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 1692329029817666.7144323677634; Thu, 17 Aug 2023 20:23:49 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qWq4V-0007Ru-AZ; Thu, 17 Aug 2023 23:22:47 -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 1qWq4T-0007QE-6w for qemu-devel@nongnu.org; Thu, 17 Aug 2023 23:22:45 -0400 Received: from m12.mail.163.com ([220.181.12.199]) by eggs.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qWq4O-00004x-7K for qemu-devel@nongnu.org; Thu, 17 Aug 2023 23:22:44 -0400 Received: from localhost.localdomain (unknown [112.5.139.96]) by zwqz-smtp-mta-g4-0 (Coremail) with SMTP id _____wC3vz_n495kGwNjDQ--.7108S2; Fri, 18 Aug 2023 11:22:21 +0800 (CST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=163.com; s=s110527; h=From:Subject:Date:Message-Id:MIME-Version; bh=31qCj GtUi6Mud1nHsJu2fpGL0YO/zHs8tY6CmlEjMTU=; b=DOglM/fSEt68nQbnm6B9x /M+I1iniQ6IPFAXqLon5/r3J4w6tpZu1XMVeE18L5MuGCNZvSh8RY/fL7Tnko0tG Qrr6o8ODgBmGJF2IWFDKtuFdsdpDEH2ovpcWUCpjXMJndIkSz3kKtYKFa5iWFyDB esbNRAcqTi0k/FQR4sJl+E= From: M_O_Bz@163.com To: qemu-devel@nongnu.org Cc: vr_qemu@t-online.de, kraxel@redhat.com, manos.pitsidianakis@linaro.org, M_O_Bz@163.com, linwei6@ruijie.com.cn Subject: [PATCH] HDA codec: Fix wanted_r/w position overflow Date: Fri, 18 Aug 2023 11:22:17 +0800 Message-Id: <20230818032217.31134-1-M_O_Bz@163.com> X-Mailer: git-send-email 2.35.1 MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-CM-TRANSID: _____wC3vz_n495kGwNjDQ--.7108S2 X-Coremail-Antispam: 1Uf129KBjvJXoWxXFy3Xw15Zw17AF4fKFyrCrg_yoW5AFyrpr ZruasxCa18twnIyr4xCFn8G3W5Gw1vyw1YkrWUKFsayan5Xrs7WryqgryS9FW8AFZIqr15 ur4jyFy7Gw42vrJanT9S1TB71UUUUUUqnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDUYxBIdaVFxhVjvjDU0xZFpf9x0pE6wZnUUUUU= X-Originating-IP: [112.5.139.96] X-CM-SenderInfo: ppbrsur26rljoofrz/1tbiNBTP9VaED2GA3QAAs7 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=220.181.12.199; envelope-from=m_o_bz@163.com; helo=m12.mail.163.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, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_BL=0.001, RCVD_IN_MSPIKE_L4=0.001, 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 @163.com) X-ZM-MESSAGEID: 1692329030660100002 Content-Type: text/plain; charset="utf-8" From: zeroway when the duration now - buft_start reach to some kind of value, which will get the multiply hda_bytes_per_second(st) * (now - buft_start) o= verflow, instead of calculate the wanted_r/wpos from start time to current time, here calculate the each timer tick delta data first in wanted_r/wpos_delta, and sum it all to wanted_r/wpos to avoid the overflow Signed-off-by: zeroway --- hw/audio/hda-codec.c | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/hw/audio/hda-codec.c b/hw/audio/hda-codec.c index c51d8ba617..747188221a 100644 --- a/hw/audio/hda-codec.c +++ b/hw/audio/hda-codec.c @@ -169,6 +169,8 @@ struct HDAAudioStream { uint8_t buf[8192]; /* size must be power of two */ int64_t rpos; int64_t wpos; + int64_t wanted_rpos; + int64_t wanted_wpos; QEMUTimer *buft; int64_t buft_start; }; @@ -226,16 +228,18 @@ static void hda_audio_input_timer(void *opaque) int64_t wpos =3D st->wpos; int64_t rpos =3D st->rpos; =20 - int64_t wanted_rpos =3D hda_bytes_per_second(st) * (now - buft_start) + int64_t wanted_rpos_delta =3D hda_bytes_per_second(st) * (now - buft_s= tart) / NANOSECONDS_PER_SECOND; - wanted_rpos &=3D -4; /* IMPORTANT! clip to frames */ + st->wanted_rpos +=3D wanted_rpos_delta; + st->wanted_rpos &=3D -4; /* IMPORTANT! clip to frames */ =20 - if (wanted_rpos <=3D rpos) { + st->buft_start =3D now; + if (st->wanted_rpos <=3D rpos) { /* we already transmitted the data */ goto out_timer; } =20 - int64_t to_transfer =3D MIN(wpos - rpos, wanted_rpos - rpos); + int64_t to_transfer =3D MIN(wpos - rpos, st->wanted_rpos - rpos); while (to_transfer) { uint32_t start =3D (rpos & B_MASK); uint32_t chunk =3D MIN(B_SIZE - start, to_transfer); @@ -290,16 +294,18 @@ static void hda_audio_output_timer(void *opaque) int64_t wpos =3D st->wpos; int64_t rpos =3D st->rpos; =20 - int64_t wanted_wpos =3D hda_bytes_per_second(st) * (now - buft_start) + int64_t wanted_wpos_delta =3D hda_bytes_per_second(st) * (now - buft_s= tart) / NANOSECONDS_PER_SECOND; - wanted_wpos &=3D -4; /* IMPORTANT! clip to frames */ + st->wanted_wpos +=3D wanted_wpos_delta; + st->wanted_wpos &=3D -4; /* IMPORTANT! clip to frames */ =20 - if (wanted_wpos <=3D wpos) { + st->buft_start =3D now; + if (st->wanted_wpos <=3D wpos) { /* we already received the data */ goto out_timer; } =20 - int64_t to_transfer =3D MIN(B_SIZE - (wpos - rpos), wanted_wpos - wpos= ); + int64_t to_transfer =3D MIN(B_SIZE - (wpos - rpos), st->wanted_wpos - = wpos); while (to_transfer) { uint32_t start =3D (wpos & B_MASK); uint32_t chunk =3D MIN(B_SIZE - start, to_transfer); @@ -420,6 +426,8 @@ static void hda_audio_set_running(HDAAudioStream *st, b= ool running) int64_t now =3D qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL); st->rpos =3D 0; st->wpos =3D 0; + st->wanted_rpos =3D 0; + st->wanted_wpos =3D 0; st->buft_start =3D now; timer_mod_anticipate_ns(st->buft, now + HDA_TIMER_TICKS); } else { --=20 2.35.1