Skip to content

Commit

Permalink
Merge pull request #8 from getsentry/chore/update_from_upstream
Browse files Browse the repository at this point in the history
chore: update 2023-09-13
  • Loading branch information
supervacuus authored Sep 14, 2023
2 parents 1929f7b + f064cc8 commit 50fcdc6
Show file tree
Hide file tree
Showing 10 changed files with 132 additions and 39 deletions.
55 changes: 55 additions & 0 deletions Demangle.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* Copyright (C) 2023 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.
*/

#include <cxxabi.h>
#include <stdlib.h>

#include <string>

#ifdef SENTRY_REMOVED
#include <rustc_demangle.h>
#endif //SENTRY_REMOVED

#include <unwindstack/Demangle.h>

namespace unwindstack {

std::string DemangleNameIfNeeded(const std::string& name) {
if (name.length() < 2 || name[0] != '_') {
return name;
}

char* demangled_str = nullptr;
if (name[1] == 'Z') {
// Try to demangle C++ name.
demangled_str = abi::__cxa_demangle(name.c_str(), nullptr, nullptr, nullptr);
#ifdef SENTRY_REMOVED
} else if (name[1] == 'R') {
// Try to demangle rust name.
demangled_str = rustc_demangle(name.c_str(), nullptr, nullptr, nullptr);
#endif //SENTRY_REMOVED
}

if (demangled_str == nullptr) {
return name;
}

std::string demangled_name(demangled_str);
free(demangled_str);
return demangled_name;
}

} // namespace unwindstack
7 changes: 0 additions & 7 deletions Elf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -304,11 +304,6 @@ ElfInterface* Elf::CreateInterfaceFromMemory(Memory* memory) {
} else if (e_machine == EM_386) {
arch_ = ARCH_X86;
interface.reset(new ElfInterface32(memory));
#ifdef SENTRY_REMOVED
} else if (e_machine == EM_MIPS) {
arch_ = ARCH_MIPS;
interface.reset(new ElfInterface32(memory));
#endif // SENTRY_REMOVED
} else {
// Unsupported.
return nullptr;
Expand All @@ -325,8 +320,6 @@ ElfInterface* Elf::CreateInterfaceFromMemory(Memory* memory) {
} else if (e_machine == EM_X86_64) {
arch_ = ARCH_X86_64;
#ifdef SENTRY_REMOVED
} else if (e_machine == EM_MIPS) {
arch_ = ARCH_MIPS64;
} else if (e_machine == EM_RISCV) {
arch_ = ARCH_RISCV64;
#endif // SENTRY_REMOVED
Expand Down
27 changes: 24 additions & 3 deletions ElfInterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -528,12 +528,33 @@ template <typename ElfTypes>
void ElfInterfaceImpl<ElfTypes>::GetMaxSize(Memory* memory, uint64_t* size) {
EhdrType ehdr;
if (!memory->ReadFully(0, &ehdr, sizeof(ehdr))) {
*size = 0;
return;
}
if (ehdr.e_shnum == 0) {
return;

// If this winds up as zero, the PT_LOAD reading will get a better value.
uint64_t elf_size = ehdr.e_shoff + ehdr.e_shentsize * ehdr.e_shnum;

// Search through the PT_LOAD values and if any result in a larger elf
// size, use that.
uint64_t offset = ehdr.e_phoff;
for (size_t i = 0; i < ehdr.e_phnum; i++, offset += ehdr.e_phentsize) {
PhdrType phdr;
if (!memory->ReadFully(offset, &phdr, sizeof(phdr))) {
break;
}
if (phdr.p_type == PT_LOAD) {
uint64_t end_offset;
if (__builtin_add_overflow(phdr.p_offset, phdr.p_memsz, &end_offset)) {
continue;
}
if (end_offset > elf_size) {
elf_size = end_offset;
}
}
}
*size = ehdr.e_shoff + ehdr.e_shentsize * ehdr.e_shnum;

*size = elf_size;
}

template <typename EhdrType, typename ShdrType>
Expand Down
9 changes: 7 additions & 2 deletions ElfInterfaceArm.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
#include <elf.h>
#include <stdint.h>

#include <iterator>
#include <unordered_map>

#include <unwindstack/ElfInterface.h>
Expand All @@ -32,8 +31,14 @@ class ElfInterfaceArm : public ElfInterface32 {
ElfInterfaceArm(Memory* memory) : ElfInterface32(memory) {}
virtual ~ElfInterfaceArm() = default;

class iterator : public std::iterator<std::bidirectional_iterator_tag, uint32_t> {
class iterator {
public:
using iterator_category = std::bidirectional_iterator_tag;
using value_type = uint32_t;
using difference_type = std::ptrdiff_t;
using pointer = uint32_t*;
using reference = uint32_t&;

iterator(ElfInterfaceArm* interface, size_t index) : interface_(interface), index_(index) { }

iterator& operator++() { index_++; return *this; }
Expand Down
4 changes: 0 additions & 4 deletions GlobalDebugImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -410,9 +410,6 @@ std::unique_ptr<GlobalDebugInterface<Symfile>> CreateGlobalDebugImpl(
return std::make_unique<Impl>(arch, jit_memory, search_libs, global_variable_name);
}
case ARCH_ARM: {
#ifdef SENTRY_REMOVED
case ARCH_MIPS: {
#endif // SENTRY_REMOVED
using Impl = GlobalDebugImpl<Symfile, uint32_t, Uint64_A>;
static_assert(offsetof(typename Impl::JITCodeEntry, symfile_size) == 16, "layout");
static_assert(offsetof(typename Impl::JITCodeEntry, seqlock) == 32, "layout");
Expand All @@ -423,7 +420,6 @@ std::unique_ptr<GlobalDebugInterface<Symfile>> CreateGlobalDebugImpl(
case ARCH_ARM64:
case ARCH_X86_64: {
#ifdef SENTRY_REMOVED
case ARCH_MIPS64:
case ARCH_RISCV64: {
#endif // SENTRY_REMOVED
using Impl = GlobalDebugImpl<Symfile, uint64_t, Uint64_A>;
Expand Down
14 changes: 4 additions & 10 deletions Regs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,10 @@ ArchEnum Regs::RemoteGetArch(pid_t pid, ErrorCode* error_code) {
return ARCH_ARM;
case sizeof(arm64_user_regs):
return ARCH_ARM64;
#ifdef SENTRY_REMOVED
case sizeof(riscv64_user_regs):
return ARCH_RISCV64;
#endif // SENTRY_REMOVED
}

Log::Error("No matching size of user regs structure for pid %d: size %zu", pid, io.iov_len);
Expand Down Expand Up @@ -224,16 +228,6 @@ uint64_t GetPcAdjustment(uint64_t rel_pc, Elf* elf, ArchEnum arch) {
return 0;
}
return 4;
#ifdef SENTRY_REMOVED
}
case ARCH_MIPS:
case ARCH_MIPS64: {
if (rel_pc < 8) {
return 0;
}
// For now, just assume no compact branches
return 8;
#endif // SENTRY_REMOVED
}
case ARCH_X86:
case ARCH_X86_64: {
Expand Down
16 changes: 10 additions & 6 deletions Unwinder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -406,13 +406,17 @@ bool UnwinderFromPid::Init() {
}
}

jit_debug_ptr_ = CreateJitDebug(arch_, process_memory_);
jit_debug_ = jit_debug_ptr_.get();
SetJitDebug(jit_debug_);
// jit_debug_ and dex_files_ may have already been set, for example in
// AndroidLocalUnwinder::InternalUnwind.
if (jit_debug_ == nullptr) {
jit_debug_ptr_ = CreateJitDebug(arch_, process_memory_);
SetJitDebug(jit_debug_ptr_.get());
}
#if defined(DEXFILE_SUPPORT)
dex_files_ptr_ = CreateDexFiles(arch_, process_memory_);
dex_files_ = dex_files_ptr_.get();
SetDexFiles(dex_files_);
if (dex_files_ == nullptr) {
dex_files_ptr_ = CreateDexFiles(arch_, process_memory_);
SetDexFiles(dex_files_ptr_.get());
}
#endif

return true;
Expand Down
5 changes: 0 additions & 5 deletions include/unwindstack/Arch.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ enum ArchEnum : uint8_t {
ARCH_X86,
ARCH_X86_64,
#ifdef SENTRY_REMOVED
ARCH_MIPS,
ARCH_MIPS64,
ARCH_RISCV64,
#endif // SENTRY_REMOVED
};
Expand All @@ -37,9 +35,6 @@ static inline bool ArchIs32Bit(ArchEnum arch) {
switch (arch) {
case ARCH_ARM:
case ARCH_X86:
#ifdef SENTRY_REMOVED
case ARCH_MIPS:
#endif // SENTRY_REMOVED
return true;
default:
return false;
Expand Down
25 changes: 25 additions & 0 deletions include/unwindstack/Demangle.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Copyright (C) 2023 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.
*/

#pragma once

#include <string>

namespace unwindstack {

std::string DemangleNameIfNeeded(const std::string& name);

} // namespace unwindstack
9 changes: 7 additions & 2 deletions include/unwindstack/DwarfSection.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

#include <stdint.h>

#include <iterator>
#include <map>
#include <optional>
#include <unordered_map>
Expand All @@ -42,8 +41,14 @@ class DwarfSection {
DwarfSection(Memory* memory);
virtual ~DwarfSection() = default;

class iterator : public std::iterator<std::bidirectional_iterator_tag, DwarfFde*> {
class iterator {
public:
using iterator_category = std::bidirectional_iterator_tag;
using value_type = DwarfFde*;
using difference_type = std::ptrdiff_t;
using pointer = DwarfFde**;
using reference = DwarfFde*&;

iterator(DwarfSection* section, size_t index) : index_(index) {
section->GetFdes(&fdes_);
if (index_ == static_cast<size_t>(-1)) {
Expand Down

0 comments on commit 50fcdc6

Please sign in to comment.