• 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 "include/core/SkTypes.h"
12 
13 #include <string.h>
14 
15 #ifdef __SANITIZE_ADDRESS__
16     #define SK_SANITIZE_ADDRESS 1
17 #endif
18 #if !defined(SK_SANITIZE_ADDRESS) && defined(__has_feature)
19     #if __has_feature(address_sanitizer)
20         #define SK_SANITIZE_ADDRESS 1
21     #endif
22 #endif
23 
24 // Typically declared in LLVM's asan_interface.h.
25 #ifdef SK_SANITIZE_ADDRESS
26 extern "C" {
27     void __asan_poison_memory_region(void const volatile *addr, size_t size);
28     void __asan_unpoison_memory_region(void const volatile *addr, size_t size);
29 }
30 #endif
31 
32 // Code that implements bespoke allocation arenas can poison the entire arena on creation, then
33 // unpoison chunks of arena memory as they are parceled out. Consider leaving gaps between blocks
34 // to detect buffer overrun.
sk_asan_poison_memory_region(void const volatile * addr,size_t size)35 static inline void sk_asan_poison_memory_region(void const volatile *addr, size_t size) {
36 #ifdef SK_SANITIZE_ADDRESS
37     __asan_poison_memory_region(addr, size);
38 #endif
39 }
40 
sk_asan_unpoison_memory_region(void const volatile * addr,size_t size)41 static inline void sk_asan_unpoison_memory_region(void const volatile *addr, size_t size) {
42 #ifdef SK_SANITIZE_ADDRESS
43     __asan_unpoison_memory_region(addr, size);
44 #endif
45 }
46 
47 #endif  // SkASAN_DEFINED
48