Commit 8034b9a5 authored by Igor Sheludko's avatar Igor Sheludko Committed by Commit Bot

[heap] Add missing notification about freed code range

It was accidentally removed in CL that introduced BoundedPageAllocator.

This CL also cleans up the CodeRangeAddressHint a bit.

Bug: v8:8096, chromium:887252
Change-Id: Idc84796dd1ff1b440cbe3515732984264defcf2d
Reviewed-on: https://chromium-review.googlesource.com/1249125
Commit-Queue: Igor Sheludko <ishell@chromium.org>
Reviewed-by: 's avatarMichael Lippautz <mlippautz@chromium.org>
Cr-Commit-Position: refs/heads/master@{#56287}
parent fa65063a
......@@ -97,18 +97,18 @@ PauseAllocationObserversScope::~PauseAllocationObserversScope() {
static base::LazyInstance<CodeRangeAddressHint>::type code_range_address_hint =
LAZY_INSTANCE_INITIALIZER;
void* CodeRangeAddressHint::GetAddressHint(size_t code_range_size) {
Address CodeRangeAddressHint::GetAddressHint(size_t code_range_size) {
base::LockGuard<base::Mutex> guard(&mutex_);
auto it = recently_freed_.find(code_range_size);
if (it == recently_freed_.end() || it->second.empty()) {
return GetRandomMmapAddr();
return reinterpret_cast<Address>(GetRandomMmapAddr());
}
void* result = it->second.back();
Address result = it->second.back();
it->second.pop_back();
return result;
}
void CodeRangeAddressHint::NotifyFreedCodeRange(void* code_range_start,
void CodeRangeAddressHint::NotifyFreedCodeRange(Address code_range_start,
size_t code_range_size) {
base::LockGuard<base::Mutex> guard(&mutex_);
recently_freed_[code_range_size].push_back(code_range_start);
......@@ -158,9 +158,11 @@ void MemoryAllocator::InitializeCodePageAllocator(
}
DCHECK(!kRequiresCodeRange || requested <= kMaximalCodeRangeSize);
void* hint = code_range_address_hint.Pointer()->GetAddressHint(requested);
Address hint =
RoundDown(code_range_address_hint.Pointer()->GetAddressHint(requested),
page_allocator->AllocatePageSize());
VirtualMemory reservation(
page_allocator, requested, hint,
page_allocator, requested, reinterpret_cast<void*>(hint),
Max(kCodeRangeAreaAlignment, page_allocator->AllocatePageSize()));
if (!reservation.IsReserved()) {
V8::FatalProcessOutOfMemory(isolate_,
......@@ -213,6 +215,16 @@ void MemoryAllocator::TearDown() {
if (last_chunk_.IsReserved()) {
last_chunk_.Free();
}
if (code_page_allocator_instance_.get()) {
DCHECK(!code_range_.is_empty());
code_range_address_hint.Pointer()->NotifyFreedCodeRange(code_range_.begin(),
code_range_.size());
code_range_ = base::AddressRegion();
code_page_allocator_instance_.reset();
}
code_page_allocator_ = nullptr;
data_page_allocator_ = nullptr;
}
class MemoryAllocator::Unmapper::UnmapFreeMemoryTask : public CancelableTask {
......
......@@ -1088,9 +1088,9 @@ class CodeRangeAddressHint {
public:
// Returns the most recently freed code range start address for the given
// size. If there is no such entry, then a random address is returned.
V8_EXPORT_PRIVATE void* GetAddressHint(size_t code_range_size);
V8_EXPORT_PRIVATE Address GetAddressHint(size_t code_range_size);
V8_EXPORT_PRIVATE void NotifyFreedCodeRange(void* code_range_start,
V8_EXPORT_PRIVATE void NotifyFreedCodeRange(Address code_range_start,
size_t code_range_size);
private:
......@@ -1099,7 +1099,7 @@ class CodeRangeAddressHint {
// addresses. There should be O(1) different code range sizes.
// The length of each array is limited by the peak number of code ranges,
// which should be also O(1).
std::map<size_t, std::vector<void*>> recently_freed_;
std::unordered_map<size_t, std::vector<Address>> recently_freed_;
};
class SkipList {
......
......@@ -118,9 +118,9 @@ TEST_F(SpacesTest, WriteBarrierInNewSpaceFromSpace) {
TEST_F(SpacesTest, CodeRangeAddressReuse) {
CodeRangeAddressHint hint;
// Create code ranges.
void* code_range1 = hint.GetAddressHint(100);
void* code_range2 = hint.GetAddressHint(200);
void* code_range3 = hint.GetAddressHint(100);
Address code_range1 = hint.GetAddressHint(100);
Address code_range2 = hint.GetAddressHint(200);
Address code_range3 = hint.GetAddressHint(100);
// Since the addresses are random, we cannot check that they are different.
......@@ -129,14 +129,14 @@ TEST_F(SpacesTest, CodeRangeAddressReuse) {
hint.NotifyFreedCodeRange(code_range2, 200);
// The next two code ranges should reuse the freed addresses.
void* code_range4 = hint.GetAddressHint(100);
Address code_range4 = hint.GetAddressHint(100);
EXPECT_EQ(code_range4, code_range1);
void* code_range5 = hint.GetAddressHint(200);
Address code_range5 = hint.GetAddressHint(200);
EXPECT_EQ(code_range5, code_range2);
// Free the third code range and check address reuse.
hint.NotifyFreedCodeRange(code_range3, 100);
void* code_range6 = hint.GetAddressHint(100);
Address code_range6 = hint.GetAddressHint(100);
EXPECT_EQ(code_range6, code_range3);
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment