Skip to content

Commit

Permalink
Support ARM
Browse files Browse the repository at this point in the history
  • Loading branch information
hmgle committed Oct 13, 2021
1 parent 0bd869d commit f649b90
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 1 deletion.
4 changes: 4 additions & 0 deletions graftcp.c
Original file line number Diff line number Diff line change
Expand Up @@ -171,10 +171,14 @@ void close_pre_handle(struct proc_info *pinfp)

void clone_pre_handle(struct proc_info *pinfp)
{
#if defined(__x86_64__)
long flags = get_syscall_arg(pinfp->pid, 0);

flags &= ~CLONE_UNTRACED;
ptrace(PTRACE_POKEUSER, pinfp->pid, sizeof(long) * RDI, flags);
#elif defined(__arm__) || defined(__aarch64__)
/* Do not know how to handle this */
#endif
}

void socket_exiting_handle(struct proc_info *pinfp, int fd)
Expand Down
32 changes: 32 additions & 0 deletions graftcp.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,46 @@
#include <string.h>
#include <sys/wait.h>
#include <sys/user.h>
#if defined(__x86_64__)
#include <sys/reg.h>
#elif defined(__arm__)
#include <asm/ptrace.h>
#endif
#include <sys/syscall.h>
#include <fcntl.h>
#include <linux/sched.h>
#include <assert.h>

#include "uthash.h"

#ifdef __arm__

#ifndef SYS_socket
#define SYS_socket __NR_socket
#endif

#ifndef SYS_connect
#define SYS_connect __NR_connect
#endif

#ifndef SYS_close
#define SYS_close __NR_close
#endif

#ifndef SYS_clone
#define SYS_clone __NR_clone
#endif

#ifndef SYS_exit
#define SYS_exit __NR_exit
#endif

#ifndef SYS_exit_group
#define SYS_exit_group __NR_exit_group
#endif

#endif

#define satosin(x) ((struct sockaddr_in *) &(x))
#define SOCKADDR(x) (satosin(x)->sin_addr.s_addr)
#define SOCKPORT(x) (satosin(x)->sin_port)
Expand Down
55 changes: 54 additions & 1 deletion util.c
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ struct proc_info *alloc_proc_info(pid_t pid)

int get_syscall_number(pid_t pid)
{
#if defined(__x86_64__)
#if 1
errno = 0;
int offset = offsetof(struct user, regs.orig_rax);
Expand All @@ -80,10 +81,18 @@ int get_syscall_number(pid_t pid)
assert(errno == 0);
return regs.orig_rax;
#endif
#elif defined(__arm__)
errno = 0;
struct pt_regs regs;
ptrace(PTRACE_GETREGS, pid, 0, &regs);
assert(errno == 0);
return regs.ARM_r7;
#endif
}

int get_retval(pid_t pid)
{
#if defined(__x86_64__)
#if 1
errno = 0;
int offset = offsetof(struct user, regs.rax);
Expand All @@ -94,10 +103,17 @@ int get_retval(pid_t pid)
ptrace(PTRACE_GETREGS, pid, 0, &regs);
return regs.rax;
#endif
#elif defined(__arm__)
errno = 0;
struct pt_regs regs;
ptrace(PTRACE_GETREGS, pid, 0, &regs);
return regs.ARM_r0;
#endif
}

void set_retval(pid_t pid, long new_val)
{
#if defined(__x86_64__)
struct user_regs_struct regs;
ptrace(PTRACE_GETREGS, pid, 0, &regs);
assert(errno == 0);
Expand All @@ -106,12 +122,23 @@ void set_retval(pid_t pid, long new_val)
regs.rax = new_val;
ptrace(PTRACE_SETREGS, pid, 0, &regs);
assert(errno == 0);
#elif defined(__arm__)
struct pt_regs regs;
ptrace(PTRACE_GETREGS, pid, 0, &regs);
assert(errno == 0);
if ((long)regs.ARM_r0 == new_val)
return;
regs.ARM_r0 = new_val;
ptrace(PTRACE_SETREGS, pid, 0, &regs);
assert(errno == 0);
#endif
}

long get_syscall_arg(pid_t pid, int order)
{
int offset;
long val;
#if defined(__x86_64__)
int offset;

switch (order) {
case 0:
Expand All @@ -138,6 +165,32 @@ long get_syscall_arg(pid_t pid, int order)
errno = 0;
val = ptrace(PTRACE_PEEKUSER, pid, offset);
assert(errno == 0);
#elif defined(__arm__)
struct pt_regs regs;
ptrace(PTRACE_GETREGS, pid, 0, &regs);
switch (order) {
case 0:
val = regs.ARM_ORIG_r0;
break;
case 1:
val = regs.ARM_r1;
break;
case 2:
val = regs.ARM_r2;
break;
case 3:
val = regs.ARM_r3;
break;
case 4:
val = regs.ARM_r4;
break;
case 5:
val = regs.ARM_r5;
break;
default:
return -1;
}
#endif
return val;
}

Expand Down

0 comments on commit f649b90

Please sign in to comment.