1*288bf522SAndroid Build Coastguard Worker /*
2*288bf522SAndroid Build Coastguard Worker * Copyright (C) 2020 The Android Open Source Project
3*288bf522SAndroid Build Coastguard Worker *
4*288bf522SAndroid Build Coastguard Worker * Licensed under the Apache License, Version 2.0 (the "License");
5*288bf522SAndroid Build Coastguard Worker * you may not use this file except in compliance with the License.
6*288bf522SAndroid Build Coastguard Worker * You may obtain a copy of the License at
7*288bf522SAndroid Build Coastguard Worker *
8*288bf522SAndroid Build Coastguard Worker * http://www.apache.org/licenses/LICENSE-2.0
9*288bf522SAndroid Build Coastguard Worker *
10*288bf522SAndroid Build Coastguard Worker * Unless required by applicable law or agreed to in writing, software
11*288bf522SAndroid Build Coastguard Worker * distributed under the License is distributed on an "AS IS" BASIS,
12*288bf522SAndroid Build Coastguard Worker * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*288bf522SAndroid Build Coastguard Worker * See the License for the specific language governing permissions and
14*288bf522SAndroid Build Coastguard Worker * limitations under the License.
15*288bf522SAndroid Build Coastguard Worker */
16*288bf522SAndroid Build Coastguard Worker
17*288bf522SAndroid Build Coastguard Worker #include "OfflineUnwinder.h"
18*288bf522SAndroid Build Coastguard Worker #include "OfflineUnwinder_impl.h"
19*288bf522SAndroid Build Coastguard Worker
20*288bf522SAndroid Build Coastguard Worker #include <android-base/parseint.h>
21*288bf522SAndroid Build Coastguard Worker #include <unwindstack/RegsArm64.h>
22*288bf522SAndroid Build Coastguard Worker
23*288bf522SAndroid Build Coastguard Worker #include <gtest/gtest.h>
24*288bf522SAndroid Build Coastguard Worker
25*288bf522SAndroid Build Coastguard Worker using namespace simpleperf;
26*288bf522SAndroid Build Coastguard Worker
CheckUnwindMaps(UnwindMaps & maps,const MapSet & map_set)27*288bf522SAndroid Build Coastguard Worker bool CheckUnwindMaps(UnwindMaps& maps, const MapSet& map_set) {
28*288bf522SAndroid Build Coastguard Worker if (maps.Total() != map_set.maps.size()) {
29*288bf522SAndroid Build Coastguard Worker return false;
30*288bf522SAndroid Build Coastguard Worker }
31*288bf522SAndroid Build Coastguard Worker std::shared_ptr<unwindstack::MapInfo> prev_real_map;
32*288bf522SAndroid Build Coastguard Worker for (auto& info : maps) {
33*288bf522SAndroid Build Coastguard Worker if (info == nullptr || map_set.maps.find(info->start()) == map_set.maps.end()) {
34*288bf522SAndroid Build Coastguard Worker return false;
35*288bf522SAndroid Build Coastguard Worker }
36*288bf522SAndroid Build Coastguard Worker if (prev_real_map != nullptr && prev_real_map->name() == info->name() &&
37*288bf522SAndroid Build Coastguard Worker prev_real_map != info->GetPrevRealMap()) {
38*288bf522SAndroid Build Coastguard Worker return false;
39*288bf522SAndroid Build Coastguard Worker }
40*288bf522SAndroid Build Coastguard Worker if (!info->IsBlank()) {
41*288bf522SAndroid Build Coastguard Worker prev_real_map = info;
42*288bf522SAndroid Build Coastguard Worker }
43*288bf522SAndroid Build Coastguard Worker }
44*288bf522SAndroid Build Coastguard Worker return true;
45*288bf522SAndroid Build Coastguard Worker }
46*288bf522SAndroid Build Coastguard Worker
47*288bf522SAndroid Build Coastguard Worker // @CddTest = 6.1/C-0-2
TEST(OfflineUnwinder,UnwindMaps)48*288bf522SAndroid Build Coastguard Worker TEST(OfflineUnwinder, UnwindMaps) {
49*288bf522SAndroid Build Coastguard Worker // 1. Create fake map entries.
50*288bf522SAndroid Build Coastguard Worker std::unique_ptr<Dso> fake_dso = Dso::CreateDso(DSO_UNKNOWN_FILE, "unknown");
51*288bf522SAndroid Build Coastguard Worker std::vector<MapEntry> map_entries;
52*288bf522SAndroid Build Coastguard Worker for (size_t i = 0; i < 10; i++) {
53*288bf522SAndroid Build Coastguard Worker map_entries.emplace_back(i, 1, i, fake_dso.get(), false);
54*288bf522SAndroid Build Coastguard Worker }
55*288bf522SAndroid Build Coastguard Worker
56*288bf522SAndroid Build Coastguard Worker // 2. Init with empty maps.
57*288bf522SAndroid Build Coastguard Worker MapSet map_set;
58*288bf522SAndroid Build Coastguard Worker UnwindMaps maps;
59*288bf522SAndroid Build Coastguard Worker maps.UpdateMaps(map_set);
60*288bf522SAndroid Build Coastguard Worker ASSERT_TRUE(CheckUnwindMaps(maps, map_set));
61*288bf522SAndroid Build Coastguard Worker
62*288bf522SAndroid Build Coastguard Worker // 3. Add maps starting from even addr.
63*288bf522SAndroid Build Coastguard Worker map_set.version = 1;
64*288bf522SAndroid Build Coastguard Worker for (size_t i = 0; i < map_entries.size(); i += 2) {
65*288bf522SAndroid Build Coastguard Worker map_set.maps.insert(std::make_pair(map_entries[i].start_addr, &map_entries[i]));
66*288bf522SAndroid Build Coastguard Worker }
67*288bf522SAndroid Build Coastguard Worker
68*288bf522SAndroid Build Coastguard Worker maps.UpdateMaps(map_set);
69*288bf522SAndroid Build Coastguard Worker ASSERT_TRUE(CheckUnwindMaps(maps, map_set));
70*288bf522SAndroid Build Coastguard Worker
71*288bf522SAndroid Build Coastguard Worker // 4. Add maps starting from odd addr.
72*288bf522SAndroid Build Coastguard Worker map_set.version = 2;
73*288bf522SAndroid Build Coastguard Worker for (size_t i = 1; i < 10; i += 2) {
74*288bf522SAndroid Build Coastguard Worker map_set.maps.insert(std::make_pair(map_entries[i].start_addr, &map_entries[i]));
75*288bf522SAndroid Build Coastguard Worker }
76*288bf522SAndroid Build Coastguard Worker maps.UpdateMaps(map_set);
77*288bf522SAndroid Build Coastguard Worker ASSERT_TRUE(CheckUnwindMaps(maps, map_set));
78*288bf522SAndroid Build Coastguard Worker
79*288bf522SAndroid Build Coastguard Worker // 5. Remove maps starting from even addr.
80*288bf522SAndroid Build Coastguard Worker map_set.version = 3;
81*288bf522SAndroid Build Coastguard Worker for (size_t i = 0; i < 10; i += 2) {
82*288bf522SAndroid Build Coastguard Worker map_set.maps.erase(map_entries[i].start_addr);
83*288bf522SAndroid Build Coastguard Worker }
84*288bf522SAndroid Build Coastguard Worker maps.UpdateMaps(map_set);
85*288bf522SAndroid Build Coastguard Worker ASSERT_TRUE(CheckUnwindMaps(maps, map_set));
86*288bf522SAndroid Build Coastguard Worker
87*288bf522SAndroid Build Coastguard Worker // 6. Remove all maps.
88*288bf522SAndroid Build Coastguard Worker map_set.version = 4;
89*288bf522SAndroid Build Coastguard Worker map_set.maps.clear();
90*288bf522SAndroid Build Coastguard Worker maps.UpdateMaps(map_set);
91*288bf522SAndroid Build Coastguard Worker ASSERT_TRUE(CheckUnwindMaps(maps, map_set));
92*288bf522SAndroid Build Coastguard Worker }
93*288bf522SAndroid Build Coastguard Worker
94*288bf522SAndroid Build Coastguard Worker // @CddTest = 6.1/C-0-2
TEST(OfflineUnwinder,CollectMetaInfo)95*288bf522SAndroid Build Coastguard Worker TEST(OfflineUnwinder, CollectMetaInfo) {
96*288bf522SAndroid Build Coastguard Worker std::unordered_map<std::string, std::string> info_map;
97*288bf522SAndroid Build Coastguard Worker OfflineUnwinder::CollectMetaInfo(&info_map);
98*288bf522SAndroid Build Coastguard Worker if (auto it = info_map.find(OfflineUnwinder::META_KEY_ARM64_PAC_MASK); it != info_map.end()) {
99*288bf522SAndroid Build Coastguard Worker uint64_t arm64_pack_mask;
100*288bf522SAndroid Build Coastguard Worker ASSERT_TRUE(android::base::ParseUint(it->second, &arm64_pack_mask));
101*288bf522SAndroid Build Coastguard Worker ASSERT_NE(arm64_pack_mask, 0);
102*288bf522SAndroid Build Coastguard Worker }
103*288bf522SAndroid Build Coastguard Worker }
104*288bf522SAndroid Build Coastguard Worker
105*288bf522SAndroid Build Coastguard Worker // @CddTest = 6.1/C-0-2
TEST(OfflineUnwinder,ARM64PackMask)106*288bf522SAndroid Build Coastguard Worker TEST(OfflineUnwinder, ARM64PackMask) {
107*288bf522SAndroid Build Coastguard Worker std::unordered_map<std::string, std::string> info_map;
108*288bf522SAndroid Build Coastguard Worker info_map[OfflineUnwinder::META_KEY_ARM64_PAC_MASK] = "0xff00000000";
109*288bf522SAndroid Build Coastguard Worker std::unique_ptr<OfflineUnwinderImpl> unwinder(new OfflineUnwinderImpl(false));
110*288bf522SAndroid Build Coastguard Worker unwinder->LoadMetaInfo(info_map);
111*288bf522SAndroid Build Coastguard Worker
112*288bf522SAndroid Build Coastguard Worker RegSet fake_regs(0, 0, nullptr);
113*288bf522SAndroid Build Coastguard Worker fake_regs.arch = ARCH_ARM64;
114*288bf522SAndroid Build Coastguard Worker unwindstack::Regs* regs = unwinder->GetBacktraceRegs(fake_regs);
115*288bf522SAndroid Build Coastguard Worker ASSERT_TRUE(regs != nullptr);
116*288bf522SAndroid Build Coastguard Worker auto& arm64 = *static_cast<unwindstack::RegsArm64*>(regs);
117*288bf522SAndroid Build Coastguard Worker arm64.SetPseudoRegister(unwindstack::Arm64Reg::ARM64_PREG_RA_SIGN_STATE, 1);
118*288bf522SAndroid Build Coastguard Worker arm64.set_pc(0xffccccccccULL);
119*288bf522SAndroid Build Coastguard Worker ASSERT_EQ(arm64.pc(), 0xccccccccULL);
120*288bf522SAndroid Build Coastguard Worker }
121