1 /*
2 * Copyright 2017, The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include <debuggerd/client.h>
18
19 #include <fcntl.h>
20 #include <stdio.h>
21 #include <sys/eventfd.h>
22 #include <unistd.h>
23
24 #include <chrono>
25 #include <thread>
26 #include <vector>
27
28 #include <gtest/gtest.h>
29
30 #include <android-base/file.h>
31 #include <android-base/properties.h>
32 #include <android-base/stringprintf.h>
33 #include <android-base/strings.h>
34 #include <android-base/unique_fd.h>
35
36 #include "util.h"
37
38 using namespace std::chrono_literals;
39 using android::base::unique_fd;
40
getThreadCount()41 static int getThreadCount() {
42 int threadCount = 1024;
43 std::vector<std::string> characteristics =
44 android::base::Split(android::base::GetProperty("ro.build.characteristics", ""), ",");
45 if (std::find(characteristics.begin(), characteristics.end(), "embedded")
46 != characteristics.end()) {
47 // 128 is the realistic number for iot devices.
48 threadCount = 128;
49 }
50 return threadCount;
51 }
52
TEST(debuggerd_client,race)53 TEST(debuggerd_client, race) {
54 static int THREAD_COUNT = getThreadCount();
55
56 // Semaphore incremented once per thread started.
57 unique_fd barrier(eventfd(0, EFD_SEMAPHORE));
58 ASSERT_NE(-1, barrier.get());
59
60 pid_t forkpid = fork();
61 ASSERT_NE(-1, forkpid);
62 if (forkpid == 0) {
63 // Spawn a bunch of threads, to make crash_dump take longer.
64 std::vector<std::thread> threads;
65 threads.reserve(THREAD_COUNT);
66 for (int i = 0; i < THREAD_COUNT; ++i) {
67 threads.emplace_back([&barrier]() {
68 uint64_t count = 1;
69 ASSERT_NE(-1, write(barrier.get(), &count, sizeof(count)));
70 for (;;) {
71 pause();
72 }
73 });
74 }
75 for (;;) {
76 pause();
77 }
78 }
79
80 // Wait for the child to spawn all of its threads.
81 for (int i = 0; i < THREAD_COUNT; ++i) {
82 uint64_t count;
83 ASSERT_NE(-1, read(barrier.get(), &count, sizeof(count)));
84 }
85
86 unique_fd pipe_read, pipe_write;
87 ASSERT_TRUE(Pipe(&pipe_read, &pipe_write));
88
89 // 16 MiB should be enough for everyone.
90 constexpr int PIPE_SIZE = 16 * 1024 * 1024;
91 ASSERT_EQ(PIPE_SIZE, fcntl(pipe_read.get(), F_SETPIPE_SZ, PIPE_SIZE));
92
93 ASSERT_TRUE(
94 debuggerd_trigger_dump(forkpid, kDebuggerdNativeBacktrace, 60000, std::move(pipe_write)));
95 // Immediately kill the forked child, to make sure that the dump didn't return early.
96 ASSERT_EQ(0, kill(forkpid, SIGKILL)) << strerror(errno);
97
98 // Check the output.
99 std::string result;
100 ASSERT_TRUE(android::base::ReadFdToString(pipe_read.get(), &result));
101
102 // Look for "----- end <PID> -----"
103 int found_end = 0;
104
105 std::string expected_end = android::base::StringPrintf("----- end %d -----", forkpid);
106
107 std::vector<std::string> lines = android::base::Split(result, "\n");
108 for (const std::string& line : lines) {
109 if (line == expected_end) {
110 ++found_end;
111 }
112 }
113
114 EXPECT_EQ(1, found_end) << "\nOutput: \n" << result;
115 }
116
TEST(debuggerd_client,no_timeout)117 TEST(debuggerd_client, no_timeout) {
118 unique_fd pipe_read, pipe_write;
119 ASSERT_TRUE(Pipe(&pipe_read, &pipe_write));
120
121 pid_t forkpid = fork();
122 ASSERT_NE(-1, forkpid);
123 if (forkpid == 0) {
124 pipe_write.reset();
125 char dummy;
126 TEMP_FAILURE_RETRY(read(pipe_read.get(), &dummy, sizeof(dummy)));
127 exit(0);
128 }
129
130 pipe_read.reset();
131
132 unique_fd output_read, output_write;
133 ASSERT_TRUE(Pipe(&output_read, &output_write));
134 ASSERT_TRUE(
135 debuggerd_trigger_dump(forkpid, kDebuggerdNativeBacktrace, 0, std::move(output_write)));
136 }
137