Java Code Examples for com.sun.jna.Pointer#setInt()

The following examples show how to use com.sun.jna.Pointer#setInt() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SocketIO.java    From unidbg with Apache License 2.0 6 votes vote down vote up
@Override
public int getsockopt(int level, int optname, Pointer optval, Pointer optlen) {
    try {
        switch (level) {
            case SOL_SOCKET:
                if (optname == SO_ERROR) {
                    optlen.setInt(0, 4);
                    optval.setInt(0, 0);
                    return 0;
                }
                break;
            case IPPROTO_TCP:
                if (optname == TCP_NODELAY) {
                    optlen.setInt(0, 4);
                    optval.setInt(0, getTcpNoDelay());
                    return 0;
                }
                break;
        }
    } catch (IOException e) {
        throw new IllegalStateException(e);
    }
    return super.getsockopt(level, optname, optval, optlen);
}
 
Example 2
Source File: ARM32SyscallHandler.java    From unidbg with Apache License 2.0 6 votes vote down vote up
private int getcpu(Emulator<?> emulator) {
    Arm32RegisterContext context = emulator.getContext();
    Pointer cpu = context.getR0Pointer();
    Pointer node = context.getR1Pointer();
    Pointer tcache = context.getR2Pointer();
    if (log.isDebugEnabled()) {
        log.debug("getcpu cpu=" + cpu + ", node=" + node + ", tcache=" + tcache);
    }
    if (cpu != null) {
        cpu.setInt(0, 0);
    }
    if (node != null) {
        node.setInt(0, 0);
    }
    return 0;
}
 
Example 3
Source File: ARM32SyscallHandler.java    From unidbg with Apache License 2.0 6 votes vote down vote up
private int _kernelrpc_mach_port_construct_trap(Emulator<?> emulator) {
    Unicorn unicorn = emulator.getUnicorn();
    int task = ((Number) unicorn.reg_read(ArmConst.UC_ARM_REG_R0)).intValue();
    Pointer options = UnicornPointer.register(emulator, ArmConst.UC_ARM_REG_R1);
    int r2 = ((Number) unicorn.reg_read(ArmConst.UC_ARM_REG_R2)).intValue();
    long r3 = ((Number) unicorn.reg_read(ArmConst.UC_ARM_REG_R3)).intValue();
    long context = r2 | (r3 << 32);
    Pointer name = UnicornPointer.register(emulator, ArmConst.UC_ARM_REG_R4);
    if (log.isDebugEnabled()) {
        MachPortOptions portOptions = new MachPortOptions(options);
        portOptions.unpack();
        log.debug("_kernelrpc_mach_port_construct_trap task=" + task + ", options=" + options + ", context=0x" + Long.toHexString(context) + ", name=" + name + ", portOptions=" + portOptions);
    }
    name.setInt(0, 0x88);
    return 0;
}
 
Example 4
Source File: ARM32SyscallHandler.java    From unidbg with Apache License 2.0 6 votes vote down vote up
private int clock_gettime(Unicorn u, Emulator<?> emulator) {
    int clk_id = ((Number) u.reg_read(ArmConst.UC_ARM_REG_R0)).intValue();
    Pointer tp = UnicornPointer.register(emulator, ArmConst.UC_ARM_REG_R1);
    long offset = clk_id == CLOCK_REALTIME ? System.currentTimeMillis() * 1000000L : System.nanoTime() - nanoTime;
    long tv_sec = offset / 1000000000L;
    long tv_nsec = offset % 1000000000L;
    if (log.isDebugEnabled()) {
        log.debug("clock_gettime clk_id=" + clk_id + ", tp=" + tp + ", offset=" + offset + ", tv_sec=" + tv_sec + ", tv_nsec=" + tv_nsec);
    }
    switch (clk_id) {
        case CLOCK_REALTIME:
        case CLOCK_MONOTONIC:
        case CLOCK_MONOTONIC_RAW:
        case CLOCK_MONOTONIC_COARSE:
        case CLOCK_BOOTTIME:
            tp.setInt(0, (int) tv_sec);
            tp.setInt(4, (int) tv_nsec);
            return 0;
    }
    throw new UnsupportedOperationException("clk_id=" + clk_id);
}
 
