Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions tcmalloc/internal/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ cc_test(
copts = TCMALLOC_DEFAULT_COPTS,
deps = [
":allocation_guard",
":scoped_allow_allocation",
"@com_google_absl//absl/base",
"@com_google_absl//absl/base:core_headers",
"@com_google_googletest//:gtest_main",
Expand Down
1 change: 1 addition & 0 deletions tcmalloc/internal/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ tcmalloc_cc_test(
"absl::base"
"absl::core_headers"
"tcmalloc::internal_allocation_guard"
"tcmalloc::internal_scoped_allow_allocation"
"tcmalloc::tcmalloc"
)

Expand Down
1 change: 1 addition & 0 deletions tcmalloc/internal/allocation_guard.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ namespace tcmalloc::tcmalloc_internal {

#ifndef NDEBUG
ABSL_CONST_INIT thread_local int AllocationGuard::disallowed_ = 0;
ABSL_CONST_INIT thread_local int tcmalloc_reentrancy_count = 0;
#endif

} // namespace tcmalloc::tcmalloc_internal
Expand Down
15 changes: 15 additions & 0 deletions tcmalloc/internal/allocation_guard.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@
GOOGLE_MALLOC_SECTION_BEGIN
namespace tcmalloc::tcmalloc_internal {

#if !defined(NDEBUG)
ABSL_CONST_INIT extern thread_local int tcmalloc_reentrancy_count;
#endif

// In debug mode, ensures that no allocations will occur on the current thread
// while this class is in scope.
class AllocationGuard {
Expand All @@ -34,18 +38,29 @@ class AllocationGuard {
(void)MallocHook::AddNewHook(Hook);
}
++disallowed_;
++tcmalloc_reentrancy_count;
#endif
}

~AllocationGuard() {
#if !defined(NDEBUG) && !defined(FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION)
--tcmalloc_reentrancy_count;
--disallowed_;
if (disallowed_ == 0) {
(void)MallocHook::RemoveNewHook(Hook);
}
#endif
}

static void Disarm() {
#if !defined(NDEBUG) && !defined(FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION)
if (disallowed_ > 0) {
disallowed_ = 0;
(void)MallocHook::RemoveNewHook(Hook);
}
#endif
}

private:
static void Hook(const MallocHook::NewInfo& info) {
#ifndef NDEBUG
Expand Down
35 changes: 35 additions & 0 deletions tcmalloc/internal/allocation_guard_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "gtest/gtest.h"
#include "absl/base/const_init.h"
#include "absl/base/internal/spinlock.h"
#include "tcmalloc/internal/scoped_allow_allocation.h"

namespace tcmalloc::tcmalloc_internal {
namespace {
Expand All @@ -28,6 +29,40 @@ TEST(AllocationGuard, Noncooperative) {
AllocationGuardSpinLockHolder h(lock);
}

#if !defined(NDEBUG) && !defined(FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION)
TEST(AllocationGuard, ReentrancyCount) {
EXPECT_EQ(tcmalloc_reentrancy_count, 0);
int under_lock = -1;
int under_nested = -1;
int after_nested = -1;
int under_allow = -1;
int after_allow = -1;
{
absl::base_internal::SpinLock lock(
absl::base_internal::SCHEDULE_KERNEL_ONLY);
AllocationGuardSpinLockHolder h(lock);
under_lock = tcmalloc_reentrancy_count;
{
AllocationGuard nested;
under_nested = tcmalloc_reentrancy_count;
}
after_nested = tcmalloc_reentrancy_count;
{
ScopedAllocationAllow allow;
under_allow = tcmalloc_reentrancy_count;
::operator delete(::operator new(1));
}
after_allow = tcmalloc_reentrancy_count;
}
EXPECT_EQ(under_lock, 1);
EXPECT_EQ(under_nested, 2);
EXPECT_EQ(after_nested, 1);
EXPECT_EQ(under_allow, 0);
EXPECT_EQ(after_allow, 1);
EXPECT_EQ(tcmalloc_reentrancy_count, 0);
}
#endif

TEST(AllocationGuard, CooperativeDeathTest) {
absl::base_internal::SpinLock lock;

Expand Down
23 changes: 11 additions & 12 deletions tcmalloc/internal/logging.cc
Original file line number Diff line number Diff line change
Expand Up @@ -173,16 +173,19 @@ void RecordCrash(absl::string_view detector, absl::string_view error,
if (!crashed) {
crashed = true;
first_crash = true;
#if !defined(NDEBUG)
// Reset reentrancy count to allow calls to malloc/free under any signal
// handlers that we may trigger from abort().
//
// We only reset this, rather than permit an unlimited amount of
// reentrancy while crashing, in order to maintain modest stack depths.
tcmalloc_reentrancy_count = 0;
#endif
}
}
#if !defined(NDEBUG)
if (first_crash) {
// Reset reentrancy count to allow calls to malloc/free under any signal
// handlers that we may trigger from abort().
//
// We only reset this, rather than permit an unlimited amount of
// reentrancy while crashing, in order to maintain modest stack depths.
tcmalloc_reentrancy_count = 0;
AllocationGuard::Disarm();
}
#endif

(*log_message_writer)(msg, msglen);
if (first_crash && oom) {
Expand Down Expand Up @@ -301,10 +304,6 @@ PbtxtRegion PbtxtRegion::CreateSubRegion(absl::string_view key) {
return sub;
}

#if !defined(NDEBUG)
ABSL_CONST_INIT thread_local int tcmalloc_reentrancy_count = 0;
#endif

} // namespace tcmalloc_internal
} // namespace tcmalloc
GOOGLE_MALLOC_SECTION_END
6 changes: 4 additions & 2 deletions tcmalloc/internal/scoped_allow_allocation.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,14 @@ namespace tcmalloc::tcmalloc_internal {
class ScopedAllocationAllow {
public:
ScopedAllocationAllow() {
#ifndef NDEBUG
#if !defined(NDEBUG) && !defined(FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION)
--AllocationGuard::disallowed_;
--tcmalloc_reentrancy_count;
#endif
}
~ScopedAllocationAllow() {
#ifndef NDEBUG
#if !defined(NDEBUG) && !defined(FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION)
++tcmalloc_reentrancy_count;
++AllocationGuard::disallowed_;
#endif
}
Expand Down
Loading