| 1 |
/* |
| 2 |
* Copyright 2020 Google LLC |
| 3 |
* |
| 4 |
* Use of this source code is governed by a BSD-style license that can be |
| 5 |
* found in the LICENSE file. |
| 6 |
*/ |
| 7 |
|
| 8 |
#ifndef SkASAN_DEFINED |
| 9 |
#define SkASAN_DEFINED |
| 10 |
|
| 11 |
#include <cstddef> |
| 12 |
|
| 13 |
#ifdef __SANITIZE_ADDRESS__ |
| 14 |
#define SK_SANITIZE_ADDRESS 1 |
| 15 |
#endif |
| 16 |
#if !defined(SK_SANITIZE_ADDRESS) && defined(__has_feature) |
| 17 |
#if __has_feature(address_sanitizer) |
| 18 |
#define SK_SANITIZE_ADDRESS 1 |
| 19 |
#endif |
| 20 |
#endif |
| 21 |
|
| 22 |
// Typically declared in LLVM's asan_interface.h. |
| 23 |
#ifdef SK_SANITIZE_ADDRESS |
| 24 |
extern "C" { |
| 25 |
void __asan_poison_memory_region(void const volatile *addr, size_t size); |
| 26 |
void __asan_unpoison_memory_region(void const volatile *addr, size_t size); |
| 27 |
int __asan_address_is_poisoned(void const volatile *addr); |
| 28 |
} |
| 29 |
#endif |
| 30 |
|
| 31 |
// Code that implements bespoke allocation arenas can poison the entire arena on creation, then |
| 32 |
// unpoison chunks of arena memory as they are parceled out. Consider leaving gaps between blocks |
| 33 |
// to detect buffer overrun. |
| 34 |
static inline void sk_asan_poison_memory_region([[maybe_unused]] void const volatile* addr, |
| 35 |
[[maybe_unused]] size_t size) { |
| 36 |
#ifdef SK_SANITIZE_ADDRESS |
| 37 |
__asan_poison_memory_region(addr, size); |
| 38 |
#endif |
| 39 |
} |
| 40 |
|
| 41 |
static inline void sk_asan_unpoison_memory_region([[maybe_unused]] void const volatile* addr, |
| 42 |
[[maybe_unused]] size_t size) { |
| 43 |
#ifdef SK_SANITIZE_ADDRESS |
| 44 |
__asan_unpoison_memory_region(addr, size); |
| 45 |
#endif |
| 46 |
} |
| 47 |
|
| 48 |
static inline int sk_asan_address_is_poisoned([[maybe_unused]] void const volatile* addr) { |
| 49 |
#ifdef SK_SANITIZE_ADDRESS |
| 50 |
return __asan_address_is_poisoned(addr); |
| 51 |
#else |
| 52 |
return 0; |
| 53 |
#endif |
| 54 |
} |
| 55 |
|
| 56 |
#endif // SkASAN_DEFINED |