blob: 145ba1ac26609ab818bff89d1c48184731bfebaa [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>
Elliott Hughes33697a02016-01-26 13:04:57 -080024#include <sys/types.h>
25#include <sys/wait.h>
Yabin Cui76144aa2015-11-19 13:52:16 -080026#include <unistd.h>
27
Martin Stjernholma2763432020-04-23 16:47:19 +010028#if defined(__BIONIC__)
Evgeny Eltsinb56d1182020-06-08 21:12:56 +020029#include <sys/system_properties.h>
30#endif
31
32#if defined(__BIONIC__)
Peter Collingbourne2528dab2020-03-12 18:29:52 -070033#include <bionic/macros.h>
Martin Stjernholma2763432020-04-23 16:47:19 +010034#else
35#define untag_address(p) p
36#endif
Peter Collingbourne2528dab2020-03-12 18:29:52 -070037
Yabin Cui76144aa2015-11-19 13:52:16 -080038#include <atomic>
39#include <string>
40#include <regex>
41
Elliott Hughes939a7e02015-12-04 15:27:46 -080042#include <android-base/file.h>
Elliott Hughes4af70b42017-11-28 18:02:16 -080043#include <android-base/macros.h>
Tom Cherryb8ab6182017-04-05 16:20:29 -070044#include <android-base/scopeguard.h>
Elliott Hughes939a7e02015-12-04 15:27:46 -080045#include <android-base/stringprintf.h>
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -070046
Dimitry Ivanov35c8e3b2017-02-27 12:17:47 -080047#if defined(__LP64__)
48#define PATH_TO_SYSTEM_LIB "/system/lib64/"
49#else
50#define PATH_TO_SYSTEM_LIB "/system/lib/"
51#endif
52
Elliott Hughes14e3ff92017-10-06 16:58:36 -070053#if defined(__GLIBC__)
54#define BIN_DIR "/bin/"
55#else
56#define BIN_DIR "/system/bin/"
57#endif
58
Josh Gao2f06e102017-01-10 13:00:37 -080059#if defined(__BIONIC__)
60#define KNOWN_FAILURE_ON_BIONIC(x) xfail_ ## x
61#else
62#define KNOWN_FAILURE_ON_BIONIC(x) x
63#endif
64
Elliott Hughes966d8a32017-08-29 11:29:28 -070065// bionic's dlsym doesn't work in static binaries, so we can't access icu,
66// so any unicode test case will fail.
67static inline bool have_dl() {
68 return (dlopen("libc.so", 0) != nullptr);
69}
70
Evgenii Stepanovacd6f4f2018-11-06 16:48:27 -080071extern "C" void __hwasan_init() __attribute__((weak));
72
73static inline bool running_with_hwasan() {
74 return &__hwasan_init != 0;
75}
76
Elliott Hughesbcaa4542019-03-08 15:20:23 -080077#define SKIP_WITH_HWASAN if (running_with_hwasan()) GTEST_SKIP()
Evgenii Stepanovacd6f4f2018-11-06 16:48:27 -080078
Evgeny Eltsinb56d1182020-06-08 21:12:56 +020079static inline bool running_with_native_bridge() {
80#if defined(__BIONIC__)
Lev Rumyantsev37c5ed32020-09-18 15:09:01 -070081 static const prop_info* pi = __system_property_find("ro.dalvik.vm.isa." ABI_STRING);
Evgeny Eltsinb56d1182020-06-08 21:12:56 +020082 return pi != nullptr;
Evgeny Eltsinb56d1182020-06-08 21:12:56 +020083#endif
84 return false;
85}
86
87#define SKIP_WITH_NATIVE_BRIDGE if (running_with_native_bridge()) GTEST_SKIP()
88
Elliott Hughesffbb0f82016-10-10 18:34:27 -070089#if defined(__linux__)
90
Elliott Hughes3fa758f2017-05-17 17:36:08 -070091#include <sys/sysmacros.h>
92
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -070093struct map_record {
94 uintptr_t addr_start;
95 uintptr_t addr_end;
96
97 int perms;
98
99 size_t offset;
100
101 dev_t device;
102 ino_t inode;
103
104 std::string pathname;
105};
106
107class Maps {
108 public:
109 static bool parse_maps(std::vector<map_record>* maps) {
Elliott Hughesa8384902017-10-03 10:18:58 -0700110 maps->clear();
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700111
Elliott Hughesa8384902017-10-03 10:18:58 -0700112 std::unique_ptr<FILE, decltype(&fclose)> fp(fopen("/proc/self/maps", "re"), fclose);
113 if (!fp) return false;
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700114
115 char line[BUFSIZ];
Elliott Hughesa8384902017-10-03 10:18:58 -0700116 while (fgets(line, sizeof(line), fp.get()) != nullptr) {
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700117 map_record record;
Dmitriy Ivanov1dce3ed2015-04-06 19:05:58 -0700118 uint32_t dev_major, dev_minor;
Elliott Hughes15dfd632015-09-22 16:40:14 -0700119 int path_offset;
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700120 char prot[5]; // sizeof("rwxp")
Elliott Hughes15dfd632015-09-22 16:40:14 -0700121 if (sscanf(line, "%" SCNxPTR "-%" SCNxPTR " %4s %" SCNxPTR " %x:%x %lu %n",
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700122 &record.addr_start, &record.addr_end, prot, &record.offset,
Elliott Hughes15dfd632015-09-22 16:40:14 -0700123 &dev_major, &dev_minor, &record.inode, &path_offset) == 7) {
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700124 record.perms = 0;
125 if (prot[0] == 'r') {
126 record.perms |= PROT_READ;
127 }
128 if (prot[1] == 'w') {
129 record.perms |= PROT_WRITE;
130 }
131 if (prot[2] == 'x') {
132 record.perms |= PROT_EXEC;
133 }
134
135 // TODO: parse shared/private?
136
137 record.device = makedev(dev_major, dev_minor);
Elliott Hughes15dfd632015-09-22 16:40:14 -0700138 record.pathname = line + path_offset;
139 if (!record.pathname.empty() && record.pathname.back() == '\n') {
140 record.pathname.pop_back();
141 }
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700142 maps->push_back(record);
143 }
144 }
145
146 return true;
147 }
148};
149
Yabin Cui76144aa2015-11-19 13:52:16 -0800150extern "C" pid_t gettid();
151
Elliott Hughesffbb0f82016-10-10 18:34:27 -0700152#endif
153
Yabin Cui76144aa2015-11-19 13:52:16 -0800154static inline void WaitUntilThreadSleep(std::atomic<pid_t>& tid) {
155 while (tid == 0) {
156 usleep(1000);
157 }
158 std::string filename = android::base::StringPrintf("/proc/%d/stat", tid.load());
159 std::regex regex {R"(\s+S\s+)"};
160
161 while (true) {
162 std::string content;
163 ASSERT_TRUE(android::base::ReadFileToString(filename, &content));
164 if (std::regex_search(content, regex)) {
165 break;
166 }
167 usleep(1000);
168 }
169}
170
Christopher Ferrisd6a1dc22018-02-07 18:42:14 -0800171static inline void AssertChildExited(int pid, int expected_exit_status,
172 const std::string* error_msg = nullptr) {
Elliott Hughes33697a02016-01-26 13:04:57 -0800173 int status;
Christopher Ferrisd6a1dc22018-02-07 18:42:14 -0800174 std::string error;
175 if (error_msg == nullptr) {
176 error_msg = &error;
177 }
178 ASSERT_EQ(pid, TEMP_FAILURE_RETRY(waitpid(pid, &status, 0))) << *error_msg;
Evgenii Stepanov68ecec12017-01-31 13:19:30 -0800179 if (expected_exit_status >= 0) {
Christopher Ferrisd6a1dc22018-02-07 18:42:14 -0800180 ASSERT_TRUE(WIFEXITED(status)) << *error_msg;
181 ASSERT_EQ(expected_exit_status, WEXITSTATUS(status)) << *error_msg;
Evgenii Stepanov68ecec12017-01-31 13:19:30 -0800182 } else {
Christopher Ferrisd6a1dc22018-02-07 18:42:14 -0800183 ASSERT_TRUE(WIFSIGNALED(status)) << *error_msg;
184 ASSERT_EQ(-expected_exit_status, WTERMSIG(status)) << *error_msg;
Evgenii Stepanov68ecec12017-01-31 13:19:30 -0800185 }
Elliott Hughes33697a02016-01-26 13:04:57 -0800186}
187
Elliott Hughesf9cfecf2021-02-04 16:58:13 -0800188static inline bool CloseOnExec(int fd) {
Elliott Hughesa7f12942017-12-15 13:55:53 -0800189 int flags = fcntl(fd, F_GETFD);
Elliott Hughesf9cfecf2021-02-04 16:58:13 -0800190 // This isn't ideal, but the alternatives are worse:
191 // * If we return void and use ASSERT_NE here, we get failures at utils.h:191
192 // rather than in the relevant test.
193 // * If we ignore failures of fcntl(), well, that's obviously a bad idea.
194 if (flags == -1) abort();
195 return flags & FD_CLOEXEC;
Elliott Hughesa7f12942017-12-15 13:55:53 -0800196}
197
Dimitry Ivanov2ba1cf32016-05-17 13:29:37 -0700198// The absolute path to the executable
199const std::string& get_executable_path();
Dimitry Ivanovd17a3772016-03-01 13:11:28 -0800200
Dimitry Ivanov55437462016-07-20 15:33:07 -0700201// Access to argc/argv/envp
202int get_argc();
203char** get_argv();
204char** get_envp();
205
Evgenii Stepanov68ecec12017-01-31 13:19:30 -0800206// ExecTestHelper is only used in bionic and glibc tests.
207#ifndef __APPLE__
208class ExecTestHelper {
209 public:
210 char** GetArgs() {
211 return const_cast<char**>(args_.data());
212 }
Elliott Hughes14e3ff92017-10-06 16:58:36 -0700213 const char* GetArg0() {
214 return args_[0];
215 }
Evgenii Stepanov68ecec12017-01-31 13:19:30 -0800216 char** GetEnv() {
217 return const_cast<char**>(env_.data());
218 }
Ryan Prichard8f639a42018-10-01 23:10:05 -0700219 const std::string& GetOutput() {
220 return output_;
221 }
Evgenii Stepanov68ecec12017-01-31 13:19:30 -0800222
Elliott Hughes5cec3772018-01-19 15:45:23 -0800223 void SetArgs(const std::vector<const char*>& args) {
Evgenii Stepanov68ecec12017-01-31 13:19:30 -0800224 args_ = args;
225 }
Elliott Hughes5cec3772018-01-19 15:45:23 -0800226 void SetEnv(const std::vector<const char*>& env) {
Evgenii Stepanov68ecec12017-01-31 13:19:30 -0800227 env_ = env;
228 }
229
230 void Run(const std::function<void()>& child_fn, int expected_exit_status,
231 const char* expected_output) {
232 int fds[2];
233 ASSERT_NE(pipe(fds), -1);
234
235 pid_t pid = fork();
236 ASSERT_NE(pid, -1);
237
238 if (pid == 0) {
239 // Child.
240 close(fds[0]);
241 dup2(fds[1], STDOUT_FILENO);
242 dup2(fds[1], STDERR_FILENO);
243 if (fds[1] != STDOUT_FILENO && fds[1] != STDERR_FILENO) close(fds[1]);
244 child_fn();
245 FAIL();
246 }
247
248 // Parent.
249 close(fds[1]);
Ryan Prichard8f639a42018-10-01 23:10:05 -0700250 output_.clear();
Evgenii Stepanov68ecec12017-01-31 13:19:30 -0800251 char buf[BUFSIZ];
252 ssize_t bytes_read;
253 while ((bytes_read = TEMP_FAILURE_RETRY(read(fds[0], buf, sizeof(buf)))) > 0) {
Ryan Prichard8f639a42018-10-01 23:10:05 -0700254 output_.append(buf, bytes_read);
Evgenii Stepanov68ecec12017-01-31 13:19:30 -0800255 }
256 close(fds[0]);
257
Ryan Prichard8f639a42018-10-01 23:10:05 -0700258 std::string error_msg("Test output:\n" + output_);
Christopher Ferrisd6a1dc22018-02-07 18:42:14 -0800259 AssertChildExited(pid, expected_exit_status, &error_msg);
Evgenii Stepanov68ecec12017-01-31 13:19:30 -0800260 if (expected_output != nullptr) {
Ryan Prichard8f639a42018-10-01 23:10:05 -0700261 ASSERT_EQ(expected_output, output_);
Evgenii Stepanov68ecec12017-01-31 13:19:30 -0800262 }
263 }
264
265 private:
266 std::vector<const char*> args_;
267 std::vector<const char*> env_;
Ryan Prichard8f639a42018-10-01 23:10:05 -0700268 std::string output_;
Evgenii Stepanov68ecec12017-01-31 13:19:30 -0800269};
270#endif
Tom Cherryfd7216c2019-11-05 11:31:42 -0800271
272class FdLeakChecker {
273 public:
274 FdLeakChecker() {
275 }
276
277 ~FdLeakChecker() {
278 size_t end_count = CountOpenFds();
279 EXPECT_EQ(start_count_, end_count);
280 }
281
282 private:
283 static size_t CountOpenFds() {
284 auto fd_dir = std::unique_ptr<DIR, decltype(&closedir)>{ opendir("/proc/self/fd"), closedir };
285 size_t count = 0;
286 dirent* de = nullptr;
287 while ((de = readdir(fd_dir.get())) != nullptr) {
288 if (de->d_type == DT_LNK) {
289 ++count;
290 }
291 }
292 return count;
293 }
294
295 size_t start_count_ = CountOpenFds();
296};
Elliott Hughes7cda75f2020-10-22 13:22:35 -0700297
298// From <benchmark/benchmark.h>.
299template <class Tp>
300static inline void DoNotOptimize(Tp const& value) {
301 asm volatile("" : : "r,m"(value) : "memory");
302}
303template <class Tp>
304static inline void DoNotOptimize(Tp& value) {
305 asm volatile("" : "+r,m"(value) : : "memory");
306}