v8
V8 is Google’s open source high-performance JavaScript and WebAssembly engine, written in C++.
Loading...
Searching...
No Matches
asan.h
Go to the documentation of this file.
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#ifndef V8_BASE_SANITIZER_ASAN_H_
6#define V8_BASE_SANITIZER_ASAN_H_
7
8// AddressSanitizer support.
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
37class AsanUnpoisonScope final {
38 public:
39 AsanUnpoisonScope(const void* addr, size_t size)
40 : addr_(addr),
41 size_(size),
42 was_poisoned_(
43 __asan_region_is_poisoned(const_cast<void*>(addr_), size_)) {
44 if (was_poisoned_) {
46 }
47 }
49 if (was_poisoned_) {
51 }
52 }
53
54 private:
55 const void* addr_;
56 size_t size_;
57 bool was_poisoned_;
58};
59
60#else // !V8_USE_ADDRESS_SANITIZER
61
62#define DISABLE_ASAN
63
64#define ASAN_POISON_MEMORY_REGION(start, size) \
65 static_assert(std::is_pointer<decltype(start)>::value, \
66 "static type violation"); \
67 static_assert(std::is_convertible<decltype(size), size_t>::value, \
68 "static type violation"); \
69 USE(start, size)
70
71#define ASAN_UNPOISON_MEMORY_REGION(start, size) \
72 ASAN_POISON_MEMORY_REGION(start, size)
73
74#define ASAN_CHECK_WHOLE_MEMORY_REGION_IS_POISONED(start, size) \
75 ASAN_POISON_MEMORY_REGION(start, size)
76
77class AsanUnpoisonScope final {
78 public:
79 AsanUnpoisonScope(const void*, size_t) {}
80};
81
82#endif // !V8_USE_ADDRESS_SANITIZER
83
84#ifdef V8_USE_HWADDRESS_SANITIZER
85
86#define DISABLE_HWASAN __attribute__((no_sanitize("hwaddress")))
87
88#else // !V8_USE_HWADDRESS_SANITIZER
89
90#define DISABLE_HWASAN
91
92#endif // !V8_USE_HWADDRESS_SANITIZER
93
94#endif // V8_BASE_SANITIZER_ASAN_H_
#define ASAN_UNPOISON_MEMORY_REGION(start, size)
Definition asan.h:71
#define ASAN_POISON_MEMORY_REGION(start, size)
Definition asan.h:64
AsanUnpoisonScope(const void *, size_t)
Definition asan.h:79
const int size_
Definition assembler.cc:132