From 4ecd70a284b9bc658d5ad6524d9cd1d7b9d80e15 Mon Sep 17 00:00:00 2001 From: Igor Sheludko Date: Tue, 11 Jan 2022 14:29:03 +0100 Subject: [PATCH] [ext-code-space] Fix CodeRange hint logic on Windows and Linux Windows requires additional writable page to be allocated in front of the code range, but at the same time the code range must not cross 4 GB boundary in order to make Code pointer compression work for Code pointers. All these constraints make the logic of hint calculation too dependent on what VirtualMemoryCage::InitReservation() would do with the provided hint. This CL simplifies the hint calculation and fully relies on VirtualMemoryCage::InitReservation() to do the right thing. On Linux the implementation of OS::GetFreeMemoryRangesWithin() doesn't work when Chromium sandbox is enabled, so we use the beginning of the preferred short builtin calls region as a hint. It should be at least as good as the fallback hint but with higher chances to point to free address space location. Bug: v8:11880 Change-Id: I0b6ebec98dd0cf483f67e6ba8a919deb9ce7cc25 Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/3380585 Reviewed-by: Michael Lippautz Commit-Queue: Igor Sheludko Cr-Commit-Position: refs/heads/main@{#78568} --- src/heap/code-range.cc | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/src/heap/code-range.cc b/src/heap/code-range.cc index 5c5911d676..c4cb88ed6a 100644 --- a/src/heap/code-range.cc +++ b/src/heap/code-range.cc @@ -53,6 +53,11 @@ Address CodeRangeAddressHint::GetAddressHint(size_t code_range_size, CHECK(IsAligned(result, alignment)); return result; } + // The empty memory_ranges means that GetFreeMemoryRangesWithin() API + // is not supported, so use the lowest address from the preferred region + // as a hint because it'll be at least as good as the fallback hint but + // with a higher chances to point to the free address space range. + return RoundUp(preferred_region.begin(), alignment); } return RoundUp(FUNCTION_ADDR(&FunctionInStaticBinaryForAddressHint), alignment); @@ -124,16 +129,8 @@ bool CodeRange::InitReservation(v8::PageAllocator* page_allocator, : VirtualMemoryCage::ReservationParams::kAnyBaseAlignment; params.base_bias_size = RoundUp(reserved_area, allocate_page_size); params.page_size = MemoryChunk::kPageSize; - // V8_EXTERNAL_CODE_SPACE imposes additional alignment requirement for the - // base address, so make sure the hint calculation function takes that into - // account. Otherwise the allocated reservation might be outside of the - // preferred region (see Isolate::GetShortBuiltinsCallRegion()). - const size_t hint_alignment = - V8_EXTERNAL_CODE_SPACE_BOOL - ? RoundUp(params.base_alignment, allocate_page_size) - : allocate_page_size; params.requested_start_hint = - GetCodeRangeAddressHint()->GetAddressHint(requested, hint_alignment); + GetCodeRangeAddressHint()->GetAddressHint(requested, allocate_page_size); if (!VirtualMemoryCage::InitReservation(params)) return false;