Commit 99930ee2 authored by Etienne Pierre-doray's avatar Etienne Pierre-doray Committed by Commit Bot

[Clank SSM]: Inject chrome module into ChromeUnwinderAndroid.

As preparation for NativeUnwinderAndroid, Chrome Module is injected into
ChromeUnwinderAndroid. Module are compared using their addresses.
The chrome module will eventually be created from proc/maps.

Bug: 989102
Change-Id: I3120c4e352fcefef577c4ef9d8c3e7ed7b5a370b
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2083439
Commit-Queue: Etienne Pierre-Doray <etiennep@chromium.org>
Reviewed-by: default avatarMike Wittman <wittman@chromium.org>
Cr-Commit-Position: refs/heads/master@{#747443}
parent 2b958d13
......@@ -4,102 +4,25 @@
#include "base/profiler/chrome_unwinder_android.h"
#include "base/android/library_loader/anchor_functions.h"
#include "base/debug/elf_reader.h"
#include "base/debug/proc_maps_linux.h"
#include "base/no_destructor.h"
#include "base/numerics/checked_math.h"
#include "base/profiler/module_cache.h"
#include "base/profiler/native_unwinder.h"
#include "base/profiler/profile_builder.h"
#include "build/build_config.h"
extern "C" {
// The address of |__executable_start| gives the start address of the
// executable or shared library. This value is used to find the offset address
// of the instruction in binary from PC.
extern char __executable_start;
}
namespace base {
namespace {
const std::string& GetChromeModuleId() {
static const base::NoDestructor<std::string> build_id([] {
#if defined(OFFICIAL_BUILD)
base::debug::ElfBuildIdBuffer build_id;
size_t build_id_length =
base::debug::ReadElfBuildId(&__executable_start, true, build_id);
DCHECK_GT(build_id_length, 0u);
// Append 0 for the age value.
return std::string(build_id, build_id_length) + "0";
#else
// Local chromium builds don't have an ELF build-id note. A synthetic
// build id is provided. https://crbug.com/870919
return std::string("CCCCCCCCDB511330464892F0B600B4D60");
#endif
}());
return *build_id;
}
StringPiece GetChromeLibraryName() {
static const StringPiece library_name([] {
Optional<StringPiece> library_name =
base::debug::ReadElfLibraryName(&__executable_start);
DCHECK(library_name);
return *library_name;
}());
return library_name;
}
class ChromeModule : public ModuleCache::Module {
public:
ChromeModule() : build_id_(GetChromeModuleId()) {}
~ChromeModule() override = default;
uintptr_t GetBaseAddress() const override {
return reinterpret_cast<uintptr_t>(&__executable_start);
}
std::string GetId() const override { return build_id_; }
FilePath GetDebugBasename() const override {
return FilePath(GetChromeLibraryName());
}
// Gets the size of the module.
size_t GetSize() const override {
return base::android::kEndOfText - GetBaseAddress();
}
// True if this is a native module.
bool IsNative() const override { return true; }
std::string build_id_;
};
} // namespace
ChromeUnwinderAndroid::ChromeUnwinderAndroid(const ArmCFITable* cfi_table)
: cfi_table_(cfi_table) {
ChromeUnwinderAndroid::ChromeUnwinderAndroid(
const ArmCFITable* cfi_table,
const ModuleCache::Module* chrome_module)
: cfi_table_(cfi_table), chrome_module_(chrome_module) {
DCHECK(cfi_table_);
DCHECK(chrome_module_);
}
ChromeUnwinderAndroid::~ChromeUnwinderAndroid() = default;
void ChromeUnwinderAndroid::AddInitialModules(ModuleCache* module_cache) {
std::vector<std::unique_ptr<const ModuleCache::Module>> modules;
modules.push_back(std::make_unique<ChromeModule>());
chrome_module_id_ = modules.back()->GetId();
module_cache->UpdateNonNativeModules({}, std::move(modules));
}
bool ChromeUnwinderAndroid::CanUnwindFrom(const Frame* current_frame) const {
// AddNonNativeModules() should be called first.
DCHECK(!chrome_module_id_.empty());
return current_frame->module &&
current_frame->module->GetId() == chrome_module_id_;
return current_frame->module == chrome_module_;
}
UnwindResult ChromeUnwinderAndroid::TryUnwind(RegisterContext* thread_context,
......@@ -109,7 +32,6 @@ UnwindResult ChromeUnwinderAndroid::TryUnwind(RegisterContext* thread_context,
DCHECK(CanUnwindFrom(&stack->back()));
do {
const ModuleCache::Module* module = stack->back().module;
uintptr_t pc = RegisterContextInstructionPointer(thread_context);
DCHECK_GE(pc, module->GetBaseAddress());
uintptr_t func_addr = pc - module->GetBaseAddress();
......@@ -126,11 +48,6 @@ UnwindResult ChromeUnwinderAndroid::TryUnwind(RegisterContext* thread_context,
return UnwindResult::UNRECOGNIZED_FRAME;
}
void ChromeUnwinderAndroid::SetExpectedChromeModuleIdForTesting(
const std::string& chrome_module_id) {
chrome_module_id_ = chrome_module_id;
}
// static
bool ChromeUnwinderAndroid::Step(RegisterContext* thread_context,
uintptr_t stack_top,
......
......@@ -18,15 +18,13 @@ namespace base {
// Chrome unwinder implementation for Android, using ArmCfiTable.
class BASE_EXPORT ChromeUnwinderAndroid : public Unwinder {
public:
ChromeUnwinderAndroid(const ArmCFITable* cfi_table);
ChromeUnwinderAndroid(const ArmCFITable* cfi_table,
const ModuleCache::Module* chrome_module);
~ChromeUnwinderAndroid() override;
ChromeUnwinderAndroid(const ChromeUnwinderAndroid&) = delete;
ChromeUnwinderAndroid& operator=(const ChromeUnwinderAndroid&) = delete;
void SetExpectedChromeModuleIdForTesting(const std::string& chrome_module_id);
// Unwinder:
void AddInitialModules(ModuleCache* module_cache) override;
bool CanUnwindFrom(const Frame* current_frame) const override;
UnwindResult TryUnwind(RegisterContext* thread_context,
uintptr_t stack_top,
......@@ -45,7 +43,7 @@ class BASE_EXPORT ChromeUnwinderAndroid : public Unwinder {
const ArmCFITable::FrameEntry& entry);
const ArmCFITable* cfi_table_;
std::string chrome_module_id_;
const ModuleCache::Module* const chrome_module_;
};
} // namespace base
......
......@@ -213,8 +213,7 @@ TEST(ChromeUnwinderAndroidTest, CanUnwindFrom) {
auto non_chrome_module =
std::make_unique<TestModule>(0x2000, 0x500, "OtherModule");
ChromeUnwinderAndroid unwinder(cfi_table.get());
unwinder.SetExpectedChromeModuleIdForTesting("ChromeModule");
ChromeUnwinderAndroid unwinder(cfi_table.get(), chrome_module.get());
Frame chrome_frame{0x1100, chrome_module.get()};
EXPECT_TRUE(unwinder.CanUnwindFrom(&chrome_frame));
......@@ -231,8 +230,7 @@ TEST(ChromeUnwinderAndroidTest, TryUnwind) {
const ModuleCache::Module* chrome_module = AddNativeModule(
&module_cache, std::make_unique<TestModule>(0x1000, 0x500));
ChromeUnwinderAndroid unwinder(cfi_table.get());
unwinder.SetExpectedChromeModuleIdForTesting(chrome_module->GetId());
ChromeUnwinderAndroid unwinder(cfi_table.get(), chrome_module);
std::vector<uintptr_t> stack_buffer = {
0xFFFF,
......@@ -269,8 +267,7 @@ TEST(ChromeUnwinderAndroidTest, TryUnwindAbort) {
const ModuleCache::Module* chrome_module = AddNativeModule(
&module_cache, std::make_unique<TestModule>(0x1000, 0x500));
ChromeUnwinderAndroid unwinder(cfi_table.get());
unwinder.SetExpectedChromeModuleIdForTesting(chrome_module->GetId());
ChromeUnwinderAndroid unwinder(cfi_table.get(), chrome_module);
std::vector<uintptr_t> stack_buffer = {
0xFFFF,
......@@ -301,8 +298,7 @@ TEST(ChromeUnwinderAndroidTest, TryUnwindNoData) {
const ModuleCache::Module* chrome_module = AddNativeModule(
&module_cache, std::make_unique<TestModule>(0x1000, 0x500));
ChromeUnwinderAndroid unwinder(cfi_table.get());
unwinder.SetExpectedChromeModuleIdForTesting(chrome_module->GetId());
ChromeUnwinderAndroid unwinder(cfi_table.get(), chrome_module);
std::vector<uintptr_t> stack_buffer = {0xFFFF};
......@@ -324,16 +320,4 @@ TEST(ChromeUnwinderAndroidTest, TryUnwindNoData) {
EXPECT_EQ(std::vector<Frame>({{0x1200, chrome_module}}), stack);
}
TEST(ChromeUnwinderAndroidTest, AddInitialModules) {
auto cfi_table = ArmCFITable::Parse(
{reinterpret_cast<const uint8_t*>(cfi_data), sizeof(cfi_data)});
ChromeUnwinderAndroid unwinder(cfi_table.get());
ModuleCache module_cache;
unwinder.AddInitialModules(&module_cache);
EXPECT_NE(module_cache.GetModuleForAddress(reinterpret_cast<uintptr_t>(
&ChromeUnwinderAndroid::StepForTesting)),
nullptr);
}
} // namespace base
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment