[Lldb-commits] [lldb] [lldb] Fix tests for FindInMemory SB API introduced in #95007. (PR #96565)

Miro Bucko via lldb-commits lldb-commits at lists.llvm.org
Mon Jun 24 15:10:17 PDT 2024


https://github.com/mbucko created https://github.com/llvm/llvm-project/pull/96565

None

>From b19c3daa8eb513eac9de77f9348b25035bdd9697 Mon Sep 17 00:00:00 2001
From: Miro Bucko <mbucko at meta.com>
Date: Mon, 24 Jun 2024 17:54:05 -0400
Subject: [PATCH] [lldb] Fix tests for FindInMemory SB API introduced in
 #95007.

---
 .../find_in_memory/TestFindInMemory.py        | 25 ++++++++++++++++++-
 .../find_in_memory/address_ranges_helper.py   |  3 ++-
 .../API/python_api/find_in_memory/main.cpp    | 21 ++++++++++++----
 3 files changed, 42 insertions(+), 7 deletions(-)

diff --git a/lldb/test/API/python_api/find_in_memory/TestFindInMemory.py b/lldb/test/API/python_api/find_in_memory/TestFindInMemory.py
index 4a459c47bcc02..9ab4619b1f8f4 100644
--- a/lldb/test/API/python_api/find_in_memory/TestFindInMemory.py
+++ b/lldb/test/API/python_api/find_in_memory/TestFindInMemory.py
@@ -21,10 +21,33 @@ def setUp(self):
             self.thread,
             self.bp,
         ) = lldbutil.run_to_source_breakpoint(
-            self, "break here", lldb.SBFileSpec("main.cpp")
+            self,
+            "break here",
+            lldb.SBFileSpec("main.cpp"),
         )
         self.assertTrue(self.bp.IsValid())
 
+    def test_check_stack_pointer(self):
+        """Make sure the 'stack_pointer' variable lives on the stack"""
+        self.assertTrue(self.process, PROCESS_IS_VALID)
+        self.assertState(self.process.GetState(), lldb.eStateStopped, PROCESS_STOPPED)
+
+        frame = self.thread.GetSelectedFrame()
+        ex = frame.EvaluateExpression("&stack_pointer")
+        variable_region = lldb.SBMemoryRegionInfo()
+        self.assertTrue(
+            self.process.GetMemoryRegionInfo(
+                ex.GetValueAsUnsigned(), variable_region
+            ).Success(),
+        )
+
+        stack_region = lldb.SBMemoryRegionInfo()
+        self.assertTrue(
+            self.process.GetMemoryRegionInfo(frame.GetSP(), stack_region).Success(),
+        )
+
+        self.assertEqual(variable_region, stack_region)
+
     def test_find_in_memory_ok(self):
         """Make sure a match exists in the heap memory and the right address ranges are provided"""
         self.assertTrue(self.process, PROCESS_IS_VALID)
diff --git a/lldb/test/API/python_api/find_in_memory/address_ranges_helper.py b/lldb/test/API/python_api/find_in_memory/address_ranges_helper.py
index 2c11fba80766f..810fb9fee3861 100644
--- a/lldb/test/API/python_api/find_in_memory/address_ranges_helper.py
+++ b/lldb/test/API/python_api/find_in_memory/address_ranges_helper.py
@@ -15,7 +15,7 @@ def GetAlignedRange(test_base):
 
 def GetStackRange(test_base):
     frame = test_base.thread.GetSelectedFrame()
-    ex = frame.EvaluateExpression("stack_pointer")
+    ex = frame.EvaluateExpression("&stack_pointer")
     test_base.assertTrue(ex.IsValid())
     return GetRangeFromAddrValue(test_base, ex)
 
@@ -35,6 +35,7 @@ def GetRangeFromAddrValue(test_base, addr):
     )
 
     test_base.assertTrue(region.IsReadable())
+    test_base.assertFalse(region.IsExecutable())
 
     address_start = lldb.SBAddress(region.GetRegionBase(), test_base.target)
     stack_size = region.GetRegionEnd() - region.GetRegionBase()
diff --git a/lldb/test/API/python_api/find_in_memory/main.cpp b/lldb/test/API/python_api/find_in_memory/main.cpp
index 98d378cb48b84..bb23ddd7389dd 100644
--- a/lldb/test/API/python_api/find_in_memory/main.cpp
+++ b/lldb/test/API/python_api/find_in_memory/main.cpp
@@ -1,10 +1,10 @@
-#include <cstdlib>
 #include <cstring>
+#include <memory>
 #include <string>
 
 int main() {
   // Stack
-  const char *stack_pointer = "stack_there_is_only_one_of_me";
+  const char stack_pointer[] = "stack_there_is_only_one_of_me";
 
   // Heap
   const std::string heap_string1("heap_there_is_exactly_two_of_me");
@@ -14,14 +14,25 @@ int main() {
 
   // Aligned Heap
   constexpr char aligned_string[] = "i_am_unaligned_string_on_the_heap";
+  constexpr size_t buffer_size = 100;
   constexpr size_t len = sizeof(aligned_string) + 1;
   // Allocate memory aligned to 8-byte boundary
-  void *aligned_string_ptr = aligned_alloc(8, len);
+  void *aligned_string_ptr = new size_t[buffer_size];
+  if (aligned_string_ptr == nullptr) {
+    return -1;
+  }
+  // Zero out the memory
+  memset(aligned_string_ptr, 0, buffer_size);
+
+  // Align the pointer to a multiple of 8 bytes
+  size_t size = buffer_size;
+  aligned_string_ptr = std::align(8, len, aligned_string_ptr, size);
+
+  // Copy the string to aligned memory
   memcpy(aligned_string_ptr, aligned_string, len);
 
   (void)stack_pointer;
   (void)heap_pointer1;
-  (void)heap_pointer2;
-  (void)aligned_string_ptr; // break here
+  (void)heap_pointer2; // break here
   return 0;
 }



More information about the lldb-commits mailing list