Kaido Kert | f309f9a | 2021-04-30 12:09:15 -0700 | [diff] [blame] | 1 | // Copyright 2020 the V8 project authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef V8_HEAP_CPPGC_SANITIZERS_H_ |
| 6 | #define V8_HEAP_CPPGC_SANITIZERS_H_ |
| 7 | |
| 8 | #include <stdint.h> |
| 9 | #include <string.h> |
| 10 | |
| 11 | #include "src/base/macros.h" |
| 12 | |
| 13 | // |
| 14 | // TODO(chromium:1056170): Find a place in base for sanitizer support. |
| 15 | // |
| 16 | |
| 17 | #ifdef V8_USE_ADDRESS_SANITIZER |
| 18 | |
| 19 | #include <sanitizer/asan_interface.h> |
| 20 | |
| 21 | #define NO_SANITIZE_ADDRESS __attribute__((no_sanitize_address)) |
| 22 | #if !defined(ASAN_POISON_MEMORY_REGION) || !defined(ASAN_UNPOISON_MEMORY_REGION) |
| 23 | #error "ASAN_POISON_MEMORY_REGION must be defined" |
| 24 | #endif |
| 25 | |
| 26 | #else // !V8_USE_ADDRESS_SANITIZER |
| 27 | |
| 28 | #define NO_SANITIZE_ADDRESS |
| 29 | #define ASAN_POISON_MEMORY_REGION(addr, size) ((void)(addr), (void)(size)) |
| 30 | #define ASAN_UNPOISON_MEMORY_REGION(addr, size) ((void)(addr), (void)(size)) |
| 31 | |
| 32 | #endif // V8_USE_ADDRESS_SANITIZER |
| 33 | |
| 34 | #ifdef V8_USE_MEMORY_SANITIZER |
| 35 | |
| 36 | #include <sanitizer/msan_interface.h> |
| 37 | |
| 38 | #define MSAN_POISON(addr, size) __msan_allocated_memory(addr, size) |
| 39 | #define MSAN_UNPOISON(addr, size) __msan_unpoison(addr, size) |
| 40 | |
| 41 | #else // !V8_USE_MEMORY_SANITIZER |
| 42 | |
| 43 | #define MSAN_POISON(addr, size) ((void)(addr), (void)(size)) |
| 44 | #define MSAN_UNPOISON(addr, size) ((void)(addr), (void)(size)) |
| 45 | |
| 46 | #endif // V8_USE_MEMORY_SANITIZER |
| 47 | |
| 48 | // API for newly allocated or reclaimed memory. |
| 49 | #if defined(V8_USE_MEMORY_SANITIZER) |
| 50 | #define SET_MEMORY_ACCESSIBLE(address, size) MSAN_UNPOISON(address, size); |
| 51 | #define SET_MEMORY_INACCESSIBLE(address, size) \ |
| 52 | memset((address), 0, (size)); \ |
| 53 | MSAN_POISON((address), (size)) |
| 54 | #elif defined(V8_USE_ADDRESS_SANITIZER) |
| 55 | #define SET_MEMORY_ACCESSIBLE(address, size) \ |
| 56 | ASAN_UNPOISON_MEMORY_REGION(address, size); |
| 57 | #define SET_MEMORY_INACCESSIBLE(address, size) \ |
| 58 | memset((address), 0, (size)); \ |
| 59 | ASAN_POISON_MEMORY_REGION(address, size) |
| 60 | #elif DEBUG |
| 61 | #define SET_MEMORY_ACCESSIBLE(address, size) memset((address), 0, (size)) |
| 62 | #define SET_MEMORY_INACCESSIBLE(address, size) \ |
| 63 | ::cppgc::internal::ZapMemory((address), (size)); |
| 64 | #else |
| 65 | #define SET_MEMORY_ACCESSIBLE(address, size) ((void)(address), (void)(size)) |
| 66 | #define SET_MEMORY_INACCESSIBLE(address, size) memset((address), 0, (size)) |
| 67 | #endif |
| 68 | |
| 69 | namespace cppgc { |
| 70 | namespace internal { |
| 71 | |
| 72 | inline void ZapMemory(void* address, size_t size) { |
| 73 | // The lowest bit of the zapped value should be 0 so that zapped object |
| 74 | // are never viewed as fully constructed objects. |
| 75 | static constexpr uint8_t kZappedValue = 0xdc; |
| 76 | memset(address, kZappedValue, size); |
| 77 | } |
| 78 | |
| 79 | } // namespace internal |
| 80 | } // namespace cppgc |
| 81 | |
| 82 | #endif // V8_HEAP_CPPGC_SANITIZERS_H_ |