blob: daf382e3e4a6a83c6a6b7fdfef54453900cd608a [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
Josh Gao2f06e102017-01-10 13:00:37 -080041#if defined(__BIONIC__)
42#define KNOWN_FAILURE_ON_BIONIC(x) xfail_ ## x
43#else
44#define KNOWN_FAILURE_ON_BIONIC(x) x
45#endif
46
Elliott Hughes966d8a32017-08-29 11:29:28 -070047// bionic's dlsym doesn't work in static binaries, so we can't access icu,
48// so any unicode test case will fail.
49static inline bool have_dl() {
50 return (dlopen("libc.so", 0) != nullptr);
51}
52
Elliott Hughesffbb0f82016-10-10 18:34:27 -070053#if defined(__linux__)
54
Elliott Hughes3fa758f2017-05-17 17:36:08 -070055#include <sys/sysmacros.h>
56
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -070057struct map_record {
58 uintptr_t addr_start;
59 uintptr_t addr_end;
60
61 int perms;
62
63 size_t offset;
64
65 dev_t device;
66 ino_t inode;
67
68 std::string pathname;
69};
70
71class Maps {
72 public:
73 static bool parse_maps(std::vector<map_record>* maps) {
Elliott Hughesa8384902017-10-03 10:18:58 -070074 maps->clear();
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -070075
Elliott Hughesa8384902017-10-03 10:18:58 -070076 std::unique_ptr<FILE, decltype(&fclose)> fp(fopen("/proc/self/maps", "re"), fclose);
77 if (!fp) return false;
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -070078
79 char line[BUFSIZ];
Elliott Hughesa8384902017-10-03 10:18:58 -070080 while (fgets(line, sizeof(line), fp.get()) != nullptr) {
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -070081 map_record record;
Dmitriy Ivanov1dce3ed2015-04-06 19:05:58 -070082 uint32_t dev_major, dev_minor;
Elliott Hughes15dfd632015-09-22 16:40:14 -070083 int path_offset;
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -070084 char prot[5]; // sizeof("rwxp")
Elliott Hughes15dfd632015-09-22 16:40:14 -070085 if (sscanf(line, "%" SCNxPTR "-%" SCNxPTR " %4s %" SCNxPTR " %x:%x %lu %n",
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -070086 &record.addr_start, &record.addr_end, prot, &record.offset,
Elliott Hughes15dfd632015-09-22 16:40:14 -070087 &dev_major, &dev_minor, &record.inode, &path_offset) == 7) {
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -070088 record.perms = 0;
89 if (prot[0] == 'r') {
90 record.perms |= PROT_READ;
91 }
92 if (prot[1] == 'w') {
93 record.perms |= PROT_WRITE;
94 }
95 if (prot[2] == 'x') {
96 record.perms |= PROT_EXEC;
97 }
98
99 // TODO: parse shared/private?
100
101 record.device = makedev(dev_major, dev_minor);
Elliott Hughes15dfd632015-09-22 16:40:14 -0700102 record.pathname = line + path_offset;
103 if (!record.pathname.empty() && record.pathname.back() == '\n') {
104 record.pathname.pop_back();
105 }
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700106 maps->push_back(record);
107 }
108 }
109
110 return true;
111 }
112};
113
Yabin Cui76144aa2015-11-19 13:52:16 -0800114extern "C" pid_t gettid();
115
Elliott Hughesffbb0f82016-10-10 18:34:27 -0700116#endif
117
Yabin Cui76144aa2015-11-19 13:52:16 -0800118static inline void WaitUntilThreadSleep(std::atomic<pid_t>& tid) {
119 while (tid == 0) {
120 usleep(1000);
121 }
122 std::string filename = android::base::StringPrintf("/proc/%d/stat", tid.load());
123 std::regex regex {R"(\s+S\s+)"};
124
125 while (true) {
126 std::string content;
127 ASSERT_TRUE(android::base::ReadFileToString(filename, &content));
128 if (std::regex_search(content, regex)) {
129 break;
130 }
131 usleep(1000);
132 }
133}
134
Elliott Hughes33697a02016-01-26 13:04:57 -0800135static inline void AssertChildExited(int pid, int expected_exit_status) {
136 int status;
137 ASSERT_EQ(pid, waitpid(pid, &status, 0));
Evgenii Stepanov68ecec12017-01-31 13:19:30 -0800138 if (expected_exit_status >= 0) {
139 ASSERT_TRUE(WIFEXITED(status));
140 ASSERT_EQ(expected_exit_status, WEXITSTATUS(status));
141 } else {
142 ASSERT_TRUE(WIFSIGNALED(status));
143 ASSERT_EQ(-expected_exit_status, WTERMSIG(status));
144 }
Elliott Hughes33697a02016-01-26 13:04:57 -0800145}
146
Dimitry Ivanov2ba1cf32016-05-17 13:29:37 -0700147// The absolute path to the executable
148const std::string& get_executable_path();
Dimitry Ivanovd17a3772016-03-01 13:11:28 -0800149
Dimitry Ivanov55437462016-07-20 15:33:07 -0700150// Access to argc/argv/envp
151int get_argc();
152char** get_argv();
153char** get_envp();
154
Evgenii Stepanov68ecec12017-01-31 13:19:30 -0800155// ExecTestHelper is only used in bionic and glibc tests.
156#ifndef __APPLE__
157class ExecTestHelper {
158 public:
159 char** GetArgs() {
160 return const_cast<char**>(args_.data());
161 }
162 char** GetEnv() {
163 return const_cast<char**>(env_.data());
164 }
165
166 void SetArgs(const std::vector<const char*> args) {
167 args_ = args;
168 }
169 void SetEnv(const std::vector<const char*> env) {
170 env_ = env;
171 }
172
173 void Run(const std::function<void()>& child_fn, int expected_exit_status,
174 const char* expected_output) {
175 int fds[2];
176 ASSERT_NE(pipe(fds), -1);
177
178 pid_t pid = fork();
179 ASSERT_NE(pid, -1);
180
181 if (pid == 0) {
182 // Child.
183 close(fds[0]);
184 dup2(fds[1], STDOUT_FILENO);
185 dup2(fds[1], STDERR_FILENO);
186 if (fds[1] != STDOUT_FILENO && fds[1] != STDERR_FILENO) close(fds[1]);
187 child_fn();
188 FAIL();
189 }
190
191 // Parent.
192 close(fds[1]);
193 std::string output;
194 char buf[BUFSIZ];
195 ssize_t bytes_read;
196 while ((bytes_read = TEMP_FAILURE_RETRY(read(fds[0], buf, sizeof(buf)))) > 0) {
197 output.append(buf, bytes_read);
198 }
199 close(fds[0]);
200
201 AssertChildExited(pid, expected_exit_status);
202 if (expected_output != nullptr) {
203 ASSERT_EQ(expected_output, output);
204 }
205 }
206
207 private:
208 std::vector<const char*> args_;
209 std::vector<const char*> env_;
210};
211#endif
212
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700213#endif