Example 5
Source File: NotesStringUtils.java    From domino-jna with Apache License 2.0 6 votes vote down vote up
/**
 * Writes a UNID string to memory
 * 
 * @param unidStr UNID string
 * @param target target memory
 */
public static void unidToPointer(String unidStr, Pointer target) {
	try {
		int fileInnards1 = (int) (Long.parseLong(unidStr.substring(0,8), 16) & 0xffffffff);
		int fileInnards0 = (int) (Long.parseLong(unidStr.substring(8,16), 16) & 0xffffffff);

		int noteInnards1 = (int) (Long.parseLong(unidStr.substring(16,24), 16) & 0xffffffff);
		int noteInnards0 = (int) (Long.parseLong(unidStr.substring(24,32), 16) & 0xffffffff);

		target.setInt(0, fileInnards0);
		target.share(4).setInt(0, fileInnards1);
		target.share(8).setInt(0, noteInnards0);
		target.share(12).setInt(0, noteInnards1);
	}
	catch (Exception e) {
		throw new NotesError(0, "Could not convert UNID to memory: "+unidStr, e);
	}
}
 
Example 6
Source File: SocketIO.java    From unidbg with Apache License 2.0 6 votes vote down vote up
protected final void fillAddress(InetSocketAddress socketAddress, Pointer addr, Pointer addrlen) {
    InetAddress address = socketAddress.getAddress();
    SockAddr sockAddr = new SockAddr(addr);
    sockAddr.sin_port = (short) socketAddress.getPort();
    if (address instanceof Inet4Address) {
        sockAddr.sin_family = AF_INET;
        sockAddr.sin_addr = Arrays.copyOf(address.getAddress(), IPV4_ADDR_LEN - 4);
        addrlen.setInt(0, IPV4_ADDR_LEN);
    } else if (address instanceof Inet6Address) {
        sockAddr.sin_family = AF_INET6;
        sockAddr.sin_addr = Arrays.copyOf(address.getAddress(), IPV6_ADDR_LEN - 4);
        addrlen.setInt(0, IPV6_ADDR_LEN);
    } else {
        throw new UnsupportedOperationException();
    }
}
 
Example 7
Source File: SocketIO.java    From unidbg with Apache License 2.0 6 votes vote down vote up
@Override
public int getsockopt(int level, int optname, Pointer optval, Pointer optlen) {
    try {
        switch (level) {
            case SOL_SOCKET:
                if (optname == SO_ERROR) {
                    optlen.setInt(0, 4);
                    optval.setInt(0, 0);
                    return 0;
                }
                break;
            case IPPROTO_TCP:
                if (optname == TCP_NODELAY) {
                    optlen.setInt(0, 4);
                    optval.setInt(0, getTcpNoDelay());
                    return 0;
                }
                break;
        }
    } catch (IOException e) {
        throw new IllegalStateException(e);
    }
    return super.getsockopt(level, optname, optval, optlen);
}
 
Example 8
Source File: SocketIO.java    From unidbg with Apache License 2.0 6 votes vote down vote up
protected final void fillAddress(InetSocketAddress socketAddress, Pointer addr, Pointer addrlen) {
    InetAddress address = socketAddress.getAddress();
    SockAddr sockAddr = new SockAddr(addr);
    sockAddr.sin_port = (short) socketAddress.getPort();
    if (address instanceof Inet4Address) {
        sockAddr.sin_family = AF_INET;
        sockAddr.sin_addr = Arrays.copyOf(address.getAddress(), IPV4_ADDR_LEN - 4);
        addrlen.setInt(0, IPV4_ADDR_LEN);
    } else if (address instanceof Inet6Address) {
        sockAddr.sin_family = AF_INET6;
        sockAddr.sin_addr = Arrays.copyOf(address.getAddress(), IPV6_ADDR_LEN - 4);
        addrlen.setInt(0, IPV6_ADDR_LEN);
    } else {
        throw new UnsupportedOperationException();
    }
}
 
