diff --git a/src/heap/heap-inl.h b/src/heap/heap-inl.h index bab0f89164..26c9ad2b13 100644 --- a/src/heap/heap-inl.h +++ b/src/heap/heap-inl.h @@ -183,7 +183,12 @@ AllocationResult Heap::AllocateRaw(int size_in_bytes, AllocationType type, IncrementObjectCounters(); #endif - bool large_object = size_in_bytes > kMaxRegularHeapObjectSize; + size_t large_object_threshold = + AllocationType::kCode == type + ? std::min(kMaxRegularHeapObjectSize, code_space()->AreaSize()) + : kMaxRegularHeapObjectSize; + bool large_object = + static_cast(size_in_bytes) > large_object_threshold; HeapObject object; AllocationResult allocation; @@ -216,10 +221,10 @@ AllocationResult Heap::AllocateRaw(int size_in_bytes, AllocationType type, allocation = old_space_->AllocateRaw(size_in_bytes, alignment, origin); } } else if (AllocationType::kCode == type) { - if (size_in_bytes <= code_space()->AreaSize() && !large_object) { - allocation = code_space_->AllocateRawUnaligned(size_in_bytes); - } else { + if (large_object) { allocation = code_lo_space_->AllocateRaw(size_in_bytes); + } else { + allocation = code_space_->AllocateRawUnaligned(size_in_bytes); } } else if (AllocationType::kMap == type) { allocation = map_space_->AllocateRawUnaligned(size_in_bytes); diff --git a/src/heap/heap.h b/src/heap/heap.h index 1cfade4024..dd83a6b125 100644 --- a/src/heap/heap.h +++ b/src/heap/heap.h @@ -1441,8 +1441,10 @@ class Heap { // Heap object allocation tracking. ========================================== // =========================================================================== - void AddHeapObjectAllocationTracker(HeapObjectAllocationTracker* tracker); - void RemoveHeapObjectAllocationTracker(HeapObjectAllocationTracker* tracker); + V8_EXPORT_PRIVATE void AddHeapObjectAllocationTracker( + HeapObjectAllocationTracker* tracker); + V8_EXPORT_PRIVATE void RemoveHeapObjectAllocationTracker( + HeapObjectAllocationTracker* tracker); bool has_heap_object_allocation_tracker() const { return !allocation_trackers_.empty(); } diff --git a/test/cctest/heap/heap-tester.h b/test/cctest/heap/heap-tester.h index 5ee8d3330e..92ee4d7055 100644 --- a/test/cctest/heap/heap-tester.h +++ b/test/cctest/heap/heap-tester.h @@ -11,6 +11,7 @@ // Tests that should have access to private methods of {v8::internal::Heap}. // Those tests need to be defined using HEAP_TEST(Name) { ... }. #define HEAP_TEST_METHODS(V) \ + V(CodeLargeObjectSpace) \ V(CompactionFullAbortedPage) \ V(CompactionPartiallyAbortedPage) \ V(CompactionPartiallyAbortedPageIntraAbortedPointers) \ diff --git a/test/cctest/heap/test-heap.cc b/test/cctest/heap/test-heap.cc index 052f031b4e..2225d6ee3c 100644 --- a/test/cctest/heap/test-heap.cc +++ b/test/cctest/heap/test-heap.cc @@ -7196,6 +7196,39 @@ TEST(Regress10698) { filler.set_map_after_allocation(*factory->one_pointer_filler_map()); } +class TestAllocationTracker : public HeapObjectAllocationTracker { + public: + explicit TestAllocationTracker(int expected_size) + : expected_size_(expected_size) {} + + void AllocationEvent(Address addr, int size) { + CHECK(expected_size_ == size); + address_ = addr; + } + + Address address() { return address_; } + + private: + int expected_size_; + Address address_; +}; + +HEAP_TEST(CodeLargeObjectSpace) { + Heap* heap = CcTest::heap(); + int size_in_bytes = kMaxRegularHeapObjectSize + kSystemPointerSize; + TestAllocationTracker allocation_tracker{size_in_bytes}; + heap->AddHeapObjectAllocationTracker(&allocation_tracker); + + AllocationResult allocation = heap->AllocateRaw( + size_in_bytes, AllocationType::kCode, AllocationOrigin::kGeneratedCode, + AllocationAlignment::kCodeAligned); + + CHECK(allocation.ToAddress() == allocation_tracker.address()); + heap->CreateFillerObjectAt(allocation.ToAddress(), size_in_bytes, + ClearRecordedSlots::kNo); + heap->RemoveHeapObjectAllocationTracker(&allocation_tracker); +} + } // namespace heap } // namespace internal } // namespace v8