From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mails.dpdk.org (mails.dpdk.org [217.70.189.124]) by inbox.dpdk.org (Postfix) with ESMTP id 035EF42C52; Wed, 7 Jun 2023 18:20:39 +0200 (CEST) Received: from mails.dpdk.org (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id CCF5C42C76; Wed, 7 Jun 2023 18:20:38 +0200 (CEST) Received: from mx0b-0016f401.pphosted.com (mx0a-0016f401.pphosted.com [67.231.148.174]) by mails.dpdk.org (Postfix) with ESMTP id 77E7042C4D for ; Wed, 7 Jun 2023 18:20:37 +0200 (CEST) Received: from pps.filterd (m0045849.ppops.net [127.0.0.1]) by mx0a-0016f401.pphosted.com (8.17.1.19/8.17.1.19) with ESMTP id 357DvjHQ028182 for ; Wed, 7 Jun 2023 09:20:36 -0700 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=marvell.com; h=from : to : cc : subject : date : message-id : in-reply-to : references : mime-version : content-type; s=pfpt0220; bh=PUgJcOuXAY/Ad0hYT72CDFl/RxgCUIKO/CtVwiLckb4=; b=O8XyhWVkILc6E33i+KGHs2wrq70d6xTf5SMu/a0ojeJTEu2xu5ku1Cp4Oa7N1fjOeOs2 fpJJH1US6A+RxCgZd2Qh+l3waOkzZcFeVTuF5SOSLdUBPrQ7WwCFcl7OSC0Z6+X8PtKS Ud9b1CRo8SV1WBG6z7JdU39WKQqj3KQNo0Ko7nRt/pbqHn5l5jfwwhaOhsfcTbv4lbL+ 8tYLMXGXeYd/Zydi4Hpo3G55JFDVF1DpZVA517tCTPe6yEZHl+K/z5vc5KPT2taTivk+ g75G3RaqYjKRupomsMwaodm2Qg3gHlF+eJtkVxWvOh1fJJdWFyJ4g/fwC/YUET+XsHpg Yw== Received: from dc5-exch01.marvell.com ([199.233.59.181]) by mx0a-0016f401.pphosted.com (PPS) with ESMTPS id 3r2a7bvg96-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-SHA384 bits=256 verify=NOT) for ; Wed, 07 Jun 2023 09:20:36 -0700 Received: from DC5-EXCH01.marvell.com (10.69.176.38) by DC5-EXCH01.marvell.com (10.69.176.38) with Microsoft SMTP Server (TLS) id 15.0.1497.48; Wed, 7 Jun 2023 09:20:34 -0700 Received: from maili.marvell.com (10.69.176.80) by DC5-EXCH01.marvell.com (10.69.176.38) with Microsoft SMTP Server id 15.0.1497.48 via Frontend Transport; Wed, 7 Jun 2023 09:20:34 -0700 Received: from ml-host-33.caveonetworks.com (unknown [10.110.143.233]) by maili.marvell.com (Postfix) with ESMTP id A70F63F703F; Wed, 7 Jun 2023 09:20:34 -0700 (PDT) From: Srikanth Yalavarthi To: Srikanth Yalavarthi , Anup Prabhu CC: , , Subject: [PATCH v5] app/mldev: add internal function for file read Date: Wed, 7 Jun 2023 09:20:30 -0700 Message-ID: <20230607162030.3004-1-syalavarthi@marvell.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20230323152801.27666-1-syalavarthi@marvell.com> References: <20230323152801.27666-1-syalavarthi@marvell.com> MIME-Version: 1.0 Content-Type: text/plain X-Proofpoint-ORIG-GUID: 9BAOy1LLp-iLQiG1Cvt6AKOcE_2RB0aq X-Proofpoint-GUID: 9BAOy1LLp-iLQiG1Cvt6AKOcE_2RB0aq X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.254,Aquarius:18.0.957,Hydra:6.0.573,FMLib:17.11.176.26 definitions=2023-06-07_07,2023-06-07_01,2023-05-22_02 X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: DPDK patches and discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dev-bounces@dpdk.org Added internal function to read model, input and reference files with required error checks. This change fixes the unchecked return value and improper use of negative value issues reported by coverity scan for file read operations. Coverity issue: 383742, 383743 Fixes: f6661e6d9a3a ("app/mldev: validate model operations") Fixes: da6793390596 ("app/mldev: support inference validation") Signed-off-by: Srikanth Yalavarthi --- v5: * Updated as per review comments v4: * Drop use of fread and replace with system calls v3: * Fix incorrect use of rte_free with free v2: * Replace rte_malloc in ml_read_file with malloc app/test-mldev/test_common.c | 56 ++++++++++++++++++++++++++ app/test-mldev/test_common.h | 2 + app/test-mldev/test_inference_common.c | 56 ++++++++++---------------- app/test-mldev/test_model_common.c | 39 +++++------------- 4 files changed, 89 insertions(+), 64 deletions(-) diff --git a/app/test-mldev/test_common.c b/app/test-mldev/test_common.c index 016b31c6ba..cc38e8af05 100644 --- a/app/test-mldev/test_common.c +++ b/app/test-mldev/test_common.c @@ -5,12 +5,68 @@ #include #include +#include #include #include #include "ml_common.h" #include "test_common.h" +#include +#include +#include +#include + +int +ml_read_file(char *file, size_t *size, char **buffer) +{ + char *file_buffer = NULL; + struct stat file_stat; + char *file_map; + int ret; + int fd; + + fd = open(file, O_RDONLY); + if (fd == -1) { + ml_err("Failed to open file: %s\n", file); + return -errno; + } + + if (fstat(fd, &file_stat) != 0) { + ml_err("fstat failed for file: %s\n", file); + return -errno; + } + + file_buffer = malloc(file_stat.st_size); + if (file_buffer == NULL) { + ml_err("Failed to allocate memory: %s\n", file); + ret = -ENOMEM; + goto error; + } + + file_map = mmap(0, file_stat.st_size, PROT_READ, MAP_PRIVATE, fd, 0); + if (file_map == MAP_FAILED) { + ml_err("Failed to map file: %s\n", file); + ret = -errno; + goto error; + } + + rte_memcpy(file_buffer, file_map, file_stat.st_size); + munmap(file_map, file_stat.st_size); + + *size = file_stat.st_size; + *buffer = file_buffer; + + return 0; + +error: + free(file_buffer); + + close(fd); + + return ret; +} + bool ml_test_cap_check(struct ml_options *opt) { diff --git a/app/test-mldev/test_common.h b/app/test-mldev/test_common.h index def108d5b2..793841a917 100644 --- a/app/test-mldev/test_common.h +++ b/app/test-mldev/test_common.h @@ -27,4 +27,6 @@ int ml_test_device_close(struct ml_test *test, struct ml_options *opt); int ml_test_device_start(struct ml_test *test, struct ml_options *opt); int ml_test_device_stop(struct ml_test *test, struct ml_options *opt); +int ml_read_file(char *file, size_t *size, char **buffer); + #endif /* TEST_COMMON_H */ diff --git a/app/test-mldev/test_inference_common.c b/app/test-mldev/test_inference_common.c index d929ff6e61..8a6e4725df 100644 --- a/app/test-mldev/test_inference_common.c +++ b/app/test-mldev/test_inference_common.c @@ -599,10 +599,10 @@ ml_inference_iomem_setup(struct ml_test *test, struct ml_options *opt, uint16_t char mp_name[RTE_MEMPOOL_NAMESIZE]; const struct rte_memzone *mz; uint64_t nb_buffers; + char *buffer = NULL; uint32_t buff_size; uint32_t mz_size; - uint32_t fsize; - FILE *fp; + size_t fsize; int ret; /* get input buffer size */ @@ -642,51 +642,36 @@ ml_inference_iomem_setup(struct ml_test *test, struct ml_options *opt, uint16_t t->model[fid].reference = NULL; /* load input file */ - fp = fopen(opt->filelist[fid].input, "r"); - if (fp == NULL) { - ml_err("Failed to open input file : %s\n", opt->filelist[fid].input); - ret = -errno; + ret = ml_read_file(opt->filelist[fid].input, &fsize, &buffer); + if (ret != 0) goto error; - } - fseek(fp, 0, SEEK_END); - fsize = ftell(fp); - fseek(fp, 0, SEEK_SET); - if (fsize != t->model[fid].inp_dsize) { - ml_err("Invalid input file, size = %u (expected size = %" PRIu64 ")\n", fsize, + if (fsize == t->model[fid].inp_dsize) { + rte_memcpy(t->model[fid].input, buffer, fsize); + free(buffer); + } else { + ml_err("Invalid input file, size = %zu (expected size = %" PRIu64 ")\n", fsize, t->model[fid].inp_dsize); ret = -EINVAL; - fclose(fp); - goto error; - } - - if (fread(t->model[fid].input, 1, t->model[fid].inp_dsize, fp) != t->model[fid].inp_dsize) { - ml_err("Failed to read input file : %s\n", opt->filelist[fid].input); - ret = -errno; - fclose(fp); goto error; } - fclose(fp); /* load reference file */ + buffer = NULL; if (t->model[fid].reference != NULL) { - fp = fopen(opt->filelist[fid].reference, "r"); - if (fp == NULL) { - ml_err("Failed to open reference file : %s\n", - opt->filelist[fid].reference); - ret = -errno; + ret = ml_read_file(opt->filelist[fid].reference, &fsize, &buffer); + if (ret != 0) goto error; - } - if (fread(t->model[fid].reference, 1, t->model[fid].out_dsize, fp) != - t->model[fid].out_dsize) { - ml_err("Failed to read reference file : %s\n", - opt->filelist[fid].reference); - ret = -errno; - fclose(fp); + if (fsize == t->model[fid].out_dsize) { + rte_memcpy(t->model[fid].reference, buffer, fsize); + free(buffer); + } else { + ml_err("Invalid reference file, size = %zu (expected size = %" PRIu64 ")\n", + fsize, t->model[fid].out_dsize); + ret = -EINVAL; goto error; } - fclose(fp); } /* create mempool for quantized input and output buffers. ml_request_initialize is @@ -718,6 +703,9 @@ ml_inference_iomem_setup(struct ml_test *test, struct ml_options *opt, uint16_t t->model[fid].io_pool = NULL; } + if (buffer) + free(buffer); + return ret; } diff --git a/app/test-mldev/test_model_common.c b/app/test-mldev/test_model_common.c index c28e452f29..8dbb0ff89f 100644 --- a/app/test-mldev/test_model_common.c +++ b/app/test-mldev/test_model_common.c @@ -14,11 +14,11 @@ int ml_model_load(struct ml_test *test, struct ml_options *opt, struct ml_model *model, uint16_t fid) { - struct test_common *t = ml_test_priv(test); struct rte_ml_model_params model_params; - FILE *fp; int ret; + RTE_SET_USED(test); + if (model->state == MODEL_LOADED) return 0; @@ -26,43 +26,22 @@ ml_model_load(struct ml_test *test, struct ml_options *opt, struct ml_model *mod return -EINVAL; /* read model binary */ - fp = fopen(opt->filelist[fid].model, "r"); - if (fp == NULL) { - ml_err("Failed to open model file : %s\n", opt->filelist[fid].model); - return -1; - } - - fseek(fp, 0, SEEK_END); - model_params.size = ftell(fp); - fseek(fp, 0, SEEK_SET); - - model_params.addr = rte_malloc_socket("ml_model", model_params.size, - t->dev_info.min_align_size, opt->socket_id); - if (model_params.addr == NULL) { - ml_err("Failed to allocate memory for model: %s\n", opt->filelist[fid].model); - fclose(fp); - return -ENOMEM; - } - - if (fread(model_params.addr, 1, model_params.size, fp) != model_params.size) { - ml_err("Failed to read model file : %s\n", opt->filelist[fid].model); - rte_free(model_params.addr); - fclose(fp); - return -1; - } - fclose(fp); + ret = ml_read_file(opt->filelist[fid].model, &model_params.size, + (char **)&model_params.addr); + if (ret != 0) + return ret; /* load model to device */ ret = rte_ml_model_load(opt->dev_id, &model_params, &model->id); if (ret != 0) { ml_err("Failed to load model : %s\n", opt->filelist[fid].model); model->state = MODEL_ERROR; - rte_free(model_params.addr); + free(model_params.addr); return ret; } - /* release mz */ - rte_free(model_params.addr); + /* release buffer */ + free(model_params.addr); /* get model info */ ret = rte_ml_model_info_get(opt->dev_id, model->id, &model->info); -- 2.17.1