blob: fe41019a71c3284e1ea58f04b69a753648cf9854 [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
17#ifndef __TEST_UTILS_H
18#define __TEST_UTILS_H
Elliott Hughes33697a02016-01-26 13:04:57 -080019
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
28#include <atomic>
29#include <string>
30#include <regex>
31
Elliott Hughes939a7e02015-12-04 15:27:46 -080032#include <android-base/file.h>
Elliott Hughes4af70b42017-11-28 18:02:16 -080033#include <android-base/macros.h>
Tom Cherryb8ab6182017-04-05 16:20:29 -070034#include <android-base/scopeguard.h>
Elliott Hughes939a7e02015-12-04 15:27:46 -080035#include <android-base/stringprintf.h>
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -070036
Dimitry Ivanov35c8e3b2017-02-27 12:17:47 -080037#if defined(__LP64__)
38#define PATH_TO_SYSTEM_LIB "/system/lib64/"
39#else
40#define PATH_TO_SYSTEM_LIB "/system/lib/"
41#endif
42
Elliott Hughes14e3ff92017-10-06 16:58:36 -070043#if defined(__GLIBC__)
44#define BIN_DIR "/bin/"
45#else
46#define BIN_DIR "/system/bin/"
47#endif
48
Josh Gao2f06e102017-01-10 13:00:37 -080049#if defined(__BIONIC__)
50#define KNOWN_FAILURE_ON_BIONIC(x) xfail_ ## x
51#else
52#define KNOWN_FAILURE_ON_BIONIC(x) x
53#endif
54
Elliott Hughes966d8a32017-08-29 11:29:28 -070055// bionic's dlsym doesn't work in static binaries, so we can't access icu,
56// so any unicode test case will fail.
57static inline bool have_dl() {
58 return (dlopen("libc.so", 0) != nullptr);
59}
60
Evgenii Stepanovacd6f4f2018-11-06 16:48:27 -080061extern "C" void __hwasan_init() __attribute__((weak));
62
63static inline bool running_with_hwasan() {
64 return &__hwasan_init != 0;
65}
66
Elliott Hughesbcaa4542019-03-08 15:20:23 -080067#define SKIP_WITH_HWASAN if (running_with_hwasan()) GTEST_SKIP()
Evgenii Stepanovacd6f4f2018-11-06 16:48:27 -080068
Evgenii Stepanov7cc67062019-02-05 18:43:34 -080069static inline void* untag_address(void* addr) {
70#if defined(__LP64__)
71 if (running_with_hwasan()) {
72 constexpr uintptr_t mask = (static_cast<uintptr_t>(1) << 56) - 1;
73 addr = reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(addr) & mask);
74 }
75#endif
76 return addr;
77}
78
Elliott Hughesffbb0f82016-10-10 18:34:27 -070079#if defined(__linux__)
80
Elliott Hughes3fa758f2017-05-17 17:36:08 -070081#include <sys/sysmacros.h>
82
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -070083struct map_record {
84 uintptr_t addr_start;
85 uintptr_t addr_end;
86
87 int perms;
88
89 size_t offset;
90
91 dev_t device;
92 ino_t inode;
93
94 std::string pathname;
95};
96
97class Maps {
98 public:
99 static bool parse_maps(std::vector<map_record>* maps) {
Elliott Hughesa8384902017-10-03 10:18:58 -0700100 maps->clear();
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700101
Elliott Hughesa8384902017-10-03 10:18:58 -0700102 std::unique_ptr<FILE, decltype(&fclose)> fp(fopen("/proc/self/maps", "re"), fclose);
103 if (!fp) return false;
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700104
105 char line[BUFSIZ];
Elliott Hughesa8384902017-10-03 10:18:58 -0700106 while (fgets(line, sizeof(line), fp.get()) != nullptr) {
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700107 map_record record;
Dmitriy Ivanov1dce3ed2015-04-06 19:05:58 -0700108 uint32_t dev_major, dev_minor;
Elliott Hughes15dfd632015-09-22 16:40:14 -0700109 int path_offset;
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700110 char prot[5]; // sizeof("rwxp")
Elliott Hughes15dfd632015-09-22 16:40:14 -0700111 if (sscanf(line, "%" SCNxPTR "-%" SCNxPTR " %4s %" SCNxPTR " %x:%x %lu %n",
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700112 &record.addr_start, &record.addr_end, prot, &record.offset,
Elliott Hughes15dfd632015-09-22 16:40:14 -0700113 &dev_major, &dev_minor, &record.inode, &path_offset) == 7) {
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700114 record.perms = 0;
115 if (prot[0] == 'r') {
116 record.perms |= PROT_READ;
117 }
118 if (prot[1] == 'w') {
119 record.perms |= PROT_WRITE;
120 }
121 if (prot[2] == 'x') {
122 record.perms |= PROT_EXEC;
123 }
124
125 // TODO: parse shared/private?
126
127 record.device = makedev(dev_major, dev_minor);
Elliott Hughes15dfd632015-09-22 16:40:14 -0700128 record.pathname = line + path_offset;
129 if (!record.pathname.empty() && record.pathname.back() == '\n') {
130 record.pathname.pop_back();
131 }
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700132 maps->push_back(record);
133 }
134 }
135
136 return true;
137 }
138};
139
Yabin Cui76144aa2015-11-19 13:52:16 -0800140extern "C" pid_t gettid();
141
Elliott Hughesffbb0f82016-10-10 18:34:27 -0700142#endif
143
Yabin Cui76144aa2015-11-19 13:52:16 -0800144static inline void WaitUntilThreadSleep(std::atomic<pid_t>& tid) {
145 while (tid == 0) {
146 usleep(1000);
147 }
148 std::string filename = android::base::StringPrintf("/proc/%d/stat", tid.load());
149 std::regex regex {R"(\s+S\s+)"};
150
151 while (true) {
152 std::string content;
153 ASSERT_TRUE(android::base::ReadFileToString(filename, &content));
154 if (std::regex_search(content, regex)) {
155 break;
156 }
157 usleep(1000);
158 }
159}
160
Christopher Ferrisd6a1dc22018-02-07 18:42:14 -0800161static inline void AssertChildExited(int pid, int expected_exit_status,
162 const std::string* error_msg = nullptr) {
Elliott Hughes33697a02016-01-26 13:04:57 -0800163 int status;
Christopher Ferrisd6a1dc22018-02-07 18:42:14 -0800164 std::string error;
165 if (error_msg == nullptr) {
166 error_msg = &error;
167 }
168 ASSERT_EQ(pid, TEMP_FAILURE_RETRY(waitpid(pid, &status, 0))) << *error_msg;
Evgenii Stepanov68ecec12017-01-31 13:19:30 -0800169 if (expected_exit_status >= 0) {
Christopher Ferrisd6a1dc22018-02-07 18:42:14 -0800170 ASSERT_TRUE(WIFEXITED(status)) << *error_msg;
171 ASSERT_EQ(expected_exit_status, WEXITSTATUS(status)) << *error_msg;
Evgenii Stepanov68ecec12017-01-31 13:19:30 -0800172 } else {
Christopher Ferrisd6a1dc22018-02-07 18:42:14 -0800173 ASSERT_TRUE(WIFSIGNALED(status)) << *error_msg;
174 ASSERT_EQ(-expected_exit_status, WTERMSIG(status)) << *error_msg;
Evgenii Stepanov68ecec12017-01-31 13:19:30 -0800175 }
Elliott Hughes33697a02016-01-26 13:04:57 -0800176}
177
Elliott Hughesa7f12942017-12-15 13:55:53 -0800178static inline void AssertCloseOnExec(int fd, bool close_on_exec) {
179 int flags = fcntl(fd, F_GETFD);
180 ASSERT_NE(flags, -1);
181 ASSERT_EQ(close_on_exec ? FD_CLOEXEC : 0, flags & FD_CLOEXEC);
182}
183
Dimitry Ivanov2ba1cf32016-05-17 13:29:37 -0700184// The absolute path to the executable
185const std::string& get_executable_path();
Dimitry Ivanovd17a3772016-03-01 13:11:28 -0800186
Dimitry Ivanov55437462016-07-20 15:33:07 -0700187// Access to argc/argv/envp
188int get_argc();
189char** get_argv();
190char** get_envp();
191
Evgenii Stepanov68ecec12017-01-31 13:19:30 -0800192// ExecTestHelper is only used in bionic and glibc tests.
193#ifndef __APPLE__
194class ExecTestHelper {
195 public:
196 char** GetArgs() {
197 return const_cast<char**>(args_.data());
198 }
Elliott Hughes14e3ff92017-10-06 16:58:36 -0700199 const char* GetArg0() {
200 return args_[0];
201 }
Evgenii Stepanov68ecec12017-01-31 13:19:30 -0800202 char** GetEnv() {
203 return const_cast<char**>(env_.data());
204 }
Ryan Prichard8f639a42018-10-01 23:10:05 -0700205 const std::string& GetOutput() {
206 return output_;
207 }
Evgenii Stepanov68ecec12017-01-31 13:19:30 -0800208
Elliott Hughes5cec3772018-01-19 15:45:23 -0800209 void SetArgs(const std::vector<const char*>& args) {
Evgenii Stepanov68ecec12017-01-31 13:19:30 -0800210 args_ = args;
211 }
Elliott Hughes5cec3772018-01-19 15:45:23 -0800212 void SetEnv(const std::vector<const char*>& env) {
Evgenii Stepanov68ecec12017-01-31 13:19:30 -0800213 env_ = env;
214 }
215
216 void Run(const std::function<void()>& child_fn, int expected_exit_status,
217 const char* expected_output) {
218 int fds[2];
219 ASSERT_NE(pipe(fds), -1);
220
221 pid_t pid = fork();
222 ASSERT_NE(pid, -1);
223
224 if (pid == 0) {
225 // Child.
226 close(fds[0]);
227 dup2(fds[1], STDOUT_FILENO);
228 dup2(fds[1], STDERR_FILENO);
229 if (fds[1] != STDOUT_FILENO && fds[1] != STDERR_FILENO) close(fds[1]);
230 child_fn();
231 FAIL();
232 }
233
234 // Parent.
235 close(fds[1]);
Ryan Prichard8f639a42018-10-01 23:10:05 -0700236 output_.clear();
Evgenii Stepanov68ecec12017-01-31 13:19:30 -0800237 char buf[BUFSIZ];
238 ssize_t bytes_read;
239 while ((bytes_read = TEMP_FAILURE_RETRY(read(fds[0], buf, sizeof(buf)))) > 0) {
Ryan Prichard8f639a42018-10-01 23:10:05 -0700240 output_.append(buf, bytes_read);
Evgenii Stepanov68ecec12017-01-31 13:19:30 -0800241 }
242 close(fds[0]);
243
Ryan Prichard8f639a42018-10-01 23:10:05 -0700244 std::string error_msg("Test output:\n" + output_);
Christopher Ferrisd6a1dc22018-02-07 18:42:14 -0800245 AssertChildExited(pid, expected_exit_status, &error_msg);
Evgenii Stepanov68ecec12017-01-31 13:19:30 -0800246 if (expected_output != nullptr) {
Ryan Prichard8f639a42018-10-01 23:10:05 -0700247 ASSERT_EQ(expected_output, output_);
Evgenii Stepanov68ecec12017-01-31 13:19:30 -0800248 }
249 }
250
251 private:
252 std::vector<const char*> args_;
253 std::vector<const char*> env_;
Ryan Prichard8f639a42018-10-01 23:10:05 -0700254 std::string output_;
Evgenii Stepanov68ecec12017-01-31 13:19:30 -0800255};
256#endif
257
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700258#endif