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 {
// Deal with unaligned addr
n := 0
r := pageSize - int(addr%uintptr(pageSize))
if r == 0 {
r = pageSize
// Handle unaligned address: calculate remaining bytes to page boundary
totalRead := 0 // Total bytes read so far
// Calculate distance to next page boundary, nextRead is the number of bytes to read
nextRead := pageSize - int(addr%uintptr(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 {
if l := len(buff); r < l {
r = l
// If remaining buffer is smaller than planned read size, reduce read size
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 {
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]) {
return nil
}
n += nn
buff = buff[nn:]
r = pageSize
// Update counters and buffer
totalRead += curRead // Update total bytes read
buff = buff[curRead:] // Move buffer pointer
nextRead = pageSize // Reset to full page size
}
return nil
}