[Lldb-commits] [PATCH] D62715: [NativeProcessLinux] Reuse memory read by process_vm_readv before calling ptrace

Pavel Labath via Phabricator via lldb-commits lldb-commits at lists.llvm.org
Fri May 31 07:46:35 PDT 2019


labath added a comment.

In D62715#1525085 <https://reviews.llvm.org/D62715#1525085>, @aadsm wrote:

> That's a good idea for the tests, will look into that.


Actually, it looks like getting `process_vm_readv` to fail is as simple as running `mprotect(..., PROT_NONE)` on the piece of memory. Here's my test program, in case you find it useful for anything:

  #include <cassert>
  #include <cerrno>
  #include <cstdio>
  #include <cstring>
  #include <sys/mman.h>
  #include <sys/prctl.h>
  #include <sys/ptrace.h>
  #include <sys/uio.h>
  #include <sys/wait.h>
  #include <unistd.h>
  
  long *mem;
  
  void child() {
    assert(ptrace(PTRACE_TRACEME, 0, 0, 0) == 0);
    raise(SIGSTOP);
    _exit(0);
  }
  
  void parent(pid_t child) {
    int status;
    assert(waitpid(child, &status, __WALL) == child);
    assert(WIFSTOPPED(status));
    assert(WSTOPSIG(status) == SIGSTOP);
    unsigned long myx;
    struct iovec local;
    local.iov_base = &myx;
    local.iov_len = sizeof myx;
  
    struct iovec remote;
    remote.iov_base = mem;
    remote.iov_len = sizeof myx;
  
    ssize_t s = process_vm_readv(child, &local, 1, &remote, 1, 0);
    fprintf(stderr, "readv: %zd (%d - %s)\n", s, errno, strerror(errno));
    myx = ptrace(PTRACE_PEEKDATA, child, mem, 0);
    fprintf(stderr, "peek: %lx (%d - %s)\n", myx, errno, strerror(errno));
    _exit(0);
  }
  
  int main() {
    mem = reinterpret_cast<long *>(mmap(nullptr, 0x1000, PROT_READ | PROT_WRITE,
                                        MAP_PRIVATE | MAP_ANONYMOUS, 0, 0));
    assert(mem != MAP_FAILED);
    *mem = 0x424344454647;
    assert(mprotect(mem, 0x1000, PROT_NONE) == 0);
  
    pid_t pid = fork();
    assert(pid != -1);
    if (pid)
      parent(pid);
    else
      child();
  }
  
  
  $ g++ a.cc
  $ ./a.out 
  readv: -1 (14 - Bad address)
  peek: 424344454647 (0 - Success)


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D62715/new/

https://reviews.llvm.org/D62715





More information about the lldb-commits mailing list