xref: /llvm-project/lldb/unittests/Target/MemoryTagMapTest.cpp (revision 2fe8327406050d2585d2ced910a678e28caefcf5)
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 #include <optional>
14 
15 using namespace lldb_private;
16 using namespace lldb;
17 
18 // In these tests we use the AArch64 MTE tag manager because it is the only
19 // implementation of a memory tag manager. MemoryTagMap itself is generic.
20 
TEST(MemoryTagMapTest,EmptyTagMap)21 TEST(MemoryTagMapTest, EmptyTagMap) {
22   MemoryTagManagerAArch64MTE manager;
23   MemoryTagMap tag_map(&manager);
24 
25   tag_map.InsertTags(0, {});
26   ASSERT_TRUE(tag_map.Empty());
27   tag_map.InsertTags(0, {0});
28   ASSERT_FALSE(tag_map.Empty());
29 }
30 
TEST(MemoryTagMapTest,GetTags)31 TEST(MemoryTagMapTest, GetTags) {
32   using TagsVec = std::vector<std::optional<lldb::addr_t>>;
33 
34   MemoryTagManagerAArch64MTE manager;
35   MemoryTagMap tag_map(&manager);
36 
37   // No tags for an address not in the map
38   ASSERT_TRUE(tag_map.GetTags(0, 16).empty());
39 
40   tag_map.InsertTags(0, {0, 1});
41 
42   // No tags if you read zero length
43   ASSERT_TRUE(tag_map.GetTags(0, 0).empty());
44 
45   EXPECT_THAT(tag_map.GetTags(0, 16), ::testing::ContainerEq(TagsVec{0}));
46 
47   EXPECT_THAT(tag_map.GetTags(0, 32), ::testing::ContainerEq(TagsVec{0, 1}));
48 
49   // Last granule of the range is not tagged
50   EXPECT_THAT(tag_map.GetTags(0, 48),
51               ::testing::ContainerEq(TagsVec{0, 1, std::nullopt}));
52 
53   EXPECT_THAT(tag_map.GetTags(16, 32),
54               ::testing::ContainerEq(TagsVec{1, std::nullopt}));
55 
56   // Reading beyond that address gives you no tags at all
57   EXPECT_THAT(tag_map.GetTags(32, 16), ::testing::ContainerEq(TagsVec{}));
58 
59   // Address is granule aligned for you
60   // The length here is set such that alignment doesn't produce a 2 granule
61   // range.
62   EXPECT_THAT(tag_map.GetTags(8, 8), ::testing::ContainerEq(TagsVec{0}));
63 
64   EXPECT_THAT(tag_map.GetTags(30, 2), ::testing::ContainerEq(TagsVec{1}));
65 
66   // Here the length pushes the range into the next granule. When aligned
67   // this produces 2 granules.
68   EXPECT_THAT(tag_map.GetTags(30, 4),
69               ::testing::ContainerEq(TagsVec{1, std::nullopt}));
70 
71   // A range can also have gaps at the beginning or in the middle.
72   // Add more tags, 1 granule away from the first range.
73   tag_map.InsertTags(48, {3, 4});
74 
75   // Untagged first granule
76   EXPECT_THAT(tag_map.GetTags(32, 32),
77               ::testing::ContainerEq(TagsVec{std::nullopt, 3}));
78 
79   // Untagged middle granule
80   EXPECT_THAT(tag_map.GetTags(16, 48),
81               ::testing::ContainerEq(TagsVec{1, std::nullopt, 3}));
82 }
83