Skip to content

Conversation

mbucko
Copy link
Contributor

@mbucko mbucko commented Jun 24, 2024

No description provided.

@mbucko mbucko requested a review from JDevlieghere as a code owner June 24, 2024 22:10
@llvmbot llvmbot added the lldb label Jun 24, 2024
@llvmbot
Copy link
Member

llvmbot commented Jun 24, 2024

@llvm/pr-subscribers-lldb

Author: Miro Bucko (mbucko)

Changes

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

3 Files Affected:

  • (modified) lldb/test/API/python_api/find_in_memory/TestFindInMemory.py (+24-1)
  • (modified) lldb/test/API/python_api/find_in_memory/address_ranges_helper.py (+2-1)
  • (modified) lldb/test/API/python_api/find_in_memory/main.cpp (+16-5)
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;
 }

Copy link
Collaborator

@clayborg clayborg left a comment

Choose a reason for hiding this comment

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

Just add a description to what this test had wrong, what is fixed and any other needed details and this should be good to go

@mbucko mbucko closed this Jun 24, 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.

3 participants