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