1 // Copyright 2020 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 "include/cppgc/platform.h"
6
7 #include "src/base/lazy-instance.h"
8 #include "src/base/logging.h"
9 #include "src/base/macros.h"
10 #include "src/base/platform/platform.h"
11 #include "src/base/sanitizer/asan.h"
12 #include "src/heap/cppgc/gc-info-table.h"
13 #include "src/heap/cppgc/globals.h"
14 #include "src/heap/cppgc/platform.h"
15
16 namespace cppgc {
17 namespace internal {
18
Fatal(const std::string& reason, const SourceLocation& loc)19 void Fatal(const std::string& reason, const SourceLocation& loc) {
20 #ifdef DEBUG
21 V8_Fatal(loc.FileName(), static_cast<int>(loc.Line()), "%s", reason.c_str());
22 #else // !DEBUG
23 V8_Fatal("%s", reason.c_str());
24 #endif // !DEBUG
25 }
26
operator ()(const std::string& reason, const SourceLocation& loc) const27 void FatalOutOfMemoryHandler::operator()(const std::string& reason,
28 const SourceLocation& loc) const {
29 if (custom_handler_) {
30 (*custom_handler_)(reason, loc, heap_);
31 FATAL("Custom out of memory handler should not have returned");
32 }
33 #ifdef DEBUG
34 V8_Fatal(loc.FileName(), static_cast<int>(loc.Line()),
35 "Oilpan: Out of memory (%s)", reason.c_str());
36 #else // !DEBUG
37 V8_Fatal("Oilpan: Out of memory");
38 #endif // !DEBUG
39 }
40
SetCustomHandler(Callback* callback)41 void FatalOutOfMemoryHandler::SetCustomHandler(Callback* callback) {
42 custom_handler_ = callback;
43 }
44
45 } // namespace internal
46
47 namespace {
48 PageAllocator* g_page_allocator = nullptr;
49 } // namespace
50
GetTracingController()51 TracingController* Platform::GetTracingController() {
52 static v8::base::LeakyObject<TracingController> tracing_controller;
53 return tracing_controller.get();
54 }
55
InitializeProcess(PageAllocator* page_allocator)56 void InitializeProcess(PageAllocator* page_allocator) {
57 #if defined(V8_USE_ADDRESS_SANITIZER) && defined(V8_TARGET_ARCH_64_BIT)
58 // Retrieve asan's internal shadow memory granularity and check that Oilpan's
59 // object alignment/sizes are multiple of this granularity. This is needed to
60 // perform poisoness checks.
61 size_t shadow_scale;
62 __asan_get_shadow_mapping(&shadow_scale, nullptr);
63 DCHECK(shadow_scale);
64 const size_t poisoning_granularity = 1 << shadow_scale;
65 CHECK_EQ(0u, internal::kAllocationGranularity % poisoning_granularity);
66 #endif
67
68 CHECK(!g_page_allocator);
69 internal::GlobalGCInfoTable::Initialize(page_allocator);
70 g_page_allocator = page_allocator;
71 }
72
ShutdownProcess()73 void ShutdownProcess() { g_page_allocator = nullptr; }
74
75 } // namespace cppgc
76