2016-10-19 03:17:52 +02:00
|
|
|
/*
|
|
|
|
* Copyright 2016, 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.
|
|
|
|
*/
|
|
|
|
|
2021-02-18 00:39:06 +01:00
|
|
|
#include <dirent.h>
|
2016-10-19 03:17:52 +02:00
|
|
|
#include <err.h>
|
|
|
|
#include <fcntl.h>
|
2021-01-15 20:34:26 +01:00
|
|
|
#include <malloc.h>
|
2017-11-01 23:00:40 +01:00
|
|
|
#include <stdlib.h>
|
2017-02-17 10:39:15 +01:00
|
|
|
#include <sys/capability.h>
|
2020-07-21 00:08:52 +02:00
|
|
|
#include <sys/mman.h>
|
2017-01-23 21:05:35 +01:00
|
|
|
#include <sys/prctl.h>
|
2017-08-19 00:37:26 +02:00
|
|
|
#include <sys/ptrace.h>
|
2018-02-22 20:38:33 +01:00
|
|
|
#include <sys/resource.h>
|
2017-10-11 20:35:40 +02:00
|
|
|
#include <sys/syscall.h>
|
2016-10-19 03:17:52 +02:00
|
|
|
#include <sys/types.h>
|
2017-08-19 00:37:26 +02:00
|
|
|
#include <unistd.h>
|
2016-10-19 03:17:52 +02:00
|
|
|
|
|
|
|
#include <chrono>
|
|
|
|
#include <regex>
|
|
|
|
#include <thread>
|
|
|
|
|
2018-08-28 01:34:01 +02:00
|
|
|
#include <android/fdsan.h>
|
2017-02-17 10:39:15 +01:00
|
|
|
#include <android/set_abort_message.h>
|
2020-04-07 23:07:32 +02:00
|
|
|
#include <bionic/mte.h>
|
2019-12-13 23:11:04 +01:00
|
|
|
#include <bionic/reserved_signals.h>
|
2017-02-17 10:39:15 +01:00
|
|
|
|
2019-01-10 02:01:49 +01:00
|
|
|
#include <android-base/cmsg.h>
|
2016-10-19 03:17:52 +02:00
|
|
|
#include <android-base/file.h>
|
|
|
|
#include <android-base/logging.h>
|
2017-05-05 02:12:57 +02:00
|
|
|
#include <android-base/macros.h>
|
2016-10-19 03:17:52 +02:00
|
|
|
#include <android-base/parseint.h>
|
|
|
|
#include <android-base/properties.h>
|
2018-09-12 23:51:03 +02:00
|
|
|
#include <android-base/stringprintf.h>
|
2016-10-19 03:17:52 +02:00
|
|
|
#include <android-base/strings.h>
|
2017-04-28 04:48:44 +02:00
|
|
|
#include <android-base/test_utils.h>
|
2016-10-19 03:17:52 +02:00
|
|
|
#include <android-base/unique_fd.h>
|
|
|
|
#include <cutils/sockets.h>
|
|
|
|
#include <gtest/gtest.h>
|
|
|
|
|
2018-01-17 00:38:17 +01:00
|
|
|
#include <libminijail.h>
|
|
|
|
#include <scoped_minijail.h>
|
|
|
|
|
2017-05-10 11:58:59 +02:00
|
|
|
#include "debuggerd/handler.h"
|
|
|
|
#include "protocol.h"
|
|
|
|
#include "tombstoned/tombstoned.h"
|
|
|
|
#include "util.h"
|
|
|
|
|
2016-10-19 03:17:52 +02:00
|
|
|
using namespace std::chrono_literals;
|
2019-01-10 02:01:49 +01:00
|
|
|
|
|
|
|
using android::base::SendFileDescriptors;
|
2016-10-19 03:17:52 +02:00
|
|
|
using android::base::unique_fd;
|
|
|
|
|
|
|
|
#if defined(__LP64__)
|
|
|
|
#define ARCH_SUFFIX "64"
|
|
|
|
#else
|
|
|
|
#define ARCH_SUFFIX ""
|
|
|
|
#endif
|
|
|
|
|
2021-03-17 17:15:15 +01:00
|
|
|
constexpr char kWaitForDebuggerKey[] = "debug.debuggerd.wait_for_debugger";
|
2016-10-19 03:17:52 +02:00
|
|
|
|
|
|
|
#define TIMEOUT(seconds, expr) \
|
|
|
|
[&]() { \
|
|
|
|
struct sigaction old_sigaction; \
|
|
|
|
struct sigaction new_sigaction = {}; \
|
|
|
|
new_sigaction.sa_handler = [](int) {}; \
|
|
|
|
if (sigaction(SIGALRM, &new_sigaction, &new_sigaction) != 0) { \
|
|
|
|
err(1, "sigaction failed"); \
|
|
|
|
} \
|
|
|
|
alarm(seconds); \
|
|
|
|
auto value = expr; \
|
|
|
|
int saved_errno = errno; \
|
|
|
|
if (sigaction(SIGALRM, &old_sigaction, nullptr) != 0) { \
|
|
|
|
err(1, "sigaction failed"); \
|
|
|
|
} \
|
|
|
|
alarm(0); \
|
|
|
|
errno = saved_errno; \
|
|
|
|
return value; \
|
|
|
|
}()
|
|
|
|
|
2018-05-12 01:01:21 +02:00
|
|
|
// Backtrace frame dump could contain:
|
|
|
|
// #01 pc 0001cded /data/tmp/debuggerd_test32 (raise_debugger_signal+80)
|
|
|
|
// or
|
|
|
|
// #01 pc 00022a09 /data/tmp/debuggerd_test32 (offset 0x12000) (raise_debugger_signal+80)
|
2018-01-17 00:38:17 +01:00
|
|
|
#define ASSERT_BACKTRACE_FRAME(result, frame_name) \
|
2018-05-12 01:01:21 +02:00
|
|
|
ASSERT_MATCH(result, \
|
|
|
|
R"(#\d\d pc [0-9a-f]+\s+ \S+ (\(offset 0x[0-9a-f]+\) )?\()" frame_name R"(\+)");
|
2017-06-15 11:20:34 +02:00
|
|
|
|
2017-05-24 16:07:25 +02:00
|
|
|
static void tombstoned_intercept(pid_t target_pid, unique_fd* intercept_fd, unique_fd* output_fd,
|
2017-06-02 16:42:06 +02:00
|
|
|
InterceptStatus* status, DebuggerdDumpType intercept_type) {
|
2017-03-31 01:40:47 +02:00
|
|
|
intercept_fd->reset(socket_local_client(kTombstonedInterceptSocketName,
|
|
|
|
ANDROID_SOCKET_NAMESPACE_RESERVED, SOCK_SEQPACKET));
|
|
|
|
if (intercept_fd->get() == -1) {
|
|
|
|
FAIL() << "failed to contact tombstoned: " << strerror(errno);
|
|
|
|
}
|
|
|
|
|
2019-10-08 08:28:15 +02:00
|
|
|
InterceptRequest req = {
|
|
|
|
.dump_type = intercept_type,
|
|
|
|
.pid = target_pid,
|
|
|
|
};
|
2017-03-31 01:40:47 +02:00
|
|
|
|
|
|
|
unique_fd output_pipe_write;
|
|
|
|
if (!Pipe(output_fd, &output_pipe_write)) {
|
|
|
|
FAIL() << "failed to create output pipe: " << strerror(errno);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string pipe_size_str;
|
|
|
|
int pipe_buffer_size;
|
|
|
|
if (!android::base::ReadFileToString("/proc/sys/fs/pipe-max-size", &pipe_size_str)) {
|
|
|
|
FAIL() << "failed to read /proc/sys/fs/pipe-max-size: " << strerror(errno);
|
|
|
|
}
|
|
|
|
|
|
|
|
pipe_size_str = android::base::Trim(pipe_size_str);
|
|
|
|
|
|
|
|
if (!android::base::ParseInt(pipe_size_str.c_str(), &pipe_buffer_size, 0)) {
|
|
|
|
FAIL() << "failed to parse pipe max size";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fcntl(output_fd->get(), F_SETPIPE_SZ, pipe_buffer_size) != pipe_buffer_size) {
|
|
|
|
FAIL() << "failed to set pipe size: " << strerror(errno);
|
|
|
|
}
|
|
|
|
|
2017-06-01 21:19:53 +02:00
|
|
|
ASSERT_GE(pipe_buffer_size, 1024 * 1024);
|
|
|
|
|
2019-01-10 02:01:49 +01:00
|
|
|
ssize_t rc = SendFileDescriptors(intercept_fd->get(), &req, sizeof(req), output_pipe_write.get());
|
|
|
|
output_pipe_write.reset();
|
|
|
|
if (rc != sizeof(req)) {
|
2017-03-31 01:40:47 +02:00
|
|
|
FAIL() << "failed to send output fd to tombstoned: " << strerror(errno);
|
|
|
|
}
|
|
|
|
|
|
|
|
InterceptResponse response;
|
2019-01-10 02:01:49 +01:00
|
|
|
rc = TEMP_FAILURE_RETRY(read(intercept_fd->get(), &response, sizeof(response)));
|
2017-03-31 01:40:47 +02:00
|
|
|
if (rc == -1) {
|
|
|
|
FAIL() << "failed to read response from tombstoned: " << strerror(errno);
|
|
|
|
} else if (rc == 0) {
|
|
|
|
FAIL() << "failed to read response from tombstoned (EOF)";
|
|
|
|
} else if (rc != sizeof(response)) {
|
|
|
|
FAIL() << "received packet of unexpected length from tombstoned: expected " << sizeof(response)
|
|
|
|
<< ", received " << rc;
|
|
|
|
}
|
|
|
|
|
2017-06-02 16:42:06 +02:00
|
|
|
*status = response.status;
|
2017-03-31 01:40:47 +02:00
|
|
|
}
|
|
|
|
|
2016-10-19 03:17:52 +02:00
|
|
|
class CrasherTest : public ::testing::Test {
|
|
|
|
public:
|
|
|
|
pid_t crasher_pid = -1;
|
2021-03-17 17:15:15 +01:00
|
|
|
bool previous_wait_for_debugger;
|
2016-10-19 03:17:52 +02:00
|
|
|
unique_fd crasher_pipe;
|
|
|
|
unique_fd intercept_fd;
|
|
|
|
|
|
|
|
CrasherTest();
|
|
|
|
~CrasherTest();
|
|
|
|
|
2017-05-24 16:07:25 +02:00
|
|
|
void StartIntercept(unique_fd* output_fd, DebuggerdDumpType intercept_type = kDebuggerdTombstone);
|
2016-10-19 03:17:52 +02:00
|
|
|
|
|
|
|
// Returns -1 if we fail to read a response from tombstoned, otherwise the received return code.
|
|
|
|
void FinishIntercept(int* result);
|
|
|
|
|
2017-05-05 02:12:57 +02:00
|
|
|
void StartProcess(std::function<void()> function, std::function<pid_t()> forker = fork);
|
2016-10-19 03:17:52 +02:00
|
|
|
void StartCrasher(const std::string& crash_type);
|
|
|
|
void FinishCrasher();
|
|
|
|
void AssertDeath(int signo);
|
2020-07-17 23:49:31 +02:00
|
|
|
|
|
|
|
static void Trap(void* ptr);
|
2016-10-19 03:17:52 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
CrasherTest::CrasherTest() {
|
2021-03-17 17:15:15 +01:00
|
|
|
previous_wait_for_debugger = android::base::GetBoolProperty(kWaitForDebuggerKey, false);
|
|
|
|
android::base::SetProperty(kWaitForDebuggerKey, "0");
|
|
|
|
|
|
|
|
// Clear the old property too, just in case someone's been using it
|
|
|
|
// on this device. (We only document the new name, but we still support
|
|
|
|
// the old name so we don't break anyone's existing setups.)
|
|
|
|
android::base::SetProperty("debug.debuggerd.wait_for_gdb", "0");
|
2016-10-19 03:17:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
CrasherTest::~CrasherTest() {
|
|
|
|
if (crasher_pid != -1) {
|
|
|
|
kill(crasher_pid, SIGKILL);
|
|
|
|
int status;
|
2019-09-19 02:48:30 +02:00
|
|
|
TEMP_FAILURE_RETRY(waitpid(crasher_pid, &status, WUNTRACED));
|
2016-10-19 03:17:52 +02:00
|
|
|
}
|
|
|
|
|
2021-03-17 17:15:15 +01:00
|
|
|
android::base::SetProperty(kWaitForDebuggerKey, previous_wait_for_debugger ? "1" : "0");
|
2016-10-19 03:17:52 +02:00
|
|
|
}
|
|
|
|
|
2017-05-24 16:07:25 +02:00
|
|
|
void CrasherTest::StartIntercept(unique_fd* output_fd, DebuggerdDumpType intercept_type) {
|
2016-10-19 03:17:52 +02:00
|
|
|
if (crasher_pid == -1) {
|
|
|
|
FAIL() << "crasher hasn't been started";
|
|
|
|
}
|
|
|
|
|
2017-06-02 16:42:06 +02:00
|
|
|
InterceptStatus status;
|
|
|
|
tombstoned_intercept(crasher_pid, &this->intercept_fd, output_fd, &status, intercept_type);
|
|
|
|
ASSERT_EQ(InterceptStatus::kRegistered, status);
|
2016-10-19 03:17:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void CrasherTest::FinishIntercept(int* result) {
|
|
|
|
InterceptResponse response;
|
|
|
|
|
2019-09-20 23:18:55 +02:00
|
|
|
ssize_t rc = TIMEOUT(30, read(intercept_fd.get(), &response, sizeof(response)));
|
2016-10-19 03:17:52 +02:00
|
|
|
if (rc == -1) {
|
|
|
|
FAIL() << "failed to read response from tombstoned: " << strerror(errno);
|
|
|
|
} else if (rc == 0) {
|
|
|
|
*result = -1;
|
|
|
|
} else if (rc != sizeof(response)) {
|
|
|
|
FAIL() << "received packet of unexpected length from tombstoned: expected " << sizeof(response)
|
|
|
|
<< ", received " << rc;
|
|
|
|
} else {
|
2017-03-31 01:40:47 +02:00
|
|
|
*result = response.status == InterceptStatus::kStarted ? 1 : 0;
|
2016-10-19 03:17:52 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-05 02:12:57 +02:00
|
|
|
void CrasherTest::StartProcess(std::function<void()> function, std::function<pid_t()> forker) {
|
2017-01-23 21:05:35 +01:00
|
|
|
unique_fd read_pipe;
|
2016-10-19 03:17:52 +02:00
|
|
|
unique_fd crasher_read_pipe;
|
|
|
|
if (!Pipe(&crasher_read_pipe, &crasher_pipe)) {
|
|
|
|
FAIL() << "failed to create pipe: " << strerror(errno);
|
|
|
|
}
|
|
|
|
|
2017-05-05 02:12:57 +02:00
|
|
|
crasher_pid = forker();
|
2016-10-19 03:17:52 +02:00
|
|
|
if (crasher_pid == -1) {
|
|
|
|
FAIL() << "fork failed: " << strerror(errno);
|
|
|
|
} else if (crasher_pid == 0) {
|
2017-02-17 10:39:15 +01:00
|
|
|
char dummy;
|
|
|
|
crasher_pipe.reset();
|
|
|
|
TEMP_FAILURE_RETRY(read(crasher_read_pipe.get(), &dummy, 1));
|
2017-01-23 21:05:35 +01:00
|
|
|
function();
|
|
|
|
_exit(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-19 03:17:52 +02:00
|
|
|
void CrasherTest::FinishCrasher() {
|
|
|
|
if (crasher_pipe == -1) {
|
|
|
|
FAIL() << "crasher pipe uninitialized";
|
|
|
|
}
|
|
|
|
|
2019-09-19 02:48:30 +02:00
|
|
|
ssize_t rc = TEMP_FAILURE_RETRY(write(crasher_pipe.get(), "\n", 1));
|
2016-10-19 03:17:52 +02:00
|
|
|
if (rc == -1) {
|
|
|
|
FAIL() << "failed to write to crasher pipe: " << strerror(errno);
|
|
|
|
} else if (rc == 0) {
|
|
|
|
FAIL() << "crasher pipe was closed";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CrasherTest::AssertDeath(int signo) {
|
|
|
|
int status;
|
2019-09-20 23:18:55 +02:00
|
|
|
pid_t pid = TIMEOUT(30, waitpid(crasher_pid, &status, 0));
|
2016-10-19 03:17:52 +02:00
|
|
|
if (pid != crasher_pid) {
|
2019-06-27 00:08:51 +02:00
|
|
|
printf("failed to wait for crasher (expected pid %d, return value %d): %s\n", crasher_pid, pid,
|
|
|
|
strerror(errno));
|
2017-08-21 23:31:17 +02:00
|
|
|
sleep(100);
|
2016-10-19 03:17:52 +02:00
|
|
|
FAIL() << "failed to wait for crasher: " << strerror(errno);
|
|
|
|
}
|
|
|
|
|
2017-04-28 01:50:38 +02:00
|
|
|
if (signo == 0) {
|
|
|
|
ASSERT_TRUE(WIFEXITED(status));
|
|
|
|
ASSERT_EQ(0, WEXITSTATUS(signo));
|
|
|
|
} else {
|
|
|
|
ASSERT_FALSE(WIFEXITED(status));
|
|
|
|
ASSERT_TRUE(WIFSIGNALED(status)) << "crasher didn't terminate via a signal";
|
|
|
|
ASSERT_EQ(signo, WTERMSIG(status));
|
2016-10-19 03:17:52 +02:00
|
|
|
}
|
|
|
|
crasher_pid = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void ConsumeFd(unique_fd fd, std::string* output) {
|
|
|
|
constexpr size_t read_length = PAGE_SIZE;
|
|
|
|
std::string result;
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
size_t offset = result.size();
|
|
|
|
result.resize(result.size() + PAGE_SIZE);
|
|
|
|
ssize_t rc = TEMP_FAILURE_RETRY(read(fd.get(), &result[offset], read_length));
|
|
|
|
if (rc == -1) {
|
|
|
|
FAIL() << "read failed: " << strerror(errno);
|
|
|
|
} else if (rc == 0) {
|
|
|
|
result.resize(result.size() - PAGE_SIZE);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
result.resize(result.size() - PAGE_SIZE + rc);
|
|
|
|
}
|
|
|
|
|
|
|
|
*output = std::move(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(CrasherTest, smoke) {
|
|
|
|
int intercept_result;
|
|
|
|
unique_fd output_fd;
|
2017-02-17 10:39:15 +01:00
|
|
|
StartProcess([]() {
|
|
|
|
*reinterpret_cast<volatile char*>(0xdead) = '1';
|
|
|
|
});
|
|
|
|
|
2016-10-19 03:17:52 +02:00
|
|
|
StartIntercept(&output_fd);
|
|
|
|
FinishCrasher();
|
|
|
|
AssertDeath(SIGSEGV);
|
|
|
|
FinishIntercept(&intercept_result);
|
|
|
|
|
|
|
|
ASSERT_EQ(1, intercept_result) << "tombstoned reported failure";
|
|
|
|
|
|
|
|
std::string result;
|
|
|
|
ConsumeFd(std::move(output_fd), &result);
|
|
|
|
ASSERT_MATCH(result, R"(signal 11 \(SIGSEGV\), code 1 \(SEGV_MAPERR\), fault addr 0xdead)");
|
2020-09-15 05:27:36 +02:00
|
|
|
|
|
|
|
if (mte_supported()) {
|
|
|
|
// Test that the default TAGGED_ADDR_CTRL value is set.
|
2021-02-03 04:00:45 +01:00
|
|
|
ASSERT_MATCH(result, R"(tagged_addr_ctrl: 000000000007fff3)");
|
2020-09-15 05:27:36 +02:00
|
|
|
}
|
2016-10-19 03:17:52 +02:00
|
|
|
}
|
|
|
|
|
2020-03-21 02:09:00 +01:00
|
|
|
TEST_F(CrasherTest, tagged_fault_addr) {
|
|
|
|
#if !defined(__aarch64__)
|
|
|
|
GTEST_SKIP() << "Requires aarch64";
|
|
|
|
#endif
|
|
|
|
int intercept_result;
|
|
|
|
unique_fd output_fd;
|
|
|
|
StartProcess([]() {
|
|
|
|
*reinterpret_cast<volatile char*>(0x100000000000dead) = '1';
|
|
|
|
});
|
|
|
|
|
|
|
|
StartIntercept(&output_fd);
|
|
|
|
FinishCrasher();
|
|
|
|
AssertDeath(SIGSEGV);
|
|
|
|
FinishIntercept(&intercept_result);
|
|
|
|
|
|
|
|
ASSERT_EQ(1, intercept_result) << "tombstoned reported failure";
|
|
|
|
|
|
|
|
std::string result;
|
|
|
|
ConsumeFd(std::move(output_fd), &result);
|
|
|
|
|
|
|
|
// The address can either be tagged (new kernels) or untagged (old kernels).
|
|
|
|
ASSERT_MATCH(
|
|
|
|
result,
|
|
|
|
R"(signal 11 \(SIGSEGV\), code 1 \(SEGV_MAPERR\), fault addr (0x100000000000dead|0xdead))");
|
|
|
|
}
|
|
|
|
|
2020-07-17 23:49:31 +02:00
|
|
|
// Marked as weak to prevent the compiler from removing the malloc in the caller. In theory, the
|
|
|
|
// compiler could still clobber the argument register before trapping, but that's unlikely.
|
|
|
|
__attribute__((weak)) void CrasherTest::Trap(void* ptr ATTRIBUTE_UNUSED) {
|
|
|
|
__builtin_trap();
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(CrasherTest, heap_addr_in_register) {
|
|
|
|
#if defined(__i386__)
|
|
|
|
GTEST_SKIP() << "architecture does not pass arguments in registers";
|
|
|
|
#endif
|
|
|
|
int intercept_result;
|
|
|
|
unique_fd output_fd;
|
|
|
|
StartProcess([]() {
|
|
|
|
// Crash with a heap pointer in the first argument register.
|
|
|
|
Trap(malloc(1));
|
|
|
|
});
|
|
|
|
|
|
|
|
StartIntercept(&output_fd);
|
|
|
|
FinishCrasher();
|
|
|
|
int status;
|
|
|
|
ASSERT_EQ(crasher_pid, TIMEOUT(30, waitpid(crasher_pid, &status, 0)));
|
|
|
|
ASSERT_TRUE(WIFSIGNALED(status)) << "crasher didn't terminate via a signal";
|
|
|
|
// Don't test the signal number because different architectures use different signals for
|
|
|
|
// __builtin_trap().
|
|
|
|
FinishIntercept(&intercept_result);
|
|
|
|
|
|
|
|
ASSERT_EQ(1, intercept_result) << "tombstoned reported failure";
|
|
|
|
|
|
|
|
std::string result;
|
|
|
|
ConsumeFd(std::move(output_fd), &result);
|
|
|
|
|
|
|
|
#if defined(__aarch64__)
|
2021-02-05 23:59:08 +01:00
|
|
|
ASSERT_MATCH(result, "memory near x0 \\(\\[anon:");
|
2020-07-17 23:49:31 +02:00
|
|
|
#elif defined(__arm__)
|
2021-02-05 23:59:08 +01:00
|
|
|
ASSERT_MATCH(result, "memory near r0 \\(\\[anon:");
|
2020-07-17 23:49:31 +02:00
|
|
|
#elif defined(__x86_64__)
|
2021-02-05 23:59:08 +01:00
|
|
|
ASSERT_MATCH(result, "memory near rdi \\(\\[anon:");
|
2020-07-17 23:49:31 +02:00
|
|
|
#else
|
|
|
|
ASSERT_TRUE(false) << "unsupported architecture";
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2020-12-21 23:08:38 +01:00
|
|
|
#if defined(__aarch64__)
|
2020-04-07 23:07:32 +02:00
|
|
|
static void SetTagCheckingLevelSync() {
|
2021-01-15 20:34:26 +01:00
|
|
|
if (mallopt(M_BIONIC_SET_HEAP_TAGGING_LEVEL, M_HEAP_TAGGING_LEVEL_SYNC) == 0) {
|
2020-04-07 23:07:32 +02:00
|
|
|
abort();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2021-01-07 06:02:19 +01:00
|
|
|
struct SizeParamCrasherTest : CrasherTest, testing::WithParamInterface<size_t> {};
|
|
|
|
|
|
|
|
INSTANTIATE_TEST_SUITE_P(Sizes, SizeParamCrasherTest, testing::Values(16, 131072));
|
|
|
|
|
|
|
|
TEST_P(SizeParamCrasherTest, mte_uaf) {
|
2020-12-21 23:08:38 +01:00
|
|
|
#if defined(__aarch64__)
|
2020-04-07 23:07:32 +02:00
|
|
|
if (!mte_supported()) {
|
|
|
|
GTEST_SKIP() << "Requires MTE";
|
|
|
|
}
|
|
|
|
|
|
|
|
int intercept_result;
|
|
|
|
unique_fd output_fd;
|
2021-01-07 06:02:19 +01:00
|
|
|
StartProcess([&]() {
|
2020-04-07 23:07:32 +02:00
|
|
|
SetTagCheckingLevelSync();
|
2021-01-07 06:02:19 +01:00
|
|
|
volatile int* p = (volatile int*)malloc(GetParam());
|
2020-04-07 23:07:32 +02:00
|
|
|
free((void *)p);
|
|
|
|
p[0] = 42;
|
|
|
|
});
|
|
|
|
|
|
|
|
StartIntercept(&output_fd);
|
|
|
|
FinishCrasher();
|
|
|
|
AssertDeath(SIGSEGV);
|
|
|
|
FinishIntercept(&intercept_result);
|
|
|
|
|
|
|
|
ASSERT_EQ(1, intercept_result) << "tombstoned reported failure";
|
|
|
|
|
|
|
|
std::string result;
|
|
|
|
ConsumeFd(std::move(output_fd), &result);
|
|
|
|
|
2021-01-07 06:02:19 +01:00
|
|
|
ASSERT_MATCH(result, R"(signal 11 \(SIGSEGV\))");
|
|
|
|
ASSERT_MATCH(result, R"(Cause: \[MTE\]: Use After Free, 0 bytes into a )" +
|
|
|
|
std::to_string(GetParam()) + R"(-byte allocation.*
|
2020-05-08 19:11:19 +02:00
|
|
|
|
|
|
|
allocated by thread .*
|
|
|
|
#00 pc)");
|
|
|
|
ASSERT_MATCH(result, R"(deallocated by thread .*
|
|
|
|
#00 pc)");
|
2020-04-07 23:07:32 +02:00
|
|
|
#else
|
2020-12-21 23:08:38 +01:00
|
|
|
GTEST_SKIP() << "Requires aarch64";
|
2020-04-07 23:07:32 +02:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2021-01-07 06:02:19 +01:00
|
|
|
TEST_P(SizeParamCrasherTest, mte_overflow) {
|
2020-12-21 23:08:38 +01:00
|
|
|
#if defined(__aarch64__)
|
2020-04-07 23:07:32 +02:00
|
|
|
if (!mte_supported()) {
|
|
|
|
GTEST_SKIP() << "Requires MTE";
|
|
|
|
}
|
|
|
|
|
|
|
|
int intercept_result;
|
|
|
|
unique_fd output_fd;
|
2021-01-07 06:02:19 +01:00
|
|
|
StartProcess([&]() {
|
2020-04-07 23:07:32 +02:00
|
|
|
SetTagCheckingLevelSync();
|
2021-01-07 06:02:19 +01:00
|
|
|
volatile char* p = (volatile char*)malloc(GetParam());
|
|
|
|
p[GetParam()] = 42;
|
2020-04-07 23:07:32 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
StartIntercept(&output_fd);
|
|
|
|
FinishCrasher();
|
|
|
|
AssertDeath(SIGSEGV);
|
|
|
|
FinishIntercept(&intercept_result);
|
|
|
|
|
|
|
|
ASSERT_EQ(1, intercept_result) << "tombstoned reported failure";
|
|
|
|
|
|
|
|
std::string result;
|
|
|
|
ConsumeFd(std::move(output_fd), &result);
|
|
|
|
|
|
|
|
ASSERT_MATCH(result, R"(signal 11 \(SIGSEGV\))");
|
2021-01-07 06:02:19 +01:00
|
|
|
ASSERT_MATCH(result, R"(Cause: \[MTE\]: Buffer Overflow, 0 bytes right of a )" +
|
|
|
|
std::to_string(GetParam()) + R"(-byte allocation.*
|
2020-05-08 19:11:19 +02:00
|
|
|
|
|
|
|
allocated by thread .*
|
|
|
|
#00 pc)");
|
2020-04-07 23:07:32 +02:00
|
|
|
#else
|
2020-12-21 23:08:38 +01:00
|
|
|
GTEST_SKIP() << "Requires aarch64";
|
2020-04-07 23:07:32 +02:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2021-01-07 06:02:19 +01:00
|
|
|
TEST_P(SizeParamCrasherTest, mte_underflow) {
|
2020-12-21 23:08:38 +01:00
|
|
|
#if defined(__aarch64__)
|
2020-04-07 23:07:32 +02:00
|
|
|
if (!mte_supported()) {
|
|
|
|
GTEST_SKIP() << "Requires MTE";
|
|
|
|
}
|
|
|
|
|
|
|
|
int intercept_result;
|
|
|
|
unique_fd output_fd;
|
2021-01-07 06:02:19 +01:00
|
|
|
StartProcess([&]() {
|
2020-04-07 23:07:32 +02:00
|
|
|
SetTagCheckingLevelSync();
|
2021-01-07 06:02:19 +01:00
|
|
|
volatile int* p = (volatile int*)malloc(GetParam());
|
2020-04-07 23:07:32 +02:00
|
|
|
p[-1] = 42;
|
|
|
|
});
|
|
|
|
|
|
|
|
StartIntercept(&output_fd);
|
|
|
|
FinishCrasher();
|
|
|
|
AssertDeath(SIGSEGV);
|
|
|
|
FinishIntercept(&intercept_result);
|
|
|
|
|
|
|
|
ASSERT_EQ(1, intercept_result) << "tombstoned reported failure";
|
|
|
|
|
|
|
|
std::string result;
|
|
|
|
ConsumeFd(std::move(output_fd), &result);
|
|
|
|
|
|
|
|
ASSERT_MATCH(result, R"(signal 11 \(SIGSEGV\), code 9 \(SEGV_MTESERR\))");
|
2021-01-07 06:02:19 +01:00
|
|
|
ASSERT_MATCH(result, R"(Cause: \[MTE\]: Buffer Underflow, 4 bytes left of a )" +
|
|
|
|
std::to_string(GetParam()) + R"(-byte allocation.*
|
2020-05-08 19:11:19 +02:00
|
|
|
|
|
|
|
allocated by thread .*
|
|
|
|
#00 pc)");
|
2020-04-07 23:07:32 +02:00
|
|
|
#else
|
2020-12-21 23:08:38 +01:00
|
|
|
GTEST_SKIP() << "Requires aarch64";
|
2020-04-07 23:07:32 +02:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(CrasherTest, mte_multiple_causes) {
|
2020-12-21 23:08:38 +01:00
|
|
|
#if defined(__aarch64__)
|
2020-04-07 23:07:32 +02:00
|
|
|
if (!mte_supported()) {
|
|
|
|
GTEST_SKIP() << "Requires MTE";
|
|
|
|
}
|
|
|
|
|
|
|
|
int intercept_result;
|
|
|
|
unique_fd output_fd;
|
|
|
|
StartProcess([]() {
|
|
|
|
SetTagCheckingLevelSync();
|
|
|
|
|
|
|
|
// Make two allocations with the same tag and close to one another. Check for both properties
|
|
|
|
// with a bounds check -- this relies on the fact that only if the allocations have the same tag
|
|
|
|
// would they be measured as closer than 128 bytes to each other. Otherwise they would be about
|
|
|
|
// (some non-zero value << 56) apart.
|
|
|
|
//
|
|
|
|
// The out-of-bounds access will be considered either an overflow of one or an underflow of the
|
|
|
|
// other.
|
|
|
|
std::set<uintptr_t> allocs;
|
|
|
|
for (int i = 0; i != 4096; ++i) {
|
|
|
|
uintptr_t alloc = reinterpret_cast<uintptr_t>(malloc(16));
|
|
|
|
auto it = allocs.insert(alloc).first;
|
|
|
|
if (it != allocs.begin() && *std::prev(it) + 128 > alloc) {
|
|
|
|
*reinterpret_cast<int*>(*std::prev(it) + 16) = 42;
|
|
|
|
}
|
|
|
|
if (std::next(it) != allocs.end() && alloc + 128 > *std::next(it)) {
|
|
|
|
*reinterpret_cast<int*>(alloc + 16) = 42;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
StartIntercept(&output_fd);
|
|
|
|
FinishCrasher();
|
|
|
|
AssertDeath(SIGSEGV);
|
|
|
|
FinishIntercept(&intercept_result);
|
|
|
|
|
|
|
|
ASSERT_EQ(1, intercept_result) << "tombstoned reported failure";
|
|
|
|
|
|
|
|
std::string result;
|
|
|
|
ConsumeFd(std::move(output_fd), &result);
|
|
|
|
|
|
|
|
ASSERT_MATCH(result, R"(signal 11 \(SIGSEGV\))");
|
|
|
|
ASSERT_MATCH(
|
|
|
|
result,
|
|
|
|
R"(Note: multiple potential causes for this crash were detected, listing them in decreasing order of probability.)");
|
|
|
|
|
|
|
|
// Adjacent untracked allocations may cause us to see the wrong underflow here (or only
|
|
|
|
// overflows), so we can't match explicitly for an underflow message.
|
|
|
|
ASSERT_MATCH(result, R"(Cause: \[MTE\]: Buffer Overflow, 0 bytes right of a 16-byte allocation)");
|
|
|
|
#else
|
2020-12-21 23:08:38 +01:00
|
|
|
GTEST_SKIP() << "Requires aarch64";
|
2020-04-07 23:07:32 +02:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2020-12-21 23:08:38 +01:00
|
|
|
#if defined(__aarch64__)
|
2020-07-21 00:08:52 +02:00
|
|
|
static uintptr_t CreateTagMapping() {
|
|
|
|
uintptr_t mapping =
|
|
|
|
reinterpret_cast<uintptr_t>(mmap(nullptr, getpagesize(), PROT_READ | PROT_WRITE | PROT_MTE,
|
|
|
|
MAP_PRIVATE | MAP_ANONYMOUS, -1, 0));
|
|
|
|
if (reinterpret_cast<void*>(mapping) == MAP_FAILED) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
__asm__ __volatile__(".arch_extension mte; stg %0, [%0]"
|
|
|
|
:
|
|
|
|
: "r"(mapping + (1ULL << 56))
|
|
|
|
: "memory");
|
|
|
|
return mapping;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
TEST_F(CrasherTest, mte_tag_dump) {
|
2020-12-21 23:08:38 +01:00
|
|
|
#if defined(__aarch64__)
|
2020-07-21 00:08:52 +02:00
|
|
|
if (!mte_supported()) {
|
|
|
|
GTEST_SKIP() << "Requires MTE";
|
|
|
|
}
|
|
|
|
|
|
|
|
int intercept_result;
|
|
|
|
unique_fd output_fd;
|
|
|
|
StartProcess([&]() {
|
|
|
|
SetTagCheckingLevelSync();
|
|
|
|
Trap(reinterpret_cast<void *>(CreateTagMapping()));
|
|
|
|
});
|
|
|
|
|
|
|
|
StartIntercept(&output_fd);
|
|
|
|
FinishCrasher();
|
|
|
|
AssertDeath(SIGTRAP);
|
|
|
|
FinishIntercept(&intercept_result);
|
|
|
|
|
|
|
|
ASSERT_EQ(1, intercept_result) << "tombstoned reported failure";
|
|
|
|
|
|
|
|
std::string result;
|
|
|
|
ConsumeFd(std::move(output_fd), &result);
|
|
|
|
|
|
|
|
ASSERT_MATCH(result, R"(memory near x0:
|
|
|
|
.*
|
|
|
|
.*
|
|
|
|
01.............0 0000000000000000 0000000000000000 ................
|
|
|
|
00.............0)");
|
|
|
|
#else
|
2020-12-21 23:08:38 +01:00
|
|
|
GTEST_SKIP() << "Requires aarch64";
|
2020-07-21 00:08:52 +02:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2017-11-01 23:00:40 +01:00
|
|
|
TEST_F(CrasherTest, LD_PRELOAD) {
|
|
|
|
int intercept_result;
|
|
|
|
unique_fd output_fd;
|
|
|
|
StartProcess([]() {
|
|
|
|
setenv("LD_PRELOAD", "nonexistent.so", 1);
|
|
|
|
*reinterpret_cast<volatile char*>(0xdead) = '1';
|
|
|
|
});
|
|
|
|
|
|
|
|
StartIntercept(&output_fd);
|
|
|
|
FinishCrasher();
|
|
|
|
AssertDeath(SIGSEGV);
|
|
|
|
FinishIntercept(&intercept_result);
|
|
|
|
|
|
|
|
ASSERT_EQ(1, intercept_result) << "tombstoned reported failure";
|
|
|
|
|
|
|
|
std::string result;
|
|
|
|
ConsumeFd(std::move(output_fd), &result);
|
|
|
|
ASSERT_MATCH(result, R"(signal 11 \(SIGSEGV\), code 1 \(SEGV_MAPERR\), fault addr 0xdead)");
|
|
|
|
}
|
|
|
|
|
2016-10-19 03:17:52 +02:00
|
|
|
TEST_F(CrasherTest, abort) {
|
|
|
|
int intercept_result;
|
|
|
|
unique_fd output_fd;
|
2017-02-17 10:39:15 +01:00
|
|
|
StartProcess([]() {
|
|
|
|
abort();
|
|
|
|
});
|
2016-10-19 03:17:52 +02:00
|
|
|
StartIntercept(&output_fd);
|
|
|
|
FinishCrasher();
|
|
|
|
AssertDeath(SIGABRT);
|
|
|
|
FinishIntercept(&intercept_result);
|
|
|
|
|
|
|
|
ASSERT_EQ(1, intercept_result) << "tombstoned reported failure";
|
|
|
|
|
|
|
|
std::string result;
|
|
|
|
ConsumeFd(std::move(output_fd), &result);
|
2017-06-23 05:14:43 +02:00
|
|
|
ASSERT_BACKTRACE_FRAME(result, "abort");
|
2016-10-19 03:17:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(CrasherTest, signal) {
|
|
|
|
int intercept_result;
|
|
|
|
unique_fd output_fd;
|
2017-02-17 10:39:15 +01:00
|
|
|
StartProcess([]() {
|
2017-08-21 23:31:17 +02:00
|
|
|
while (true) {
|
|
|
|
sleep(1);
|
|
|
|
}
|
2017-02-17 10:39:15 +01:00
|
|
|
});
|
2016-10-19 03:17:52 +02:00
|
|
|
StartIntercept(&output_fd);
|
2017-08-21 23:31:17 +02:00
|
|
|
FinishCrasher();
|
2016-10-19 03:17:52 +02:00
|
|
|
ASSERT_EQ(0, kill(crasher_pid, SIGSEGV));
|
|
|
|
|
|
|
|
AssertDeath(SIGSEGV);
|
|
|
|
FinishIntercept(&intercept_result);
|
|
|
|
|
|
|
|
ASSERT_EQ(1, intercept_result) << "tombstoned reported failure";
|
|
|
|
|
|
|
|
std::string result;
|
|
|
|
ConsumeFd(std::move(output_fd), &result);
|
2018-05-02 19:47:00 +02:00
|
|
|
ASSERT_MATCH(
|
|
|
|
result,
|
|
|
|
R"(signal 11 \(SIGSEGV\), code 0 \(SI_USER from pid \d+, uid \d+\), fault addr --------)");
|
2016-10-19 03:17:52 +02:00
|
|
|
ASSERT_MATCH(result, R"(backtrace:)");
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(CrasherTest, abort_message) {
|
|
|
|
int intercept_result;
|
|
|
|
unique_fd output_fd;
|
2017-02-17 10:39:15 +01:00
|
|
|
StartProcess([]() {
|
2018-02-13 22:16:17 +01:00
|
|
|
// Arrived at experimentally;
|
|
|
|
// logd truncates at 4062.
|
|
|
|
// strlen("Abort message: ''") is 17.
|
|
|
|
// That's 4045, but we also want a NUL.
|
|
|
|
char buf[4045 + 1];
|
|
|
|
memset(buf, 'x', sizeof(buf));
|
|
|
|
buf[sizeof(buf) - 1] = '\0';
|
|
|
|
android_set_abort_message(buf);
|
2017-02-17 10:39:15 +01:00
|
|
|
abort();
|
|
|
|
});
|
2016-10-19 03:17:52 +02:00
|
|
|
StartIntercept(&output_fd);
|
|
|
|
FinishCrasher();
|
|
|
|
AssertDeath(SIGABRT);
|
|
|
|
FinishIntercept(&intercept_result);
|
|
|
|
|
|
|
|
ASSERT_EQ(1, intercept_result) << "tombstoned reported failure";
|
|
|
|
|
|
|
|
std::string result;
|
|
|
|
ConsumeFd(std::move(output_fd), &result);
|
2018-02-13 22:16:17 +01:00
|
|
|
ASSERT_MATCH(result, R"(Abort message: 'x{4045}')");
|
2016-10-19 03:17:52 +02:00
|
|
|
}
|
|
|
|
|
2017-04-28 01:50:38 +02:00
|
|
|
TEST_F(CrasherTest, abort_message_backtrace) {
|
|
|
|
int intercept_result;
|
|
|
|
unique_fd output_fd;
|
|
|
|
StartProcess([]() {
|
|
|
|
android_set_abort_message("not actually aborting");
|
2019-12-13 23:11:04 +01:00
|
|
|
raise(BIONIC_SIGNAL_DEBUGGER);
|
2017-04-28 01:50:38 +02:00
|
|
|
exit(0);
|
|
|
|
});
|
|
|
|
StartIntercept(&output_fd);
|
|
|
|
FinishCrasher();
|
|
|
|
AssertDeath(0);
|
|
|
|
FinishIntercept(&intercept_result);
|
|
|
|
|
|
|
|
ASSERT_EQ(1, intercept_result) << "tombstoned reported failure";
|
|
|
|
|
|
|
|
std::string result;
|
|
|
|
ConsumeFd(std::move(output_fd), &result);
|
|
|
|
ASSERT_NOT_MATCH(result, R"(Abort message:)");
|
|
|
|
}
|
|
|
|
|
2016-10-19 03:17:52 +02:00
|
|
|
TEST_F(CrasherTest, intercept_timeout) {
|
|
|
|
int intercept_result;
|
|
|
|
unique_fd output_fd;
|
2017-02-17 10:39:15 +01:00
|
|
|
StartProcess([]() {
|
|
|
|
abort();
|
|
|
|
});
|
2016-10-19 03:17:52 +02:00
|
|
|
StartIntercept(&output_fd);
|
|
|
|
|
|
|
|
// Don't let crasher finish until we timeout.
|
|
|
|
FinishIntercept(&intercept_result);
|
|
|
|
|
|
|
|
ASSERT_NE(1, intercept_result) << "tombstoned reported success? (intercept_result = "
|
|
|
|
<< intercept_result << ")";
|
|
|
|
|
|
|
|
FinishCrasher();
|
|
|
|
AssertDeath(SIGABRT);
|
|
|
|
}
|
|
|
|
|
2021-03-17 17:15:15 +01:00
|
|
|
TEST_F(CrasherTest, wait_for_debugger) {
|
|
|
|
if (!android::base::SetProperty(kWaitForDebuggerKey, "1")) {
|
|
|
|
FAIL() << "failed to enable wait_for_debugger";
|
2016-10-19 03:17:52 +02:00
|
|
|
}
|
|
|
|
sleep(1);
|
|
|
|
|
2017-02-17 10:39:15 +01:00
|
|
|
StartProcess([]() {
|
|
|
|
abort();
|
|
|
|
});
|
2016-10-19 03:17:52 +02:00
|
|
|
FinishCrasher();
|
|
|
|
|
|
|
|
int status;
|
2019-09-19 02:48:30 +02:00
|
|
|
ASSERT_EQ(crasher_pid, TEMP_FAILURE_RETRY(waitpid(crasher_pid, &status, WUNTRACED)));
|
2016-10-19 03:17:52 +02:00
|
|
|
ASSERT_TRUE(WIFSTOPPED(status));
|
|
|
|
ASSERT_EQ(SIGSTOP, WSTOPSIG(status));
|
|
|
|
|
|
|
|
ASSERT_EQ(0, kill(crasher_pid, SIGCONT));
|
|
|
|
|
|
|
|
AssertDeath(SIGABRT);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(CrasherTest, backtrace) {
|
|
|
|
std::string result;
|
|
|
|
int intercept_result;
|
|
|
|
unique_fd output_fd;
|
2017-02-17 10:39:15 +01:00
|
|
|
|
|
|
|
StartProcess([]() {
|
|
|
|
abort();
|
|
|
|
});
|
2017-05-24 16:07:25 +02:00
|
|
|
StartIntercept(&output_fd, kDebuggerdNativeBacktrace);
|
2016-10-19 03:17:52 +02:00
|
|
|
|
|
|
|
std::this_thread::sleep_for(500ms);
|
|
|
|
|
|
|
|
sigval val;
|
|
|
|
val.sival_int = 1;
|
2019-12-13 23:11:04 +01:00
|
|
|
ASSERT_EQ(0, sigqueue(crasher_pid, BIONIC_SIGNAL_DEBUGGER, val)) << strerror(errno);
|
2016-10-19 03:17:52 +02:00
|
|
|
FinishIntercept(&intercept_result);
|
|
|
|
ASSERT_EQ(1, intercept_result) << "tombstoned reported failure";
|
|
|
|
ConsumeFd(std::move(output_fd), &result);
|
2017-06-15 11:20:34 +02:00
|
|
|
ASSERT_BACKTRACE_FRAME(result, "read");
|
2016-10-19 03:17:52 +02:00
|
|
|
|
|
|
|
int status;
|
|
|
|
ASSERT_EQ(0, waitpid(crasher_pid, &status, WNOHANG | WUNTRACED));
|
|
|
|
|
|
|
|
StartIntercept(&output_fd);
|
|
|
|
FinishCrasher();
|
|
|
|
AssertDeath(SIGABRT);
|
|
|
|
FinishIntercept(&intercept_result);
|
|
|
|
ASSERT_EQ(1, intercept_result) << "tombstoned reported failure";
|
|
|
|
ConsumeFd(std::move(output_fd), &result);
|
2017-06-23 05:14:43 +02:00
|
|
|
ASSERT_BACKTRACE_FRAME(result, "abort");
|
2016-10-19 03:17:52 +02:00
|
|
|
}
|
2017-01-23 21:05:35 +01:00
|
|
|
|
|
|
|
TEST_F(CrasherTest, PR_SET_DUMPABLE_0_crash) {
|
2017-02-17 10:39:15 +01:00
|
|
|
int intercept_result;
|
|
|
|
unique_fd output_fd;
|
2017-01-23 21:05:35 +01:00
|
|
|
StartProcess([]() {
|
|
|
|
prctl(PR_SET_DUMPABLE, 0);
|
2017-02-17 10:39:15 +01:00
|
|
|
abort();
|
2017-01-23 21:05:35 +01:00
|
|
|
});
|
2017-02-17 10:39:15 +01:00
|
|
|
|
|
|
|
StartIntercept(&output_fd);
|
|
|
|
FinishCrasher();
|
|
|
|
AssertDeath(SIGABRT);
|
|
|
|
FinishIntercept(&intercept_result);
|
|
|
|
|
|
|
|
ASSERT_EQ(1, intercept_result) << "tombstoned reported failure";
|
|
|
|
|
|
|
|
std::string result;
|
|
|
|
ConsumeFd(std::move(output_fd), &result);
|
2017-06-23 05:14:43 +02:00
|
|
|
ASSERT_BACKTRACE_FRAME(result, "abort");
|
2017-01-23 21:05:35 +01:00
|
|
|
}
|
|
|
|
|
2017-02-17 10:39:15 +01:00
|
|
|
TEST_F(CrasherTest, capabilities) {
|
|
|
|
ASSERT_EQ(0U, getuid()) << "capability test requires root";
|
|
|
|
|
2017-01-23 21:05:35 +01:00
|
|
|
StartProcess([]() {
|
2017-02-17 10:39:15 +01:00
|
|
|
if (prctl(PR_SET_KEEPCAPS, 1, 0, 0, 0) != 0) {
|
|
|
|
err(1, "failed to set PR_SET_KEEPCAPS");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (setresuid(1, 1, 1) != 0) {
|
|
|
|
err(1, "setresuid failed");
|
|
|
|
}
|
|
|
|
|
|
|
|
__user_cap_header_struct capheader;
|
|
|
|
__user_cap_data_struct capdata[2];
|
|
|
|
memset(&capheader, 0, sizeof(capheader));
|
|
|
|
memset(&capdata, 0, sizeof(capdata));
|
|
|
|
|
|
|
|
capheader.version = _LINUX_CAPABILITY_VERSION_3;
|
|
|
|
capheader.pid = 0;
|
|
|
|
|
|
|
|
// Turn on every third capability.
|
|
|
|
static_assert(CAP_LAST_CAP > 33, "CAP_LAST_CAP <= 32");
|
|
|
|
for (int i = 0; i < CAP_LAST_CAP; i += 3) {
|
|
|
|
capdata[CAP_TO_INDEX(i)].permitted |= CAP_TO_MASK(i);
|
|
|
|
capdata[CAP_TO_INDEX(i)].effective |= CAP_TO_MASK(i);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure CAP_SYS_PTRACE is off.
|
|
|
|
capdata[CAP_TO_INDEX(CAP_SYS_PTRACE)].permitted &= ~(CAP_TO_MASK(CAP_SYS_PTRACE));
|
|
|
|
capdata[CAP_TO_INDEX(CAP_SYS_PTRACE)].effective &= ~(CAP_TO_MASK(CAP_SYS_PTRACE));
|
|
|
|
|
|
|
|
if (capset(&capheader, &capdata[0]) != 0) {
|
|
|
|
err(1, "capset failed");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_CLEAR_ALL, 0, 0, 0) != 0) {
|
|
|
|
err(1, "failed to drop ambient capabilities");
|
|
|
|
}
|
|
|
|
|
2017-04-03 22:18:34 +02:00
|
|
|
pthread_setname_np(pthread_self(), "thread_name");
|
2017-02-17 10:39:15 +01:00
|
|
|
raise(SIGSYS);
|
2017-01-23 21:05:35 +01:00
|
|
|
});
|
2017-02-17 10:39:15 +01:00
|
|
|
|
|
|
|
unique_fd output_fd;
|
|
|
|
StartIntercept(&output_fd);
|
|
|
|
FinishCrasher();
|
|
|
|
AssertDeath(SIGSYS);
|
|
|
|
|
|
|
|
std::string result;
|
|
|
|
int intercept_result;
|
|
|
|
FinishIntercept(&intercept_result);
|
|
|
|
ASSERT_EQ(1, intercept_result) << "tombstoned reported failure";
|
|
|
|
ConsumeFd(std::move(output_fd), &result);
|
2017-04-03 22:18:34 +02:00
|
|
|
ASSERT_MATCH(result, R"(name: thread_name\s+>>> .+debuggerd_test(32|64) <<<)");
|
2017-06-15 11:20:34 +02:00
|
|
|
ASSERT_BACKTRACE_FRAME(result, "tgkill");
|
2017-01-23 21:05:35 +01:00
|
|
|
}
|
2017-02-14 01:36:18 +01:00
|
|
|
|
2017-05-05 02:12:57 +02:00
|
|
|
TEST_F(CrasherTest, fake_pid) {
|
|
|
|
int intercept_result;
|
|
|
|
unique_fd output_fd;
|
|
|
|
|
|
|
|
// Prime the getpid/gettid caches.
|
|
|
|
UNUSED(getpid());
|
|
|
|
UNUSED(gettid());
|
|
|
|
|
|
|
|
std::function<pid_t()> clone_fn = []() {
|
|
|
|
return syscall(__NR_clone, SIGCHLD, nullptr, nullptr, nullptr, nullptr);
|
|
|
|
};
|
|
|
|
StartProcess(
|
|
|
|
[]() {
|
|
|
|
ASSERT_NE(getpid(), syscall(__NR_getpid));
|
|
|
|
ASSERT_NE(gettid(), syscall(__NR_gettid));
|
|
|
|
raise(SIGSEGV);
|
|
|
|
},
|
|
|
|
clone_fn);
|
|
|
|
|
|
|
|
StartIntercept(&output_fd);
|
|
|
|
FinishCrasher();
|
|
|
|
AssertDeath(SIGSEGV);
|
|
|
|
FinishIntercept(&intercept_result);
|
|
|
|
|
|
|
|
ASSERT_EQ(1, intercept_result) << "tombstoned reported failure";
|
|
|
|
|
|
|
|
std::string result;
|
|
|
|
ConsumeFd(std::move(output_fd), &result);
|
2017-06-15 11:20:34 +02:00
|
|
|
ASSERT_BACKTRACE_FRAME(result, "tgkill");
|
2017-05-05 02:12:57 +02:00
|
|
|
}
|
|
|
|
|
2018-01-17 00:38:17 +01:00
|
|
|
static const char* const kDebuggerdSeccompPolicy =
|
|
|
|
"/system/etc/seccomp_policy/crash_dump." ABI_STRING ".policy";
|
|
|
|
|
2018-02-22 20:38:33 +01:00
|
|
|
static pid_t seccomp_fork_impl(void (*prejail)()) {
|
2018-09-12 22:55:47 +02:00
|
|
|
std::string policy;
|
|
|
|
if (!android::base::ReadFileToString(kDebuggerdSeccompPolicy, &policy)) {
|
|
|
|
PLOG(FATAL) << "failed to read policy file";
|
|
|
|
}
|
|
|
|
|
|
|
|
// Allow a bunch of syscalls used by the tests.
|
|
|
|
policy += "\nclone: 1";
|
|
|
|
policy += "\nsigaltstack: 1";
|
|
|
|
policy += "\nnanosleep: 1";
|
2019-09-18 00:31:47 +02:00
|
|
|
policy += "\ngetrlimit: 1";
|
|
|
|
policy += "\nugetrlimit: 1";
|
2018-09-12 22:55:47 +02:00
|
|
|
|
|
|
|
FILE* tmp_file = tmpfile();
|
|
|
|
if (!tmp_file) {
|
|
|
|
PLOG(FATAL) << "tmpfile failed";
|
|
|
|
}
|
|
|
|
|
2019-09-19 02:48:30 +02:00
|
|
|
unique_fd tmp_fd(TEMP_FAILURE_RETRY(dup(fileno(tmp_file))));
|
2018-09-12 22:55:47 +02:00
|
|
|
if (!android::base::WriteStringToFd(policy, tmp_fd.get())) {
|
|
|
|
PLOG(FATAL) << "failed to write policy to tmpfile";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (lseek(tmp_fd.get(), 0, SEEK_SET) != 0) {
|
|
|
|
PLOG(FATAL) << "failed to seek tmp_fd";
|
2018-01-17 00:38:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ScopedMinijail jail{minijail_new()};
|
|
|
|
if (!jail) {
|
|
|
|
LOG(FATAL) << "failed to create minijail";
|
|
|
|
}
|
|
|
|
|
|
|
|
minijail_no_new_privs(jail.get());
|
|
|
|
minijail_log_seccomp_filter_failures(jail.get());
|
|
|
|
minijail_use_seccomp_filter(jail.get());
|
2018-09-12 22:55:47 +02:00
|
|
|
minijail_parse_seccomp_filters_from_fd(jail.get(), tmp_fd.release());
|
2018-01-17 00:38:17 +01:00
|
|
|
|
|
|
|
pid_t result = fork();
|
|
|
|
if (result == -1) {
|
|
|
|
return result;
|
|
|
|
} else if (result != 0) {
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Spawn and detach a thread that spins forever.
|
|
|
|
std::atomic<bool> thread_ready(false);
|
|
|
|
std::thread thread([&jail, &thread_ready]() {
|
|
|
|
minijail_enter(jail.get());
|
|
|
|
thread_ready = true;
|
|
|
|
for (;;)
|
|
|
|
;
|
|
|
|
});
|
|
|
|
thread.detach();
|
|
|
|
|
|
|
|
while (!thread_ready) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-02-22 20:38:33 +01:00
|
|
|
if (prejail) {
|
|
|
|
prejail();
|
|
|
|
}
|
|
|
|
|
2018-01-17 00:38:17 +01:00
|
|
|
minijail_enter(jail.get());
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2018-02-22 20:38:33 +01:00
|
|
|
static pid_t seccomp_fork() {
|
|
|
|
return seccomp_fork_impl(nullptr);
|
|
|
|
}
|
|
|
|
|
2018-01-17 00:38:17 +01:00
|
|
|
TEST_F(CrasherTest, seccomp_crash) {
|
|
|
|
int intercept_result;
|
|
|
|
unique_fd output_fd;
|
|
|
|
|
|
|
|
StartProcess([]() { abort(); }, &seccomp_fork);
|
|
|
|
|
|
|
|
StartIntercept(&output_fd);
|
|
|
|
FinishCrasher();
|
|
|
|
AssertDeath(SIGABRT);
|
|
|
|
FinishIntercept(&intercept_result);
|
|
|
|
ASSERT_EQ(1, intercept_result) << "tombstoned reported failure";
|
|
|
|
|
|
|
|
std::string result;
|
|
|
|
ConsumeFd(std::move(output_fd), &result);
|
|
|
|
ASSERT_BACKTRACE_FRAME(result, "abort");
|
|
|
|
}
|
|
|
|
|
2018-02-22 20:38:33 +01:00
|
|
|
static pid_t seccomp_fork_rlimit() {
|
|
|
|
return seccomp_fork_impl([]() {
|
|
|
|
struct rlimit rlim = {
|
|
|
|
.rlim_cur = 512 * 1024 * 1024,
|
|
|
|
.rlim_max = 512 * 1024 * 1024,
|
|
|
|
};
|
|
|
|
|
|
|
|
if (setrlimit(RLIMIT_AS, &rlim) != 0) {
|
|
|
|
raise(SIGINT);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(CrasherTest, seccomp_crash_oom) {
|
|
|
|
int intercept_result;
|
|
|
|
unique_fd output_fd;
|
|
|
|
|
|
|
|
StartProcess(
|
|
|
|
[]() {
|
|
|
|
std::vector<void*> vec;
|
|
|
|
for (int i = 0; i < 512; ++i) {
|
|
|
|
char* buf = static_cast<char*>(malloc(1024 * 1024));
|
|
|
|
if (!buf) {
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
memset(buf, 0xff, 1024 * 1024);
|
|
|
|
vec.push_back(buf);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
&seccomp_fork_rlimit);
|
|
|
|
|
|
|
|
StartIntercept(&output_fd);
|
|
|
|
FinishCrasher();
|
|
|
|
AssertDeath(SIGABRT);
|
|
|
|
FinishIntercept(&intercept_result);
|
|
|
|
ASSERT_EQ(1, intercept_result) << "tombstoned reported failure";
|
|
|
|
|
|
|
|
// We can't actually generate a backtrace, just make sure that the process terminates.
|
|
|
|
}
|
|
|
|
|
2018-01-17 00:38:17 +01:00
|
|
|
__attribute__((noinline)) extern "C" bool raise_debugger_signal(DebuggerdDumpType dump_type) {
|
|
|
|
siginfo_t siginfo;
|
|
|
|
siginfo.si_code = SI_QUEUE;
|
|
|
|
siginfo.si_pid = getpid();
|
|
|
|
siginfo.si_uid = getuid();
|
|
|
|
|
|
|
|
if (dump_type != kDebuggerdNativeBacktrace && dump_type != kDebuggerdTombstone) {
|
|
|
|
PLOG(FATAL) << "invalid dump type";
|
|
|
|
}
|
|
|
|
|
|
|
|
siginfo.si_value.sival_int = dump_type == kDebuggerdNativeBacktrace;
|
|
|
|
|
2019-12-13 23:11:04 +01:00
|
|
|
if (syscall(__NR_rt_tgsigqueueinfo, getpid(), gettid(), BIONIC_SIGNAL_DEBUGGER, &siginfo) != 0) {
|
2018-01-17 00:38:17 +01:00
|
|
|
PLOG(ERROR) << "libdebuggerd_client: failed to send signal to self";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(CrasherTest, seccomp_tombstone) {
|
|
|
|
int intercept_result;
|
|
|
|
unique_fd output_fd;
|
|
|
|
|
|
|
|
static const auto dump_type = kDebuggerdTombstone;
|
|
|
|
StartProcess(
|
|
|
|
[]() {
|
|
|
|
raise_debugger_signal(dump_type);
|
|
|
|
_exit(0);
|
|
|
|
},
|
|
|
|
&seccomp_fork);
|
|
|
|
|
|
|
|
StartIntercept(&output_fd, dump_type);
|
|
|
|
FinishCrasher();
|
|
|
|
AssertDeath(0);
|
|
|
|
FinishIntercept(&intercept_result);
|
|
|
|
ASSERT_EQ(1, intercept_result) << "tombstoned reported failure";
|
|
|
|
|
|
|
|
std::string result;
|
|
|
|
ConsumeFd(std::move(output_fd), &result);
|
|
|
|
ASSERT_BACKTRACE_FRAME(result, "raise_debugger_signal");
|
|
|
|
}
|
|
|
|
|
2018-09-12 22:55:47 +02:00
|
|
|
extern "C" void foo() {
|
|
|
|
LOG(INFO) << "foo";
|
|
|
|
std::this_thread::sleep_for(1s);
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" void bar() {
|
|
|
|
LOG(INFO) << "bar";
|
|
|
|
std::this_thread::sleep_for(1s);
|
|
|
|
}
|
|
|
|
|
2018-01-17 00:38:17 +01:00
|
|
|
TEST_F(CrasherTest, seccomp_backtrace) {
|
|
|
|
int intercept_result;
|
|
|
|
unique_fd output_fd;
|
|
|
|
|
|
|
|
static const auto dump_type = kDebuggerdNativeBacktrace;
|
|
|
|
StartProcess(
|
|
|
|
[]() {
|
2018-09-12 22:55:47 +02:00
|
|
|
std::thread a(foo);
|
|
|
|
std::thread b(bar);
|
|
|
|
|
|
|
|
std::this_thread::sleep_for(100ms);
|
|
|
|
|
2018-01-17 00:38:17 +01:00
|
|
|
raise_debugger_signal(dump_type);
|
|
|
|
_exit(0);
|
|
|
|
},
|
|
|
|
&seccomp_fork);
|
|
|
|
|
|
|
|
StartIntercept(&output_fd, dump_type);
|
|
|
|
FinishCrasher();
|
|
|
|
AssertDeath(0);
|
|
|
|
FinishIntercept(&intercept_result);
|
|
|
|
ASSERT_EQ(1, intercept_result) << "tombstoned reported failure";
|
|
|
|
|
|
|
|
std::string result;
|
|
|
|
ConsumeFd(std::move(output_fd), &result);
|
|
|
|
ASSERT_BACKTRACE_FRAME(result, "raise_debugger_signal");
|
2018-09-12 22:55:47 +02:00
|
|
|
ASSERT_BACKTRACE_FRAME(result, "foo");
|
|
|
|
ASSERT_BACKTRACE_FRAME(result, "bar");
|
2018-01-17 00:38:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(CrasherTest, seccomp_crash_logcat) {
|
|
|
|
StartProcess([]() { abort(); }, &seccomp_fork);
|
|
|
|
FinishCrasher();
|
|
|
|
|
|
|
|
// Make sure we don't get SIGSYS when trying to dump a crash to logcat.
|
|
|
|
AssertDeath(SIGABRT);
|
|
|
|
}
|
|
|
|
|
2017-08-19 00:37:26 +02:00
|
|
|
TEST_F(CrasherTest, competing_tracer) {
|
|
|
|
int intercept_result;
|
|
|
|
unique_fd output_fd;
|
|
|
|
StartProcess([]() {
|
2017-08-21 23:31:17 +02:00
|
|
|
raise(SIGABRT);
|
2017-08-19 00:37:26 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
StartIntercept(&output_fd);
|
|
|
|
|
|
|
|
ASSERT_EQ(0, ptrace(PTRACE_SEIZE, crasher_pid, 0, 0));
|
2017-08-21 23:31:17 +02:00
|
|
|
FinishCrasher();
|
2017-08-19 00:37:26 +02:00
|
|
|
|
|
|
|
int status;
|
2019-09-19 02:48:30 +02:00
|
|
|
ASSERT_EQ(crasher_pid, TEMP_FAILURE_RETRY(waitpid(crasher_pid, &status, 0)));
|
2017-08-19 00:37:26 +02:00
|
|
|
ASSERT_TRUE(WIFSTOPPED(status));
|
|
|
|
ASSERT_EQ(SIGABRT, WSTOPSIG(status));
|
|
|
|
|
|
|
|
ASSERT_EQ(0, ptrace(PTRACE_CONT, crasher_pid, 0, SIGABRT));
|
|
|
|
FinishIntercept(&intercept_result);
|
|
|
|
ASSERT_EQ(1, intercept_result) << "tombstoned reported failure";
|
|
|
|
|
|
|
|
std::string result;
|
|
|
|
ConsumeFd(std::move(output_fd), &result);
|
|
|
|
std::string regex = R"(failed to attach to thread \d+, already traced by )";
|
|
|
|
regex += std::to_string(gettid());
|
|
|
|
regex += R"( \(.+debuggerd_test)";
|
|
|
|
ASSERT_MATCH(result, regex.c_str());
|
|
|
|
|
2019-09-19 02:48:30 +02:00
|
|
|
ASSERT_EQ(crasher_pid, TEMP_FAILURE_RETRY(waitpid(crasher_pid, &status, 0)));
|
2017-08-21 23:31:17 +02:00
|
|
|
ASSERT_TRUE(WIFSTOPPED(status));
|
|
|
|
ASSERT_EQ(SIGABRT, WSTOPSIG(status));
|
|
|
|
|
2017-08-19 00:37:26 +02:00
|
|
|
ASSERT_EQ(0, ptrace(PTRACE_DETACH, crasher_pid, 0, SIGABRT));
|
|
|
|
AssertDeath(SIGABRT);
|
|
|
|
}
|
|
|
|
|
2018-08-28 01:34:01 +02:00
|
|
|
TEST_F(CrasherTest, fdsan_warning_abort_message) {
|
|
|
|
int intercept_result;
|
|
|
|
unique_fd output_fd;
|
|
|
|
|
|
|
|
StartProcess([]() {
|
|
|
|
android_fdsan_set_error_level(ANDROID_FDSAN_ERROR_LEVEL_WARN_ONCE);
|
2019-09-19 02:48:30 +02:00
|
|
|
unique_fd fd(TEMP_FAILURE_RETRY(open("/dev/null", O_RDONLY | O_CLOEXEC)));
|
2018-08-28 01:34:01 +02:00
|
|
|
if (fd == -1) {
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
close(fd.get());
|
|
|
|
_exit(0);
|
|
|
|
});
|
|
|
|
|
|
|
|
StartIntercept(&output_fd);
|
|
|
|
FinishCrasher();
|
|
|
|
AssertDeath(0);
|
|
|
|
FinishIntercept(&intercept_result);
|
|
|
|
ASSERT_EQ(1, intercept_result) << "tombstoned reported failure";
|
|
|
|
|
|
|
|
std::string result;
|
|
|
|
ConsumeFd(std::move(output_fd), &result);
|
|
|
|
ASSERT_MATCH(result, "Abort message: 'attempted to close");
|
|
|
|
}
|
|
|
|
|
2017-02-14 01:36:18 +01:00
|
|
|
TEST(crash_dump, zombie) {
|
|
|
|
pid_t forkpid = fork();
|
|
|
|
|
|
|
|
pid_t rc;
|
|
|
|
int status;
|
|
|
|
|
|
|
|
if (forkpid == 0) {
|
|
|
|
errno = 0;
|
|
|
|
rc = waitpid(-1, &status, WNOHANG | __WALL | __WNOTHREAD);
|
|
|
|
if (rc != -1 || errno != ECHILD) {
|
|
|
|
errx(2, "first waitpid returned %d (%s), expected failure with ECHILD", rc, strerror(errno));
|
|
|
|
}
|
|
|
|
|
2019-12-13 23:11:04 +01:00
|
|
|
raise(BIONIC_SIGNAL_DEBUGGER);
|
2017-02-14 01:36:18 +01:00
|
|
|
|
|
|
|
errno = 0;
|
2019-09-19 02:48:30 +02:00
|
|
|
rc = TEMP_FAILURE_RETRY(waitpid(-1, &status, __WALL | __WNOTHREAD));
|
2017-02-14 01:36:18 +01:00
|
|
|
if (rc != -1 || errno != ECHILD) {
|
|
|
|
errx(2, "second waitpid returned %d (%s), expected failure with ECHILD", rc, strerror(errno));
|
|
|
|
}
|
|
|
|
_exit(0);
|
|
|
|
} else {
|
2019-09-19 02:48:30 +02:00
|
|
|
rc = TEMP_FAILURE_RETRY(waitpid(forkpid, &status, 0));
|
2017-02-14 01:36:18 +01:00
|
|
|
ASSERT_EQ(forkpid, rc);
|
|
|
|
ASSERT_TRUE(WIFEXITED(status));
|
|
|
|
ASSERT_EQ(0, WEXITSTATUS(status));
|
|
|
|
}
|
|
|
|
}
|
2017-03-31 01:46:21 +02:00
|
|
|
|
|
|
|
TEST(tombstoned, no_notify) {
|
|
|
|
// Do this a few times.
|
|
|
|
for (int i = 0; i < 3; ++i) {
|
|
|
|
pid_t pid = 123'456'789 + i;
|
|
|
|
|
|
|
|
unique_fd intercept_fd, output_fd;
|
2017-06-02 16:42:06 +02:00
|
|
|
InterceptStatus status;
|
|
|
|
tombstoned_intercept(pid, &intercept_fd, &output_fd, &status, kDebuggerdTombstone);
|
|
|
|
ASSERT_EQ(InterceptStatus::kRegistered, status);
|
2017-03-31 01:46:21 +02:00
|
|
|
|
|
|
|
{
|
|
|
|
unique_fd tombstoned_socket, input_fd;
|
2017-05-24 16:07:25 +02:00
|
|
|
ASSERT_TRUE(tombstoned_connect(pid, &tombstoned_socket, &input_fd, kDebuggerdTombstone));
|
2017-03-31 01:46:21 +02:00
|
|
|
ASSERT_TRUE(android::base::WriteFully(input_fd.get(), &pid, sizeof(pid)));
|
|
|
|
}
|
|
|
|
|
|
|
|
pid_t read_pid;
|
|
|
|
ASSERT_TRUE(android::base::ReadFully(output_fd.get(), &read_pid, sizeof(read_pid)));
|
|
|
|
ASSERT_EQ(read_pid, pid);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(tombstoned, stress) {
|
|
|
|
// Spawn threads to simultaneously do a bunch of failing dumps and a bunch of successful dumps.
|
|
|
|
static constexpr int kDumpCount = 100;
|
|
|
|
|
|
|
|
std::atomic<bool> start(false);
|
|
|
|
std::vector<std::thread> threads;
|
|
|
|
threads.emplace_back([&start]() {
|
|
|
|
while (!start) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Use a way out of range pid, to avoid stomping on an actual process.
|
|
|
|
pid_t pid_base = 1'000'000;
|
|
|
|
|
|
|
|
for (int dump = 0; dump < kDumpCount; ++dump) {
|
|
|
|
pid_t pid = pid_base + dump;
|
|
|
|
|
|
|
|
unique_fd intercept_fd, output_fd;
|
2017-06-02 16:42:06 +02:00
|
|
|
InterceptStatus status;
|
|
|
|
tombstoned_intercept(pid, &intercept_fd, &output_fd, &status, kDebuggerdTombstone);
|
|
|
|
ASSERT_EQ(InterceptStatus::kRegistered, status);
|
2017-03-31 01:46:21 +02:00
|
|
|
|
|
|
|
// Pretend to crash, and then immediately close the socket.
|
|
|
|
unique_fd sockfd(socket_local_client(kTombstonedCrashSocketName,
|
|
|
|
ANDROID_SOCKET_NAMESPACE_RESERVED, SOCK_SEQPACKET));
|
|
|
|
if (sockfd == -1) {
|
|
|
|
FAIL() << "failed to connect to tombstoned: " << strerror(errno);
|
|
|
|
}
|
|
|
|
TombstonedCrashPacket packet = {};
|
|
|
|
packet.packet_type = CrashPacketType::kDumpRequest;
|
|
|
|
packet.packet.dump_request.pid = pid;
|
|
|
|
if (TEMP_FAILURE_RETRY(write(sockfd, &packet, sizeof(packet))) != sizeof(packet)) {
|
|
|
|
FAIL() << "failed to write to tombstoned: " << strerror(errno);
|
|
|
|
}
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
threads.emplace_back([&start]() {
|
|
|
|
while (!start) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Use a way out of range pid, to avoid stomping on an actual process.
|
|
|
|
pid_t pid_base = 2'000'000;
|
|
|
|
|
|
|
|
for (int dump = 0; dump < kDumpCount; ++dump) {
|
|
|
|
pid_t pid = pid_base + dump;
|
|
|
|
|
|
|
|
unique_fd intercept_fd, output_fd;
|
2017-06-02 16:42:06 +02:00
|
|
|
InterceptStatus status;
|
|
|
|
tombstoned_intercept(pid, &intercept_fd, &output_fd, &status, kDebuggerdTombstone);
|
|
|
|
ASSERT_EQ(InterceptStatus::kRegistered, status);
|
2017-03-31 01:46:21 +02:00
|
|
|
|
|
|
|
{
|
|
|
|
unique_fd tombstoned_socket, input_fd;
|
2017-05-24 16:07:25 +02:00
|
|
|
ASSERT_TRUE(tombstoned_connect(pid, &tombstoned_socket, &input_fd, kDebuggerdTombstone));
|
2017-03-31 01:46:21 +02:00
|
|
|
ASSERT_TRUE(android::base::WriteFully(input_fd.get(), &pid, sizeof(pid)));
|
|
|
|
tombstoned_notify_completion(tombstoned_socket.get());
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Fix the race that requires this sleep.
|
|
|
|
std::this_thread::sleep_for(50ms);
|
|
|
|
|
|
|
|
pid_t read_pid;
|
|
|
|
ASSERT_TRUE(android::base::ReadFully(output_fd.get(), &read_pid, sizeof(read_pid)));
|
|
|
|
ASSERT_EQ(read_pid, pid);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
start = true;
|
|
|
|
|
|
|
|
for (std::thread& thread : threads) {
|
|
|
|
thread.join();
|
|
|
|
}
|
|
|
|
}
|
2017-06-02 16:42:06 +02:00
|
|
|
|
|
|
|
TEST(tombstoned, java_trace_intercept_smoke) {
|
|
|
|
// Using a "real" PID is a little dangerous here - if the test fails
|
|
|
|
// or crashes, we might end up getting a bogus / unreliable stack
|
|
|
|
// trace.
|
|
|
|
const pid_t self = getpid();
|
|
|
|
|
|
|
|
unique_fd intercept_fd, output_fd;
|
|
|
|
InterceptStatus status;
|
|
|
|
tombstoned_intercept(self, &intercept_fd, &output_fd, &status, kDebuggerdJavaBacktrace);
|
|
|
|
ASSERT_EQ(InterceptStatus::kRegistered, status);
|
|
|
|
|
2021-01-27 00:53:11 +01:00
|
|
|
// First connect to tombstoned requesting a native tombstone. This
|
2017-06-02 16:42:06 +02:00
|
|
|
// should result in a "regular" FD and not the installed intercept.
|
|
|
|
const char native[] = "native";
|
|
|
|
unique_fd tombstoned_socket, input_fd;
|
2021-01-27 00:53:11 +01:00
|
|
|
ASSERT_TRUE(tombstoned_connect(self, &tombstoned_socket, &input_fd, kDebuggerdTombstone));
|
2017-06-02 16:42:06 +02:00
|
|
|
ASSERT_TRUE(android::base::WriteFully(input_fd.get(), native, sizeof(native)));
|
|
|
|
tombstoned_notify_completion(tombstoned_socket.get());
|
|
|
|
|
|
|
|
// Then, connect to tombstoned asking for a java backtrace. This *should*
|
|
|
|
// trigger the intercept.
|
|
|
|
const char java[] = "java";
|
|
|
|
ASSERT_TRUE(tombstoned_connect(self, &tombstoned_socket, &input_fd, kDebuggerdJavaBacktrace));
|
|
|
|
ASSERT_TRUE(android::base::WriteFully(input_fd.get(), java, sizeof(java)));
|
|
|
|
tombstoned_notify_completion(tombstoned_socket.get());
|
|
|
|
|
|
|
|
char outbuf[sizeof(java)];
|
|
|
|
ASSERT_TRUE(android::base::ReadFully(output_fd.get(), outbuf, sizeof(outbuf)));
|
|
|
|
ASSERT_STREQ("java", outbuf);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(tombstoned, multiple_intercepts) {
|
|
|
|
const pid_t fake_pid = 1'234'567;
|
|
|
|
unique_fd intercept_fd, output_fd;
|
|
|
|
InterceptStatus status;
|
|
|
|
tombstoned_intercept(fake_pid, &intercept_fd, &output_fd, &status, kDebuggerdJavaBacktrace);
|
|
|
|
ASSERT_EQ(InterceptStatus::kRegistered, status);
|
|
|
|
|
|
|
|
unique_fd intercept_fd_2, output_fd_2;
|
|
|
|
tombstoned_intercept(fake_pid, &intercept_fd_2, &output_fd_2, &status, kDebuggerdNativeBacktrace);
|
|
|
|
ASSERT_EQ(InterceptStatus::kFailedAlreadyRegistered, status);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(tombstoned, intercept_any) {
|
|
|
|
const pid_t fake_pid = 1'234'567;
|
|
|
|
|
|
|
|
unique_fd intercept_fd, output_fd;
|
|
|
|
InterceptStatus status;
|
|
|
|
tombstoned_intercept(fake_pid, &intercept_fd, &output_fd, &status, kDebuggerdNativeBacktrace);
|
|
|
|
ASSERT_EQ(InterceptStatus::kRegistered, status);
|
|
|
|
|
|
|
|
const char any[] = "any";
|
|
|
|
unique_fd tombstoned_socket, input_fd;
|
|
|
|
ASSERT_TRUE(tombstoned_connect(fake_pid, &tombstoned_socket, &input_fd, kDebuggerdAnyIntercept));
|
|
|
|
ASSERT_TRUE(android::base::WriteFully(input_fd.get(), any, sizeof(any)));
|
|
|
|
tombstoned_notify_completion(tombstoned_socket.get());
|
|
|
|
|
|
|
|
char outbuf[sizeof(any)];
|
|
|
|
ASSERT_TRUE(android::base::ReadFully(output_fd.get(), outbuf, sizeof(outbuf)));
|
|
|
|
ASSERT_STREQ("any", outbuf);
|
|
|
|
}
|
2018-09-12 23:51:03 +02:00
|
|
|
|
|
|
|
TEST(tombstoned, interceptless_backtrace) {
|
|
|
|
// Generate 50 backtraces, and then check to see that we haven't created 50 new tombstones.
|
|
|
|
auto get_tombstone_timestamps = []() -> std::map<int, time_t> {
|
|
|
|
std::map<int, time_t> result;
|
|
|
|
for (int i = 0; i < 99; ++i) {
|
|
|
|
std::string path = android::base::StringPrintf("/data/tombstones/tombstone_%02d", i);
|
|
|
|
struct stat st;
|
|
|
|
if (stat(path.c_str(), &st) == 0) {
|
|
|
|
result[i] = st.st_mtim.tv_sec;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
};
|
|
|
|
|
|
|
|
auto before = get_tombstone_timestamps();
|
|
|
|
for (int i = 0; i < 50; ++i) {
|
|
|
|
raise_debugger_signal(kDebuggerdNativeBacktrace);
|
|
|
|
}
|
|
|
|
auto after = get_tombstone_timestamps();
|
|
|
|
|
|
|
|
int diff = 0;
|
|
|
|
for (int i = 0; i < 99; ++i) {
|
|
|
|
if (after.count(i) == 0) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (before.count(i) == 0) {
|
|
|
|
++diff;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (before[i] != after[i]) {
|
|
|
|
++diff;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// We can't be sure that nothing's crash looping in the background.
|
|
|
|
// This should be good enough, though...
|
|
|
|
ASSERT_LT(diff, 10) << "too many new tombstones; is something crashing in the background?";
|
|
|
|
}
|
2019-07-16 02:13:24 +02:00
|
|
|
|
|
|
|
static __attribute__((__noinline__)) void overflow_stack(void* p) {
|
|
|
|
void* buf[1];
|
|
|
|
buf[0] = p;
|
|
|
|
static volatile void* global = buf;
|
|
|
|
if (global) {
|
|
|
|
global = buf;
|
|
|
|
overflow_stack(&buf);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(CrasherTest, stack_overflow) {
|
|
|
|
int intercept_result;
|
|
|
|
unique_fd output_fd;
|
|
|
|
StartProcess([]() { overflow_stack(nullptr); });
|
|
|
|
|
|
|
|
StartIntercept(&output_fd);
|
|
|
|
FinishCrasher();
|
|
|
|
AssertDeath(SIGSEGV);
|
|
|
|
FinishIntercept(&intercept_result);
|
|
|
|
|
|
|
|
ASSERT_EQ(1, intercept_result) << "tombstoned reported failure";
|
|
|
|
|
|
|
|
std::string result;
|
|
|
|
ConsumeFd(std::move(output_fd), &result);
|
|
|
|
ASSERT_MATCH(result, R"(Cause: stack pointer[^\n]*stack overflow.\n)");
|
|
|
|
}
|
2021-01-27 00:53:11 +01:00
|
|
|
|
|
|
|
TEST(tombstoned, proto) {
|
|
|
|
const pid_t self = getpid();
|
|
|
|
unique_fd tombstoned_socket, text_fd, proto_fd;
|
|
|
|
ASSERT_TRUE(
|
|
|
|
tombstoned_connect(self, &tombstoned_socket, &text_fd, &proto_fd, kDebuggerdTombstoneProto));
|
|
|
|
|
|
|
|
tombstoned_notify_completion(tombstoned_socket.get());
|
|
|
|
|
|
|
|
ASSERT_NE(-1, text_fd.get());
|
|
|
|
ASSERT_NE(-1, proto_fd.get());
|
|
|
|
|
|
|
|
struct stat text_st;
|
|
|
|
ASSERT_EQ(0, fstat(text_fd.get(), &text_st));
|
|
|
|
|
|
|
|
// Give tombstoned some time to link the files into place.
|
|
|
|
std::this_thread::sleep_for(100ms);
|
|
|
|
|
|
|
|
// Find the tombstone.
|
2021-02-18 00:39:06 +01:00
|
|
|
std::optional<std::string> tombstone_file;
|
|
|
|
std::unique_ptr<DIR, decltype(&closedir)> dir_h(opendir("/data/tombstones"), closedir);
|
|
|
|
ASSERT_TRUE(dir_h != nullptr);
|
|
|
|
std::regex tombstone_re("tombstone_\\d+");
|
|
|
|
dirent* entry;
|
|
|
|
while ((entry = readdir(dir_h.get())) != nullptr) {
|
|
|
|
if (!std::regex_match(entry->d_name, tombstone_re)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
std::string path = android::base::StringPrintf("/data/tombstones/%s", entry->d_name);
|
2021-01-27 00:53:11 +01:00
|
|
|
|
|
|
|
struct stat st;
|
|
|
|
if (TEMP_FAILURE_RETRY(stat(path.c_str(), &st)) != 0) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (st.st_dev == text_st.st_dev && st.st_ino == text_st.st_ino) {
|
2021-02-18 00:39:06 +01:00
|
|
|
tombstone_file = path;
|
2021-01-27 00:53:11 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-18 00:39:06 +01:00
|
|
|
ASSERT_TRUE(tombstone_file);
|
|
|
|
std::string proto_path = tombstone_file.value() + ".pb";
|
2021-01-27 00:53:11 +01:00
|
|
|
|
|
|
|
struct stat proto_fd_st;
|
|
|
|
struct stat proto_file_st;
|
|
|
|
ASSERT_EQ(0, fstat(proto_fd.get(), &proto_fd_st));
|
|
|
|
ASSERT_EQ(0, stat(proto_path.c_str(), &proto_file_st));
|
|
|
|
|
|
|
|
ASSERT_EQ(proto_fd_st.st_dev, proto_file_st.st_dev);
|
|
|
|
ASSERT_EQ(proto_fd_st.st_ino, proto_file_st.st_ino);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(tombstoned, proto_intercept) {
|
|
|
|
const pid_t self = getpid();
|
|
|
|
unique_fd intercept_fd, output_fd;
|
|
|
|
InterceptStatus status;
|
|
|
|
|
|
|
|
tombstoned_intercept(self, &intercept_fd, &output_fd, &status, kDebuggerdTombstone);
|
|
|
|
ASSERT_EQ(InterceptStatus::kRegistered, status);
|
|
|
|
|
|
|
|
unique_fd tombstoned_socket, text_fd, proto_fd;
|
|
|
|
ASSERT_TRUE(
|
|
|
|
tombstoned_connect(self, &tombstoned_socket, &text_fd, &proto_fd, kDebuggerdTombstoneProto));
|
|
|
|
ASSERT_TRUE(android::base::WriteStringToFd("foo", text_fd.get()));
|
|
|
|
tombstoned_notify_completion(tombstoned_socket.get());
|
|
|
|
|
|
|
|
text_fd.reset();
|
|
|
|
|
|
|
|
std::string output;
|
|
|
|
ASSERT_TRUE(android::base::ReadFdToString(output_fd, &output));
|
|
|
|
ASSERT_EQ("foo", output);
|
|
|
|
}
|