blob: 296b6bf827d8b63c9aaf426e8b3e9382b4960392 [file] [log] [blame]
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07001/*
2 * Copyright (C) 2012 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
Elliott Hughesca3f8e42019-10-28 15:59:38 -070017#pragma once
Elliott Hughes33697a02016-01-26 13:04:57 -080018
Tom Cherryfd7216c2019-11-05 11:31:42 -080019#include <dirent.h>
Elliott Hughes966d8a32017-08-29 11:29:28 -070020#include <dlfcn.h>
Elliott Hughesa7f12942017-12-15 13:55:53 -080021#include <fcntl.h>
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -070022#include <inttypes.h>
23#include <sys/mman.h>
Evgenii Stepanov53df1f32021-07-12 14:44:02 -070024#include <sys/prctl.h>
Elliott Hughes33697a02016-01-26 13:04:57 -080025#include <sys/types.h>
26#include <sys/wait.h>
Yabin Cui76144aa2015-11-19 13:52:16 -080027#include <unistd.h>
28
Martin Stjernholma2763432020-04-23 16:47:19 +010029#if defined(__BIONIC__)
Evgeny Eltsinb56d1182020-06-08 21:12:56 +020030#include <sys/system_properties.h>
31#endif
32
33#if defined(__BIONIC__)
Peter Collingbourne2528dab2020-03-12 18:29:52 -070034#include <bionic/macros.h>
Martin Stjernholma2763432020-04-23 16:47:19 +010035#else
36#define untag_address(p) p
37#endif
Peter Collingbourne2528dab2020-03-12 18:29:52 -070038
Yabin Cui76144aa2015-11-19 13:52:16 -080039#include <atomic>
40#include <string>
41#include <regex>
42
Elliott Hughes939a7e02015-12-04 15:27:46 -080043#include <android-base/file.h>
Elliott Hughes4af70b42017-11-28 18:02:16 -080044#include <android-base/macros.h>
Tom Cherryb8ab6182017-04-05 16:20:29 -070045#include <android-base/scopeguard.h>
Elliott Hughes939a7e02015-12-04 15:27:46 -080046#include <android-base/stringprintf.h>
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -070047
Dimitry Ivanov35c8e3b2017-02-27 12:17:47 -080048#if defined(__LP64__)
49#define PATH_TO_SYSTEM_LIB "/system/lib64/"
50#else
51#define PATH_TO_SYSTEM_LIB "/system/lib/"
52#endif
53
Elliott Hughes14e3ff92017-10-06 16:58:36 -070054#if defined(__GLIBC__)
55#define BIN_DIR "/bin/"
56#else
57#define BIN_DIR "/system/bin/"
58#endif
59
Josh Gao2f06e102017-01-10 13:00:37 -080060#if defined(__BIONIC__)
61#define KNOWN_FAILURE_ON_BIONIC(x) xfail_ ## x
62#else
63#define KNOWN_FAILURE_ON_BIONIC(x) x
64#endif
65
Elliott Hughes966d8a32017-08-29 11:29:28 -070066// bionic's dlsym doesn't work in static binaries, so we can't access icu,
67// so any unicode test case will fail.
68static inline bool have_dl() {
69 return (dlopen("libc.so", 0) != nullptr);
70}
71
Evgenii Stepanovacd6f4f2018-11-06 16:48:27 -080072extern "C" void __hwasan_init() __attribute__((weak));
73
74static inline bool running_with_hwasan() {
75 return &__hwasan_init != 0;
76}
77
Elliott Hughesbcaa4542019-03-08 15:20:23 -080078#define SKIP_WITH_HWASAN if (running_with_hwasan()) GTEST_SKIP()
Evgenii Stepanovacd6f4f2018-11-06 16:48:27 -080079
Evgeny Eltsinb56d1182020-06-08 21:12:56 +020080static inline bool running_with_native_bridge() {
81#if defined(__BIONIC__)
Lev Rumyantsev37c5ed32020-09-18 15:09:01 -070082 static const prop_info* pi = __system_property_find("ro.dalvik.vm.isa." ABI_STRING);
Evgeny Eltsinb56d1182020-06-08 21:12:56 +020083 return pi != nullptr;
Evgeny Eltsinb56d1182020-06-08 21:12:56 +020084#endif
85 return false;
86}
87
88#define SKIP_WITH_NATIVE_BRIDGE if (running_with_native_bridge()) GTEST_SKIP()
89
Elliott Hughesffbb0f82016-10-10 18:34:27 -070090#if defined(__linux__)
91
Elliott Hughes3fa758f2017-05-17 17:36:08 -070092#include <sys/sysmacros.h>
93
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -070094struct map_record {
95 uintptr_t addr_start;
96 uintptr_t addr_end;
97
98 int perms;
99
100 size_t offset;
101
102 dev_t device;
103 ino_t inode;
104
105 std::string pathname;
106};
107
108class Maps {
109 public:
110 static bool parse_maps(std::vector<map_record>* maps) {
Elliott Hughesa8384902017-10-03 10:18:58 -0700111 maps->clear();
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700112
Elliott Hughesa8384902017-10-03 10:18:58 -0700113 std::unique_ptr<FILE, decltype(&fclose)> fp(fopen("/proc/self/maps", "re"), fclose);
114 if (!fp) return false;
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700115
116 char line[BUFSIZ];
Elliott Hughesa8384902017-10-03 10:18:58 -0700117 while (fgets(line, sizeof(line), fp.get()) != nullptr) {
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700118 map_record record;
Dmitriy Ivanov1dce3ed2015-04-06 19:05:58 -0700119 uint32_t dev_major, dev_minor;
Elliott Hughes15dfd632015-09-22 16:40:14 -0700120 int path_offset;
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700121 char prot[5]; // sizeof("rwxp")
Elliott Hughes15dfd632015-09-22 16:40:14 -0700122 if (sscanf(line, "%" SCNxPTR "-%" SCNxPTR " %4s %" SCNxPTR " %x:%x %lu %n",
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700123 &record.addr_start, &record.addr_end, prot, &record.offset,
Elliott Hughes15dfd632015-09-22 16:40:14 -0700124 &dev_major, &dev_minor, &record.inode, &path_offset) == 7) {
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700125 record.perms = 0;
126 if (prot[0] == 'r') {
127 record.perms |= PROT_READ;
128 }
129 if (prot[1] == 'w') {
130 record.perms |= PROT_WRITE;
131 }
132 if (prot[2] == 'x') {
133 record.perms |= PROT_EXEC;
134 }
135
136 // TODO: parse shared/private?
137
138 record.device = makedev(dev_major, dev_minor);
Elliott Hughes15dfd632015-09-22 16:40:14 -0700139 record.pathname = line + path_offset;
140 if (!record.pathname.empty() && record.pathname.back() == '\n') {
141 record.pathname.pop_back();
142 }
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700143 maps->push_back(record);
144 }
145 }
146
147 return true;
148 }
149};
150
Yabin Cui76144aa2015-11-19 13:52:16 -0800151extern "C" pid_t gettid();
152
Elliott Hughesffbb0f82016-10-10 18:34:27 -0700153#endif
154
Yabin Cui76144aa2015-11-19 13:52:16 -0800155static inline void WaitUntilThreadSleep(std::atomic<pid_t>& tid) {
156 while (tid == 0) {
157 usleep(1000);
158 }
159 std::string filename = android::base::StringPrintf("/proc/%d/stat", tid.load());
160 std::regex regex {R"(\s+S\s+)"};
161
162 while (true) {
163 std::string content;
164 ASSERT_TRUE(android::base::ReadFileToString(filename, &content));
165 if (std::regex_search(content, regex)) {
166 break;
167 }
168 usleep(1000);
169 }
170}
171
Christopher Ferrisd6a1dc22018-02-07 18:42:14 -0800172static inline void AssertChildExited(int pid, int expected_exit_status,
173 const std::string* error_msg = nullptr) {
Elliott Hughes33697a02016-01-26 13:04:57 -0800174 int status;
Christopher Ferrisd6a1dc22018-02-07 18:42:14 -0800175 std::string error;
176 if (error_msg == nullptr) {
177 error_msg = &error;
178 }
179 ASSERT_EQ(pid, TEMP_FAILURE_RETRY(waitpid(pid, &status, 0))) << *error_msg;
Evgenii Stepanov68ecec12017-01-31 13:19:30 -0800180 if (expected_exit_status >= 0) {
Christopher Ferrisd6a1dc22018-02-07 18:42:14 -0800181 ASSERT_TRUE(WIFEXITED(status)) << *error_msg;
182 ASSERT_EQ(expected_exit_status, WEXITSTATUS(status)) << *error_msg;
Evgenii Stepanov68ecec12017-01-31 13:19:30 -0800183 } else {
Christopher Ferrisd6a1dc22018-02-07 18:42:14 -0800184 ASSERT_TRUE(WIFSIGNALED(status)) << *error_msg;
185 ASSERT_EQ(-expected_exit_status, WTERMSIG(status)) << *error_msg;
Evgenii Stepanov68ecec12017-01-31 13:19:30 -0800186 }
Elliott Hughes33697a02016-01-26 13:04:57 -0800187}
188
Elliott Hughesf9cfecf2021-02-04 16:58:13 -0800189static inline bool CloseOnExec(int fd) {
Elliott Hughesa7f12942017-12-15 13:55:53 -0800190 int flags = fcntl(fd, F_GETFD);
Elliott Hughesf9cfecf2021-02-04 16:58:13 -0800191 // This isn't ideal, but the alternatives are worse:
192 // * If we return void and use ASSERT_NE here, we get failures at utils.h:191
193 // rather than in the relevant test.
194 // * If we ignore failures of fcntl(), well, that's obviously a bad idea.
195 if (flags == -1) abort();
196 return flags & FD_CLOEXEC;
Elliott Hughesa7f12942017-12-15 13:55:53 -0800197}
198
Dimitry Ivanov2ba1cf32016-05-17 13:29:37 -0700199// The absolute path to the executable
200const std::string& get_executable_path();
Dimitry Ivanovd17a3772016-03-01 13:11:28 -0800201
Dimitry Ivanov55437462016-07-20 15:33:07 -0700202// Access to argc/argv/envp
203int get_argc();
204char** get_argv();
205char** get_envp();
206
Evgenii Stepanov68ecec12017-01-31 13:19:30 -0800207// ExecTestHelper is only used in bionic and glibc tests.
208#ifndef __APPLE__
209class ExecTestHelper {
210 public:
211 char** GetArgs() {
212 return const_cast<char**>(args_.data());
213 }
Elliott Hughes14e3ff92017-10-06 16:58:36 -0700214 const char* GetArg0() {
215 return args_[0];
216 }
Evgenii Stepanov68ecec12017-01-31 13:19:30 -0800217 char** GetEnv() {
218 return const_cast<char**>(env_.data());
219 }
Ryan Prichard8f639a42018-10-01 23:10:05 -0700220 const std::string& GetOutput() {
221 return output_;
222 }
Evgenii Stepanov68ecec12017-01-31 13:19:30 -0800223
Elliott Hughes5cec3772018-01-19 15:45:23 -0800224 void SetArgs(const std::vector<const char*>& args) {
Evgenii Stepanov68ecec12017-01-31 13:19:30 -0800225 args_ = args;
226 }
Elliott Hughes5cec3772018-01-19 15:45:23 -0800227 void SetEnv(const std::vector<const char*>& env) {
Evgenii Stepanov68ecec12017-01-31 13:19:30 -0800228 env_ = env;
229 }
230
231 void Run(const std::function<void()>& child_fn, int expected_exit_status,
Elliott Hughes419554e2021-10-01 10:12:15 -0700232 const char* expected_output_regex) {
Evgenii Stepanov68ecec12017-01-31 13:19:30 -0800233 int fds[2];
234 ASSERT_NE(pipe(fds), -1);
235
236 pid_t pid = fork();
237 ASSERT_NE(pid, -1);
238
239 if (pid == 0) {
240 // Child.
241 close(fds[0]);
242 dup2(fds[1], STDOUT_FILENO);
243 dup2(fds[1], STDERR_FILENO);
244 if (fds[1] != STDOUT_FILENO && fds[1] != STDERR_FILENO) close(fds[1]);
245 child_fn();
246 FAIL();
247 }
248
249 // Parent.
250 close(fds[1]);
Ryan Prichard8f639a42018-10-01 23:10:05 -0700251 output_.clear();
Evgenii Stepanov68ecec12017-01-31 13:19:30 -0800252 char buf[BUFSIZ];
253 ssize_t bytes_read;
254 while ((bytes_read = TEMP_FAILURE_RETRY(read(fds[0], buf, sizeof(buf)))) > 0) {
Ryan Prichard8f639a42018-10-01 23:10:05 -0700255 output_.append(buf, bytes_read);
Evgenii Stepanov68ecec12017-01-31 13:19:30 -0800256 }
257 close(fds[0]);
258
Ryan Prichard8f639a42018-10-01 23:10:05 -0700259 std::string error_msg("Test output:\n" + output_);
Christopher Ferrisd6a1dc22018-02-07 18:42:14 -0800260 AssertChildExited(pid, expected_exit_status, &error_msg);
Elliott Hughes419554e2021-10-01 10:12:15 -0700261 if (expected_output_regex != nullptr) {
Steven Moreland4ef83d62021-10-07 00:19:18 +0000262 if (!std::regex_search(output_, std::regex(expected_output_regex))) {
263 FAIL() << "regex " << expected_output_regex << " didn't match " << output_;
264 }
Evgenii Stepanov68ecec12017-01-31 13:19:30 -0800265 }
266 }
267
268 private:
269 std::vector<const char*> args_;
270 std::vector<const char*> env_;
Ryan Prichard8f639a42018-10-01 23:10:05 -0700271 std::string output_;
Evgenii Stepanov68ecec12017-01-31 13:19:30 -0800272};
Mitch Phillipse6997d52020-11-30 15:04:14 -0800273
274void RunGwpAsanTest(const char* test_name);
Evgenii Stepanov68ecec12017-01-31 13:19:30 -0800275#endif
Tom Cherryfd7216c2019-11-05 11:31:42 -0800276
277class FdLeakChecker {
278 public:
279 FdLeakChecker() {
280 }
281
282 ~FdLeakChecker() {
283 size_t end_count = CountOpenFds();
284 EXPECT_EQ(start_count_, end_count);
285 }
286
287 private:
288 static size_t CountOpenFds() {
289 auto fd_dir = std::unique_ptr<DIR, decltype(&closedir)>{ opendir("/proc/self/fd"), closedir };
290 size_t count = 0;
291 dirent* de = nullptr;
292 while ((de = readdir(fd_dir.get())) != nullptr) {
293 if (de->d_type == DT_LNK) {
294 ++count;
295 }
296 }
297 return count;
298 }
299
300 size_t start_count_ = CountOpenFds();
301};
Elliott Hughes7cda75f2020-10-22 13:22:35 -0700302
303// From <benchmark/benchmark.h>.
304template <class Tp>
305static inline void DoNotOptimize(Tp const& value) {
306 asm volatile("" : : "r,m"(value) : "memory");
307}
308template <class Tp>
309static inline void DoNotOptimize(Tp& value) {
310 asm volatile("" : "+r,m"(value) : : "memory");
311}
Evgenii Stepanov53df1f32021-07-12 14:44:02 -0700312
313static inline bool running_with_mte() {
314#ifdef __aarch64__
315 int level = prctl(PR_GET_TAGGED_ADDR_CTRL, 0, 0, 0, 0);
316 return level >= 0 && (level & PR_TAGGED_ADDR_ENABLE) &&
317 (level & PR_MTE_TCF_MASK) != PR_MTE_TCF_NONE;
318#else
319 return false;
320#endif
321}