1 //===-- MemoryTagMapTest.cpp ----------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8
9 #include "lldb/Target/MemoryTagMap.h"
10 #include "Plugins/Process/Utility/MemoryTagManagerAArch64MTE.h"
11 #include "gmock/gmock.h"
12 #include "gtest/gtest.h"
13
14 using namespace lldb_private;
15 using namespace lldb;
16
17 // In these tests we use the AArch64 MTE tag manager because it is the only
18 // implementation of a memory tag manager. MemoryTagMap itself is generic.
19
TEST(MemoryTagMapTest,EmptyTagMap)20 TEST(MemoryTagMapTest, EmptyTagMap) {
21 MemoryTagManagerAArch64MTE manager;
22 MemoryTagMap tag_map(&manager);
23
24 tag_map.InsertTags(0, {});
25 ASSERT_TRUE(tag_map.Empty());
26 tag_map.InsertTags(0, {0});
27 ASSERT_FALSE(tag_map.Empty());
28 }
29
TEST(MemoryTagMapTest,GetTags)30 TEST(MemoryTagMapTest, GetTags) {
31 using TagsVec = std::vector<llvm::Optional<lldb::addr_t>>;
32
33 MemoryTagManagerAArch64MTE manager;
34 MemoryTagMap tag_map(&manager);
35
36 // No tags for an address not in the map
37 ASSERT_TRUE(tag_map.GetTags(0, 16).empty());
38
39 tag_map.InsertTags(0, {0, 1});
40
41 // No tags if you read zero length
42 ASSERT_TRUE(tag_map.GetTags(0, 0).empty());
43
44 EXPECT_THAT(tag_map.GetTags(0, 16), ::testing::ContainerEq(TagsVec{0}));
45
46 EXPECT_THAT(tag_map.GetTags(0, 32), ::testing::ContainerEq(TagsVec{0, 1}));
47
48 // Last granule of the range is not tagged
49 EXPECT_THAT(tag_map.GetTags(0, 48),
50 ::testing::ContainerEq(TagsVec{0, 1, llvm::None}));
51
52 EXPECT_THAT(tag_map.GetTags(16, 32),
53 ::testing::ContainerEq(TagsVec{1, llvm::None}));
54
55 // Reading beyond that address gives you no tags at all
56 EXPECT_THAT(tag_map.GetTags(32, 16), ::testing::ContainerEq(TagsVec{}));
57
58 // Address is granule aligned for you
59 // The length here is set such that alignment doesn't produce a 2 granule
60 // range.
61 EXPECT_THAT(tag_map.GetTags(8, 8), ::testing::ContainerEq(TagsVec{0}));
62
63 EXPECT_THAT(tag_map.GetTags(30, 2), ::testing::ContainerEq(TagsVec{1}));
64
65 // Here the length pushes the range into the next granule. When aligned
66 // this produces 2 granules.
67 EXPECT_THAT(tag_map.GetTags(30, 4),
68 ::testing::ContainerEq(TagsVec{1, llvm::None}));
69
70 // A range can also have gaps at the beginning or in the middle.
71 // Add more tags, 1 granule away from the first range.
72 tag_map.InsertTags(48, {3, 4});
73
74 // Untagged first granule
75 EXPECT_THAT(tag_map.GetTags(32, 32),
76 ::testing::ContainerEq(TagsVec{llvm::None, 3}));
77
78 // Untagged middle granule
79 EXPECT_THAT(tag_map.GetTags(16, 48),
80 ::testing::ContainerEq(TagsVec{1, llvm::None, 3}));
81 }
82