Skip to content

Conversation

Copilot
Copy link
Contributor

@Copilot Copilot AI commented Sep 3, 2025

This PR implements comprehensive memory region reading from debug backends to resolve the critical performance issue where string search attempts to read the entire address space (0xffffffff or 0xffffffffffffffff bytes), causing operations to hang indefinitely.

Problem

Currently, the debugger only has limited segment information (binary path and start address) with no knowledge of segment lengths or memory permissions. This causes two major issues:

  1. Infinite String Search: The DebuggerFileAccessor reports the entire address space as readable (UINT64_MAX), so string search tries to read all memory addresses, never completing.
  2. Missing Memory Metadata: No information about memory permissions (rwx) or region types (stack, heap, modules).

Solution

Added complete memory region infrastructure with backend-specific implementations:

Core Infrastructure

  • DebugMemoryRegion: New struct storing start/end addresses, permissions (read/write/execute), region name, and associated module
  • GetMemoryRegions(): Virtual method added to DebugAdapter base class
  • DebuggerMemoryRegions: Management class for caching and validating memory regions
  • Memory Validation: DebuggerMemory::ReadBlock() now validates addresses against readable regions before attempting reads

Backend Implementations

  • GDB Adapter: Parses /proc/PID/maps to extract detailed Linux memory layout with permissions
  • LLDB Adapter: Uses SBProcess::GetMemoryRegionInfo() for cross-platform region enumeration
  • DbgEng Adapter: Leverages IDebugDataSpaces::QueryVirtual() for Windows memory regions
  • Fallback Support: Gracefully handles adapters without memory region support

Example Memory Layout (Linux)

0x400000-0x40b000 r-x /bin/ls          # Text segment
0x60a000-0x60b000 r-- /bin/ls          # Read-only data
0x1c1a000-0x1c3b000 rw- [heap]         # Heap region
0x7ffffffde000-0x7ffffffff000 rw- [stack] # Stack region

Performance Impact

Before: String search attempts to scan entire address space

  • 32-bit: ~1M blocks (4GB ÷ 4KB blocks)
  • 64-bit: Effectively infinite

After: Only scans valid memory regions

  • Typical process: ~512 blocks (2MB ÷ 4KB blocks)
  • Result: ~2000x performance improvement

Testing

Comprehensive test suite validates:

  • Memory region parsing from /proc/maps format
  • Address validation and boundary conditions
  • Permission-based access control
  • Edge cases (empty regions, overlaps, invalid ranges)
  • Performance optimization verification

The implementation is robust, handles all edge cases, and provides graceful fallback when memory regions aren't available.

Fixes #96.


💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.

@CLAassistant
Copy link

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

@Copilot Copilot AI changed the title [WIP] Read memory map from the backend Read memory map from backend to fix infinite string search issue Sep 3, 2025
@Copilot Copilot AI requested a review from xusheng6 September 3, 2025 10:32
Copilot finished work on behalf of xusheng6 September 3, 2025 10:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Read memory map from the backend
3 participants