Skip to content

Commit

Permalink
merge #389
Browse files Browse the repository at this point in the history
  • Loading branch information
zhkl0228 committed Dec 28, 2021
1 parent 9052947 commit f56b933
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,9 @@ public void hook(Backend backend, int intno, int swi, Object user) {
case 177:
backend.reg_write(ArmConst.UC_ARM_REG_R0, rt_sigtimedwait(emulator));
return;
case 178:
backend.reg_write(ArmConst.UC_ARM_REG_R0, rt_sigqueue(emulator));
return;
case 180:
backend.reg_write(ArmConst.UC_ARM_REG_R0, pread64(emulator));
return;
Expand Down Expand Up @@ -976,7 +979,7 @@ private int llseek(Backend backend, Emulator<?> emulator) {
int fd = backend.reg_read(ArmConst.UC_ARM_REG_R0).intValue();
long offset_high = backend.reg_read(ArmConst.UC_ARM_REG_R1).intValue() & 0xffffffffL;
long offset_low = backend.reg_read(ArmConst.UC_ARM_REG_R2).intValue() & 0xffffffffL;
long offset = (offset_high<<32) | offset_low;
long offset = (offset_high << 32) | offset_low;
Pointer result = UnidbgPointer.register(emulator, ArmConst.UC_ARM_REG_R3);
int whence = backend.reg_read(ArmConst.UC_ARM_REG_R4).intValue();
if (log.isDebugEnabled()) {
Expand Down Expand Up @@ -1240,7 +1243,7 @@ private int poll(Backend backend, Emulator<?> emulator) {
pollfd.setShort(6, (short) 0);
} else {
short revents = 0;
if((events & POLLOUT) != 0) {
if ((events & POLLOUT) != 0) {
revents = POLLOUT;
} else if ((events & POLLIN) != 0) {
revents = POLLIN;
Expand Down Expand Up @@ -1685,7 +1688,7 @@ private int munmap(Backend backend, Emulator<?> emulator) {
private static final int PR_SET_DUMPABLE = 4;
private static final int PR_SET_NAME = 15;
private static final int PR_GET_NAME = 16;
private static final int BIONIC_PR_SET_VMA = 0x53564d41;
private static final int BIONIC_PR_SET_VMA = 0x53564d41;
private static final int PR_SET_PTRACER = 0x59616d61;

private int prctl(Backend backend, Emulator<?> emulator) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,10 +143,10 @@ public void hook(Backend backend, int intno, int swi, Object user) {
case 62:
backend.reg_write(Arm64Const.UC_ARM64_REG_X0, lseek(emulator));
return;
case 172: // getpid
case 172: // getpid
backend.reg_write(Arm64Const.UC_ARM64_REG_X0, emulator.getPid());
return;
case 178: // gettid
case 178: // gettid
Task task = emulator.get(Task.TASK_KEY);
backend.reg_write(Arm64Const.UC_ARM64_REG_X0, task == null ? 0 : task.getId());
return;
Expand Down Expand Up @@ -249,6 +249,9 @@ public void hook(Backend backend, int intno, int swi, Object user) {
case 137:
backend.reg_write(Arm64Const.UC_ARM64_REG_X0, rt_sigtimedwait(emulator));
return;
case 138:
backend.reg_write(Arm64Const.UC_ARM64_REG_X0, rt_sigqueue(emulator));
return;
case 167:
backend.reg_write(Arm64Const.UC_ARM64_REG_X0, prctl(emulator));
return;
Expand Down Expand Up @@ -685,7 +688,7 @@ private int ppoll(Emulator<?> emulator) {
pollfd.setShort(6, (short) 0);
} else {
short revents = 0;
if((events & POLLOUT) != 0) {
if ((events & POLLOUT) != 0) {
revents = POLLOUT;
} else if ((events & POLLIN) != 0) {
revents = POLLIN;
Expand Down Expand Up @@ -1074,7 +1077,7 @@ private long mremap(Emulator<?> emulator) {

private static final int PR_SET_NAME = 15;
private static final int PR_SET_NO_NEW_PRIVS = 38;
private static final int BIONIC_PR_SET_VMA = 0x53564d41;
private static final int BIONIC_PR_SET_VMA = 0x53564d41;
private static final int PR_SET_PTRACER = 0x59616d61;

private int prctl(Emulator<?> emulator) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,33 @@ protected int rt_sigtimedwait(Emulator<AndroidFileIO> emulator) {
return 0;
}

protected int rt_sigqueue(Emulator<AndroidFileIO> emulator) {
RegisterContext context = emulator.getContext();
int tgid = context.getIntArg(0);
int sig = context.getIntArg(1);
UnidbgPointer info = context.getPointerArg(2);
if (log.isDebugEnabled()) {
log.debug("rt_sigqueue tgid=" + tgid + ", sig=" + sig);
}
Task task = emulator.get(Task.TASK_KEY);
// 检查pid是有匹配进程存在
if (!(tgid == 0 || tgid == -1 || Math.abs(tgid) == emulator.getPid())) {
return -UnixEmulator.ESRCH;
}
// 检查进程是否存在, 无需发送信号
if (sig == 0) {
return 0;
}
if (sig < 0 || sig > 64) {
return -UnixEmulator.EINVAL;
}
if (task != null) {
SigAction sigAction = sigActionMap.get(sig);
return processSignal(emulator.getThreadDispatcher(), sig, task, sigAction, info);
}
throw new UnsupportedOperationException();
}

@Override
protected FileResult<AndroidFileIO> createFdDir(int oflags, String pathname) {
List<DirectoryFileIO.DirectoryEntry> list = new ArrayList<>();
Expand Down Expand Up @@ -486,7 +513,7 @@ final int select(int nfds, Pointer checkfds, Pointer clearfds, boolean checkRead
int count = 0;
for (int i = 0; i < nfds; i++) {
int mask = checkfds.getInt(i / 32);
if(((mask >> i) & 1) == 1) {
if (((mask >> i) & 1) == 1) {
AndroidFileIO io = fdMap.get(i);
if (!checkRead || io.canRead()) {
count++;
Expand Down Expand Up @@ -610,21 +637,27 @@ protected int kill(Emulator<?> emulator) {
if (log.isDebugEnabled()) {
log.debug("kill pid=" + pid + ", sig=" + sig);
}
if (sig == 0) {
return 0;
}
if (sig < 0 || sig > 64) {
return -UnixEmulator.EINVAL;
}
Task task = emulator.get(Task.TASK_KEY);
if (pid == 0 && sig > 0 && task != null) {
if ((pid == 0 || pid == emulator.getPid()) && task != null) {
SigAction action = sigActionMap.get(sig);
return processSignal(emulator.getThreadDispatcher(), sig, task, action);
return processSignal(emulator.getThreadDispatcher(), sig, task, action, null);
}
throw new UnsupportedOperationException("kill pid=" + pid + ", sig=" + sig + ", LR=" + context.getLRPointer());
}

private int processSignal(ThreadDispatcher threadDispatcher, int sig, Task task, SigAction action) {
private int processSignal(ThreadDispatcher threadDispatcher, int sig, Task task, SigAction action, Pointer sig_info) {
if (action != null) {
SignalOps signalOps = task.isMainThread() ? threadDispatcher : task;
SigSet sigMaskSet = signalOps.getSigMaskSet();
SigSet sigPendingSet = signalOps.getSigPendingSet();
if (sigMaskSet == null || !sigMaskSet.containsSigNumber(sig)) {
task.addSignalTask(new SignalTask(sig, action));
task.addSignalTask(new SignalTask(sig, action, sig_info));
throw new ThreadContextSwitchException().setReturnValue(0);
} else if (sigPendingSet != null) {
sigPendingSet.addSigNumber(sig);
Expand All @@ -641,12 +674,16 @@ protected int tgkill(Emulator<?> emulator) {
if (log.isDebugEnabled()) {
log.debug("tgkill tgid=" + tgid + ", tid=" + tid + ", sig=" + sig);
}
if (sig > 0) {
SigAction action = sigActionMap.get(sig);
if (action != null &&
emulator.getThreadDispatcher().sendSignal(tid, new SignalTask(sig, action))) {
throw new ThreadContextSwitchException().setReturnValue(0);
}
if (sig == 0) {
return 0;
}
if (sig < 0 || sig > 64) {
return -UnixEmulator.EINVAL;
}
SigAction action = sigActionMap.get(sig);
if (action != null &&
emulator.getThreadDispatcher().sendSignal(tid, new SignalTask(sig, action))) {
throw new ThreadContextSwitchException().setReturnValue(0);
}
return 0;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import com.github.unidbg.signal.SigSet;
import com.github.unidbg.signal.SignalOps;
import com.github.unidbg.signal.UnixSigSet;
import com.sun.jna.Pointer;
import unicorn.Arm64Const;
import unicorn.ArmConst;

Expand All @@ -17,10 +18,16 @@ public class SignalTask extends AbstractSignalTask {
private final SigAction action;

public SignalTask(int signum, SigAction action) {
this(signum, action, null);
}

public SignalTask(int signum, SigAction action, Pointer sig_info) {
super(signum);
this.action = action;
this.sig_info = sig_info;
}

private Pointer sig_info;
private UnidbgPointer stack;

@Override
Expand Down Expand Up @@ -48,20 +55,21 @@ private Number runHandler(AbstractEmulator<?> emulator) {
if (stack == null) {
stack = allocateStack(emulator);
}
if (action.needSigInfo() && infoBlock == null) {
if (action.needSigInfo() && infoBlock == null && sig_info == null) {
infoBlock = emulator.getMemory().malloc(128, true);
infoBlock.getPointer().setInt(0, signum);
sig_info = infoBlock.getPointer();
}
if (emulator.is32Bit()) {
backend.reg_write(ArmConst.UC_ARM_REG_SP, stack.peer);
backend.reg_write(ArmConst.UC_ARM_REG_R0, signum);
backend.reg_write(ArmConst.UC_ARM_REG_R1, infoBlock == null ? 0 : infoBlock.getPointer().peer); // siginfo_t *info
backend.reg_write(ArmConst.UC_ARM_REG_R1, sig_info == null ? 0 : UnidbgPointer.nativeValue(sig_info)); // siginfo_t *info
backend.reg_write(ArmConst.UC_ARM_REG_R2, 0); // void *ucontext
backend.reg_write(ArmConst.UC_ARM_REG_LR, emulator.getReturnAddress());
} else {
backend.reg_write(Arm64Const.UC_ARM64_REG_SP, stack.peer);
backend.reg_write(Arm64Const.UC_ARM64_REG_X0, signum);
backend.reg_write(Arm64Const.UC_ARM64_REG_X1, infoBlock == null ? 0 : infoBlock.getPointer().peer); // siginfo_t *info
backend.reg_write(Arm64Const.UC_ARM64_REG_X1, sig_info == null ? 0 : UnidbgPointer.nativeValue(sig_info)); // siginfo_t *info
backend.reg_write(Arm64Const.UC_ARM64_REG_X2, 0); // void *ucontext
backend.reg_write(Arm64Const.UC_ARM64_REG_LR, emulator.getReturnAddress());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ public interface UnixEmulator {

int EPERM = 1; /* Operation not permitted */
int ENOENT = 2; /* No such file or directory */
int ESRCH = 3; /* No such process */
int EINTR = 4; /* Interrupted system call */
int EBADF = 9; /* Bad file descriptor */
int EAGAIN = 11; /* Resource temporarily unavailable */
Expand Down

0 comments on commit f56b933

Please sign in to comment.