Dmitriy Ivanov | aae859c | 2015-03-31 11:14:03 -0700 | [diff] [blame] | 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 | |
Elliott Hughes | ca3f8e4 | 2019-10-28 15:59:38 -0700 | [diff] [blame] | 17 | #pragma once |
Elliott Hughes | 33697a0 | 2016-01-26 13:04:57 -0800 | [diff] [blame] | 18 | |
Tom Cherry | fd7216c | 2019-11-05 11:31:42 -0800 | [diff] [blame] | 19 | #include <dirent.h> |
Elliott Hughes | 966d8a3 | 2017-08-29 11:29:28 -0700 | [diff] [blame] | 20 | #include <dlfcn.h> |
Elliott Hughes | a7f1294 | 2017-12-15 13:55:53 -0800 | [diff] [blame] | 21 | #include <fcntl.h> |
Dmitriy Ivanov | aae859c | 2015-03-31 11:14:03 -0700 | [diff] [blame] | 22 | #include <inttypes.h> |
| 23 | #include <sys/mman.h> |
Elliott Hughes | 33697a0 | 2016-01-26 13:04:57 -0800 | [diff] [blame] | 24 | #include <sys/types.h> |
| 25 | #include <sys/wait.h> |
Yabin Cui | 76144aa | 2015-11-19 13:52:16 -0800 | [diff] [blame] | 26 | #include <unistd.h> |
| 27 | |
Peter Collingbourne | 2528dab | 2020-03-12 18:29:52 -0700 | [diff] [blame^] | 28 | #include <bionic/macros.h> |
| 29 | |
Yabin Cui | 76144aa | 2015-11-19 13:52:16 -0800 | [diff] [blame] | 30 | #include <atomic> |
| 31 | #include <string> |
| 32 | #include <regex> |
| 33 | |
Elliott Hughes | 939a7e0 | 2015-12-04 15:27:46 -0800 | [diff] [blame] | 34 | #include <android-base/file.h> |
Elliott Hughes | 4af70b4 | 2017-11-28 18:02:16 -0800 | [diff] [blame] | 35 | #include <android-base/macros.h> |
Tom Cherry | b8ab618 | 2017-04-05 16:20:29 -0700 | [diff] [blame] | 36 | #include <android-base/scopeguard.h> |
Elliott Hughes | 939a7e0 | 2015-12-04 15:27:46 -0800 | [diff] [blame] | 37 | #include <android-base/stringprintf.h> |
Dmitriy Ivanov | aae859c | 2015-03-31 11:14:03 -0700 | [diff] [blame] | 38 | |
Dimitry Ivanov | 35c8e3b | 2017-02-27 12:17:47 -0800 | [diff] [blame] | 39 | #if defined(__LP64__) |
| 40 | #define PATH_TO_SYSTEM_LIB "/system/lib64/" |
| 41 | #else |
| 42 | #define PATH_TO_SYSTEM_LIB "/system/lib/" |
| 43 | #endif |
| 44 | |
Elliott Hughes | 14e3ff9 | 2017-10-06 16:58:36 -0700 | [diff] [blame] | 45 | #if defined(__GLIBC__) |
| 46 | #define BIN_DIR "/bin/" |
| 47 | #else |
| 48 | #define BIN_DIR "/system/bin/" |
| 49 | #endif |
| 50 | |
Josh Gao | 2f06e10 | 2017-01-10 13:00:37 -0800 | [diff] [blame] | 51 | #if defined(__BIONIC__) |
| 52 | #define KNOWN_FAILURE_ON_BIONIC(x) xfail_ ## x |
| 53 | #else |
| 54 | #define KNOWN_FAILURE_ON_BIONIC(x) x |
| 55 | #endif |
| 56 | |
Elliott Hughes | 966d8a3 | 2017-08-29 11:29:28 -0700 | [diff] [blame] | 57 | // bionic's dlsym doesn't work in static binaries, so we can't access icu, |
| 58 | // so any unicode test case will fail. |
| 59 | static inline bool have_dl() { |
| 60 | return (dlopen("libc.so", 0) != nullptr); |
| 61 | } |
| 62 | |
Evgenii Stepanov | acd6f4f | 2018-11-06 16:48:27 -0800 | [diff] [blame] | 63 | extern "C" void __hwasan_init() __attribute__((weak)); |
| 64 | |
| 65 | static inline bool running_with_hwasan() { |
| 66 | return &__hwasan_init != 0; |
| 67 | } |
| 68 | |
Elliott Hughes | bcaa454 | 2019-03-08 15:20:23 -0800 | [diff] [blame] | 69 | #define SKIP_WITH_HWASAN if (running_with_hwasan()) GTEST_SKIP() |
Evgenii Stepanov | acd6f4f | 2018-11-06 16:48:27 -0800 | [diff] [blame] | 70 | |
Elliott Hughes | ffbb0f8 | 2016-10-10 18:34:27 -0700 | [diff] [blame] | 71 | #if defined(__linux__) |
| 72 | |
Elliott Hughes | 3fa758f | 2017-05-17 17:36:08 -0700 | [diff] [blame] | 73 | #include <sys/sysmacros.h> |
| 74 | |
Dmitriy Ivanov | aae859c | 2015-03-31 11:14:03 -0700 | [diff] [blame] | 75 | struct map_record { |
| 76 | uintptr_t addr_start; |
| 77 | uintptr_t addr_end; |
| 78 | |
| 79 | int perms; |
| 80 | |
| 81 | size_t offset; |
| 82 | |
| 83 | dev_t device; |
| 84 | ino_t inode; |
| 85 | |
| 86 | std::string pathname; |
| 87 | }; |
| 88 | |
| 89 | class Maps { |
| 90 | public: |
| 91 | static bool parse_maps(std::vector<map_record>* maps) { |
Elliott Hughes | a838490 | 2017-10-03 10:18:58 -0700 | [diff] [blame] | 92 | maps->clear(); |
Dmitriy Ivanov | aae859c | 2015-03-31 11:14:03 -0700 | [diff] [blame] | 93 | |
Elliott Hughes | a838490 | 2017-10-03 10:18:58 -0700 | [diff] [blame] | 94 | std::unique_ptr<FILE, decltype(&fclose)> fp(fopen("/proc/self/maps", "re"), fclose); |
| 95 | if (!fp) return false; |
Dmitriy Ivanov | aae859c | 2015-03-31 11:14:03 -0700 | [diff] [blame] | 96 | |
| 97 | char line[BUFSIZ]; |
Elliott Hughes | a838490 | 2017-10-03 10:18:58 -0700 | [diff] [blame] | 98 | while (fgets(line, sizeof(line), fp.get()) != nullptr) { |
Dmitriy Ivanov | aae859c | 2015-03-31 11:14:03 -0700 | [diff] [blame] | 99 | map_record record; |
Dmitriy Ivanov | 1dce3ed | 2015-04-06 19:05:58 -0700 | [diff] [blame] | 100 | uint32_t dev_major, dev_minor; |
Elliott Hughes | 15dfd63 | 2015-09-22 16:40:14 -0700 | [diff] [blame] | 101 | int path_offset; |
Dmitriy Ivanov | aae859c | 2015-03-31 11:14:03 -0700 | [diff] [blame] | 102 | char prot[5]; // sizeof("rwxp") |
Elliott Hughes | 15dfd63 | 2015-09-22 16:40:14 -0700 | [diff] [blame] | 103 | if (sscanf(line, "%" SCNxPTR "-%" SCNxPTR " %4s %" SCNxPTR " %x:%x %lu %n", |
Dmitriy Ivanov | aae859c | 2015-03-31 11:14:03 -0700 | [diff] [blame] | 104 | &record.addr_start, &record.addr_end, prot, &record.offset, |
Elliott Hughes | 15dfd63 | 2015-09-22 16:40:14 -0700 | [diff] [blame] | 105 | &dev_major, &dev_minor, &record.inode, &path_offset) == 7) { |
Dmitriy Ivanov | aae859c | 2015-03-31 11:14:03 -0700 | [diff] [blame] | 106 | record.perms = 0; |
| 107 | if (prot[0] == 'r') { |
| 108 | record.perms |= PROT_READ; |
| 109 | } |
| 110 | if (prot[1] == 'w') { |
| 111 | record.perms |= PROT_WRITE; |
| 112 | } |
| 113 | if (prot[2] == 'x') { |
| 114 | record.perms |= PROT_EXEC; |
| 115 | } |
| 116 | |
| 117 | // TODO: parse shared/private? |
| 118 | |
| 119 | record.device = makedev(dev_major, dev_minor); |
Elliott Hughes | 15dfd63 | 2015-09-22 16:40:14 -0700 | [diff] [blame] | 120 | record.pathname = line + path_offset; |
| 121 | if (!record.pathname.empty() && record.pathname.back() == '\n') { |
| 122 | record.pathname.pop_back(); |
| 123 | } |
Dmitriy Ivanov | aae859c | 2015-03-31 11:14:03 -0700 | [diff] [blame] | 124 | maps->push_back(record); |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | return true; |
| 129 | } |
| 130 | }; |
| 131 | |
Yabin Cui | 76144aa | 2015-11-19 13:52:16 -0800 | [diff] [blame] | 132 | extern "C" pid_t gettid(); |
| 133 | |
Elliott Hughes | ffbb0f8 | 2016-10-10 18:34:27 -0700 | [diff] [blame] | 134 | #endif |
| 135 | |
Yabin Cui | 76144aa | 2015-11-19 13:52:16 -0800 | [diff] [blame] | 136 | static inline void WaitUntilThreadSleep(std::atomic<pid_t>& tid) { |
| 137 | while (tid == 0) { |
| 138 | usleep(1000); |
| 139 | } |
| 140 | std::string filename = android::base::StringPrintf("/proc/%d/stat", tid.load()); |
| 141 | std::regex regex {R"(\s+S\s+)"}; |
| 142 | |
| 143 | while (true) { |
| 144 | std::string content; |
| 145 | ASSERT_TRUE(android::base::ReadFileToString(filename, &content)); |
| 146 | if (std::regex_search(content, regex)) { |
| 147 | break; |
| 148 | } |
| 149 | usleep(1000); |
| 150 | } |
| 151 | } |
| 152 | |
Christopher Ferris | d6a1dc2 | 2018-02-07 18:42:14 -0800 | [diff] [blame] | 153 | static inline void AssertChildExited(int pid, int expected_exit_status, |
| 154 | const std::string* error_msg = nullptr) { |
Elliott Hughes | 33697a0 | 2016-01-26 13:04:57 -0800 | [diff] [blame] | 155 | int status; |
Christopher Ferris | d6a1dc2 | 2018-02-07 18:42:14 -0800 | [diff] [blame] | 156 | std::string error; |
| 157 | if (error_msg == nullptr) { |
| 158 | error_msg = &error; |
| 159 | } |
| 160 | ASSERT_EQ(pid, TEMP_FAILURE_RETRY(waitpid(pid, &status, 0))) << *error_msg; |
Evgenii Stepanov | 68ecec1 | 2017-01-31 13:19:30 -0800 | [diff] [blame] | 161 | if (expected_exit_status >= 0) { |
Christopher Ferris | d6a1dc2 | 2018-02-07 18:42:14 -0800 | [diff] [blame] | 162 | ASSERT_TRUE(WIFEXITED(status)) << *error_msg; |
| 163 | ASSERT_EQ(expected_exit_status, WEXITSTATUS(status)) << *error_msg; |
Evgenii Stepanov | 68ecec1 | 2017-01-31 13:19:30 -0800 | [diff] [blame] | 164 | } else { |
Christopher Ferris | d6a1dc2 | 2018-02-07 18:42:14 -0800 | [diff] [blame] | 165 | ASSERT_TRUE(WIFSIGNALED(status)) << *error_msg; |
| 166 | ASSERT_EQ(-expected_exit_status, WTERMSIG(status)) << *error_msg; |
Evgenii Stepanov | 68ecec1 | 2017-01-31 13:19:30 -0800 | [diff] [blame] | 167 | } |
Elliott Hughes | 33697a0 | 2016-01-26 13:04:57 -0800 | [diff] [blame] | 168 | } |
| 169 | |
Elliott Hughes | a7f1294 | 2017-12-15 13:55:53 -0800 | [diff] [blame] | 170 | static inline void AssertCloseOnExec(int fd, bool close_on_exec) { |
| 171 | int flags = fcntl(fd, F_GETFD); |
| 172 | ASSERT_NE(flags, -1); |
| 173 | ASSERT_EQ(close_on_exec ? FD_CLOEXEC : 0, flags & FD_CLOEXEC); |
| 174 | } |
| 175 | |
Dimitry Ivanov | 2ba1cf3 | 2016-05-17 13:29:37 -0700 | [diff] [blame] | 176 | // The absolute path to the executable |
| 177 | const std::string& get_executable_path(); |
Dimitry Ivanov | d17a377 | 2016-03-01 13:11:28 -0800 | [diff] [blame] | 178 | |
Dimitry Ivanov | 5543746 | 2016-07-20 15:33:07 -0700 | [diff] [blame] | 179 | // Access to argc/argv/envp |
| 180 | int get_argc(); |
| 181 | char** get_argv(); |
| 182 | char** get_envp(); |
| 183 | |
Evgenii Stepanov | 68ecec1 | 2017-01-31 13:19:30 -0800 | [diff] [blame] | 184 | // ExecTestHelper is only used in bionic and glibc tests. |
| 185 | #ifndef __APPLE__ |
| 186 | class ExecTestHelper { |
| 187 | public: |
| 188 | char** GetArgs() { |
| 189 | return const_cast<char**>(args_.data()); |
| 190 | } |
Elliott Hughes | 14e3ff9 | 2017-10-06 16:58:36 -0700 | [diff] [blame] | 191 | const char* GetArg0() { |
| 192 | return args_[0]; |
| 193 | } |
Evgenii Stepanov | 68ecec1 | 2017-01-31 13:19:30 -0800 | [diff] [blame] | 194 | char** GetEnv() { |
| 195 | return const_cast<char**>(env_.data()); |
| 196 | } |
Ryan Prichard | 8f639a4 | 2018-10-01 23:10:05 -0700 | [diff] [blame] | 197 | const std::string& GetOutput() { |
| 198 | return output_; |
| 199 | } |
Evgenii Stepanov | 68ecec1 | 2017-01-31 13:19:30 -0800 | [diff] [blame] | 200 | |
Elliott Hughes | 5cec377 | 2018-01-19 15:45:23 -0800 | [diff] [blame] | 201 | void SetArgs(const std::vector<const char*>& args) { |
Evgenii Stepanov | 68ecec1 | 2017-01-31 13:19:30 -0800 | [diff] [blame] | 202 | args_ = args; |
| 203 | } |
Elliott Hughes | 5cec377 | 2018-01-19 15:45:23 -0800 | [diff] [blame] | 204 | void SetEnv(const std::vector<const char*>& env) { |
Evgenii Stepanov | 68ecec1 | 2017-01-31 13:19:30 -0800 | [diff] [blame] | 205 | env_ = env; |
| 206 | } |
| 207 | |
| 208 | void Run(const std::function<void()>& child_fn, int expected_exit_status, |
| 209 | const char* expected_output) { |
| 210 | int fds[2]; |
| 211 | ASSERT_NE(pipe(fds), -1); |
| 212 | |
| 213 | pid_t pid = fork(); |
| 214 | ASSERT_NE(pid, -1); |
| 215 | |
| 216 | if (pid == 0) { |
| 217 | // Child. |
| 218 | close(fds[0]); |
| 219 | dup2(fds[1], STDOUT_FILENO); |
| 220 | dup2(fds[1], STDERR_FILENO); |
| 221 | if (fds[1] != STDOUT_FILENO && fds[1] != STDERR_FILENO) close(fds[1]); |
| 222 | child_fn(); |
| 223 | FAIL(); |
| 224 | } |
| 225 | |
| 226 | // Parent. |
| 227 | close(fds[1]); |
Ryan Prichard | 8f639a4 | 2018-10-01 23:10:05 -0700 | [diff] [blame] | 228 | output_.clear(); |
Evgenii Stepanov | 68ecec1 | 2017-01-31 13:19:30 -0800 | [diff] [blame] | 229 | char buf[BUFSIZ]; |
| 230 | ssize_t bytes_read; |
| 231 | while ((bytes_read = TEMP_FAILURE_RETRY(read(fds[0], buf, sizeof(buf)))) > 0) { |
Ryan Prichard | 8f639a4 | 2018-10-01 23:10:05 -0700 | [diff] [blame] | 232 | output_.append(buf, bytes_read); |
Evgenii Stepanov | 68ecec1 | 2017-01-31 13:19:30 -0800 | [diff] [blame] | 233 | } |
| 234 | close(fds[0]); |
| 235 | |
Ryan Prichard | 8f639a4 | 2018-10-01 23:10:05 -0700 | [diff] [blame] | 236 | std::string error_msg("Test output:\n" + output_); |
Christopher Ferris | d6a1dc2 | 2018-02-07 18:42:14 -0800 | [diff] [blame] | 237 | AssertChildExited(pid, expected_exit_status, &error_msg); |
Evgenii Stepanov | 68ecec1 | 2017-01-31 13:19:30 -0800 | [diff] [blame] | 238 | if (expected_output != nullptr) { |
Ryan Prichard | 8f639a4 | 2018-10-01 23:10:05 -0700 | [diff] [blame] | 239 | ASSERT_EQ(expected_output, output_); |
Evgenii Stepanov | 68ecec1 | 2017-01-31 13:19:30 -0800 | [diff] [blame] | 240 | } |
| 241 | } |
| 242 | |
| 243 | private: |
| 244 | std::vector<const char*> args_; |
| 245 | std::vector<const char*> env_; |
Ryan Prichard | 8f639a4 | 2018-10-01 23:10:05 -0700 | [diff] [blame] | 246 | std::string output_; |
Evgenii Stepanov | 68ecec1 | 2017-01-31 13:19:30 -0800 | [diff] [blame] | 247 | }; |
| 248 | #endif |
Tom Cherry | fd7216c | 2019-11-05 11:31:42 -0800 | [diff] [blame] | 249 | |
| 250 | class FdLeakChecker { |
| 251 | public: |
| 252 | FdLeakChecker() { |
| 253 | } |
| 254 | |
| 255 | ~FdLeakChecker() { |
| 256 | size_t end_count = CountOpenFds(); |
| 257 | EXPECT_EQ(start_count_, end_count); |
| 258 | } |
| 259 | |
| 260 | private: |
| 261 | static size_t CountOpenFds() { |
| 262 | auto fd_dir = std::unique_ptr<DIR, decltype(&closedir)>{ opendir("/proc/self/fd"), closedir }; |
| 263 | size_t count = 0; |
| 264 | dirent* de = nullptr; |
| 265 | while ((de = readdir(fd_dir.get())) != nullptr) { |
| 266 | if (de->d_type == DT_LNK) { |
| 267 | ++count; |
| 268 | } |
| 269 | } |
| 270 | return count; |
| 271 | } |
| 272 | |
| 273 | size_t start_count_ = CountOpenFds(); |
| 274 | }; |