Skip to content

Commit 2d192f6

Browse files
committed
change core dump stacks to only include up to the stack pointer (+ red zone)
Add python tests to verify we can read up to sp + redzone - 1.
1 parent e3ca558 commit 2d192f6

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
lines changed

lldb/source/Target/Process.cpp

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3857,8 +3857,8 @@ thread_result_t Process::RunPrivateStateThread(bool is_secondary_thread) {
38573857
// case we should tell it to stop doing that. Normally, we don't NEED
38583858
// to do that because we will next close the communication to the stub
38593859
// and that will get it to shut down. But there are remote debugging
3860-
// cases where relying on that side-effect causes the shutdown to be
3861-
// flakey, so we should send a positive signal to interrupt the wait.
3860+
// cases where relying on that side-effect causes the shutdown to be
3861+
// flakey, so we should send a positive signal to interrupt the wait.
38623862
Status error = HaltPrivate();
38633863
BroadcastEvent(eBroadcastBitInterrupt, nullptr);
38643864
} else if (StateIsRunningState(m_last_broadcast_state)) {
@@ -6410,12 +6410,20 @@ GetCoreFileSaveRangesStackOnly(Process &process,
64106410
if (!reg_ctx_sp)
64116411
continue;
64126412
const addr_t sp = reg_ctx_sp->GetSP();
6413+
const size_t red_zone = process.GetABI()->GetRedZoneSize();
64136414
lldb_private::MemoryRegionInfo sp_region;
64146415
if (process.GetMemoryRegionInfo(sp, sp_region).Success()) {
64156416
// Only add this region if not already added above. If our stack pointer
64166417
// is pointing off in the weeds, we will want this range.
6417-
if (stack_bases.count(sp_region.GetRange().GetRangeBase()) == 0)
6418+
if (stack_bases.count(sp_region.GetRange().GetRangeBase()) == 0) {
6419+
// Take only the start of the stack to the stack pointer and include the redzone.
6420+
// Because stacks grow 'down' to include the red_zone we have to subtract it from the sp.
6421+
const size_t stack_head = (sp - red_zone);
6422+
const size_t stack_size = sp_region.GetRange().GetRangeEnd() - (stack_head);
6423+
sp_region.GetRange().SetRangeBase(stack_head);
6424+
sp_region.GetRange().SetByteSize(stack_size);
64186425
AddRegion(sp_region, try_dirty_pages, ranges);
6426+
}
64196427
}
64206428
}
64216429
}

lldb/test/API/functionalities/process_save_core_minidump/TestProcessSaveCoreMinidump.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ class ProcessSaveCoreMinidumpTestCase(TestBase):
1414
def verify_core_file(
1515
self, core_path, expected_pid, expected_modules, expected_threads
1616
):
17+
breakpoint()
1718
# To verify, we'll launch with the mini dump
1819
target = self.dbg.CreateTarget(None)
1920
process = target.LoadCore(core_path)
@@ -36,11 +37,22 @@ def verify_core_file(
3637
self.assertEqual(module_file_name, expected_file_name)
3738
self.assertEqual(module.GetUUIDString(), expected.GetUUIDString())
3839

40+
red_zone = process.GetTarget().GetStackRedZoneSize()
3941
for thread_idx in range(process.GetNumThreads()):
4042
thread = process.GetThreadAtIndex(thread_idx)
4143
self.assertTrue(thread.IsValid())
4244
thread_id = thread.GetThreadID()
4345
self.assertIn(thread_id, expected_threads)
46+
oldest_frame = thread.GetFrameAtIndex(thread.GetNumFrames() - 1)
47+
stack_start = oldest_frame.GetSymbol().GetStartAddress().GetFileAddress()
48+
frame = thread.GetFrameAtIndex(0)
49+
sp = frame.GetSP()
50+
stack_size = stack_start - (sp - red_zone)
51+
byte_array = process.ReadMemory(sp - red_zone + 1, stack_size, error)
52+
self.assertTrue(error.Success(), "Failed to read stack")
53+
self.assertEqual(stack_size - 1, len(byte_array), "Incorrect stack size read"),
54+
55+
4456
self.dbg.DeleteTarget(target)
4557

4658
@skipUnlessArch("x86_64")

0 commit comments

Comments
 (0)