Add method to use allocator app defaults.
Currently, apps turn off zeroing of memory and enable the decay
timer. For shell commands, set these values when the environment
variable MALLOC_USE_APP_DEFAULTS is set. This fixes performance
differences found between shell spawned command-line tools and
native code running in an app.
Add a unit test to verify setting after exec.
Bug: 302212507
Test: Set the variable to 1 and verified the decay time is enabled.
Test: Set the variable to "" and also unset it and verified the decay
Test: time is not enabled.
Test: Verified that with the variable set, the memory is not zero'd
Test: by default.
Test: Unit tests pass.
Change-Id: I22a47f70f1ce1205c16195532c54b2bf9403e9cd
diff --git a/tests/malloc_test.cpp b/tests/malloc_test.cpp
index 8bd8bc6..37c1ef0 100644
--- a/tests/malloc_test.cpp
+++ b/tests/malloc_test.cpp
@@ -1779,3 +1779,32 @@
GTEST_SKIP() << "bionic-only test";
#endif
}
+
+TEST(android_mallopt, DISABLED_verify_decay_time_on) {
+#if defined(__BIONIC__)
+ bool value;
+ EXPECT_TRUE(android_mallopt(M_GET_DECAY_TIME_ENABLED, &value, sizeof(value)));
+ EXPECT_TRUE(value) << "decay time did not get enabled properly.";
+#endif
+}
+
+TEST(android_mallopt, decay_time_set_using_env_variable) {
+#if defined(__BIONIC__)
+ SKIP_WITH_HWASAN << "hwasan does not implement mallopt";
+
+ bool value;
+ ASSERT_TRUE(android_mallopt(M_GET_DECAY_TIME_ENABLED, &value, sizeof(value)));
+ ASSERT_FALSE(value) << "decay time did not get disabled properly.";
+
+ // Verify that setting the environment variable here will be carried into
+ // fork'd and exec'd processes.
+ ASSERT_EQ(0, setenv("MALLOC_USE_APP_DEFAULTS", "1", 1));
+ ExecTestHelper eth;
+ eth.SetArgs({testing::internal::GetArgvs()[0].c_str(), "--gtest_also_run_disabled_tests",
+ "--gtest_filter=android_mallopt.DISABLED_verify_decay_time_on", nullptr});
+ eth.Run([&]() { execv(testing::internal::GetArgvs()[0].c_str(), eth.GetArgs()); }, 0,
+ R"(\[ PASSED \] 1 test)");
+#else
+ GTEST_SKIP() << "bionic-only test";
+#endif
+}