[Lldb-commits] [lldb] a1a1a4c - [lldb] Handle an empty SBMemoryRegionInfo from scripted process (#115963)
via lldb-commits
lldb-commits at lists.llvm.org
Fri Nov 15 00:26:13 PST 2024
Author: Jason Molenda
Date: 2024-11-15T00:26:10-08:00
New Revision: a1a1a4ced9d4ecba428175c45a24da476bdc55f4
URL: https://github.com/llvm/llvm-project/commit/a1a1a4ced9d4ecba428175c45a24da476bdc55f4
DIFF: https://github.com/llvm/llvm-project/commit/a1a1a4ced9d4ecba428175c45a24da476bdc55f4.diff
LOG: [lldb] Handle an empty SBMemoryRegionInfo from scripted process (#115963)
A scripted process implementation might return an SBMemoryRegionInfo
object in its implementation of `get_memory_region_containing_address`
which will have an address 0 and size 0, without realizing the problems
this can cause. Several algorithms in lldb will try to iterate over the
MemoryRegions of the process, starting at address 0 and expecting to
iterate up to the highest vm address, stepping by the size of each
region, so a 0-length region will result in an infinite loop. Add a
check to Process::GetMemoryRegionInfo that rejects a MemoryRegion which
does not contain the requested address; a 0-length memory region will
therefor always be rejected.
rdar://139678032
Added:
lldb/test/API/functionalities/scripted_process_empty_memory_region/Makefile
lldb/test/API/functionalities/scripted_process_empty_memory_region/TestScriptedProcessEmptyMemoryRegion.py
lldb/test/API/functionalities/scripted_process_empty_memory_region/dummy_scripted_process.py
lldb/test/API/functionalities/scripted_process_empty_memory_region/main.c
Modified:
lldb/source/Target/Process.cpp
Removed:
################################################################################
diff --git a/lldb/source/Target/Process.cpp b/lldb/source/Target/Process.cpp
index b99692b8a0bfd9..9125ceca74a003 100644
--- a/lldb/source/Target/Process.cpp
+++ b/lldb/source/Target/Process.cpp
@@ -6184,7 +6184,12 @@ Status Process::GetMemoryRegionInfo(lldb::addr_t load_addr,
MemoryRegionInfo &range_info) {
if (const lldb::ABISP &abi = GetABI())
load_addr = abi->FixAnyAddress(load_addr);
- return DoGetMemoryRegionInfo(load_addr, range_info);
+ Status error = DoGetMemoryRegionInfo(load_addr, range_info);
+ // Reject a region that does not contain the requested address.
+ if (error.Success() && !range_info.GetRange().Contains(load_addr))
+ error = Status::FromErrorString("Invalid memory region");
+
+ return error;
}
Status Process::GetMemoryRegions(lldb_private::MemoryRegionInfos ®ion_list) {
diff --git a/lldb/test/API/functionalities/scripted_process_empty_memory_region/Makefile b/lldb/test/API/functionalities/scripted_process_empty_memory_region/Makefile
new file mode 100644
index 00000000000000..10495940055b63
--- /dev/null
+++ b/lldb/test/API/functionalities/scripted_process_empty_memory_region/Makefile
@@ -0,0 +1,3 @@
+C_SOURCES := main.c
+
+include Makefile.rules
diff --git a/lldb/test/API/functionalities/scripted_process_empty_memory_region/TestScriptedProcessEmptyMemoryRegion.py b/lldb/test/API/functionalities/scripted_process_empty_memory_region/TestScriptedProcessEmptyMemoryRegion.py
new file mode 100644
index 00000000000000..1ff084cfb0278e
--- /dev/null
+++ b/lldb/test/API/functionalities/scripted_process_empty_memory_region/TestScriptedProcessEmptyMemoryRegion.py
@@ -0,0 +1,33 @@
+"""
+Test python scripted process which returns an empty SBMemoryRegionInfo
+"""
+
+import os, shutil
+
+import lldb
+from lldbsuite.test.decorators import *
+from lldbsuite.test.lldbtest import *
+from lldbsuite.test import lldbutil
+from lldbsuite.test import lldbtest
+
+
+class ScriptedProcessEmptyMemoryRegion(TestBase):
+ NO_DEBUG_INFO_TESTCASE = True
+
+ def test_scripted_process_empty_memory_region(self):
+ """Test that lldb handles an empty SBMemoryRegionInfo object from
+ a scripted process plugin."""
+ self.build()
+
+ target = self.dbg.CreateTarget(self.getBuildArtifact("a.out"))
+ self.assertTrue(target, VALID_TARGET)
+
+ scripted_process_example_relpath = "dummy_scripted_process.py"
+ self.runCmd(
+ "command script import "
+ + os.path.join(self.getSourceDir(), scripted_process_example_relpath)
+ )
+
+ self.expect("memory region 0", error=True, substrs=["Invalid memory region"])
+
+ self.expect("expr -- 5", substrs=["5"])
diff --git a/lldb/test/API/functionalities/scripted_process_empty_memory_region/dummy_scripted_process.py b/lldb/test/API/functionalities/scripted_process_empty_memory_region/dummy_scripted_process.py
new file mode 100644
index 00000000000000..31e28a57122f66
--- /dev/null
+++ b/lldb/test/API/functionalities/scripted_process_empty_memory_region/dummy_scripted_process.py
@@ -0,0 +1,110 @@
+import os, struct, signal
+
+from typing import Any, Dict
+
+import lldb
+from lldb.plugins.scripted_process import ScriptedProcess
+from lldb.plugins.scripted_process import ScriptedThread
+
+
+class DummyScriptedProcess(ScriptedProcess):
+ memory = None
+
+ def __init__(self, exe_ctx: lldb.SBExecutionContext, args: lldb.SBStructuredData):
+ super().__init__(exe_ctx, args)
+ self.threads[0] = DummyScriptedThread(self, None)
+ self.memory = {}
+ addr = 0x500000000
+ debugger = self.target.GetDebugger()
+ index = debugger.GetIndexOfTarget(self.target)
+ self.memory[addr] = "Hello, target " + str(index)
+ self.handled_stop = False
+
+ def read_memory_at_address(
+ self, addr: int, size: int, error: lldb.SBError
+ ) -> lldb.SBData:
+ data = lldb.SBData().CreateDataFromCString(
+ self.target.GetByteOrder(), self.target.GetCodeByteSize(), self.memory[addr]
+ )
+
+ return data
+
+ def get_memory_region_containing_address(
+ self, addr: int
+ ) -> lldb.SBMemoryRegionInfo:
+ return lldb.SBMemoryRegionInfo()
+
+ def write_memory_at_address(self, addr, data, error):
+ self.memory[addr] = data.GetString(error, 0)
+ return len(self.memory[addr]) + 1
+
+ def get_loaded_images(self):
+ return self.loaded_images
+
+ def get_process_id(self) -> int:
+ return 42
+
+ def should_stop(self) -> bool:
+ return True
+
+ def is_alive(self) -> bool:
+ return True
+
+ def get_scripted_thread_plugin(self):
+ return DummyScriptedThread.__module__ + "." + DummyScriptedThread.__name__
+
+ def my_super_secret_method(self):
+ if hasattr(self, "my_super_secret_member"):
+ return self.my_super_secret_member
+ else:
+ return None
+
+
+class DummyScriptedThread(ScriptedThread):
+ def __init__(self, process, args):
+ super().__init__(process, args)
+ self.frames.append({"pc": 0x0100001B00})
+
+ def get_thread_id(self) -> int:
+ return 0x19
+
+ def get_name(self) -> str:
+ return DummyScriptedThread.__name__ + ".thread-1"
+
+ def get_state(self) -> int:
+ return lldb.eStateStopped
+
+ def get_stop_reason(self) -> Dict[str, Any]:
+ return {"type": lldb.eStopReasonTrace, "data": {}}
+
+ def get_register_context(self) -> str:
+ return struct.pack(
+ "21Q",
+ 1,
+ 2,
+ 3,
+ 4,
+ 5,
+ 6,
+ 7,
+ 8,
+ 9,
+ 10,
+ 11,
+ 12,
+ 13,
+ 14,
+ 15,
+ 16,
+ 17,
+ 18,
+ 19,
+ 20,
+ 21,
+ )
+
+
+def __lldb_init_module(debugger, dict):
+ debugger.HandleCommand(
+ "process launch -C %s.%s" % (__name__, DummyScriptedProcess.__name__)
+ )
diff --git a/lldb/test/API/functionalities/scripted_process_empty_memory_region/main.c b/lldb/test/API/functionalities/scripted_process_empty_memory_region/main.c
new file mode 100644
index 00000000000000..237c8ce181774d
--- /dev/null
+++ b/lldb/test/API/functionalities/scripted_process_empty_memory_region/main.c
@@ -0,0 +1 @@
+int main() {}
More information about the lldb-commits
mailing list