You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nuttx.apache.org by xi...@apache.org on 2024/09/13 15:14:13 UTC

(nuttx-apps) branch master updated (7cd9919a4 -> 7c94d13be)

This is an automated email from the ASF dual-hosted git repository.

xiaoxiang pushed a change to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx-apps.git


    from 7cd9919a4 testing/crypto: add rsa testcase
     new 85ad50c39 testing: refactor kasan test
     new 797fd0479 kasantest: Fix errors running on 64 bit
     new 277c968b6 kasantest: Add the ability to run a single program
     new f2c21b11d kasantest: Add a test set for global variable out of bounds detection
     new 7c94d13be kasantest: Add performance testing for Kasan, including pile insertion testing and algorithm testing

The 5 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 testing/kasantest/Kconfig     |  20 ++
 testing/kasantest/Makefile    |   7 +-
 testing/kasantest/kasantest.c | 417 +++++++++++++++++++++++++++++++++---------
 3 files changed, 357 insertions(+), 87 deletions(-)


(nuttx-apps) 04/05: kasantest: Add a test set for global variable out of bounds detection

Posted by xi...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

xiaoxiang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx-apps.git

commit f2c21b11d83a74a7f0dac5270a9bc22fcd21c3f7
Author: wangmingrong1 <wa...@xiaomi.com>
AuthorDate: Fri Sep 6 11:14:28 2024 +0800

    kasantest: Add a test set for global variable out of bounds detection
    
    Signed-off-by: wangmingrong1 <wa...@xiaomi.com>
---
 testing/kasantest/Makefile    |  1 +
 testing/kasantest/kasantest.c | 29 ++++++++++++++++++++++++++++-
 2 files changed, 29 insertions(+), 1 deletion(-)

diff --git a/testing/kasantest/Makefile b/testing/kasantest/Makefile
index 1f5f02936..a17edcac3 100644
--- a/testing/kasantest/Makefile
+++ b/testing/kasantest/Makefile
@@ -29,5 +29,6 @@ STACKSIZE = $(CONFIG_DEFAULT_TASK_STACKSIZE)
 
 CFLAGS += -Wno-error -Wno-use-after-free
 CFLAGS += -Wno-array-bounds -Wno-free-nonheap-object
+CFLAGS += -Wno-stringop-overflow
 
 include $(APPDIR)/Application.mk
diff --git a/testing/kasantest/kasantest.c b/testing/kasantest/kasantest.c
index e264e7055..b99c8d861 100644
--- a/testing/kasantest/kasantest.c
+++ b/testing/kasantest/kasantest.c
@@ -72,6 +72,11 @@ static bool test_heap_memset(FAR struct mm_heap_s *heap, size_t size);
 static bool test_heap_memcpy(FAR struct mm_heap_s *heap, size_t size);
 static bool test_heap_memmove(FAR struct mm_heap_s *heap, size_t size);
 
+#ifdef CONFIG_MM_KASAN_GLOBAL
+static bool test_global_underflow(FAR struct mm_heap_s *heap, size_t size);
+static bool test_global_overflow(FAR struct mm_heap_s *heap, size_t size);
+#endif
+
 /****************************************************************************
  * Private Data
  ****************************************************************************/
@@ -87,11 +92,19 @@ const static testcase_t g_kasan_test[] =
   {test_heap_unpoison, "heap unpoison"},
   {test_heap_memset, "heap memset"},
   {test_heap_memcpy, "heap memcpy"},
-  {test_heap_memmove, "heap memmove"}
+  {test_heap_memmove, "heap memmove"},
+#ifdef CONFIG_MM_KASAN_GLOBAL
+  {test_global_underflow, "globals underflow"},
+  {test_global_overflow, "globals overflow"},
+#endif
 };
 
 static char g_kasan_heap[65536] aligned_data(8);
 
+#ifdef CONFIG_MM_KASAN_GLOBAL
+static char g_kasan_globals[32];
+#endif
+
 /****************************************************************************
  * Private Functions
  ****************************************************************************/
@@ -210,6 +223,20 @@ static bool test_heap_memmove(FAR struct mm_heap_s *heap, size_t size)
   return false;
 }
 
