v8
V8 is Google’s open source high-performance JavaScript and WebAssembly engine, written in C++.
Loading...
Searching...
No Matches
memory.h
Go to the documentation of this file.
1// Copyright 2021 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_MEMORY_H_
6#define V8_HEAP_CPPGC_MEMORY_H_
7
8#include <cstddef>
9#include <cstdint>
10#include <cstring>
11
12#include "src/base/macros.h"
16
17namespace cppgc {
18namespace internal {
19
20V8_NOINLINE DISABLE_ASAN void NoSanitizeMemset(void* address, char c,
21 size_t bytes);
22
23static constexpr uint8_t kZappedValue = 0xdc;
24
25V8_INLINE void ZapMemory(void* address, size_t size) {
26 // The lowest bit of the zapped value should be 0 so that zapped object are
27 // never viewed as fully constructed objects.
28 memset(address, kZappedValue, size);
29}
30
31V8_INLINE void CheckMemoryIsZapped(const void* address, size_t size) {
32 for (size_t i = 0; i < size; i++) {
33 CHECK_EQ(kZappedValue, reinterpret_cast<ConstAddress>(address)[i]);
34 }
35}
36
37V8_INLINE void CheckMemoryIsZero(const void* address, size_t size) {
38 for (size_t i = 0; i < size; i++) {
39 CHECK_EQ(0, reinterpret_cast<ConstAddress>(address)[i]);
40 }
41}
42
43// Together `SetMemoryAccessible()` and `SetMemoryInaccessible()` form the
44// memory access model for allocation and free.
45
46#if defined(V8_USE_MEMORY_SANITIZER) || defined(V8_USE_ADDRESS_SANITIZER) || \
47 DEBUG
48
49void SetMemoryAccessible(void* address, size_t size);
50void SetMemoryInaccessible(void* address, size_t size);
51void CheckMemoryIsInaccessible(const void* address, size_t size);
52
53constexpr bool CheckMemoryIsInaccessibleIsNoop() {
54#if defined(V8_USE_MEMORY_SANITIZER)
55
56 return true;
57
58#elif defined(V8_USE_ADDRESS_SANITIZER)
59
60 return false;
61
62#else // Debug builds.
63
64 return false;
65
66#endif // Debug builds.
67}
68
69#else
70
71// Nothing to be done for release builds.
72V8_INLINE void SetMemoryAccessible(void* address, size_t size) {}
73V8_INLINE void CheckMemoryIsInaccessible(const void* address, size_t size) {}
74constexpr bool CheckMemoryIsInaccessibleIsNoop() { return true; }
75
76V8_INLINE void SetMemoryInaccessible(void* address, size_t size) {
77 memset(address, 0, size);
78}
79
80#endif
81
82} // namespace internal
83} // namespace cppgc
84
85#endif // V8_HEAP_CPPGC_MEMORY_H_
#define DISABLE_ASAN
Definition asan.h:62
V8_INLINE void CheckMemoryIsZero(const void *address, size_t size)
Definition memory.h:37
static constexpr uint8_t kZappedValue
Definition memory.h:23
void NoSanitizeMemset(void *address, char c, size_t bytes)
Definition memory.cc:14
V8_INLINE void CheckMemoryIsInaccessible(const void *address, size_t size)
Definition memory.h:73
V8_INLINE void CheckMemoryIsZapped(const void *address, size_t size)
Definition memory.h:31
V8_INLINE void SetMemoryInaccessible(void *address, size_t size)
Definition memory.h:76
V8_INLINE void ZapMemory(void *address, size_t size)
Definition memory.h:25
V8_INLINE void SetMemoryAccessible(void *address, size_t size)
Definition memory.h:72
constexpr bool CheckMemoryIsInaccessibleIsNoop()
Definition memory.h:74
const uint8_t * ConstAddress
Definition globals.h:18
#define CHECK_EQ(lhs, rhs)
#define V8_INLINE
Definition v8config.h:500
#define V8_NOINLINE
Definition v8config.h:586