Skip to content

Commit 2ba1aee

Browse files
authored
[lldb] Don't use a vm addr range starting at 0 for local memory (#100288)
When an inferior stub cannot allocate memory for lldb, and lldb needs to store the result of expressions, it will do it in lldb's own memory range ("host memory"). But it needs to find a virtual address range that is not used in the inferior process. It tries to use the qMemoryRegionInfo gdb remote serial protocol packet to find a range that is inaccessible, starting at address 0 and moving up the size of each region. If the first region found at address 0 is inaccessible, lldb will use the address range starting at 0 to mean "read lldb's host memory, not the process memory", and programs that crash with a null dereference will have poor behavior. This patch skips consideration of a memory region that starts at address 0. I also clarified the documentation of qMemoryRegionInfo to make it clear that the stub is required to provide permissions for a memory range that is accessable, it is not an optional key in this response. This issue was originally found by a stub that did not list permissions in its response, and lldb treated the first region returned as the one it would use. (the stub also didn't support the memory-allocate packet)
1 parent e894df6 commit 2ba1aee

File tree

2 files changed

+20
-6
lines changed

2 files changed

+20
-6
lines changed

lldb/docs/resources/lldbgdbremote.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1403,6 +1403,12 @@ For instance, with a macOS process which has nothing mapped in the first
14031403
The lack of `permissions:` indicates that none of read/write/execute are valid
14041404
for this region.
14051405
1406+
The stub must include `permissions:` key-value on all memory ranges
1407+
that are valid to access in the inferior process -- the lack of
1408+
`permissions:` means that this is an inaccessible (no page table
1409+
entries exist, in a system using VM) memory range. If a stub cannot
1410+
determine actual permissions, return `rwx`.
1411+
14061412
**Priority To Implement:** Medium
14071413
14081414
This is nice to have, but it isn't necessary. It helps LLDB
@@ -2434,4 +2440,4 @@ The `0x` prefixes are optional - like most of the gdb-remote packets,
24342440
omitting them will work fine; these numbers are always base 16.
24352441
24362442
The length of the payload is not provided. A reliable, 8-bit clean,
2437-
transport layer is assumed.
2443+
transport layer is assumed.

lldb/source/Expression/IRMemoryMap.cpp

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ lldb::addr_t IRMemoryMap::FindSpace(size_t size) {
8484
// any allocations. Otherwise start at the beginning of memory.
8585

8686
if (m_allocations.empty()) {
87-
ret = 0x0;
87+
ret = 0;
8888
} else {
8989
auto back = m_allocations.rbegin();
9090
lldb::addr_t addr = back->first;
@@ -116,10 +116,18 @@ lldb::addr_t IRMemoryMap::FindSpace(size_t size) {
116116
Status err = process_sp->GetMemoryRegionInfo(ret, region_info);
117117
if (err.Success()) {
118118
while (true) {
119-
if (region_info.GetReadable() != MemoryRegionInfo::OptionalBool::eNo ||
120-
region_info.GetWritable() != MemoryRegionInfo::OptionalBool::eNo ||
121-
region_info.GetExecutable() !=
122-
MemoryRegionInfo::OptionalBool::eNo) {
119+
if (region_info.GetRange().GetRangeBase() == 0 &&
120+
region_info.GetRange().GetRangeEnd() < end_of_memory) {
121+
// Don't use a region that starts at address 0,
122+
// it can make it harder to debug null dereference crashes
123+
// in the inferior.
124+
ret = region_info.GetRange().GetRangeEnd();
125+
} else if (region_info.GetReadable() !=
126+
MemoryRegionInfo::OptionalBool::eNo ||
127+
region_info.GetWritable() !=
128+
MemoryRegionInfo::OptionalBool::eNo ||
129+
region_info.GetExecutable() !=
130+
MemoryRegionInfo::OptionalBool::eNo) {
123131
if (region_info.GetRange().GetRangeEnd() - 1 >= end_of_memory) {
124132
ret = LLDB_INVALID_ADDRESS;
125133
break;

0 commit comments

Comments
 (0)