blob: e3e0b0dc93abaa2c5e29e55acd1458512b9986c5 [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
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -070020#include <inttypes.h>
21#include <sys/mman.h>
Elliott Hughes5d949da2017-05-17 11:43:39 -070022#include <sys/sysmacros.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 Hughesffbb0f82016-10-10 18:34:27 -070047#if defined(__linux__)
48
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -070049struct map_record {
50 uintptr_t addr_start;
51 uintptr_t addr_end;
52
53 int perms;
54
55 size_t offset;
56
57 dev_t device;
58 ino_t inode;
59
60 std::string pathname;
61};
62
63class Maps {
64 public:
65 static bool parse_maps(std::vector<map_record>* maps) {
Elliott Hughes15dfd632015-09-22 16:40:14 -070066 FILE* fp = fopen("/proc/self/maps", "re");
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -070067 if (fp == nullptr) {
68 return false;
69 }
70
Tom Cherryb8ab6182017-04-05 16:20:29 -070071 auto fp_guard = android::base::make_scope_guard([&]() { fclose(fp); });
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -070072
73 char line[BUFSIZ];
74 while (fgets(line, sizeof(line), fp) != nullptr) {
75 map_record record;
Dmitriy Ivanov1dce3ed2015-04-06 19:05:58 -070076 uint32_t dev_major, dev_minor;
Elliott Hughes15dfd632015-09-22 16:40:14 -070077 int path_offset;
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -070078 char prot[5]; // sizeof("rwxp")
Elliott Hughes15dfd632015-09-22 16:40:14 -070079 if (sscanf(line, "%" SCNxPTR "-%" SCNxPTR " %4s %" SCNxPTR " %x:%x %lu %n",
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -070080 &record.addr_start, &record.addr_end, prot, &record.offset,
Elliott Hughes15dfd632015-09-22 16:40:14 -070081 &dev_major, &dev_minor, &record.inode, &path_offset) == 7) {
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -070082 record.perms = 0;
83 if (prot[0] == 'r') {
84 record.perms |= PROT_READ;
85 }
86 if (prot[1] == 'w') {
87 record.perms |= PROT_WRITE;
88 }
89 if (prot[2] == 'x') {
90 record.perms |= PROT_EXEC;
91 }
92
93 // TODO: parse shared/private?
94
95 record.device = makedev(dev_major, dev_minor);
Elliott Hughes15dfd632015-09-22 16:40:14 -070096 record.pathname = line + path_offset;
97 if (!record.pathname.empty() && record.pathname.back() == '\n') {
98 record.pathname.pop_back();
99 }
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700100 maps->push_back(record);
101 }
102 }
103
104 return true;
105 }
106};
107
Yabin Cui76144aa2015-11-19 13:52:16 -0800108extern "C" pid_t gettid();
109
Elliott Hughesffbb0f82016-10-10 18:34:27 -0700110#endif
111
Yabin Cui76144aa2015-11-19 13:52:16 -0800112static inline void WaitUntilThreadSleep(std::atomic<pid_t>& tid) {
113 while (tid == 0) {
114 usleep(1000);
115 }
116 std::string filename = android::base::StringPrintf("/proc/%d/stat", tid.load());
117 std::regex regex {R"(\s+S\s+)"};
118
119 while (true) {
120 std::string content;
121 ASSERT_TRUE(android::base::ReadFileToString(filename, &content));
122 if (std::regex_search(content, regex)) {
123 break;
124 }
125 usleep(1000);
126 }
127}
128
Elliott Hughes33697a02016-01-26 13:04:57 -0800129static inline void AssertChildExited(int pid, int expected_exit_status) {
130 int status;
131 ASSERT_EQ(pid, waitpid(pid, &status, 0));
Evgenii Stepanov68ecec12017-01-31 13:19:30 -0800132 if (expected_exit_status >= 0) {
133 ASSERT_TRUE(WIFEXITED(status));
134 ASSERT_EQ(expected_exit_status, WEXITSTATUS(status));
135 } else {
136 ASSERT_TRUE(WIFSIGNALED(status));
137 ASSERT_EQ(-expected_exit_status, WTERMSIG(status));
138 }
Elliott Hughes33697a02016-01-26 13:04:57 -0800139}
140
Dimitry Ivanov2ba1cf32016-05-17 13:29:37 -0700141// The absolute path to the executable
142const std::string& get_executable_path();
Dimitry Ivanovd17a3772016-03-01 13:11:28 -0800143
Dimitry Ivanov55437462016-07-20 15:33:07 -0700144// Access to argc/argv/envp
145int get_argc();
146char** get_argv();
147char** get_envp();
148
Evgenii Stepanov68ecec12017-01-31 13:19:30 -0800149// ExecTestHelper is only used in bionic and glibc tests.
150#ifndef __APPLE__
151class ExecTestHelper {
152 public:
153 char** GetArgs() {
154 return const_cast<char**>(args_.data());
155 }
156 char** GetEnv() {
157 return const_cast<char**>(env_.data());
158 }
159
160 void SetArgs(const std::vector<const char*> args) {
161 args_ = args;
162 }
163 void SetEnv(const std::vector<const char*> env) {
164 env_ = env;
165 }
166
167 void Run(const std::function<void()>& child_fn, int expected_exit_status,
168 const char* expected_output) {
169 int fds[2];
170 ASSERT_NE(pipe(fds), -1);
171
172 pid_t pid = fork();
173 ASSERT_NE(pid, -1);
174
175 if (pid == 0) {
176 // Child.
177 close(fds[0]);
178 dup2(fds[1], STDOUT_FILENO);
179 dup2(fds[1], STDERR_FILENO);
180 if (fds[1] != STDOUT_FILENO && fds[1] != STDERR_FILENO) close(fds[1]);
181 child_fn();
182 FAIL();
183 }
184
185 // Parent.
186 close(fds[1]);
187 std::string output;
188 char buf[BUFSIZ];
189 ssize_t bytes_read;
190 while ((bytes_read = TEMP_FAILURE_RETRY(read(fds[0], buf, sizeof(buf)))) > 0) {
191 output.append(buf, bytes_read);
192 }
193 close(fds[0]);
194
195 AssertChildExited(pid, expected_exit_status);
196 if (expected_output != nullptr) {
197 ASSERT_EQ(expected_output, output);
198 }
199 }
200
201 private:
202 std::vector<const char*> args_;
203 std::vector<const char*> env_;
204};
205#endif
206
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700207#endif