memory.hpp 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. #pragma once
  24. #include <stdio.h>
  25. #include <optional>
  26. #ifndef ASAN
  27. #define ASAN 0
  28. #endif
  29. #ifndef MSAN
  30. #define MSAN 0
  31. #endif
  32. namespace fuzzing {
  33. namespace memory {
  34. #ifndef FUZZING_HEADERS_NO_IMPL
  35. #if ASAN == 1
  36. extern "C" void *__asan_region_is_poisoned(const void *beg, size_t size);
  37. #endif
  38. #if MSAN == 1
  39. extern "C" void __msan_check_mem_is_initialized(const volatile void *x, size_t size);
  40. #endif
  41. void memory_test_asan(const void* data, const size_t size)
  42. {
  43. (void)data;
  44. (void)size;
  45. #if ASAN == 1
  46. if ( __asan_region_is_poisoned(data, size) != NULL ) {
  47. abort();
  48. }
  49. #endif
  50. }
  51. void memory_test_msan(const void* data, const size_t size)
  52. {
  53. (void)data;
  54. (void)size;
  55. #if MSAN == 1
  56. __msan_check_mem_is_initialized(data, size);
  57. #endif
  58. }
  59. void memory_test(const void* data, const size_t size)
  60. {
  61. memory_test_asan(data, size);
  62. memory_test_msan(data, size);
  63. }
  64. template <class T>
  65. void memory_test(const T& t)
  66. {
  67. (void)t;
  68. }
  69. template <>
  70. void memory_test(const std::string& s)
  71. {
  72. (void)s;
  73. #if MSAN == 1
  74. memory_test(s.data(), s.size());
  75. #endif
  76. }
  77. #endif
  78. } /* namespace memory */
  79. } /* namespace fuzzing */