[Lldb-commits] [lldb] 72e2597 - [lldb/API] Add SetDataWithOwnership method to SBData

Med Ismail Bennani via lldb-commits lldb-commits at lists.llvm.org
Mon Dec 13 11:05:27 PST 2021


Author: Med Ismail Bennani
Date: 2021-12-13T11:05:06-08:00
New Revision: 72e25978f93f0bf7577593aba9591c727526423c

URL: https://github.com/llvm/llvm-project/commit/72e25978f93f0bf7577593aba9591c727526423c
DIFF: https://github.com/llvm/llvm-project/commit/72e25978f93f0bf7577593aba9591c727526423c.diff

LOG: [lldb/API] Add SetDataWithOwnership method to SBData

This patch introduces a new method to SBData: SetDataWithOwnership.

Instead of referencing the pointer to the data, this method copies the
data buffer into lldb's heap memory.

This can prevent having the underlying DataExtractor object point to
freed/garbage-collected memory.

Differential Revision: https://reviews.llvm.org/D115652

Signed-off-by: Med Ismail Bennani <medismail.bennani at gmail.com>

Added: 
    

Modified: 
    lldb/bindings/interface/SBData.i
    lldb/include/lldb/API/SBData.h
    lldb/source/API/SBData.cpp
    lldb/test/API/python_api/sbdata/TestSBData.py

Removed: 
    


################################################################################
diff  --git a/lldb/bindings/interface/SBData.i b/lldb/bindings/interface/SBData.i
index a1fb4472cd234..1f2f9fbf05e2d 100644
--- a/lldb/bindings/interface/SBData.i
+++ b/lldb/bindings/interface/SBData.i
@@ -96,6 +96,10 @@ public:
     void
     SetData (lldb::SBError& error, const void *buf, size_t size, lldb::ByteOrder endian, uint8_t addr_size);
 
+    void
+    SetDataWithOwnership (lldb::SBError& error, const void *buf, size_t size,
+                          lldb::ByteOrder endian, uint8_t addr_size);
+
     bool
     Append (const SBData& rhs);
 

diff  --git a/lldb/include/lldb/API/SBData.h b/lldb/include/lldb/API/SBData.h
index 85c8110e181c2..89a699f2f713a 100644
--- a/lldb/include/lldb/API/SBData.h
+++ b/lldb/include/lldb/API/SBData.h
@@ -83,6 +83,9 @@ class LLDB_API SBData {
   void SetData(lldb::SBError &error, const void *buf, size_t size,
                lldb::ByteOrder endian, uint8_t addr_size);
 
+  void SetDataWithOwnership(lldb::SBError &error, const void *buf, size_t size,
+                            lldb::ByteOrder endian, uint8_t addr_size);
+
   // see SetData() for why we don't have Append(const void* buf, size_t size)
   bool Append(const SBData &rhs);
 

diff  --git a/lldb/source/API/SBData.cpp b/lldb/source/API/SBData.cpp
index daf313ad55c99..9fc590578bceb 100644
--- a/lldb/source/API/SBData.cpp
+++ b/lldb/source/API/SBData.cpp
@@ -374,6 +374,25 @@ void SBData::SetData(lldb::SBError &error, const void *buf, size_t size,
   }
 }
 
+void SBData::SetDataWithOwnership(lldb::SBError &error, const void *buf,
+                                  size_t size, lldb::ByteOrder endian,
+                                  uint8_t addr_size) {
+  LLDB_RECORD_DUMMY(
+      void, SBData, SetData,
+      (lldb::SBError &, const void *, size_t, lldb::ByteOrder, uint8_t, bool),
+      error, buf, size, endian, addr_size, copy);
+
+  lldb::DataBufferSP buffer_sp = std::make_shared<DataBufferHeap>(buf, size);
+
+  if (!m_opaque_sp.get())
+    m_opaque_sp = std::make_shared<DataExtractor>(buf, size, endian, addr_size);
+  else {
+    m_opaque_sp->SetData(buffer_sp);
+    m_opaque_sp->SetByteOrder(endian);
+    m_opaque_sp->SetAddressByteSize(addr_size);
+  }
+}
+
 bool SBData::Append(const SBData &rhs) {
   LLDB_RECORD_METHOD(bool, SBData, Append, (const lldb::SBData &), rhs);
 

diff  --git a/lldb/test/API/python_api/sbdata/TestSBData.py b/lldb/test/API/python_api/sbdata/TestSBData.py
index b845c0abd734d..1b20cf205e63d 100644
--- a/lldb/test/API/python_api/sbdata/TestSBData.py
+++ b/lldb/test/API/python_api/sbdata/TestSBData.py
@@ -39,6 +39,17 @@ def test_byte_order_and_address_byte_size(self):
         addr = data.GetAddress(error, 0)
         self.assertEqual(addr, 0x8877665544332211);
 
+    def test_byte_order_and_address_byte_size_with_ownership(self):
+        """Test the SBData::SetDataWithOwnership() to ensure the byte order
+        and address byte size are obeyed even when source date is released"""
+        addr_data = b'\x11\x22\x33\x44\x55\x66\x77\x88'
+        error = lldb.SBError()
+        data = lldb.SBData()
+        data.SetDataWithOwnership(error, addr_data, lldb.eByteOrderBig, 8)
+        del addr_data
+        addr = data.GetAddress(error, 0)
+        self.assertEqual(addr, 0x1122334455667788);
+
     def test_with_run_command(self):
         """Test the SBData APIs."""
         self.build()


        


More information about the lldb-commits mailing list