From patchwork Thu Sep 2 15:41:26 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Rheinhardt X-Patchwork-Id: 29964 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6602:2a4a:0:0:0:0 with SMTP id k10csp400274iov; Thu, 2 Sep 2021 08:42:24 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwTJM0BbGKXjNE8n/OcNM3z+ps3jiDoefjaQaD+e1IvLL6okaMhGRxf5ki6FpeOaVWdndEp X-Received: by 2002:a17:906:3497:: with SMTP id g23mr4488959ejb.85.1630597344114; Thu, 02 Sep 2021 08:42:24 -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 hr16si2412204ejc.352.2021.09.02.08.42.23; Thu, 02 Sep 2021 08:42:24 -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=WtT71ofj; 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 CC5C268A5F7; Thu, 2 Sep 2021 18:42:01 +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-oln040092074061.outbound.protection.outlook.com [40.92.74.61]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 4FED668A56C for ; Thu, 2 Sep 2021 18:41:55 +0300 (EEST) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=na/SomO+xIP3dolZBmgVrbmx9VCeKMFHK+FK+egaknUF9qvNojQUsM85lwIFAl7LHT14BwGa5EDKxKNNyD+2/hW9g5X+qcOuI9FoAp7deByN0Sf2jvCBSfEkJU+HjH/HrMI6HJTzf2Yglh0w1sRcpZNw7oR+/DZ1FcQqhb09I29YVOZXsQrD7qxs3+v5fHqqtAbtrTz13LK/MvdC6Sepxsp2EEcgGguIyJQbfJhwffZa64nm+Iw1noxycgimGNVOGUKMgDP/RFdfoa3UayPE/TCv5jyLQZzyRcX+aGz45WPtT+rA0uSR6X0aXnBAWzgPufuY+eef79D/qFIyfsaf+g== 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; bh=424dqzrSGF7E3e6MoIvqzIRNLLL3ryWQolh+4CRS/XM=; b=oNi5xeN7S4za47PcS2WdGKXDfO2D/vejfxUuHjdEo3CMrHE8TQpRIXmBTNZY9dW7YlY3zhqBaNIrAut1+rA02pmiTkmcCKUMd2L/ttnPi464xIc4E9KicbV7RT1XjkXqjJYNBIX3eHaNt4+qtgRsb5tdtPkTfBD6Z8OmiVPd+CzLY/xQkYq2/KhcJPIGWAktB4tMgyw27MfqSLb+yKq3j4H9l+6J8N9A3Md4rwB6V1/bio/u3hbffelvMxETpYkf1uBVBdrylIWVOl/EKTlFgUbeXkf66eAME1L1QxDZQyNJE/cNisl0JjEuUugn4mH4j6R+ZfzwfP197MClEuGVcg== 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=424dqzrSGF7E3e6MoIvqzIRNLLL3ryWQolh+4CRS/XM=; b=WtT71ofjl8QFv978N9PQ/djrmJsEjhU8aO+MNvqlSHKX7IrWF9Tdw5UQrC3ZKYrNopJVKGf2wdW0tETUSwo6fAMDgrxMdwvPsry/0JbEGxS5mXf3ExkhXOdmCw28uzCTlkl6Dhn0xyxoL5MJdr27FACB/6uAQtEkdPZH2hh8Q0wmi3GDJru8p+GndayliwXq5Jpzl5JfsyyVRWuDktSDnPZP5NbwulPwa55fmEnFehddA8TftgNmV5SU9f+p8xDosS6tFFcvYdQIlT94C1vLlyYHctrRc289q5Sm8997Mze4pnkU0Lba6VBUXwP9NiV9fn7mkqQiacWWwu1n/akjNQ== Received: from AM7PR03MB6660.eurprd03.prod.outlook.com (2603:10a6:20b:1c1::22) by AM5PR0301MB2436.eurprd03.prod.outlook.com (2603:10a6:203:8::18) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4478.19; Thu, 2 Sep 2021 15:41:54 +0000 Received: from AM7PR03MB6660.eurprd03.prod.outlook.com ([fe80::f8b5:1ef2:89b2:f8c5]) by AM7PR03MB6660.eurprd03.prod.outlook.com ([fe80::f8b5:1ef2:89b2:f8c5%7]) with mapi id 15.20.4457.024; Thu, 2 Sep 2021 15:41:54 +0000 From: Andreas Rheinhardt To: ffmpeg-devel@ffmpeg.org Date: Thu, 2 Sep 2021 17:41:26 +0200 Message-ID: X-Mailer: git-send-email 2.30.2 In-Reply-To: References: X-TMN: [WryGBBqqOA/eKDvuECiG0D/dO1FTv+q9] X-ClientProxiedBy: PR1P264CA0031.FRAP264.PROD.OUTLOOK.COM (2603:10a6:102:19f::18) To AM7PR03MB6660.eurprd03.prod.outlook.com (2603:10a6:20b:1c1::22) X-Microsoft-Original-Message-ID: <20210902154127.2571616-11-andreas.rheinhardt@outlook.com> MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from sblaptop.fritz.box (188.192.142.38) by PR1P264CA0031.FRAP264.PROD.OUTLOOK.COM (2603:10a6:102:19f::18) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4478.20 via Frontend Transport; Thu, 2 Sep 2021 15:41:53 +0000 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: b6d142bd-c118-42c7-a2dd-08d96e28305e X-MS-Exchange-SLBlob-MailProps: gjx25WM8ZNW7ZO6S4gJQgEvgNWj+v++jwUl3JxxzG0r6IkKnEvydZb5OnOpX2hygjZE4TyceQIr1pUqcRhfo4HX+WiPpdVz0D3SYG4vSeW7BijOCOgvlmQIu4w919vLxEDbPUBP6Wcko2VQgLLVMhvxDCOLoePRtiaQPoFeDjOPVlnzY8YFYI2wXDlSmhiToZrpJYNwD6J6SsuxMO0ylq7fMDi0Z5pOPLPbA7l6guKuw/CHpsDO+Vgm+9TZF6wXjJIlTr7MJo9DrObH2QWGr5Ox1rfXlQb0lq9HuR6X5508drDZDX+cssv9pDozpNrPD/Qxd1aC/saIwILbgKKT0n3lviek2Hup53Hqbfn2m9zxA7yoduiYae+YoFheFCkRTj+zU0gUUGZ2v6ynLEHzwX49ZxhgRKW9kk38jXC2zTQp6dBsStLoEEImdVvI7ET1cBO29+EXXBkF3GKTQ2RwfeDIPSgmbirJ/u800YFvzkzOec5ZnOz5a/8BLv7S4kOkiosMj4+gahkDhi1avv9Hqi8yFTb3z8EYQ2FCDJonHH7/w858kmZ2/6nII+x9dAc26INdY5e25D+EnXUfrH61WQ+e/94R4T5da6TimldDnj8jnpcLHI1sSkTxK8dK9XX0OvOYcDMd3CKfZ9dfsUziNTL9GuGjouMdx+nt0KlwpkAO7uPAVluoIW3Ph2+PXIrW6v38TQye1RnDomhfuUEuQNDiRBvib/KWs4AIZX9GmClo= X-MS-TrafficTypeDiagnostic: AM5PR0301MB2436: X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: KlQTKXnlj1DqKcVvT/R84Q1VJKZOdki5brwrhQDgOEpt2z42ZLvB93yoPBH97UszVTJOjVwj/jcG+wX+WJiTDwlag8MWU7+eb/iGzCE60TBX1qEnB6Y2EbDhKFYo9fmYO17/pWt3haIlff4VbRck2REUXVX3OLoJv5wo7NEoanZxgUu03bd1pjaIX5Cs9moqu7bl7gCbjxtZPJtmF6T8LiFlUnk3egLsEnf/ogc6A7UZaD7/0Vjs//dhaFnFv5jNb1nlvu13BbvQFxVOmcOAIhduR/nWaul0iVxIKYgCOmYAKbFdcNTsANy2Q+QViWvvhrJXf4nFOidW7itaVSvWH4/UVpWXsyy16os72Q1mEQqxISb4Xs38PRAzMY8gY1ku+7jcwBJn9IWb7/ip1lvSppRdHImddUeYkXa+1ere+qqZ5kFKaCgnMG/NRYo2xW8u X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: kVPHvS4VTIomNsiu/OH6spFuMk3TfvkT+/fSD+Bk1U3Z9hXpqUidjC/PZ76bgBrt2gs7d4nPqQ2TwqwyOTuOl1D8wD5i5FH+wQ/jR/sKRR1hTTjYa8+6ygc6UtezGjHI6EsbwwkDOQ6Yamc7jVBMNA== X-OriginatorOrg: outlook.com X-MS-Exchange-CrossTenant-Network-Message-Id: b6d142bd-c118-42c7-a2dd-08d96e28305e X-MS-Exchange-CrossTenant-AuthSource: AM7PR03MB6660.eurprd03.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 02 Sep 2021 15:41:54.0852 (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: AM5PR0301MB2436 Subject: [FFmpeg-devel] [PATCH 12/13] avcodec/omx: Check initializing mutexes/conditions 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: 8xmnKYPo1PtG The earlier code did not properly check these initializations: It only recorded whether the part of init where these initializations are has been reached, but it did not check whether the initializations were successful, although destroying them would be undefined behaviour if they had not been initialized successfully. Furthermore cleanup() always locked a mutex regardless of whether there was any attempt to initialize these mutexes at all. Signed-off-by: Andreas Rheinhardt --- This is mostly untested: I only tested whether it compiles. libavcodec/omx.c | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/libavcodec/omx.c b/libavcodec/omx.c index 9597c60057..7086ddd3a4 100644 --- a/libavcodec/omx.c +++ b/libavcodec/omx.c @@ -43,6 +43,7 @@ #include "avcodec.h" #include "h264.h" #include "internal.h" +#include "pthread_internal.h" #ifdef OMX_SKIP64BIT static OMX_TICKS to_omx_ticks(int64_t value) @@ -218,7 +219,7 @@ typedef struct OMXCodecContext { OMX_STATETYPE state; OMX_ERRORTYPE error; - int mutex_cond_inited; + unsigned mutex_cond_inited_cnt; int eos_sent, got_eos; @@ -229,6 +230,12 @@ typedef struct OMXCodecContext { int profile; } OMXCodecContext; +#define NB_MUTEX_CONDS 6 +#define OFF(field) offsetof(OMXCodecContext, field) +DEFINE_OFFSET_ARRAY(OMXCodecContext, omx_codec_context, mutex_cond_inited_cnt, + (OFF(input_mutex), OFF(output_mutex), OFF(state_mutex)), + (OFF(input_cond), OFF(output_cond), OFF(state_cond))); + static void append_buffer(pthread_mutex_t *mutex, pthread_cond_t *cond, int* array_size, OMX_BUFFERHEADERTYPE **array, OMX_BUFFERHEADERTYPE *buffer) @@ -591,6 +598,9 @@ static av_cold void cleanup(OMXCodecContext *s) { int i, executing; + /* If the mutexes/condition variables have not been properly initialized, + * nothing has been initialized and locking the mutex might be unsafe. */ + if (s->mutex_cond_inited_cnt == NB_MUTEX_CONDS) { pthread_mutex_lock(&s->state_mutex); executing = s->state == OMX_StateExecuting; pthread_mutex_unlock(&s->state_mutex); @@ -620,20 +630,13 @@ static av_cold void cleanup(OMXCodecContext *s) omx_deinit(s->omx_context); s->omx_context = NULL; - if (s->mutex_cond_inited) { - pthread_cond_destroy(&s->state_cond); - pthread_mutex_destroy(&s->state_mutex); - pthread_cond_destroy(&s->input_cond); - pthread_mutex_destroy(&s->input_mutex); - pthread_cond_destroy(&s->output_cond); - pthread_mutex_destroy(&s->output_mutex); - s->mutex_cond_inited = 0; - } av_freep(&s->in_buffer_headers); av_freep(&s->out_buffer_headers); av_freep(&s->free_in_buffers); av_freep(&s->done_out_buffers); av_freep(&s->output_buf); + } + ff_pthread_free(s, omx_codec_context_offsets); } static av_cold int omx_encode_init(AVCodecContext *avctx) @@ -644,17 +647,14 @@ static av_cold int omx_encode_init(AVCodecContext *avctx) OMX_BUFFERHEADERTYPE *buffer; OMX_ERRORTYPE err; + /* cleanup relies on the mutexes/conditions being initialized first. */ + ret = ff_pthread_init(s, omx_codec_context_offsets); + if (ret < 0) + return ret; s->omx_context = omx_init(avctx, s->libname, s->libprefix); if (!s->omx_context) return AVERROR_ENCODER_NOT_FOUND; - pthread_mutex_init(&s->state_mutex, NULL); - pthread_cond_init(&s->state_cond, NULL); - pthread_mutex_init(&s->input_mutex, NULL); - pthread_cond_init(&s->input_cond, NULL); - pthread_mutex_init(&s->output_mutex, NULL); - pthread_cond_init(&s->output_cond, NULL); - s->mutex_cond_inited = 1; s->avctx = avctx; s->state = OMX_StateLoaded; s->error = OMX_ErrorNone;