11cb0ef41Sopenharmony_ci// Copyright 2022 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 INCLUDE_CPPGC_INTERNAL_CAGED_HEAP_H_ 61cb0ef41Sopenharmony_ci#define INCLUDE_CPPGC_INTERNAL_CAGED_HEAP_H_ 71cb0ef41Sopenharmony_ci 81cb0ef41Sopenharmony_ci#include <climits> 91cb0ef41Sopenharmony_ci#include <cstddef> 101cb0ef41Sopenharmony_ci 111cb0ef41Sopenharmony_ci#include "cppgc/internal/api-constants.h" 121cb0ef41Sopenharmony_ci#include "cppgc/internal/base-page-handle.h" 131cb0ef41Sopenharmony_ci#include "v8config.h" // NOLINT(build/include_directory) 141cb0ef41Sopenharmony_ci 151cb0ef41Sopenharmony_ci#if defined(CPPGC_CAGED_HEAP) 161cb0ef41Sopenharmony_ci 171cb0ef41Sopenharmony_cinamespace cppgc { 181cb0ef41Sopenharmony_cinamespace internal { 191cb0ef41Sopenharmony_ci 201cb0ef41Sopenharmony_ciclass V8_EXPORT CagedHeapBase { 211cb0ef41Sopenharmony_ci public: 221cb0ef41Sopenharmony_ci V8_INLINE static uintptr_t OffsetFromAddress(const void* address) { 231cb0ef41Sopenharmony_ci return reinterpret_cast<uintptr_t>(address) & 241cb0ef41Sopenharmony_ci (api_constants::kCagedHeapReservationAlignment - 1); 251cb0ef41Sopenharmony_ci } 261cb0ef41Sopenharmony_ci 271cb0ef41Sopenharmony_ci V8_INLINE static bool IsWithinCage(const void* address) { 281cb0ef41Sopenharmony_ci CPPGC_DCHECK(g_heap_base_); 291cb0ef41Sopenharmony_ci return (reinterpret_cast<uintptr_t>(address) & 301cb0ef41Sopenharmony_ci ~(api_constants::kCagedHeapReservationAlignment - 1)) == 311cb0ef41Sopenharmony_ci g_heap_base_; 321cb0ef41Sopenharmony_ci } 331cb0ef41Sopenharmony_ci 341cb0ef41Sopenharmony_ci V8_INLINE static bool AreWithinCage(const void* addr1, const void* addr2) { 351cb0ef41Sopenharmony_ci#if defined(CPPGC_2GB_CAGE) 361cb0ef41Sopenharmony_ci static constexpr size_t kHalfWordShift = sizeof(uint32_t) * CHAR_BIT - 1; 371cb0ef41Sopenharmony_ci#else //! defined(CPPGC_2GB_CAGE) 381cb0ef41Sopenharmony_ci static constexpr size_t kHalfWordShift = sizeof(uint32_t) * CHAR_BIT; 391cb0ef41Sopenharmony_ci#endif //! defined(CPPGC_2GB_CAGE) 401cb0ef41Sopenharmony_ci static_assert((static_cast<size_t>(1) << kHalfWordShift) == 411cb0ef41Sopenharmony_ci api_constants::kCagedHeapReservationSize); 421cb0ef41Sopenharmony_ci CPPGC_DCHECK(g_heap_base_); 431cb0ef41Sopenharmony_ci return !(((reinterpret_cast<uintptr_t>(addr1) ^ g_heap_base_) | 441cb0ef41Sopenharmony_ci (reinterpret_cast<uintptr_t>(addr2) ^ g_heap_base_)) >> 451cb0ef41Sopenharmony_ci kHalfWordShift); 461cb0ef41Sopenharmony_ci } 471cb0ef41Sopenharmony_ci 481cb0ef41Sopenharmony_ci V8_INLINE static uintptr_t GetBase() { return g_heap_base_; } 491cb0ef41Sopenharmony_ci 501cb0ef41Sopenharmony_ci private: 511cb0ef41Sopenharmony_ci friend class CagedHeap; 521cb0ef41Sopenharmony_ci 531cb0ef41Sopenharmony_ci static uintptr_t g_heap_base_; 541cb0ef41Sopenharmony_ci}; 551cb0ef41Sopenharmony_ci 561cb0ef41Sopenharmony_ci} // namespace internal 571cb0ef41Sopenharmony_ci} // namespace cppgc 581cb0ef41Sopenharmony_ci 591cb0ef41Sopenharmony_ci#endif // defined(CPPGC_CAGED_HEAP) 601cb0ef41Sopenharmony_ci 611cb0ef41Sopenharmony_ci#endif // INCLUDE_CPPGC_INTERNAL_CAGED_HEAP_H_ 62