1 // Copyright 2021 The Chromium Authors 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 "partition_alloc/allocation_guard.h" 6 #include "partition_alloc/partition_alloc_base/immediate_crash.h" 7 #include "partition_alloc/partition_alloc_config.h" 8 9 #if PA_CONFIG(HAS_ALLOCATION_GUARD) 10 11 namespace partition_alloc { 12 13 namespace { 14 thread_local bool g_disallow_allocations; 15 } // namespace 16 ScopedDisallowAllocations()17ScopedDisallowAllocations::ScopedDisallowAllocations() { 18 if (g_disallow_allocations) { 19 PA_IMMEDIATE_CRASH(); 20 } 21 22 g_disallow_allocations = true; 23 } 24 ~ScopedDisallowAllocations()25ScopedDisallowAllocations::~ScopedDisallowAllocations() { 26 g_disallow_allocations = false; 27 } 28 ScopedAllowAllocations()29ScopedAllowAllocations::ScopedAllowAllocations() { 30 // Save the previous value, as ScopedAllowAllocations is used in all 31 // partitions, not just the malloc() ones(s). 32 saved_value_ = g_disallow_allocations; 33 g_disallow_allocations = false; 34 } 35 ~ScopedAllowAllocations()36ScopedAllowAllocations::~ScopedAllowAllocations() { 37 g_disallow_allocations = saved_value_; 38 } 39 40 } // namespace partition_alloc 41 42 #endif // PA_CONFIG(HAS_ALLOCATION_GUARD) 43