Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:42:53

0001 //===-- MemoryTagMap.h ------------------------------------------*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 
0009 #ifndef LLDB_TARGET_MEMORYTAGMAP_H
0010 #define LLDB_TARGET_MEMORYTAGMAP_H
0011 
0012 #include "lldb/Target/MemoryTagManager.h"
0013 #include "lldb/lldb-private.h"
0014 #include <map>
0015 #include <optional>
0016 
0017 namespace lldb_private {
0018 
0019 /// MemoryTagMap provides a way to give a sparse read result
0020 /// when reading memory tags for a range. This is useful when
0021 /// you want to annotate some large memory dump that might include
0022 /// tagged memory but you don't know that it is all tagged.
0023 class MemoryTagMap {
0024 public:
0025   /// Init an empty tag map
0026   ///
0027   /// \param [in] manager
0028   ///     Non-null pointer to a memory tag manager.
0029   MemoryTagMap(const MemoryTagManager *manager);
0030 
0031   /// Insert tags into the map starting from addr.
0032   ///
0033   /// \param [in] addr
0034   ///     Start address of the range to insert tags for.
0035   ///     This address should be granule aligned and have had
0036   ///     any non address bits removed.
0037   ///     (ideally you would use the base of the range you used
0038   ///     to read the tags in the first place)
0039   ///
0040   /// \param [in] tags
0041   ///     Vector of tags to insert. The first tag will be inserted
0042   ///     at addr, the next at addr+granule size and so on until
0043   ///     all tags have been inserted.
0044   void InsertTags(lldb::addr_t addr, const std::vector<lldb::addr_t> tags);
0045 
0046   bool Empty() const;
0047 
0048   /// Lookup memory tags for a range of memory from addr to addr+len.
0049   ///
0050   /// \param [in] addr
0051   ///    The start of the range. This may include non address bits and
0052   ///    does not have to be granule aligned.
0053   ///
0054   /// \param [in] len
0055   ///    The length in bytes of the range to read tags for. This does
0056   ///    not need to be multiple of the granule size.
0057   ///
0058   /// \return
0059   ///    A vector containing the tags found for the granules in the
0060   ///    range. (which is the result of granule aligning the given range)
0061   ///
0062   ///    Each item in the vector is an optional tag. Meaning that if
0063   ///    it is valid then the granule had a tag and if not, it didn't.
0064   ///
0065   ///    If the range had no tags at all, the vector will be empty.
0066   ///    If some of the range was tagged it will have items and some
0067   ///    of them may be std::nullopt.
0068   ///    (this saves the caller checking whether all items are std::nullopt)
0069   std::vector<std::optional<lldb::addr_t>> GetTags(lldb::addr_t addr,
0070                                                    size_t len) const;
0071 
0072 private:
0073   /// Lookup the tag for address
0074   ///
0075   /// \param [in] address
0076   ///     The address to lookup a tag for. This should be aligned
0077   ///     to a granule boundary.
0078   ///
0079   /// \return
0080   ///     The tag for the granule that address refers to, or std::nullopt
0081   ///     if it has no memory tag.
0082   std::optional<lldb::addr_t> GetTag(lldb::addr_t addr) const;
0083 
0084   // A map of granule aligned addresses to their memory tag
0085   std::map<lldb::addr_t, lldb::addr_t> m_addr_to_tag;
0086 
0087   // Memory tag manager used to align addresses and get granule size.
0088   // Ideally this would be a const& but only certain architectures will
0089   // have a memory tag manager class to provide here. So for a method
0090   // returning a MemoryTagMap, std::optional<MemoryTagMap> allows it to handle
0091   // architectures without memory tagging. Optionals cannot hold references
0092   // so we go with a pointer that we assume will be not be null.
0093   const MemoryTagManager *m_manager;
0094 };
0095 
0096 } // namespace lldb_private
0097 
0098 #endif // LLDB_TARGET_MEMORYTAGMAP_H