LLDB  mainline
MemoryTagMap.cpp
Go to the documentation of this file.
1 //===-- MemoryTagMap.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 
10 
11 using namespace lldb_private;
12 
14  : m_manager(manager) {
15  assert(m_manager && "valid tag manager required to construct a MemoryTagMap");
16 }
17 
19  const std::vector<lldb::addr_t> tags) {
20  // We're assuming that addr has no non address bits and is granule aligned.
21  size_t granule_size = m_manager->GetGranuleSize();
22  for (auto tag : tags) {
23  m_addr_to_tag[addr] = tag;
24  addr += granule_size;
25  }
26 }
27 
28 bool MemoryTagMap::Empty() const { return m_addr_to_tag.empty(); }
29 
30 std::vector<llvm::Optional<lldb::addr_t>>
31 MemoryTagMap::GetTags(lldb::addr_t addr, size_t len) const {
32  // Addr and len might be unaligned
33  addr = m_manager->RemoveTagBits(addr);
34  MemoryTagManager::TagRange range(addr, len);
35  range = m_manager->ExpandToGranule(range);
36 
37  std::vector<llvm::Optional<lldb::addr_t>> tags;
38  lldb::addr_t end_addr = range.GetRangeEnd();
39  addr = range.GetRangeBase();
40  bool got_valid_tags = false;
41  size_t granule_size = m_manager->GetGranuleSize();
42 
43  for (; addr < end_addr; addr += granule_size) {
44  llvm::Optional<lldb::addr_t> tag = GetTag(addr);
45  tags.push_back(tag);
46  if (tag)
47  got_valid_tags = true;
48  }
49 
50  // To save the caller checking if every item is llvm::None,
51  // we return an empty vector if we got no tags at all.
52  if (got_valid_tags)
53  return tags;
54  return {};
55 }
56 
57 llvm::Optional<lldb::addr_t> MemoryTagMap::GetTag(lldb::addr_t addr) const {
58  // Here we assume that addr is granule aligned, just like when the tags
59  // were inserted.
60  auto found = m_addr_to_tag.find(addr);
61  if (found == m_addr_to_tag.end())
62  return llvm::None;
63  return found->second;
64 }
lldb_private::Range::GetRangeBase
BaseType GetRangeBase() const
Definition: RangeMap.h:46
lldb_private::MemoryTagMap::Empty
bool Empty() const
Definition: MemoryTagMap.cpp:28
lldb::addr_t
uint64_t addr_t
Definition: lldb-types.h:83
lldb_private::MemoryTagManager
Definition: MemoryTagManager.h:28
MemoryTagMap.h
lldb_private::Range< lldb::addr_t, lldb::addr_t >
lldb_private::MemoryTagManager::RemoveTagBits
virtual lldb::addr_t RemoveTagBits(lldb::addr_t addr) const =0
lldb_private::MemoryTagMap::m_addr_to_tag
std::map< lldb::addr_t, lldb::addr_t > m_addr_to_tag
Definition: MemoryTagMap.h:85
lldb_private::MemoryTagMap::InsertTags
void InsertTags(lldb::addr_t addr, const std::vector< lldb::addr_t > tags)
Insert tags into the map starting from addr.
Definition: MemoryTagMap.cpp:18
lldb_private::MemoryTagMap::MemoryTagMap
MemoryTagMap(const MemoryTagManager *manager)
Init an empty tag map.
Definition: MemoryTagMap.cpp:13
lldb_private::MemoryTagManager::GetGranuleSize
virtual lldb::addr_t GetGranuleSize() const =0
lldb_private::MemoryTagMap::GetTag
llvm::Optional< lldb::addr_t > GetTag(lldb::addr_t addr) const
Lookup the tag for address.
Definition: MemoryTagMap.cpp:57
lldb_private::MemoryTagMap::GetTags
std::vector< llvm::Optional< lldb::addr_t > > GetTags(lldb::addr_t addr, size_t len) const
Lookup memory tags for a range of memory from addr to addr+len.
Definition: MemoryTagMap.cpp:31
lldb_private
A class that represents a running process on the host machine.
Definition: SBCommandInterpreterRunOptions.h:16
lldb_private::MemoryTagMap::m_manager
const MemoryTagManager * m_manager
Definition: MemoryTagMap.h:93
lldb_private::MemoryTagManager::ExpandToGranule
virtual TagRange ExpandToGranule(TagRange range) const =0
lldb_private::Range::GetRangeEnd
BaseType GetRangeEnd() const
Definition: RangeMap.h:78