blob: 1f722f2f608952dff173d9a377178835a70f7d08 [file] [log] [blame]
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001// Copyright 2012 the V8 project authors. All rights reserved.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
Steve Blocka7e24c12009-10-30 11:49:00 +00004
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005#include "src/zone.h"
6
7#include <cstring>
Ben Murdoch85b71792012-04-11 18:30:58 +01008
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009#include "src/v8.h"
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000010
11#ifdef V8_USE_ADDRESS_SANITIZER
12#include <sanitizer/asan_interface.h>
13#endif // V8_USE_ADDRESS_SANITIZER
Steve Blocka7e24c12009-10-30 11:49:00 +000014
15namespace v8 {
16namespace internal {
17
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000018namespace {
19
20#if V8_USE_ADDRESS_SANITIZER
21
22const size_t kASanRedzoneBytes = 24; // Must be a multiple of 8.
23
24#else
25
26#define ASAN_POISON_MEMORY_REGION(start, size) \
27 do { \
28 USE(start); \
29 USE(size); \
30 } while (false)
31
32#define ASAN_UNPOISON_MEMORY_REGION(start, size) \
33 do { \
34 USE(start); \
35 USE(size); \
36 } while (false)
37
38const size_t kASanRedzoneBytes = 0;
39
40#endif // V8_USE_ADDRESS_SANITIZER
41
42} // namespace
43
Steve Blocka7e24c12009-10-30 11:49:00 +000044
Steve Blocka7e24c12009-10-30 11:49:00 +000045// Segments represent chunks of memory: They have starting address
46// (encoded in the this pointer) and a size in bytes. Segments are
47// chained together forming a LIFO structure with the newest segment
Steve Block44f0eee2011-05-26 01:26:41 +010048// available as segment_head_. Segments are allocated using malloc()
Steve Blocka7e24c12009-10-30 11:49:00 +000049// and de-allocated using free().
50
51class Segment {
52 public:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000053 void Initialize(Segment* next, size_t size) {
Ben Murdoch69a99ed2011-11-30 16:03:39 +000054 next_ = next;
55 size_ = size;
56 }
57
Steve Blocka7e24c12009-10-30 11:49:00 +000058 Segment* next() const { return next_; }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000059 void clear_next() { next_ = nullptr; }
Steve Blocka7e24c12009-10-30 11:49:00 +000060
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000061 size_t size() const { return size_; }
62 size_t capacity() const { return size_ - sizeof(Segment); }
Steve Blocka7e24c12009-10-30 11:49:00 +000063
64 Address start() const { return address(sizeof(Segment)); }
65 Address end() const { return address(size_); }
66
Steve Blocka7e24c12009-10-30 11:49:00 +000067 private:
68 // Computes the address of the nth byte in this segment.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000069 Address address(size_t n) const { return Address(this) + n; }
Steve Blocka7e24c12009-10-30 11:49:00 +000070
Steve Blocka7e24c12009-10-30 11:49:00 +000071 Segment* next_;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000072 size_t size_;
Steve Blocka7e24c12009-10-30 11:49:00 +000073};
74
75
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000076Zone::Zone()
Ben Murdochb8a8cc12014-11-26 15:28:44 +000077 : allocation_size_(0),
Ben Murdoch69a99ed2011-11-30 16:03:39 +000078 segment_bytes_allocated_(0),
79 position_(0),
80 limit_(0),
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000081 segment_head_(nullptr) {}
Ben Murdoch69a99ed2011-11-30 16:03:39 +000082
Ben Murdochb8a8cc12014-11-26 15:28:44 +000083
84Zone::~Zone() {
85 DeleteAll();
86 DeleteKeptSegment();
87
88 DCHECK(segment_bytes_allocated_ == 0);
89}
90
91
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000092void* Zone::New(size_t size) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000093 // Round up the requested size to fit the alignment.
94 size = RoundUp(size, kAlignment);
95
96 // If the allocation size is divisible by 8 then we return an 8-byte aligned
97 // address.
98 if (kPointerSize == 4 && kAlignment == 4) {
99 position_ += ((~size) & 4) & (reinterpret_cast<intptr_t>(position_) & 4);
100 } else {
101 DCHECK(kAlignment >= kPointerSize);
102 }
103
104 // Check if the requested size is available without expanding.
105 Address result = position_;
106
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000107 const size_t size_with_redzone = size + kASanRedzoneBytes;
Ben Murdoch097c5b22016-05-18 11:27:45 +0100108 const uintptr_t limit = reinterpret_cast<uintptr_t>(limit_);
109 const uintptr_t position = reinterpret_cast<uintptr_t>(position_);
110 // position_ > limit_ can be true after the alignment correction above.
111 if (limit < position || size_with_redzone > limit - position) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000112 result = NewExpand(size_with_redzone);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000113 } else {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000114 position_ += size_with_redzone;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000115 }
116
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000117 Address redzone_position = result + size;
118 DCHECK(redzone_position + kASanRedzoneBytes == position_);
119 ASAN_POISON_MEMORY_REGION(redzone_position, kASanRedzoneBytes);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000120
121 // Check that the result has the proper alignment and return it.
122 DCHECK(IsAddressAligned(result, kAlignment, 0));
123 allocation_size_ += size;
124 return reinterpret_cast<void*>(result);
125}
126
127
128void Zone::DeleteAll() {
129#ifdef DEBUG
130 // Constant byte value used for zapping dead memory in debug mode.
131 static const unsigned char kZapDeadByte = 0xcd;
132#endif
133
134 // Find a segment with a suitable size to keep around.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000135 Segment* keep = nullptr;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000136 // Traverse the chained list of segments, zapping (in debug mode)
137 // and freeing every segment except the one we wish to keep.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000138 for (Segment* current = segment_head_; current;) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000139 Segment* next = current->next();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000140 if (!keep && current->size() <= kMaximumKeptSegmentSize) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000141 // Unlink the segment we wish to keep from the list.
142 keep = current;
143 keep->clear_next();
144 } else {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000145 size_t size = current->size();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000146#ifdef DEBUG
147 // Un-poison first so the zapping doesn't trigger ASan complaints.
148 ASAN_UNPOISON_MEMORY_REGION(current, size);
149 // Zap the entire current segment (including the header).
150 memset(current, kZapDeadByte, size);
151#endif
152 DeleteSegment(current, size);
153 }
154 current = next;
155 }
156
157 // If we have found a segment we want to keep, we must recompute the
158 // variables 'position' and 'limit' to prepare for future allocate
159 // attempts. Otherwise, we must clear the position and limit to
160 // force a new segment to be allocated on demand.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000161 if (keep) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000162 Address start = keep->start();
163 position_ = RoundUp(start, kAlignment);
164 limit_ = keep->end();
165 // Un-poison so we can re-use the segment later.
166 ASAN_UNPOISON_MEMORY_REGION(start, keep->capacity());
167#ifdef DEBUG
168 // Zap the contents of the kept segment (but not the header).
169 memset(start, kZapDeadByte, keep->capacity());
170#endif
171 } else {
172 position_ = limit_ = 0;
173 }
174
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400175 allocation_size_ = 0;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000176 // Update the head segment to be the kept segment (if any).
177 segment_head_ = keep;
178}
179
180
181void Zone::DeleteKeptSegment() {
182#ifdef DEBUG
183 // Constant byte value used for zapping dead memory in debug mode.
184 static const unsigned char kZapDeadByte = 0xcd;
185#endif
186
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000187 DCHECK(segment_head_ == nullptr || segment_head_->next() == nullptr);
188 if (segment_head_ != nullptr) {
189 size_t size = segment_head_->size();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000190#ifdef DEBUG
191 // Un-poison first so the zapping doesn't trigger ASan complaints.
192 ASAN_UNPOISON_MEMORY_REGION(segment_head_, size);
193 // Zap the entire kept segment (including the header).
194 memset(segment_head_, kZapDeadByte, size);
195#endif
196 DeleteSegment(segment_head_, size);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000197 segment_head_ = nullptr;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000198 }
199
200 DCHECK(segment_bytes_allocated_ == 0);
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000201}
202
203
Steve Block44f0eee2011-05-26 01:26:41 +0100204// Creates a new segment, sets it size, and pushes it to the front
205// of the segment chain. Returns the new segment.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000206Segment* Zone::NewSegment(size_t size) {
Steve Block44f0eee2011-05-26 01:26:41 +0100207 Segment* result = reinterpret_cast<Segment*>(Malloced::New(size));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000208 segment_bytes_allocated_ += size;
209 if (result != nullptr) {
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000210 result->Initialize(segment_head_, size);
Steve Block44f0eee2011-05-26 01:26:41 +0100211 segment_head_ = result;
212 }
213 return result;
214}
215
216
217// Deletes the given segment. Does not touch the segment chain.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000218void Zone::DeleteSegment(Segment* segment, size_t size) {
219 segment_bytes_allocated_ -= size;
Steve Block44f0eee2011-05-26 01:26:41 +0100220 Malloced::Delete(segment);
221}
Steve Blocka7e24c12009-10-30 11:49:00 +0000222
223
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000224Address Zone::NewExpand(size_t size) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000225 // Make sure the requested size is already properly aligned and that
226 // there isn't enough room in the Zone to satisfy the request.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000227 DCHECK_EQ(size, RoundDown(size, kAlignment));
Ben Murdoch097c5b22016-05-18 11:27:45 +0100228 DCHECK(limit_ < position_ ||
229 reinterpret_cast<uintptr_t>(limit_) -
230 reinterpret_cast<uintptr_t>(position_) <
231 size);
Steve Blocka7e24c12009-10-30 11:49:00 +0000232
233 // Compute the new segment size. We use a 'high water mark'
234 // strategy, where we increase the segment size every time we expand
235 // except that we employ a maximum segment size when we delete. This
236 // is to avoid excessive malloc() and free() overhead.
Steve Block44f0eee2011-05-26 01:26:41 +0100237 Segment* head = segment_head_;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000238 const size_t old_size = (head == nullptr) ? 0 : head->size();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000239 static const size_t kSegmentOverhead = sizeof(Segment) + kAlignment;
240 const size_t new_size_no_overhead = size + (old_size << 1);
241 size_t new_size = kSegmentOverhead + new_size_no_overhead;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000242 const size_t min_new_size = kSegmentOverhead + size;
Ben Murdoch589d6972011-11-30 16:04:58 +0000243 // Guard against integer overflow.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000244 if (new_size_no_overhead < size || new_size < kSegmentOverhead) {
Ben Murdoch589d6972011-11-30 16:04:58 +0000245 V8::FatalProcessOutOfMemory("Zone");
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000246 return nullptr;
Ben Murdoch589d6972011-11-30 16:04:58 +0000247 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000248 if (new_size < kMinimumSegmentSize) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000249 new_size = kMinimumSegmentSize;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000250 } else if (new_size > kMaximumSegmentSize) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000251 // Limit the size of new segments to avoid growing the segment size
252 // exponentially, thus putting pressure on contiguous virtual address space.
253 // All the while making sure to allocate a segment large enough to hold the
254 // requested size.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000255 new_size = Max(min_new_size, kMaximumSegmentSize);
Steve Blocka7e24c12009-10-30 11:49:00 +0000256 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000257 if (new_size > INT_MAX) {
258 V8::FatalProcessOutOfMemory("Zone");
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000259 return nullptr;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000260 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000261 Segment* segment = NewSegment(new_size);
262 if (segment == nullptr) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000263 V8::FatalProcessOutOfMemory("Zone");
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000264 return nullptr;
Steve Blocka7e24c12009-10-30 11:49:00 +0000265 }
266
267 // Recompute 'top' and 'limit' based on the new segment.
268 Address result = RoundUp(segment->start(), kAlignment);
269 position_ = result + size;
Ben Murdoch589d6972011-11-30 16:04:58 +0000270 // Check for address overflow.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000271 // (Should not happen since the segment is guaranteed to accomodate
272 // size bytes + header and alignment padding)
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000273 DCHECK(reinterpret_cast<uintptr_t>(position_) >=
274 reinterpret_cast<uintptr_t>(result));
Steve Blocka7e24c12009-10-30 11:49:00 +0000275 limit_ = segment->end();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000276 DCHECK(position_ <= limit_);
Steve Blocka7e24c12009-10-30 11:49:00 +0000277 return result;
278}
279
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000280} // namespace internal
281} // namespace v8