1From 465e173018f4a3f6d8f840db2d24c440ca5c3e75 Mon Sep 17 00:00:00 2001 2From: Zhu Guodong <zhuguodong0001@163.com> 3Date: Tue, 23 May 2023 16:22:24 +0800 4Subject: [PATCH] replace memcpy with memcpy_s in mindir 5 6--- 7 mindspore/lite/mindir/BUILD.gn | 2 ++ 8 .../lite/mindir/src/mindir_memory_manager.cc | 13 ++++++++++--- 9 mindspore/lite/mindir/src/mindir_tensor.cc | 17 ++++++++++++----- 10 mindspore/lite/mindir/src/utils.cc | 8 +++++++- 11 4 files changed, 31 insertions(+), 9 deletions(-) 12 13diff --git a/mindspore/lite/mindir/BUILD.gn b/mindspore/lite/mindir/BUILD.gn 14index 7bb190ae..e260a12f 100644 15--- a/mindspore/lite/mindir/BUILD.gn 16+++ b/mindspore/lite/mindir/BUILD.gn 17@@ -26,6 +26,7 @@ ohos_shared_library("mindir_lib") { 18 "include", 19 "inner_headers", 20 "//third_party/flatbuffers/include", 21+ "//third_party/bounds_checking_function/include", 22 ] 23 sources = [ 24 "../src/common/log.cc", 25@@ -44,6 +45,7 @@ ohos_shared_library("mindir_lib") { 26 "hdf_core:libhdi", 27 "hilog_native:libhilog", 28 "ipc:ipc_core", 29+ "bounds_checking_function:libsec_shared", 30 ] 31 configs = ["../:disable_android"] 32 defines = [ "MS_COMPILE_OHOS" ] 33diff --git a/mindspore/lite/mindir/src/mindir_memory_manager.cc b/mindspore/lite/mindir/src/mindir_memory_manager.cc 34index c775fa70..88c78d25 100644 35--- a/mindspore/lite/mindir/src/mindir_memory_manager.cc 36+++ b/mindspore/lite/mindir/src/mindir_memory_manager.cc 37@@ -1,7 +1,9 @@ 38 #include "mindir_memory_manager.h" 39-#include "src/common/log.h" 40-#include "utils.h" 41 #include <iostream> 42+#include "securec.h" 43+#include "utils.h" 44+#include "src/common/log.h" 45+ 46 namespace mindspore { 47 namespace lite { 48 namespace { 49@@ -29,7 +31,12 @@ void *MindIRMemoryManager::CopyFbbToNewMemory(flatbuffers::FlatBufferBuilder &fb 50 fbb.Clear(); 51 return nullptr; 52 } 53- memcpy(buff, fbb.GetBufferPointer(), fbb.GetSize()); 54+ auto ret = memcpy_s(buff, fbb.GetSize(), fbb.GetBufferPointer(), fbb.GetSize()); 55+ if (ret != EOK) { 56+ MS_LOG(ERROR) << "memcpy failed, ret:" << ret; 57+ fbb.Clear(); 58+ return nullptr; 59+ } 60 fbb.Clear(); 61 return buff; 62 } 63diff --git a/mindspore/lite/mindir/src/mindir_tensor.cc b/mindspore/lite/mindir/src/mindir_tensor.cc 64index 2e453438..9ec2d0e4 100644 65--- a/mindspore/lite/mindir/src/mindir_tensor.cc 66+++ b/mindspore/lite/mindir/src/mindir_tensor.cc 67@@ -16,6 +16,7 @@ 68 #include "src/common/log.h" 69 #include "mindir.h" 70 #include "utils.h" 71+#include "securec.h" 72 #include "mindir_memory_manager.h" 73 #include "nnrt/v1_0/nnrt_types.h" 74 #include "nnrt/v2_0/nnrt_types.h" 75@@ -259,13 +260,16 @@ OHOS::HDI::Nnrt::V1_0::SharedBuffer MindIR_Tensor_GetData(ConstTensorPtr tensor, 76 } 77 result.fd = buffer_templete.fd; 78 result.bufferSize = buffer_templete.bufferSize; 79- // MS_LOG(ERROR) << "offset:" << offset << ",src->size():" << value->data()->size(); 80- memcpy(mmap_ptr + offset, value->data()->data(), value->data()->size()); 81+ auto ret = memcpy_s(mmap_ptr + offset, value->data()->size(), value->data()->data(), value->data()->size()); 82+ if (ret != EOK) { 83+ MS_LOG(ERROR) << "Tensor memcpy failed, ret:" << ret; 84+ return {-1, 0, offset, 0}; 85+ } 86 result.offset = offset; 87 result.dataSize = value->data()->size(); 88 return result; 89 } else { 90- MS_LOG(WARNING) << "Tensor GetData failed, mmap pointer should not be nullptr"; 91+ MS_LOG(ERROR) << "Tensor GetData failed, mmap pointer should not be nullptr"; 92 return {-1, 0, offset, 0}; 93 } 94 } else { 95@@ -294,8 +298,11 @@ OHOS::HDI::Nnrt::V2_0::SharedBuffer MindIR_Tensor_GetData_V2_0(ConstTensorPtr te 96 } 97 result.fd = buffer_templete.fd; 98 result.bufferSize = buffer_templete.bufferSize; 99- // MS_LOG(ERROR) << "offset:" << offset << ",src->size():" << value->data()->size(); 100- memcpy(mmap_ptr + offset, value->data()->data(), value->data()->size()); 101+ auto ret = memcpy_s(mmap_ptr + offset, value->data()->size(), value->data()->data(), value->data()->size()); 102+ if (ret != EOK) { 103+ MS_LOG(ERROR) << "Tensor memcpy failed, ret:" << ret; 104+ return {-1, 0, offset, 0}; 105+ } 106 result.offset = offset; 107 result.dataSize = value->data()->size(); 108 return result; 109diff --git a/mindspore/lite/mindir/src/utils.cc b/mindspore/lite/mindir/src/utils.cc 110index 6a02d6bb..b044f414 100644 111--- a/mindspore/lite/mindir/src/utils.cc 112+++ b/mindspore/lite/mindir/src/utils.cc 113@@ -14,6 +14,7 @@ 114 * limitations under the License. 115 */ 116 #include "utils.h" 117+#include "securec.h" 118 #include "src/common/log.h" 119 #include "mindir_memory_manager.h" 120 namespace mindspore { 121@@ -41,7 +42,12 @@ PrimitivePtr MindIR_CreatePrimitiveFromBuilder(flatbuffers::FlatBufferBuilder &f 122 fbb.Clear(); 123 return nullptr; 124 } 125- memcpy(buff, fbb.GetBufferPointer(), fbb.GetSize()); 126+ auto ret = memcpy_s(buff, fbb.GetSize(), fbb.GetBufferPointer(), fbb.GetSize()); 127+ if (ret != EOK) { 128+ MS_LOG(ERROR) << "memcpy failed, ret:" << ret; 129+ fbb.Clear(); 130+ return nullptr; 131+ } 132 fbb.Clear(); 133 return buff; 134 } 135-- 1362.34.1 137 138