Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ptrace mac support #947

Closed
xd009642 opened this issue Oct 4, 2018 · 7 comments · Fixed by #949
Closed

ptrace mac support #947

xd009642 opened this issue Oct 4, 2018 · 7 comments · Fixed by #949

Comments

@xd009642
Copy link
Contributor

xd009642 commented Oct 4, 2018

It's came up as part of xd009642/tarpaulin#152 but ptrace is partially supported in macos (source). I'll look into it myself this weekend but I think it would be useful to go through the ptrace api and cfg in the parts supported on Macs.

Any thoughts, opinions, objections?

@asomers
Copy link
Member

asomers commented Oct 4, 2018

No objections at all. Sounds like a useful feature.

@xd009642
Copy link
Contributor Author

xd009642 commented Oct 5, 2018

So ptrace mac support looks like it's going to be a bit faffy as apple name their constants differently PT_TRACEME instead of PTRACE_TRACEME.

So from this I see a few potential approaches just wondering if there were any thoughts/opinions/preferences.

  • Keep using the libc_enum! macro and use cfg_if to generate a version of the Request enum for macos and one for linux/android and sprinkle cfg_ifs around the functions to choose the right request constant for the OS.
  • Move ptrace into it's own module with an apple version and the existing file we have now for linux/android. Apple ptrace is heavily restricted and adds new extensions so might not be a bad shout
  • Abandon libc_enum! so I can keep the same constant names as the module currently uses (could do a hybrid of this and the first bullet point)

I've got a PR for the ptrace constants on macos being merged into libc. Bors is just merging it in as we speak so I'll start this properly once that's in.

@asomers
Copy link
Member

asomers commented Oct 5, 2018

Nix should not attempt to provide a common interface between different OSes. That responsibility belongs to crates that live atop Nix. So don't do bullet 3. Bullet 1 is the easiest and would be fine. Bullet 2 is an option if the differences are so large that Bullet 1 becomes unreadable.

@xd009642
Copy link
Contributor Author

xd009642 commented Oct 5, 2018

Okay I thought that might be the case for bullet 3.

My gut feeling is that going for the first approach would make it unreadable, but I'll try it out first and move ptrace into it's own module if that becomes the case.

@xd009642
Copy link
Contributor Author

xd009642 commented Oct 7, 2018

So the existing PTRACE interface for linux has this deprecated function, I'm guessing I shouldn't replicate duplicated functionality in the Apple/BSD interface.

/// Performs a ptrace request. If the request in question is provided by a specialised function
/// this function will return an unsupported operation error.
#[deprecated(
    since="0.10.0",
    note="usages of `ptrace()` should be replaced with the specialized helper functions instead"
)]
pub unsafe fn ptrace(request: Request, pid: Pid, addr: *mut c_void, data: *mut c_void) -> Result<c_long> {
    use self::Request::*;
    match request {
        PTRACE_PEEKTEXT | PTRACE_PEEKDATA | PTRACE_PEEKUSER => ptrace_peek(request, pid, addr, data),
        PTRACE_GETSIGINFO | PTRACE_GETEVENTMSG | PTRACE_SETSIGINFO | PTRACE_SETOPTIONS => Err(Error::UnsupportedOperation),
        _ => ptrace_other(request, pid, addr, data)
    }
}

Also none of the PTRACE events seem to be supported in BSD. Some are in headers I found online but libc test errored so they didn't make it into rust-lang/libc and as a result the ptrace interface for BSD will probably be smaller than linux

@asomers
Copy link
Member

asomers commented Oct 7, 2018

They aren't supported in the BSDs because nobody has needed them yet. Feel free to add them, both to libc and Nix.

@xd009642
Copy link
Contributor Author

xd009642 commented Oct 7, 2018

Oh the Ptrace requests are in libc already I did a PR, but the events didn't work and not all BSDs offer them so I've left them out entirely. I've opened a PR now if you want to have a look at what I've done #949

bors bot added a commit that referenced this issue Oct 21, 2018
949: ptrace support for BSDs r=Susurrus a=xd009642

This PR adds support to the ptrace API for BSDs to close #947. It also adds a read and write method for reading and writing to a traced processes memory. The ptrace API created for linux offers this via a deprecated function so I added this so they can be feature equivalent without replicating a deprecated part of the API.

Due to the differences in ptrace on BSD and linux I've made a ptrace module to keep things readable.

Still to do - revert travis config to remove my feature branch and update the changelog. 

Co-authored-by: xd009642 <danielmckenna93@gmail.com>
@bors bors bot closed this as completed in #949 Oct 21, 2018
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 a pull request may close this issue.

2 participants