2020-01-17 21:05:03 +08:00
|
|
|
// Copyright 2019 Google LLC
|
2019-03-19 00:21:48 +08:00
|
|
|
//
|
|
|
|
// 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
|
|
|
|
//
|
2022-01-28 17:38:27 +08:00
|
|
|
// https://www.apache.org/licenses/LICENSE-2.0
|
2019-03-19 00:21:48 +08:00
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
|
|
|
|
#include "sandboxed_api/sandbox2/unwind/unwind.h"
|
|
|
|
|
2019-04-15 22:37:03 +08:00
|
|
|
#include <cxxabi.h>
|
2023-03-01 19:54:29 +08:00
|
|
|
#include <sys/ptrace.h>
|
2019-04-15 22:37:03 +08:00
|
|
|
|
2023-03-01 19:54:29 +08:00
|
|
|
#include <cerrno>
|
2019-03-19 00:21:48 +08:00
|
|
|
#include <cstddef>
|
|
|
|
#include <cstdint>
|
|
|
|
#include <cstdio>
|
2022-02-08 22:23:13 +08:00
|
|
|
#include <cstdlib>
|
2019-03-19 00:21:48 +08:00
|
|
|
#include <map>
|
|
|
|
#include <memory>
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
|
2022-02-08 22:23:13 +08:00
|
|
|
#include "absl/cleanup/cleanup.h"
|
2023-03-01 19:54:29 +08:00
|
|
|
#include "absl/status/status.h"
|
2021-06-28 17:02:35 +08:00
|
|
|
#include "absl/status/statusor.h"
|
2020-11-25 22:46:11 +08:00
|
|
|
#include "absl/strings/match.h"
|
2019-03-19 00:21:48 +08:00
|
|
|
#include "absl/strings/str_cat.h"
|
2019-07-01 17:53:20 +08:00
|
|
|
#include "libunwind-ptrace.h"
|
2022-11-30 16:16:02 +08:00
|
|
|
#include "sandboxed_api/config.h"
|
2019-03-19 00:21:48 +08:00
|
|
|
#include "sandboxed_api/sandbox2/comms.h"
|
2020-07-17 18:20:18 +08:00
|
|
|
#include "sandboxed_api/sandbox2/unwind/ptrace_hook.h"
|
2019-03-19 00:21:48 +08:00
|
|
|
#include "sandboxed_api/sandbox2/unwind/unwind.pb.h"
|
|
|
|
#include "sandboxed_api/sandbox2/util/maps_parser.h"
|
|
|
|
#include "sandboxed_api/sandbox2/util/minielf.h"
|
2021-04-28 22:48:48 +08:00
|
|
|
#include "sandboxed_api/util/file_helpers.h"
|
2019-03-19 00:21:48 +08:00
|
|
|
#include "sandboxed_api/util/raw_logging.h"
|
2021-06-28 17:02:35 +08:00
|
|
|
#include "sandboxed_api/util/status_macros.h"
|
2019-03-19 00:21:48 +08:00
|
|
|
|
|
|
|
namespace sandbox2 {
|
|
|
|
namespace {
|
|
|
|
|
2019-04-15 22:37:03 +08:00
|
|
|
std::string DemangleSymbol(const std::string& maybe_mangled) {
|
|
|
|
int status;
|
2021-04-28 22:48:48 +08:00
|
|
|
size_t length;
|
|
|
|
std::unique_ptr<char, decltype(&std::free)> symbol(
|
|
|
|
abi::__cxa_demangle(maybe_mangled.c_str(), /*output_buffer=*/nullptr,
|
|
|
|
&length, &status),
|
|
|
|
std::free);
|
2019-04-15 22:37:03 +08:00
|
|
|
if (symbol && status == 0) {
|
2021-04-28 22:48:48 +08:00
|
|
|
return std::string(symbol.get(), length);
|
2019-04-15 22:37:03 +08:00
|
|
|
}
|
|
|
|
return maybe_mangled;
|
|
|
|
}
|
2019-04-24 01:41:28 +08:00
|
|
|
|
2023-03-01 19:54:29 +08:00
|
|
|
absl::StatusOr<uintptr_t> ReadMemory(pid_t pid, uintptr_t addr) {
|
|
|
|
errno = 0;
|
|
|
|
uintptr_t val = ptrace(PTRACE_PEEKDATA, pid, addr, 0);
|
|
|
|
if (errno != 0) {
|
|
|
|
return absl::ErrnoToStatus(errno, "ptrace() failed");
|
|
|
|
}
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
|
|
|
absl::StatusOr<std::vector<uintptr_t>> UnwindUsingFramePointer(pid_t pid,
|
|
|
|
int max_frames,
|
|
|
|
uintptr_t fp) {
|
|
|
|
#if defined(SAPI_PPC64_LE)
|
|
|
|
constexpr int kIPOffset = 2;
|
|
|
|
#else
|
|
|
|
constexpr int kIPOffset = 1;
|
|
|
|
#endif
|
|
|
|
std::vector<uintptr_t> ips;
|
|
|
|
for (int i = 0; fp != 0 && i < max_frames; ++i) {
|
|
|
|
SAPI_ASSIGN_OR_RETURN(uintptr_t ip,
|
|
|
|
ReadMemory(pid, fp + kIPOffset * sizeof(void*)));
|
|
|
|
ips.push_back(ip);
|
|
|
|
SAPI_ASSIGN_OR_RETURN(fp, ReadMemory(pid, fp));
|
|
|
|
}
|
|
|
|
return ips;
|
|
|
|
}
|
|
|
|
|
2021-11-12 21:59:17 +08:00
|
|
|
absl::StatusOr<std::vector<uintptr_t>> RunLibUnwind(pid_t pid, int max_frames) {
|
|
|
|
static unw_addr_space_t as =
|
|
|
|
unw_create_addr_space(&_UPT_accessors, 0 /* byte order */);
|
|
|
|
if (as == nullptr) {
|
|
|
|
return absl::InternalError("unw_create_addr_space() failed");
|
|
|
|
}
|
|
|
|
|
2022-02-08 22:23:13 +08:00
|
|
|
void* context = _UPT_create(pid);
|
|
|
|
if (context == nullptr) {
|
2021-11-12 21:59:17 +08:00
|
|
|
return absl::InternalError("_UPT_create() failed");
|
|
|
|
}
|
2022-02-08 22:23:13 +08:00
|
|
|
absl::Cleanup context_cleanup = [&context] { _UPT_destroy(context); };
|
2021-11-12 21:59:17 +08:00
|
|
|
|
2022-02-08 22:23:13 +08:00
|
|
|
unw_cursor_t cursor;
|
|
|
|
if (int rc = unw_init_remote(&cursor, as, context); rc < 0) {
|
2021-11-12 21:59:17 +08:00
|
|
|
// Could be UNW_EINVAL (8), UNW_EUNSPEC (1) or UNW_EBADREG (3).
|
|
|
|
return absl::InternalError(
|
|
|
|
absl::StrCat("unw_init_remote() failed with error ", rc));
|
|
|
|
}
|
|
|
|
std::vector<uintptr_t> ips;
|
|
|
|
for (int i = 0; i < max_frames; ++i) {
|
|
|
|
unw_word_t ip;
|
2023-03-01 19:54:29 +08:00
|
|
|
unw_word_t fp = 0;
|
2022-02-08 22:23:13 +08:00
|
|
|
int rc = unw_get_reg(&cursor, UNW_REG_IP, &ip);
|
2021-11-12 21:59:17 +08:00
|
|
|
if (rc < 0) {
|
|
|
|
// Could be UNW_EUNSPEC or UNW_EBADREG.
|
|
|
|
SAPI_RAW_LOG(WARNING, "unw_get_reg() failed with error %d", rc);
|
|
|
|
break;
|
|
|
|
}
|
2023-03-01 19:54:29 +08:00
|
|
|
#if defined(SAPI_ARM64)
|
|
|
|
constexpr int kFpReg = UNW_AARCH64_X29;
|
|
|
|
#elif defined(SAPI_ARM)
|
|
|
|
constexpr int kFpReg = UNW_ARM_R11;
|
|
|
|
#elif defined(SAPI_X86_64)
|
|
|
|
constexpr int kFpReg = UNW_X86_64_RBP;
|
|
|
|
#elif defined(SAPI_PPC64_LE)
|
|
|
|
constexpr int kFpReg = UNW_PPC64_R1;
|
|
|
|
#endif
|
|
|
|
rc = unw_get_reg(&cursor, kFpReg, &fp);
|
|
|
|
if (rc < 0) {
|
|
|
|
SAPI_RAW_LOG(WARNING, "unw_get_reg() failed with error %d", rc);
|
|
|
|
}
|
2021-11-12 21:59:17 +08:00
|
|
|
ips.push_back(ip);
|
2023-03-01 19:54:29 +08:00
|
|
|
rc = unw_step(&cursor);
|
|
|
|
if (rc <= 0) {
|
|
|
|
if (rc < 0) {
|
|
|
|
SAPI_RAW_LOG(WARNING, "unw_step() failed with error %d", rc);
|
|
|
|
}
|
|
|
|
if (fp != 0) {
|
|
|
|
SAPI_RAW_LOG(INFO, "Falling back to frame based unwinding at FP: %lx",
|
|
|
|
fp);
|
|
|
|
absl::StatusOr<std::vector<uintptr_t>> fp_ips =
|
|
|
|
UnwindUsingFramePointer(pid, max_frames - ips.size(), fp);
|
|
|
|
if (!fp_ips.ok()) {
|
|
|
|
SAPI_RAW_LOG(WARNING, "FP based unwinding failed: %s",
|
|
|
|
std::string(fp_ips.status().message()).c_str());
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
ips.insert(ips.end(), fp_ips->begin(), fp_ips->end());
|
|
|
|
}
|
2021-11-12 21:59:17 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ips;
|
|
|
|
}
|
|
|
|
|
|
|
|
absl::StatusOr<std::vector<std::string>> SymbolizeStacktrace(
|
|
|
|
pid_t pid, const std::vector<uintptr_t>& ips) {
|
|
|
|
SAPI_ASSIGN_OR_RETURN(auto addr_to_symbol, LoadSymbolsMap(pid));
|
|
|
|
std::vector<std::string> stack_trace;
|
|
|
|
stack_trace.reserve(ips.size());
|
|
|
|
// Symbolize stacktrace
|
|
|
|
for (uintptr_t ip : ips) {
|
|
|
|
const std::string symbol =
|
|
|
|
GetSymbolAt(addr_to_symbol, static_cast<uint64_t>(ip));
|
|
|
|
stack_trace.push_back(absl::StrCat(symbol, "(0x", absl::Hex(ip), ")"));
|
|
|
|
}
|
|
|
|
return stack_trace;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
std::string GetSymbolAt(const SymbolMap& addr_to_symbol, uint64_t addr) {
|
2019-03-19 00:21:48 +08:00
|
|
|
auto entry_for_next_symbol = addr_to_symbol.lower_bound(addr);
|
|
|
|
if (entry_for_next_symbol != addr_to_symbol.end() &&
|
|
|
|
entry_for_next_symbol != addr_to_symbol.begin()) {
|
|
|
|
// Matches the addr exactly:
|
|
|
|
if (entry_for_next_symbol->first == addr) {
|
2019-04-15 22:37:03 +08:00
|
|
|
return DemangleSymbol(entry_for_next_symbol->second);
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Might be inside a function, return symbol+offset;
|
|
|
|
const auto entry_for_previous_symbol = --entry_for_next_symbol;
|
|
|
|
if (!entry_for_previous_symbol->second.empty()) {
|
2019-04-15 22:37:03 +08:00
|
|
|
return absl::StrCat(DemangleSymbol(entry_for_previous_symbol->second),
|
|
|
|
"+0x",
|
2019-03-19 00:21:48 +08:00
|
|
|
absl::Hex(addr - entry_for_previous_symbol->first));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
2021-11-12 21:59:17 +08:00
|
|
|
absl::StatusOr<SymbolMap> LoadSymbolsMap(pid_t pid) {
|
2021-06-28 17:02:35 +08:00
|
|
|
const std::string maps_filename = absl::StrCat("/proc/", pid, "/maps");
|
|
|
|
std::string maps_content;
|
|
|
|
SAPI_RETURN_IF_ERROR(sapi::file::GetContents(maps_filename, &maps_content,
|
|
|
|
sapi::file::Defaults()));
|
|
|
|
|
|
|
|
SAPI_ASSIGN_OR_RETURN(std::vector<MapsEntry> maps,
|
|
|
|
ParseProcMaps(maps_content));
|
2021-06-22 20:51:42 +08:00
|
|
|
|
2021-06-28 17:02:35 +08:00
|
|
|
// Get symbols for each file entry in the maps entry.
|
|
|
|
// This is not a very efficient way, so we might want to optimize it.
|
2021-11-12 21:59:17 +08:00
|
|
|
SymbolMap addr_to_symbol;
|
2021-06-28 17:02:35 +08:00
|
|
|
for (const MapsEntry& entry : maps) {
|
|
|
|
if (!entry.is_executable ||
|
|
|
|
entry.inode == 0 || // Only parse file-backed entries
|
|
|
|
entry.path.empty() ||
|
|
|
|
absl::EndsWith(entry.path, " (deleted)") // Skip deleted files
|
|
|
|
) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Store details about start + end of this map.
|
|
|
|
// The maps entries are ordered and thus sorted with increasing adresses.
|
|
|
|
// This means if there is a symbol @ entry.end, it will be overwritten in
|
|
|
|
// the next iteration.
|
2021-08-18 22:13:45 +08:00
|
|
|
std::string map = absl::StrCat("map:", entry.path);
|
|
|
|
if (entry.pgoff) {
|
2021-09-21 00:01:39 +08:00
|
|
|
absl::StrAppend(&map, "+0x", absl::Hex(entry.pgoff));
|
2021-08-18 22:13:45 +08:00
|
|
|
}
|
|
|
|
addr_to_symbol[entry.start] = map;
|
2021-06-28 17:02:35 +08:00
|
|
|
addr_to_symbol[entry.end] = "";
|
|
|
|
|
|
|
|
absl::StatusOr<ElfFile> elf =
|
|
|
|
ElfFile::ParseFromFile(entry.path, ElfFile::kLoadSymbols);
|
|
|
|
if (!elf.ok()) {
|
|
|
|
SAPI_RAW_LOG(WARNING, "Could not load symbols for %s: %s",
|
|
|
|
entry.path.c_str(),
|
|
|
|
std::string(elf.status().message()).c_str());
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const ElfFile::Symbol& symbol : elf->symbols()) {
|
2022-11-30 16:16:02 +08:00
|
|
|
// Skip Mapping Symbols on ARM
|
|
|
|
// ARM documentation for Mapping Symbols:
|
|
|
|
// https://developer.arm.com/documentation/dui0803/a/Accessing-and-managing-symbols-with-armlink/About-mapping-symbols
|
|
|
|
if constexpr (sapi::host_cpu::IsArm64() || sapi::host_cpu::IsArm()) {
|
|
|
|
if (absl::StartsWith(symbol.name, "$x") ||
|
|
|
|
absl::StartsWith(symbol.name, "$d") ||
|
|
|
|
absl::StartsWith(symbol.name, "$t") ||
|
|
|
|
absl::StartsWith(symbol.name, "$a") ||
|
|
|
|
absl::StartsWith(symbol.name, "$v")) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-28 17:02:35 +08:00
|
|
|
if (elf->position_independent()) {
|
2022-02-11 23:19:01 +08:00
|
|
|
if (symbol.address >= entry.pgoff &&
|
|
|
|
symbol.address - entry.pgoff < entry.end - entry.start) {
|
|
|
|
addr_to_symbol[symbol.address + entry.start - entry.pgoff] =
|
|
|
|
symbol.name;
|
2021-06-28 17:02:35 +08:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (symbol.address >= entry.start && symbol.address < entry.end) {
|
|
|
|
addr_to_symbol[symbol.address] = symbol.name;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return addr_to_symbol;
|
|
|
|
}
|
|
|
|
|
2020-07-17 18:20:18 +08:00
|
|
|
bool RunLibUnwindAndSymbolizer(Comms* comms) {
|
2020-07-20 15:24:12 +08:00
|
|
|
UnwindSetup setup;
|
|
|
|
if (!comms->RecvProtoBuf(&setup)) {
|
2020-07-17 18:20:18 +08:00
|
|
|
return false;
|
|
|
|
}
|
2023-01-19 21:44:11 +08:00
|
|
|
int mem_fd;
|
|
|
|
if (!comms->RecvFD(&mem_fd)) {
|
|
|
|
return false;
|
|
|
|
}
|
2020-07-17 18:20:18 +08:00
|
|
|
|
2023-01-19 21:44:11 +08:00
|
|
|
EnablePtraceEmulationWithUserRegs(setup.pid(), setup.regs(), mem_fd);
|
2020-07-17 18:20:18 +08:00
|
|
|
|
2023-03-02 18:39:33 +08:00
|
|
|
absl::StatusOr<std::vector<std::string>> stack_trace =
|
|
|
|
RunLibUnwindAndSymbolizer(setup.pid(), setup.default_max_frames());
|
2019-03-19 00:21:48 +08:00
|
|
|
|
2021-06-28 17:02:35 +08:00
|
|
|
if (!comms->SendStatus(stack_trace.status())) {
|
|
|
|
return false;
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
|
2021-06-28 17:02:35 +08:00
|
|
|
if (!stack_trace.ok()) {
|
|
|
|
return true;
|
2021-06-23 05:49:54 +08:00
|
|
|
}
|
|
|
|
|
2021-06-28 17:02:35 +08:00
|
|
|
UnwindResult msg;
|
|
|
|
*msg.mutable_stacktrace() = {stack_trace->begin(), stack_trace->end()};
|
|
|
|
return comms->SendProtoBuf(msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
absl::StatusOr<std::vector<std::string>> RunLibUnwindAndSymbolizer(
|
|
|
|
pid_t pid, int max_frames) {
|
|
|
|
SAPI_ASSIGN_OR_RETURN(std::vector<uintptr_t> ips,
|
|
|
|
RunLibUnwind(pid, max_frames));
|
|
|
|
return SymbolizeStacktrace(pid, ips);
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace sandbox2
|