* Copyright (c) 1999 IBM Deutschland Entwicklung GmbH, IBM Corporation
* Linux for s390 port by D.J. Barrow
* <barrow_dj@mail.yahoo.com,djbarrow@de.ibm.com>
- * Copyright (c) 1999-2017 The strace developers.
+ * Copyright (c) 1999-2018 The strace developers.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
umoven_peekdata(const int pid, kernel_ulong_t addr, unsigned int len,
void *laddr)
{
- unsigned int n, m, nread;
- union {
- long val;
- char x[sizeof(long)];
- } u;
-
- nread = 0;
- if (addr & (sizeof(long) - 1)) {
- /* addr not a multiple of sizeof(long) */
- n = addr & (sizeof(long) - 1); /* residue */
- addr &= -sizeof(long); /* aligned address */
- errno = 0;
- u.val = ptrace(PTRACE_PEEKDATA, pid, addr, 0);
- switch (errno) {
- case 0:
- break;
- case ESRCH: case EINVAL:
- /* these could be seen if the process is gone */
- return -1;
- case EFAULT: case EIO: case EPERM:
- /* address space is inaccessible */
- return -1;
- default:
- /* all the rest is strange and should be reported */
- perror_msg("umoven: PTRACE_PEEKDATA pid:%d @0x%" PRI_klx,
- pid, addr);
- return -1;
- }
- m = MIN(sizeof(long) - n, len);
- memcpy(laddr, &u.x[n], m);
- addr += sizeof(long);
- laddr += m;
- nread += m;
- len -= m;
- }
+ unsigned int nread = 0;
+ unsigned int residue = addr & (sizeof(long) - 1);
+
while (len) {
+ addr &= -sizeof(long); /* aligned address */
+
errno = 0;
- u.val = ptrace(PTRACE_PEEKDATA, pid, addr, 0);
+ union {
+ long val;
+ char x[sizeof(long)];
+ } u = { .val = ptrace(PTRACE_PEEKDATA, pid, addr, 0) };
+
switch (errno) {
case 0:
break;
pid, addr);
return -1;
}
- m = MIN(sizeof(long), len);
- memcpy(laddr, u.x, m);
+
+ unsigned int m = MIN(sizeof(long) - residue, len);
+ memcpy(laddr, &u.x[residue], m);
+ residue = 0;
addr += sizeof(long);
laddr += m;
nread += m;
umovestr_peekdata(const int pid, kernel_ulong_t addr, unsigned int len,
void *laddr)
{
- const unsigned long x01010101 = (unsigned long) 0x0101010101010101ULL;
- const unsigned long x80808080 = (unsigned long) 0x8080808080808080ULL;
-
- unsigned int n, m, nread;
- union {
- unsigned long val;
- char x[sizeof(long)];
- } u;
-
- nread = 0;
- if (addr & (sizeof(long) - 1)) {
- /* addr not a multiple of sizeof(long) */
- n = addr & (sizeof(long) - 1); /* residue */
- addr &= -sizeof(long); /* aligned address */
- errno = 0;
- u.val = ptrace(PTRACE_PEEKDATA, pid, addr, 0);
- switch (errno) {
- case 0:
- break;
- case ESRCH: case EINVAL:
- /* these could be seen if the process is gone */
- return -1;
- case EFAULT: case EIO: case EPERM:
- /* address space is inaccessible */
- return -1;
- default:
- /* all the rest is strange and should be reported */
- perror_msg("umovestr: PTRACE_PEEKDATA pid:%d @0x%" PRI_klx,
- pid, addr);
- return -1;
- }
- m = MIN(sizeof(long) - n, len);
- memcpy(laddr, &u.x[n], m);
- while (n & (sizeof(long) - 1))
- if (u.x[n++] == '\0')
- return 1;
- addr += sizeof(long);
- laddr += m;
- nread += m;
- len -= m;
- }
+ unsigned int nread = 0;
+ unsigned int residue = addr & (sizeof(long) - 1);
+ void *const orig_addr = laddr;
while (len) {
+ addr &= -sizeof(long); /* aligned address */
+
errno = 0;
- u.val = ptrace(PTRACE_PEEKDATA, pid, addr, 0);
+ union {
+ unsigned long val;
+ char x[sizeof(long)];
+ } u = { .val = ptrace(PTRACE_PEEKDATA, pid, addr, 0) };
+
switch (errno) {
case 0:
break;
pid, addr);
return -1;
}
- m = MIN(sizeof(long), len);
- memcpy(laddr, u.x, m);
- /* "If a NUL char exists in this word" */
- if ((u.val - x01010101) & ~u.val & x80808080)
- return 1;
+
+ unsigned int m = MIN(sizeof(long) - residue, len);
+ memcpy(laddr, &u.x[residue], m);
+ while (residue < sizeof(long))
+ if (u.x[residue++] == '\0')
+ return (laddr - orig_addr) + residue;
+ residue = 0;
addr += sizeof(long);
laddr += m;
nread += m;
* Like `umove' but make the additional effort of looking
* for a terminating zero byte.
*
- * Returns < 0 on error, > 0 if NUL was seen,
- * (TODO if useful: return count of bytes including NUL),
+ * Returns < 0 on error, strlen + 1 if NUL was seen,
* else 0 if len bytes were read but no NUL byte seen.
*
* Note: there is no guarantee we won't overwrite some bytes
int r = vm_read_mem(pid, laddr, addr, chunk_len);
if (r > 0) {
- if (memchr(laddr, '\0', r))
- return 1;
+ char *nul_addr = memchr(laddr, '\0', r);
+
+ if (nul_addr)
+ return (nul_addr - laddr) + 1;
addr += r;
laddr += r;
nread += r;
if (!nread)
return umovestr_peekdata(pid, addr,
len, laddr);
- /* fall through */
+ ATTRIBUTE_FALLTHROUGH;
case EFAULT: case EIO:
/* address space is inaccessible */
if (nread)