[Lldb-commits] [PATCH] D115652: [lldb/API] Add SetDataWithOwnership method to SBData

Med Ismail Bennani via Phabricator via lldb-commits lldb-commits at lists.llvm.org
Mon Dec 13 10:40:15 PST 2021


mib created this revision.
mib added a reviewer: JDevlieghere.
mib added a project: LLDB.
Herald added a subscriber: yaxunl.
mib requested review of this revision.
Herald added a subscriber: lldb-commits.

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.

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


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D115652

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


Index: lldb/test/API/python_api/sbdata/TestSBData.py
===================================================================
--- lldb/test/API/python_api/sbdata/TestSBData.py
+++ lldb/test/API/python_api/sbdata/TestSBData.py
@@ -39,6 +39,17 @@
         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()
Index: lldb/source/API/SBData.cpp
===================================================================
--- lldb/source/API/SBData.cpp
+++ lldb/source/API/SBData.cpp
@@ -374,6 +374,25 @@
   }
 }
 
+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);
 
Index: lldb/include/lldb/API/SBData.h
===================================================================
--- lldb/include/lldb/API/SBData.h
+++ lldb/include/lldb/API/SBData.h
@@ -83,6 +83,9 @@
   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);
 
Index: lldb/bindings/interface/SBData.i
===================================================================
--- lldb/bindings/interface/SBData.i
+++ lldb/bindings/interface/SBData.i
@@ -96,6 +96,10 @@
     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);
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D115652.393960.patch
Type: text/x-patch
Size: 3137 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/lldb-commits/attachments/20211213/e8c8fb0e/attachment.bin>


More information about the lldb-commits mailing list