1 /*
2 * Copyright (C) 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 "perfetto/ext/base/utils.h"
18
19 #include "perfetto/base/build_config.h"
20
21 #if PERFETTO_BUILDFLAG(PERFETTO_OS_WIN)
22 #include <Windows.h>
23 #else
24 #include <fcntl.h>
25 #include <signal.h>
26 #include <unistd.h>
27 #endif
28
29 #include <stdint.h>
30
31 #include <algorithm>
32 #include <random>
33 #include <thread>
34
35 #include "perfetto/ext/base/file_utils.h"
36 #include "perfetto/ext/base/pipe.h"
37 #include "perfetto/ext/base/temp_file.h"
38 #include "test/gtest_and_gmock.h"
39
40 namespace perfetto {
41 namespace base {
42 namespace {
43
TEST(UtilsTest,ArraySize)44 TEST(UtilsTest, ArraySize) {
45 char char_arr_1[1];
46 char char_arr_4[4];
47 EXPECT_EQ(1u, ArraySize(char_arr_1));
48 EXPECT_EQ(4u, ArraySize(char_arr_4));
49
50 int32_t int32_arr_1[1];
51 int32_t int32_arr_4[4];
52 EXPECT_EQ(1u, ArraySize(int32_arr_1));
53 EXPECT_EQ(4u, ArraySize(int32_arr_4));
54
55 uint64_t int64_arr_1[1];
56 uint64_t int64_arr_4[4];
57 EXPECT_EQ(1u, ArraySize(int64_arr_1));
58 EXPECT_EQ(4u, ArraySize(int64_arr_4));
59
60 char kString[] = "foo";
61 EXPECT_EQ(4u, ArraySize(kString));
62
63 struct Bar {
64 int32_t a;
65 int32_t b;
66 };
67 Bar bar_1[1];
68 Bar bar_4[4];
69 EXPECT_EQ(1u, ArraySize(bar_1));
70 EXPECT_EQ(4u, ArraySize(bar_4));
71 }
72
TEST(UtilsTest,PipeBlockingRW)73 TEST(UtilsTest, PipeBlockingRW) {
74 Pipe pipe = Pipe::Create();
75 std::string expected;
76 expected.resize(1024 * 512u);
77 for (size_t i = 0; i < expected.size(); i++)
78 expected[i] = '!' + static_cast<char>(i % 64);
79
80 std::thread writer([&] {
81 std::string tx = expected;
82 std::minstd_rand0 rnd_engine(0);
83
84 while (!tx.empty()) {
85 size_t wsize = static_cast<size_t>(rnd_engine() % 4096) + 1;
86 wsize = std::min(wsize, tx.size());
87 WriteAllHandle(*pipe.wr, &tx[0], wsize);
88 tx.erase(0, wsize);
89 }
90 pipe.wr.reset();
91 });
92
93 std::string actual;
94 ASSERT_TRUE(ReadPlatformHandle(*pipe.rd, &actual));
95 ASSERT_EQ(actual, expected);
96 writer.join();
97 }
98
99 // Tests that WriteAllHandle and ReadPlatformHandle work as advertised.
100 // TODO(primiano): normalize File handling on Windows. Right now some places
101 // use POSIX-compat APIs that use "int" file descriptors (_open, _read, _write),
102 // some other places use WINAPI files (CreateFile(), ReadFile()), where the file
103 // is a HANDLE.
TEST(UtilsTest,ReadWritePlatformHandle)104 TEST(UtilsTest, ReadWritePlatformHandle) {
105 auto tmp = TempDir::Create();
106 std::string payload = "foo\nbar\0baz\r\nqux";
107 std::string tmp_path = tmp.path() + "/temp.txt";
108
109 // Write a file using PlatformHandle. Note: the {} blocks are to make sure
110 // that the file is automatically closed via RAII before being reopened.
111 {
112 ScopedPlatformHandle handle {
113 #if PERFETTO_BUILDFLAG(PERFETTO_OS_WIN)
114 ::CreateFileA(tmp_path.c_str(), GENERIC_WRITE, 0, nullptr, CREATE_ALWAYS,
115 FILE_ATTRIBUTE_NORMAL, nullptr)
116 #else
117 OpenFile(tmp_path, O_WRONLY | O_CREAT | O_TRUNC, 0644)
118 #endif
119 };
120 ASSERT_TRUE(handle);
121 ASSERT_EQ(WriteAllHandle(*handle, payload.data(), payload.size()),
122 static_cast<ssize_t>(payload.size()));
123 }
124
125 // Read it back.
126 {
127 ScopedPlatformHandle handle {
128 #if PERFETTO_BUILDFLAG(PERFETTO_OS_WIN)
129 ::CreateFileA(tmp_path.c_str(), GENERIC_READ, 0, nullptr, OPEN_EXISTING,
130 FILE_ATTRIBUTE_NORMAL, nullptr)
131 #else
132 OpenFile(tmp_path, O_RDONLY)
133 #endif
134 };
135 ASSERT_TRUE(handle);
136 std::string actual = "preserve_existing:";
137 ASSERT_TRUE(ReadPlatformHandle(*handle, &actual));
138 ASSERT_EQ(actual, "preserve_existing:" + payload);
139 }
140
141 ASSERT_EQ(remove(tmp_path.c_str()), 0);
142 }
143
144 // Fuchsia doesn't currently support sigaction(), see
145 // https://fxbug.dev/42105390 .
146 #if PERFETTO_BUILDFLAG(PERFETTO_OS_LINUX) || \
147 PERFETTO_BUILDFLAG(PERFETTO_OS_ANDROID) || \
148 PERFETTO_BUILDFLAG(PERFETTO_OS_APPLE)
TEST(UtilsTest,EintrWrapper)149 TEST(UtilsTest, EintrWrapper) {
150 Pipe pipe = Pipe::Create();
151
152 struct sigaction sa = {};
153 struct sigaction old_sa = {};
154
155 // Glibc headers for sa_sigaction trigger this.
156 #pragma GCC diagnostic push
157 #if defined(__clang__)
158 #pragma GCC diagnostic ignored "-Wdisabled-macro-expansion"
159 #endif
160 sa.sa_sigaction = [](int, siginfo_t*, void*) {};
161 #pragma GCC diagnostic pop
162
163 ASSERT_EQ(0, sigaction(SIGUSR2, &sa, &old_sa));
164 int parent_pid = getpid();
165 pid_t pid = fork();
166 ASSERT_NE(-1, pid);
167 if (pid == 0 /* child */) {
168 usleep(5000);
169 kill(parent_pid, SIGUSR2);
170 ignore_result(WriteAll(*pipe.wr, "foo\0", 4));
171 _exit(0);
172 }
173
174 char buf[6] = {};
175 EXPECT_EQ(4, PERFETTO_EINTR(read(*pipe.rd, buf, sizeof(buf))));
176 EXPECT_TRUE(close(*pipe.rd) == 0 || errno == EINTR);
177 pipe.wr.reset();
178
179 // A 2nd close should fail with the proper errno.
180 int res = close(*pipe.rd);
181 auto err = errno;
182 EXPECT_EQ(-1, res);
183 EXPECT_EQ(EBADF, err);
184 pipe.rd.release();
185
186 // Restore the old handler.
187 sigaction(SIGUSR2, &old_sa, nullptr);
188 }
189 #endif // LINUX | ANDROID | APPLE
190
TEST(UtilsTest,Align)191 TEST(UtilsTest, Align) {
192 EXPECT_EQ(0u, AlignUp<4>(0));
193 EXPECT_EQ(4u, AlignUp<4>(1));
194 EXPECT_EQ(4u, AlignUp<4>(3));
195 EXPECT_EQ(4u, AlignUp<4>(4));
196 EXPECT_EQ(8u, AlignUp<4>(5));
197 EXPECT_EQ(0u, AlignUp<16>(0));
198 EXPECT_EQ(16u, AlignUp<16>(1));
199 EXPECT_EQ(16u, AlignUp<16>(15));
200 EXPECT_EQ(16u, AlignUp<16>(16));
201 EXPECT_EQ(32u, AlignUp<16>(17));
202 EXPECT_EQ(0xffffff00u, AlignUp<16>(0xffffff00 - 1));
203 }
204
TEST(UtilsTest,HexDump)205 TEST(UtilsTest, HexDump) {
206 char input[] = {0x00, 0x00, 'a', 'b', 'c', 'd', 'e', 'f', 'g',
207 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p'};
208
209 std::string output = HexDump(input, sizeof(input));
210
211 EXPECT_EQ(
212 output,
213 R"(00000000: 00 00 61 62 63 64 65 66 67 68 69 6A 6B 6C 6D 6E ..abcdefghijklmn
214 00000010: 6F 70 op
215 )");
216 }
217
218 } // namespace
219 } // namespace base
220 } // namespace perfetto
221