Skip to content

Commit 57564eb

Browse files
danbevaddaleax
authored andcommittedSep 23, 2020
deps: V8: cherry-pick 71736859756b2bd0444bdb0a87a
Original commit message: [heap] Add large_object_threshold to AllocateRaw This commit adds a check in Heap::AllocateRaw when setting the large_object variable, when the AllocationType is of type kCode, to take into account the size of the CodeSpace's area size. The motivation for this change is that without this check it is possible that size_in_bytes is less than 128, and hence not considered a large object, but it might be larger than the available space in code_space->AreaSize(), which will cause the object to be created in the CodeLargeObjectSpace. This will later cause a segmentation fault when calling the following chain of functions: if (!large_object) { MemoryChunk::FromHeapObject(heap_object) ->GetCodeObjectRegistry() ->RegisterNewlyAllocatedCodeObject(heap_object.address()); } We (Red Hat) ran into this issue when running Node.js v12.16.1 in combination with yarn on aarch64 (this was the only architecture that this happed on). Bug: v8:10808 Change-Id: I0c396b0eb64bc4cc91d9a3be521254f3130eac7b Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/2390665 Commit-Queue: Ulan Degenbaev <ulan@chromium.org> Reviewed-by: Ulan Degenbaev <ulan@chromium.org> Cr-Commit-Position: refs/heads/master@{#69876} Refs: v8/v8@7173685 PR-URL: #35205 Reviewed-By: Anna Henningsen <anna@addaleax.net>
1 parent 26ede7f commit 57564eb

File tree

5 files changed

+48
-7
lines changed

5 files changed

+48
-7
lines changed
 

‎common.gypi

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434

3535
# Reset this number to 0 on major V8 upgrades.
3636
# Increment by one for each non-official patch applied to deps/v8.
37-
'v8_embedder_string': '-node.40',
37+
'v8_embedder_string': '-node.41',
3838

3939
##### V8 defaults for Node.js #####
4040

‎deps/v8/src/heap/heap-inl.h

+10-4
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,13 @@ AllocationResult Heap::AllocateRaw(int size_in_bytes, AllocationType type,
171171
IncrementObjectCounters();
172172
#endif
173173

174-
bool large_object = size_in_bytes > kMaxRegularHeapObjectSize;
174+
size_t large_object_threshold =
175+
AllocationType::kCode == type
176+
? std::min(kMaxRegularHeapObjectSize, code_space()->AreaSize())
177+
: kMaxRegularHeapObjectSize;
178+
bool large_object =
179+
static_cast<size_t>(size_in_bytes) > large_object_threshold;
180+
175181

176182
HeapObject object;
177183
AllocationResult allocation;
@@ -200,10 +206,10 @@ AllocationResult Heap::AllocateRaw(int size_in_bytes, AllocationType type,
200206
allocation = old_space_->AllocateRaw(size_in_bytes, alignment, origin);
201207
}
202208
} else if (AllocationType::kCode == type) {
203-
if (size_in_bytes <= code_space()->AreaSize() && !large_object) {
204-
allocation = code_space_->AllocateRawUnaligned(size_in_bytes);
205-
} else {
209+
if (large_object) {
206210
allocation = code_lo_space_->AllocateRaw(size_in_bytes);
211+
} else {
212+
allocation = code_space_->AllocateRawUnaligned(size_in_bytes);
207213
}
208214
} else if (AllocationType::kMap == type) {
209215
allocation = map_space_->AllocateRawUnaligned(size_in_bytes);

‎deps/v8/src/heap/heap.h

+4-2
Original file line numberDiff line numberDiff line change
@@ -1262,8 +1262,10 @@ class Heap {
12621262
// Heap object allocation tracking. ==========================================
12631263
// ===========================================================================
12641264

1265-
void AddHeapObjectAllocationTracker(HeapObjectAllocationTracker* tracker);
1266-
void RemoveHeapObjectAllocationTracker(HeapObjectAllocationTracker* tracker);
1265+
V8_EXPORT_PRIVATE void AddHeapObjectAllocationTracker(
1266+
HeapObjectAllocationTracker* tracker);
1267+
V8_EXPORT_PRIVATE void RemoveHeapObjectAllocationTracker(
1268+
HeapObjectAllocationTracker* tracker);
12671269
bool has_heap_object_allocation_tracker() const {
12681270
return !allocation_trackers_.empty();
12691271
}

‎deps/v8/test/cctest/heap/heap-tester.h

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
// Tests that should have access to private methods of {v8::internal::Heap}.
1212
// Those tests need to be defined using HEAP_TEST(Name) { ... }.
1313
#define HEAP_TEST_METHODS(V) \
14+
V(CodeLargeObjectSpace) \
1415
V(CompactionFullAbortedPage) \
1516
V(CompactionPartiallyAbortedPage) \
1617
V(CompactionPartiallyAbortedPageIntraAbortedPointers) \

‎deps/v8/test/cctest/heap/test-heap.cc

+32
Original file line numberDiff line numberDiff line change
@@ -6754,6 +6754,38 @@ TEST(CodeObjectRegistry) {
67546754
CHECK(MemoryChunk::FromAddress(code2_address)->Contains(code2_address));
67556755
}
67566756

6757+
class TestAllocationTracker : public HeapObjectAllocationTracker {
6758+
public:
6759+
explicit TestAllocationTracker(int expected_size) : expected_size_(expected_size) {}
6760+
6761+
void AllocationEvent(Address addr, int size) {
6762+
CHECK(expected_size_ == size);
6763+
address_ = addr;
6764+
}
6765+
6766+
Address address() { return address_; }
6767+
6768+
private:
6769+
int expected_size_;
6770+
Address address_;
6771+
};
6772+
6773+
HEAP_TEST(CodeLargeObjectSpace) {
6774+
Heap* heap = CcTest::heap();
6775+
int size_in_bytes = kMaxRegularHeapObjectSize + kSystemPointerSize;
6776+
TestAllocationTracker allocation_tracker{size_in_bytes};
6777+
heap->AddHeapObjectAllocationTracker(&allocation_tracker);
6778+
6779+
AllocationResult allocation = heap->AllocateRaw(
6780+
size_in_bytes, AllocationType::kCode, AllocationOrigin::kGeneratedCode,
6781+
AllocationAlignment::kWordAligned);
6782+
6783+
CHECK(allocation.ToObjectChecked().address() == allocation_tracker.address());
6784+
heap->CreateFillerObjectAt(allocation.ToObjectChecked().address(),
6785+
size_in_bytes, ClearRecordedSlots::kNo);
6786+
heap->RemoveHeapObjectAllocationTracker(&allocation_tracker);
6787+
}
6788+
67576789
} // namespace heap
67586790
} // namespace internal
67596791
} // namespace v8

0 commit comments

Comments
 (0)
Please sign in to comment.