Example 9
Source File: ByteArray.java    From unidbg with Apache License 2.0 5 votes vote down vote up
@Override
public UnicornPointer _GetArrayCritical(Emulator<?> emulator, Pointer isCopy) {
    if (isCopy != null) {
        isCopy.setInt(0, VM.JNI_TRUE);
    }
    UnicornPointer pointer = this.allocateMemoryBlock(emulator, value.length);
    pointer.write(0, value, 0, value.length);
    return pointer;
}
 
Example 10
Source File: ARM64SyscallHandler.java    From unidbg with Apache License 2.0 5 votes vote down vote up
private int _kernelrpc_mach_port_construct_trap(Emulator<?> emulator) {
    Arm64RegisterContext context = emulator.getContext();
    int task = context.getXInt(0);
    Pointer options = context.getXPointer(1);
    long ctx = context.getXInt(2);
    Pointer name = context.getXPointer(3);
    if (log.isDebugEnabled()) {
        MachPortOptions portOptions = new MachPortOptions(options);
        portOptions.unpack();
        log.debug("_kernelrpc_mach_port_construct_trap task=" + task + ", options=" + options + ", context=0x" + Long.toHexString(ctx) + ", name=" + name + ", portOptions=" + portOptions);
    }
    name.setInt(0, 0x88);
    return 0;
}
 
Example 11
Source File: ARM64SyscallHandler.java    From unidbg with Apache License 2.0 5 votes vote down vote up
private int pthread_getugid_np(Emulator<?> emulator) {
    RegisterContext context = emulator.getContext();
    Pointer uid = context.getPointerArg(0);
    Pointer gid = context.getPointerArg(1);
    if (log.isDebugEnabled()) {
        log.debug("pthread_getugid_np uid=" + uid + ", gid=" + gid);
    }
    uid.setInt(0, 0);
    gid.setInt(0, 0);
    return 0;
}
 
Example 12
Source File: ARM64SyscallHandler.java    From unidbg with Apache License 2.0 5 votes vote down vote up
private int _kernelrpc_mach_port_allocate_trap(Emulator<?> emulator) {
    RegisterContext context = emulator.getContext();
    int task = context.getIntArg(0);
    int right = context.getIntArg(1);
    Pointer name = context.getPointerArg(2);
    if (log.isDebugEnabled()) {
        log.debug("_kernelrpc_mach_port_allocate_trap task=" + task + ", right=" + right + ", name=" + name);
    }
    name.setInt(0, STATIC_PORT);
    return 0;
}
 
Example 13
Source File: ARM32SyscallHandler.java    From unidbg with Apache License 2.0 5 votes vote down vote up
private int _kernelrpc_mach_port_allocate_trap(Emulator<?> emulator) {
    Unicorn unicorn = emulator.getUnicorn();
    int task = ((Number) unicorn.reg_read(ArmConst.UC_ARM_REG_R0)).intValue();
    int right = ((Number) unicorn.reg_read(ArmConst.UC_ARM_REG_R1)).intValue();
    Pointer name = UnicornPointer.register(emulator, ArmConst.UC_ARM_REG_R2);
    if (log.isDebugEnabled()) {
        log.debug("_kernelrpc_mach_port_allocate_trap task=" + task + ", right=" + right + ", name=" + name);
    }
    name.setInt(0, STATIC_PORT);
    return 0;
}
 
Example 14
Source File: SizeTByReference.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public void setValue(SizeT value) {
  Pointer p = getPointer();
  if (Native.SIZE_T_SIZE == 8) {
    p.setLong(0, value.longValue());
  } else {
    p.setInt(0, value.intValue());
  }
}
 
Example 15
Source File: IntArray.java    From unidbg with Apache License 2.0 5 votes vote down vote up
@Override
public UnicornPointer _GetArrayCritical(Emulator<?> emulator, Pointer isCopy) {
    if (isCopy != null) {
        isCopy.setInt(0, VM.JNI_TRUE);
    }
    UnicornPointer pointer = this.allocateMemoryBlock(emulator, value.length * 4);
    pointer.write(0, value, 0, value.length);
    return pointer;
}
 
Example 16
Source File: ARM32SyscallHandler.java    From unidbg with Apache License 2.0 5 votes vote down vote up
private int futex(Unicorn u, Emulator<?> emulator) {
    Pointer uaddr = UnicornPointer.register(emulator, ArmConst.UC_ARM_REG_R0);
    int futex_op = ((Number) u.reg_read(ArmConst.UC_ARM_REG_R1)).intValue();
    int val = ((Number) u.reg_read(ArmConst.UC_ARM_REG_R2)).intValue();
    int old = uaddr.getInt(0);
    if (log.isDebugEnabled()) {
        log.debug("futex uaddr=" + uaddr + ", _futexop=" + futex_op + ", op=" + (futex_op & 0x7f) + ", val=" + val + ", old=" + old);
    }

    switch (futex_op & 0x7f) {
        case FUTEX_WAIT:
            if (old != val) {
                throw new IllegalStateException("old=" + old + ", val=" + val);
            }
            Pointer timeout = UnicornPointer.register(emulator, ArmConst.UC_ARM_REG_R3);
            int mytype = val & 0xc000;
            int shared = val & 0x2000;
            if (log.isDebugEnabled()) {
                log.debug("futex FUTEX_WAIT mytype=" + mytype + ", shared=" + shared + ", timeout=" + timeout + ", test=" + (mytype | shared));
            }
            uaddr.setInt(0, mytype | shared);
            return 0;
        case FUTEX_WAKE:
            return 0;
        default:
            throw new AbstractMethodError();
    }
}
 
Example 17
Source File: ARM64SyscallHandler.java    From unidbg with Apache License 2.0 5 votes vote down vote up
private int futex(Unicorn u, Emulator<?> emulator) {
    Pointer uaddr = UnicornPointer.register(emulator, Arm64Const.UC_ARM64_REG_X0);
    int futex_op = ((Number) u.reg_read(Arm64Const.UC_ARM64_REG_X1)).intValue();
    int val = ((Number) u.reg_read(Arm64Const.UC_ARM64_REG_X2)).intValue();
    int old = uaddr.getInt(0);
    if (log.isDebugEnabled()) {
        log.debug("futex uaddr=" + uaddr + ", _futexop=" + futex_op + ", op=" + (futex_op & 0x7f) + ", val=" + val + ", old=" + old);
    }

    switch (futex_op & 0x7f) {
        case FUTEX_WAIT:
            if (old != val) {
                throw new IllegalStateException("old=" + old + ", val=" + val);
            }
            Pointer timeout = UnicornPointer.register(emulator, Arm64Const.UC_ARM64_REG_X3);
            int mytype = val & 0xc000;
            int shared = val & 0x2000;
            if (log.isDebugEnabled()) {
                log.debug("futex FUTEX_WAIT mytype=" + mytype + ", shared=" + shared + ", timeout=" + timeout + ", test=" + (mytype | shared));
            }
            uaddr.setInt(0, mytype | shared);
            return 0;
        case FUTEX_WAKE:
            return 0;
        default:
            throw new AbstractMethodError();
    }
}
 
Example 18
Source File: ARM32SyscallHandler.java    From unidbg with Apache License 2.0 5 votes vote down vote up
private int pthread_getugid_np(Emulator<?> emulator) {
    Pointer uid = UnicornPointer.register(emulator, ArmConst.UC_ARM_REG_R0);
    Pointer gid = UnicornPointer.register(emulator, ArmConst.UC_ARM_REG_R1);
    if (log.isDebugEnabled()) {
        log.debug("pthread_getugid_np uid=" + uid + ", gid=" + gid);
    }
    uid.setInt(0, 0);
    gid.setInt(0, 0);
    return 0;
}
 
