diff options
| author | Colin Cross <ccross@android.com> | 2017-06-22 10:50:05 -0700 |
|---|---|---|
| committer | Colin Cross <ccross@android.com> | 2017-06-22 10:58:05 -0700 |
| commit | a83881e33ce29ee236c924d669cb41a9d816962d (patch) | |
| tree | 10d1c371968a5a726641e1d2c0e92739008be678 /libmemunreachable/tests | |
| parent | 07a57f0f283938c5e558d79c62d4c2d4c24d3d03 (diff) | |
| download | system_core-a83881e33ce29ee236c924d669cb41a9d816962d.tar.gz system_core-a83881e33ce29ee236c924d669cb41a9d816962d.tar.bz2 system_core-a83881e33ce29ee236c924d669cb41a9d816962d.zip | |
libmemunreachable: clang-format everything
clang-format -i --sort-includes $(find . -name "*.cpp" -o -name "*.h")
Test: builds
Change-Id: Ia8e0677fe7f3f26dddba3a851cd2dfab9f14e421
Diffstat (limited to 'libmemunreachable/tests')
| -rw-r--r-- | libmemunreachable/tests/Allocator_test.cpp | 22 | ||||
| -rw-r--r-- | libmemunreachable/tests/DisableMalloc_test.cpp | 128 | ||||
| -rw-r--r-- | libmemunreachable/tests/HeapWalker_test.cpp | 8 | ||||
| -rw-r--r-- | libmemunreachable/tests/HostMallocStub.cpp | 6 | ||||
| -rw-r--r-- | libmemunreachable/tests/LeakFolding_test.cpp | 38 | ||||
| -rw-r--r-- | libmemunreachable/tests/MemUnreachable_test.cpp | 19 | ||||
| -rw-r--r-- | libmemunreachable/tests/ThreadCapture_test.cpp | 182 |
7 files changed, 198 insertions, 205 deletions
diff --git a/libmemunreachable/tests/Allocator_test.cpp b/libmemunreachable/tests/Allocator_test.cpp index 21c821890..0bb4f3134 100644 --- a/libmemunreachable/tests/Allocator_test.cpp +++ b/libmemunreachable/tests/Allocator_test.cpp @@ -16,44 +16,42 @@ #include <Allocator.h> -#include <gtest/gtest.h> #include <ScopedDisableMalloc.h> - +#include <gtest/gtest.h> std::function<void()> ScopedAlarm::func_; class AllocatorTest : public testing::Test { protected: AllocatorTest() : heap(), disable_malloc_() {} - virtual void SetUp() { - heap_count = 0; - } + virtual void SetUp() { heap_count = 0; } virtual void TearDown() { ASSERT_EQ(heap_count, 0); ASSERT_TRUE(heap.empty()); ASSERT_FALSE(disable_malloc_.timed_out()); } Heap heap; + private: ScopedDisableMallocTimeout disable_malloc_; }; TEST_F(AllocatorTest, simple) { Allocator<char[100]> allocator(heap); - void *ptr = allocator.allocate(); + void* ptr = allocator.allocate(); ASSERT_TRUE(ptr != NULL); allocator.deallocate(ptr); } TEST_F(AllocatorTest, multiple) { Allocator<char[100]> allocator(heap); - void *ptr1 = allocator.allocate(); + void* ptr1 = allocator.allocate(); ASSERT_TRUE(ptr1 != NULL); - void *ptr2 = allocator.allocate(); + void* ptr2 = allocator.allocate(); ASSERT_TRUE(ptr2 != NULL); ASSERT_NE(ptr1, ptr2); allocator.deallocate(ptr1); - void *ptr3 = allocator.allocate(); + void* ptr3 = allocator.allocate(); ASSERT_EQ(ptr1, ptr3); allocator.deallocate(ptr3); allocator.deallocate(ptr2); @@ -63,7 +61,7 @@ TEST_F(AllocatorTest, many) { const int num = 4096; const int size = 128; Allocator<char[size]> allocator(heap); - void *ptr[num]; + void* ptr[num]; for (int i = 0; i < num; i++) { ptr[i] = allocator.allocate(); memset(ptr[i], 0xaa, size); @@ -87,7 +85,7 @@ TEST_F(AllocatorTest, many) { TEST_F(AllocatorTest, large) { const size_t size = 1024 * 1024; Allocator<char[size]> allocator(heap); - void *ptr = allocator.allocate(); + void* ptr = allocator.allocate(); memset(ptr, 0xaa, size); allocator.deallocate(ptr); } @@ -96,7 +94,7 @@ TEST_F(AllocatorTest, many_large) { const int num = 128; const int size = 1024 * 1024; Allocator<char[size]> allocator(heap); - void *ptr[num]; + void* ptr[num]; for (int i = 0; i < num; i++) { ptr[i] = allocator.allocate(); memset(ptr[i], 0xaa, size); diff --git a/libmemunreachable/tests/DisableMalloc_test.cpp b/libmemunreachable/tests/DisableMalloc_test.cpp index 4e6155b81..2db584839 100644 --- a/libmemunreachable/tests/DisableMalloc_test.cpp +++ b/libmemunreachable/tests/DisableMalloc_test.cpp @@ -19,8 +19,8 @@ #include <chrono> #include <functional> -#include <gtest/gtest.h> #include <ScopedDisableMalloc.h> +#include <gtest/gtest.h> using namespace std::chrono_literals; @@ -36,75 +36,83 @@ class DisableMallocTest : public ::testing::Test { }; TEST_F(DisableMallocTest, reenable) { - ASSERT_EXIT({ - alarm(100ms); - void *ptr1 = malloc(128); - ASSERT_NE(ptr1, nullptr); - free(ptr1); - { - ScopedDisableMalloc disable_malloc; - } - void *ptr2 = malloc(128); - ASSERT_NE(ptr2, nullptr); - free(ptr2); - _exit(1); - }, ::testing::ExitedWithCode(1), ""); + ASSERT_EXIT( + { + alarm(100ms); + void* ptr1 = malloc(128); + ASSERT_NE(ptr1, nullptr); + free(ptr1); + { ScopedDisableMalloc disable_malloc; } + void* ptr2 = malloc(128); + ASSERT_NE(ptr2, nullptr); + free(ptr2); + _exit(1); + }, + ::testing::ExitedWithCode(1), ""); } TEST_F(DisableMallocTest, deadlock_allocate) { - ASSERT_DEATH({ - void *ptr = malloc(128); - ASSERT_NE(ptr, nullptr); - free(ptr); - { - alarm(100ms); - ScopedDisableMalloc disable_malloc; - void* ptr = malloc(128); - ASSERT_NE(ptr, nullptr); - free(ptr); - } - }, ""); + ASSERT_DEATH( + { + void* ptr = malloc(128); + ASSERT_NE(ptr, nullptr); + free(ptr); + { + alarm(100ms); + ScopedDisableMalloc disable_malloc; + void* ptr = malloc(128); + ASSERT_NE(ptr, nullptr); + free(ptr); + } + }, + ""); } TEST_F(DisableMallocTest, deadlock_new) { - ASSERT_DEATH({ - char* ptr = new(char); - ASSERT_NE(ptr, nullptr); - delete(ptr); - { - alarm(100ms); - ScopedDisableMalloc disable_malloc; - char* ptr = new (std::nothrow)(char); - ASSERT_NE(ptr, nullptr); - delete(ptr); - } - }, ""); + ASSERT_DEATH( + { + char* ptr = new (char); + ASSERT_NE(ptr, nullptr); + delete (ptr); + { + alarm(100ms); + ScopedDisableMalloc disable_malloc; + char* ptr = new (std::nothrow)(char); + ASSERT_NE(ptr, nullptr); + delete (ptr); + } + }, + ""); } TEST_F(DisableMallocTest, deadlock_delete) { - ASSERT_DEATH({ - char* ptr = new(char); - ASSERT_NE(ptr, nullptr); - { - alarm(250ms); - ScopedDisableMalloc disable_malloc; - delete(ptr); - // Force ptr usage or this code gets optimized away by the arm64 compiler. - ASSERT_NE(ptr, nullptr); - } - }, ""); + ASSERT_DEATH( + { + char* ptr = new (char); + ASSERT_NE(ptr, nullptr); + { + alarm(250ms); + ScopedDisableMalloc disable_malloc; + delete (ptr); + // Force ptr usage or this code gets optimized away by the arm64 compiler. + ASSERT_NE(ptr, nullptr); + } + }, + ""); } TEST_F(DisableMallocTest, deadlock_free) { - ASSERT_DEATH({ - void *ptr = malloc(128); - ASSERT_NE(ptr, nullptr); - { - alarm(100ms); - ScopedDisableMalloc disable_malloc; - free(ptr); - } - }, ""); + ASSERT_DEATH( + { + void* ptr = malloc(128); + ASSERT_NE(ptr, nullptr); + { + alarm(100ms); + ScopedDisableMalloc disable_malloc; + free(ptr); + } + }, + ""); } TEST_F(DisableMallocTest, deadlock_fork) { @@ -113,6 +121,6 @@ TEST_F(DisableMallocTest, deadlock_fork) { alarm(100ms); ScopedDisableMalloc disable_malloc; fork(); - } - }, ""); +} +}, ""); } diff --git a/libmemunreachable/tests/HeapWalker_test.cpp b/libmemunreachable/tests/HeapWalker_test.cpp index 98e4aa1fd..1b258ee75 100644 --- a/libmemunreachable/tests/HeapWalker_test.cpp +++ b/libmemunreachable/tests/HeapWalker_test.cpp @@ -19,8 +19,8 @@ #include "HeapWalker.h" -#include <gtest/gtest.h> #include <ScopedDisableMalloc.h> +#include <gtest/gtest.h> #include "Allocator.h" class HeapWalkerTest : public ::testing::Test { @@ -172,20 +172,20 @@ TEST_F(HeapWalkerTest, cycle) { ASSERT_EQ(true, heap_walker.Leaked(leaked, 100, &num_leaks, &leaked_bytes)); EXPECT_EQ(2U, num_leaks); - EXPECT_EQ(2*sizeof(uintptr_t), leaked_bytes); + EXPECT_EQ(2 * sizeof(uintptr_t), leaked_bytes); ASSERT_EQ(2U, leaked.size()); } TEST_F(HeapWalkerTest, segv) { const size_t page_size = sysconf(_SC_PAGE_SIZE); - void* buffer1 = mmap(NULL, page_size, PROT_NONE, MAP_ANONYMOUS|MAP_PRIVATE, -1, 0); + void* buffer1 = mmap(NULL, page_size, PROT_NONE, MAP_ANONYMOUS | MAP_PRIVATE, -1, 0); ASSERT_NE(buffer1, nullptr); void* buffer2; buffer2 = &buffer1; HeapWalker heap_walker(heap_); - heap_walker.Allocation(buffer_begin(buffer1), buffer_begin(buffer1)+page_size); + heap_walker.Allocation(buffer_begin(buffer1), buffer_begin(buffer1) + page_size); heap_walker.Root(buffer_begin(buffer2), buffer_end(buffer2)); ASSERT_EQ(true, heap_walker.DetectLeaks()); diff --git a/libmemunreachable/tests/HostMallocStub.cpp b/libmemunreachable/tests/HostMallocStub.cpp index a7e3f07d3..0ef04870f 100644 --- a/libmemunreachable/tests/HostMallocStub.cpp +++ b/libmemunreachable/tests/HostMallocStub.cpp @@ -16,8 +16,6 @@ #include "bionic.h" -void malloc_disable() { -} +void malloc_disable() {} -void malloc_enable() { -} +void malloc_enable() {} diff --git a/libmemunreachable/tests/LeakFolding_test.cpp b/libmemunreachable/tests/LeakFolding_test.cpp index e85df5f58..7ae7f7607 100644 --- a/libmemunreachable/tests/LeakFolding_test.cpp +++ b/libmemunreachable/tests/LeakFolding_test.cpp @@ -14,11 +14,11 @@ * limitations under the License. */ -#include "HeapWalker.h" #include "LeakFolding.h" +#include "HeapWalker.h" -#include <gtest/gtest.h> #include <ScopedDisableMalloc.h> +#include <gtest/gtest.h> #include "Allocator.h" class LeakFoldingTest : public ::testing::Test { @@ -84,7 +84,7 @@ TEST_F(LeakFoldingTest, two) { ASSERT_EQ(true, folding.Leaked(leaked, &num_leaks, &leaked_bytes)); EXPECT_EQ(2U, num_leaks); - EXPECT_EQ(2*sizeof(uintptr_t), leaked_bytes); + EXPECT_EQ(2 * sizeof(uintptr_t), leaked_bytes); ASSERT_EQ(2U, leaked.size()); EXPECT_EQ(0U, leaked[0].referenced_count); EXPECT_EQ(0U, leaked[0].referenced_size); @@ -113,7 +113,7 @@ TEST_F(LeakFoldingTest, dominator) { ASSERT_EQ(true, folding.Leaked(leaked, &num_leaks, &leaked_bytes)); EXPECT_EQ(2U, num_leaks); - EXPECT_EQ(2*sizeof(uintptr_t), leaked_bytes); + EXPECT_EQ(2 * sizeof(uintptr_t), leaked_bytes); ASSERT_EQ(1U, leaked.size()); EXPECT_EQ(1U, leaked[0].referenced_count); EXPECT_EQ(sizeof(uintptr_t), leaked[0].referenced_size); @@ -144,10 +144,10 @@ TEST_F(LeakFoldingTest, cycle) { ASSERT_EQ(true, folding.Leaked(leaked, &num_leaks, &leaked_bytes)); EXPECT_EQ(3U, num_leaks); - EXPECT_EQ(3*sizeof(uintptr_t), leaked_bytes); + EXPECT_EQ(3 * sizeof(uintptr_t), leaked_bytes); ASSERT_EQ(1U, leaked.size()); EXPECT_EQ(2U, leaked[0].referenced_count); - EXPECT_EQ(2*sizeof(uintptr_t), leaked[0].referenced_size); + EXPECT_EQ(2 * sizeof(uintptr_t), leaked[0].referenced_size); } TEST_F(LeakFoldingTest, dominator_cycle) { @@ -175,13 +175,13 @@ TEST_F(LeakFoldingTest, dominator_cycle) { ASSERT_EQ(true, folding.Leaked(leaked, &num_leaks, &leaked_bytes)); EXPECT_EQ(3U, num_leaks); - EXPECT_EQ(5*sizeof(uintptr_t), leaked_bytes); + EXPECT_EQ(5 * sizeof(uintptr_t), leaked_bytes); ASSERT_EQ(2U, leaked.size()); EXPECT_EQ(2U, leaked[0].referenced_count); - EXPECT_EQ(3*sizeof(uintptr_t), leaked[0].referenced_size); + EXPECT_EQ(3 * sizeof(uintptr_t), leaked[0].referenced_size); EXPECT_EQ(2U, leaked[1].referenced_count); - EXPECT_EQ(3*sizeof(uintptr_t), leaked[1].referenced_size); + EXPECT_EQ(3 * sizeof(uintptr_t), leaked[1].referenced_size); } TEST_F(LeakFoldingTest, two_cycles) { @@ -218,12 +218,12 @@ TEST_F(LeakFoldingTest, two_cycles) { ASSERT_EQ(true, folding.Leaked(leaked, &num_leaks, &leaked_bytes)); EXPECT_EQ(6U, num_leaks); - EXPECT_EQ(6*sizeof(uintptr_t), leaked_bytes); + EXPECT_EQ(6 * sizeof(uintptr_t), leaked_bytes); ASSERT_EQ(2U, leaked.size()); EXPECT_EQ(2U, leaked[0].referenced_count); - EXPECT_EQ(2*sizeof(uintptr_t), leaked[0].referenced_size); + EXPECT_EQ(2 * sizeof(uintptr_t), leaked[0].referenced_size); EXPECT_EQ(2U, leaked[1].referenced_count); - EXPECT_EQ(2*sizeof(uintptr_t), leaked[1].referenced_size); + EXPECT_EQ(2 * sizeof(uintptr_t), leaked[1].referenced_size); } TEST_F(LeakFoldingTest, two_dominator_cycles) { @@ -254,7 +254,7 @@ TEST_F(LeakFoldingTest, two_dominator_cycles) { ASSERT_EQ(true, folding.Leaked(leaked, &num_leaks, &leaked_bytes)); EXPECT_EQ(4U, num_leaks); - EXPECT_EQ(4*sizeof(uintptr_t), leaked_bytes); + EXPECT_EQ(4 * sizeof(uintptr_t), leaked_bytes); ASSERT_EQ(4U, leaked.size()); EXPECT_EQ(1U, leaked[0].referenced_count); EXPECT_EQ(sizeof(uintptr_t), leaked[0].referenced_size); @@ -272,13 +272,13 @@ TEST_F(LeakFoldingTest, giant_dominator_cycle) { HeapWalker heap_walker(heap_); - for (size_t i = 0; i < n; i ++) { + for (size_t i = 0; i < n; i++) { ASSERT_TRUE(heap_walker.Allocation(reinterpret_cast<uintptr_t>(&buffer[i]), - reinterpret_cast<uintptr_t>(&buffer[i+1]))); + reinterpret_cast<uintptr_t>(&buffer[i + 1]))); } for (size_t i = 0; i < n - 1; i++) { - buffer[i] = &buffer[i+1]; + buffer[i] = &buffer[i + 1]; } buffer[n - 1] = &buffer[0]; @@ -306,15 +306,15 @@ TEST_F(LeakFoldingTest, giant_cycle) { HeapWalker heap_walker(heap_); for (size_t i = 0; i < n - 1; i++) { - buffer[i] = &buffer[i+1]; + buffer[i] = &buffer[i + 1]; } buffer[n - 1] = &buffer[0]; buffer1[0] = &buffer[0]; - for (size_t i = 0; i < n; i ++) { + for (size_t i = 0; i < n; i++) { ASSERT_TRUE(heap_walker.Allocation(reinterpret_cast<uintptr_t>(&buffer[i]), - reinterpret_cast<uintptr_t>(&buffer[i+1]))); + reinterpret_cast<uintptr_t>(&buffer[i + 1]))); } ALLOCATION(heap_walker, buffer1); diff --git a/libmemunreachable/tests/MemUnreachable_test.cpp b/libmemunreachable/tests/MemUnreachable_test.cpp index 71da3655a..a231f4fb6 100644 --- a/libmemunreachable/tests/MemUnreachable_test.cpp +++ b/libmemunreachable/tests/MemUnreachable_test.cpp @@ -16,8 +16,8 @@ #include <fcntl.h> #include <stdlib.h> -#include <unistd.h> #include <sys/prctl.h> +#include <unistd.h> #include <gtest/gtest.h> @@ -25,23 +25,16 @@ class HiddenPointer { public: - explicit HiddenPointer(size_t size = 256) { - Set(malloc(size)); - } - ~HiddenPointer() { - Free(); - } - void* Get() { - return reinterpret_cast<void*>(~ptr_); - } + explicit HiddenPointer(size_t size = 256) { Set(malloc(size)); } + ~HiddenPointer() { Free(); } + void* Get() { return reinterpret_cast<void*>(~ptr_); } void Free() { free(Get()); Set(nullptr); } + private: - void Set(void* ptr) { - ptr_ = ~reinterpret_cast<uintptr_t>(ptr); - } + void Set(void* ptr) { ptr_ = ~reinterpret_cast<uintptr_t>(ptr); } volatile uintptr_t ptr_; }; diff --git a/libmemunreachable/tests/ThreadCapture_test.cpp b/libmemunreachable/tests/ThreadCapture_test.cpp index 44aabd755..5bcb79e51 100644 --- a/libmemunreachable/tests/ThreadCapture_test.cpp +++ b/libmemunreachable/tests/ThreadCapture_test.cpp @@ -45,12 +45,10 @@ class ThreadListTest : public ::testing::TestWithParam<int> { WaitForThreads(); } - virtual void TearDown() { - ASSERT_TRUE(heap.empty()); - } + virtual void TearDown() { ASSERT_TRUE(heap.empty()); } protected: - template<class Function> + template <class Function> void StartThreads(unsigned int threads, Function&& func) { threads_.reserve(threads); tids_.reserve(threads); @@ -68,14 +66,14 @@ class ThreadListTest : public ::testing::TestWithParam<int> { { std::unique_lock<std::mutex> lk(m_); - cv_stop_.wait(lk, [&] {return stop_;}); + cv_stop_.wait(lk, [&] { return stop_; }); } }); } { std::unique_lock<std::mutex> lk(m_); - cv_start_.wait(lk, [&]{ return tids_.size() == threads; }); + cv_start_.wait(lk, [&] { return tids_.size() == threads; }); } } @@ -93,9 +91,7 @@ class ThreadListTest : public ::testing::TestWithParam<int> { tids_.clear(); } - std::vector<pid_t>& tids() { - return tids_; - } + std::vector<pid_t>& tids() { return tids_; } Heap heap; @@ -143,7 +139,7 @@ TEST_F(ThreadListTest, list_one) { TEST_P(ThreadListTest, list_some) { const unsigned int threads = GetParam() - 1; - StartThreads(threads, [](){}); + StartThreads(threads, []() {}); std::vector<pid_t> expected_tids = tids(); expected_tids.push_back(getpid()); @@ -176,10 +172,8 @@ class ThreadCaptureTest : public ThreadListTest { public: ThreadCaptureTest() {} ~ThreadCaptureTest() {} - void Fork(std::function<void()>&& child_init, - std::function<void()>&& child_cleanup, - std::function<void(pid_t)>&& parent) { - + void Fork(std::function<void()>&& child_init, std::function<void()>&& child_cleanup, + std::function<void(pid_t)>&& parent) { ScopedPipe start_pipe; ScopedPipe stop_pipe; @@ -211,39 +205,40 @@ class ThreadCaptureTest : public ThreadListTest { TEST_P(ThreadCaptureTest, capture_some) { const unsigned int threads = GetParam(); - Fork([&](){ - // child init - StartThreads(threads - 1, [](){}); - }, - [&](){ - // child cleanup - StopThreads(); - }, - [&](pid_t child){ - // parent - ASSERT_GT(child, 0); + Fork( + [&]() { + // child init + StartThreads(threads - 1, []() {}); + }, + [&]() { + // child cleanup + StopThreads(); + }, + [&](pid_t child) { + // parent + ASSERT_GT(child, 0); - { - ScopedDisableMallocTimeout disable_malloc; + { + ScopedDisableMallocTimeout disable_malloc; - ThreadCapture thread_capture(child, heap); - auto list_tids = allocator::vector<pid_t>(heap); + ThreadCapture thread_capture(child, heap); + auto list_tids = allocator::vector<pid_t>(heap); - ASSERT_TRUE(thread_capture.ListThreads(list_tids)); - ASSERT_EQ(threads, list_tids.size()); + ASSERT_TRUE(thread_capture.ListThreads(list_tids)); + ASSERT_EQ(threads, list_tids.size()); - ASSERT_TRUE(thread_capture.CaptureThreads()); + ASSERT_TRUE(thread_capture.CaptureThreads()); - auto thread_info = allocator::vector<ThreadInfo>(heap); - ASSERT_TRUE(thread_capture.CapturedThreadInfo(thread_info)); - ASSERT_EQ(threads, thread_info.size()); - ASSERT_TRUE(thread_capture.ReleaseThreads()); + auto thread_info = allocator::vector<ThreadInfo>(heap); + ASSERT_TRUE(thread_capture.CapturedThreadInfo(thread_info)); + ASSERT_EQ(threads, thread_info.size()); + ASSERT_TRUE(thread_capture.ReleaseThreads()); - if (!HasFailure()) { - ASSERT_FALSE(disable_malloc.timed_out()); - } -} - }); + if (!HasFailure()) { + ASSERT_FALSE(disable_malloc.timed_out()); + } + } + }); } INSTANTIATE_TEST_CASE_P(ThreadCaptureTest, ThreadCaptureTest, ::testing::Values(1, 2, 10, 1024)); @@ -262,7 +257,7 @@ TEST_F(ThreadCaptureTest, capture_kill) { ScopedDisableMallocTimeout disable_malloc; ThreadCapture thread_capture(ret, heap); - thread_capture.InjectTestFunc([&](pid_t tid){ + thread_capture.InjectTestFunc([&](pid_t tid) { syscall(SYS_tgkill, ret, tid, SIGKILL); usleep(10000); }); @@ -288,62 +283,63 @@ TEST_F(ThreadCaptureTest, capture_signal) { // For signal handler static ScopedPipe* g_pipe; - Fork([&](){ - // child init - pipe.CloseReceiver(); - - g_pipe = &pipe; - - struct sigaction act{}; - act.sa_handler = [](int){ - char buf = '+'; - write(g_pipe->Sender(), &buf, 1); - g_pipe->CloseSender(); - }; - sigaction(sig, &act, NULL); - sigset_t set; - sigemptyset(&set); - sigaddset(&set, sig); - pthread_sigmask(SIG_UNBLOCK, &set, NULL); - }, - [&](){ - // child cleanup - g_pipe = nullptr; - pipe.Close(); - }, - [&](pid_t child){ - // parent - ASSERT_GT(child, 0); - pipe.CloseSender(); + Fork( + [&]() { + // child init + pipe.CloseReceiver(); + + g_pipe = &pipe; + + struct sigaction act {}; + act.sa_handler = [](int) { + char buf = '+'; + write(g_pipe->Sender(), &buf, 1); + g_pipe->CloseSender(); + }; + sigaction(sig, &act, NULL); + sigset_t set; + sigemptyset(&set); + sigaddset(&set, sig); + pthread_sigmask(SIG_UNBLOCK, &set, NULL); + }, + [&]() { + // child cleanup + g_pipe = nullptr; + pipe.Close(); + }, + [&](pid_t child) { + // parent + ASSERT_GT(child, 0); + pipe.CloseSender(); - { - ScopedDisableMallocTimeout disable_malloc; + { + ScopedDisableMallocTimeout disable_malloc; - ThreadCapture thread_capture(child, heap); - thread_capture.InjectTestFunc([&](pid_t tid){ - syscall(SYS_tgkill, child, tid, sig); - usleep(10000); - }); - auto list_tids = allocator::vector<pid_t>(heap); + ThreadCapture thread_capture(child, heap); + thread_capture.InjectTestFunc([&](pid_t tid) { + syscall(SYS_tgkill, child, tid, sig); + usleep(10000); + }); + auto list_tids = allocator::vector<pid_t>(heap); - ASSERT_TRUE(thread_capture.ListThreads(list_tids)); - ASSERT_EQ(1U, list_tids.size()); + ASSERT_TRUE(thread_capture.ListThreads(list_tids)); + ASSERT_EQ(1U, list_tids.size()); - ASSERT_TRUE(thread_capture.CaptureThreads()); + ASSERT_TRUE(thread_capture.CaptureThreads()); - auto thread_info = allocator::vector<ThreadInfo>(heap); - ASSERT_TRUE(thread_capture.CapturedThreadInfo(thread_info)); - ASSERT_EQ(1U, thread_info.size()); - ASSERT_TRUE(thread_capture.ReleaseThreads()); + auto thread_info = allocator::vector<ThreadInfo>(heap); + ASSERT_TRUE(thread_capture.CapturedThreadInfo(thread_info)); + ASSERT_EQ(1U, thread_info.size()); + ASSERT_TRUE(thread_capture.ReleaseThreads()); - usleep(100000); - char buf; - ASSERT_EQ(1, TEMP_FAILURE_RETRY(read(pipe.Receiver(), &buf, 1))); - ASSERT_EQ(buf, '+'); + usleep(100000); + char buf; + ASSERT_EQ(1, TEMP_FAILURE_RETRY(read(pipe.Receiver(), &buf, 1))); + ASSERT_EQ(buf, '+'); - if (!HasFailure()) { - ASSERT_FALSE(disable_malloc.timed_out()); - } - } - }); + if (!HasFailure()) { + ASSERT_FALSE(disable_malloc.timed_out()); + } + } + }); } |
