• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 }
28 #endif
29 
30 // Code that implements bespoke allocation arenas can poison the entire arena on creation, then
31 // unpoison chunks of arena memory as they are parceled out. Consider leaving gaps between blocks
32 // to detect buffer overrun.
sk_asan_poison_memory_region(void const volatile * addr,size_t size)33 static inline void sk_asan_poison_memory_region(void const volatile *addr, size_t size) {
34 #ifdef SK_SANITIZE_ADDRESS
35     __asan_poison_memory_region(addr, size);
36 #endif
37 }
38 
sk_asan_unpoison_memory_region(void const volatile * addr,size_t size)39 static inline void sk_asan_unpoison_memory_region(void const volatile *addr, size_t size) {
40 #ifdef SK_SANITIZE_ADDRESS
41     __asan_unpoison_memory_region(addr, size);
42 #endif
43 }
44 
45 #endif  // SkASAN_DEFINED
46