From patchwork Thu Oct 7 09:31:27 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Rheinhardt X-Patchwork-Id: 30966 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a6b:6506:0:0:0:0:0 with SMTP id z6csp1284697iob; Thu, 7 Oct 2021 02:32:47 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzLz4uRW45VgVmXbCYwkkEurMbaLoDOTh/IJINfUDxjNK8PzZeh0pLfY1pWrVVC3ZlEyu1Z X-Received: by 2002:a50:8405:: with SMTP id 5mr4739734edp.111.1633599167665; Thu, 07 Oct 2021 02:32:47 -0700 (PDT) Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id kx3si36297109ejc.108.2021.10.07.02.32.47; Thu, 07 Oct 2021 02:32:47 -0700 (PDT) Received-SPF: pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) client-ip=79.124.17.100; Authentication-Results: mx.google.com; dkim=neutral (body hash did not verify) header.i=@outlook.com header.s=selector1 header.b="FvUtK2w/"; arc=fail (body hash mismatch); spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org; dmarc=fail (p=NONE sp=QUARANTINE dis=NONE) header.from=outlook.com Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id E193268A5DE; Thu, 7 Oct 2021 12:31:58 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from EUR04-DB3-obe.outbound.protection.outlook.com (mail-oln040092074015.outbound.protection.outlook.com [40.92.74.15]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id C445B68A32E for ; Thu, 7 Oct 2021 12:31:55 +0300 (EEST) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=G1h+IFd1mQEGYw9CC1jGSjh5Gp/gPbHX88ivomJnjC52jkHOW1oczKC5h6++1YKjP5rZmpnD+KgDkfICDSzEp7YvKVsOa3cTNle9Q35WW6RJioYZw9twDBylVrJzgsmYCd/JnAwLTHW8ai5NA+dLFOUfo6BkLh7m1O2ZHC2VX01zLvMj6pED2u9E081hRWkPMnY9ArLlBdSJxWstIYQbHKS1mlnWyPTtZtdj5SwbTRp3Lw50Kg/akmhkLbxIAgApXYIa3Aw2JHFEeeDTTCu31s43SDqvgrUbPYkJ2ybBrvaZlopGwVcW34Z3ybxJ6llKbCXhblAqOOjlVgtzYx83Ww== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=s0rnziKD5S1FnfeEdKzafsRIS0YAgkFZHTdKi/mspk8=; b=COnLf74m0VvnLZMZJGBcYa4Ewu8ScIQss/Yd+Vi+asYy9dq8b7m20432gosIVVPcRQsl00blhPEcnW8X2+7Dbtw+JGXQbKIlwQSTxHhZme8+oMvLl+NA/P83QiSyaEknWex1MhVxyMYDduv35XDEvgnnstvKkslpVVd/64se+J+D52sMivnuVX1wqPIZP8hiPu8ap+CGQKFz9EKUy/cVnHJfrejUSKZGtAsNinjLPy8jbCUSXdObJGK7FvH9ZXPO+1g8gfzhPVyMNU4KdbKA9RyMAg7DLvQYnhfYdnZmdwVqto66wh8bcWAAl5Dm0UYbqleGvb8rQQkDMeNzaM/KVA== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=none; dmarc=none; dkim=none; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=outlook.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=s0rnziKD5S1FnfeEdKzafsRIS0YAgkFZHTdKi/mspk8=; b=FvUtK2w/ISiNFtM2cKxpmmDJp3X58/uJrKb5naffQkVN6DBbZ+kylcEcNYIei/jSpfTw7LoetKUTb7VFQ8gqJI06NrMSQ5X2PXD++4++QFb7K8TPArlv53vN0aOK0tz/frfsc280bpHeemDUthsMbfd7pZWYlkObt8VUW/LNGRPbnp4tADrtiCX33/+jTXAzHQ7Fe0CLj9RMZRvdzkCg+DEtLKU4/D+D1BErCAYQYMJ6JEghM74m3se+IJgiZzXmBPoYvIV5tvlBz/CyhoNSb2UPi3+pYlc0udRnqHQxwI4cYNSSRjvNysiD8vzx1MOtYISi+3rkGkrQyRjfx2lhGA== Received: from AM7PR03MB6660.eurprd03.prod.outlook.com (2603:10a6:20b:1c1::22) by AM7PR03MB6325.eurprd03.prod.outlook.com (2603:10a6:20b:13c::18) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4587.18; Thu, 7 Oct 2021 09:31:42 +0000 Received: from AM7PR03MB6660.eurprd03.prod.outlook.com ([fe80::700f:d70b:3bb8:4d51]) by AM7PR03MB6660.eurprd03.prod.outlook.com ([fe80::700f:d70b:3bb8:4d51%7]) with mapi id 15.20.4566.022; Thu, 7 Oct 2021 09:31:42 +0000 From: Andreas Rheinhardt To: ffmpeg-devel@ffmpeg.org Date: Thu, 7 Oct 2021 11:31:27 +0200 Message-ID: X-Mailer: git-send-email 2.30.2 In-Reply-To: References: X-TMN: [K6fU0XzJlI3jguxJ6F0WYnMUFedhZ1X/] X-ClientProxiedBy: VI1PR07CA0161.eurprd07.prod.outlook.com (2603:10a6:802:16::48) To AM7PR03MB6660.eurprd03.prod.outlook.com (2603:10a6:20b:1c1::22) X-Microsoft-Original-Message-ID: <20211007093128.684774-6-andreas.rheinhardt@outlook.com> MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from sblaptop.fritz.box (188.193.170.49) by VI1PR07CA0161.eurprd07.prod.outlook.com (2603:10a6:802:16::48) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4587.16 via Frontend Transport; Thu, 7 Oct 2021 09:31:42 +0000 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: e1ede2fc-f9cd-4f45-13f7-08d9897545ef X-MS-Exchange-SLBlob-MailProps: S/btQ8cKWiStnrXejT7dVM2UBhIQPPsKAlirX2O5UQiHlf/nH0DiNnAL8XEUM22SF56bW2yhCy2abOl6PjQRb7CHsgFPvNh08eUiy1E7fppsTIplKWjAAJwfTuG39UU6bskB5KnQHTOhMbqBawzkWVXaXSfXuArdTjJtRq8LrK2uJVAJ0cucyLXwDSPopNbNr1ERrL2mrCt3QovTi4UUlcJ5iqXEsCuvZnrjkVupCyHCU3RmR4+A88e8vAjT+YQVzyw/fyXAJfQsIl5Lobho/kywxGhSi6ygjvKnho6A8v7uMw8llolncghyxU7Df4wXpvcNJYc51Q1k8q/OaC6N1r7ld2LrotpCImCzdTYfUwlov0lTowQQY27WUAetjXJnCDt9aQbnYO5GX4XkgfFlWFWO9QUQTC4V1xdzvpvaaPVVmzrI77IIsQFeCN+0diQoHHOaX7mUdExtS58fMGrrufVZ7mBk0US1agZ7SPinzRNB+O7lJpnZ+uzWRdx78qjHd+RXBhqbUgQQR7pW9HCeG/P7mXkV/+usQouhbH//a6M/Q7j6XYfhWFtpDhkcmo0kR2Rui0h++NHsuKlp/hvHsdm6iufyEZYvA1Mi8uZ7zio05hxQrAqEtO4e90sImw7/1jVDcu756u8XHPLrdnpDZazig/RYHkNmK6iBs61dQTqzhye1lRXjcWlSdfMv8RiTg33VLg9bdcExmDVukyam1Gy+YPx9ebA2T34PvPlXau3xvpZl5HmcH6xzA5SkoZV7cCAULfXTkj8= X-MS-TrafficTypeDiagnostic: AM7PR03MB6325: X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: rBTeffzUDmcFfPeyhqSqaMQCzAvUdZumlZaggNfW8elEaySYSXDzGHu+KP3pj81ILLCitsQlrLbC2odPFGyjq/C0Oim3KzXxJwo3Du3ref3uSO32cS1JOwozdNIhkDz+6vtVUTK+rSbMDeGEOv2VdQJPugJl+2cZiSH4UwMhE8O0v4xExS0AEKtsnIdqEf+jPxEeIiSQ913R5axlLmNl9TUZjAIEIhp6uHKGbI5Gp5gL7/4Ke75YAoe4TPMRUk2qohrPt+f6V6IytzmEXekfI0ZPQ7eGxoRSIkRxnHxABwGEuwqcJ3OQIqli4nNvXz142lEIHFTmT7IdCekdFhEIzZYRGjdZjhFz4FSp6bhI2PkEHvaQfPRpEhxAIvVaqDQX9LkkCfNuXel5vs9qg+HbBWVokNBGcBkPrHlarrat4YhdO5UXn2j+iGmPHsOBYAvu X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: hpr+8HStlhVANTYo4SSULZ9xv0Wyw2h9RctoNg6kSLnpQiDcmJE75uzOA84ns3gcJr1EEJUtbwkSvzvG9Ol6bB73+T8OP/6JHfJpGDf611joWObpqTIU+1egmi4bbnXTRtL64eZvUnhiWmXwim9XAA== X-OriginatorOrg: outlook.com X-MS-Exchange-CrossTenant-Network-Message-Id: e1ede2fc-f9cd-4f45-13f7-08d9897545ef X-MS-Exchange-CrossTenant-AuthSource: AM7PR03MB6660.eurprd03.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 07 Oct 2021 09:31:42.9460 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 84df9e7f-e9f6-40af-b435-aaaaaaaaaaaa X-MS-Exchange-CrossTenant-RMS-PersistedConsumerOrg: 00000000-0000-0000-0000-000000000000 X-MS-Exchange-Transport-CrossTenantHeadersStamped: AM7PR03MB6325 Subject: [FFmpeg-devel] [PATCH 7/8] avfilter/asrc_flite: Fix races upon (un)registering voices X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches Cc: Andreas Rheinhardt Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: N5qYbHISXmmk The voice registration system in libflite is broken: It is not thread-safe and also not based on internal counters; instead any call to unregister a voice frees said voice even if there are still many other users of said voice who have also registered said voice. While there is no way to guard against another library unregistering voices behind our back, we can at least be correct in the absence of other users of libflite. The current code already tried this by using a reference count of our own for each voice; but the implementation of this is not thread-safe at all. Fix this by using a mutex to guard all of libavfilter's libflite registration and unregistration calls, thereby being thread-safe in the absence of other libflite users. Signed-off-by: Andreas Rheinhardt --- I wonder whether we should unregister voices at all; after all, there might be a non-libavfilter user of these voices left who can't guard against us unregistering this voice. If we did so, the reference counters could be removed. If we also stopped worrying about calling the registration/init functions multiple times (as long as we don't do it concurrently), we could remove storing the voices in voice_entry and make voice_entries const: Every user would just have to register the voice itself. libavfilter/asrc_flite.c | 29 +++++++++++++++++++++-------- 1 file changed, 21 insertions(+), 8 deletions(-) diff --git a/libavfilter/asrc_flite.c b/libavfilter/asrc_flite.c index bd2ae774de..6335a81f0e 100644 --- a/libavfilter/asrc_flite.c +++ b/libavfilter/asrc_flite.c @@ -27,6 +27,7 @@ #include "libavutil/channel_layout.h" #include "libavutil/file.h" #include "libavutil/opt.h" +#include "libavutil/thread.h" #include "avfilter.h" #include "audio.h" #include "formats.h" @@ -63,7 +64,9 @@ static const AVOption flite_options[] = { AVFILTER_DEFINE_CLASS(flite); -static volatile int flite_inited = 0; +static AVMutex flite_mutex = AV_MUTEX_INITIALIZER; + +static int flite_inited = 0; /* declare functions for all the supported voices */ #define DECLARE_REGISTER_VOICE_FN(name) \ @@ -111,14 +114,19 @@ static int select_voice(struct voice_entry **entry_ret, const char *voice_name, for (i = 0; i < FF_ARRAY_ELEMS(voice_entries); i++) { struct voice_entry *entry = &voice_entries[i]; if (!strcmp(entry->name, voice_name)) { + cst_voice *voice; + pthread_mutex_lock(&flite_mutex); if (!entry->voice) entry->voice = entry->register_fn(NULL); - if (!entry->voice) { + voice = entry->voice; + if (voice) + entry->usage_count++; + pthread_mutex_unlock(&flite_mutex); + if (!voice) { av_log(log_ctx, AV_LOG_ERROR, "Could not register voice '%s'\n", voice_name); return AVERROR_UNKNOWN; } - entry->usage_count++; *entry_ret = entry; return 0; } @@ -141,12 +149,15 @@ static av_cold int init(AVFilterContext *ctx) return AVERROR_EXIT; } + pthread_mutex_lock(&flite_mutex); if (!flite_inited) { - if (flite_init() < 0) { - av_log(ctx, AV_LOG_ERROR, "flite initialization failed\n"); - return AVERROR_UNKNOWN; - } - flite_inited++; + if ((ret = flite_init()) >= 0) + flite_inited = 1; + } + pthread_mutex_unlock(&flite_mutex); + if (ret < 0) { + av_log(ctx, AV_LOG_ERROR, "flite initialization failed\n"); + return AVERROR_EXTERNAL; } if ((ret = select_voice(&flite->voice_entry, flite->voice_str, ctx)) < 0) @@ -197,10 +208,12 @@ static av_cold void uninit(AVFilterContext *ctx) FliteContext *flite = ctx->priv; if (flite->voice_entry) { + pthread_mutex_lock(&flite_mutex); if (!--flite->voice_entry->usage_count) { flite->voice_entry->unregister_fn(flite->voice); flite->voice_entry->voice = NULL; } + pthread_mutex_unlock(&flite_mutex); } delete_wave(flite->wave); flite->wave = NULL;