Skip to content

Commit 7c94d13

Browse files
W-M-Rxiaoxiang781216
authored andcommitted
kasantest: Add performance testing for Kasan, including pile insertion testing and algorithm testing
Signed-off-by: wangmingrong1 <[email protected]>
1 parent f2c21b1 commit 7c94d13

File tree

3 files changed

+124
-22
lines changed

3 files changed

+124
-22
lines changed

testing/kasantest/Kconfig

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,23 @@ config TESTING_KASAN
88
default n
99
---help---
1010
Enable the KASan validity test
11+
12+
if TESTING_KASAN
13+
14+
config TESTING_KASAN_PRIORITY
15+
int "KASan test priority"
16+
default 101
17+
18+
config TESTING_KASAN_STACKSIZE
19+
int "KASan test stack size"
20+
default 8192
21+
22+
config TESTING_KASAN_PERF_HEAP_SIZE
23+
int "KASan test heap size"
24+
default 1024000
25+
26+
config TESTING_KASAN_PERF_CYCLES
27+
int "KASan test performance cycles"
28+
default 4096
29+
30+
endif

testing/kasantest/Makefile

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,12 @@ include $(APPDIR)/Make.defs
2424

2525
MAINSRC = kasantest.c
2626
PROGNAME = kasantest
27-
PRIORITY = SCHED_PRIORITY_DEFAULT
28-
STACKSIZE = $(CONFIG_DEFAULT_TASK_STACKSIZE)
27+
PRIORITY = $(CONFIG_TESTING_KASAN_PRIORITY)
28+
STACKSIZE = $(CONFIG_TESTING_KASAN_STACKSIZE)
2929

3030
CFLAGS += -Wno-error -Wno-use-after-free
3131
CFLAGS += -Wno-array-bounds -Wno-free-nonheap-object
3232
CFLAGS += -Wno-stringop-overflow
33+
CFLAGS += "-O0"
3334

3435
include $(APPDIR)/Application.mk

testing/kasantest/kasantest.c

Lines changed: 101 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,10 @@
2828
#include <stdint.h>
2929
#include <stdio.h>
3030
#include <stdlib.h>
31-
#include <sys/types.h>
3231
#include <syslog.h>
32+
#include <time.h>
3333

34+
#include <sys/types.h>
3435
#include <sys/param.h>
3536
#include <sys/wait.h>
3637

@@ -45,6 +46,7 @@
4546
typedef struct testcase_s
4647
{
4748
bool (*func)(FAR struct mm_heap_s *heap, size_t size);
49+
bool is_auto;
4850
FAR const char *name;
4951
} testcase_t;
5052

@@ -71,6 +73,8 @@ static bool test_heap_unpoison(FAR struct mm_heap_s *heap, size_t size);
7173
static bool test_heap_memset(FAR struct mm_heap_s *heap, size_t size);
7274
static bool test_heap_memcpy(FAR struct mm_heap_s *heap, size_t size);
7375
static bool test_heap_memmove(FAR struct mm_heap_s *heap, size_t size);
76+
static bool test_insert_perf(FAR struct mm_heap_s *heap, size_t size);
77+
static bool test_algorithm_perf(FAR struct mm_heap_s *heap, size_t size);
7478

7579
#ifdef CONFIG_MM_KASAN_GLOBAL
7680
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);
8387

8488
const static testcase_t g_kasan_test[] =
8589
{
86-
{test_heap_underflow, "heap underflow"},
87-
{test_heap_overflow, "heap overflow"},
88-
{test_heap_use_after_free, "heap use after free"},
89-
{test_heap_invalid_free, "heap inval free"},
90-
{test_heap_double_free, "test heap double free"},
91-
{test_heap_poison, "heap poison"},
92-
{test_heap_unpoison, "heap unpoison"},
93-
{test_heap_memset, "heap memset"},
94-
{test_heap_memcpy, "heap memcpy"},
95-
{test_heap_memmove, "heap memmove"},
90+
{test_heap_underflow, true, "heap underflow"},
91+
{test_heap_overflow, true, "heap overflow"},
92+
{test_heap_use_after_free, true, "heap use after free"},
93+
{test_heap_invalid_free, true, "heap inval free"},
94+
{test_heap_double_free, true, "test heap double free"},
95+
{test_heap_poison, true, "heap poison"},
96+
{test_heap_unpoison, true, "heap unpoison"},
97+
{test_heap_memset, true, "heap memset"},
98+
{test_heap_memcpy, true, "heap memcpy"},
99+
{test_heap_memmove, true, "heap memmove"},
100+
{test_insert_perf, false, "Kasan insert performance testing"},
101+
{test_algorithm_perf, false, "Kasan algorithm performance testing"},
96102
#ifdef CONFIG_MM_KASAN_GLOBAL
97-
{test_global_underflow, "globals underflow"},
98-
{test_global_overflow, "globals overflow"},
103+
{test_global_underflow, true, "globals underflow"},
104+
{test_global_overflow, true, "globals overflow"},
99105
#endif
100106
};
101107

102-
static char g_kasan_heap[65536] aligned_data(8);
108+
static char g_kasan_heap[10240] aligned_data(8);
103109

