blob: 1f4cece19311ab8254c6198dfd1656f337293487 [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
Elliott Hughes966d8a32017-08-29 11:29:28 -070019#include <dlfcn.h>
Elliott Hughesa7f12942017-12-15 13:55:53 -080020#include <fcntl.h>
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -070021#include <inttypes.h>
22#include <sys/mman.h>
Elliott Hughes33697a02016-01-26 13:04:57 -080023#include <sys/types.h>
24#include <sys/wait.h>
Yabin Cui76144aa2015-11-19 13:52:16 -080025#include <unistd.h>
26
27#include <atomic>
28#include <string>
29#include <regex>
30
Elliott Hughes939a7e02015-12-04 15:27:46 -080031#include <android-base/file.h>
Elliott Hughes4af70b42017-11-28 18:02:16 -080032#include <android-base/macros.h>
Tom Cherryb8ab6182017-04-05 16:20:29 -070033#include <android-base/scopeguard.h>
Elliott Hughes939a7e02015-12-04 15:27:46 -080034#include <android-base/stringprintf.h>
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -070035
Dimitry Ivanov35c8e3b2017-02-27 12:17:47 -080036#if defined(__LP64__)
37#define PATH_TO_SYSTEM_LIB "/system/lib64/"
38#else
39#define PATH_TO_SYSTEM_LIB "/system/lib/"
40#endif
41
Elliott Hughes14e3ff92017-10-06 16:58:36 -070042#if defined(__GLIBC__)
43#define BIN_DIR "/bin/"
44#else
45#define BIN_DIR "/system/bin/"
46#endif
47
Josh Gao2f06e102017-01-10 13:00:37 -080048#if defined(__BIONIC__)
49#define KNOWN_FAILURE_ON_BIONIC(x) xfail_ ## x
50#else
51#define KNOWN_FAILURE_ON_BIONIC(x) x
52#endif
53
Elliott Hughes966d8a32017-08-29 11:29:28 -070054// bionic's dlsym doesn't work in static binaries, so we can't access icu,
55// so any unicode test case will fail.
56static inline bool have_dl() {
57 return (dlopen("libc.so", 0) != nullptr);
58}
59
Evgenii Stepanovacd6f4f2018-11-06 16:48:27 -080060extern "C" void __hwasan_init() __attribute__((weak));
61
62static inline bool running_with_hwasan() {
63 return &__hwasan_init != 0;
64}
65
Elliott Hughesbcaa4542019-03-08 15:20:23 -080066#define SKIP_WITH_HWASAN if (running_with_hwasan()) GTEST_SKIP()
Evgenii Stepanovacd6f4f2018-11-06 16:48:27 -080067
Evgenii Stepanov7cc67062019-02-05 18:43:34 -080068static inline void* untag_address(void* addr) {
69#if defined(__LP64__)
70 if (running_with_hwasan()) {
71 constexpr uintptr_t mask = (static_cast<uintptr_t>(1) << 56) - 1;
72 addr = reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(addr) & mask);
73 }
74#endif
75 return addr;
76}
77
Elliott Hughesffbb0f82016-10-10 18:34:27 -070078#if defined(__linux__)
79
Elliott Hughes3fa758f2017-05-17 17:36:08 -070080#include <sys/sysmacros.h>
81
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -070082struct map_record {
83 uintptr_t addr_start;
84 uintptr_t addr_end;
85
86 int perms;
87
88 size_t offset;
89
90 dev_t device;
91 ino_t inode;
92
93 std::string pathname;
94};
95
96class Maps {
97 public:
98 static bool parse_maps(std::vector<map_record>* maps) {
Elliott Hughesa8384902017-10-03 10:18:58 -070099 maps->clear();
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700100
Elliott Hughesa8384902017-10-03 10:18:58 -0700101 std::unique_ptr<FILE, decltype(&fclose)> fp(fopen("/proc/self/maps", "re"), fclose);
102 if (!fp) return false;
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700103
104 char line[BUFSIZ];
Elliott Hughesa8384902017-10-03 10:18:58 -0700105 while (fgets(line, sizeof(line), fp.get()) != nullptr) {
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700106 map_record record;
Dmitriy Ivanov1dce3ed2015-04-06 19:05:58 -0700107 uint32_t dev_major, dev_minor;
Elliott Hughes15dfd632015-09-22 16:40:14 -0700108 int path_offset;
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700109 char prot[5]; // sizeof("rwxp")
Elliott Hughes15dfd632015-09-22 16:40:14 -0700110 if (sscanf(line, "%" SCNxPTR "-%" SCNxPTR " %4s %" SCNxPTR " %x:%x %lu %n",
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700111 &record.addr_start, &record.addr_end, prot, &record.offset,
Elliott Hughes15dfd632015-09-22 16:40:14 -0700112 &dev_major, &dev_minor, &record.inode, &path_offset) == 7) {
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700113 record.perms = 0;
114 if (prot[0] == 'r') {
115 record.perms |= PROT_READ;
116 }
117 if (prot[1] == 'w') {
118 record.perms |= PROT_WRITE;
119 }
120 if (prot[2] == 'x') {
121 record.perms |= PROT_EXEC;
122 }
123
124 // TODO: parse shared/private?
125
126 record.device = makedev(dev_major, dev_minor);
Elliott Hughes15dfd632015-09-22 16:40:14 -0700127 record.pathname = line + path_offset;
128 if (!record.pathname.empty() && record.pathname.back() == '\n') {
129 record.pathname.pop_back();
130 }
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700131 maps->push_back(record);
132 }
133 }
134
135 return true;
136 }
137};
138
Yabin Cui76144aa2015-11-19 13:52:16 -0800139extern "C" pid_t gettid();
140
Elliott Hughesffbb0f82016-10-10 18:34:27 -0700141#endif
142
Yabin Cui76144aa2015-11-19 13:52:16 -0800143static inline void WaitUntilThreadSleep(std::atomic<pid_t>& tid) {
144 while (tid == 0) {
145 usleep(1000);
146 }
147 std::string filename = android::base::StringPrintf("/proc/%d/stat", tid.load());
148 std::regex regex {R"(\s+S\s+)"};
149
150 while (true) {
151 std::string content;
152 ASSERT_TRUE(android::base::ReadFileToString(filename, &content));
153 if (std::regex_search(content, regex)) {
154 break;
155 }
156 usleep(1000);
157 }
158}
159
Christopher Ferrisd6a1dc22018-02-07 18:42:14 -0800160static inline void AssertChildExited(int pid, int expected_exit_status,
161 const std::string* error_msg = nullptr) {
Elliott Hughes33697a02016-01-26 13:04:57 -0800162 int status;
Christopher Ferrisd6a1dc22018-02-07 18:42:14 -0800163 std::string error;
164 if (error_msg == nullptr) {
165 error_msg = &error;
166 }
167 ASSERT_EQ(pid, TEMP_FAILURE_RETRY(waitpid(pid, &status, 0))) << *error_msg;
Evgenii Stepanov68ecec12017-01-31 13:19:30 -0800168 if (expected_exit_status >= 0) {
Christopher Ferrisd6a1dc22018-02-07 18:42:14 -0800169 ASSERT_TRUE(WIFEXITED(status)) << *error_msg;
170 ASSERT_EQ(expected_exit_status, WEXITSTATUS(status)) << *error_msg;
Evgenii Stepanov68ecec12017-01-31 13:19:30 -0800171 } else {
Christopher Ferrisd6a1dc22018-02-07 18:42:14 -0800172 ASSERT_TRUE(WIFSIGNALED(status)) << *error_msg;
173 ASSERT_EQ(-expected_exit_status, WTERMSIG(status)) << *error_msg;
Evgenii Stepanov68ecec12017-01-31 13:19:30 -0800174 }
Elliott Hughes33697a02016-01-26 13:04:57 -0800175}
176
Elliott Hughesa7f12942017-12-15 13:55:53 -0800177static inline void AssertCloseOnExec(int fd, bool close_on_exec) {
178 int flags = fcntl(fd, F_GETFD);
179 ASSERT_NE(flags, -1);
180 ASSERT_EQ(close_on_exec ? FD_CLOEXEC : 0, flags & FD_CLOEXEC);
181}
182
Dimitry Ivanov2ba1cf32016-05-17 13:29:37 -0700183// The absolute path to the executable
184const std::string& get_executable_path();
Dimitry Ivanovd17a3772016-03-01 13:11:28 -0800185
Dimitry Ivanov55437462016-07-20 15:33:07 -0700186// Access to argc/argv/envp
187int get_argc();
188char** get_argv();
189char** get_envp();
190
Evgenii Stepanov68ecec12017-01-31 13:19:30 -0800191// ExecTestHelper is only used in bionic and glibc tests.
192#ifndef __APPLE__
193class ExecTestHelper {
194 public:
195 char** GetArgs() {
196 return const_cast<char**>(args_.data());
197 }
Elliott Hughes14e3ff92017-10-06 16:58:36 -0700198 const char* GetArg0() {
199 return args_[0];
200 }
Evgenii Stepanov68ecec12017-01-31 13:19:30 -0800201 char** GetEnv() {
202 return const_cast<char**>(env_.data());
203 }
Ryan Prichard8f639a42018-10-01 23:10:05 -0700204 const std::string& GetOutput() {
205 return output_;
206 }
Evgenii Stepanov68ecec12017-01-31 13:19:30 -0800207
Elliott Hughes5cec3772018-01-19 15:45:23 -0800208 void SetArgs(const std::vector<const char*>& args) {
Evgenii Stepanov68ecec12017-01-31 13:19:30 -0800209 args_ = args;
210 }
Elliott Hughes5cec3772018-01-19 15:45:23 -0800211 void SetEnv(const std::vector<const char*>& env) {
Evgenii Stepanov68ecec12017-01-31 13:19:30 -0800212 env_ = env;
213 }
214
215 void Run(const std::function<void()>& child_fn, int expected_exit_status,
216 const char* expected_output) {
217 int fds[2];
218 ASSERT_NE(pipe(fds), -1);
219
220 pid_t pid = fork();
221 ASSERT_NE(pid, -1);
222
223 if (pid == 0) {
224 // Child.
225 close(fds[0]);
226 dup2(fds[1], STDOUT_FILENO);
227 dup2(fds[1], STDERR_FILENO);
228 if (fds[1] != STDOUT_FILENO && fds[1] != STDERR_FILENO) close(fds[1]);
229 child_fn();
230 FAIL();
231 }
232
233 // Parent.
234 close(fds[1]);
Ryan Prichard8f639a42018-10-01 23:10:05 -0700235 output_.clear();
Evgenii Stepanov68ecec12017-01-31 13:19:30 -0800236 char buf[BUFSIZ];
237 ssize_t bytes_read;
238 while ((bytes_read = TEMP_FAILURE_RETRY(read(fds[0], buf, sizeof(buf)))) > 0) {
Ryan Prichard8f639a42018-10-01 23:10:05 -0700239 output_.append(buf, bytes_read);
Evgenii Stepanov68ecec12017-01-31 13:19:30 -0800240 }
241 close(fds[0]);
242
Ryan Prichard8f639a42018-10-01 23:10:05 -0700243 std::string error_msg("Test output:\n" + output_);
Christopher Ferrisd6a1dc22018-02-07 18:42:14 -0800244 AssertChildExited(pid, expected_exit_status, &error_msg);
Evgenii Stepanov68ecec12017-01-31 13:19:30 -0800245 if (expected_output != nullptr) {
Ryan Prichard8f639a42018-10-01 23:10:05 -0700246 ASSERT_EQ(expected_output, output_);
Evgenii Stepanov68ecec12017-01-31 13:19:30 -0800247 }
248 }
249
250 private:
251 std::vector<const char*> args_;
252 std::vector<const char*> env_;
Ryan Prichard8f639a42018-10-01 23:10:05 -0700253 std::string output_;
Evgenii Stepanov68ecec12017-01-31 13:19:30 -0800254};
255#endif