fix(ptracer): fix vmReadStr logic error

This commit is contained in:
ZQ 2025-02-11 12:11:28 +08:00
parent 99241ac91a
commit 8b7494f796

View File

@ -36,30 +36,37 @@ func getIovecs(base *byte, l int) []unix.Iovec {
} }
func vmReadStr(pid int, addr uintptr, buff []byte) error { func vmReadStr(pid int, addr uintptr, buff []byte) error {
// Deal with unaligned addr // Handle unaligned address: calculate remaining bytes to page boundary
n := 0 totalRead := 0 // Total bytes read so far
r := pageSize - int(addr%uintptr(pageSize)) // Calculate distance to next page boundary, nextRead is the number of bytes to read
if r == 0 { nextRead := pageSize - int(addr%uintptr(pageSize))
r = pageSize if nextRead == 0 {
nextRead = pageSize // If exactly at page boundary, use full page size
} }
// Read in a loop until buffer is full or termination condition is met
for len(buff) > 0 { for len(buff) > 0 {
if l := len(buff); r < l { // If remaining buffer is smaller than planned read size, reduce read size
r = l if restToRead := len(buff); restToRead < nextRead {
nextRead = restToRead
} }
nn, err := vmRead(pid, addr+uintptr(n), buff[:r]) // Read data from current position
curRead, err := vmRead(pid, addr+uintptr(totalRead), buff[:nextRead])
if err != nil { if err != nil {
return err return err // Read error
}
if curRead == 0 {
break // No more data to read
}
if hasNull(buff[:curRead]) {
break // Found string terminator
} }
if hasNull(buff[:nn]) { // Update counters and buffer
return nil totalRead += curRead // Update total bytes read
} buff = buff[curRead:] // Move buffer pointer
nextRead = pageSize // Reset to full page size
n += nn
buff = buff[nn:]
r = pageSize
} }
return nil return nil
} }