1 /*
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
19
20 #include <inttypes.h>
21 #include <sys/mman.h>
22 #include <sys/types.h>
23 #include <sys/wait.h>
24 #include <unistd.h>
25
26 #include <atomic>
27 #include <string>
28 #include <regex>
29
30 #include <android-base/file.h>
31 #include <android-base/scopeguard.h>
32 #include <android-base/stringprintf.h>
33
34 #if defined(__LP64__)
35 #define PATH_TO_SYSTEM_LIB "/system/lib64/"
36 #else
37 #define PATH_TO_SYSTEM_LIB "/system/lib/"
38 #endif
39
40 #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
46 #if defined(__linux__)
47
48 #include <sys/sysmacros.h>
49
50 struct map_record {
51 uintptr_t addr_start;
52 uintptr_t addr_end;
53
54 int perms;
55
56 size_t offset;
57
58 dev_t device;
59 ino_t inode;
60
61 std::string pathname;
62 };
63
64 class Maps {
65 public:
parse_maps(std::vector<map_record> * maps)66 static bool parse_maps(std::vector<map_record>* maps) {
67 FILE* fp = fopen("/proc/self/maps", "re");
68 if (fp == nullptr) {
69 return false;
70 }
71
72 auto fp_guard = android::base::make_scope_guard([&]() { fclose(fp); });
73
74 char line[BUFSIZ];
75 while (fgets(line, sizeof(line), fp) != nullptr) {
76 map_record record;
77 uint32_t dev_major, dev_minor;
78 int path_offset;
79 char prot[5]; // sizeof("rwxp")
80 if (sscanf(line, "%" SCNxPTR "-%" SCNxPTR " %4s %" SCNxPTR " %x:%x %lu %n",
81 &record.addr_start, &record.addr_end, prot, &record.offset,
82 &dev_major, &dev_minor, &record.inode, &path_offset) == 7) {
83 record.perms = 0;
84 if (prot[0] == 'r') {
85 record.perms |= PROT_READ;
86 }
87 if (prot[1] == 'w') {
88 record.perms |= PROT_WRITE;
89 }
90 if (prot[2] == 'x') {
91 record.perms |= PROT_EXEC;
92 }
93
94 // TODO: parse shared/private?
95
96 record.device = makedev(dev_major, dev_minor);
97 record.pathname = line + path_offset;
98 if (!record.pathname.empty() && record.pathname.back() == '\n') {
99 record.pathname.pop_back();
100 }
101 maps->push_back(record);
102 }
103 }
104
105 return true;
106 }
107 };
108
109 extern "C" pid_t gettid();
110
111 #endif
112
WaitUntilThreadSleep(std::atomic<pid_t> & tid)113 static inline void WaitUntilThreadSleep(std::atomic<pid_t>& tid) {
114 while (tid == 0) {
115 usleep(1000);
116 }
117 std::string filename = android::base::StringPrintf("/proc/%d/stat", tid.load());
118 std::regex regex {R"(\s+S\s+)"};
119
120 while (true) {
121 std::string content;
122 ASSERT_TRUE(android::base::ReadFileToString(filename, &content));
123 if (std::regex_search(content, regex)) {
124 break;
125 }
126 usleep(1000);
127 }
128 }
129
AssertChildExited(int pid,int expected_exit_status)130 static inline void AssertChildExited(int pid, int expected_exit_status) {
131 int status;
132 ASSERT_EQ(pid, waitpid(pid, &status, 0));
133 if (expected_exit_status >= 0) {
134 ASSERT_TRUE(WIFEXITED(status));
135 ASSERT_EQ(expected_exit_status, WEXITSTATUS(status));
136 } else {
137 ASSERT_TRUE(WIFSIGNALED(status));
138 ASSERT_EQ(-expected_exit_status, WTERMSIG(status));
139 }
140 }
141
142 // The absolute path to the executable
143 const std::string& get_executable_path();
144
145 // Access to argc/argv/envp
146 int get_argc();
147 char** get_argv();
148 char** get_envp();
149
150 // ExecTestHelper is only used in bionic and glibc tests.
151 #ifndef __APPLE__
152 class ExecTestHelper {
153 public:
GetArgs()154 char** GetArgs() {
155 return const_cast<char**>(args_.data());
156 }
GetEnv()157 char** GetEnv() {
158 return const_cast<char**>(env_.data());
159 }
160
SetArgs(const std::vector<const char * > args)161 void SetArgs(const std::vector<const char*> args) {
162 args_ = args;
163 }
SetEnv(const std::vector<const char * > env)164 void SetEnv(const std::vector<const char*> env) {
165 env_ = env;
166 }
167
Run(const std::function<void ()> & child_fn,int expected_exit_status,const char * expected_output)168 void Run(const std::function<void()>& child_fn, int expected_exit_status,
169 const char* expected_output) {
170 int fds[2];
171 ASSERT_NE(pipe(fds), -1);
172
173 pid_t pid = fork();
174 ASSERT_NE(pid, -1);
175
176 if (pid == 0) {
177 // Child.
178 close(fds[0]);
179 dup2(fds[1], STDOUT_FILENO);
180 dup2(fds[1], STDERR_FILENO);
181 if (fds[1] != STDOUT_FILENO && fds[1] != STDERR_FILENO) close(fds[1]);
182 child_fn();
183 FAIL();
184 }
185
186 // Parent.
187 close(fds[1]);
188 std::string output;
189 char buf[BUFSIZ];
190 ssize_t bytes_read;
191 while ((bytes_read = TEMP_FAILURE_RETRY(read(fds[0], buf, sizeof(buf)))) > 0) {
192 output.append(buf, bytes_read);
193 }
194 close(fds[0]);
195
196 AssertChildExited(pid, expected_exit_status);
197 if (expected_output != nullptr) {
198 ASSERT_EQ(expected_output, output);
199 }
200 }
201
202 private:
203 std::vector<const char*> args_;
204 std::vector<const char*> env_;
205 };
206 #endif
207
208 #endif
209