• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1From a7a733c9f2a5ed38749ef358b64f955444d78328 Mon Sep 17 00:00:00 2001
2From: fangzhou12 <fangzhou12@huawei.com>
3Date: Wed, 28 Jun 2023 08:57:09 +0800
4Subject: [PATCH] auto-apply 0027-js-buffer-leakage-bugfix.patch
5
6---
7 include/js_api/mslite_model_napi.h            |  1 -
8 include/js_api/mstensor_napi.h                |  7 ++--
9 .../src/runtime/js_api/mslite_model_napi.cc   | 34 ++++++++++---------
10 .../lite/src/runtime/js_api/mstensor_napi.cc  | 24 ++++++-------
11 4 files changed, 30 insertions(+), 36 deletions(-)
12
13diff --git a/include/js_api/mslite_model_napi.h b/include/js_api/mslite_model_napi.h
14index be1c5167..717b595a 100644
15--- a/include/js_api/mslite_model_napi.h
16+++ b/include/js_api/mslite_model_napi.h
17@@ -75,7 +75,6 @@ class MSLiteModelNapi {
18
19   static thread_local napi_ref constructor_;
20   napi_env env_ = nullptr;
21-  napi_ref wrapper_ = nullptr;
22
23   static ModelInfo *model_info_;
24   static ContextInfo *context_;
25diff --git a/include/js_api/mstensor_napi.h b/include/js_api/mstensor_napi.h
26index 0e9462c8..e2b181b8 100644
27--- a/include/js_api/mstensor_napi.h
28+++ b/include/js_api/mstensor_napi.h
29@@ -24,7 +24,8 @@ namespace mindspore {
30 class MSTensorNapi {
31  public:
32   static napi_value NewInstance(napi_env env, mindspore::MSTensor tensor);
33-
34+  MSTensorNapi();
35+  ~MSTensorNapi();
36  private:
37   static napi_value Constructor(napi_env env, napi_callback_info info);
38   static void Finalize(napi_env env, void *nativeObject, void *finalize);
39@@ -39,12 +40,8 @@ class MSTensorNapi {
40   static napi_value GetDataBuffer(napi_env env, napi_callback_info info);
41   static napi_value SetData(napi_env env, napi_callback_info info);
42
43-  MSTensorNapi();
44-  ~MSTensorNapi();
45-
46   static thread_local napi_ref constructor_;
47   napi_env env_ = nullptr;
48-  napi_ref wrapper_ = nullptr;
49
50   std::unique_ptr<MSTensor> nativeMSTensor_ = nullptr;
51 };
52diff --git a/mindspore/lite/src/runtime/js_api/mslite_model_napi.cc b/mindspore/lite/src/runtime/js_api/mslite_model_napi.cc
53index 4ebc1a3d..0f1934a7 100644
54--- a/mindspore/lite/src/runtime/js_api/mslite_model_napi.cc
55+++ b/mindspore/lite/src/runtime/js_api/mslite_model_napi.cc
56@@ -65,14 +65,13 @@ const std::unordered_map<std::string, DeviceType> kDeviceTypes{
57 };
58 }  // namespace
59
60-MSLiteModelNapi::MSLiteModelNapi() : env_(nullptr), wrapper_(nullptr) {
61+MSLiteModelNapi::MSLiteModelNapi() : native_model_(nullptr), env_(nullptr) {
62   MS_LOG(INFO) << "MSLiteModelNapi Instances create.";
63 }
64
65 MSLiteModelNapi::~MSLiteModelNapi() {
66-  if (wrapper_ != nullptr) {
67-    napi_delete_reference(env_, wrapper_);
68-  }
69+  native_model_ = nullptr;
70+  env_ = nullptr;
71   if (model_info_ != nullptr) {
72     if (model_info_->model_buffer_data != nullptr) {
73       model_info_->model_buffer_data = nullptr;
74@@ -203,12 +202,12 @@ std::shared_ptr<mindspore::Model> MSLiteModelNapi::CreateModel(ModelInfo *model_
75       }
76       auto ret = model_ptr->Build(model_info_ptr->model_buffer_data, model_info_ptr->model_buffer_total,
77                                   mindspore::kMindIR, context);
78-
79+
80+      (void)munmap(model_info_ptr->model_buffer_data, model_info_ptr->model_buffer_total);
81       if (ret == mindspore::kSuccess) {
82         MS_LOG(INFO) << "Build model from fd success.";
83         return model_ptr;
84       }
85-      (void)munmap(model_info_ptr->model_buffer_data, model_info_ptr->model_buffer_total);
86
87       break;
88     }
89@@ -262,9 +261,10 @@ int32_t MSLiteModelNapi::GetDeviceInfoContext(ContextInfo *context_ptr,
90 napi_value MSLiteModelNapi::Constructor(napi_env env, napi_callback_info info) {
91   napi_status status;
92   napi_value result = nullptr;
93+  napi_get_undefined(env, &result);
94   GET_PARAMS(env, info, ARGS_TWO);
95
96-  MSLiteModelNapi *model_napi = new (std::nothrow) MSLiteModelNapi();
97+  std::unique_ptr<MSLiteModelNapi> model_napi = std::make_unique<MSLiteModelNapi>();
98   if (model_napi == nullptr) {
99     MS_LOG(ERROR) << "No memory";
100     return result;
101@@ -277,15 +277,13 @@ napi_value MSLiteModelNapi::Constructor(napi_env env, napi_callback_info info) {
102     return result;
103   }
104
105-  status = napi_wrap(env, thisVar, reinterpret_cast<void *>(model_napi), MSLiteModelNapi::Finalize, nullptr,
106+  status = napi_wrap(env, thisVar, reinterpret_cast<void *>(model_napi.get()), MSLiteModelNapi::Finalize, nullptr,
107                      nullptr);
108-  if (status != napi_ok) {
109-    delete model_napi;
110-    napi_get_undefined(env, &result);
111-    MS_LOG(ERROR) << "Failed to wrap native instance";
112-    return result;
113+  if (status == napi_ok) {
114+    model_napi.release();
115+    return thisVar;
116   }
117-  return thisVar;
118+  return result;
119 }
120
121 int32_t MSLiteModelNapi::ParseModelInfo(napi_env env, napi_value root, ModelInfo &model_info) {
122@@ -788,14 +786,18 @@ napi_value MSLiteModelNapi::GetInputs(napi_env env, napi_callback_info info) {
123 napi_value MSLiteModelNapi::Resize(napi_env env, napi_callback_info info) {
124   napi_value undefinedResult = nullptr;
125   bool result = false;
126-  napi_get_undefined(env, &undefinedResult);
127+  napi_status status = napi_get_boolean(env, result, &undefinedResult);
128+  if (status != napi_ok) {
129+    MS_LOG(ERROR) << "get bool error";
130+    return undefinedResult;
131+  }
132
133   napi_value jsThis = nullptr;
134   napi_value jsResult = nullptr;
135   MSLiteModelNapi *modelNapi = nullptr;
136   napi_value argv[ARGS_TWO] = {0};
137   size_t argCount = PARAM2;
138-  napi_status status = napi_get_cb_info(env, info, &argCount, argv, &jsThis, nullptr);
139+  status = napi_get_cb_info(env, info, &argCount, argv, &jsThis, nullptr);
140   if (status != napi_ok || jsThis == nullptr) {
141     MS_LOG(ERROR) << "failed to retrieve details about the callback";
142     return undefinedResult;
143diff --git a/mindspore/lite/src/runtime/js_api/mstensor_napi.cc b/mindspore/lite/src/runtime/js_api/mstensor_napi.cc
144index 898e81bd..975d3d94 100644
145--- a/mindspore/lite/src/runtime/js_api/mstensor_napi.cc
146+++ b/mindspore/lite/src/runtime/js_api/mstensor_napi.cc
147@@ -45,11 +45,8 @@ const int ARGS_TWO = 2;
148 MSTensorNapi::MSTensorNapi() { MS_LOG(DEBUG) << "MSLITE MSTensorNapi Instances create."; }
149
150 MSTensorNapi::~MSTensorNapi() {
151-  if (wrapper_ != nullptr) {
152-    napi_delete_reference(env_, wrapper_);
153-  }
154   if (nativeMSTensor_ != nullptr) {
155-    nativeMSTensor_->SetData(nullptr);
156+    nativeMSTensor_ = nullptr;
157   }
158   MS_LOG(INFO) << "MSLITE MSTensorNapi Instances destroy.";
159 }
160@@ -62,22 +59,21 @@ napi_value MSTensorNapi::Constructor(napi_env env, napi_callback_info info) {
161     return nullptr;
162   }
163
164-  MSTensorNapi *tensprNapi = new (std::nothrow) MSTensorNapi();
165-  if (tensprNapi == nullptr) {
166+  std::unique_ptr<MSTensorNapi> tensorNapi = std::make_unique<MSTensorNapi>();
167+  if (tensorNapi == nullptr) {
168     MS_LOG(ERROR) << "No memory";
169     return nullptr;
170   }
171
172-  tensprNapi->env_ = env;
173-  status = napi_wrap(env, jsThis, tensprNapi, MSTensorNapi::Finalize, nullptr, nullptr);
174-  if (status != napi_ok) {
175-    delete tensprNapi;
176-    MS_LOG(ERROR) << "Failed to wrap native instance";
177-    return nullptr;
178+  tensorNapi->env_ = env;
179+  status = napi_wrap(env, jsThis, reinterpret_cast<void *>(tensorNapi.get()), MSTensorNapi::Finalize, nullptr, nullptr);
180+  if (status == napi_ok) {
181+    tensorNapi.release();
182+    return jsThis;
183   }
184
185-  MS_LOG(INFO) << "Constructor success.";
186-  return jsThis;
187+  MS_LOG(ERROR) << "Constructor fail.";
188+  return nullptr;
189 }
190
191 void MSTensorNapi::Finalize(napi_env env, void *nativeObject, void *finalize) {
192--
1932.17.1
194
195