1 // Copyright 2012 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 #include "src/zone/zone.h"
6
7 #include <cstring>
8
9 #include "src/asan.h"
10 #include "src/utils.h"
11 #include "src/v8.h"
12
13 namespace v8 {
14 namespace internal {
15
16 namespace {
17
18 #ifdef V8_USE_ADDRESS_SANITIZER
19
20 constexpr size_t kASanRedzoneBytes = 24; // Must be a multiple of 8.
21
22 #else // !V8_USE_ADDRESS_SANITIZER
23
24 constexpr size_t kASanRedzoneBytes = 0;
25
26 #endif // V8_USE_ADDRESS_SANITIZER
27
28 } // namespace
29
Zone(AccountingAllocator * allocator,const char * name,SegmentSize segment_size)30 Zone::Zone(AccountingAllocator* allocator, const char* name,
31 SegmentSize segment_size)
32 : allocation_size_(0),
33 segment_bytes_allocated_(0),
34 position_(0),
35 limit_(0),
36 allocator_(allocator),
37 segment_head_(nullptr),
38 name_(name),
39 sealed_(false),
40 segment_size_(segment_size) {
41 allocator_->ZoneCreation(this);
42 }
43
~Zone()44 Zone::~Zone() {
45 allocator_->ZoneDestruction(this);
46
47 DeleteAll();
48
49 DCHECK_EQ(segment_bytes_allocated_, 0);
50 }
51
New(size_t size)52 void* Zone::New(size_t size) {
53 CHECK(!sealed_);
54
55 // Round up the requested size to fit the alignment.
56 size = RoundUp(size, kAlignmentInBytes);
57
58 // Check if the requested size is available without expanding.
59 Address result = position_;
60
61 const size_t size_with_redzone = size + kASanRedzoneBytes;
62 DCHECK(limit_ >= position_);
63 if (size_with_redzone > limit_ - position_) {
64 result = NewExpand(size_with_redzone);
65 } else {
66 position_ += size_with_redzone;
67 }
68
69 Address redzone_position = result + size;
70 DCHECK_EQ(redzone_position + kASanRedzoneBytes, position_);
71 ASAN_POISON_MEMORY_REGION(reinterpret_cast<void*>(redzone_position),
72 kASanRedzoneBytes);
73
74 // Check that the result has the proper alignment and return it.
75 DCHECK(IsAddressAligned(result, kAlignmentInBytes, 0));
76 allocation_size_ += size;
77 return reinterpret_cast<void*>(result);
78 }
79
DeleteAll()80 void Zone::DeleteAll() {
81 // Traverse the chained list of segments and return them all to the allocator.
82 for (Segment* current = segment_head_; current;) {
83 Segment* next = current->next();
84 size_t size = current->size();
85
86 // Un-poison the segment content so we can re-use or zap it later.
87 ASAN_UNPOISON_MEMORY_REGION(reinterpret_cast<void*>(current->start()),
88 current->capacity());
89
90 segment_bytes_allocated_ -= size;
91 allocator_->ReturnSegment(current);
92 current = next;
93 }
94
95 position_ = limit_ = 0;
96 allocation_size_ = 0;
97 segment_head_ = nullptr;
98 }
99
100 // Creates a new segment, sets it size, and pushes it to the front
101 // of the segment chain. Returns the new segment.
NewSegment(size_t requested_size)102 Segment* Zone::NewSegment(size_t requested_size) {
103 Segment* result = allocator_->GetSegment(requested_size);
104 if (result != nullptr) {
105 DCHECK_GE(result->size(), requested_size);
106 segment_bytes_allocated_ += result->size();
107 result->set_zone(this);
108 result->set_next(segment_head_);
109 segment_head_ = result;
110 }
111 return result;
112 }
113
NewExpand(size_t size)114 Address Zone::NewExpand(size_t size) {
115 // Make sure the requested size is already properly aligned and that
116 // there isn't enough room in the Zone to satisfy the request.
117 DCHECK_EQ(size, RoundDown(size, kAlignmentInBytes));
118 DCHECK(limit_ - position_ < size);
119
120 // Compute the new segment size. We use a 'high water mark'
121 // strategy, where we increase the segment size every time we expand
122 // except that we employ a maximum segment size when we delete. This
123 // is to avoid excessive malloc() and free() overhead.
124 Segment* head = segment_head_;
125 const size_t old_size = (head == nullptr) ? 0 : head->size();
126 static const size_t kSegmentOverhead = sizeof(Segment) + kAlignmentInBytes;
127 const size_t new_size_no_overhead = size + (old_size << 1);
128 size_t new_size = kSegmentOverhead + new_size_no_overhead;
129 const size_t min_new_size = kSegmentOverhead + size;
130 // Guard against integer overflow.
131 if (new_size_no_overhead < size || new_size < kSegmentOverhead) {
132 V8::FatalProcessOutOfMemory(nullptr, "Zone");
133 return kNullAddress;
134 }
135 if (segment_size_ == SegmentSize::kLarge) {
136 new_size = kMaximumSegmentSize;
137 }
138 if (new_size < kMinimumSegmentSize) {
139 new_size = kMinimumSegmentSize;
140 } else if (new_size > kMaximumSegmentSize) {
141 // Limit the size of new segments to avoid growing the segment size
142 // exponentially, thus putting pressure on contiguous virtual address space.
143 // All the while making sure to allocate a segment large enough to hold the
144 // requested size.
145 new_size = Max(min_new_size, kMaximumSegmentSize);
146 }
147 if (new_size > INT_MAX) {
148 V8::FatalProcessOutOfMemory(nullptr, "Zone");
149 return kNullAddress;
150 }
151 Segment* segment = NewSegment(new_size);
152 if (segment == nullptr) {
153 V8::FatalProcessOutOfMemory(nullptr, "Zone");
154 return kNullAddress;
155 }
156
157 // Recompute 'top' and 'limit' based on the new segment.
158 Address result = RoundUp(segment->start(), kAlignmentInBytes);
159 position_ = result + size;
160 // Check for address overflow.
161 // (Should not happen since the segment is guaranteed to accommodate
162 // size bytes + header and alignment padding)
163 DCHECK(position_ >= result);
164 limit_ = segment->end();
165 DCHECK(position_ <= limit_);
166 return result;
167 }
168
169 } // namespace internal
170 } // namespace v8
171