xref: /third_party/node/deps/v8/src/base/sanitizer/asan.h (revision 1cb0ef41)
1// Copyright 2018 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// AddressSanitizer support.
6
7#ifndef V8_BASE_SANITIZER_ASAN_H_
8#define V8_BASE_SANITIZER_ASAN_H_
9
10#include <type_traits>
11
12#include "src/base/macros.h"
13
14#ifdef V8_USE_ADDRESS_SANITIZER
15
16#include <sanitizer/asan_interface.h>
17
18#if !defined(ASAN_POISON_MEMORY_REGION) || !defined(ASAN_UNPOISON_MEMORY_REGION)
19#error \
20    "ASAN_POISON_MEMORY_REGION and ASAN_UNPOISON_MEMORY_REGION must be defined"
21#endif
22
23#define DISABLE_ASAN __attribute__((no_sanitize_address))
24
25// Check that all bytes in a memory region are poisoned. This is different from
26// `__asan_region_is_poisoned()` which only requires a single byte in the region
27// to be poisoned. Please note that the macro only works if both start and size
28// are multiple of asan's shadow memory granularity.
29#define ASAN_CHECK_WHOLE_MEMORY_REGION_IS_POISONED(start, size)               \
30  do {                                                                        \
31    for (size_t i = 0; i < size; i++) {                                       \
32      CHECK(__asan_address_is_poisoned(reinterpret_cast<const char*>(start) + \
33                                       i));                                   \
34    }                                                                         \
35  } while (0)
36
37#else  // !V8_USE_ADDRESS_SANITIZER
38
39#define DISABLE_ASAN
40
41#define ASAN_POISON_MEMORY_REGION(start, size)                      \
42  static_assert(std::is_pointer<decltype(start)>::value,            \
43                "static type violation");                           \
44  static_assert(std::is_convertible<decltype(size), size_t>::value, \
45                "static type violation");                           \
46  USE(start, size)
47
48#define ASAN_UNPOISON_MEMORY_REGION(start, size) \
49  ASAN_POISON_MEMORY_REGION(start, size)
50
51#define ASAN_CHECK_WHOLE_MEMORY_REGION_IS_POISONED(start, size) \
52  ASAN_POISON_MEMORY_REGION(start, size)
53
54#endif  // !V8_USE_ADDRESS_SANITIZER
55
56#endif  // V8_BASE_SANITIZER_ASAN_H_
57