Skip to content

[lldb] Fix SBAddressRange validation checks. #95997

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 21, 2024

Conversation

mbucko
Copy link
Contributor

@mbucko mbucko commented Jun 18, 2024

No description provided.

@mbucko mbucko requested a review from JDevlieghere as a code owner June 18, 2024 21:43
@llvmbot llvmbot added the lldb label Jun 18, 2024
@mbucko mbucko requested a review from clayborg June 18, 2024 21:44
@llvmbot
Copy link
Member

llvmbot commented Jun 18, 2024

@llvm/pr-subscribers-lldb

Author: Miro Bucko (mbucko)

Changes

Full diff: https://github.com/llvm/llvm-project/pull/95997.diff

2 Files Affected:

  • (modified) lldb/source/API/SBAddressRange.cpp (+3-15)
  • (modified) lldb/test/API/python_api/address_range/TestAddressRange.py (+1-1)
diff --git a/lldb/source/API/SBAddressRange.cpp b/lldb/source/API/SBAddressRange.cpp
index 9b1affdade439..0fba252979093 100644
--- a/lldb/source/API/SBAddressRange.cpp
+++ b/lldb/source/API/SBAddressRange.cpp
@@ -50,8 +50,6 @@ 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));
 }
 
@@ -64,28 +62,24 @@ bool SBAddressRange::operator!=(const SBAddressRange &rhs) {
 void SBAddressRange::Clear() {
   LLDB_INSTRUMENT_VA(this);
 
-  m_opaque_up.reset();
+  m_opaque_up->Clear();
 }
 
 bool SBAddressRange::IsValid() const {
   LLDB_INSTRUMENT_VA(this);
 
-  return m_opaque_up && m_opaque_up->IsValid();
+  return m_opaque_up->IsValid();
 }
 
 lldb::SBAddress SBAddressRange::GetBaseAddress() const {
   LLDB_INSTRUMENT_VA(this);
 
-  if (!IsValid())
-    return lldb::SBAddress();
   return lldb::SBAddress(m_opaque_up->GetBaseAddress());
 }
 
 lldb::addr_t SBAddressRange::GetByteSize() const {
   LLDB_INSTRUMENT_VA(this);
 
-  if (!IsValid())
-    return 0;
   return m_opaque_up->GetByteSize();
 }
 
@@ -93,11 +87,5 @@ 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 m_opaque_up->GetDescription(&description.ref(), target.GetSP().get());
 }
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."""

Copy link
Member

@JDevlieghere JDevlieghere left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This class behaves quite differently from other SB API classes. Normally, the opaque pointer can be cleared to release the potentially more resource heavy private counterpart. AddressRange is a pretty simple class, so I understand that it makes things easier if we guarantee the pointer is always valid, but it is somewhat of a surprise.

Personally, I think consistency beats the small advantage of not having to check the pointer. If we want to stick to this approach, I'd like to see an assert that makes it clear that in this class, we have a precondition that the pointer is always valid:

assert(m_opaque_up && "opaque pointer must always be valid");

@mbucko
Copy link
Contributor Author

mbucko commented Jun 20, 2024

This class behaves quite differently from other SB API classes. Normally, the opaque pointer can be cleared to release the potentially more resource heavy private counterpart. AddressRange is a pretty simple class, so I understand that it makes things easier if we guarantee the pointer is always valid, but it is somewhat of a surprise.

Personally, I think consistency beats the small advantage of not having to check the pointer. If we want to stick to this approach, I'd like to see an assert that makes it clear that in this class, we have a precondition that the pointer is always valid:

assert(m_opaque_up && "opaque pointer must always be valid");

So you're ok with keeping the code as is plus adding the asserts?

@mbucko mbucko force-pushed the sb_address_range_fix branch from 786e94d to 59c382f Compare June 20, 2024 17:22
@clayborg
Copy link
Collaborator

This class behaves quite differently from other SB API classes. Normally, the opaque pointer can be cleared to release the potentially more resource heavy private counterpart. AddressRange is a pretty simple class, so I understand that it makes things easier if we guarantee the pointer is always valid, but it is somewhat of a surprise.

Personally, I think consistency beats the small advantage of not having to check the pointer. If we want to stick to this approach, I'd like to see an assert that makes it clear that in this class, we have a precondition that the pointer is always valid:

assert(m_opaque_up && "opaque pointer must always be valid");

For simple classes, there is no need to clear the unique pointer, so I like this approach for small classes.

We can use the assert in a new protected ref() method:

lldb_private::AddressRange & SBAddressRange::ref() {
  assert(m_opaque_up && "opaque pointer must always be valid");
  return *m_opaque_up;
}

And then have everything that accesses m_opaque_up use the ref() function. It is similar to other classes and makes the code nicer when we don't see direct uses of m_opaque_up

@mbucko mbucko force-pushed the sb_address_range_fix branch from 59c382f to ca4dcf3 Compare June 20, 2024 17:37
@mbucko mbucko force-pushed the sb_address_range_fix branch from ca4dcf3 to 3ba6a55 Compare June 20, 2024 20:44
@mbucko mbucko merged commit a083e50 into llvm:main Jun 21, 2024
6 checks passed
@mbucko mbucko deleted the sb_address_range_fix branch June 21, 2024 15:24
AlexisPerry pushed a commit to llvm-project-tlp/llvm-project that referenced this pull request Jul 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants