zone.cc 8.31 KB
Newer Older
1
// Copyright 2012 the V8 project authors. All rights reserved.
2 3
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
4

5
#include "src/zone.h"
yangguo@chromium.org's avatar
yangguo@chromium.org committed
6

7 8
#include <cstring>

9 10
#include "src/v8.h"

11 12 13
#ifdef V8_USE_ADDRESS_SANITIZER
#include <sanitizer/asan_interface.h>
#endif  // V8_USE_ADDRESS_SANITIZER
14

15 16
namespace v8 {
namespace internal {
17

18 19 20 21
namespace {

#if V8_USE_ADDRESS_SANITIZER

22
const size_t kASanRedzoneBytes = 24;  // Must be a multiple of 8.
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37

#else

#define ASAN_POISON_MEMORY_REGION(start, size) \
  do {                                         \
    USE(start);                                \
    USE(size);                                 \
  } while (false)

#define ASAN_UNPOISON_MEMORY_REGION(start, size) \
  do {                                           \
    USE(start);                                  \
    USE(size);                                   \
  } while (false)

38
const size_t kASanRedzoneBytes = 0;
39 40 41 42 43

#endif  // V8_USE_ADDRESS_SANITIZER

}  // namespace

44

45 46 47
// Segments represent chunks of memory: They have starting address
// (encoded in the this pointer) and a size in bytes. Segments are
// chained together forming a LIFO structure with the newest segment
48
// available as segment_head_. Segments are allocated using malloc()
49 50 51 52
// and de-allocated using free().

class Segment {
 public:
53
  void Initialize(Segment* next, size_t size) {
54 55 56 57
    next_ = next;
    size_ = size;
  }

58
  Segment* next() const { return next_; }
59
  void clear_next() { next_ = nullptr; }
60

61 62
  size_t size() const { return size_; }
  size_t capacity() const { return size_ - sizeof(Segment); }
63 64 65 66 67 68

  Address start() const { return address(sizeof(Segment)); }
  Address end() const { return address(size_); }

 private:
  // Computes the address of the nth byte in this segment.
69
  Address address(size_t n) const { return Address(this) + n; }
70 71

