[Svace] Fix svace defects reported by ahub service
authorDongju Chae <dongju.chae@samsung.com>
Thu, 9 Apr 2020 08:39:51 +0000 (17:39 +0900)
committer채동주/On-Device Lab(SR)/Staff Engineer/삼성전자 <dongju.chae@samsung.com>
Fri, 10 Apr 2020 05:55:58 +0000 (14:55 +0900)
This patch fixes save defects reported by ahub service

Signed-off-by: Dongju Chae <dongju.chae@samsung.com>
12 files changed:
include/common/npubinfmt.h
include/host/libnpuhost.h
src/core/ne-handler.cc
src/core/ne-hwmem.cc
src/core/utils/ne-conf.cc
tests/apptests/dummy_inference.c
tests/unittests/ne_core_comm_test.cc
tests/unittests/ne_core_npu_test.cc
tests/unittests/ne_core_thread_pool_test.cc
tests/unittests/ne_core_utils_test.cc
tests/unittests/ne_unittest_utils.h [new file with mode: 0644]
tests/utils/ne_test_utils.c

index 8644412..75e49e9 100644 (file)
@@ -22,7 +22,7 @@
 
 /* size of metadata */
 #define NPUBIN_META_SIZE (4096) /* unit size of metadata (minimal size) */
-#define NPUBIN_META_EXTRA(magiccode) ((magiccode >> 8) & 0xFFULL) /* number of extra metadata */
+#define NPUBIN_META_EXTRA(magiccode) ((uint64_t)(((magiccode) >> 8) & 0xFFULL)) /* number of extra metadata */
 #define NPUBIN_META_EXTRA_SIZE(magiccode) (NPUBIN_META_EXTRA(magiccode) * NPUBIN_META_SIZE)
 #define NPUBIN_META_TOTAL_SIZE(magiccode) (NPUBIN_META_SIZE + NPUBIN_META_EXTRA_SIZE(magiccode))
 
