summaryrefslogtreecommitdiff
path: root/tests/malloc_test.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'tests/malloc_test.cpp')
-rw-r--r--tests/malloc_test.cpp107
1 files changed, 40 insertions, 67 deletions
diff --git a/tests/malloc_test.cpp b/tests/malloc_test.cpp
index 594441487..55bd1499f 100644
--- a/tests/malloc_test.cpp
+++ b/tests/malloc_test.cpp
@@ -662,6 +662,46 @@ TEST(malloc, mallopt_purge) {
#endif
}
+#if defined(__BIONIC__)
+static void GetAllocatorVersion(bool* allocator_scudo) {
+ TemporaryFile tf;
+ ASSERT_TRUE(tf.fd != -1);
+ FILE* fp = fdopen(tf.fd, "w+");
+ tf.release();
+ ASSERT_TRUE(fp != nullptr);
+ ASSERT_EQ(0, malloc_info(0, fp));
+ ASSERT_EQ(0, fclose(fp));
+
+ std::string contents;
+ ASSERT_TRUE(android::base::ReadFileToString(tf.path, &contents));
+
+ tinyxml2::XMLDocument doc;
+ ASSERT_EQ(tinyxml2::XML_SUCCESS, doc.Parse(contents.c_str()));
+
+ auto root = doc.FirstChildElement();
+ ASSERT_NE(nullptr, root);
+ ASSERT_STREQ("malloc", root->Name());
+ std::string version(root->Attribute("version"));
+ *allocator_scudo = (version == "scudo-1");
+}
+#endif
+
+TEST(malloc, mallopt_scudo_only_options) {
+#if defined(__BIONIC__)
+ SKIP_WITH_HWASAN << "hwasan does not implement mallopt";
+ bool allocator_scudo;
+ GetAllocatorVersion(&allocator_scudo);
+ if (!allocator_scudo) {
+ GTEST_SKIP() << "scudo allocator only test";
+ }
+ ASSERT_EQ(1, mallopt(M_CACHE_COUNT_MAX, 100));
+ ASSERT_EQ(1, mallopt(M_CACHE_SIZE_MAX, 1024 * 1024 * 2));
+ ASSERT_EQ(1, mallopt(M_TSDS_COUNT_MAX, 8));
+#else
+ GTEST_SKIP() << "bionic-only test";
+#endif
+}
+
TEST(malloc, reallocarray_overflow) {
#if HAVE_REALLOCARRAY
// Values that cause overflow to a result small enough (8 on LP64) that malloc would "succeed".
@@ -1201,70 +1241,3 @@ TEST(android_mallopt, set_allocation_limit_multiple_threads) {
GTEST_SKIP() << "bionic extension";
#endif
}
-
-#if defined(__BIONIC__) && defined(__aarch64__) && defined(ANDROID_EXPERIMENTAL_MTE)
-template <int SiCode> void CheckSiCode(int, siginfo_t* info, void*) {
- if (info->si_code != SiCode) {
- _exit(2);
- }
- _exit(1);
-}
-
-static bool SetTagCheckingLevel(int level) {
- int tagged_addr_ctrl = prctl(PR_GET_TAGGED_ADDR_CTRL, 0, 0, 0, 0);
- if (tagged_addr_ctrl < 0) {
- return false;
- }
-
- tagged_addr_ctrl = (tagged_addr_ctrl & ~PR_MTE_TCF_MASK) | level;
- return prctl(PR_SET_TAGGED_ADDR_CTRL, tagged_addr_ctrl, 0, 0, 0) == 0;
-}
-#endif
-
-TEST(android_mallopt, tag_level) {
-#if defined(__BIONIC__) && defined(__aarch64__) && defined(ANDROID_EXPERIMENTAL_MTE)
- if (!(getauxval(AT_HWCAP2) & HWCAP2_MTE)) {
- GTEST_SKIP() << "requires MTE support";
- return;
- }
-
- std::unique_ptr<int[]> p = std::make_unique<int[]>(4);
-
- // First, check that memory tagging is enabled and the default tag checking level is async.
- // We assume that scudo is used on all MTE enabled hardware; scudo inserts a header with a
- // mismatching tag before each allocation.
- EXPECT_EXIT(
- {
- ScopedSignalHandler ssh(SIGSEGV, CheckSiCode<SEGV_MTEAERR>, SA_SIGINFO);
- p[-1] = 42;
- },
- testing::ExitedWithCode(1), "");
-
- EXPECT_TRUE(SetTagCheckingLevel(PR_MTE_TCF_SYNC));
- EXPECT_EXIT(
- {
- ScopedSignalHandler ssh(SIGSEGV, CheckSiCode<SEGV_MTESERR>, SA_SIGINFO);
- p[-1] = 42;
- },
- testing::ExitedWithCode(1), "");
-
- EXPECT_TRUE(SetTagCheckingLevel(PR_MTE_TCF_NONE));
- volatile int oob ATTRIBUTE_UNUSED = p[-1];
-
- HeapTaggingLevel tag_level = M_HEAP_TAGGING_LEVEL_TBI;
- EXPECT_FALSE(android_mallopt(M_SET_HEAP_TAGGING_LEVEL, &tag_level, sizeof(tag_level)));
-
- tag_level = M_HEAP_TAGGING_LEVEL_NONE;
- EXPECT_TRUE(android_mallopt(M_SET_HEAP_TAGGING_LEVEL, &tag_level, sizeof(tag_level)));
- std::unique_ptr<int[]> p2 = std::make_unique<int[]>(4);
- EXPECT_EQ(0u, reinterpret_cast<uintptr_t>(p2.get()) >> 56);
-
- tag_level = M_HEAP_TAGGING_LEVEL_ASYNC;
- EXPECT_FALSE(android_mallopt(M_SET_HEAP_TAGGING_LEVEL, &tag_level, sizeof(tag_level)));
-
- tag_level = M_HEAP_TAGGING_LEVEL_NONE;
- EXPECT_TRUE(android_mallopt(M_SET_HEAP_TAGGING_LEVEL, &tag_level, sizeof(tag_level)));
-#else
- GTEST_SKIP() << "arm64 only";
-#endif
-}