• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2018 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "src/base/sanitizer/lsan-page-allocator.h"
6 
7 #include "include/v8-platform.h"
8 #include "src/base/logging.h"
9 
10 #if defined(LEAK_SANITIZER)
11 #include <sanitizer/lsan_interface.h>
12 #endif
13 
14 namespace v8 {
15 namespace base {
16 
LsanPageAllocator(v8::PageAllocator * page_allocator)17 LsanPageAllocator::LsanPageAllocator(v8::PageAllocator* page_allocator)
18     : page_allocator_(page_allocator),
19       allocate_page_size_(page_allocator_->AllocatePageSize()),
20       commit_page_size_(page_allocator_->CommitPageSize()) {
21   DCHECK_NOT_NULL(page_allocator);
22 }
23 
AllocatePages(void * hint,size_t size,size_t alignment,PageAllocator::Permission access)24 void* LsanPageAllocator::AllocatePages(void* hint, size_t size,
25                                        size_t alignment,
26                                        PageAllocator::Permission access) {
27   void* result = page_allocator_->AllocatePages(hint, size, alignment, access);
28 #if defined(LEAK_SANITIZER)
29   if (result != nullptr) {
30     __lsan_register_root_region(result, size);
31   }
32 #endif
33   return result;
34 }
35 
36 std::unique_ptr<v8::PageAllocator::SharedMemory>
AllocateSharedPages(size_t size,const void * original_address)37 LsanPageAllocator::AllocateSharedPages(size_t size,
38                                        const void* original_address) {
39   auto result = page_allocator_->AllocateSharedPages(size, original_address);
40 #if defined(LEAK_SANITIZER)
41   if (result != nullptr) {
42     __lsan_register_root_region(result->GetMemory(), size);
43   }
44 #endif
45   return result;
46 }
47 
CanAllocateSharedPages()48 bool LsanPageAllocator::CanAllocateSharedPages() {
49   return page_allocator_->CanAllocateSharedPages();
50 }
51 
FreePages(void * address,size_t size)52 bool LsanPageAllocator::FreePages(void* address, size_t size) {
53   CHECK(page_allocator_->FreePages(address, size));
54 #if defined(LEAK_SANITIZER)
55   __lsan_unregister_root_region(address, size);
56 #endif
57   return true;
58 }
59 
ReleasePages(void * address,size_t size,size_t new_size)60 bool LsanPageAllocator::ReleasePages(void* address, size_t size,
61                                      size_t new_size) {
62   CHECK(page_allocator_->ReleasePages(address, size, new_size));
63 #if defined(LEAK_SANITIZER)
64   __lsan_unregister_root_region(address, size);
65   __lsan_register_root_region(address, new_size);
66 #endif
67   return true;
68 }
69 
70 }  // namespace base
71 }  // namespace v8
72