104110
#ifdef CONFIG_MM_KASAN_GLOBAL
105111
static char g_kasan_globals[32];
@@ -122,6 +128,20 @@ static void error_handler(void)
122128
}
123129
}
124130

131+
static void timespec_sub(struct timespec *dest,
132+
struct timespec *ts1,
133+
struct timespec *ts2)
134+
{
135+
dest->tv_sec = ts1->tv_sec - ts2->tv_sec;
136+
dest->tv_nsec = ts1->tv_nsec - ts2->tv_nsec;
137+
138+
if (dest->tv_nsec < 0)
139+
{
140+
dest->tv_nsec += 1000000000;
141+
dest->tv_sec -= 1;
142+
}
143+
}
144+
125145
static bool test_heap_underflow(FAR struct mm_heap_s *heap, size_t size)
126146
{
127147
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)
223243
return false;
224244
}
225245

246+
static bool test_insert_perf(FAR struct mm_heap_s *heap, size_t size)
247+
{
248+
int num = 0;
249+
char value;
250+
char *p;
251+
int i;
252+
253+
p = (char *)malloc(CONFIG_TESTING_KASAN_PERF_HEAP_SIZE);
254+
if (!p)
255+
{
256+
printf("Failed to allocate memory for performance testing\n");
257+
return false;
258+
}
259+
260+
do
261+
{
262+
value = num % INT8_MAX;
263+
for (i = 0; i < CONFIG_TESTING_KASAN_PERF_HEAP_SIZE; i++)
264+
{
265+
p[i] = value;
266+
}
267+
}
268+
while (num++ < CONFIG_TESTING_KASAN_PERF_CYCLES);
269+
270+
return true;
271+
}
272+
273+
static bool test_algorithm_perf(FAR struct mm_heap_s *heap, size_t size)
274+
{
275+
int num = 0;
276+
char *p;
277+
278+
p = (char *)malloc(CONFIG_TESTING_KASAN_PERF_HEAP_SIZE);
279+
if (!p)
280+
{
281+
printf("Failed to allocate memory for performance testing\n");
282+
return false;
283+
}
284+
285+
do
286+
{
287+
memset(p, num % INT8_MAX, CONFIG_TESTING_KASAN_PERF_HEAP_SIZE);
288+
}
289+
while (num++ < CONFIG_TESTING_KASAN_PERF_CYCLES);
290+
291+
return true;
292+
}
293+
226294
#ifdef CONFIG_MM_KASAN_GLOBAL
227295
static bool test_global_underflow(FAR struct mm_heap_s *heap, size_t size)
228296
{
@@ -239,7 +307,7 @@ static bool test_global_overflow(FAR struct mm_heap_s *heap, size_t size)
239307

240308
static int run_test(FAR const testcase_t *test)
241309
{
242-
size_t heap_size = 65536;
310+
size_t heap_size = sizeof(g_kasan_heap);
243311
FAR char *argv[3];
244312
FAR run_t *run;
245313
int status;
@@ -273,11 +341,11 @@ static int run_test(FAR const testcase_t *test)
273341
waitpid(pid, &status, 0);
274342
if (status == 0)
275343
{
276-
printf("KASan test: %s, size: %ld FAIL\n", test->name, run->size);
344+
printf("KASan test: %s, size: %zu FAIL\n", test->name, run->size);
277345
}
278346
else
279347
{
280-
printf("KASan test: %s, size: %ld PASS\n", test->name, run->size);
348+
printf("KASan test: %s, size: %zu PASS\n", test->name, run->size);
281349
}
282350

283351
mm_uninitialize(run->heap);
@@ -287,7 +355,11 @@ static int run_test(FAR const testcase_t *test)
287355
static int run_testcase(int argc, FAR char *argv[])
288356
{
289357
uintptr_t index = strtoul(argv[1], NULL, 0);
358+
struct timespec result;
359+
struct timespec start;
360+
struct timespec end;
290361
FAR run_t *run;
362+
int ret;
291363

292364
/* Pass in the number to run the specified case,
293365
* and the string of the number will not be very long
@@ -311,8 +383,17 @@ static int run_testcase(int argc, FAR char *argv[])
311383
return EXIT_SUCCESS;
312384
}
313385

314-
run = (FAR run_t *)index;
315-
return run->testcase->func(run->heap, run->size);
386+
run = (FAR run_t *)(uintptr_t)strtoul(argv[1], NULL, 16);
387+
clock_gettime(CLOCK_MONOTONIC, &start);
388+
ret = run->testcase->func(run->heap, run->size);
389+
clock_gettime(CLOCK_MONOTONIC, &end);
390+
391+
timespec_sub(&result, &end, &start);
392+
printf("%s spending %ld.%lds\n", run->testcase->name,
393+
result.tv_sec,
394+
result.tv_nsec);
395+
396+
return ret;
316397
}
317398

318399
/****************************************************************************
@@ -326,7 +407,7 @@ int main(int argc, FAR char *argv[])
326407
size_t j;
327408
for (j = 0; j < nitems(g_kasan_test); j++)
328409
{
329-
if (run_test(&g_kasan_test[j]) < 0)
410+
if (g_kasan_test[j].is_auto && run_test(&g_kasan_test[j]) < 0)
330411
{
331412
return EXIT_FAILURE;
332413
}

0 commit comments

Comments
 (0)