28
28
#include <stdint.h>
29
29
#include <stdio.h>
30
30
#include <stdlib.h>
31
- #include <sys/types.h>
32
31
#include <syslog.h>
32
+ #include <time.h>
33
33
34
+ #include <sys/types.h>
34
35
#include <sys/param.h>
35
36
#include <sys/wait.h>
36
37
45
46
typedef struct testcase_s
46
47
{
47
48
bool (* func )(FAR struct mm_heap_s * heap , size_t size );
49
+ bool is_auto ;
48
50
FAR const char * name ;
49
51
} testcase_t ;
50
52
@@ -71,6 +73,8 @@ static bool test_heap_unpoison(FAR struct mm_heap_s *heap, size_t size);
71
73
static bool test_heap_memset (FAR struct mm_heap_s * heap , size_t size );
72
74
static bool test_heap_memcpy (FAR struct mm_heap_s * heap , size_t size );
73
75
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 );
74
78
75
79
#ifdef CONFIG_MM_KASAN_GLOBAL
76
80
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);
83
87
84
88
const static testcase_t g_kasan_test [] =
85
89
{
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" },
96
102
#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" },
99
105
#endif
100
106
};
101
107
102
- static char g_kasan_heap [65536 ] aligned_data (8 );
108
+ static char g_kasan_heap [10240 ] aligned_data (8 );
103
109
104
110
#ifdef CONFIG_MM_KASAN_GLOBAL
105
111
static char g_kasan_globals [32 ];
@@ -122,6 +128,20 @@ static void error_handler(void)
122
128
}
123
129
}
124
130
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
+
125
145
static bool test_heap_underflow (FAR struct mm_heap_s * heap , size_t size )
126
146
{
127
147
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)
223
243
return false;
224
244
}
225
245
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
+
226
294
#ifdef CONFIG_MM_KASAN_GLOBAL
227
295
static bool test_global_underflow (FAR struct mm_heap_s * heap , size_t size )
228
296
{
@@ -239,7 +307,7 @@ static bool test_global_overflow(FAR struct mm_heap_s *heap, size_t size)
239
307
240
308
static int run_test (FAR const testcase_t * test )
241
309
{
242
- size_t heap_size = 65536 ;
310
+ size_t heap_size = sizeof ( g_kasan_heap ) ;
243
311
FAR char * argv [3 ];
244
312
FAR run_t * run ;
245
313
int status ;
@@ -273,11 +341,11 @@ static int run_test(FAR const testcase_t *test)
273
341
waitpid (pid , & status , 0 );
274
342
if (status == 0 )
275
343
{
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 );
277
345
}
278
346
else
279
347
{
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 );
281
349
}
282
350
283
351
mm_uninitialize (run -> heap );
@@ -287,7 +355,11 @@ static int run_test(FAR const testcase_t *test)
287
355
static int run_testcase (int argc , FAR char * argv [])
288
356
{
289
357
uintptr_t index = strtoul (argv [1 ], NULL , 0 );
358
+ struct timespec result ;
359
+ struct timespec start ;
360
+ struct timespec end ;
290
361
FAR run_t * run ;
362
+ int ret ;
291
363
292
364
/* Pass in the number to run the specified case,
293
365
* and the string of the number will not be very long
@@ -311,8 +383,17 @@ static int run_testcase(int argc, FAR char *argv[])
311
383
return EXIT_SUCCESS ;
312
384
}
313
385
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 ;
316
397
}
317
398
318
399
/****************************************************************************
@@ -326,7 +407,7 @@ int main(int argc, FAR char *argv[])
326
407
size_t j ;
327
408
for (j = 0 ; j < nitems (g_kasan_test ); j ++ )
328
409
{
329
- if (run_test (& g_kasan_test [j ]) < 0 )
410
+ if (g_kasan_test [ j ]. is_auto && run_test (& g_kasan_test [j ]) < 0 )
330
411
{
331
412
return EXIT_FAILURE ;
332
413
}
0 commit comments