expand emergency malloc test coverage
We add coverage of calloc and we also cover no-hooks case.
diff --git a/src/tests/tcmalloc_unittest.cc b/src/tests/tcmalloc_unittest.cc
index 58bfbbd..199a1c0 100644
--- a/src/tests/tcmalloc_unittest.cc
+++ b/src/tests/tcmalloc_unittest.cc
@@ -1556,6 +1556,7 @@
TEST(TCMallocTest, EmergencyMalloc) {
auto portal = TestingPortal::Get();
if (!portal->HasEmergencyMalloc()) {
+ printf("EmergencyMalloc test skipped\n");
return;
}
@@ -1582,7 +1583,7 @@
// Emergency malloc doesn't return pointers recognized by MallocExtension
ASSERT_EQ(MallocExtension::instance()->GetOwnership(p1), MallocExtension::kOwned);
- ASSERT_NE(MallocExtension::instance()->GetOwnership(p2), MallocExtension::kOwned);
+ ASSERT_EQ(MallocExtension::instance()->GetOwnership(p2), MallocExtension::kNotOwned);
// Emergency malloc automagically does the right thing for free()
// calls and doesn't invoke hooks.
@@ -1593,6 +1594,46 @@
VerifyDeleteHookWasCalled();
}
+TEST(TCMallocTest, EmergencyMallocNoHook) {
+ auto portal = TestingPortal::Get();
+ if (!portal->HasEmergencyMalloc()) {
+ printf("EmergencyMallocNoHook test skipped\n");
+ return;
+ }
+
+ void* p1 = noopt(tc_malloc)(32);
+ void* p2 = nullptr;
+ void* p3 = nullptr;
+
+ portal->WithEmergencyMallocEnabled([&] () {
+ p2 = noopt(malloc)(32);
+ p3 = tc_calloc(4096, 1024);
+ });
+
+ ASSERT_NE(p2, nullptr);
+ ASSERT_NE(p3, nullptr);
+
+ // Emergency malloc doesn't return pointers recognized by MallocExtension
+ ASSERT_EQ(MallocExtension::instance()->GetOwnership(p1), MallocExtension::kOwned);
+ ASSERT_EQ(MallocExtension::instance()->GetOwnership(p2), MallocExtension::kNotOwned);
+ ASSERT_EQ(MallocExtension::instance()->GetOwnership(p3), MallocExtension::kNotOwned);
+
+ SetNewHook();
+ SetDeleteHook();
+ tcmalloc::Cleanup unhook([] () {
+ ResetNewHook();
+ ResetDeleteHook();
+ });
+
+ // Emergency malloc automagically does the right thing for free()
+ // calls and doesn't invoke hooks.
+ tc_free(p2);
+ ASSERT_EQ(g_DeleteHook_calls, 0);
+
+ tc_free(p1);
+ VerifyDeleteHookWasCalled();
+}
+
TEST(TCMallocTest, Version) {
// Test tc_version()
int major;