Example 19
Source File: ArmLD.java    From unidbg with Apache License 2.0 4 votes vote down vote up
private long dlopen(Memory memory, String filename, Emulator<?> emulator) {
    Pointer pointer = UnicornPointer.register(emulator, ArmConst.UC_ARM_REG_SP);
    try {
        Module module = memory.dlopen(filename, false);
        pointer = pointer.share(-4); // return value
        if (module == null) {
            pointer.setInt(0, 0);

            pointer = pointer.share(-4); // NULL-terminated
            pointer.setInt(0, 0);

            if (!"libnetd_client.so".equals(filename)) {
                log.info("dlopen failed: " + filename);
            } else if(log.isDebugEnabled()) {
                log.debug("dlopen failed: " + filename);
            }
            this.error.setString(0, "Resolve library " + filename + " failed");
            return 0;
        } else {
            pointer.setInt(0, (int) module.base);

            pointer = pointer.share(-4); // NULL-terminated
            pointer.setInt(0, 0);

            for (Module md : memory.getLoadedModules()) {
                LinuxModule m = (LinuxModule) md;
                if (!m.getUnresolvedSymbol().isEmpty()) {
                    continue;
                }
                for (InitFunction initFunction : m.initFunctionList) {
                    if (log.isDebugEnabled()) {
                        log.debug("[" + m.name + "]PushInitFunction: 0x" + Long.toHexString(initFunction.getAddress()));
                    }
                    pointer = pointer.share(-4); // init array
                    pointer.setInt(0, (int) initFunction.getAddress());
                }
                m.initFunctionList.clear();
            }

            return module.base;
        }
    } finally {
        unicorn.reg_write(ArmConst.UC_ARM_REG_SP, ((UnicornPointer) pointer).peer);
    }
}
 
Example 20
Source File: MyARMSyscallHandler.java    From unidbg with Apache License 2.0 4 votes vote down vote up
@Override
    protected int ptrace(Unicorn u, Emulator<?> emulator) {
        RegisterContext context = emulator.getContext();
        int request = context.getIntArg(0);
        int pid = context.getIntArg(1);
        UnicornPointer addr = context.getPointerArg(2);
        Pointer data = context.getPointerArg(3);
        String msg = "ptrace request=0x" + Integer.toHexString(request) + ", pid=" + pid + ", addr=" + addr + ", data=" + data + ", LR=" + context.getLRPointer();
        switch (request) {
            case PTrace.PTRACE_ATTACH:
            case PTrace.PTRACE_CONT:
            case PTrace.PTRACE_DETACH:
            case PTrace.PTRACE_KILL:
            case PTrace.PTRACE_POKETEXT:
                break;
            case PTrace.PTRACE_POKEDATA: {
                addr.setPointer(0, data);
                break;
            }
            case PTrace.PTRACE_PEEKTEXT: {
                int val = addr.getInt(0);
                data.setInt(0, val);
                break;
            }
            case PTrace.PTRACE_GETREGS: {
                ArmRegister register = new ArmRegister(data);
                register.fill(u);
                register.pack();
                System.out.println(register);
                break;
            }
            case PTrace.PTRACE_PEEKUSR: {
                int off = (int) addr.toUIntPeer() / 4;
                int reg = ArmConst.UC_ARM_REG_INVALID;
                if (off == Reg32.ARM_pc) {
                    reg = ArmConst.UC_ARM_REG_PC;
                } else {
                    msg += (", off=" + off);
                }
                if (reg != ArmConst.UC_ARM_REG_INVALID) {
                    data.setInt(0, ArmRegister.readReg(u, reg));
                    break;
                }
            }
            default:
                System.err.println(msg);
                emulator.attach().debug();
                return -1;
        }
//        System.out.println(msg);
        return 0;
    }