blob: ba006f12b57aa21db02650b4869465dbff194d4a [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>
Tom Cherryb8ab6182017-04-05 16:20:29 -070032#include <android-base/scopeguard.h>
Elliott Hughes939a7e02015-12-04 15:27:46 -080033#include <android-base/stringprintf.h>
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -070034
Dimitry Ivanov35c8e3b2017-02-27 12:17:47 -080035#if defined(__LP64__)
36#define PATH_TO_SYSTEM_LIB "/system/lib64/"
37#else
38#define PATH_TO_SYSTEM_LIB "/system/lib/"
39#endif
40
Elliott Hughes14e3ff92017-10-06 16:58:36 -070041#if defined(__GLIBC__)
42#define BIN_DIR "/bin/"
43#else
44#define BIN_DIR "/system/bin/"
45#endif
46
Josh Gao2f06e102017-01-10 13:00:37 -080047#if defined(__BIONIC__)
48#define KNOWN_FAILURE_ON_BIONIC(x) xfail_ ## x
49#else
50#define KNOWN_FAILURE_ON_BIONIC(x) x
51#endif
52
Elliott Hughes966d8a32017-08-29 11:29:28 -070053// bionic's dlsym doesn't work in static binaries, so we can't access icu,
54// so any unicode test case will fail.
55static inline bool have_dl() {
56 return (dlopen("libc.so", 0) != nullptr);
57}
58
Elliott Hughesffbb0f82016-10-10 18:34:27 -070059#if defined(__linux__)
60
Elliott Hughes3fa758f2017-05-17 17:36:08 -070061#include <sys/sysmacros.h>
62
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -070063struct map_record {
64 uintptr_t addr_start;
65 uintptr_t addr_end;
66
67 int perms;
68
69 size_t offset;
70
71 dev_t device;
72 ino_t inode;
73
74 std::string pathname;
75};
76
77class Maps {
78 public:
79 static bool parse_maps(std::vector<map_record>* maps) {
Elliott Hughesa8384902017-10-03 10:18:58 -070080 maps->clear();
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -070081
Elliott Hughesa8384902017-10-03 10:18:58 -070082 std::unique_ptr<FILE, decltype(&fclose)> fp(fopen("/proc/self/maps", "re"), fclose);
83 if (!fp) return false;
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -070084
85 char line[BUFSIZ];
Elliott Hughesa8384902017-10-03 10:18:58 -070086 while (fgets(line, sizeof(line), fp.get()) != nullptr) {
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -070087 map_record record;
Dmitriy Ivanov1dce3ed2015-04-06 19:05:58 -070088 uint32_t dev_major, dev_minor;
Elliott Hughes15dfd632015-09-22 16:40:14 -070089 int path_offset;
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -070090 char prot[5]; // sizeof("rwxp")
Elliott Hughes15dfd632015-09-22 16:40:14 -070091 if (sscanf(line, "%" SCNxPTR "-%" SCNxPTR " %4s %" SCNxPTR " %x:%x %lu %n",
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -070092 &record.addr_start, &record.addr_end, prot, &record.offset,
Elliott Hughes15dfd632015-09-22 16:40:14 -070093 &dev_major, &dev_minor, &record.inode, &path_offset) == 7) {
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -070094 record.perms = 0;
95 if (prot[0] == 'r') {
96 record.perms |= PROT_READ;
97 }
98 if (prot[1] == 'w') {
99 record.perms |= PROT_WRITE;
100 }
101 if (prot[2] == 'x') {
102 record.perms |= PROT_EXEC;
103 }
104
105 // TODO: parse shared/private?
106
107 record.device = makedev(dev_major, dev_minor);
Elliott Hughes15dfd632015-09-22 16:40:14 -0700108 record.pathname = line + path_offset;
109 if (!record.pathname.empty() && record.pathname.back() == '\n') {
110 record.pathname.pop_back();
111 }
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700112 maps->push_back(record);
113 }
114 }
115
116 return true;
117 }
118};
119
Yabin Cui76144aa2015-11-19 13:52:16 -0800120extern "C" pid_t gettid();
121
Elliott Hughesffbb0f82016-10-10 18:34:27 -0700122#endif
123
Yabin Cui76144aa2015-11-19 13:52:16 -0800124static inline void WaitUntilThreadSleep(std::atomic<pid_t>& tid) {
125 while (tid == 0) {
126 usleep(1000);
127 }
128 std::string filename = android::base::StringPrintf("/proc/%d/stat", tid.load());
129 std::regex regex {R"(\s+S\s+)"};
130
131 while (true) {
132 std::string content;
133 ASSERT_TRUE(android::base::ReadFileToString(filename, &content));
134 if (std::regex_search(content, regex)) {
135 break;
136 }
137 usleep(1000);
138 }
139}
140
Elliott Hughes33697a02016-01-26 13:04:57 -0800141static inline void AssertChildExited(int pid, int expected_exit_status) {
142 int status;
143 ASSERT_EQ(pid, waitpid(pid, &status, 0));
Evgenii Stepanov68ecec12017-01-31 13:19:30 -0800144 if (expected_exit_status >= 0) {
145 ASSERT_TRUE(WIFEXITED(status));
146 ASSERT_EQ(expected_exit_status, WEXITSTATUS(status));
147 } else {
148 ASSERT_TRUE(WIFSIGNALED(status));
149 ASSERT_EQ(-expected_exit_status, WTERMSIG(status));
150 }
Elliott Hughes33697a02016-01-26 13:04:57 -0800151}
152
Dimitry Ivanov2ba1cf32016-05-17 13:29:37 -0700153// The absolute path to the executable
154const std::string& get_executable_path();
Dimitry Ivanovd17a3772016-03-01 13:11:28 -0800155
Dimitry Ivanov55437462016-07-20 15:33:07 -0700156// Access to argc/argv/envp
157int get_argc();
158char** get_argv();
159char** get_envp();
160
Evgenii Stepanov68ecec12017-01-31 13:19:30 -0800161// ExecTestHelper is only used in bionic and glibc tests.
162#ifndef __APPLE__
163class ExecTestHelper {
164 public:
165 char** GetArgs() {
166 return const_cast<char**>(args_.data());
167 }
Elliott Hughes14e3ff92017-10-06 16:58:36 -0700168 const char* GetArg0() {
169 return args_[0];
170 }
Evgenii Stepanov68ecec12017-01-31 13:19:30 -0800171 char** GetEnv() {
172 return const_cast<char**>(env_.data());
173 }
174
175 void SetArgs(const std::vector<const char*> args) {
176 args_ = args;
177 }
178 void SetEnv(const std::vector<const char*> env) {
179 env_ = env;
180 }
181
182 void Run(const std::function<void()>& child_fn, int expected_exit_status,
183 const char* expected_output) {
184 int fds[2];
185 ASSERT_NE(pipe(fds), -1);
186
187 pid_t pid = fork();
188 ASSERT_NE(pid, -1);
189
190 if (pid == 0) {
191 // Child.
192 close(fds[0]);
193 dup2(fds[1], STDOUT_FILENO);
194 dup2(fds[1], STDERR_FILENO);
195 if (fds[1] != STDOUT_FILENO && fds[1] != STDERR_FILENO) close(fds[1]);
196 child_fn();
197 FAIL();
198 }
199
200 // Parent.
201 close(fds[1]);
202 std::string output;
203 char buf[BUFSIZ];
204 ssize_t bytes_read;
205 while ((bytes_read = TEMP_FAILURE_RETRY(read(fds[0], buf, sizeof(buf)))) > 0) {
206 output.append(buf, bytes_read);
207 }
208 close(fds[0]);
209
210 AssertChildExited(pid, expected_exit_status);
211 if (expected_output != nullptr) {
212 ASSERT_EQ(expected_output, output);
213 }
214 }
215
216 private:
217 std::vector<const char*> args_;
218 std::vector<const char*> env_;
219};
220#endif
221
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700222#endif