blob: 410b427487aacf99a8f8ccb8ab7b129241af035e [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>
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
Elliott Hughesffbb0f82016-10-10 18:34:27 -070060#if defined(__linux__)
61
Elliott Hughes3fa758f2017-05-17 17:36:08 -070062#include <sys/sysmacros.h>
63
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -070064struct map_record {
65 uintptr_t addr_start;
66 uintptr_t addr_end;
67
68 int perms;
69
70 size_t offset;
71
72 dev_t device;
73 ino_t inode;
74
75 std::string pathname;
76};
77
78class Maps {
79 public:
80 static bool parse_maps(std::vector<map_record>* maps) {
Elliott Hughesa8384902017-10-03 10:18:58 -070081 maps->clear();
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -070082
Elliott Hughesa8384902017-10-03 10:18:58 -070083 std::unique_ptr<FILE, decltype(&fclose)> fp(fopen("/proc/self/maps", "re"), fclose);
84 if (!fp) return false;
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -070085
86 char line[BUFSIZ];
Elliott Hughesa8384902017-10-03 10:18:58 -070087 while (fgets(line, sizeof(line), fp.get()) != nullptr) {
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -070088 map_record record;
Dmitriy Ivanov1dce3ed2015-04-06 19:05:58 -070089 uint32_t dev_major, dev_minor;
Elliott Hughes15dfd632015-09-22 16:40:14 -070090 int path_offset;
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -070091 char prot[5]; // sizeof("rwxp")
Elliott Hughes15dfd632015-09-22 16:40:14 -070092 if (sscanf(line, "%" SCNxPTR "-%" SCNxPTR " %4s %" SCNxPTR " %x:%x %lu %n",
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -070093 &record.addr_start, &record.addr_end, prot, &record.offset,
Elliott Hughes15dfd632015-09-22 16:40:14 -070094 &dev_major, &dev_minor, &record.inode, &path_offset) == 7) {
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -070095 record.perms = 0;
96 if (prot[0] == 'r') {
97 record.perms |= PROT_READ;
98 }
99 if (prot[1] == 'w') {
100 record.perms |= PROT_WRITE;
101 }
102 if (prot[2] == 'x') {
103 record.perms |= PROT_EXEC;
104 }
105
106 // TODO: parse shared/private?
107
108 record.device = makedev(dev_major, dev_minor);
Elliott Hughes15dfd632015-09-22 16:40:14 -0700109 record.pathname = line + path_offset;
110 if (!record.pathname.empty() && record.pathname.back() == '\n') {
111 record.pathname.pop_back();
112 }
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700113 maps->push_back(record);
114 }
115 }
116
117 return true;
118 }
119};
120
Yabin Cui76144aa2015-11-19 13:52:16 -0800121extern "C" pid_t gettid();
122
Elliott Hughesffbb0f82016-10-10 18:34:27 -0700123#endif
124
Yabin Cui76144aa2015-11-19 13:52:16 -0800125static inline void WaitUntilThreadSleep(std::atomic<pid_t>& tid) {
126 while (tid == 0) {
127 usleep(1000);
128 }
129 std::string filename = android::base::StringPrintf("/proc/%d/stat", tid.load());
130 std::regex regex {R"(\s+S\s+)"};
131
132 while (true) {
133 std::string content;
134 ASSERT_TRUE(android::base::ReadFileToString(filename, &content));
135 if (std::regex_search(content, regex)) {
136 break;
137 }
138 usleep(1000);
139 }
140}
141
Elliott Hughes33697a02016-01-26 13:04:57 -0800142static inline void AssertChildExited(int pid, int expected_exit_status) {
143 int status;
Elliott Hughescabc77f2017-11-28 12:55:19 -0800144 ASSERT_EQ(pid, TEMP_FAILURE_RETRY(waitpid(pid, &status, 0)));
Evgenii Stepanov68ecec12017-01-31 13:19:30 -0800145 if (expected_exit_status >= 0) {
146 ASSERT_TRUE(WIFEXITED(status));
147 ASSERT_EQ(expected_exit_status, WEXITSTATUS(status));
148 } else {
149 ASSERT_TRUE(WIFSIGNALED(status));
150 ASSERT_EQ(-expected_exit_status, WTERMSIG(status));
151 }
Elliott Hughes33697a02016-01-26 13:04:57 -0800152}
153
Dimitry Ivanov2ba1cf32016-05-17 13:29:37 -0700154// The absolute path to the executable
155const std::string& get_executable_path();
Dimitry Ivanovd17a3772016-03-01 13:11:28 -0800156
Dimitry Ivanov55437462016-07-20 15:33:07 -0700157// Access to argc/argv/envp
158int get_argc();
159char** get_argv();
160char** get_envp();
161
Evgenii Stepanov68ecec12017-01-31 13:19:30 -0800162// ExecTestHelper is only used in bionic and glibc tests.
163#ifndef __APPLE__
164class ExecTestHelper {
165 public:
166 char** GetArgs() {
167 return const_cast<char**>(args_.data());
168 }
Elliott Hughes14e3ff92017-10-06 16:58:36 -0700169 const char* GetArg0() {
170 return args_[0];
171 }
Evgenii Stepanov68ecec12017-01-31 13:19:30 -0800172 char** GetEnv() {
173 return const_cast<char**>(env_.data());
174 }
175
176 void SetArgs(const std::vector<const char*> args) {
177 args_ = args;
178 }
179 void SetEnv(const std::vector<const char*> env) {
180 env_ = env;
181 }
182
183 void Run(const std::function<void()>& child_fn, int expected_exit_status,
184 const char* expected_output) {
185 int fds[2];
186 ASSERT_NE(pipe(fds), -1);
187
188 pid_t pid = fork();
189 ASSERT_NE(pid, -1);
190
191 if (pid == 0) {
192 // Child.
193 close(fds[0]);
194 dup2(fds[1], STDOUT_FILENO);
195 dup2(fds[1], STDERR_FILENO);
196 if (fds[1] != STDOUT_FILENO && fds[1] != STDERR_FILENO) close(fds[1]);
197 child_fn();
198 FAIL();
199 }
200
201 // Parent.
202 close(fds[1]);
203 std::string output;
204 char buf[BUFSIZ];
205 ssize_t bytes_read;
206 while ((bytes_read = TEMP_FAILURE_RETRY(read(fds[0], buf, sizeof(buf)))) > 0) {
207 output.append(buf, bytes_read);
208 }
209 close(fds[0]);
210
211 AssertChildExited(pid, expected_exit_status);
212 if (expected_output != nullptr) {
213 ASSERT_EQ(expected_output, output);
214 }
215 }
216
217 private:
218 std::vector<const char*> args_;
219 std::vector<const char*> env_;
220};
221#endif
222
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700223#endif