2015-03-31 20:14:03 +02:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2012 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2019-10-28 23:59:38 +01:00
|
|
|
#pragma once
|
2016-01-26 22:04:57 +01:00
|
|
|
|
2019-11-05 20:31:42 +01:00
|
|
|
#include <dirent.h>
|
2017-08-29 20:29:28 +02:00
|
|
|
#include <dlfcn.h>
|
2017-12-15 22:55:53 +01:00
|
|
|
#include <fcntl.h>
|
2022-06-23 20:07:00 +02:00
|
|
|
#include <gtest/gtest.h>
|
2015-03-31 20:14:03 +02:00
|
|
|
#include <inttypes.h>
|
|
|
|
#include <sys/mman.h>
|
2021-07-12 23:44:02 +02:00
|
|
|
#include <sys/prctl.h>
|
2016-01-26 22:04:57 +01:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/wait.h>
|
2015-11-19 22:52:16 +01:00
|
|
|
#include <unistd.h>
|
|
|
|
|
2020-06-08 21:12:56 +02:00
|
|
|
#if defined(__BIONIC__)
|
|
|
|
#include <sys/system_properties.h>
|
|
|
|
#endif
|
|
|
|
|
2020-04-23 17:47:19 +02:00
|
|
|
#if defined(__BIONIC__)
|
2020-03-13 02:29:52 +01:00
|
|
|
#include <bionic/macros.h>
|
2020-04-23 17:47:19 +02:00
|
|
|
#else
|
|
|
|
#define untag_address(p) p
|
|
|
|
#endif
|
2020-03-13 02:29:52 +01:00
|
|
|
|
2015-11-19 22:52:16 +01:00
|
|
|
#include <atomic>
|
2024-05-20 15:12:09 +02:00
|
|
|
#include <iomanip>
|
2015-11-19 22:52:16 +01:00
|
|
|
#include <string>
|
|
|
|
#include <regex>
|
|
|
|
|
2015-12-05 00:27:46 +01:00
|
|
|
#include <android-base/file.h>
|
2017-11-29 03:02:16 +01:00
|
|
|
#include <android-base/macros.h>
|
2017-04-06 01:20:29 +02:00
|
|
|
#include <android-base/scopeguard.h>
|
2015-12-05 00:27:46 +01:00
|
|
|
#include <android-base/stringprintf.h>
|
2015-03-31 20:14:03 +02:00
|
|
|
|
2017-02-27 21:17:47 +01:00
|
|
|
#if defined(__LP64__)
|
|
|
|
#define PATH_TO_SYSTEM_LIB "/system/lib64/"
|
|
|
|
#else
|
|
|
|
#define PATH_TO_SYSTEM_LIB "/system/lib/"
|
|
|
|
#endif
|
|
|
|
|
2017-10-07 01:58:36 +02:00
|
|
|
#if defined(__GLIBC__)
|
|
|
|
#define BIN_DIR "/bin/"
|
|
|
|
#else
|
|
|
|
#define BIN_DIR "/system/bin/"
|
|
|
|
#endif
|
|
|
|
|
2017-01-10 22:00:37 +01:00
|
|
|
#if defined(__BIONIC__)
|
|
|
|
#define KNOWN_FAILURE_ON_BIONIC(x) xfail_ ## x
|
|
|
|
#else
|
|
|
|
#define KNOWN_FAILURE_ON_BIONIC(x) x
|
|
|
|
#endif
|
|
|
|
|
2017-08-29 20:29:28 +02:00
|
|
|
// bionic's dlsym doesn't work in static binaries, so we can't access icu,
|
|
|
|
// so any unicode test case will fail.
|
|
|
|
static inline bool have_dl() {
|
|
|
|
return (dlopen("libc.so", 0) != nullptr);
|
|
|
|
}
|
|
|
|
|
2020-06-08 21:12:56 +02:00
|
|
|
static inline bool running_with_native_bridge() {
|
|
|
|
#if defined(__BIONIC__)
|
2020-09-19 00:09:01 +02:00
|
|
|
static const prop_info* pi = __system_property_find("ro.dalvik.vm.isa." ABI_STRING);
|
2020-06-08 21:12:56 +02:00
|
|
|
return pi != nullptr;
|
|
|
|
#endif
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define SKIP_WITH_NATIVE_BRIDGE if (running_with_native_bridge()) GTEST_SKIP()
|
|
|
|
|
2016-10-11 03:34:27 +02:00
|
|
|
#if defined(__linux__)
|
|
|
|
|
2017-05-18 02:36:08 +02:00
|
|
|
#include <sys/sysmacros.h>
|
|
|
|
|
2015-03-31 20:14:03 +02:00
|
|
|
struct map_record {
|
|
|
|
uintptr_t addr_start;
|
|
|
|
uintptr_t addr_end;
|
|
|
|
|
|
|
|
int perms;
|
|
|
|
|
|
|
|
size_t offset;
|
|
|
|
|
|
|
|
dev_t device;
|
|
|
|
ino_t inode;
|
|
|
|
|
|
|
|
std::string pathname;
|
|
|
|
};
|
|
|
|
|
|
|
|
class Maps {
|
|
|
|
public:
|
|
|
|
static bool parse_maps(std::vector<map_record>* maps) {
|
2017-10-03 19:18:58 +02:00
|
|
|
maps->clear();
|
2015-03-31 20:14:03 +02:00
|
|
|
|
2017-10-03 19:18:58 +02:00
|
|
|
std::unique_ptr<FILE, decltype(&fclose)> fp(fopen("/proc/self/maps", "re"), fclose);
|
|
|
|
if (!fp) return false;
|
2015-03-31 20:14:03 +02:00
|
|
|
|
|
|
|
char line[BUFSIZ];
|
2017-10-03 19:18:58 +02:00
|
|
|
while (fgets(line, sizeof(line), fp.get()) != nullptr) {
|
2015-03-31 20:14:03 +02:00
|
|
|
map_record record;
|
2015-04-07 04:05:58 +02:00
|
|
|
uint32_t dev_major, dev_minor;
|
2015-09-23 01:40:14 +02:00
|
|
|
int path_offset;
|
2015-03-31 20:14:03 +02:00
|
|
|
char prot[5]; // sizeof("rwxp")
|
2015-09-23 01:40:14 +02:00
|
|
|
if (sscanf(line, "%" SCNxPTR "-%" SCNxPTR " %4s %" SCNxPTR " %x:%x %lu %n",
|
2015-03-31 20:14:03 +02:00
|
|
|
&record.addr_start, &record.addr_end, prot, &record.offset,
|
2015-09-23 01:40:14 +02:00
|
|
|
&dev_major, &dev_minor, &record.inode, &path_offset) == 7) {
|
2015-03-31 20:14:03 +02:00
|
|
|
record.perms = 0;
|
|
|
|
if (prot[0] == 'r') {
|
|
|
|
record.perms |= PROT_READ;
|
|
|
|
}
|
|
|
|
if (prot[1] == 'w') {
|
|
|
|
record.perms |= PROT_WRITE;
|
|
|
|
}
|
|
|
|
if (prot[2] == 'x') {
|
|
|
|
record.perms |= PROT_EXEC;
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: parse shared/private?
|
|
|
|
|
|
|
|
record.device = makedev(dev_major, dev_minor);
|
2015-09-23 01:40:14 +02:00
|
|
|
record.pathname = line + path_offset;
|
|
|
|
if (!record.pathname.empty() && record.pathname.back() == '\n') {
|
|
|
|
record.pathname.pop_back();
|
|
|
|
}
|
2015-03-31 20:14:03 +02:00
|
|
|
maps->push_back(record);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-11-19 22:52:16 +01:00
|
|
|
extern "C" pid_t gettid();
|
|
|
|
|
2016-10-11 03:34:27 +02:00
|
|
|
#endif
|
|
|
|
|
2015-11-19 22:52:16 +01:00
|
|
|
static inline void WaitUntilThreadSleep(std::atomic<pid_t>& tid) {
|
|
|
|
while (tid == 0) {
|
|
|
|
usleep(1000);
|
|
|
|
}
|
|
|
|
std::string filename = android::base::StringPrintf("/proc/%d/stat", tid.load());
|
|
|
|
std::regex regex {R"(\s+S\s+)"};
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
std::string content;
|
|
|
|
ASSERT_TRUE(android::base::ReadFileToString(filename, &content));
|
|
|
|
if (std::regex_search(content, regex)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
usleep(1000);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-08 03:42:14 +01:00
|
|
|
static inline void AssertChildExited(int pid, int expected_exit_status,
|
|
|
|
const std::string* error_msg = nullptr) {
|
2016-01-26 22:04:57 +01:00
|
|
|
int status;
|
2018-02-08 03:42:14 +01:00
|
|
|
std::string error;
|
|
|
|
if (error_msg == nullptr) {
|
|
|
|
error_msg = &error;
|
|
|
|
}
|
|
|
|
ASSERT_EQ(pid, TEMP_FAILURE_RETRY(waitpid(pid, &status, 0))) << *error_msg;
|
2017-01-31 22:19:30 +01:00
|
|
|
if (expected_exit_status >= 0) {
|
2018-02-08 03:42:14 +01:00
|
|
|
ASSERT_TRUE(WIFEXITED(status)) << *error_msg;
|
|
|
|
ASSERT_EQ(expected_exit_status, WEXITSTATUS(status)) << *error_msg;
|
2017-01-31 22:19:30 +01:00
|
|
|
} else {
|
2018-02-08 03:42:14 +01:00
|
|
|
ASSERT_TRUE(WIFSIGNALED(status)) << *error_msg;
|
|
|
|
ASSERT_EQ(-expected_exit_status, WTERMSIG(status)) << *error_msg;
|
2017-01-31 22:19:30 +01:00
|
|
|
}
|
2016-01-26 22:04:57 +01:00
|
|
|
}
|
|
|
|
|
2021-02-05 01:58:13 +01:00
|
|
|
static inline bool CloseOnExec(int fd) {
|
2017-12-15 22:55:53 +01:00
|
|
|
int flags = fcntl(fd, F_GETFD);
|
2021-02-05 01:58:13 +01:00
|
|
|
// This isn't ideal, but the alternatives are worse:
|
|
|
|
// * If we return void and use ASSERT_NE here, we get failures at utils.h:191
|
|
|
|
// rather than in the relevant test.
|
|
|
|
// * If we ignore failures of fcntl(), well, that's obviously a bad idea.
|
|
|
|
if (flags == -1) abort();
|
|
|
|
return flags & FD_CLOEXEC;
|
2017-12-15 22:55:53 +01:00
|
|
|
}
|
|
|
|
|
2016-05-17 22:29:37 +02:00
|
|
|
// The absolute path to the executable
|
|
|
|
const std::string& get_executable_path();
|
2016-03-01 22:11:28 +01:00
|
|
|
|
2016-07-21 00:33:07 +02:00
|
|
|
// Access to argc/argv/envp
|
|
|
|
int get_argc();
|
|
|
|
char** get_argv();
|
|
|
|
char** get_envp();
|
|
|
|
|
2017-01-31 22:19:30 +01:00
|
|
|
// ExecTestHelper is only used in bionic and glibc tests.
|
|
|
|
#ifndef __APPLE__
|
|
|
|
class ExecTestHelper {
|
|
|
|
public:
|
|
|
|
char** GetArgs() {
|
|
|
|
return const_cast<char**>(args_.data());
|
|
|
|
}
|
2017-10-07 01:58:36 +02:00
|
|
|
const char* GetArg0() {
|
|
|
|
return args_[0];
|
|
|
|
}
|
2017-01-31 22:19:30 +01:00
|
|
|
char** GetEnv() {
|
|
|
|
return const_cast<char**>(env_.data());
|
|
|
|
}
|
2018-10-02 08:10:05 +02:00
|
|
|
const std::string& GetOutput() {
|
|
|
|
return output_;
|
|
|
|
}
|
2017-01-31 22:19:30 +01:00
|
|
|
|
2018-01-20 00:45:23 +01:00
|
|
|
void SetArgs(const std::vector<const char*>& args) {
|
2017-01-31 22:19:30 +01:00
|
|
|
args_ = args;
|
|
|
|
}
|
2018-01-20 00:45:23 +01:00
|
|
|
void SetEnv(const std::vector<const char*>& env) {
|
2017-01-31 22:19:30 +01:00
|
|
|
env_ = env;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Run(const std::function<void()>& child_fn, int expected_exit_status,
|
2021-10-01 19:12:15 +02:00
|
|
|
const char* expected_output_regex) {
|
2017-01-31 22:19:30 +01:00
|
|
|
int fds[2];
|
|
|
|
ASSERT_NE(pipe(fds), -1);
|
|
|
|
|
|
|
|
pid_t pid = fork();
|
|
|
|
ASSERT_NE(pid, -1);
|
|
|
|
|
|
|
|
if (pid == 0) {
|
|
|
|
// Child.
|
|
|
|
close(fds[0]);
|
|
|
|
dup2(fds[1], STDOUT_FILENO);
|
|
|
|
dup2(fds[1], STDERR_FILENO);
|
|
|
|
if (fds[1] != STDOUT_FILENO && fds[1] != STDERR_FILENO) close(fds[1]);
|
|
|
|
child_fn();
|
|
|
|
FAIL();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parent.
|
|
|
|
close(fds[1]);
|
2018-10-02 08:10:05 +02:00
|
|
|
output_.clear();
|
2017-01-31 22:19:30 +01:00
|
|
|
char buf[BUFSIZ];
|
|
|
|
ssize_t bytes_read;
|
|
|
|
while ((bytes_read = TEMP_FAILURE_RETRY(read(fds[0], buf, sizeof(buf)))) > 0) {
|
2018-10-02 08:10:05 +02:00
|
|
|
output_.append(buf, bytes_read);
|
2017-01-31 22:19:30 +01:00
|
|
|
}
|
|
|
|
close(fds[0]);
|
|
|
|
|
2018-10-02 08:10:05 +02:00
|
|
|
std::string error_msg("Test output:\n" + output_);
|
2018-02-08 03:42:14 +01:00
|
|
|
AssertChildExited(pid, expected_exit_status, &error_msg);
|
2021-10-01 19:12:15 +02:00
|
|
|
if (expected_output_regex != nullptr) {
|
2021-10-07 02:19:18 +02:00
|
|
|
if (!std::regex_search(output_, std::regex(expected_output_regex))) {
|
2024-05-20 15:12:09 +02:00
|
|
|
FAIL() << "regex " << std::quoted(expected_output_regex) << " didn't match " << std::quoted(output_);
|
2021-10-07 02:19:18 +02:00
|
|
|
}
|
2017-01-31 22:19:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::vector<const char*> args_;
|
|
|
|
std::vector<const char*> env_;
|
2018-10-02 08:10:05 +02:00
|
|
|
std::string output_;
|
2017-01-31 22:19:30 +01:00
|
|
|
};
|
2020-12-01 00:04:14 +01:00
|
|
|
|
|
|
|
void RunGwpAsanTest(const char* test_name);
|
2022-06-23 20:07:00 +02:00
|
|
|
void RunSubtestNoEnv(const char* test_name);
|
2017-01-31 22:19:30 +01:00
|
|
|
#endif
|
2019-11-05 20:31:42 +01:00
|
|
|
|
|
|
|
class FdLeakChecker {
|
|
|
|
public:
|
|
|
|
FdLeakChecker() {
|
|
|
|
}
|
|
|
|
|
|
|
|
~FdLeakChecker() {
|
|
|
|
size_t end_count = CountOpenFds();
|
|
|
|
EXPECT_EQ(start_count_, end_count);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
static size_t CountOpenFds() {
|
|
|
|
auto fd_dir = std::unique_ptr<DIR, decltype(&closedir)>{ opendir("/proc/self/fd"), closedir };
|
|
|
|
size_t count = 0;
|
|
|
|
dirent* de = nullptr;
|
|
|
|
while ((de = readdir(fd_dir.get())) != nullptr) {
|
|
|
|
if (de->d_type == DT_LNK) {
|
|
|
|
++count;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t start_count_ = CountOpenFds();
|
|
|
|
};
|
2020-10-22 22:22:35 +02:00
|
|
|
|
|
|
|
// From <benchmark/benchmark.h>.
|
|
|
|
template <class Tp>
|
|
|
|
static inline void DoNotOptimize(Tp const& value) {
|
|
|
|
asm volatile("" : : "r,m"(value) : "memory");
|
|
|
|
}
|
|
|
|
template <class Tp>
|
|
|
|
static inline void DoNotOptimize(Tp& value) {
|
|
|
|
asm volatile("" : "+r,m"(value) : : "memory");
|
|
|
|
}
|
2021-07-12 23:44:02 +02:00
|
|
|
|
|
|
|
static inline bool running_with_mte() {
|
|
|
|
#ifdef __aarch64__
|
|
|
|
int level = prctl(PR_GET_TAGGED_ADDR_CTRL, 0, 0, 0, 0);
|
|
|
|
return level >= 0 && (level & PR_TAGGED_ADDR_ENABLE) &&
|
|
|
|
(level & PR_MTE_TCF_MASK) != PR_MTE_TCF_NONE;
|
|
|
|
#else
|
|
|
|
return false;
|
|
|
|
#endif
|
|
|
|
}
|
2022-06-03 00:20:23 +02:00
|
|
|
|
|
|
|
bool IsLowRamDevice();
|
2023-09-21 23:11:19 +02:00
|
|
|
|
2023-10-20 01:50:59 +02:00
|
|
|
int64_t NanoTime();
|
|
|
|
|
2023-09-21 23:11:19 +02:00
|
|
|
class Errno {
|
|
|
|
public:
|
|
|
|
Errno(int e) : errno_(e) {}
|
|
|
|
int errno_;
|
|
|
|
};
|
|
|
|
void PrintTo(const Errno& e, std::ostream* os);
|
|
|
|
bool operator==(const Errno& lhs, const Errno& rhs);
|
|
|
|
#define ASSERT_ERRNO(expected_errno) ASSERT_EQ(Errno(expected_errno), Errno(errno))
|
|
|
|
#define EXPECT_ERRNO(expected_errno) EXPECT_EQ(Errno(expected_errno), Errno(errno))
|