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#include "src/base/sanitizer/lsan-virtual-address-space.h"
61cb0ef41Sopenharmony_ci
71cb0ef41Sopenharmony_ci#include "include/v8-platform.h"
81cb0ef41Sopenharmony_ci#include "src/base/logging.h"
91cb0ef41Sopenharmony_ci
101cb0ef41Sopenharmony_ci#if defined(LEAK_SANITIZER)
111cb0ef41Sopenharmony_ci#include <sanitizer/lsan_interface.h>
121cb0ef41Sopenharmony_ci#endif
131cb0ef41Sopenharmony_ci
141cb0ef41Sopenharmony_cinamespace v8 {
151cb0ef41Sopenharmony_cinamespace base {
161cb0ef41Sopenharmony_ci
171cb0ef41Sopenharmony_ciLsanVirtualAddressSpace::LsanVirtualAddressSpace(
181cb0ef41Sopenharmony_ci    std::unique_ptr<v8::VirtualAddressSpace> vas)
191cb0ef41Sopenharmony_ci    : VirtualAddressSpace(vas->page_size(), vas->allocation_granularity(),
201cb0ef41Sopenharmony_ci                          vas->base(), vas->size(),
211cb0ef41Sopenharmony_ci                          vas->max_page_permissions()),
221cb0ef41Sopenharmony_ci      vas_(std::move(vas)) {
231cb0ef41Sopenharmony_ci  DCHECK_NOT_NULL(vas_);
241cb0ef41Sopenharmony_ci}
251cb0ef41Sopenharmony_ci
261cb0ef41Sopenharmony_ciAddress LsanVirtualAddressSpace::AllocatePages(Address hint, size_t size,
271cb0ef41Sopenharmony_ci                                               size_t alignment,
281cb0ef41Sopenharmony_ci                                               PagePermissions permissions) {
291cb0ef41Sopenharmony_ci  Address result = vas_->AllocatePages(hint, size, alignment, permissions);
301cb0ef41Sopenharmony_ci#if defined(LEAK_SANITIZER)
311cb0ef41Sopenharmony_ci  if (result) {
321cb0ef41Sopenharmony_ci    __lsan_register_root_region(reinterpret_cast<void*>(result), size);
331cb0ef41Sopenharmony_ci  }
341cb0ef41Sopenharmony_ci#endif  // defined(LEAK_SANITIZER)
351cb0ef41Sopenharmony_ci  return result;
361cb0ef41Sopenharmony_ci}
371cb0ef41Sopenharmony_ci
381cb0ef41Sopenharmony_civoid LsanVirtualAddressSpace::FreePages(Address address, size_t size) {
391cb0ef41Sopenharmony_ci  vas_->FreePages(address, size);
401cb0ef41Sopenharmony_ci#if defined(LEAK_SANITIZER)
411cb0ef41Sopenharmony_ci  __lsan_unregister_root_region(reinterpret_cast<void*>(address), size);
421cb0ef41Sopenharmony_ci#endif  // defined(LEAK_SANITIZER)
431cb0ef41Sopenharmony_ci}
441cb0ef41Sopenharmony_ci
451cb0ef41Sopenharmony_ciAddress LsanVirtualAddressSpace::AllocateSharedPages(
461cb0ef41Sopenharmony_ci    Address hint, size_t size, PagePermissions permissions,
471cb0ef41Sopenharmony_ci    PlatformSharedMemoryHandle handle, uint64_t offset) {
481cb0ef41Sopenharmony_ci  Address result =
491cb0ef41Sopenharmony_ci      vas_->AllocateSharedPages(hint, size, permissions, handle, offset);
501cb0ef41Sopenharmony_ci#if defined(LEAK_SANITIZER)
511cb0ef41Sopenharmony_ci  if (result) {
521cb0ef41Sopenharmony_ci    __lsan_register_root_region(reinterpret_cast<void*>(result), size);
531cb0ef41Sopenharmony_ci  }
541cb0ef41Sopenharmony_ci#endif  // defined(LEAK_SANITIZER)
551cb0ef41Sopenharmony_ci  return result;
561cb0ef41Sopenharmony_ci}
571cb0ef41Sopenharmony_ci
581cb0ef41Sopenharmony_civoid LsanVirtualAddressSpace::FreeSharedPages(Address address, size_t size) {
591cb0ef41Sopenharmony_ci  vas_->FreeSharedPages(address, size);
601cb0ef41Sopenharmony_ci#if defined(LEAK_SANITIZER)
611cb0ef41Sopenharmony_ci  __lsan_unregister_root_region(reinterpret_cast<void*>(address), size);
621cb0ef41Sopenharmony_ci#endif  // defined(LEAK_SANITIZER)
631cb0ef41Sopenharmony_ci}
641cb0ef41Sopenharmony_ci
651cb0ef41Sopenharmony_cistd::unique_ptr<VirtualAddressSpace> LsanVirtualAddressSpace::AllocateSubspace(
661cb0ef41Sopenharmony_ci    Address hint, size_t size, size_t alignment,
671cb0ef41Sopenharmony_ci    PagePermissions max_page_permissions) {
681cb0ef41Sopenharmony_ci  auto subspace =
691cb0ef41Sopenharmony_ci      vas_->AllocateSubspace(hint, size, alignment, max_page_permissions);
701cb0ef41Sopenharmony_ci#if defined(LEAK_SANITIZER)
711cb0ef41Sopenharmony_ci  if (subspace) {
721cb0ef41Sopenharmony_ci    subspace = std::make_unique<LsanVirtualAddressSpace>(std::move(subspace));
731cb0ef41Sopenharmony_ci  }
741cb0ef41Sopenharmony_ci#endif  // defined(LEAK_SANITIZER)
751cb0ef41Sopenharmony_ci  return subspace;
761cb0ef41Sopenharmony_ci}
771cb0ef41Sopenharmony_ci
781cb0ef41Sopenharmony_ci}  // namespace base
791cb0ef41Sopenharmony_ci}  // namespace v8
80