From patchwork Fri Oct 1 21:08:30 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Rheinhardt X-Patchwork-Id: 30871 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a6b:6506:0:0:0:0:0 with SMTP id z6csp2089389iob; Fri, 1 Oct 2021 14:11:09 -0700 (PDT) X-Google-Smtp-Source: ABdhPJykD0gP3tSL/lfml/sZobJ+5aXkIsOM4Qb5GRlhKdj/ebIchZPb+zL1UjX1RU7tA1RfJxud X-Received: by 2002:a05:6402:50cc:: with SMTP id h12mr347673edb.112.1633122669527; Fri, 01 Oct 2021 14:11:09 -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 x42si285119edy.427.2021.10.01.14.11.09; Fri, 01 Oct 2021 14:11:09 -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="hR06WFM/"; 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 7141468A9C4; Sat, 2 Oct 2021 00:09:47 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from EUR02-HE1-obe.outbound.protection.outlook.com (mail-oln040092068078.outbound.protection.outlook.com [40.92.68.78]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 9998C68A9C1 for ; Sat, 2 Oct 2021 00:09:46 +0300 (EEST) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=BPjDgRJkRp+KMkipztHggdDlXv8MUvp6RzYs0lRBxFVEXSgWdvJAZ3MtuETiieIq9U2hgR9j2AgZzSDbVPnnW7W2+o8TtZPycuti/7aKPD/cf+Kn5ctoAM8/h4ZlhPxGJGWAPNuY69McgRyixVSmJDD+Br/+BlNEwQuzHsq/oxeOcInw79uq1LyJ1uGJC/sGFhrlrGFcrZWMDWHpmyQ+rfdzj8V0cLEXYmqBifE52uVrcK9sMhxiNYJnv3WtOMEqnMHCEtArH/p9zHo1X1e9jUGnAI8C2jusCrEJ/0eiz2OGkMGeR6M2BUwe+qyCBDZKAuYBDfyxvG5tWL8i+Lj/3w== 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=OS/3TpgBS4q9ohtYDSlDO6Q4kI+1745GcSjUaL9d8hc=; b=Z9OqpeQdxURyAHHv6Mrw9LD+NoWn65rANrxegIRDe5e6nem0QihMLXkO5DYvl1ak+q5H8BAh1SQD8FRw4k/cwZq64I0qpZtHUh/zessxZTRqHil7EB/oVhhwQD7S+TtuqZ2FNEZpi8vLCHq1fYk2Bjm/hvaDL3A/pKAIkrugdrTqBxGovoObiW4Mw/X8B+AkYZmI33jK7wieT1GxmbHOBwH+F8rQOiE9P6BVzeHlxjDy605R9LfJhynBHG1Wi3wfhppHJx5fPUl86+zg12Y8iidNVBjiJmKuozp+A7D6BfwypIkG8BeDGL8NO/is1SHe0ccq6v9goi1zfJWrjvuPwQ== 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=OS/3TpgBS4q9ohtYDSlDO6Q4kI+1745GcSjUaL9d8hc=; b=hR06WFM/rm/yqwHlWqqfwU8yeGhLucDQjkW3jYg/Ezx6ky6yRXSV91pW6FTlo96MEus4glK1AwcweS+9GGAvJe2b/oTWGier+1fNso++6b3CfzbnqApjZwSaRW+7lUV3VpybTQNIWus/AsAG1CsCVIOM8qNmg7zCfuDPGwXwpboBc7raPMjAsLajlpuknSbQDe5kxsJAKeQLJA7m41pn5AkBRAccD2yChbDGa6pyCVWVC8tPoWhMekiic6P48Utzy5h79goL1PWPe5T4MPvoG3/cNug/nOiZCHGWH6wqURs61KEBEYZ3r9EneJ/NtKxl2VRua1hu3POpkb6v1Sxwfg== Received: from AM7PR03MB6660.eurprd03.prod.outlook.com (2603:10a6:20b:1c1::22) by AS8PR03MB6952.eurprd03.prod.outlook.com (2603:10a6:20b:29a::13) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4566.17; Fri, 1 Oct 2021 21:09:45 +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.019; Fri, 1 Oct 2021 21:09:45 +0000 From: Andreas Rheinhardt To: ffmpeg-devel@ffmpeg.org Date: Fri, 1 Oct 2021 23:08:30 +0200 Message-ID: X-Mailer: git-send-email 2.30.2 In-Reply-To: References: X-TMN: [CqsZOc7eI/ttc9A6oVghupm+tnw5/GmQ] X-ClientProxiedBy: FR3P281CA0071.DEUP281.PROD.OUTLOOK.COM (2603:10a6:d10:4b::7) To AM7PR03MB6660.eurprd03.prod.outlook.com (2603:10a6:20b:1c1::22) X-Microsoft-Original-Message-ID: <20211001210837.2879205-12-andreas.rheinhardt@outlook.com> MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from sblaptop.fritz.box (188.192.142.38) by FR3P281CA0071.DEUP281.PROD.OUTLOOK.COM (2603:10a6:d10:4b::7) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4587.8 via Frontend Transport; Fri, 1 Oct 2021 21:09:44 +0000 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: b0e21aa9-f930-42a8-faed-08d9851fcb3e X-MS-Exchange-SLBlob-MailProps: S/btQ8cKWiTgZdhvd8QKnLjC/Dfe6SG9g2cmkr7FYGLG5/fU3Rr75c6bkT/g3Lios44wenLqxqKzIK+1gzUbedoxVWbBcNE5sK5Q/RPVHik31Xx4Oue1PRhyxnuYppAVBLoWE2IM6F15+3V2QBtnhB4da3IJI0pY6R/LzbZElwEnT6bPG2z1ZuKECo/u34ItxKkRqBInX1ad4v7rADKM+9KClFpTepDwKqkRziXPwyDHCR69w7lCcaOvznOG94H//v3h91JjDz0rOLuqC59ypYBKSrMbyxr0U3BCwHdaUQ19gXKsjvQ9UujEy7fgnBH2HMKxAv47BR1drQGmBwCOeLAYrDbuj53qcfKzKWIfxk03nWvwxS0cBsHJNjol+nXpwq+nJtqtHnORoh+QboKZkwgU0y2joRi+Dzpp+xZtPTPLiArvPNhoKMYyb72kk3ckKO7WyT12URfwavGNPj0whSV5YhNkNr4x2kjy69qVVQC7D/ntAKyRvAl0inJ5ns277KprMF62Tsbo2NrHdj0larMb3wdB5kflsfjruNw38U7c88iAjyAgjBZwiI4tGvznMPjSIjth2Wd6rFdS8q9vFvb1LUERTrjomCH2N4YjyNrppdAUQtMt8ciUe6ywfvMXQQz8WtMeipCY5wmx8DoMKZmxoVB1BAlbZjCNyE6vjCxUB9Hx7qEZ1+yOHYoydf7iFnfxpkGcs5LJoBsCuCjP+5O5EvQLq69cKPiU5VTK12YyOOrGB8gan7JgVeKLA8AjLLNbx1RuRjU= X-MS-TrafficTypeDiagnostic: AS8PR03MB6952: X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: 4YmHKy2vKVvhBd/n1E1XfNr/0QRO9W1ybImM4eh4eDbyzMdlqisshD8Lw93iYPGxRRgH6+PVpHolY9dSnI4/kgtNkOmhfyZjpzfCNw7HcAAguwbneGIFRAptnD8WSVNww/Ns5xDTBaiJWLkHd+ndlxUz8fAPaPxYBUN62KhXWnhSObV5N8RRGgqyWHLzmUDYFF9S1vlnVB153I5fzyP10vvJeh6535qjJlMCGtFq9d3JOXJcXG+W4Va7h0B9/22vtuuxdozJeMKD+qQjneujzXUzM90AUWMwsD+FNBJRybI1An4OlBQlktPOuLVPOcrz/S3iogMb8A4dXSlPh1arQKNel4ixHj2OMSfRsoPoZ5PcVtq3AqqKCWVHNA64QdDuGuchLd8I2wlJv59GRXVz4bBYMDccqAR53mI0whU5GyTMateyJJedMlnpT98O0hlN X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: CKCUVY0pJn/Gcs3WpWo0KiRLls1RSaxXQjUU34oisiXkKmxcKGSyS32K9Q6IAST2HEaVtdtihG0Zs35kebTMs+LXMiAdUOM8LKDz9oQBNxBX1DO+Z/xQ8/s9VXZ8DfT5PnX9ozT7SQFbE4uu99Kp6A== X-OriginatorOrg: outlook.com X-MS-Exchange-CrossTenant-Network-Message-Id: b0e21aa9-f930-42a8-faed-08d9851fcb3e X-MS-Exchange-CrossTenant-AuthSource: AM7PR03MB6660.eurprd03.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 01 Oct 2021 21:09:45.1750 (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: AS8PR03MB6952 Subject: [FFmpeg-devel] [PATCH 13/20] avformat/allformats: Fix data race when accessing devices lists 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: EEk/o+MV4M6L Up until now setting the input and output devices lists is guarded by a mutex. This prevents data races emanating from multiple concurrent calls to avpriv_register_devices() (triggered by multiple concurrent calls to avdevice_register_all()). Yet reading the lists pointers was done without any lock and with nonatomic variables. This means that there are data races in case of concurrent calls to av_(de)muxer_iterate() and avdevice_register_all() (but only if the iteration in av_(de)muxer_iterate exhausts the non-device (de)muxers). This commit fixes this by putting said pointers into atomic objects. Due to the unavailability of _Atomic the object is an atomic_uintptr, leading to ugly casts. Switching to atomics also allowed to remove the mutex currently used in avpriv_register_devices(). Signed-off-by: Andreas Rheinhardt --- libavformat/allformats.c | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/libavformat/allformats.c b/libavformat/allformats.c index 5471f7c16f..cadaf057fc 100644 --- a/libavformat/allformats.c +++ b/libavformat/allformats.c @@ -19,7 +19,7 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ -#include "libavutil/thread.h" +#include #include "libavformat/internal.h" #include "avformat.h" @@ -534,18 +534,20 @@ extern const AVInputFormat ff_vapoursynth_demuxer; #include "libavformat/muxer_list.c" #include "libavformat/demuxer_list.c" -static const AVInputFormat * const *indev_list = NULL; -static const AVOutputFormat * const *outdev_list = NULL; +static atomic_uintptr_t indev_list_intptr = ATOMIC_VAR_INIT(0); +static atomic_uintptr_t outdev_list_intptr = ATOMIC_VAR_INIT(0); const AVOutputFormat *av_muxer_iterate(void **opaque) { static const uintptr_t size = sizeof(muxer_list)/sizeof(muxer_list[0]) - 1; uintptr_t i = (uintptr_t)*opaque; const AVOutputFormat *f = NULL; + uintptr_t tmp; if (i < size) { f = muxer_list[i]; - } else if (outdev_list) { + } else if (tmp = atomic_load_explicit(&outdev_list_intptr, memory_order_relaxed)) { + const AVOutputFormat *const *outdev_list = (const AVOutputFormat *const *)tmp; f = outdev_list[i - size]; } @@ -559,10 +561,12 @@ const AVInputFormat *av_demuxer_iterate(void **opaque) static const uintptr_t size = sizeof(demuxer_list)/sizeof(demuxer_list[0]) - 1; uintptr_t i = (uintptr_t)*opaque; const AVInputFormat *f = NULL; + uintptr_t tmp; if (i < size) { f = demuxer_list[i]; - } else if (indev_list) { + } else if (tmp = atomic_load_explicit(&indev_list_intptr, memory_order_relaxed)) { + const AVInputFormat *const *indev_list = (const AVInputFormat *const *)tmp; f = indev_list[i - size]; } @@ -571,12 +575,8 @@ const AVInputFormat *av_demuxer_iterate(void **opaque) return f; } -static AVMutex avpriv_register_devices_mutex = AV_MUTEX_INITIALIZER; - void avpriv_register_devices(const AVOutputFormat * const o[], const AVInputFormat * const i[]) { - ff_mutex_lock(&avpriv_register_devices_mutex); - outdev_list = o; - indev_list = i; - ff_mutex_unlock(&avpriv_register_devices_mutex); + atomic_store_explicit(&outdev_list_intptr, (uintptr_t)o, memory_order_relaxed); + atomic_store_explicit(&indev_list_intptr, (uintptr_t)i, memory_order_relaxed); }