diff options
Diffstat (limited to 'libc/malloc_debug/tests/malloc_debug_unit_tests.cpp')
-rw-r--r-- | libc/malloc_debug/tests/malloc_debug_unit_tests.cpp | 85 |
1 files changed, 85 insertions, 0 deletions
diff --git a/libc/malloc_debug/tests/malloc_debug_unit_tests.cpp b/libc/malloc_debug/tests/malloc_debug_unit_tests.cpp index a72db3b91..66955db59 100644 --- a/libc/malloc_debug/tests/malloc_debug_unit_tests.cpp +++ b/libc/malloc_debug/tests/malloc_debug_unit_tests.cpp @@ -16,6 +16,7 @@ #include <malloc.h> #include <signal.h> +#include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/cdefs.h> @@ -25,10 +26,13 @@ #include <unistd.h> #include <algorithm> +#include <memory> #include <thread> #include <vector> #include <utility> +#include <tinyxml2.h> + #include <gtest/gtest.h> #include <android-base/file.h> @@ -62,6 +66,7 @@ void debug_free_malloc_leak_info(uint8_t*); struct mallinfo debug_mallinfo(); int debug_mallopt(int, int); +int debug_malloc_info(int, FILE*); #if defined(HAVE_DEPRECATED_MALLOC_FUNCS) void* debug_pvalloc(size_t); @@ -136,6 +141,7 @@ MallocDispatch MallocDebugTest::dispatch = { nullptr, mallopt, aligned_alloc, + malloc_info, }; std::string ShowDiffs(uint8_t* a, uint8_t* b, size_t size) { @@ -2465,3 +2471,82 @@ TEST_F(MallocDebugTest, abort_on_error_header_tag_corrupted) { pointer[-get_tag_offset()] = tag_value; } +TEST_F(MallocDebugTest, malloc_info_no_pointer_tracking) { + Init("fill"); + + char* buffer; + size_t size; + FILE* memstream = open_memstream(&buffer, &size); + ASSERT_TRUE(memstream != nullptr); + ASSERT_EQ(0, debug_malloc_info(0, memstream)); + ASSERT_EQ(0, fclose(memstream)); + + tinyxml2::XMLDocument doc; + ASSERT_EQ(tinyxml2::XML_SUCCESS, doc.Parse(buffer)); + auto root = doc.FirstChildElement(); + ASSERT_TRUE(root != nullptr); + ASSERT_STREQ("malloc", root->Name()); + // Don't care what the underyling implementation says, just that it's + // not generated by debug malloc. + ASSERT_STRNE("debug-malloc-1", root->Attribute("version")); +} + +TEST_F(MallocDebugTest, malloc_info_with_pointer_tracking) { + Init("verify_pointers"); + + std::unique_ptr<void, decltype(debug_free)*> ptr1(debug_malloc(1000), debug_free); + ASSERT_TRUE(ptr1.get() != nullptr); + std::unique_ptr<void, decltype(debug_free)*> ptr2(debug_malloc(1000), debug_free); + ASSERT_TRUE(ptr2.get() != nullptr); + std::unique_ptr<void, decltype(debug_free)*> ptr3(debug_malloc(500), debug_free); + ASSERT_TRUE(ptr3.get() != nullptr); + std::unique_ptr<void, decltype(debug_free)*> ptr4(debug_malloc(1200), debug_free); + ASSERT_TRUE(ptr4.get() != nullptr); + + char* buffer; + size_t size; + FILE* memstream = open_memstream(&buffer, &size); + ASSERT_TRUE(memstream != nullptr); + ASSERT_EQ(0, debug_malloc_info(0, memstream)); + ASSERT_EQ(0, fclose(memstream)); + + SCOPED_TRACE(testing::Message() << "Output:\n" << buffer); + + tinyxml2::XMLDocument doc; + ASSERT_EQ(tinyxml2::XML_SUCCESS, doc.Parse(buffer)); + auto root = doc.FirstChildElement(); + ASSERT_TRUE(root != nullptr); + ASSERT_STREQ("malloc", root->Name()); + ASSERT_STREQ("debug-malloc-1", root->Attribute("version")); + + auto alloc = root->FirstChildElement(); + ASSERT_TRUE(alloc != nullptr); + ASSERT_STREQ("allocation", alloc->Name()); + int val; + ASSERT_EQ(tinyxml2::XML_SUCCESS, alloc->QueryIntAttribute("nr", &val)); + ASSERT_EQ(0, val); + ASSERT_EQ(tinyxml2::XML_SUCCESS, alloc->FirstChildElement("size")->QueryIntText(&val)); + ASSERT_EQ(1200, val); + ASSERT_EQ(tinyxml2::XML_SUCCESS, alloc->FirstChildElement("total")->QueryIntText(&val)); + ASSERT_EQ(1, val); + + alloc = alloc->NextSiblingElement(); + ASSERT_TRUE(alloc != nullptr); + ASSERT_STREQ("allocation", alloc->Name()); + ASSERT_EQ(tinyxml2::XML_SUCCESS, alloc->QueryIntAttribute("nr", &val)); + ASSERT_EQ(1, val); + ASSERT_EQ(tinyxml2::XML_SUCCESS, alloc->FirstChildElement("size")->QueryIntText(&val)); + ASSERT_EQ(1000, val); + ASSERT_EQ(tinyxml2::XML_SUCCESS, alloc->FirstChildElement("total")->QueryIntText(&val)); + ASSERT_EQ(2, val); + + alloc = alloc->NextSiblingElement(); + ASSERT_TRUE(alloc != nullptr); + ASSERT_STREQ("allocation", alloc->Name()); + ASSERT_EQ(tinyxml2::XML_SUCCESS, alloc->QueryIntAttribute("nr", &val)); + ASSERT_EQ(2, val); + ASSERT_EQ(tinyxml2::XML_SUCCESS, alloc->FirstChildElement("size")->QueryIntText(&val)); + ASSERT_EQ(500, val); + ASSERT_EQ(tinyxml2::XML_SUCCESS, alloc->FirstChildElement("total")->QueryIntText(&val)); + ASSERT_EQ(1, val); +} |