diff options
author | Christopher Ferris <cferris@google.com> | 2020-01-29 13:09:31 -0800 |
---|---|---|
committer | Christopher Ferris <cferris@google.com> | 2020-01-29 14:40:37 -0800 |
commit | 201dcf491bf74fe589e93584f97f3908c353af4b (patch) | |
tree | 357a5bbd07cf84d8fc9c4e161c1ae6c29bc33ad8 /tests/malloc_test.cpp | |
parent | 0e0b473a4c01ec6b739ac7aa9eccb63005fa3da2 (diff) |
Add a disabled test to verify alloc after fork.
The alloc after fork is a test that should pass, but jemalloc
doesn't right now. Leave the test disabled until the native
allocator can pass this.
Test: Ran the test 1000 times on glibc to verify it passes.
Test: On device, verified it does not run.
Change-Id: I482af4db2fee81c947ac081c7a6f25a2aff80350
Diffstat (limited to 'tests/malloc_test.cpp')
-rw-r--r-- | tests/malloc_test.cpp | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/tests/malloc_test.cpp b/tests/malloc_test.cpp index ef2f895d7..6620cdbfd 100644 --- a/tests/malloc_test.cpp +++ b/tests/malloc_test.cpp @@ -829,6 +829,63 @@ TEST(malloc, align_check) { #endif } +// Jemalloc doesn't pass this test right now, so leave it as disabled. +TEST(malloc, DISABLED_alloc_after_fork) { + // Both of these need to be a power of 2. + static constexpr size_t kMinAllocationSize = 8; + static constexpr size_t kMaxAllocationSize = 2097152; + + static constexpr size_t kNumAllocatingThreads = 5; + static constexpr size_t kNumForkLoops = 100; + + std::atomic_bool stop; + + // Create threads that simply allocate and free different sizes. + std::vector<std::thread*> threads; + for (size_t i = 0; i < kNumAllocatingThreads; i++) { + std::thread* t = new std::thread([&stop] { + while (!stop) { + for (size_t size = kMinAllocationSize; size <= kMaxAllocationSize; size <<= 1) { + void* ptr = malloc(size); + if (ptr == nullptr) { + return; + } + // Make sure this value is not optimized away. + asm volatile("" : : "r,m"(ptr) : "memory"); + free(ptr); + } + } + }); + threads.push_back(t); + } + + // Create a thread to fork and allocate. + for (size_t i = 0; i < kNumForkLoops; i++) { + pid_t pid; + if ((pid = fork()) == 0) { + for (size_t size = kMinAllocationSize; size <= kMaxAllocationSize; size <<= 1) { + void* ptr = malloc(size); + ASSERT_TRUE(ptr != nullptr); + // Make sure this value is not optimized away. + asm volatile("" : : "r,m"(ptr) : "memory"); + // Make sure we can touch all of the allocation. + memset(ptr, 0x1, size); + ASSERT_LE(size, malloc_usable_size(ptr)); + free(ptr); + } + _exit(10); + } + ASSERT_NE(-1, pid); + AssertChildExited(pid, 10); + } + + stop = true; + for (auto thread : threads) { + thread->join(); + delete thread; + } +} + TEST(android_mallopt, error_on_unexpected_option) { #if defined(__BIONIC__) const int unrecognized_option = -1; |