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