1 /*
2 * Copyright (C) 2024 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "src/profiling/perf/frame_pointer_unwinder.h"
18
19 #include <sys/mman.h>
20 #include <unwindstack/Unwinder.h>
21
22 #include "perfetto/base/logging.h"
23 #include "perfetto/ext/base/file_utils.h"
24 #include "perfetto/ext/base/scoped_file.h"
25 #include "test/gtest_and_gmock.h"
26
27 namespace perfetto {
28 namespace profiling {
29 namespace {
30
31 class RegsFake : public unwindstack::Regs {
32 public:
RegsFake(uint16_t total_regs)33 RegsFake(uint16_t total_regs)
34 : unwindstack::Regs(
35 total_regs,
36 unwindstack::Regs::Location(unwindstack::Regs::LOCATION_UNKNOWN,
37 0)) {
38 fake_data_ = std::make_unique<uint64_t[]>(total_regs);
39 }
40 ~RegsFake() override = default;
41
Arch()42 unwindstack::ArchEnum Arch() override { return fake_arch_; }
RawData()43 void* RawData() override { return fake_data_.get(); }
pc()44 uint64_t pc() override { return fake_pc_; }
sp()45 uint64_t sp() override { return fake_sp_; }
set_pc(uint64_t pc)46 void set_pc(uint64_t pc) override { fake_pc_ = pc; }
set_sp(uint64_t sp)47 void set_sp(uint64_t sp) override { fake_sp_ = sp; }
48
set_fp(uint64_t fp)49 void set_fp(uint64_t fp) {
50 switch (fake_arch_) {
51 case unwindstack::ARCH_ARM64:
52 fake_data_[unwindstack::Arm64Reg::ARM64_REG_R29] = fp;
53 break;
54 case unwindstack::ARCH_X86_64:
55 fake_data_[unwindstack::X86_64Reg::X86_64_REG_RBP] = fp;
56 break;
57 case unwindstack::ARCH_RISCV64:
58 fake_data_[unwindstack::Riscv64Reg::RISCV64_REG_S0] = fp;
59 break;
60 case unwindstack::ARCH_UNKNOWN:
61 case unwindstack::ARCH_ARM:
62 case unwindstack::ARCH_X86:
63 // not supported
64 ;
65 }
66 }
67
SetPcFromReturnAddress(unwindstack::Memory *)68 bool SetPcFromReturnAddress(unwindstack::Memory*) override { return false; }
69
IterateRegisters(std::function<void (const char *,uint64_t)>)70 void IterateRegisters(std::function<void(const char*, uint64_t)>) override {}
71
StepIfSignalHandler(uint64_t,unwindstack::Elf *,unwindstack::Memory *)72 bool StepIfSignalHandler(uint64_t,
73 unwindstack::Elf*,
74 unwindstack::Memory*) override {
75 return false;
76 }
77
FakeSetArch(unwindstack::ArchEnum arch)78 void FakeSetArch(unwindstack::ArchEnum arch) { fake_arch_ = arch; }
79
Clone()80 Regs* Clone() override { return nullptr; }
81
82 private:
83 unwindstack::ArchEnum fake_arch_ = unwindstack::ARCH_UNKNOWN;
84 uint64_t fake_pc_ = 0;
85 uint64_t fake_sp_ = 0;
86 std::unique_ptr<uint64_t[]> fake_data_;
87 };
88
89 class MemoryFake : public unwindstack::Memory {
90 public:
91 MemoryFake() = default;
92 ~MemoryFake() override = default;
93
Read(uint64_t addr,void * memory,size_t size)94 size_t Read(uint64_t addr, void* memory, size_t size) override {
95 uint8_t* dst = reinterpret_cast<uint8_t*>(memory);
96 for (size_t i = 0; i < size; i++, addr++) {
97 auto value = data_.find(addr);
98 if (value == data_.end()) {
99 return i;
100 }
101 dst[i] = value->second;
102 }
103 return size;
104 }
105
SetMemory(uint64_t addr,const void * memory,size_t length)106 void SetMemory(uint64_t addr, const void* memory, size_t length) {
107 const uint8_t* src = reinterpret_cast<const uint8_t*>(memory);
108 for (size_t i = 0; i < length; i++, addr++) {
109 auto value = data_.find(addr);
110 if (value != data_.end()) {
111 value->second = src[i];
112 } else {
113 data_.insert({addr, src[i]});
114 }
115 }
116 }
117
SetData8(uint64_t addr,uint8_t value)118 void SetData8(uint64_t addr, uint8_t value) {
119 SetMemory(addr, &value, sizeof(value));
120 }
121
SetData16(uint64_t addr,uint16_t value)122 void SetData16(uint64_t addr, uint16_t value) {
123 SetMemory(addr, &value, sizeof(value));
124 }
125
SetData32(uint64_t addr,uint32_t value)126 void SetData32(uint64_t addr, uint32_t value) {
127 SetMemory(addr, &value, sizeof(value));
128 }
129
SetData64(uint64_t addr,uint64_t value)130 void SetData64(uint64_t addr, uint64_t value) {
131 SetMemory(addr, &value, sizeof(value));
132 }
133
SetMemory(uint64_t addr,std::vector<uint8_t> values)134 void SetMemory(uint64_t addr, std::vector<uint8_t> values) {
135 SetMemory(addr, values.data(), values.size());
136 }
137
SetMemory(uint64_t addr,std::string string)138 void SetMemory(uint64_t addr, std::string string) {
139 SetMemory(addr, string.c_str(), string.size() + 1);
140 }
141
Clear()142 void Clear() override { data_.clear(); }
143
144 private:
145 std::unordered_map<uint64_t, uint8_t> data_;
146 };
147
148 constexpr static uint64_t kMaxFrames = 64;
149 constexpr static uint64_t kStackSize = 0xFFFFFFF;
150
151 class FramePointerUnwinderTest : public ::testing::Test {
152 protected:
SetUp()153 void SetUp() override {
154 memory_fake_ = new MemoryFake;
155 maps_.reset(new unwindstack::Maps);
156 regs_fake_ = std::make_unique<RegsFake>(64);
157 regs_fake_->FakeSetArch(unwindstack::ARCH_X86_64);
158 process_memory_.reset(memory_fake_);
159
160 unwinder_ = std::make_unique<FramePointerUnwinder>(
161 kMaxFrames, maps_.get(), regs_fake_.get(), process_memory_, kStackSize);
162 }
163
164 MemoryFake* memory_fake_;
165 std::unique_ptr<unwindstack::Maps> maps_;
166 std::unique_ptr<RegsFake> regs_fake_;
167 std::shared_ptr<unwindstack::Memory> process_memory_;
168
169 std::unique_ptr<FramePointerUnwinder> unwinder_;
170 };
171
TEST_F(FramePointerUnwinderTest,UnwindUnsupportedArch)172 TEST_F(FramePointerUnwinderTest, UnwindUnsupportedArch) {
173 regs_fake_->FakeSetArch(unwindstack::ARCH_UNKNOWN);
174 unwinder_.reset(new FramePointerUnwinder(
175 kMaxFrames, maps_.get(), regs_fake_.get(), process_memory_, kStackSize));
176 unwinder_->Unwind();
177 EXPECT_EQ(unwinder_->LastErrorCode(),
178 unwindstack::ErrorCode::ERROR_UNSUPPORTED);
179
180 regs_fake_->FakeSetArch(unwindstack::ARCH_X86);
181 unwinder_.reset(new FramePointerUnwinder(
182 kMaxFrames, maps_.get(), regs_fake_.get(), process_memory_, kStackSize));
183 unwinder_->Unwind();
184 EXPECT_EQ(unwinder_->LastErrorCode(),
185 unwindstack::ErrorCode::ERROR_UNSUPPORTED);
186
187 regs_fake_->FakeSetArch(unwindstack::ARCH_ARM);
188 unwinder_.reset(new FramePointerUnwinder(
189 kMaxFrames, maps_.get(), regs_fake_.get(), process_memory_, kStackSize));
190 unwinder_->Unwind();
191 EXPECT_EQ(unwinder_->LastErrorCode(),
192 unwindstack::ErrorCode::ERROR_UNSUPPORTED);
193 }
194
TEST_F(FramePointerUnwinderTest,UnwindInvalidMaps)195 TEST_F(FramePointerUnwinderTest, UnwindInvalidMaps) {
196 // Set up a valid stack frame
197 regs_fake_->set_pc(0x1000);
198 regs_fake_->set_sp(0x2000);
199 memory_fake_->SetData64(0x2000, 0x3000);
200 memory_fake_->SetData64(0x2008, 0x2000);
201 unwinder_->Unwind();
202 EXPECT_EQ(unwinder_->LastErrorCode(),
203 unwindstack::ErrorCode::ERROR_INVALID_MAP);
204 EXPECT_EQ(unwinder_->ConsumeFrames().size(), 0UL);
205 }
206
TEST_F(FramePointerUnwinderTest,UnwindValidStack)207 TEST_F(FramePointerUnwinderTest, UnwindValidStack) {
208 regs_fake_->set_pc(0x1900);
209 regs_fake_->set_sp(0x1800);
210 regs_fake_->set_fp(0x2000);
211
212 memory_fake_->SetData64(0x2000, 0x2200); // mock next_fp
213 memory_fake_->SetData64(0x2000 + sizeof(uint64_t),
214 0x2100); // mock return_address(next_pc)
215
216 memory_fake_->SetData64(0x2200, 0);
217
218 maps_->Add(0x1000, 0x12000, 0, PROT_READ | PROT_WRITE, "libmock.so");
219
220 unwinder_.reset(new FramePointerUnwinder(
221 kMaxFrames, maps_.get(), regs_fake_.get(), process_memory_, kStackSize));
222 unwinder_->Unwind();
223 EXPECT_EQ(unwinder_->LastErrorCode(), unwindstack::ErrorCode::ERROR_NONE);
224 EXPECT_EQ(unwinder_->ConsumeFrames().size(), 2UL);
225 }
226
227 } // namespace
228 } // namespace profiling
229 } // namespace perfetto
230