Skip to content

Commit 6cf8dd0

Browse files
committed
[CodeClean] handle error case
Code clean, prevent invalid state when configuring backend handle. Signed-off-by: Jaeyun Jung <jy1210.jung@samsung.com>
1 parent df9aa3d commit 6cf8dd0

3 files changed

Lines changed: 120 additions & 61 deletions

File tree

src/hal-backend-ml-snpe.cc

Lines changed: 42 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -32,28 +32,20 @@ typedef struct _snpe_handle_s {
3232
std::vector<Snpe_IUserBuffer_Handle_t> user_buffers;
3333
} snpe_handle_s;
3434

35-
static int
36-
ml_snpe_init (void **backend_private)
35+
/** @brief Initialize handle. */
36+
static void
37+
_init_snpe_handle (snpe_handle_s *snpe)
3738
{
38-
snpe_handle_s *snpe = g_new0 (snpe_handle_s, 1);
39+
memset (snpe, 0, sizeof (snpe_handle_s));
3940

4041
gst_tensors_info_init (&snpe->inputInfo);
4142
gst_tensors_info_init (&snpe->outputInfo);
42-
43-
*backend_private = snpe;
44-
return HAL_ML_ERROR_NONE;
4543
}
4644

47-
static int
48-
ml_snpe_deinit (void *backend_private)
45+
/** @brief Close model and clear internal data in handle. */
46+
static void
47+
_clear_snpe_handle (snpe_handle_s *snpe)
4948
{
50-
snpe_handle_s *snpe = (snpe_handle_s *) backend_private;
51-
52-
if (!snpe) {
53-
g_critical ("[snpe backend] ml_snpe_deinit called with invalid backend_private");
54-
return HAL_ML_ERROR_INVALID_PARAMETER;
55-
}
56-
5749
if (snpe->inputMap_h)
5850
Snpe_UserBufferMap_Delete (snpe->inputMap_h);
5951

@@ -69,13 +61,38 @@ ml_snpe_deinit (void *backend_private)
6961
if (snpe->snpe_h)
7062
Snpe_SNPE_Delete (snpe->snpe_h);
7163

72-
if (snpe->model_path)
73-
g_free (snpe->model_path);
64+
g_free (snpe->model_path);
7465

7566
gst_tensors_info_free (&snpe->inputInfo);
7667
gst_tensors_info_free (&snpe->outputInfo);
7768

69+
_init_snpe_handle (snpe);
70+
}
71+
72+
static int
73+
ml_snpe_init (void **backend_private)
74+
{
75+
snpe_handle_s *snpe = g_new0 (snpe_handle_s, 1);
76+
77+
_init_snpe_handle (snpe);
78+
79+
*backend_private = snpe;
80+
return HAL_ML_ERROR_NONE;
81+
}
82+
83+
static int
84+
ml_snpe_deinit (void *backend_private)
85+
{
86+
snpe_handle_s *snpe = (snpe_handle_s *) backend_private;
87+
88+
if (!snpe) {
89+
g_critical ("[snpe backend] ml_snpe_deinit called with invalid backend_private");
90+
return HAL_ML_ERROR_INVALID_PARAMETER;
91+
}
92+
93+
_clear_snpe_handle (snpe);
7894
g_free (snpe);
95+
7996
return HAL_ML_ERROR_NONE;
8097
}
8198

@@ -85,11 +102,18 @@ ml_snpe_configure_instance (void *backend_private, const void *prop_)
85102
const GstTensorFilterProperties *prop = (const GstTensorFilterProperties *) prop_;
86103
snpe_handle_s *snpe = (snpe_handle_s *) backend_private;
87104

88-
if (!snpe) {
105+
if (!snpe || !prop) {
89106
g_critical ("[snpe backend] ml_snpe_configure_instance called with invalid backend_private");
90107
return HAL_ML_ERROR_INVALID_PARAMETER;
91108
}
92109

110+
if (snpe->model_path) {
111+
g_critical ("[snpe backend] invalid state, clear old data.");
112+
_clear_snpe_handle (snpe);
113+
}
114+
115+
snpe->model_path = g_strdup (prop->model_files[0]);
116+
93117
Snpe_DlVersion_Handle_t lib_version_h = NULL;
94118
Snpe_RuntimeList_Handle_t runtime_list_h = NULL;
95119
Snpe_DlContainer_Handle_t container_h = NULL;
@@ -325,7 +349,6 @@ ml_snpe_configure_instance (void *backend_private, const void *prop_)
325349
throw std::invalid_argument (err_msg);
326350
}
327351

328-
snpe->model_path = g_strdup (prop->model_files[0]);
329352
container_h = Snpe_DlContainer_Open (snpe->model_path);
330353
if (!container_h)
331354
throw std::runtime_error (

src/hal-backend-ml-util.cc

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -125,15 +125,16 @@ GstTensorInfo * gst_tensors_info_get_nth_info (GstTensorsInfo * info, guint inde
125125
if (index < NNS_TENSOR_MEMORY_MAX)
126126
return &info->info[index];
127127

128-
if (!info->extra) {
129-
info->extra = g_new0 (GstTensorInfo, NNS_TENSOR_SIZE_EXTRA_LIMIT);
128+
if (index < NNS_TENSOR_SIZE_LIMIT) {
129+
if (!info->extra) {
130+
info->extra = g_new0 (GstTensorInfo, NNS_TENSOR_SIZE_EXTRA_LIMIT);
130131

131-
for (i = 0; i < NNS_TENSOR_SIZE_EXTRA_LIMIT; ++i)
132-
gst_tensor_info_init (&info->extra[i]);
133-
}
132+
for (i = 0; i < NNS_TENSOR_SIZE_EXTRA_LIMIT; ++i)
133+
gst_tensor_info_init (&info->extra[i]);
134+
}
134135

135-
if (index < NNS_TENSOR_SIZE_LIMIT)
136136
return &info->extra[index - NNS_TENSOR_MEMORY_MAX];
137+
}
137138

138139
g_critical ("Failed to get the information, invalid index %u (max %d).",
139140
index, NNS_TENSOR_SIZE_LIMIT);

src/hal-backend-ml-vivante.cc

Lines changed: 71 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -216,54 +216,61 @@ _helper_parse_tensor_attributes (JsonObject *tensor_obj, vsi_nn_tensor_attr_t *v
216216
static int
217217
_json_create_neural_network (vivante_handle_s *self)
218218
{
219-
g_autofree gchar *json_string = NULL;
220-
g_autoptr (GError) err = NULL;
221-
g_autoptr (JsonParser) parser = NULL;
219+
const guint node_num = 1U; /* single NBG node */
220+
const guint const_tensors_num = 0U; /** @todo support this */
221+
222+
gchar *json_string = NULL;
223+
GError *err = NULL;
224+
JsonParser *parser = NULL;
222225
JsonNode *root_node = NULL;
223226
JsonObject *root_obj = NULL;
224227
JsonArray *input_array = NULL, *output_array = NULL;
225228
guint input_tensors_num = 0, output_tensors_num = 0;
229+
guint normal_tensors_num = 0, virtual_tensors_num = 0;
226230
vsi_nn_node_t *node = NULL;
231+
int ret = HAL_ML_ERROR_RUNTIME_ERROR;
227232

228233
self->ctx = vsi_nn_CreateContext ();
229234
if (!self->ctx) {
230235
g_critical ("[vivante] Failed to create VSI context.");
231-
return HAL_ML_ERROR_RUNTIME_ERROR;
236+
goto cleanup;
232237
}
233238

234239
if (!g_file_get_contents (self->json_path, &json_string, NULL, &err)) {
235240
g_critical ("[vivante] Failed to read JSON file '%s': %s", self->json_path,
236241
err ? err->message : "Unknown error");
237-
return HAL_ML_ERROR_IO_ERROR;
242+
ret = HAL_ML_ERROR_IO_ERROR;
243+
goto cleanup;
238244
}
239245

240246
parser = json_parser_new ();
241247
if (!json_parser_load_from_data (parser, json_string, -1, &err)) {
242248
g_critical ("[vivante] Failed to parse JSON: %s", err ? err->message : "Unknown error");
243-
return HAL_ML_ERROR_INVALID_PARAMETER;
249+
ret = HAL_ML_ERROR_INVALID_PARAMETER;
250+
goto cleanup;
244251
}
245252

246253
root_node = json_parser_get_root (parser);
247254
if (!root_node || !JSON_NODE_HOLDS_OBJECT (root_node)) {
248255
g_critical ("[vivante] JSON root is not a valid object.");
249-
return HAL_ML_ERROR_INVALID_PARAMETER;
256+
ret = HAL_ML_ERROR_INVALID_PARAMETER;
257+
goto cleanup;
250258
}
251259
root_obj = json_node_get_object (root_node);
252260

253261
input_array = json_object_get_array_member (root_obj, "input_tensors");
254262
output_array = json_object_get_array_member (root_obj, "output_tensors");
255263
if (!input_array || !output_array) {
256264
g_critical ("[vivante] JSON must contain 'input_tensors' and 'output_tensors' arrays.");
257-
return HAL_ML_ERROR_INVALID_PARAMETER;
265+
ret = HAL_ML_ERROR_INVALID_PARAMETER;
266+
goto cleanup;
258267
}
259268

260269
input_tensors_num = json_array_get_length (input_array);
261270
output_tensors_num = json_array_get_length (output_array);
262271

263-
const guint node_num = 1U; /* single NBG node */
264-
const guint const_tensors_num = 0U; /** @todo support this */
265-
const guint normal_tensors_num = input_tensors_num + output_tensors_num;
266-
const guint virtual_tensors_num = output_tensors_num;
272+
normal_tensors_num = input_tensors_num + output_tensors_num;
273+
virtual_tensors_num = output_tensors_num;
267274

268275
self->graph = vsi_nn_CreateGraph (self->ctx,
269276
normal_tensors_num + virtual_tensors_num + const_tensors_num, node_num);
@@ -313,7 +320,7 @@ _json_create_neural_network (vivante_handle_s *self)
313320
}
314321

315322
for (guint i = 0; i < input_tensors_num; ++i) {
316-
g_info ("[vivante] print Input tensor #%u:", self->graph->input.tensors[i]);
323+
g_info ("[vivante] Print input tensor #%u (%u):", i, self->graph->input.tensors[i]);
317324
vsi_nn_tensor_t *tensor
318325
= vsi_nn_GetTensor (self->graph, self->graph->input.tensors[i]);
319326
vsi_nn_PrintTensor (tensor, self->graph->input.tensors[i]);
@@ -344,7 +351,7 @@ _json_create_neural_network (vivante_handle_s *self)
344351
}
345352

346353
for (guint i = 0; i < output_tensors_num; ++i) {
347-
g_info ("[vivante] print output tensor #%u:", self->graph->output.tensors[i]);
354+
g_info ("[vivante] Print output tensor #%u (%u):", i, self->graph->output.tensors[i]);
348355
vsi_nn_tensor_t *tensor
349356
= vsi_nn_GetTensor (self->graph, self->graph->output.tensors[i]);
350357
vsi_nn_PrintTensor (tensor, self->graph->output.tensors[i]);
@@ -356,11 +363,16 @@ _json_create_neural_network (vivante_handle_s *self)
356363
goto cleanup;
357364
}
358365

359-
return HAL_ML_ERROR_NONE;
366+
ret = HAL_ML_ERROR_NONE;
360367

361368
cleanup:
362-
_json_release_neural_network (self);
363-
return HAL_ML_ERROR_RUNTIME_ERROR;
369+
if (ret != HAL_ML_ERROR_NONE)
370+
_json_release_neural_network (self);
371+
372+
g_clear_error (&err);
373+
g_clear_pointer (&json_string, g_free);
374+
g_clear_object (&parser);
375+
return ret;
364376
}
365377

366378
/** @brief Releases all resources associated with a JSON-based graph. */
@@ -424,35 +436,23 @@ _so_create_neural_network (vivante_handle_s *self)
424436
return HAL_ML_ERROR_NONE;
425437
}
426438

427-
/* ===================================================================
428-
* Main HAL Implementation Functions
429-
* ===================================================================
430-
*/
431-
static int
432-
ml_vivante_init (void **backend_private)
439+
/** @brief Initialize handle. */
440+
static void
441+
_init_vivante_handle (vivante_handle_s *vivante)
433442
{
434-
vivante_handle_s *vivante = g_new0 (vivante_handle_s, 1);
443+
memset (vivante, 0, sizeof (vivante_handle_s));
435444

436445
gst_tensors_info_init (&vivante->inputInfo);
437446
gst_tensors_info_init (&vivante->outputInfo);
438447

439448
vivante->use_json_for_graph = TRUE;
440449
vivante->has_post_process = FALSE;
441-
442-
*backend_private = vivante;
443-
return HAL_ML_ERROR_NONE;
444450
}
445451

446-
static int
447-
ml_vivante_deinit (void *backend_private)
452+
/** @brief Close model and clear internal data in handle. */
453+
static void
454+
_clear_vivante_handle (vivante_handle_s *vivante)
448455
{
449-
vivante_handle_s *vivante = (vivante_handle_s *) backend_private;
450-
451-
if (!vivante) {
452-
g_critical ("[vivante] invalid backend_private");
453-
return HAL_ML_ERROR_INVALID_PARAMETER;
454-
}
455-
456456
if (vivante->use_json_for_graph) {
457457
_json_release_neural_network (vivante);
458458
} else {
@@ -470,6 +470,36 @@ ml_vivante_deinit (void *backend_private)
470470
g_free (vivante->model_path);
471471
g_free (vivante->json_path);
472472
g_free (vivante->so_path);
473+
474+
_init_vivante_handle (vivante);
475+
}
476+
477+
/* ===================================================================
478+
* Main HAL Implementation Functions
479+
* ===================================================================
480+
*/
481+
static int
482+
ml_vivante_init (void **backend_private)
483+
{
484+
vivante_handle_s *vivante = g_new0 (vivante_handle_s, 1);
485+
486+
_init_vivante_handle (vivante);
487+
488+
*backend_private = vivante;
489+
return HAL_ML_ERROR_NONE;
490+
}
491+
492+
static int
493+
ml_vivante_deinit (void *backend_private)
494+
{
495+
vivante_handle_s *vivante = (vivante_handle_s *) backend_private;
496+
497+
if (!vivante) {
498+
g_critical ("[vivante] invalid backend_private");
499+
return HAL_ML_ERROR_INVALID_PARAMETER;
500+
}
501+
502+
_clear_vivante_handle (vivante);
473503
g_free (vivante);
474504

475505
return HAL_ML_ERROR_NONE;
@@ -486,6 +516,11 @@ ml_vivante_configure_instance (void *backend_private, const void *prop_)
486516
return HAL_ML_ERROR_INVALID_PARAMETER;
487517
}
488518

519+
if (vivante->model_path) {
520+
g_critical ("[vivante] invalid state, clear old data.");
521+
_clear_vivante_handle (vivante);
522+
}
523+
489524
vivante->model_path = g_strdup (prop->model_files[0]);
490525
// Default loading strategy: if more than one model file is given, assume .so loading.
491526
if (prop->num_models > 1) {

0 commit comments

Comments
 (0)