[Lldb-commits] [lldb] [lldb] Fix SBAddressRange validation checks. (PR #95997)

Miro Bucko via lldb-commits lldb-commits at lists.llvm.org
Thu Jun 20 10:37:40 PDT 2024


https://github.com/mbucko updated https://github.com/llvm/llvm-project/pull/95997

>From ca4dcf373b0617ab588ee804e3d28f8d311da06a Mon Sep 17 00:00:00 2001
From: Miro Bucko <mbucko at meta.com>
Date: Tue, 18 Jun 2024 14:35:55 -0700
Subject: [PATCH] [lldb] Fix SBAddressRange validation checks.

---
 lldb/include/lldb/API/SBAddressRange.h        |  6 ++++
 lldb/include/lldb/API/SBAddressRangeList.h    |  2 ++
 lldb/source/API/SBAddressRange.cpp            | 29 +++++++------------
 lldb/source/API/SBAddressRangeList.cpp        | 17 +++++++----
 .../address_range/TestAddressRange.py         |  2 +-
 5 files changed, 31 insertions(+), 25 deletions(-)

diff --git a/lldb/include/lldb/API/SBAddressRange.h b/lldb/include/lldb/API/SBAddressRange.h
index 152bd82426af1..5c4d6b8c5683d 100644
--- a/lldb/include/lldb/API/SBAddressRange.h
+++ b/lldb/include/lldb/API/SBAddressRange.h
@@ -11,6 +11,10 @@
 
 #include "lldb/API/SBDefines.h"
 
+namespace lldb_private {
+class AddressRange;
+}
+
 namespace lldb {
 
 class LLDB_API SBAddressRange {
@@ -58,6 +62,8 @@ class LLDB_API SBAddressRange {
   friend class SBFunction;
   friend class SBProcess;
 
+  lldb_private::AddressRange &ref() const;
+
   AddressRangeUP m_opaque_up;
 };
 
diff --git a/lldb/include/lldb/API/SBAddressRangeList.h b/lldb/include/lldb/API/SBAddressRangeList.h
index a123287ef1b4f..5a4eeecf37dc9 100644
--- a/lldb/include/lldb/API/SBAddressRangeList.h
+++ b/lldb/include/lldb/API/SBAddressRangeList.h
@@ -46,6 +46,8 @@ class LLDB_API SBAddressRangeList {
   friend class SBBlock;
   friend class SBProcess;
 
+  lldb_private::AddressRangeListImpl &ref() const;
+
   std::unique_ptr<lldb_private::AddressRangeListImpl> m_opaque_up;
 };
 
diff --git a/lldb/source/API/SBAddressRange.cpp b/lldb/source/API/SBAddressRange.cpp
index 9b1affdade439..5834ebe5e63c0 100644
--- a/lldb/source/API/SBAddressRange.cpp
+++ b/lldb/source/API/SBAddressRange.cpp
@@ -50,9 +50,7 @@ const SBAddressRange &SBAddressRange::operator=(const SBAddressRange &rhs) {
 bool SBAddressRange::operator==(const SBAddressRange &rhs) {
   LLDB_INSTRUMENT_VA(this, rhs);
 
-  if (!IsValid() || !rhs.IsValid())
-    return false;
-  return m_opaque_up->operator==(*(rhs.m_opaque_up));
+  return ref().operator==(rhs.ref());
 }
 
 bool SBAddressRange::operator!=(const SBAddressRange &rhs) {
@@ -64,40 +62,35 @@ bool SBAddressRange::operator!=(const SBAddressRange &rhs) {
 void SBAddressRange::Clear() {
   LLDB_INSTRUMENT_VA(this);
 
-  m_opaque_up.reset();
+  ref().Clear();
 }
 
 bool SBAddressRange::IsValid() const {
   LLDB_INSTRUMENT_VA(this);
 
-  return m_opaque_up && m_opaque_up->IsValid();
+  return ref().IsValid();
 }
 
 lldb::SBAddress SBAddressRange::GetBaseAddress() const {
   LLDB_INSTRUMENT_VA(this);
 
-  if (!IsValid())
-    return lldb::SBAddress();
-  return lldb::SBAddress(m_opaque_up->GetBaseAddress());
+  return lldb::SBAddress(ref().GetBaseAddress());
 }
 
 lldb::addr_t SBAddressRange::GetByteSize() const {
   LLDB_INSTRUMENT_VA(this);
 
-  if (!IsValid())
-    return 0;
-  return m_opaque_up->GetByteSize();
+  return ref().GetByteSize();
 }
 
 bool SBAddressRange::GetDescription(SBStream &description,
                                     const SBTarget target) {
   LLDB_INSTRUMENT_VA(this, description, target);
 
-  Stream &stream = description.ref();
-  if (!IsValid()) {
-    stream << "<invalid>";
-    return true;
-  }
-  m_opaque_up->GetDescription(&stream, target.GetSP().get());
-  return true;
+  return ref().GetDescription(&description.ref(), target.GetSP().get());
+}
+
+lldb_private::AddressRange &SBAddressRange::ref() const {
+  assert(m_opaque_up && "opaque pointer must always be valid");
+  return *m_opaque_up;
 }
diff --git a/lldb/source/API/SBAddressRangeList.cpp b/lldb/source/API/SBAddressRangeList.cpp
index 20660b3ff2088..957155d5125ef 100644
--- a/lldb/source/API/SBAddressRangeList.cpp
+++ b/lldb/source/API/SBAddressRangeList.cpp
@@ -37,40 +37,40 @@ SBAddressRangeList::operator=(const SBAddressRangeList &rhs) {
   LLDB_INSTRUMENT_VA(this, rhs);
 
   if (this != &rhs)
-    *m_opaque_up = *rhs.m_opaque_up;
+    ref() = rhs.ref();
   return *this;
 }
 
 uint32_t SBAddressRangeList::GetSize() const {
   LLDB_INSTRUMENT_VA(this);
 
-  return m_opaque_up->GetSize();
+  return ref().GetSize();
 }
 
 SBAddressRange SBAddressRangeList::GetAddressRangeAtIndex(uint64_t idx) {
   LLDB_INSTRUMENT_VA(this, idx);
 
   SBAddressRange sb_addr_range;
-  (*sb_addr_range.m_opaque_up) = m_opaque_up->GetAddressRangeAtIndex(idx);
+  (*sb_addr_range.m_opaque_up) = ref().GetAddressRangeAtIndex(idx);
   return sb_addr_range;
 }
 
 void SBAddressRangeList::Clear() {
   LLDB_INSTRUMENT_VA(this);
 
-  m_opaque_up->Clear();
+  ref().Clear();
 }
 
 void SBAddressRangeList::Append(const SBAddressRange &sb_addr_range) {
   LLDB_INSTRUMENT_VA(this, sb_addr_range);
 
-  m_opaque_up->Append(*sb_addr_range.m_opaque_up);
+  ref().Append(*sb_addr_range.m_opaque_up);
 }
 
 void SBAddressRangeList::Append(const SBAddressRangeList &sb_addr_range_list) {
   LLDB_INSTRUMENT_VA(this, sb_addr_range_list);
 
-  m_opaque_up->Append(*sb_addr_range_list.m_opaque_up);
+  ref().Append(*sb_addr_range_list.m_opaque_up);
 }
 
 bool SBAddressRangeList::GetDescription(SBStream &description,
@@ -92,3 +92,8 @@ bool SBAddressRangeList::GetDescription(SBStream &description,
   stream << "]";
   return true;
 }
+
+lldb_private::AddressRangeListImpl &SBAddressRangeList::ref() const {
+  assert(m_opaque_up && "opaque pointer must always be valid");
+  return *m_opaque_up;
+}
diff --git a/lldb/test/API/python_api/address_range/TestAddressRange.py b/lldb/test/API/python_api/address_range/TestAddressRange.py
index 86ca4a62155f0..ae4b8c7c90ce4 100644
--- a/lldb/test/API/python_api/address_range/TestAddressRange.py
+++ b/lldb/test/API/python_api/address_range/TestAddressRange.py
@@ -166,7 +166,7 @@ def test_address_range_list_iterator(self):
     def test_address_range_print_invalid(self):
         """Make sure the SBAddressRange can be printed when invalid."""
         range = lldb.SBAddressRange()
-        self.assertEqual(str(range), "<invalid>")
+        self.assertEqual(str(range), "[0xffffffffffffffff-0xffffffffffffffff)")
 
     def test_address_range_print_resolved(self):
         """Make sure the SBAddressRange can be printed when resolved."""



More information about the lldb-commits mailing list