1*600f14f4SXin Li /* Copyright 2019 Guido Vranken
2*600f14f4SXin Li *
3*600f14f4SXin Li * Permission is hereby granted, free of charge, to any person obtaining
4*600f14f4SXin Li * a copy of this software and associated documentation files (the
5*600f14f4SXin Li * "Software"), to deal in the Software without restriction, including
6*600f14f4SXin Li * without limitation the rights to use, copy, modify, merge, publish,
7*600f14f4SXin Li * distribute, sublicense, and/or sell copies of the Software, and to
8*600f14f4SXin Li * permit persons to whom the Software is furnished to do so, subject
9*600f14f4SXin Li * to the following conditions:
10*600f14f4SXin Li *
11*600f14f4SXin Li * The above copyright notice and this permission notice shall be
12*600f14f4SXin Li * included in all copies or substantial portions of the Software.
13*600f14f4SXin Li *
14*600f14f4SXin Li * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15*600f14f4SXin Li * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16*600f14f4SXin Li * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17*600f14f4SXin Li * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
18*600f14f4SXin Li * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
19*600f14f4SXin Li * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20*600f14f4SXin Li * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21*600f14f4SXin Li * SOFTWARE.
22*600f14f4SXin Li */
23*600f14f4SXin Li
24*600f14f4SXin Li #pragma once
25*600f14f4SXin Li
26*600f14f4SXin Li #include <stdio.h>
27*600f14f4SXin Li #include <optional>
28*600f14f4SXin Li
29*600f14f4SXin Li #ifndef ASAN
30*600f14f4SXin Li #define ASAN 0
31*600f14f4SXin Li #endif
32*600f14f4SXin Li
33*600f14f4SXin Li #ifndef MSAN
34*600f14f4SXin Li #define MSAN 0
35*600f14f4SXin Li #endif
36*600f14f4SXin Li
37*600f14f4SXin Li namespace fuzzing {
38*600f14f4SXin Li namespace memory {
39*600f14f4SXin Li
40*600f14f4SXin Li #ifndef FUZZING_HEADERS_NO_IMPL
41*600f14f4SXin Li #if ASAN == 1
42*600f14f4SXin Li extern "C" void *__asan_region_is_poisoned(const void *beg, size_t size);
43*600f14f4SXin Li #endif
44*600f14f4SXin Li
45*600f14f4SXin Li #if MSAN == 1
46*600f14f4SXin Li extern "C" void __msan_check_mem_is_initialized(const volatile void *x, size_t size);
47*600f14f4SXin Li #endif
48*600f14f4SXin Li
memory_test_asan(const void * data,const size_t size)49*600f14f4SXin Li void memory_test_asan(const void* data, const size_t size)
50*600f14f4SXin Li {
51*600f14f4SXin Li (void)data;
52*600f14f4SXin Li (void)size;
53*600f14f4SXin Li
54*600f14f4SXin Li #if ASAN == 1
55*600f14f4SXin Li if ( __asan_region_is_poisoned(data, size) != NULL ) {
56*600f14f4SXin Li abort();
57*600f14f4SXin Li }
58*600f14f4SXin Li #endif
59*600f14f4SXin Li }
60*600f14f4SXin Li
memory_test_msan(const void * data,const size_t size)61*600f14f4SXin Li void memory_test_msan(const void* data, const size_t size)
62*600f14f4SXin Li {
63*600f14f4SXin Li (void)data;
64*600f14f4SXin Li (void)size;
65*600f14f4SXin Li
66*600f14f4SXin Li #if MSAN == 1
67*600f14f4SXin Li __msan_check_mem_is_initialized(data, size);
68*600f14f4SXin Li #endif
69*600f14f4SXin Li }
70*600f14f4SXin Li
memory_test(const void * data,const size_t size)71*600f14f4SXin Li void memory_test(const void* data, const size_t size)
72*600f14f4SXin Li {
73*600f14f4SXin Li memory_test_asan(data, size);
74*600f14f4SXin Li memory_test_msan(data, size);
75*600f14f4SXin Li }
76*600f14f4SXin Li
77*600f14f4SXin Li template <class T>
memory_test(const T & t)78*600f14f4SXin Li void memory_test(const T& t)
79*600f14f4SXin Li {
80*600f14f4SXin Li (void)t;
81*600f14f4SXin Li }
82*600f14f4SXin Li
83*600f14f4SXin Li template <>
memory_test(const std::string & s)84*600f14f4SXin Li void memory_test(const std::string& s)
85*600f14f4SXin Li {
86*600f14f4SXin Li (void)s;
87*600f14f4SXin Li
88*600f14f4SXin Li #if MSAN == 1
89*600f14f4SXin Li memory_test(s.data(), s.size());
90*600f14f4SXin Li #endif
91*600f14f4SXin Li }
92*600f14f4SXin Li
93*600f14f4SXin Li #endif
94*600f14f4SXin Li
95*600f14f4SXin Li } /* namespace memory */
96*600f14f4SXin Li } /* namespace fuzzing */
97