Skip to content

Commit

Permalink
backend/hypervisor: Update libhypervisor.dylib
Browse files Browse the repository at this point in the history
  • Loading branch information
zhkl0228 committed Dec 29, 2024
1 parent 1c0b5ee commit 65adc80
Show file tree
Hide file tree
Showing 8 changed files with 112 additions and 8 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,5 @@
*.idb
/unidbg-ios/src/test/resources/native/swift/swift_library.o
/unidbg-ios/src/test/resources/native/swift/swift_library-Swift.h
/backend/hypervisor/src/main/native/hypervisor/cmake-build-debug/
/backend/hypervisor/src/main/native/hypervisor/.idea/
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.github.unidbg.arm.backend.hypervisor;

import com.sun.jna.Pointer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -56,6 +57,18 @@ public class Hypervisor implements Closeable {
private static native int getBRPs(long handle);
private static native int getWRPs(long handle);

private static native long getCpuContext(long handle); // _hv_vcpu_get_context
private static native long getVCpus(); // find_vcpus

public final Pointer getCpuContextPointer() {
long peer = getCpuContext(nativeHandle);
return peer == 0L ? Pointer.NULL : new Pointer(peer);
}
public static Pointer getVCpusPointer() {
long peer = getVCpus();
return peer == 0 ? Pointer.NULL : new Pointer(peer);
}

public int getBRPs() {
return getBRPs(nativeHandle);
}
Expand Down Expand Up @@ -105,6 +118,10 @@ public void disable_watchpoint(int n) {
private static Hypervisor singleInstance;

public Hypervisor(boolean is64Bit) {
if (!is64Bit) {
throw new UnsupportedOperationException();
}

if (singleInstance != null) {
throw new IllegalStateException("Only one hypervisor VM instance per process allowed.");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,7 @@
import com.github.unidbg.Emulator;
import com.github.unidbg.Family;
import com.github.unidbg.arm.ARMEmulator;
import com.github.unidbg.arm.backend.BackendException;
import com.github.unidbg.arm.backend.CodeHook;
import com.github.unidbg.arm.backend.DebugHook;
import com.github.unidbg.arm.backend.HypervisorBackend;
import com.github.unidbg.arm.backend.ReadHook;
import com.github.unidbg.arm.backend.UnHook;
import com.github.unidbg.arm.backend.WriteHook;
import com.github.unidbg.arm.backend.*;
import com.github.unidbg.debugger.BreakPoint;
import com.github.unidbg.debugger.BreakPointCallback;
import com.github.unidbg.pointer.UnidbgPointer;
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 23 additions & 1 deletion backend/hypervisor/src/main/native/hypervisor/hypervisor.mm
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ static int cpu_loop(JNIEnv *env, t_hypervisor hypervisor, t_hypervisor_cpu cpu)
* Signature: ()V
*/
JNIEXPORT void JNICALL Java_com_github_unidbg_arm_backend_hypervisor_Hypervisor_testVcpu
(JNIEnv *, jclass) {
(JNIEnv *env, jclass clazz) {
auto cpu = (t_hypervisor_cpu) calloc(1, sizeof(struct hypervisor_cpu));
HYP_ASSERT_SUCCESS(hv_vcpu_create(&cpu->vcpu, &cpu->vcpu_exit, nullptr));
void *vcpu = lookupVcpu(cpu->vcpu);
Expand Down Expand Up @@ -193,6 +193,28 @@ static t_hypervisor_cpu get_hypervisor_cpu(JNIEnv *env, t_hypervisor hypervisor)
}
}

/*
* Class: com_github_unidbg_arm_backend_hypervisor_Hypervisor
* Method: getCpuContext
* Signature: (J)J
*/
JNIEXPORT jlong JNICALL Java_com_github_unidbg_arm_backend_hypervisor_Hypervisor_getCpuContext
(JNIEnv *env, jclass clazz, jlong handle) {
auto hypervisor = (t_hypervisor) handle;
t_hypervisor_cpu cpu = get_hypervisor_cpu(env, hypervisor);
return (jlong) _hv_vcpu_get_context(cpu->vcpu);
}

/*
* Class: com_github_unidbg_arm_backend_hypervisor_Hypervisor
* Method: getVCpus
* Signature: ()J
*/
JNIEXPORT jlong JNICALL Java_com_github_unidbg_arm_backend_hypervisor_Hypervisor_getVCpus
(JNIEnv *env, jclass clazz) {
return (jlong) find_vcpus();
}

/*
* Class: com_github_unidbg_arm_backend_hypervisor_Hypervisor
* Method: getBRPs
Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.github.unidbg.arm.backend.hypervisor;

import com.sun.jna.Pointer;
import junit.framework.TestCase;
import org.scijava.nativelib.NativeLoader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;

public class VCpuTest extends TestCase {

private static final Logger log = LoggerFactory.getLogger(VCpuTest.class);

static {
try {
NativeLoader.loadLibrary("hypervisor");
} catch (IOException ignored) {
}
}

public void testVcpu() throws Exception {
Pointer vcpu = Hypervisor.getVCpusPointer();
assertNotNull(vcpu);
System.out.println(vcpu);

try (final Hypervisor hypervisor = new Hypervisor(true)) {
{
Pointer context = hypervisor.getCpuContextPointer();
assertNotNull(context);
log.info("main context={}", context);
}

Thread thread = new Thread(() -> {
Pointer context = hypervisor.getCpuContextPointer();
assertNotNull(context);
log.info("thread context={}", context);
}, "TestThread");
thread.start();
thread.join();
}

}

}
8 changes: 8 additions & 0 deletions backend/hypervisor/src/test/resources/log4j.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
log4j.rootCategory=INFO, stdout

log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=[%d{HH:mm:ss SSS}] %5p [%t] (%c{2}:%L) - %m%n

log4j.logger.com.github.unidbg=INFO
log4j.logger.com.github.unidbg.arm.backend.hypervisor=DEBUG

0 comments on commit 65adc80

Please sign in to comment.