index 9509fa4..0fbf56f 100644 (file)
@@ -107,34 +107,20 @@ typedef struct {
 typedef void *npudev_h;
 
 /**
- * @brief Returns the list of NPU devices.
+ * @brief Returns the number of available NPU devices.
  * @return @c The number of NPU devices.
  * @retval 0 if no NPU devices available. if positive (number of NPUs) if NPU devices available. otherwise, a negative error value.
- */
-int getnumNPUdevice(void);
+*/
+int getnumNPUdeviceByType (dev_type type);
 
 /**
  * @brief Returns the handle of the chosen NPU devices.
  * @param[out] dev The NPU device handle
- * @param[in] id The NPU id to get the handle. 0 <= id < getNumNPUDevices().
+ * @param[in] id The NPU id to get the handle. 0 <= id < getnumNPUdeviceByType().
  * @return @c 0 if no error. otherwise a negative error value
+ * @note the caller should call putNPUdevice() to release the device handle
  */
-int getNPUdevice(npudev_h *dev, uint32_t id);
-
-/**
- * @brief Returns the list of ASR devices
- * @return @c The number of ASR devices.
- * @retval 0 if no ASR devices available. if positive (number of ASRs) if ASR devices available. otherwise, a negative error value.
- */
-int getnumASRdevice(void);
-
-/**
- * @brief Returns the handle of the chosen ASR device.
- * @param[out] dev The ASR device handle
- * @param[in] id The ASRi-dev id to get the handle. 0 <= id < getNumASRDevices().
- * @return @c 0 if no error. otherwise a negative error value
- */
-int getASRdevice(npudev_h *dev, uint32_t id);
+int getNPUdeviceByType (npudev_h *dev, dev_type type, uint32_t id);
 
 /**
  * @brief Description of npu device search conditions.
@@ -161,10 +147,18 @@ typedef struct {
  * @param[out] dev The NPU device handle
  * @param[in] cond The condition for device search.
  * @return @c 0 if no error. otherwise a negative error value
+ * @note the caller should call putNPUdevice() to release the device handle
+ * @note it's not supported yet
  */
 int getNPUdeviceByCondition(npudev_h *dev, const npucondition *cond);
 
 /**
+ * @brief release the NPU device instance obtained by getDevice ()
+ * @param[in] dev the NPU device handle
+ */
+void putNPUdevice (npudev_h dev);
+
+/**
  * @brief Get metadata for NPU model
  * @param[in] model The path of model binary file
  * @param[in] need_extra whether you want to extract the extra data in metadata
@@ -443,6 +437,42 @@ int cleanNPU_inputBuffers (npudev_h dev, input_buffers * input);
 
 /** deprecated buffer APIs; please use the above APIs */
 
+/**
+ * @deprecated
+ * @brief Returns the list of NPU devices.
+ * @return @c The number of NPU devices.
+ * @retval 0 if no NPU devices available. if positive (number of NPUs) if NPU devices available. otherwise, a negative error value.
+ */
+int getnumNPUdevice(void);
+
+/**
+ * @deprecated
+ * @brief Returns the handle of the chosen NPU devices.
+ * @param[out] dev The NPU device handle
+ * @param[in] id The NPU id to get the handle. 0 <= id < getNumNPUDevices().
+ * @return @c 0 if no error. otherwise a negative error value
+ * @note the caller should call putNPUdevice() to release the device handle
+ */
+int getNPUdevice(npudev_h *dev, uint32_t id);
+
+/**
+ * @deprecated
+ * @brief Returns the list of ASR devices
+ * @return @c The number of ASR devices.
+ * @retval 0 if no ASR devices available. if positive (number of ASRs) if ASR devices available. otherwise, a negative error value.
+ */
+int getnumASRdevice(void);
+
+/**
+ * @deprecated
+ * @brief Returns the handle of the chosen ASR device.
+ * @param[out] dev The ASR device handle
+ * @param[in] id The ASRi-dev id to get the handle. 0 <= id < getNumASRDevices().
+ * @return @c 0 if no error. otherwise a negative error value
+ * @note the caller should call putNPUdevice() to release the device handle
+ */
+int getASRdevice(npudev_h *dev, uint32_t id);
+
 /** @brief deprecated */
 int allocModelBuffer (generic_buffer *buffer);
 /** @brief deprecated */
index fc6769c..816887f 100644 (file)
@@ -113,6 +113,7 @@ class Device {
 class TrinityVision : public Device {
   public:
     TrinityVision (int id) : Device (NPUCOND_TRIV_CONN_SOCIP, id) {}
+    ~TrinityVision () {}
 
     static size_t manipulateData (const Model *model, uint32_t idx, bool is_input,
         void *dst, void *src, size_t size);
@@ -257,6 +258,7 @@ delete_exit:
 class TrinityVision2 : public Device {
   public:
     TrinityVision2 (int id) : Device (NPUCOND_TRIV2_CONN_SOCIP, id) {}
+    ~TrinityVision2 () {}
 
     static size_t manipulateData (const Model *model, uint32_t idx, bool is_input,
         void *dst, void *src, size_t size) {
@@ -272,7 +274,7 @@ class TrinityVision2 : public Device {
     int run (npu_input_opmode opmode, const Model *model,
         const input_buffers *input, npuOutputNotify cb, void *cb_data,
         uint64_t *sequence) {
-      if (opmode != NPUINPUT_HOST || opmode != NPUINPUT_HW_RECURRING)
+      if (opmode != NPUINPUT_HOST && opmode != NPUINPUT_HW_RECURRING)
         return -EINVAL;
 
       /** this device uses segment table */
@@ -298,6 +300,7 @@ class TrinityVision2 : public Device {
 class TrinityAsr : public Device {
   public:
     TrinityAsr (int id) : Device (NPUCOND_TRIA_CONN_SOCIP, id, false) {}
+    ~TrinityAsr () {}
 
     static size_t manipulateData (const Model *model, uint32_t idx, bool is_input,
         void *dst, void *src, size_t size) {
@@ -848,9 +851,14 @@ HostHandler::getDevice (npudev_h *dev, dev_type type, uint32_t id)
 int
 HostHandler::allocGenericBuffer (generic_buffer *buffer)
 {
-  if (buffer == NULL || SIZE_MAX < buffer->size)
+  if (buffer == NULL)
     return -EINVAL;
 
+  if (buffer->size > UINT32_MAX) {
+    logerr (TAG, "Don't support such a large size");
+    return -ENOMEM;
+  }
+
   if (buffer->type == BUFFER_FILE) {
     /* nothing to do */
     if (buffer->filepath == nullptr)
@@ -945,33 +953,19 @@ npudev_h HostHandler::latest_dev_ = nullptr;
  * @brief Returns the number of available NPU devices.
  * @return @c The number of NPU devices.
  * @retval 0 if no NPU devices available. if positive (number of NPUs) if NPU devices available. otherwise, a negative error value.
-*/
+ * @note the caller should call putNPUdevice() to release the device handle
+ */
 int getnumNPUdeviceByType (dev_type type)
 {
   return HostHandler::getNumDevices (type);
 }
 
 /**
- * @brief Returns the number of NPU devices (TRIV).
- */
-int getnumNPUdevice (void)
-{
-  return getnumNPUdeviceByType (NPUCOND_TRIV_CONN_SOCIP);
-}
-
-/**
- * @brief Returns the list of ASR devices (TRIA)
- */
-int getnumASRdevice (void)
-{
-  return getnumNPUdeviceByType (NPUCOND_TRIA_CONN_SOCIP);
-}
-
-/**
  * @brief Returns the handle of the chosen NPU devices.
  * @param[out] dev The NPU device handle
  * @param[in] id The NPU id to get the handle. 0 <= id < getnumNPUdeviceByType().
  * @return @c 0 if no error. otherwise a negative error value
+ * @note the caller should call putNPUdevice() to release the device handle
  */
 int getNPUdeviceByType (npudev_h *dev, dev_type type, uint32_t id)
 {
@@ -979,19 +973,27 @@ int getNPUdeviceByType (npudev_h *dev, dev_type type, uint32_t id)
 }
 
 /**
- * @brief Returns the handle of the chosen TRIV device.
+ * @brief Returns the handle of an NPU device meeting the condition
+ * @param[out] dev The NPU device handle
+ * @param[in] cond The condition for device search.
+ * @return @c 0 if no error. otherwise a negative error value
+ * @note the caller should call putNPUdevice() to release the device handle
+ * @note it's not supported yet
  */
-int getNPUdevice (npudev_h *dev, uint32_t id)
+int getNPUdeviceByCondition(npudev_h *dev, const npucondition *cond)
 {
-  return getNPUdeviceByType (dev, NPUCOND_TRIV_CONN_SOCIP, id);
+  /** not implmeneted yet */
+  return getNPUdeviceByType (dev, NPUCOND_TRIV_CONN_SOCIP, 0);
 }
 
 /**
- * @brief Returns the handle of the chosen TRIA device.
+ * @brief release the NPU device instance obtained by getDevice ()
+ * @param[in] dev the NPU device handle
  */
-int getASRdevice (npudev_h *dev, uint32_t id)
+void putNPUdevice (npudev_h dev)
 {
-  return getNPUdeviceByType (dev, NPUCOND_TRIA_CONN_SOCIP, id);
+  if (dev != nullptr)
+    delete static_cast<Device *> (dev);
 }
 
 /**
@@ -1272,6 +1274,42 @@ exit_err:
 
 /** deprecated buffer APIs; please use the above APIs */
 
+/**
+ * @brief Returns the number of NPU devices (TRIV).
+ */
+int getnumNPUdevice (void)
+{
+  logwarn (TAG, "deprecated. Please use getnumNPUdeviceByType ()\n");
+  return getnumNPUdeviceByType (NPUCOND_TRIV_CONN_SOCIP);
+}
+
+/**
+ * @brief Returns the list of ASR devices (TRIA)
+ */
+int getnumASRdevice (void)
+{
+  logwarn (TAG, "deprecated. Please use getnumNPUdeviceByType ()\n");
+  return getnumNPUdeviceByType (NPUCOND_TRIA_CONN_SOCIP);
+}
+
+/**
+ * @brief Returns the handle of the chosen TRIV device.
+ */
+int getNPUdevice (npudev_h *dev, uint32_t id)
+{
+  logwarn (TAG, "deprecated. Please use getNPUdeviceByType ()\n");
+  return getNPUdeviceByType (dev, NPUCOND_TRIV_CONN_SOCIP, id);
+}
+
+/**
+ * @brief Returns the handle of the chosen TRIA device.
+ */
+int getASRdevice (npudev_h *dev, uint32_t id)
+{
+  logwarn (TAG, "deprecated. Please use getNPUdeviceByType ()\n");
+  return getNPUdeviceByType (dev, NPUCOND_TRIA_CONN_SOCIP, id);
+}
+
 /** @brief deprecated */
 int allocModelBuffer (generic_buffer *buffer)
 {
index b096fa8..be290f5 100644 (file)
@@ -15,7 +15,7 @@
 
 /** @brief HWmem constructor */
 HWmem::HWmem (const HWmemImpl* impl)
-  : impl_ (impl), dmabuf_ (-1), base_addr_ (nullptr),
+  : impl_ (impl), api_ (nullptr), dmabuf_ (-1), base_addr_ (nullptr),
     offset_ (0), size_ (0), parent_ (nullptr)
 {
 }
index 1caf36c..64f3522 100644 (file)
@@ -130,10 +130,9 @@ Conf::setResvMemSize (const char *resv_mem_size)
           val *= 1024 * 1024 * 1024;
         }
       }
-      if (val <= SIZE_MAX) {
-        /** zero size is now allowed to indicate the KERNEL_CMA mode */
-        reserved_mem_size_ = static_cast<size_t> (val);
-      }
+
+      /** zero size is now allowed to indicate the KERNEL_CMA mode */
+      reserved_mem_size_ = static_cast<size_t> (val);
     }
   }
 }
index 0f64698..ba79f6c 100644 (file)
@@ -134,7 +134,7 @@ main (int argc, char **argv)
       } else {
         fprintf(stderr, "[APPTEST] %s: FAILED (%d)\n", argv[0], result);
       }
-      free (dev);
+      putNPUdevice (dev);
     }
   }
 
index d706f1c..6dc10c7 100644 (file)
@@ -15,6 +15,8 @@
 
 #include <fstream>
 
+#include "ne_unittest_utils.h"
+
 /**
  * @brief Test getCommPlugin()
  */
@@ -265,7 +267,5 @@ TEST (ne_core_comm_test, insert_buffer_not_supported_n)
 int
 main (int argc, char **argv)
 {
-  testing::InitGoogleTest (&argc, argv);
-
-  return RUN_ALL_TESTS();
+  return start_gtest (argc, argv);
 }
index d9b44d3..98b3646 100644 (file)
@@ -16,6 +16,8 @@
 #include <gtest/gtest.h>
 #include <NPUdrvAPI.h>
 
+#include "ne_unittest_utils.h"
+
 /**
  * @brief check getNumDevices()
  */
@@ -275,7 +277,5 @@ TEST (ne_core_npu_test, run_inference)
 int
 main (int argc, char **argv)
 {
-  testing::InitGoogleTest (&argc, argv);
-
-  return RUN_ALL_TESTS();
+  return start_gtest (argc, argv);
 }
index 3b37b8d..6d3b7ec 100644 (file)
@@ -14,6 +14,8 @@
 #include <gtest/gtest.h>
 #include <ne-thread-pool.h>
 
+#include "ne_unittest_utils.h"
+
 /**
  * @brief test getInstance()
  */
@@ -182,7 +184,5 @@ TEST (ne_core_thread_pool_test, remove_task_resolved_n)
 int
 main (int argc, char **argv)
 {
-  testing::InitGoogleTest (&argc, argv);
-
-  return RUN_ALL_TESTS();
+  return start_gtest (argc, argv);
 }
index 1ae94dd..fbd8303 100644 (file)
 #include <gtest/gtest.h>
 #include <poll.h>
 
-/** below are internal C-headers */
+#include <fstream>
+
 #include <ne-utils.h>
 #include <ne-conf.h>
 
+#include "ne_unittest_utils.h"
+
 typedef std::vector<std::pair<const char *, char *>> ENV_PAIR;
 /**
  * @brief save and unset enviroment variables
@@ -93,16 +96,15 @@ TEST (ne_core_utils_test, load_conf_ini)
   save_and_unset_envvars (envvars);
 
   /** make dummy .ini file */
-  FILE *fp = fopen ("dummy.ini", "w");
-  const char *ini_str = "[main]\n"
-                        "resv_mem_size=128k\n"
-                        "num_threads=4\n"
-                        "log_dir=/tmp/test_log\n";
+  std::ofstream ofs ("dummy.ini");
+  ASSERT_EQ (ofs.good(), true);
 
-  /** from .ini file */
-  ASSERT_NE (fp, nullptr);
-  ASSERT_EQ (fwrite (ini_str, strlen(ini_str), 1, fp), 1);
-  ASSERT_EQ (fclose (fp), 0);
+  ofs << "[main]\n";
+  ofs << "resv_mem_size=128k\n";
+  ofs << "num_threads=4\n";
+  ofs << "log_dir=/tmp/test_log\n";
+
+  ofs.close();
 
   Conf & conf = Conf::getInstance ("dummy.ini");
 
@@ -309,7 +311,5 @@ TEST (ne_core_utils_test, log_write_n)
 int
 main (int argc, char **argv)
 {
-  testing::InitGoogleTest (&argc, argv);
-
-  return RUN_ALL_TESTS();
+  return start_gtest (argc, argv);
 }
diff --git a/tests/unittests/ne_unittest_utils.h b/tests/unittests/ne_unittest_utils.h
new file mode 100644 (file)
index 0000000..809bc40
--- /dev/null
@@ -0,0 +1,36 @@
+/**
+ * Proprietary
+ * Copyright (C) 2020 Samsung Electronics
+ * Copyright (C) 2020 Dongju Chae <dongju.chae@samsung.com>
+ */
+/**
+ * @file ne_unittest_utils.h
+ * @date 09 Apr 2020
+ * @brief Utility header for UnitTests
+ * @author Dongju Chae <dongju.chae@samsung.com>
+ */
+
+#ifndef _NE_UNITTEST_UTILS_H_
+#define _NE_UNITTEST_UTILS_H_
+
+#include <gtest/gtest.h>
+
+/**
+ * @brief start gtest with tricky exception handling (due to svace)
+ */
+inline int start_gtest (int argc, char **argv) {
+  int ret = -1;
+  try {
+    testing::InitGoogleTest (&argc, argv);
+  } catch (...) {
+    std::cerr << "catch 'testing::internal::<unnamed>::ClassUniqueToAlwaysTrue'" << std::endl;
+  }
+  try {
+    ret = RUN_ALL_TESTS();
+  } catch (...) {
+    std::cerr << "catch `testing::internal::GoogleTestFailureException`" << std::endl;
+  }
+  return ret;
+}
+
+#endif /* _NE_UNITTEST_UTILS_H_ */
index a007089..e9ea68d 100644 (file)
@@ -180,7 +180,7 @@ int run_apptest (npudev_h dev, char **argv, FUNC_APPTEST func)
   }
 
   closedir(d);
-  free (dev);
+  putNPUdevice (dev);
 
   fprintf(stderr, "[APPTEST] %s: %s (%d/%d)\n",
       name, total_fails == 0 ? "PASSED" : "FAILED",