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
|
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
|
|
|
|
#include "sandboxed_api/sandbox2/unwind/unwind.h"
|
|
|
|
|
2019-04-15 22:37:03 +08:00
|
|
|
#include <cxxabi.h>
|
|
|
|
|
2019-03-19 00:21:48 +08:00
|
|
|
#include <cstddef>
|
|
|
|
#include <cstdint>
|
|
|
|
#include <cstdio>
|
|
|
|
#include <map>
|
|
|
|
#include <memory>
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
#include "absl/strings/str_cat.h"
|
2019-07-01 17:53:20 +08:00
|
|
|
#include "libunwind-ptrace.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"
|
|
|
|
#include "sandboxed_api/sandbox2/util/strerror.h"
|
|
|
|
#include "sandboxed_api/util/raw_logging.h"
|
|
|
|
|
|
|
|
namespace sandbox2 {
|
|
|
|
namespace {
|
|
|
|
|
2019-04-15 22:37:03 +08:00
|
|
|
std::string DemangleSymbol(const std::string& maybe_mangled) {
|
|
|
|
int status;
|
|
|
|
std::unique_ptr<char, std::function<void(char*)>> symbol = {
|
|
|
|
abi::__cxa_demangle(maybe_mangled.c_str(), nullptr, nullptr, &status),
|
|
|
|
free};
|
|
|
|
if (symbol && status == 0) {
|
|
|
|
return symbol.get();
|
|
|
|
}
|
|
|
|
return maybe_mangled;
|
|
|
|
}
|
2019-04-24 01:41:28 +08:00
|
|
|
|
2019-03-19 00:21:48 +08:00
|
|
|
std::string GetSymbolAt(const std::map<uint64_t, std::string>& addr_to_symbol,
|
2019-03-26 22:54:02 +08:00
|
|
|
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 "";
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2020-07-20 15:24:12 +08:00
|
|
|
std::vector<uintptr_t> GetIPList(pid_t pid, int max_frames) {
|
2019-03-19 00:21:48 +08:00
|
|
|
unw_cursor_t cursor;
|
|
|
|
static unw_addr_space_t as =
|
|
|
|
unw_create_addr_space(&_UPT_accessors, 0 /* byte order */);
|
|
|
|
if (as == nullptr) {
|
|
|
|
SAPI_RAW_LOG(WARNING, "unw_create_addr_space() failed");
|
2020-07-20 15:24:12 +08:00
|
|
|
return {};
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
|
2020-07-20 15:24:12 +08:00
|
|
|
std::unique_ptr<struct UPT_info, void (*)(void*)> ui(
|
|
|
|
reinterpret_cast<struct UPT_info*>(_UPT_create(pid)), _UPT_destroy);
|
2019-03-19 00:21:48 +08:00
|
|
|
if (ui == nullptr) {
|
|
|
|
SAPI_RAW_LOG(WARNING, "_UPT_create() failed");
|
2020-07-20 15:24:12 +08:00
|
|
|
return {};
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
|
2020-07-20 15:24:12 +08:00
|
|
|
int rc = unw_init_remote(&cursor, as, ui.get());
|
2019-03-19 00:21:48 +08:00
|
|
|
if (rc < 0) {
|
|
|
|
// Could be UNW_EINVAL (8), UNW_EUNSPEC (1) or UNW_EBADREG (3).
|
|
|
|
SAPI_RAW_LOG(WARNING, "unw_init_remote() failed with error %d", rc);
|
2020-07-20 15:24:12 +08:00
|
|
|
return {};
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
|
2020-07-20 15:24:12 +08:00
|
|
|
std::vector<uintptr_t> ips;
|
|
|
|
for (int i = 0; i < max_frames; i++) {
|
|
|
|
unw_word_t ip;
|
|
|
|
rc = unw_get_reg(&cursor, UNW_REG_IP, &ip);
|
|
|
|
if (rc < 0) {
|
|
|
|
// Could be UNW_EUNSPEC or UNW_EBADREG.
|
|
|
|
SAPI_RAW_LOG(WARNING, "unw_get_reg() failed with error %d", rc);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
ips.push_back(ip);
|
|
|
|
rc = unw_step(&cursor);
|
|
|
|
// Non-error condition: UNW_ESUCCESS (0).
|
|
|
|
if (rc < 0) {
|
|
|
|
// If anything but UNW_ESTOPUNWIND (-5), there has been an error.
|
|
|
|
// However since we can't do anything about it and it appears that
|
|
|
|
// this happens every time we don't log this.
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ips;
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2020-07-20 15:24:12 +08:00
|
|
|
const std::string& data = setup.regs();
|
2020-07-17 18:20:18 +08:00
|
|
|
InstallUserRegs(data.c_str(), data.length());
|
|
|
|
ArmPtraceEmulation();
|
|
|
|
|
2019-03-19 00:21:48 +08:00
|
|
|
std::vector<uintptr_t> ips;
|
2020-07-20 15:24:12 +08:00
|
|
|
std::vector<std::string> stack_trace =
|
|
|
|
RunLibUnwindAndSymbolizer(setup.pid(), &ips, setup.default_max_frames());
|
2019-03-19 00:21:48 +08:00
|
|
|
|
2020-07-20 15:24:12 +08:00
|
|
|
UnwindResult msg;
|
|
|
|
*msg.mutable_stacktrace() = {stack_trace.begin(), stack_trace.end()};
|
|
|
|
*msg.mutable_ip() = {ips.begin(), ips.end()};
|
2020-07-17 18:20:18 +08:00
|
|
|
return comms->SendProtoBuf(msg);
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
|
2020-07-20 15:24:12 +08:00
|
|
|
std::vector<std::string> RunLibUnwindAndSymbolizer(pid_t pid,
|
|
|
|
std::vector<uintptr_t>* ips,
|
|
|
|
int max_frames) {
|
2019-03-19 00:21:48 +08:00
|
|
|
// Run libunwind.
|
2020-07-20 15:24:12 +08:00
|
|
|
*ips = GetIPList(pid, max_frames);
|
2019-03-19 00:21:48 +08:00
|
|
|
|
|
|
|
// Open /proc/pid/maps.
|
|
|
|
std::string path_maps = absl::StrCat("/proc/", pid, "/maps");
|
|
|
|
std::unique_ptr<FILE, void (*)(FILE*)> f(fopen(path_maps.c_str(), "r"),
|
|
|
|
[](FILE* s) {
|
|
|
|
if (s) {
|
|
|
|
fclose(s);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
if (!f) {
|
|
|
|
// Could not open maps file.
|
|
|
|
SAPI_RAW_LOG(ERROR, "Could not open %s", path_maps);
|
2020-07-20 15:24:12 +08:00
|
|
|
return {};
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
constexpr static size_t kBufferSize = 10 * 1024 * 1024;
|
|
|
|
std::string maps_content(kBufferSize, '\0');
|
|
|
|
size_t bytes_read = fread(&maps_content[0], 1, kBufferSize, f.get());
|
|
|
|
if (bytes_read == 0) {
|
|
|
|
// Could not read the whole maps file.
|
|
|
|
SAPI_RAW_PLOG(ERROR, "Could not read maps file");
|
2020-07-20 15:24:12 +08:00
|
|
|
return {};
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
maps_content.resize(bytes_read);
|
|
|
|
|
2020-07-20 15:24:12 +08:00
|
|
|
auto maps = ParseProcMaps(maps_content);
|
|
|
|
if (!maps.ok()) {
|
2019-03-19 00:21:48 +08:00
|
|
|
SAPI_RAW_LOG(ERROR, "Could not parse /proc/%d/maps", pid);
|
2020-07-20 15:24:12 +08:00
|
|
|
return {};
|
2019-03-19 00:21:48 +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.
|
|
|
|
std::map<uint64_t, std::string> addr_to_symbol;
|
2020-07-20 15:24:12 +08:00
|
|
|
for (const auto& entry : *maps) {
|
2019-03-19 00:21:48 +08:00
|
|
|
if (!entry.path.empty()) {
|
|
|
|
// 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.
|
|
|
|
addr_to_symbol[entry.start] = absl::StrCat("map:", entry.path);
|
|
|
|
addr_to_symbol[entry.end] = "";
|
|
|
|
}
|
|
|
|
|
2019-09-18 23:31:48 +08:00
|
|
|
const bool should_parse = entry.is_executable && !entry.path.empty() &&
|
|
|
|
entry.path != "[vdso]" &&
|
|
|
|
entry.path != "[vsyscall]";
|
|
|
|
if (should_parse) {
|
2019-03-19 00:21:48 +08:00
|
|
|
auto elf_or = ElfFile::ParseFromFile(entry.path, ElfFile::kLoadSymbols);
|
|
|
|
if (!elf_or.ok()) {
|
|
|
|
SAPI_RAW_LOG(WARNING, "Could not load symbols for %s: %s", entry.path,
|
|
|
|
elf_or.status().message());
|
|
|
|
continue;
|
|
|
|
}
|
2020-04-02 22:42:17 +08:00
|
|
|
auto elf = std::move(elf_or).value();
|
2019-03-19 00:21:48 +08:00
|
|
|
|
|
|
|
for (const auto& symbol : elf.symbols()) {
|
|
|
|
if (elf.position_independent()) {
|
|
|
|
if (symbol.address < entry.end - entry.start) {
|
|
|
|
addr_to_symbol[symbol.address + entry.start] = symbol.name;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (symbol.address >= entry.start && symbol.address < entry.end) {
|
|
|
|
addr_to_symbol[symbol.address] = symbol.name;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-20 15:24:12 +08:00
|
|
|
std::vector<std::string> stack_trace;
|
|
|
|
stack_trace.reserve(ips->size());
|
2019-03-19 00:21:48 +08:00
|
|
|
// Symbolize stacktrace.
|
2020-07-20 15:24:12 +08:00
|
|
|
for (const auto& 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), ")"));
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
2020-07-20 15:24:12 +08:00
|
|
|
return stack_trace;
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace sandbox2
|