Skip to content

Commit bd916e6

Browse files
mlippautzCommit Bot
authored andcommitted
cppgc: Replace memory model macros with proper functions
Bug: chromium:1056170 Change-Id: I41ebc2e507d1662588364396f1129c75a0f0841d Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/2851890 Commit-Queue: Michael Lippautz <mlippautz@chromium.org> Reviewed-by: Omer Katz <omerkatz@chromium.org> Cr-Commit-Position: refs/heads/master@{#74233}
1 parent 87043bb commit bd916e6

12 files changed

Lines changed: 97 additions & 72 deletions

BUILD.gn

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4875,7 +4875,6 @@ v8_source_set("v8_cppgc_shared") {
48754875
"src/heap/base/stack.h",
48764876
"src/heap/base/worklist.cc",
48774877
"src/heap/base/worklist.h",
4878-
"src/heap/cppgc/sanitizers.h",
48794878
]
48804879

48814880
if (is_clang || !is_win) {
@@ -5022,6 +5021,7 @@ v8_source_set("cppgc_base") {
50225021
"src/heap/cppgc/marking-visitor.h",
50235022
"src/heap/cppgc/marking-worklists.cc",
50245023
"src/heap/cppgc/marking-worklists.h",
5024+
"src/heap/cppgc/memory.h",
50255025
"src/heap/cppgc/metric-recorder.h",
50265026
"src/heap/cppgc/name-trait.cc",
50275027
"src/heap/cppgc/object-allocator.cc",

src/heap/base/stack.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@
88

99
#include "src/base/macros.h"
1010
#include "src/base/platform/platform.h"
11+
#include "src/base/sanitizer/asan.h"
12+
#include "src/base/sanitizer/msan.h"
1113
#include "src/heap/cppgc/globals.h"
12-
#include "src/heap/cppgc/sanitizers.h"
1314

1415
namespace heap {
1516
namespace base {

src/heap/cppgc/compactor.cc

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include "src/heap/cppgc/heap-base.h"
1616
#include "src/heap/cppgc/heap-page.h"
1717
#include "src/heap/cppgc/heap-space.h"
18+
#include "src/heap/cppgc/memory.h"
1819
#include "src/heap/cppgc/object-poisoner.h"
1920
#include "src/heap/cppgc/raw-heap.h"
2021
#include "src/heap/cppgc/stats-collector.h"
@@ -275,7 +276,7 @@ class CompactionState final {
275276
// Return remaining available pages to the free page pool, decommitting
276277
// them from the pagefile.
277278
for (NormalPage* page : available_pages_) {
278-
SET_MEMORY_INACCESSIBLE(page->PayloadStart(), page->PayloadSize());
279+
SetMemoryInaccessible(page->PayloadStart(), page->PayloadSize());
279280
NormalPage::Destroy(page);
280281
}
281282
}
@@ -303,7 +304,7 @@ class CompactionState final {
303304
current_page_->PayloadSize() - used_bytes_in_current_page_;
304305
Address payload = current_page_->PayloadStart();
305306
Address free_start = payload + used_bytes_in_current_page_;
306-
SET_MEMORY_INACCESSIBLE(free_start, freed_size);
307+
SetMemoryInaccessible(free_start, freed_size);
307308
space_->free_list().Add({free_start, freed_size});
308309
current_page_->object_start_bitmap().SetBit(free_start);
309310
}

src/heap/cppgc/explicit-management.cc

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
#include "src/heap/cppgc/heap-base.h"
1010
#include "src/heap/cppgc/heap-object-header.h"
1111
#include "src/heap/cppgc/heap-page.h"
12-
#include "src/heap/cppgc/sanitizers.h"
12+
#include "src/heap/cppgc/memory.h"
1313

1414
namespace cppgc {
1515
namespace internal {
@@ -52,7 +52,7 @@ void FreeUnreferencedObject(void* object) {
5252
auto& normal_space = *static_cast<NormalPageSpace*>(base_page->space());
5353
auto& lab = normal_space.linear_allocation_buffer();
5454
ConstAddress payload_end = header.PayloadEnd();
55-
SET_MEMORY_INACCESSIBLE(&header, header_size);
55+
SetMemoryInaccessible(&header, header_size);
5656
if (payload_end == lab.start()) { // Returning to LAB.
5757
lab.Set(reinterpret_cast<Address>(&header), lab.size() + header_size);
5858
normal_page->object_start_bitmap().ClearBit(lab.start());
@@ -79,7 +79,7 @@ bool Grow(HeapObjectHeader& header, BasePage& base_page, size_t new_size,
7979
// LABs are considered used memory which means that no allocated size
8080
// adjustments are needed.
8181
Address delta_start = lab.Allocate(size_delta);
82-
SET_MEMORY_ACCESSIBLE(delta_start, size_delta);
82+
SetMemoryAccessible(delta_start, size_delta);
8383
header.SetSize(new_size);
8484
return true;
8585
}
@@ -100,14 +100,14 @@ bool Shrink(HeapObjectHeader& header, BasePage& base_page, size_t new_size,
100100
// LABs are considered used memory which means that no allocated size
101101
// adjustments are needed.
102102
lab.Set(free_start, lab.size() + size_delta);
103-
SET_MEMORY_INACCESSIBLE(lab.start(), size_delta);
103+
SetMemoryInaccessible(lab.start(), size_delta);
104104
header.SetSize(new_size);
105105
return true;
106106
}
107107
// Heuristic: Only return memory to the free list if the block is larger than
108108
// the smallest size class.
109109
if (size_delta >= ObjectAllocator::kSmallestSpaceSize) {
110-
SET_MEMORY_INACCESSIBLE(free_start, size_delta);
110+
SetMemoryInaccessible(free_start, size_delta);
111111
base_page.heap()->stats_collector()->NotifyExplicitFree(size_delta);
112112
normal_space.free_list().Add({free_start, size_delta});
113113
NormalPage::From(&base_page)->object_start_bitmap().SetBit(free_start);

src/heap/cppgc/free-list.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88

99
#include "include/cppgc/internal/logging.h"
1010
#include "src/base/bits.h"
11+
#include "src/base/sanitizer/asan.h"
1112
#include "src/heap/cppgc/globals.h"
1213
#include "src/heap/cppgc/heap-object-header.h"
13-
#include "src/heap/cppgc/sanitizers.h"
1414

1515
namespace cppgc {
1616
namespace internal {

src/heap/cppgc/heap-object-header.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66

77
#include "include/cppgc/internal/api-constants.h"
88
#include "src/base/macros.h"
9+
#include "src/base/sanitizer/asan.h"
910
#include "src/heap/cppgc/gc-info-table.h"
1011
#include "src/heap/cppgc/heap-page.h"
11-
#include "src/heap/cppgc/sanitizers.h"
1212

1313
namespace cppgc {
1414
namespace internal {

src/heap/cppgc/memory.h

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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"
13+
#include "src/base/sanitizer/asan.h"
14+
#include "src/base/sanitizer/msan.h"
15+
16+
namespace cppgc {
17+
namespace internal {
18+
19+
inline void ZapMemory(void* address, size_t size) {
20+
// The lowest bit of the zapped value should be 0 so that zapped object are
21+
// never viewed as fully constructed objects.
22+
static constexpr uint8_t kZappedValue = 0xdc;
23+
memset(address, kZappedValue, size);
24+
}
25+
26+
// Together `SetMemoryAccessible()` and `SetMemoryInaccessible()` form the
27+
// memory access model for allocation and free.
28+
V8_INLINE void SetMemoryAccessible(void* address, size_t size) {
29+
#if defined(V8_USE_MEMORY_SANITIZER)
30+
31+
MSAN_MEMORY_IS_INITIALIZED(address, size);
32+
33+
#elif defined(V8_USE_ADDRESS_SANITIZER)
34+
35+
ASAN_UNPOISON_MEMORY_REGION(address, size);
36+
37+
#elif DEBUG
38+
39+
memset(address, 0, size);
40+
41+
#else // Release builds.
42+
43+
// Nothing to be done for release builds.
44+
45+
#endif // Release builds.
46+
}
47+
48+
V8_INLINE void SetMemoryInaccessible(void* address, size_t size) {
49+
#if defined(V8_USE_MEMORY_SANITIZER)
50+
51+
memset(address, 0, size);
52+
MSAN_ALLOCATED_UNINITIALIZED_MEMORY(address, size);
53+
54+
#elif defined(V8_USE_ADDRESS_SANITIZER)
55+
56+
memset(address, 0, size);
57+
ASAN_POISON_MEMORY_REGION(address, size);
58+
59+
#elif DEBUG
60+
61+
::cppgc::internal::ZapMemory(address, size);
62+
63+
#else // Release builds.
64+
65+
memset(address, 0, size);
66+
67+
#endif // Release builds.
68+
}
69+
70+
} // namespace internal
71+
} // namespace cppgc
72+
73+
#endif // V8_HEAP_CPPGC_MEMORY_H_

src/heap/cppgc/object-allocator.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@
1212
#include "src/heap/cppgc/heap-object-header.h"
1313
#include "src/heap/cppgc/heap-page.h"
1414
#include "src/heap/cppgc/heap-space.h"
15+
#include "src/heap/cppgc/memory.h"
1516
#include "src/heap/cppgc/object-start-bitmap.h"
1617
#include "src/heap/cppgc/raw-heap.h"
17-
#include "src/heap/cppgc/sanitizers.h"
1818

1919
namespace cppgc {
2020

@@ -111,10 +111,10 @@ void* ObjectAllocator::AllocateObjectOnSpace(NormalPageSpace* space,
111111
#if !defined(V8_USE_MEMORY_SANITIZER) && !defined(V8_USE_ADDRESS_SANITIZER) && \
112112
DEBUG
113113
// For debug builds, unzap only the payload.
114-
SET_MEMORY_ACCESSIBLE(static_cast<char*>(raw) + sizeof(HeapObjectHeader),
115-
size - sizeof(HeapObjectHeader));
114+
SetMemoryAccessible(static_cast<char*>(raw) + sizeof(HeapObjectHeader),
115+
size - sizeof(HeapObjectHeader));
116116
#else
117-
SET_MEMORY_ACCESSIBLE(raw, size);
117+
SetMemoryAccessible(raw, size);
118118
#endif
119119
auto* header = new (raw) HeapObjectHeader(size, gcinfo);
120120

src/heap/cppgc/object-poisoner.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55
#ifndef V8_HEAP_CPPGC_OBJECT_POISONER_H_
66
#define V8_HEAP_CPPGC_OBJECT_POISONER_H_
77

8+
#include "src/base/sanitizer/asan.h"
89
#include "src/heap/cppgc/heap-object-header.h"
910
#include "src/heap/cppgc/heap-page.h"
1011
#include "src/heap/cppgc/heap-visitor.h"
11-
#include "src/heap/cppgc/sanitizers.h"
1212

1313
namespace cppgc {
1414
namespace internal {

src/heap/cppgc/page-memory.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
#include "src/heap/cppgc/page-memory.h"
66

77
#include "src/base/macros.h"
8-
#include "src/heap/cppgc/sanitizers.h"
8+
#include "src/base/sanitizer/asan.h"
99

1010
namespace cppgc {
1111
namespace internal {

0 commit comments

Comments
 (0)