Skip to content

Commit ba72dfc

Browse files
ckennellycopybara-github
authored andcommitted
Ensure internal_methods_only TCMalloc variant works under sanitizers.
Sanitizers replace malloc/free, so we need `MallocExtension::GetAllocatedSize` & friends to get a matching implementation. If we keep `MallocExtension_Internal_GetAllocatedSize` defined in tcmalloc.cc, we get TCMalloc's version and do not recognize these pointers since they came from the sanitizer. PiperOrigin-RevId: 899185297 Change-Id: Ib830c76f5334c398bd1a0558f4325720b0c0f40b
1 parent 420560a commit ba72dfc

6 files changed

Lines changed: 70 additions & 5 deletions

File tree

tcmalloc/tcmalloc.cc

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1312,14 +1312,13 @@ extern "C" void MallocHook_HooksChanged() {
13121312
using tcmalloc::tcmalloc_internal::GetOwnership;
13131313
using tcmalloc::tcmalloc_internal::GetSize;
13141314

1315-
extern "C" size_t MallocExtension_Internal_GetAllocatedSize(const void* ptr) {
1315+
extern "C" size_t TCMalloc_Internal_GetAllocatedSize(const void* ptr) {
13161316
TC_ASSERT(!ptr || GetOwnership(ptr) !=
13171317
tcmalloc::MallocExtension::Ownership::kNotOwned);
13181318
return GetSize(ptr);
13191319
}
13201320

1321-
extern "C" size_t MallocExtension_Internal_GetEstimatedAllocatedSize(
1322-
size_t size) {
1321+
extern "C" size_t TCMalloc_Internal_GetEstimatedAllocatedSize(size_t size) {
13231322
if (ABSL_PREDICT_FALSE(!tc_globals.IsInited())) {
13241323
return tcmalloc::tcmalloc_internal::nallocx_slow(size, 0);
13251324
}
@@ -1333,7 +1332,7 @@ extern "C" size_t MallocExtension_Internal_GetEstimatedAllocatedSize(
13331332
}
13341333
}
13351334

1336-
extern "C" void MallocExtension_Internal_MarkThreadBusy() {
1335+
extern "C" void TCMalloc_Internal_MarkThreadBusy() {
13371336
tc_globals.InitIfNecessary();
13381337

13391338
if (UsePerCpuCache(tc_globals)) {
@@ -1344,6 +1343,21 @@ extern "C" void MallocExtension_Internal_MarkThreadBusy() {
13441343
tcmalloc::tcmalloc_internal::ThreadCache::GetCache();
13451344
}
13461345

1346+
#ifndef TCMALLOC_INTERNAL_METHODS_ONLY
1347+
extern "C" size_t MallocExtension_Internal_GetAllocatedSize(const void* ptr) {
1348+
return TCMalloc_Internal_GetAllocatedSize(ptr);
1349+
}
1350+
1351+
extern "C" size_t MallocExtension_Internal_GetEstimatedAllocatedSize(
1352+
size_t size) {
1353+
return TCMalloc_Internal_GetEstimatedAllocatedSize(size);
1354+
}
1355+
1356+
extern "C" void MallocExtension_Internal_MarkThreadBusy() {
1357+
TCMalloc_Internal_MarkThreadBusy();
1358+
}
1359+
#endif // !TCMALLOC_INTERNAL_METHODS_ONLY
1360+
13471361
absl::StatusOr<tcmalloc::malloc_tracing_extension::AllocatedAddressRanges>
13481362
MallocTracingExtension_Internal_GetAllocatedAddressRanges() {
13491363
tcmalloc::malloc_tracing_extension::AllocatedAddressRanges

tcmalloc/tcmalloc.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,10 @@ static_assert(ALLOC_TOKEN_FALLBACK >= ALLOC_TOKEN_MAX);
281281
DECLARE_ALLOC_TOKEN_VARIANTS(ALLOC_TOKEN_FALLBACK)
282282
#endif
283283

284+
size_t TCMalloc_Internal_GetAllocatedSize(const void* ptr);
285+
size_t TCMalloc_Internal_GetEstimatedAllocatedSize(size_t size);
286+
void TCMalloc_Internal_MarkThreadBusy();
287+
284288
} // extern "C"
285289

286290
#endif // TCMALLOC_TCMALLOC_H_

tcmalloc/testing/BUILD

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -889,6 +889,20 @@ create_tcmalloc_testsuite(
889889
],
890890
)
891891

892+
cc_test(
893+
name = "malloc_extension_internal_methods_test",
894+
srcs = ["malloc_extension_system_malloc_test.cc"],
895+
copts = TCMALLOC_DEFAULT_COPTS,
896+
malloc = "//tcmalloc:tcmalloc_internal_methods_only",
897+
tags = ["noubsan"],
898+
deps = [
899+
"//tcmalloc:malloc_extension",
900+
"//tcmalloc/internal:config",
901+
"@com_google_absl//absl/random",
902+
"@com_google_googletest//:gtest_main",
903+
],
904+
)
905+
892906
cc_test(
893907
name = "malloc_extension_system_malloc_test",
894908
srcs = ["malloc_extension_system_malloc_test.cc"],
@@ -897,6 +911,7 @@ cc_test(
897911
tags = ["noubsan"],
898912
deps = [
899913
"//tcmalloc:malloc_extension",
914+
"//tcmalloc/internal:config",
900915
"@com_google_absl//absl/random",
901916
"@com_google_googletest//:gtest_main",
902917
],

tcmalloc/testing/CMakeLists.txt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -803,6 +803,21 @@ tcmalloc_cc_test_variants(
803803
"tcmalloc::malloc_extension"
804804
)
805805

806+
tcmalloc_cc_test(
807+
NAME
808+
tcmalloc_testing_malloc_extension_internal_methods_test
809+
SRCS
810+
"malloc_extension_system_malloc_test.cc"
811+
DEPS
812+
"GTest::gtest_main"
813+
"GTest::gmock_main"
814+
"GTest::gmock"
815+
"absl::random_random"
816+
"tcmalloc::internal_config"
817+
"tcmalloc::malloc_extension"
818+
"tcmalloc::tcmalloc_internal_methods_only"
819+
)
820+
806821
tcmalloc_cc_test(
807822
NAME
808823
tcmalloc_testing_malloc_extension_system_malloc_test
@@ -813,6 +828,7 @@ tcmalloc_cc_test(
813828
"GTest::gmock_main"
814829
"GTest::gmock"
815830
"absl::random_random"
831+
"tcmalloc::internal_config"
816832
"tcmalloc::malloc_extension"
817833
)
818834

tcmalloc/testing/malloc_extension_system_malloc_test.cc

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
#include "gtest/gtest.h"
2222
#include "absl/random/random.h"
23+
#include "tcmalloc/internal/config.h"
2324
#include "tcmalloc/malloc_extension.h"
2425

2526
namespace tcmalloc {
@@ -86,5 +87,19 @@ TEST(MallocExtension, AllocationProfile) {
8687
}
8788
}
8889

90+
TEST(MallocExtension, GetAllocatedSizeSystemMalloc) {
91+
void* ptr = ::operator new(100);
92+
std::optional<size_t> size = MallocExtension::GetAllocatedSize(ptr);
93+
94+
if (tcmalloc_internal::kSanitizerPresent) {
95+
EXPECT_TRUE(size.has_value());
96+
EXPECT_GE(*size, 100);
97+
} else {
98+
EXPECT_FALSE(size.has_value());
99+
}
100+
101+
::operator delete(ptr);
102+
}
103+
89104
} // namespace
90105
} // namespace tcmalloc

tcmalloc/testing/parallel_test.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ extern "C" {
2929
void* TCMallocInternalNew(size_t);
3030
void TCMallocInternalDelete(void*);
3131
void TCMallocInternalDeleteSized(void*, size_t);
32+
size_t TCMalloc_Internal_GetAllocatedSize(const void*);
3233

3334
} // extern "C"
3435

@@ -53,7 +54,7 @@ struct Allocator {
5354
}
5455

5556
for (void* ptr : v) {
56-
ABSL_CHECK_GE(*MallocExtension::GetAllocatedSize(ptr), size);
57+
ABSL_CHECK_GE(TCMalloc_Internal_GetAllocatedSize(ptr), size);
5758
}
5859

5960
for (void* ptr : v) {

0 commit comments

Comments
 (0)