blob: 2e9b99473879e586623e4413a65c6c9ba60a577c [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 Hughes33697a02016-01-26 13:04:57 -080022#include <sys/types.h>
23#include <sys/wait.h>
Yabin Cui76144aa2015-11-19 13:52:16 -080024#include <unistd.h>
25
26#include <atomic>
27#include <string>
28#include <regex>
29
Elliott Hughes939a7e02015-12-04 15:27:46 -080030#include <android-base/file.h>
Tom Cherryb8ab6182017-04-05 16:20:29 -070031#include <android-base/scopeguard.h>
Elliott Hughes939a7e02015-12-04 15:27:46 -080032#include <android-base/stringprintf.h>
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -070033
Dimitry Ivanov35c8e3b2017-02-27 12:17:47 -080034#if defined(__LP64__)
35#define PATH_TO_SYSTEM_LIB "/system/lib64/"
36#else
37#define PATH_TO_SYSTEM_LIB "/system/lib/"
38#endif
39
Josh Gao2f06e102017-01-10 13:00:37 -080040#if defined(__BIONIC__)
41#define KNOWN_FAILURE_ON_BIONIC(x) xfail_ ## x
42#else
43#define KNOWN_FAILURE_ON_BIONIC(x) x
44#endif
45
Elliott Hughesffbb0f82016-10-10 18:34:27 -070046#if defined(__linux__)
47
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -070048struct map_record {
49 uintptr_t addr_start;
50 uintptr_t addr_end;
51
52 int perms;
53
54 size_t offset;
55
56 dev_t device;
57 ino_t inode;
58
59 std::string pathname;
60};
61
62class Maps {
63 public:
64 static bool parse_maps(std::vector<map_record>* maps) {
Elliott Hughes15dfd632015-09-22 16:40:14 -070065 FILE* fp = fopen("/proc/self/maps", "re");
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -070066 if (fp == nullptr) {
67 return false;
68 }
69
Tom Cherryb8ab6182017-04-05 16:20:29 -070070 auto fp_guard = android::base::make_scope_guard([&]() { fclose(fp); });
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -070071
72 char line[BUFSIZ];
73 while (fgets(line, sizeof(line), fp) != nullptr) {
74 map_record record;
Dmitriy Ivanov1dce3ed2015-04-06 19:05:58 -070075 uint32_t dev_major, dev_minor;
Elliott Hughes15dfd632015-09-22 16:40:14 -070076 int path_offset;
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -070077 char prot[5]; // sizeof("rwxp")
Elliott Hughes15dfd632015-09-22 16:40:14 -070078 if (sscanf(line, "%" SCNxPTR "-%" SCNxPTR " %4s %" SCNxPTR " %x:%x %lu %n",
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -070079 &record.addr_start, &record.addr_end, prot, &record.offset,
Elliott Hughes15dfd632015-09-22 16:40:14 -070080 &dev_major, &dev_minor, &record.inode, &path_offset) == 7) {
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -070081 record.perms = 0;
82 if (prot[0] == 'r') {
83 record.perms |= PROT_READ;
84 }
85 if (prot[1] == 'w') {
86 record.perms |= PROT_WRITE;
87 }
88 if (prot[2] == 'x') {
89 record.perms |= PROT_EXEC;
90 }
91
92 // TODO: parse shared/private?
93
94 record.device = makedev(dev_major, dev_minor);
Elliott Hughes15dfd632015-09-22 16:40:14 -070095 record.pathname = line + path_offset;
96 if (!record.pathname.empty() && record.pathname.back() == '\n') {
97 record.pathname.pop_back();
98 }
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -070099 maps->push_back(record);
100 }
101 }
102
103 return true;
104 }
105};
106
Yabin Cui76144aa2015-11-19 13:52:16 -0800107extern "C" pid_t gettid();
108
Elliott Hughesffbb0f82016-10-10 18:34:27 -0700109#endif
110
Yabin Cui76144aa2015-11-19 13:52:16 -0800111static inline void WaitUntilThreadSleep(std::atomic<pid_t>& tid) {
112 while (tid == 0) {
113 usleep(1000);
114 }
115 std::string filename = android::base::StringPrintf("/proc/%d/stat", tid.load());
116 std::regex regex {R"(\s+S\s+)"};
117
118 while (true) {
119 std::string content;
120 ASSERT_TRUE(android::base::ReadFileToString(filename, &content));
121 if (std::regex_search(content, regex)) {
122 break;
123 }
124 usleep(1000);
125 }
126}
127
Elliott Hughes33697a02016-01-26 13:04:57 -0800128static inline void AssertChildExited(int pid, int expected_exit_status) {
129 int status;
130 ASSERT_EQ(pid, waitpid(pid, &status, 0));
Evgenii Stepanov68ecec12017-01-31 13:19:30 -0800131 if (expected_exit_status >= 0) {
132 ASSERT_TRUE(WIFEXITED(status));
133 ASSERT_EQ(expected_exit_status, WEXITSTATUS(status));
134 } else {
135 ASSERT_TRUE(WIFSIGNALED(status));
136 ASSERT_EQ(-expected_exit_status, WTERMSIG(status));
137 }
Elliott Hughes33697a02016-01-26 13:04:57 -0800138}
139
Dimitry Ivanov2ba1cf32016-05-17 13:29:37 -0700140// The absolute path to the executable
141const std::string& get_executable_path();
Dimitry Ivanovd17a3772016-03-01 13:11:28 -0800142
Dimitry Ivanov55437462016-07-20 15:33:07 -0700143// Access to argc/argv/envp
144int get_argc();
145char** get_argv();
146char** get_envp();
147
Evgenii Stepanov68ecec12017-01-31 13:19:30 -0800148// ExecTestHelper is only used in bionic and glibc tests.
149#ifndef __APPLE__
150class ExecTestHelper {
151 public:
152 char** GetArgs() {
153 return const_cast<char**>(args_.data());
154 }
155 char** GetEnv() {
156 return const_cast<char**>(env_.data());
157 }
158
159 void SetArgs(const std::vector<const char*> args) {
160 args_ = args;
161 }
162 void SetEnv(const std::vector<const char*> env) {
163 env_ = env;
164 }
165
166 void Run(const std::function<void()>& child_fn, int expected_exit_status,
167 const char* expected_output) {
168 int fds[2];
169 ASSERT_NE(pipe(fds), -1);
170
171 pid_t pid = fork();
172 ASSERT_NE(pid, -1);
173
174 if (pid == 0) {
175 // Child.
176 close(fds[0]);
177 dup2(fds[1], STDOUT_FILENO);
178 dup2(fds[1], STDERR_FILENO);
179 if (fds[1] != STDOUT_FILENO && fds[1] != STDERR_FILENO) close(fds[1]);
180 child_fn();
181 FAIL();
182 }
183
184 // Parent.
185 close(fds[1]);
186 std::string output;
187 char buf[BUFSIZ];
188 ssize_t bytes_read;
189 while ((bytes_read = TEMP_FAILURE_RETRY(read(fds[0], buf, sizeof(buf)))) > 0) {
190 output.append(buf, bytes_read);
191 }
192 close(fds[0]);
193
194 AssertChildExited(pid, expected_exit_status);
195 if (expected_output != nullptr) {
196 ASSERT_EQ(expected_output, output);
197 }
198 }
199
200 private:
201 std::vector<const char*> args_;
202 std::vector<const char*> env_;
203};
204#endif
205
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700206#endif