11cb0ef41Sopenharmony_ci// Copyright 2021 the V8 project authors. All rights reserved.
21cb0ef41Sopenharmony_ci// Use of this source code is governed by a BSD-style license that can be
31cb0ef41Sopenharmony_ci// found in the LICENSE file.
41cb0ef41Sopenharmony_ci
51cb0ef41Sopenharmony_ci#ifndef V8_HEAP_CODE_RANGE_H_
61cb0ef41Sopenharmony_ci#define V8_HEAP_CODE_RANGE_H_
71cb0ef41Sopenharmony_ci
81cb0ef41Sopenharmony_ci#include <unordered_map>
91cb0ef41Sopenharmony_ci#include <vector>
101cb0ef41Sopenharmony_ci
111cb0ef41Sopenharmony_ci#include "src/base/platform/mutex.h"
121cb0ef41Sopenharmony_ci#include "src/common/globals.h"
131cb0ef41Sopenharmony_ci#include "src/utils/allocation.h"
141cb0ef41Sopenharmony_ci
151cb0ef41Sopenharmony_cinamespace v8 {
161cb0ef41Sopenharmony_cinamespace internal {
171cb0ef41Sopenharmony_ci
181cb0ef41Sopenharmony_ci// The process-wide singleton that keeps track of code range regions with the
191cb0ef41Sopenharmony_ci// intention to reuse free code range regions as a workaround for CFG memory
201cb0ef41Sopenharmony_ci// leaks (see crbug.com/870054).
211cb0ef41Sopenharmony_ciclass CodeRangeAddressHint {
221cb0ef41Sopenharmony_ci public:
231cb0ef41Sopenharmony_ci  // When near code range is enabled, an address within
241cb0ef41Sopenharmony_ci  // kMaxPCRelativeCodeRangeInMB to the embedded blob is returned if
251cb0ef41Sopenharmony_ci  // there is enough space. Otherwise a random address is returned.
261cb0ef41Sopenharmony_ci  // When near code range is disabled, returns the most recently freed code
271cb0ef41Sopenharmony_ci  // range start address for the given size. If there is no such entry, then a
281cb0ef41Sopenharmony_ci  // random address is returned.
291cb0ef41Sopenharmony_ci  V8_EXPORT_PRIVATE Address GetAddressHint(size_t code_range_size,
301cb0ef41Sopenharmony_ci                                           size_t alignment);
311cb0ef41Sopenharmony_ci
321cb0ef41Sopenharmony_ci  V8_EXPORT_PRIVATE void NotifyFreedCodeRange(Address code_range_start,
331cb0ef41Sopenharmony_ci                                              size_t code_range_size);
341cb0ef41Sopenharmony_ci
351cb0ef41Sopenharmony_ci private:
361cb0ef41Sopenharmony_ci  base::Mutex mutex_;
371cb0ef41Sopenharmony_ci  // A map from code range size to an array of recently freed code range
381cb0ef41Sopenharmony_ci  // addresses. There should be O(1) different code range sizes.
391cb0ef41Sopenharmony_ci  // The length of each array is limited by the peak number of code ranges,
401cb0ef41Sopenharmony_ci  // which should be also O(1).
411cb0ef41Sopenharmony_ci  std::unordered_map<size_t, std::vector<Address>> recently_freed_;
421cb0ef41Sopenharmony_ci};
431cb0ef41Sopenharmony_ci
441cb0ef41Sopenharmony_ci// A code range is a virtual memory cage that may contain executable code. It
451cb0ef41Sopenharmony_ci// has the following layout.
461cb0ef41Sopenharmony_ci//
471cb0ef41Sopenharmony_ci// +------------+-----+----------------  ~~~  -+
481cb0ef41Sopenharmony_ci// |     RW     | ... |    ...                 |
491cb0ef41Sopenharmony_ci// +------------+-----+----------------- ~~~  -+
501cb0ef41Sopenharmony_ci// ^            ^     ^
511cb0ef41Sopenharmony_ci// start        base  allocatable base
521cb0ef41Sopenharmony_ci//
531cb0ef41Sopenharmony_ci// <------------>     <------------------------>
541cb0ef41Sopenharmony_ci//   reserved            allocatable region
551cb0ef41Sopenharmony_ci// <------------------------------------------->
561cb0ef41Sopenharmony_ci//               code region
571cb0ef41Sopenharmony_ci//
581cb0ef41Sopenharmony_ci// The start of the reservation may include reserved page with read-write access
591cb0ef41Sopenharmony_ci// as required by some platforms (Win64). The cage's page allocator does not
601cb0ef41Sopenharmony_ci// control the optional reserved page in the beginning of the code region.
611cb0ef41Sopenharmony_ci//
621cb0ef41Sopenharmony_ci// The following conditions hold:
631cb0ef41Sopenharmony_ci// 1) |reservation()->region()| >= |optional RW pages| +
641cb0ef41Sopenharmony_ci//    |reservation()->page_allocator()|
651cb0ef41Sopenharmony_ci// 2) |reservation()| is AllocatePageSize()-aligned
661cb0ef41Sopenharmony_ci// 3) |reservation()->page_allocator()| (i.e. allocatable base) is
671cb0ef41Sopenharmony_ci//    MemoryChunk::kAlignment-aligned
681cb0ef41Sopenharmony_ci// 4) |base()| is CommitPageSize()-aligned
691cb0ef41Sopenharmony_ciclass CodeRange final : public VirtualMemoryCage {
701cb0ef41Sopenharmony_ci public:
711cb0ef41Sopenharmony_ci  V8_EXPORT_PRIVATE ~CodeRange() override;
721cb0ef41Sopenharmony_ci
731cb0ef41Sopenharmony_ci  // Returns the size of the initial area of a code-range, which is marked
741cb0ef41Sopenharmony_ci  // writable and reserved to contain unwind information.
751cb0ef41Sopenharmony_ci  static size_t GetWritableReservedAreaSize();
761cb0ef41Sopenharmony_ci
771cb0ef41Sopenharmony_ci  uint8_t* embedded_blob_code_copy() const {
781cb0ef41Sopenharmony_ci    // remap_embedded_builtins_mutex_ is designed to protect write contention to
791cb0ef41Sopenharmony_ci    // embedded_blob_code_copy_. It is safe to be read without taking the
801cb0ef41Sopenharmony_ci    // mutex. It is read to check if short builtins ought to be enabled because
811cb0ef41Sopenharmony_ci    // a shared CodeRange has already remapped builtins and to find where the
821cb0ef41Sopenharmony_ci    // instruction stream for a builtin is.
831cb0ef41Sopenharmony_ci    //
841cb0ef41Sopenharmony_ci    // For the first, this racing with an Isolate calling RemapEmbeddedBuiltins
851cb0ef41Sopenharmony_ci    // may result in disabling short builtins, which is not a correctness issue.
861cb0ef41Sopenharmony_ci    //
871cb0ef41Sopenharmony_ci    // For the second, this racing with an Isolate calling RemapEmbeddedBuiltins
881cb0ef41Sopenharmony_ci    // may result in an already running Isolate that did not have short builtins
891cb0ef41Sopenharmony_ci    // enabled (due to max old generation size) to switch over to using remapped
901cb0ef41Sopenharmony_ci    // builtins, which is also not a correctness issue as the remapped builtins
911cb0ef41Sopenharmony_ci    // are byte-equivalent.
921cb0ef41Sopenharmony_ci    //
931cb0ef41Sopenharmony_ci    // Both these scenarios should be rare. The initial Isolate is usually
941cb0ef41Sopenharmony_ci    // created by itself, i.e. without contention. Additionally, the first
951cb0ef41Sopenharmony_ci    // Isolate usually remaps builtins on machines with enough memory, not
961cb0ef41Sopenharmony_ci    // subsequent Isolates in the same process.
971cb0ef41Sopenharmony_ci    return embedded_blob_code_copy_.load(std::memory_order_acquire);
981cb0ef41Sopenharmony_ci  }
991cb0ef41Sopenharmony_ci
1001cb0ef41Sopenharmony_ci#ifdef V8_OS_WIN64
1011cb0ef41Sopenharmony_ci  // 64-bit Windows needs to track how many Isolates are using the CodeRange for
1021cb0ef41Sopenharmony_ci  // registering and unregistering of unwind info. Note that even though
1031cb0ef41Sopenharmony_ci  // CodeRanges are used with std::shared_ptr, std::shared_ptr::use_count should
1041cb0ef41Sopenharmony_ci  // not be used for synchronization as it's usually implemented with a relaxed
1051cb0ef41Sopenharmony_ci  // read.
1061cb0ef41Sopenharmony_ci  uint32_t AtomicIncrementUnwindInfoUseCount() {
1071cb0ef41Sopenharmony_ci    return unwindinfo_use_count_.fetch_add(1, std::memory_order_acq_rel);
1081cb0ef41Sopenharmony_ci  }
1091cb0ef41Sopenharmony_ci
1101cb0ef41Sopenharmony_ci  uint32_t AtomicDecrementUnwindInfoUseCount() {
1111cb0ef41Sopenharmony_ci    return unwindinfo_use_count_.fetch_sub(1, std::memory_order_acq_rel);
1121cb0ef41Sopenharmony_ci  }
1131cb0ef41Sopenharmony_ci#endif  // V8_OS_WIN64
1141cb0ef41Sopenharmony_ci
1151cb0ef41Sopenharmony_ci  bool InitReservation(v8::PageAllocator* page_allocator, size_t requested);
1161cb0ef41Sopenharmony_ci
1171cb0ef41Sopenharmony_ci  void Free();
1181cb0ef41Sopenharmony_ci
1191cb0ef41Sopenharmony_ci  // Remap and copy the embedded builtins into this CodeRange. This method is
1201cb0ef41Sopenharmony_ci  // idempotent and only performs the copy once. This property is so that this
1211cb0ef41Sopenharmony_ci  // method can be used uniformly regardless of having a per-Isolate or a shared
1221cb0ef41Sopenharmony_ci  // pointer cage. Returns the address of the copy.
1231cb0ef41Sopenharmony_ci  //
1241cb0ef41Sopenharmony_ci  // The builtins code region will be freed with the code range at tear down.
1251cb0ef41Sopenharmony_ci  //
1261cb0ef41Sopenharmony_ci  // When ENABLE_SLOW_DCHECKS is on, the contents of the embedded_blob_code are
1271cb0ef41Sopenharmony_ci  // compared against the already copied version.
1281cb0ef41Sopenharmony_ci  uint8_t* RemapEmbeddedBuiltins(Isolate* isolate,
1291cb0ef41Sopenharmony_ci                                 const uint8_t* embedded_blob_code,
1301cb0ef41Sopenharmony_ci                                 size_t embedded_blob_code_size);
1311cb0ef41Sopenharmony_ci
1321cb0ef41Sopenharmony_ci  static std::shared_ptr<CodeRange> EnsureProcessWideCodeRange(
1331cb0ef41Sopenharmony_ci      v8::PageAllocator* page_allocator, size_t requested_size);
1341cb0ef41Sopenharmony_ci
1351cb0ef41Sopenharmony_ci  // If InitializeProcessWideCodeRangeOnce has been called, returns the
1361cb0ef41Sopenharmony_ci  // initialized CodeRange. Otherwise returns an empty std::shared_ptr.
1371cb0ef41Sopenharmony_ci  V8_EXPORT_PRIVATE static std::shared_ptr<CodeRange> GetProcessWideCodeRange();
1381cb0ef41Sopenharmony_ci
1391cb0ef41Sopenharmony_ci private:
1401cb0ef41Sopenharmony_ci  // Used when short builtin calls are enabled, where embedded builtins are
1411cb0ef41Sopenharmony_ci  // copied into the CodeRange so calls can be nearer.
1421cb0ef41Sopenharmony_ci  std::atomic<uint8_t*> embedded_blob_code_copy_{nullptr};
1431cb0ef41Sopenharmony_ci
1441cb0ef41Sopenharmony_ci  // When sharing a CodeRange among Isolates, calls to RemapEmbeddedBuiltins may
1451cb0ef41Sopenharmony_ci  // race during Isolate::Init.
1461cb0ef41Sopenharmony_ci  base::Mutex remap_embedded_builtins_mutex_;
1471cb0ef41Sopenharmony_ci
1481cb0ef41Sopenharmony_ci#ifdef V8_OS_WIN64
1491cb0ef41Sopenharmony_ci  std::atomic<uint32_t> unwindinfo_use_count_{0};
1501cb0ef41Sopenharmony_ci#endif
1511cb0ef41Sopenharmony_ci};
1521cb0ef41Sopenharmony_ci
1531cb0ef41Sopenharmony_ci}  // namespace internal
1541cb0ef41Sopenharmony_ci}  // namespace v8
1551cb0ef41Sopenharmony_ci
1561cb0ef41Sopenharmony_ci#endif  // V8_HEAP_CODE_RANGE_H_
157