xref: /aosp_15_r20/system/extras/simpleperf/OfflineUnwinder_test.cpp (revision 288bf5226967eb3dac5cce6c939ccc2a7f2b4fe5)
1 /*
2  * Copyright (C) 2020 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 "OfflineUnwinder.h"
18 #include "OfflineUnwinder_impl.h"
19 
20 #include <android-base/parseint.h>
21 #include <unwindstack/RegsArm64.h>
22 
23 #include <gtest/gtest.h>
24 
25 using namespace simpleperf;
26 
CheckUnwindMaps(UnwindMaps & maps,const MapSet & map_set)27 bool CheckUnwindMaps(UnwindMaps& maps, const MapSet& map_set) {
28   if (maps.Total() != map_set.maps.size()) {
29     return false;
30   }
31   std::shared_ptr<unwindstack::MapInfo> prev_real_map;
32   for (auto& info : maps) {
33     if (info == nullptr || map_set.maps.find(info->start()) == map_set.maps.end()) {
34       return false;
35     }
36     if (prev_real_map != nullptr && prev_real_map->name() == info->name() &&
37         prev_real_map != info->GetPrevRealMap()) {
38       return false;
39     }
40     if (!info->IsBlank()) {
41       prev_real_map = info;
42     }
43   }
44   return true;
45 }
46 
47 // @CddTest = 6.1/C-0-2
TEST(OfflineUnwinder,UnwindMaps)48 TEST(OfflineUnwinder, UnwindMaps) {
49   // 1. Create fake map entries.
50   std::unique_ptr<Dso> fake_dso = Dso::CreateDso(DSO_UNKNOWN_FILE, "unknown");
51   std::vector<MapEntry> map_entries;
52   for (size_t i = 0; i < 10; i++) {
53     map_entries.emplace_back(i, 1, i, fake_dso.get(), false);
54   }
55 
56   // 2. Init with empty maps.
57   MapSet map_set;
58   UnwindMaps maps;
59   maps.UpdateMaps(map_set);
60   ASSERT_TRUE(CheckUnwindMaps(maps, map_set));
61 
62   // 3. Add maps starting from even addr.
63   map_set.version = 1;
64   for (size_t i = 0; i < map_entries.size(); i += 2) {
65     map_set.maps.insert(std::make_pair(map_entries[i].start_addr, &map_entries[i]));
66   }
67 
68   maps.UpdateMaps(map_set);
69   ASSERT_TRUE(CheckUnwindMaps(maps, map_set));
70 
71   // 4. Add maps starting from odd addr.
72   map_set.version = 2;
73   for (size_t i = 1; i < 10; i += 2) {
74     map_set.maps.insert(std::make_pair(map_entries[i].start_addr, &map_entries[i]));
75   }
76   maps.UpdateMaps(map_set);
77   ASSERT_TRUE(CheckUnwindMaps(maps, map_set));
78 
79   // 5. Remove maps starting from even addr.
80   map_set.version = 3;
81   for (size_t i = 0; i < 10; i += 2) {
82     map_set.maps.erase(map_entries[i].start_addr);
83   }
84   maps.UpdateMaps(map_set);
85   ASSERT_TRUE(CheckUnwindMaps(maps, map_set));
86 
87   // 6. Remove all maps.
88   map_set.version = 4;
89   map_set.maps.clear();
90   maps.UpdateMaps(map_set);
91   ASSERT_TRUE(CheckUnwindMaps(maps, map_set));
92 }
93 
94 // @CddTest = 6.1/C-0-2
TEST(OfflineUnwinder,CollectMetaInfo)95 TEST(OfflineUnwinder, CollectMetaInfo) {
96   std::unordered_map<std::string, std::string> info_map;
97   OfflineUnwinder::CollectMetaInfo(&info_map);
98   if (auto it = info_map.find(OfflineUnwinder::META_KEY_ARM64_PAC_MASK); it != info_map.end()) {
99     uint64_t arm64_pack_mask;
100     ASSERT_TRUE(android::base::ParseUint(it->second, &arm64_pack_mask));
101     ASSERT_NE(arm64_pack_mask, 0);
102   }
103 }
104 
105 // @CddTest = 6.1/C-0-2
TEST(OfflineUnwinder,ARM64PackMask)106 TEST(OfflineUnwinder, ARM64PackMask) {
107   std::unordered_map<std::string, std::string> info_map;
108   info_map[OfflineUnwinder::META_KEY_ARM64_PAC_MASK] = "0xff00000000";
109   std::unique_ptr<OfflineUnwinderImpl> unwinder(new OfflineUnwinderImpl(false));
110   unwinder->LoadMetaInfo(info_map);
111 
112   RegSet fake_regs(0, 0, nullptr);
113   fake_regs.arch = ARCH_ARM64;
114   unwindstack::Regs* regs = unwinder->GetBacktraceRegs(fake_regs);
115   ASSERT_TRUE(regs != nullptr);
116   auto& arm64 = *static_cast<unwindstack::RegsArm64*>(regs);
117   arm64.SetPseudoRegister(unwindstack::Arm64Reg::ARM64_PREG_RA_SIGN_STATE, 1);
118   arm64.set_pc(0xffccccccccULL);
119   ASSERT_EQ(arm64.pc(), 0xccccccccULL);
120 }
121