From 797fd047961a5edc29d7393f87b26f425c7be523 Mon Sep 17 00:00:00 2001 From: wangmingrong1 Date: Thu, 5 Sep 2024 15:57:31 +0800 Subject: [PATCH] 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 --- 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; }