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