summaryrefslogtreecommitdiff
path: root/tests/malloc_test.cpp
diff options
context:
space:
mode:
authorChristopher Ferris <cferris@google.com>2019-05-02 18:33:11 -0700
committerChristopher Ferris <cferris@google.com>2019-05-03 07:21:45 -0700
commitdb9706afc376f74938e4060fa030913e87bb2be1 (patch)
tree22e9605dd1edde31191b9ed8e2a39907deca56f0 /tests/malloc_test.cpp
parent1eb6d36ba8666dace9a6f0c1db354750b331bf24 (diff)
Fix malloc_info missing large allocs.
Also change the names of some of the functions to make it very obvious that the functions being called are in je code. Write new test to make sure mallinfo and malloc_info match. Bug: 131864803 Test: New unit tests pass (along with all other bionic unit tests). Change-Id: I26eda7e64f57a8c56cc8d70e3ed6a29dcb87f630 Merged-In: I26eda7e64f57a8c56cc8d70e3ed6a29dcb87f630 (cherry picked from commit 125d32cb469d8e31f656543c3ad7b82154bc1689)
Diffstat (limited to 'tests/malloc_test.cpp')
-rw-r--r--tests/malloc_test.cpp54
1 files changed, 54 insertions, 0 deletions
diff --git a/tests/malloc_test.cpp b/tests/malloc_test.cpp
index 706de15db..983592f0a 100644
--- a/tests/malloc_test.cpp
+++ b/tests/malloc_test.cpp
@@ -395,6 +395,60 @@ TEST(malloc, malloc_info) {
#endif
}
+TEST(malloc, malloc_info_matches_mallinfo) {
+#ifdef __BIONIC__
+ SKIP_WITH_HWASAN; // hwasan does not implement malloc_info
+
+ char* buf;
+ size_t bufsize;
+ FILE* memstream = open_memstream(&buf, &bufsize);
+ ASSERT_NE(nullptr, memstream);
+ size_t mallinfo_before_allocated_bytes = mallinfo().uordblks;
+ ASSERT_EQ(0, malloc_info(0, memstream));
+ size_t mallinfo_after_allocated_bytes = mallinfo().uordblks;
+ ASSERT_EQ(0, fclose(memstream));
+
+ tinyxml2::XMLDocument doc;
+ ASSERT_EQ(tinyxml2::XML_SUCCESS, doc.Parse(buf));
+
+ size_t total_allocated_bytes = 0;
+ auto root = doc.FirstChildElement();
+ ASSERT_NE(nullptr, root);
+ ASSERT_STREQ("malloc", root->Name());
+ if (std::string(root->Attribute("version")) == "jemalloc-1") {
+ // Verify jemalloc version of this data.
+ ASSERT_STREQ("jemalloc-1", root->Attribute("version"));
+
+ auto arena = root->FirstChildElement();
+ for (; arena != nullptr; arena = arena->NextSiblingElement()) {
+ int val;
+
+ ASSERT_STREQ("heap", arena->Name());
+ ASSERT_EQ(tinyxml2::XML_SUCCESS, arena->QueryIntAttribute("nr", &val));
+ ASSERT_EQ(tinyxml2::XML_SUCCESS,
+ arena->FirstChildElement("allocated-large")->QueryIntText(&val));
+ total_allocated_bytes += val;
+ ASSERT_EQ(tinyxml2::XML_SUCCESS,
+ arena->FirstChildElement("allocated-huge")->QueryIntText(&val));
+ total_allocated_bytes += val;
+ ASSERT_EQ(tinyxml2::XML_SUCCESS,
+ arena->FirstChildElement("allocated-bins")->QueryIntText(&val));
+ total_allocated_bytes += val;
+ ASSERT_EQ(tinyxml2::XML_SUCCESS,
+ arena->FirstChildElement("bins-total")->QueryIntText(&val));
+ }
+ // The total needs to be between the mallinfo call before and after
+ // since malloc_info allocates some memory.
+ EXPECT_LE(mallinfo_before_allocated_bytes, total_allocated_bytes);
+ EXPECT_GE(mallinfo_after_allocated_bytes, total_allocated_bytes);
+ } else {
+ // Only verify that this is debug-malloc-1, the malloc debug unit tests
+ // verify the output.
+ ASSERT_STREQ("debug-malloc-1", root->Attribute("version"));
+ }
+#endif
+}
+
TEST(malloc, calloc_usable_size) {
for (size_t size = 1; size <= 2048; size++) {
void* pointer = malloc(size);