+#ifdef CONFIG_MM_KASAN_GLOBAL
+static bool test_global_underflow(FAR struct mm_heap_s *heap, size_t size)
+{
+  memset(g_kasan_globals - 31, 0x12, sizeof(g_kasan_globals));
+  return false;
+}
+
+static bool test_global_overflow(FAR struct mm_heap_s *heap, size_t size)
+{
+  memset(g_kasan_globals, 0xef, sizeof(g_kasan_globals) + 31);
+  return false;
+}
+#endif
+
 static int run_test(FAR const testcase_t *test)
 {
   size_t heap_size = 65536;


(nuttx-apps) 01/05: testing: refactor kasan test

Posted by xi...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

xiaoxiang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx-apps.git

commit 85ad50c39e0b67bf6529ec85387553961815423b
Author: yinshengkai <yi...@xiaomi.com>
AuthorDate: Mon Apr 22 18:44:20 2024 +0800

    testing: refactor kasan test
    
    Signed-off-by: yinshengkai <yi...@xiaomi.com>
---
 testing/kasantest/kasantest.c | 276 ++++++++++++++++++++++++++++--------------
 1 file changed, 187 insertions(+), 89 deletions(-)

diff --git a/testing/kasantest/kasantest.c b/testing/kasantest/kasantest.c
index e3d4a6a28..171773337 100644
--- a/testing/kasantest/kasantest.c
+++ b/testing/kasantest/kasantest.c
@@ -22,154 +22,252 @@
  * Included Files
  ****************************************************************************/
 
-#include <nuttx/config.h>
-#include <nuttx/mm/kasan.h>
-
+#include <assert.h>
+#include <malloc.h>
+#include <pthread.h>
+#include <stdint.h>
 #include <stdio.h>
+#include <stdlib.h>
+#include <sys/types.h>
 #include <syslog.h>
-#include <pthread.h>
+
+#include <sys/param.h>
+#include <sys/wait.h>
+
+#include <nuttx/fs/procfs.h>
+#include <nuttx/mm/mm.h>
+#include <nuttx/mm/kasan.h>
+
+/****************************************************************************
+ * Private Types Prototypes
+ ****************************************************************************/
+
+typedef struct testcase_s
+{
+  bool (*func)(FAR struct mm_heap_s *heap, size_t size);
+  FAR const char *name;
+} testcase_t;
+
+typedef struct run_s
+{
+  char argv[16];
+  FAR const testcase_t *testcase;
+  FAR struct mm_heap_s *heap;
+  size_t size;
+} run_t;
 
 /****************************************************************************
- * Pre-processor Definitions
+ * Private Function Prototypes
  ****************************************************************************/
 
-#define KASAN_TEST_MEM_SIZE 128
+static bool test_heap_underflow(FAR struct mm_heap_s *heap, size_t size);
+static bool test_heap_overflow(FAR struct mm_heap_s *heap, size_t size);
+static bool test_heap_use_after_free(FAR struct mm_heap_s *heap,
+                                     size_t size);
+static bool test_heap_invalid_free(FAR struct mm_heap_s *heap, size_t size);
+static bool test_heap_double_free(FAR struct mm_heap_s *heap, size_t size);
+static bool test_heap_poison(FAR struct mm_heap_s *heap, size_t size);
+static bool test_heap_unpoison(FAR struct mm_heap_s *heap, size_t size);
+static bool test_heap_memset(FAR struct mm_heap_s *heap, size_t size);
+static bool test_heap_memcpy(FAR struct mm_heap_s *heap, size_t size);
+static bool test_heap_memmove(FAR struct mm_heap_s *heap, size_t size);
 
 /****************************************************************************
  * Private Data
  ****************************************************************************/
 
-static char g_kasan_test_buffer[KASAN_TEST_MEM_SIZE];
+const static testcase_t g_kasan_test[] =
+{
+  {test_heap_underflow, "heap underflow"},
+  {test_heap_overflow, "heap overflow"},
+  {test_heap_use_after_free, "heap use after free"},
+  {test_heap_invalid_free, "heap inval free"},
+  {test_heap_double_free, "test heap double free"},
+  {test_heap_poison, "heap poison"},
+  {test_heap_unpoison, "heap unpoison"},
+  {test_heap_memset, "heap memset"},
+  {test_heap_memcpy, "heap memcpy"},
+  {test_heap_memmove, "heap memmove"}
+};
 
 /****************************************************************************
  * Private Functions
  ****************************************************************************/
 
-static void kasan_test(char *p, size_t size)
+static bool test_heap_underflow(FAR struct mm_heap_s *heap, size_t size)
 {
-  size_t i;
+  FAR uint8_t *mem = mm_malloc(heap, size);
+  *(mem - 1) = 0x12;
+  return false;
+}
 
-  for (i = 0; i < size + 64; i++)
-    {
-      syslog(LOG_SYSLOG,
-             "Access Buffer[%zu] : %zu address: %p", size, i, &p[i]);
-      p[i]++;
-      syslog(LOG_SYSLOG, "read: %02x -- Successful\n", p[i]);
-    }
+static bool test_heap_overflow(FAR struct mm_heap_s *heap, size_t size)
+{
+  FAR uint8_t *mem = mm_malloc(heap, size);
+  size = mm_malloc_size(heap, mem);
+
+  mem[size + 1] = 0x11;
+  return false;
 }
 
-static void kasan_test_use_after_free(void)
+static bool test_heap_use_after_free(FAR struct mm_heap_s *heap, size_t size)
 {
-  char *ptr = malloc(KASAN_TEST_MEM_SIZE);
+  FAR uint8_t *mem = mm_malloc(heap, size);
 
-  if (ptr == NULL)
-    {
-      syslog(LOG_SYSLOG, "Failed to allocate memory\n");
-      return;
-    }
+  mm_free(heap, mem);
+  mem[0] = 0x10;
+  return 0;
+}
 
-  syslog(LOG_SYSLOG, "KASan test use after free\n");
-  strcpy(ptr, "kasan test use after free");
-  free(ptr);
-  printf("%s\n", ptr);
+static bool test_heap_invalid_free(FAR struct mm_heap_s *heap, size_t size)
+{
+  int x;
+  mm_free(heap, &x);
+  return false;
 }
 
-static void kasan_test_heap_memory_out_of_bounds(char *str)
+static bool test_heap_double_free(FAR struct mm_heap_s *heap, size_t size)
 {
-  char  *endptr;
-  size_t size;
-  char  *ptr;
+  uint8_t *mem = mm_malloc(heap, size);
 
-  size = strtoul(str, &endptr, 0);
-  if (*endptr != '\0')
-    {
-      printf("Conversion failed: Not a valid integer.\n");
-      return;
-    }
+  mm_free(heap, mem);
+  mm_free(heap, mem);
+  return false;
+}
 
-  ptr = zalloc(size);
-  if (ptr == NULL)
-    {
-      syslog(LOG_SYSLOG, "Failed to allocate memory\n");
-      return;
-    }
+static bool test_heap_poison(FAR struct mm_heap_s *heap, size_t size)
+{
+  FAR uint8_t *mem = mm_malloc(heap, size);
+  size = mm_malloc_size(heap, mem);
 
-  syslog(LOG_SYSLOG,
-         "KASan test accessing heap memory out of bounds completed\n");
-  kasan_test(ptr, size);
+  kasan_poison(mem, size);
+  mem[0] = 0x10;
+  return false;
 }
 
-static void kasan_test_global_variable_out_of_bounds(void)
+static bool test_heap_unpoison(FAR struct mm_heap_s *heap, size_t size)
 {
-  syslog(LOG_SYSLOG,
-         "KASan test accessing global variable out of bounds\n");
-  kasan_test(g_kasan_test_buffer, KASAN_TEST_MEM_SIZE);
+  FAR uint8_t *mem = mm_malloc(heap, size);
+  size_t memsize = mm_malloc_size(heap, mem);
+
+  kasan_poison(mem, memsize);
+  kasan_unpoison(mem, memsize);
+  mem[0] = 0x10;
+  return true;
 }
 
-static void *mm_stampede_thread(void *arg)
+static bool test_heap_memset(FAR struct mm_heap_s *heap, size_t size)
 {
-  char *p = (char *)arg;
+  FAR uint8_t *mem = mm_malloc(heap, size);
+  size = mm_malloc_size(heap, mem);
 
-  syslog(LOG_SYSLOG, "Child thread is running");
-  kasan_test(p, KASAN_TEST_MEM_SIZE);
-  pthread_exit(NULL);
+  memset(mem, 0x11, size + 1);
+  return false;
 }
 
-static void kasan_test_memory_stampede(void)
+static bool test_heap_memcpy(FAR struct mm_heap_s *heap, size_t size)
 {
-  pthread_t thread;
-  char array[KASAN_TEST_MEM_SIZE];
+  FAR uint8_t *src;
+  FAR uint8_t *dst;
 
-  syslog(LOG_SYSLOG, "KASan test accessing memory stampede\n");
-  pthread_create(&thread, NULL, mm_stampede_thread, kasan_reset_tag(&array));
-  pthread_join(thread, NULL);
+  size = size / 2;
+  src =  mm_malloc(heap, size);
+  size = mm_malloc_size(heap, src);
+  dst = mm_malloc(heap, size);
+
+  memcpy(dst, src, size);
+  memcpy(dst, src, size + 4);
+  return false;
 }
 
-/****************************************************************************
- * Public Functions
- ****************************************************************************/
+static bool test_heap_memmove(FAR struct mm_heap_s *heap, size_t size)
+{
+  FAR uint8_t *src;
+  FAR uint8_t *dst;
 
-/****************************************************************************
- * Name: kasantest_main
- ****************************************************************************/
+  size = size / 2;
+  src =  mm_malloc(heap, size);
+  size = mm_malloc_size(heap, src);
+  dst = mm_malloc(heap, size);
+
+  memmove(dst, src, size);
+  memmove(dst, src, size + 4);
+  return false;
+}
+
+static int run_testcase(int argc, FAR char *argv[])
+{
+  FAR run_t *run = (FAR run_t *)(uintptr_t)strtoul(argv[1], NULL, 0);
+  return run->testcase->func(run->heap, run->size);
+}
 
-int main(int argc, char *argv[])
+static int run_test(FAR const testcase_t *test)
 {
-  /* NutttX cannot check the secondary release
-   * because the mm module has closed kasan instrumentation
+  size_t heap_size = 65536;
+  FAR char *argv[3];
+  FAR run_t *run;
+  int status;
+  pid_t pid;
+
+  /* There is a memory leak here because we cannot guarantee that
+   * it can be released correctly.
    */
 
-  if (argc < 2)
+  run = malloc(sizeof(run_t) + heap_size);
+  if (!run)
     {
-      printf("Usage: %s <test_option>\n", argv[0]);
-      printf("Available test options:\n");
-      printf("  -u       : Test use after free\n");
-      printf("  -h <arg> : Test heap memory out of bounds (provide size)\n");
-      printf("  -g       : Test global variable out of bounds\n");
-      printf("  -s       : Test memory stampede\n");
-      return 0;
+      return ERROR;
     }
-  else if (strncmp(argv[1], "-u", 2) == 0)
+
+  snprintf(run->argv, sizeof(run->argv), "%p", run);
+  run->testcase = test;
+  run->size = rand() % (heap_size / 2) + 1;
+  run->heap = mm_initialize("kasan", &run[1], heap_size);
+  if (!run->heap)
     {
-      kasan_test_use_after_free();
+      free(run);
+      return ERROR;
     }
-  else if (strncmp(argv[1], "-h", 2) == 0 && argc == 3)
+
+  argv[0] = "kasantest";
+  argv[1] = run->argv;
+  argv[2] = NULL;
+
+  posix_spawn(&pid, "kasantest", NULL, NULL, argv, NULL);
+  waitpid(pid, &status, 0);
+  if (status == 0)
     {
-      kasan_test_heap_memory_out_of_bounds(argv[2]);
+      printf("KASan test: %s, size: %d FAIL\n", test->name, run->size);
     }
-  else if (strncmp(argv[1], "-g", 2) == 0)
+  else
     {
-      kasan_test_global_variable_out_of_bounds();
+      printf("KASan test: %s, size: %d PASS\n", test->name, run->size);
     }
-  else if (strncmp(argv[1], "-s", 2) == 0)
+
+  return 0;
+}
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+int main(int argc, FAR char *argv[])
+{
+  if (argc < 2)
     {
-      kasan_test_memory_stampede();
+      size_t j;
+      for (j = 0; j < nitems(g_kasan_test); j++)
+        {
+          if (run_test(&g_kasan_test[j]) < 0)
+            {
+              return EXIT_FAILURE;
+            }
+        }
     }
   else
     {
-      printf("Unknown test option: %s\n", argv[1]);
+      return run_testcase(argc, argv);
     }
 
-  syslog(LOG_SYSLOG, "KASan test failed, please check\n");
-  return 0;
+  return EXIT_SUCCESS;
 }


(nuttx-apps) 05/05: kasantest: Add performance testing for Kasan, including pile insertion testing and algorithm testing

Posted by xi...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

xiaoxiang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx-apps.git

commit 7c94d13be44bb5364299b303356bb4dbc55a8db3
Author: wangmingrong1 <wa...@xiaomi.com>
AuthorDate: Fri Sep 6 15:56:36 2024 +0800

    kasantest: Add performance testing for Kasan, including pile insertion testing and algorithm testing
    
    Signed-off-by: wangmingrong1 <wa...@xiaomi.com>
---
 testing/kasantest/Kconfig     |  20 +++++++
 testing/kasantest/Makefile    |   5 +-
 testing/kasantest/kasantest.c | 121 +++++++++++++++++++++++++++++++++++-------
 3 files changed, 124 insertions(+), 22 deletions(-)

diff --git a/testing/kasantest/Kconfig b/testing/kasantest/Kconfig
index 16c7b237e..5c59684c3 100644
--- a/testing/kasantest/Kconfig
+++ b/testing/kasantest/Kconfig
@@ -8,3 +8,23 @@ config TESTING_KASAN
 	default n
 	---help---
 		Enable the KASan validity test
+
+if TESTING_KASAN
+
+config TESTING_KASAN_PRIORITY
+	int "KASan test priority"
+	default 101
+
+config TESTING_KASAN_STACKSIZE
+	int "KASan test stack size"
+	default 8192
+
+config TESTING_KASAN_PERF_HEAP_SIZE
+	int "KASan test heap size"
+	default 1024000
+
+config TESTING_KASAN_PERF_CYCLES
+	int "KASan test performance cycles"
+	default 4096
+
+endif
diff --git a/testing/kasantest/Makefile b/testing/kasantest/Makefile
index a17edcac3..9a84beeee 100644
--- a/testing/kasantest/Makefile
+++ b/testing/kasantest/Makefile
@@ -24,11 +24,12 @@ include $(APPDIR)/Make.defs
 
 MAINSRC   = kasantest.c
 PROGNAME  = kasantest
-PRIORITY  = SCHED_PRIORITY_DEFAULT
-STACKSIZE = $(CONFIG_DEFAULT_TASK_STACKSIZE)
+PRIORITY  = $(CONFIG_TESTING_KASAN_PRIORITY)
+STACKSIZE = $(CONFIG_TESTING_KASAN_STACKSIZE)
 
 CFLAGS += -Wno-error -Wno-use-after-free
 CFLAGS += -Wno-array-bounds -Wno-free-nonheap-object
 CFLAGS += -Wno-stringop-overflow
+CFLAGS += "-O0"
 
 include $(APPDIR)/Application.mk
diff --git a/testing/kasantest/kasantest.c b/testing/kasantest/kasantest.c
index b99c8d861..bfaf846ec 100644
--- a/testing/kasantest/kasantest.c
+++ b/testing/kasantest/kasantest.c
@@ -28,9 +28,10 @@
 #include <stdint.h>
 #include <stdio.h>
 #include <stdlib.h>
-#include <sys/types.h>
 #include <syslog.h>
+#include <time.h>
 
+#include <sys/types.h>
 #include <sys/param.h>
 #include <sys/wait.h>
 
@@ -45,6 +46,7 @@
 typedef struct testcase_s
 {
   bool (*func)(FAR struct mm_heap_s *heap, size_t size);
+  bool is_auto;
   FAR const char *name;
 } testcase_t;
 
@@ -71,6 +73,8 @@ static bool test_heap_unpoison(FAR struct mm_heap_s *heap, size_t size);
 static bool test_heap_memset(FAR struct mm_heap_s *heap, size_t size);
 static bool test_heap_memcpy(FAR struct mm_heap_s *heap, size_t size);
 static bool test_heap_memmove(FAR struct mm_heap_s *heap, size_t size);
+static bool test_insert_perf(FAR struct mm_heap_s *heap, size_t size);
+static bool test_algorithm_perf(FAR struct mm_heap_s *heap, size_t size);
 
 #ifdef CONFIG_MM_KASAN_GLOBAL
 static bool test_global_underflow(FAR struct mm_heap_s *heap, size_t size);
@@ -83,23 +87,25 @@ static bool test_global_overflow(FAR struct mm_heap_s *heap, size_t size);
 
 const static testcase_t g_kasan_test[] =
 {
-  {test_heap_underflow, "heap underflow"},
-  {test_heap_overflow, "heap overflow"},
-  {test_heap_use_after_free, "heap use after free"},
-  {test_heap_invalid_free, "heap inval free"},
-  {test_heap_double_free, "test heap double free"},
-  {test_heap_poison, "heap poison"},
-  {test_heap_unpoison, "heap unpoison"},
-  {test_heap_memset, "heap memset"},
-  {test_heap_memcpy, "heap memcpy"},
-  {test_heap_memmove, "heap memmove"},
+  {test_heap_underflow, true, "heap underflow"},
+  {test_heap_overflow, true, "heap overflow"},
+  {test_heap_use_after_free, true, "heap use after free"},
+  {test_heap_invalid_free, true, "heap inval free"},
+  {test_heap_double_free, true, "test heap double free"},
+  {test_heap_poison, true, "heap poison"},
+  {test_heap_unpoison, true, "heap unpoison"},
+  {test_heap_memset, true, "heap memset"},
+  {test_heap_memcpy, true, "heap memcpy"},
+  {test_heap_memmove, true, "heap memmove"},
+  {test_insert_perf, false, "Kasan insert performance testing"},
+  {test_algorithm_perf, false, "Kasan algorithm performance testing"},
 #ifdef CONFIG_MM_KASAN_GLOBAL
-  {test_global_underflow, "globals underflow"},
-  {test_global_overflow, "globals overflow"},
+  {test_global_underflow, true, "globals underflow"},
+  {test_global_overflow, true, "globals overflow"},
 #endif
 };
 
-static char g_kasan_heap[65536] aligned_data(8);
+static char g_kasan_heap[10240] aligned_data(8);
 
 #ifdef CONFIG_MM_KASAN_GLOBAL
 static char g_kasan_globals[32];
@@ -122,6 +128,20 @@ static void error_handler(void)
     }
 }
 
+static void timespec_sub(struct timespec *dest,
+                         struct timespec *ts1,
+                         struct timespec *ts2)
+{
+  dest->tv_sec = ts1->tv_sec - ts2->tv_sec;
+  dest->tv_nsec = ts1->tv_nsec - ts2->tv_nsec;
+
+  if (dest->tv_nsec < 0)
+    {
+      dest->tv_nsec += 1000000000;
+      dest->tv_sec -= 1;
+    }
+}
+
 static bool test_heap_underflow(FAR struct mm_heap_s *heap, size_t size)
 {
   FAR uint8_t *mem = mm_malloc(heap, size);
@@ -223,6 +243,54 @@ static bool test_heap_memmove(FAR struct mm_heap_s *heap, size_t size)
   return false;
 }
 
+static bool test_insert_perf(FAR struct mm_heap_s *heap, size_t size)
+{
+  int num = 0;
+  char value;
+  char *p;
+  int i;
+
+  p = (char *)malloc(CONFIG_TESTING_KASAN_PERF_HEAP_SIZE);
+  if (!p)
+    {
+      printf("Failed to allocate memory for performance testing\n");
+      return false;
+    }
+
+  do
+    {
+      value = num % INT8_MAX;
+      for (i = 0; i < CONFIG_TESTING_KASAN_PERF_HEAP_SIZE; i++)
+        {
+          p[i] = value;
+        }
+    }
+  while (num++ < CONFIG_TESTING_KASAN_PERF_CYCLES);
+
+  return true;
+}
+
+static bool test_algorithm_perf(FAR struct mm_heap_s *heap, size_t size)
+{
+  int num = 0;
+  char *p;
+
+  p = (char *)malloc(CONFIG_TESTING_KASAN_PERF_HEAP_SIZE);
+  if (!p)
+    {
+      printf("Failed to allocate memory for performance testing\n");
+      return false;
+    }
+
+  do
+    {
+      memset(p, num % INT8_MAX, CONFIG_TESTING_KASAN_PERF_HEAP_SIZE);
+    }
+  while (num++ < CONFIG_TESTING_KASAN_PERF_CYCLES);
+
+  return true;
+}
+
 #ifdef CONFIG_MM_KASAN_GLOBAL
 static bool test_global_underflow(FAR struct mm_heap_s *heap, size_t size)
 {
@@ -239,7 +307,7 @@ static bool test_global_overflow(FAR struct mm_heap_s *heap, size_t size)
 
 static int run_test(FAR const testcase_t *test)
 {
-  size_t heap_size = 65536;
+  size_t heap_size = sizeof(g_kasan_heap);
   FAR char *argv[3];
   FAR run_t *run;
   int status;
@@ -273,11 +341,11 @@ static int run_test(FAR const testcase_t *test)
   waitpid(pid, &status, 0);
   if (status == 0)
     {
-      printf("KASan test: %s, size: %ld FAIL\n", test->name, run->size);
+      printf("KASan test: %s, size: %zu FAIL\n", test->name, run->size);
     }
   else
     {
-      printf("KASan test: %s, size: %ld PASS\n", test->name, run->size);
+      printf("KASan test: %s, size: %zu PASS\n", test->name, run->size);
     }
 
   mm_uninitialize(run->heap);
@@ -287,7 +355,11 @@ static int run_test(FAR const testcase_t *test)
 static int run_testcase(int argc, FAR char *argv[])
 {
   uintptr_t index = strtoul(argv[1], NULL, 0);
+  struct timespec result;
+  struct timespec start;
+  struct timespec end;
   FAR run_t *run;
+  int ret;
 
   /* Pass in the number to run the specified case,
    * and the string of the number will not be very long
@@ -311,8 +383,17 @@ static int run_testcase(int argc, FAR char *argv[])
       return EXIT_SUCCESS;
     }
 
-  run = (FAR run_t *)index;
-  return run->testcase->func(run->heap, run->size);
+  run = (FAR run_t *)(uintptr_t)strtoul(argv[1], NULL, 16);
+  clock_gettime(CLOCK_MONOTONIC, &start);
+  ret = run->testcase->func(run->heap, run->size);
+  clock_gettime(CLOCK_MONOTONIC, &end);
+
+  timespec_sub(&result, &end, &start);
+  printf("%s spending %ld.%lds\n", run->testcase->name,
+                                   result.tv_sec,
+                                   result.tv_nsec);
+
+  return ret;
 }
 
 /****************************************************************************
@@ -326,7 +407,7 @@ int main(int argc, FAR char *argv[])
       size_t j;
       for (j = 0; j < nitems(g_kasan_test); j++)
         {
-          if (run_test(&g_kasan_test[j]) < 0)
+          if (g_kasan_test[j].is_auto && run_test(&g_kasan_test[j]) < 0)
             {
               return EXIT_FAILURE;
             }


(nuttx-apps) 03/05: kasantest: Add the ability to run a single program

Posted by xi...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

xiaoxiang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx-apps.git

commit 277c968b6ab1e9cf0b9c7079e27974a4ac4dd35f
Author: wangmingrong1 <wa...@xiaomi.com>
AuthorDate: Thu Sep 5 16:28:05 2024 +0800

    kasantest: Add the ability to run a single program
    
    Signed-off-by: wangmingrong1 <wa...@xiaomi.com>
---
 testing/kasantest/kasantest.c | 50 +++++++++++++++++++++++++++++++++++++------
 1 file changed, 44 insertions(+), 6 deletions(-)

diff --git a/testing/kasantest/kasantest.c b/testing/kasantest/kasantest.c
index b243fdc9d..e264e7055 100644
--- a/testing/kasantest/kasantest.c
+++ b/testing/kasantest/kasantest.c
@@ -96,6 +96,19 @@ static char g_kasan_heap[65536] aligned_data(8);
  * Private Functions
  ****************************************************************************/
 
+static void error_handler(void)
+{
+  int i;
+
+  printf("Usage: kasantest [-h] [case_number]\n");
+  printf("options:\n-h: show this help message\n");
+  printf("case_number:\n");
+  for (i = 0; i < nitems(g_kasan_test); i++)
+    {
+      printf("%d: %s\n", i + 1, g_kasan_test[i].name);
+    }
+}
+
 static bool test_heap_underflow(FAR struct mm_heap_s *heap, size_t size)
 {
   FAR uint8_t *mem = mm_malloc(heap, size);
@@ -197,12 +210,6 @@ static bool test_heap_memmove(FAR struct mm_heap_s *heap, size_t size)
   return false;
 }
 
-static int run_testcase(int argc, FAR char *argv[])
-{
-  FAR run_t *run = (FAR run_t *)(uintptr_t)strtoul(argv[1], NULL, 0);
-  return run->testcase->func(run->heap, run->size);
-}
-
 static int run_test(FAR const testcase_t *test)
 {
   size_t heap_size = 65536;
@@ -250,6 +257,37 @@ static int run_test(FAR const testcase_t *test)
   return 0;
 }
 
+static int run_testcase(int argc, FAR char *argv[])
+{
+  uintptr_t index = strtoul(argv[1], NULL, 0);
+  FAR run_t *run;
+
+  /* Pass in the number to run the specified case,
+   * and the string of the number will not be very long
+   */
+
+  if (strlen(argv[1]) <= 3)
+    {
+      if (memcmp(argv[1], "-h", 2) == 0
+          || index <= 0 || index > nitems(g_kasan_test))
+        {
+          error_handler();
+        }
+      else
+        {
+          if (run_test(&g_kasan_test[index - 1]) < 0)
+            {
+              return EXIT_FAILURE;
+            }
+        }
+
+      return EXIT_SUCCESS;
+    }
+
+  run = (FAR run_t *)index;
+  return run->testcase->func(run->heap, run->size);
+}
+
 /****************************************************************************
  * Public Functions
  ****************************************************************************/


(nuttx-apps) 02/05: kasantest: Fix errors running on 64 bit

Posted by xi...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

xiaoxiang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx-apps.git

commit 797fd047961a5edc29d7393f87b26f425c7be523
Author: wangmingrong1 <wa...@xiaomi.com>
AuthorDate: Thu Sep 5 15:57:31 2024 +0800

    kasantest: Fix errors running on 64 bit
    
    1. Change 'heap' to a global variable. When using the software tag 'kasan', the base of 'heap' is dynamically changing, which may result in runtime errors
    2. Modify the length of the argv array to ensure that all 64 bit addresses can be copied
    3. After initialization, the registration of the heap must be canceled
    
    Signed-off-by: wangmingrong1 <wa...@xiaomi.com>
---
 testing/kasantest/Makefile    |  1 +
 testing/kasantest/kasantest.c | 13 ++++++++-----
 2 files changed, 9 insertions(+), 5 deletions(-)

diff --git a/testing/kasantest/Makefile b/testing/kasantest/Makefile
index 1a80c2ac8..1f5f02936 100644
--- a/testing/kasantest/Makefile
+++ b/testing/kasantest/Makefile
@@ -28,5 +28,6 @@ PRIORITY  = SCHED_PRIORITY_DEFAULT
 STACKSIZE = $(CONFIG_DEFAULT_TASK_STACKSIZE)
 
 CFLAGS += -Wno-error -Wno-use-after-free
+CFLAGS += -Wno-array-bounds -Wno-free-nonheap-object
 
 include $(APPDIR)/Application.mk
diff --git a/testing/kasantest/kasantest.c b/testing/kasantest/kasantest.c
index 171773337..b243fdc9d 100644
--- a/testing/kasantest/kasantest.c
+++ b/testing/kasantest/kasantest.c
@@ -50,7 +50,7 @@ typedef struct testcase_s
 
 typedef struct run_s
 {
-  char argv[16];
+  char argv[32];
   FAR const testcase_t *testcase;
   FAR struct mm_heap_s *heap;
   size_t size;
@@ -90,6 +90,8 @@ const static testcase_t g_kasan_test[] =
   {test_heap_memmove, "heap memmove"}
 };
 
+static char g_kasan_heap[65536] aligned_data(8);
+
 /****************************************************************************
  * Private Functions
  ****************************************************************************/
@@ -213,7 +215,7 @@ static int run_test(FAR const testcase_t *test)
    * it can be released correctly.
    */
 
-  run = malloc(sizeof(run_t) + heap_size);
+  run = (run_t *)g_kasan_heap;
   if (!run)
     {
       return ERROR;
@@ -222,7 +224,7 @@ static int run_test(FAR const testcase_t *test)
   snprintf(run->argv, sizeof(run->argv), "%p", run);
   run->testcase = test;
   run->size = rand() % (heap_size / 2) + 1;
-  run->heap = mm_initialize("kasan", &run[1], heap_size);
+  run->heap = mm_initialize("kasan", (struct mm_heap_s *)&run[1], heap_size);
   if (!run->heap)
     {
       free(run);
@@ -237,13 +239,14 @@ static int run_test(FAR const testcase_t *test)
   waitpid(pid, &status, 0);
   if (status == 0)
     {
-      printf("KASan test: %s, size: %d FAIL\n", test->name, run->size);
+      printf("KASan test: %s, size: %ld FAIL\n", test->name, run->size);
     }
   else
     {
-      printf("KASan test: %s, size: %d PASS\n", test->name, run->size);
+      printf("KASan test: %s, size: %ld PASS\n", test->name, run->size);
     }
 
+  mm_uninitialize(run->heap);
   return 0;
 }