  Segment* next_;
72
  size_t size_;
73 74 75
};


76
Zone::Zone()
77
    : allocation_size_(0),
78 79 80
      segment_bytes_allocated_(0),
      position_(0),
      limit_(0),
81
      segment_head_(nullptr) {}
82

83

84
Zone::~Zone() {
85 86 87
  DeleteAll();
  DeleteKeptSegment();

88
  DCHECK(segment_bytes_allocated_ == 0);
89 90 91
}


92
void* Zone::New(size_t size) {
93 94 95 96 97 98 99 100 101 102 103 104 105 106
  // Round up the requested size to fit the alignment.
  size = RoundUp(size, kAlignment);

  // If the allocation size is divisible by 8 then we return an 8-byte aligned
  // address.
  if (kPointerSize == 4 && kAlignment == 4) {
    position_ += ((~size) & 4) & (reinterpret_cast<intptr_t>(position_) & 4);
  } else {
    DCHECK(kAlignment >= kPointerSize);
  }

  // Check if the requested size is available without expanding.
  Address result = position_;

107 108
  const size_t size_with_redzone = size + kASanRedzoneBytes;
  if (limit_ < position_ + size_with_redzone) {
109
    result = NewExpand(size_with_redzone);
110
  } else {
111
    position_ += size_with_redzone;
112 113 114 115 116 117 118 119 120 121 122 123 124
  }

  Address redzone_position = result + size;
  DCHECK(redzone_position + kASanRedzoneBytes == position_);
  ASAN_POISON_MEMORY_REGION(redzone_position, kASanRedzoneBytes);

  // Check that the result has the proper alignment and return it.
  DCHECK(IsAddressAligned(result, kAlignment, 0));
  allocation_size_ += size;
  return reinterpret_cast<void*>(result);
}


125
void Zone::DeleteAll() {
126 127 128 129 130
#ifdef DEBUG
  // Constant byte value used for zapping dead memory in debug mode.
  static const unsigned char kZapDeadByte = 0xcd;
#endif

131
  // Find a segment with a suitable size to keep around.
132
  Segment* keep = nullptr;
133 134
  // Traverse the chained list of segments, zapping (in debug mode)
  // and freeing every segment except the one we wish to keep.
135
  for (Segment* current = segment_head_; current;) {
136
    Segment* next = current->next();
137
    if (!keep && current->size() <= kMaximumKeptSegmentSize) {
138
      // Unlink the segment we wish to keep from the list.
139 140
      keep = current;
      keep->clear_next();
141
    } else {
142
      size_t size = current->size();
143
#ifdef DEBUG
144 145
      // Un-poison first so the zapping doesn't trigger ASan complaints.
      ASAN_UNPOISON_MEMORY_REGION(current, size);
146 147
      // Zap the entire current segment (including the header).
      memset(current, kZapDeadByte, size);
148
#endif
149 150
      DeleteSegment(current, size);
    }
151 152 153
    current = next;
  }

154 155 156 157
  // If we have found a segment we want to keep, we must recompute the
  // variables 'position' and 'limit' to prepare for future allocate
  // attempts. Otherwise, we must clear the position and limit to
  // force a new segment to be allocated on demand.
158
  if (keep) {
159 160 161
    Address start = keep->start();
    position_ = RoundUp(start, kAlignment);
    limit_ = keep->end();
162 163
    // Un-poison so we can re-use the segment later.
    ASAN_UNPOISON_MEMORY_REGION(start, keep->capacity());
164 165 166 167 168 169 170 171
#ifdef DEBUG
    // Zap the contents of the kept segment (but not the header).
    memset(start, kZapDeadByte, keep->capacity());
#endif
  } else {
    position_ = limit_ = 0;
  }

172
  allocation_size_ = 0;
173 174 175 176 177 178 179 180 181 182 183
  // Update the head segment to be the kept segment (if any).
  segment_head_ = keep;
}


void Zone::DeleteKeptSegment() {
#ifdef DEBUG
  // Constant byte value used for zapping dead memory in debug mode.
  static const unsigned char kZapDeadByte = 0xcd;
#endif

184 185
  DCHECK(segment_head_ == nullptr || segment_head_->next() == nullptr);
  if (segment_head_ != nullptr) {
186
    size_t size = segment_head_->size();
187
#ifdef DEBUG
188 189
    // Un-poison first so the zapping doesn't trigger ASan complaints.
    ASAN_UNPOISON_MEMORY_REGION(segment_head_, size);
190 191 192 193
    // Zap the entire kept segment (including the header).
    memset(segment_head_, kZapDeadByte, size);
#endif
    DeleteSegment(segment_head_, size);
194
    segment_head_ = nullptr;
195
  }
196

197
  DCHECK(segment_bytes_allocated_ == 0);
198 199 200
}


201 202
// Creates a new segment, sets it size, and pushes it to the front
// of the segment chain. Returns the new segment.
203
Segment* Zone::NewSegment(size_t size) {
204
  Segment* result = reinterpret_cast<Segment*>(Malloced::New(size));
205 206
  segment_bytes_allocated_ += size;
  if (result != nullptr) {
207
    result->Initialize(segment_head_, size);
208 209 210 211 212 213 214
    segment_head_ = result;
  }
  return result;
}


// Deletes the given segment. Does not touch the segment chain.
215
void Zone::DeleteSegment(Segment* segment, size_t size) {
216
  segment_bytes_allocated_ -= size;
217 218
  Malloced::Delete(segment);
}
219 220


221
Address Zone::NewExpand(size_t size) {
222 223
  // Make sure the requested size is already properly aligned and that
  // there isn't enough room in the Zone to satisfy the request.
224 225
  DCHECK_EQ(size, RoundDown(size, kAlignment));
  DCHECK_LT(limit_, position_ + size);
226 227 228 229 230

  // Compute the new segment size. We use a 'high water mark'
  // strategy, where we increase the segment size every time we expand
  // except that we employ a maximum segment size when we delete. This
  // is to avoid excessive malloc() and free() overhead.
231
  Segment* head = segment_head_;
232
  const size_t old_size = (head == nullptr) ? 0 : head->size();
233 234 235
  static const size_t kSegmentOverhead = sizeof(Segment) + kAlignment;
  const size_t new_size_no_overhead = size + (old_size << 1);
  size_t new_size = kSegmentOverhead + new_size_no_overhead;
236
  const size_t min_new_size = kSegmentOverhead + size;
237
  // Guard against integer overflow.
238
  if (new_size_no_overhead < size || new_size < kSegmentOverhead) {
239
    V8::FatalProcessOutOfMemory("Zone");
240
    return nullptr;
241
  }
242
  if (new_size < kMinimumSegmentSize) {
243
    new_size = kMinimumSegmentSize;
244
  } else if (new_size > kMaximumSegmentSize) {
245
    // Limit the size of new segments to avoid growing the segment size
246 247 248
    // exponentially, thus putting pressure on contiguous virtual address space.
    // All the while making sure to allocate a segment large enough to hold the
    // requested size.
249
    new_size = Max(min_new_size, kMaximumSegmentSize);
250
  }
251
  if (new_size > INT_MAX) {
252
    V8::FatalProcessOutOfMemory("Zone");
253
    return nullptr;
254
  }
255
  Segment* segment = NewSegment(new_size);
256
  if (segment == nullptr) {
257
    V8::FatalProcessOutOfMemory("Zone");
258
    return nullptr;
259
  }
260 261 262 263

  // Recompute 'top' and 'limit' based on the new segment.
  Address result = RoundUp(segment->start(), kAlignment);
  position_ = result + size;
264
  // Check for address overflow.
265 266
  // (Should not happen since the segment is guaranteed to accomodate
  // size bytes + header and alignment padding)
267 268
  DCHECK_GE(reinterpret_cast<uintptr_t>(position_),
            reinterpret_cast<uintptr_t>(result));
269
  limit_ = segment->end();
270
  DCHECK(position_ <= limit_);
271 272 273
  return result;
}

274 275
}  // namespace internal
}  // namespace v8