From patchwork Thu Apr 28 22:57:44 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mykola Lysenko X-Patchwork-Id: 12831310 X-Patchwork-Delegate: bpf@iogearbox.net Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 212C5C433EF for ; Thu, 28 Apr 2022 23:00:00 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1353087AbiD1XDO (ORCPT ); Thu, 28 Apr 2022 19:03:14 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33246 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1353115AbiD1XDM (ORCPT ); Thu, 28 Apr 2022 19:03:12 -0400 Received: from mx0a-00082601.pphosted.com (mx0b-00082601.pphosted.com [67.231.153.30]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E26A3C1CBE for ; Thu, 28 Apr 2022 15:59:55 -0700 (PDT) Received: from pps.filterd (m0089730.ppops.net [127.0.0.1]) by m0089730.ppops.net (8.17.1.5/8.17.1.5) with ESMTP id 23SLRsw0004423 for ; Thu, 28 Apr 2022 15:59:55 -0700 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=fb.com; h=from : to : cc : subject : date : message-id : mime-version : content-transfer-encoding : content-type; s=facebook; bh=HOBL022biVjWRW/6RDlSCvkDDv4J7Gd6v4Els1PMOYo=; b=GumGezX6Q3tq2JHBWo6ge3lSo2FZemnVqUzpeKAUh2AbP4SG1K4jYDCkwa62EjJPFnPz YFBsVfl1JZst15HSUWyKpWw685dqpcJkOX9vk4yxEYwRaQtqAGXxBxEH6a1X/2zM12sl YmB6SwT/KtBLh+U6+1ttgY7dbgv1oOXwASU= Received: from mail.thefacebook.com ([163.114.132.120]) by m0089730.ppops.net (PPS) with ESMTPS id 3fr2mc0ne6-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128 verify=NOT) for ; Thu, 28 Apr 2022 15:59:54 -0700 Received: from twshared13345.18.frc3.facebook.com (2620:10d:c085:108::4) by mail.thefacebook.com (2620:10d:c085:21d::6) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.24; Thu, 28 Apr 2022 15:59:53 -0700 Received: by devvm4897.frc0.facebook.com (Postfix, from userid 537053) id E92FB6591D7F; Thu, 28 Apr 2022 15:59:46 -0700 (PDT) From: Mykola Lysenko To: , , , CC: , Mykola Lysenko Subject: [PATCH bpf-next] selftests/bpf: fix two memory leaks in prog_tests Date: Thu, 28 Apr 2022 15:57:44 -0700 Message-ID: <20220428225744.1961643-1-mykolal@fb.com> X-Mailer: git-send-email 2.30.2 MIME-Version: 1.0 X-FB-Internal: Safe X-Proofpoint-ORIG-GUID: T4IH2hpBD6KPLrzrW6FKZq6hDiqUAZYd X-Proofpoint-GUID: T4IH2hpBD6KPLrzrW6FKZq6hDiqUAZYd X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.205,Aquarius:18.0.858,Hydra:6.0.486,FMLib:17.11.64.514 definitions=2022-04-28_05,2022-04-28_01,2022-02-23_01 Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net Fix log_fp memory leak in dispatch_thread_read_log. Remove obsolete log_fp clean-up code in dispatch_thread. Also, release memory of subtest_selector. This can be reproduced with -n 2/1 parameters. Signed-off-by: Mykola Lysenko --- tools/testing/selftests/bpf/test_progs.c | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/tools/testing/selftests/bpf/test_progs.c b/tools/testing/selftests/bpf/test_progs.c index 22fe283710fa..a07da648af3b 100644 --- a/tools/testing/selftests/bpf/test_progs.c +++ b/tools/testing/selftests/bpf/test_progs.c @@ -77,10 +77,12 @@ static void stdio_restore_cleanup(void) if (env.subtest_state) { fclose(env.subtest_state->stdout); + env.subtest_state->stdout = NULL; stdout = env.test_state->stdout; stderr = env.test_state->stdout; } else { fclose(env.test_state->stdout); + env.test_state->stdout = NULL; } #endif } @@ -1077,6 +1079,7 @@ static int read_prog_test_msg(int sock_fd, struct msg *msg, enum msg_type type) static int dispatch_thread_read_log(int sock_fd, char **log_buf, size_t *log_cnt) { FILE *log_fp = NULL; + int result = 0; log_fp = open_memstream(log_buf, log_cnt); if (!log_fp) @@ -1085,16 +1088,20 @@ static int dispatch_thread_read_log(int sock_fd, char **log_buf, size_t *log_cnt while (true) { struct msg msg; - if (read_prog_test_msg(sock_fd, &msg, MSG_TEST_LOG)) - return 1; + if (read_prog_test_msg(sock_fd, &msg, MSG_TEST_LOG)) { + result = 1; + goto out; + } fprintf(log_fp, "%s", msg.test_log.log_buf); if (msg.test_log.is_last) break; } + +out: fclose(log_fp); log_fp = NULL; - return 0; + return result; } static int dispatch_thread_send_subtests(int sock_fd, struct test_state *state) @@ -1132,7 +1139,6 @@ static void *dispatch_thread(void *ctx) { struct dispatch_data *data = ctx; int sock_fd; - FILE *log_fp = NULL; sock_fd = data->sock_fd; @@ -1214,8 +1220,6 @@ static void *dispatch_thread(void *ctx) if (env.debug) fprintf(stderr, "[%d]: Protocol/IO error: %s.\n", data->worker_id, strerror(errno)); - if (log_fp) - fclose(log_fp); done: { struct msg msg_exit; @@ -1685,6 +1689,7 @@ int main(int argc, char **argv) unload_bpf_testmod(); free_test_selector(&env.test_selector); + free_test_selector(&env.subtest_selector); free_test_states(); if (env.succ_cnt + env.fail_cnt + env.skip_cnt == 0)