1 #pragma once
2
3 #include <stdio.h>
4 #include <optional>
5
6 #ifndef ASAN
7 #define ASAN 0
8 #endif
9
10 #ifndef MSAN
11 #define MSAN 0
12 #endif
13
14 namespace fuzzing {
15 namespace memory {
16
17 #ifndef FUZZING_HEADERS_NO_IMPL
18 #if ASAN == 1
19 extern "C" void *__asan_region_is_poisoned(const void *beg, size_t size);
20 #endif
21
22 #if MSAN == 1
23 extern "C" void __msan_check_mem_is_initialized(const volatile void *x, size_t size);
24 #endif
25
memory_test_asan(const void * data,const size_t size)26 void memory_test_asan(const void* data, const size_t size)
27 {
28 (void)data;
29 (void)size;
30
31 #if ASAN == 1
32 if ( __asan_region_is_poisoned(data, size) != NULL ) {
33 abort();
34 }
35 #endif
36 }
37
memory_test_msan(const void * data,const size_t size)38 void memory_test_msan(const void* data, const size_t size)
39 {
40 (void)data;
41 (void)size;
42
43 #if MSAN == 1
44 __msan_check_mem_is_initialized(data, size);
45 #endif
46 }
47
memory_test(const void * data,const size_t size)48 void memory_test(const void* data, const size_t size)
49 {
50 memory_test_asan(data, size);
51 memory_test_msan(data, size);
52 }
53
54 template <class T>
memory_test(const T & t)55 void memory_test(const T& t)
56 {
57 (void)t;
58 }
59
60 template <>
memory_test(const std::string & s)61 void memory_test(const std::string& s)
62 {
63 (void)s;
64
65 #if MSAN == 1
66 memory_test(s.data(), s.size());
67 #endif
68 }
69
70 #endif
71
72 } /* namespace memory */
73 } /* namespace fuzzing */
74