#include "src/zone/zone.h"
#include <cstring>
#include <memory>
#include "src/base/sanitizer/asan.h"
#include "src/init/v8.h"
#include "src/utils/utils.h"
#include "src/zone/type-stats.h"
namespace v8 {
namespace internal {
namespace {
#ifdef V8_USE_ADDRESS_SANITIZER
constexpr size_t kASanRedzoneBytes = 24;
#else
constexpr size_t kASanRedzoneBytes = 0;
#endif
}
Zone::Zone(AccountingAllocator* allocator, const char* name)
: allocator_(allocator), name_(name) {
allocator_->TraceZoneCreation(this);
}
Zone::~Zone() {
DeleteAll();
DCHECK_EQ(segment_bytes_allocated_.load(), 0);
}
void* Zone::AsanNew(size_t size) {
CHECK(!sealed_);
size = RoundUp(size, kAlignmentInBytes);
const size_t size_with_redzone = size + kASanRedzoneBytes;
DCHECK_LE(position_, limit_);
if (V8_UNLIKELY(size_with_redzone > limit_ - position_)) {
Expand(size_with_redzone);
}
DCHECK_LE(size_with_redzone, limit_ - position_);
Address result = position_;
position_ += size_with_redzone;
Address redzone_position = result + size;
DCHECK_EQ(redzone_position + kASanRedzoneBytes, position_);
ASAN_POISON_MEMORY_REGION(reinterpret_cast<void*>(redzone_position),
kASanRedzoneBytes);
DCHECK(IsAligned(result, kAlignmentInBytes));
return reinterpret_cast<void*>(result);
}
void Zone::Reset() {
if (!segment_head_) return;
Segment* keep = segment_head_;
segment_head_ = segment_head_->next();
if (segment_head_ != nullptr) {
position_ = segment_head_->end();
allocation_size_ -= segment_head_->end() - segment_head_->start();
}
keep->set_next(nullptr);
DeleteAll();
allocator_->TraceZoneCreation(this);
ASAN_UNPOISON_MEMORY_REGION(reinterpret_cast<void*>(keep->start()),
keep->capacity());
keep->ZapContents();
segment_head_ = keep;
position_ = RoundUp(keep->start(), kAlignmentInBytes);
limit_ = keep->end();
DCHECK_LT(allocation_size(), kAlignmentInBytes);
DCHECK_EQ(segment_bytes_allocated_, keep->total_size());
}
#ifdef DEBUG
bool Zone::Contains(const void* ptr) const {
Address address = reinterpret_cast<Address>(ptr);
for (Segment* segment = segment_head_; segment != nullptr;
segment = segment->next()) {
if (address >= segment->start() && address < segment->end()) {
return true;
}
}
return false;
}
#endif
ZoneSnapshot Zone::Snapshot() const { return ZoneSnapshot{this}; }
void Zone::DeleteAll() {
Segment* current = segment_head_;
if (current) {
allocation_size_ = allocation_size();
segment_head_ = nullptr;
}
allocator_->TraceZoneDestruction(this);
while (current) {
Segment* next = current->next();
segment_bytes_allocated_ -= current->total_size();
ReleaseSegment(current);
current = next;
}
position_ = limit_ = 0;
allocation_size_ = 0;
#ifdef V8_ENABLE_PRECISE_ZONE_STATS
allocation_size_for_tracing_ = 0;
#endif
}
void Zone::ReleaseSegment(Segment* segment) {
ASAN_UNPOISON_MEMORY_REGION(reinterpret_cast<void*>(segment->start()),
segment->capacity());
allocator_->ReturnSegment(segment);
}
void Zone::Expand(size_t size) {
DCHECK_EQ(size, RoundDown(size, kAlignmentInBytes));
DCHECK_LT(limit_ - position_, size);
Segment* head = segment_head_;
const size_t old_size = head ? head->total_size() : 0;
static const size_t kSegmentOverhead = sizeof(Segment) + kAlignmentInBytes;
const size_t new_size_no_overhead = size + (old_size << 1);
size_t new_size = kSegmentOverhead + new_size_no_overhead;
const size_t min_new_size = kSegmentOverhead + size;
if (new_size_no_overhead < size || new_size < kSegmentOverhead) {
V8::FatalProcessOutOfMemory(nullptr, "Zone");
}
if (new_size < kMinimumSegmentSize) {
new_size = kMinimumSegmentSize;
} else if (new_size >= kMaximumSegmentSize) {
new_size = std::max({min_new_size, kMaximumSegmentSize});
}
if (new_size > INT_MAX) {
V8::FatalProcessOutOfMemory(nullptr, "Zone");
}
Segment* segment = allocator_->AllocateSegment(new_size);
if (segment == nullptr) {
V8::FatalProcessOutOfMemory(nullptr, "Zone");
}
DCHECK_GE(segment->total_size(), new_size);
segment_bytes_allocated_ += segment->total_size();
segment->set_zone(this);
segment->set_next(segment_head_);
allocation_size_ = allocation_size();
segment_head_ = segment;
allocator_->TraceAllocateSegment(segment);
position_ = RoundUp(segment->start(), kAlignmentInBytes);
limit_ = segment->end();
DCHECK_LE(position_, limit_);
DCHECK_LE(size, limit_ - position_);
}
ZoneSnapshot::ZoneSnapshot(const Zone* zone)
:
#ifdef V8_ENABLE_PRECISE_ZONE_STATS
allocation_size_for_tracing_(zone->allocation_size_for_tracing_),
freed_size_for_tracing_(zone->freed_size_for_tracing_),
#endif
allocation_size_(zone->allocation_size_),
segment_bytes_allocated_(zone->segment_bytes_allocated_),
position_(zone->position_),
limit_(zone->limit_),
segment_head_(zone->segment_head_) {
}
void ZoneSnapshot::Restore(Zone* zone) const {
Segment* current = zone->segment_head_;
while (current != segment_head_) {
CHECK_NOT_NULL(current);
Segment* next = current->next();
zone->ReleaseSegment(current);
current = next;
}
if (segment_head_ != nullptr) {
void* const start = reinterpret_cast<void*>(position_);
DCHECK_GE(start, reinterpret_cast<void*>(current->start()));
DCHECK_LE(start, reinterpret_cast<void*>(current->end()));
const size_t length = current->end() - reinterpret_cast<Address>(start);
ASAN_UNPOISON_MEMORY_REGION(start, length);
}
zone->allocation_size_ = allocation_size_;
zone->segment_bytes_allocated_ = segment_bytes_allocated_;
zone->position_ = position_;
zone->limit_ = limit_;
zone->segment_head_ = segment_head_;
#ifdef V8_ENABLE_PRECISE_ZONE_STATS
zone->allocation_size_for_tracing_ = allocation_size_for_tracing_;
zone->freed_size_for_tracing_ = freed_size_for_tracing_;
#endif
}
}
}