Use an incrementing handle instead of returning a raw pointer to the user.
This commit is contained in:
@@ -27,6 +27,9 @@ import java.nio.ByteBuffer;
|
|||||||
import java.nio.ByteOrder;
|
import java.nio.ByteOrder;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.Hashtable;
|
import java.util.Hashtable;
|
||||||
|
import java.util.Iterator;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.concurrent.atomic.AtomicLong;
|
||||||
|
|
||||||
/** Unicorn is a lightweight multi-platform, multi-architecture CPU emulator framework. */
|
/** Unicorn is a lightweight multi-platform, multi-architecture CPU emulator framework. */
|
||||||
public class Unicorn
|
public class Unicorn
|
||||||
@@ -38,6 +41,15 @@ public class Unicorn
|
|||||||
private int mode;
|
private int mode;
|
||||||
private Hashtable<Long, HookWrapper> hooks = new Hashtable<>();
|
private Hashtable<Long, HookWrapper> hooks = new Hashtable<>();
|
||||||
|
|
||||||
|
/** Instead of handing out native pointers, we'll hand out a handle
|
||||||
|
* ID for safety. This prevents things like "double frees" -
|
||||||
|
* accidentally releasing an unrelated object via handle reuse. */
|
||||||
|
private static AtomicLong allocCounter = new AtomicLong(0x1000);
|
||||||
|
|
||||||
|
private static long nextAllocCounter() {
|
||||||
|
return allocCounter.addAndGet(8);
|
||||||
|
}
|
||||||
|
|
||||||
/** Wrapper around a registered hook */
|
/** Wrapper around a registered hook */
|
||||||
private static class HookWrapper {
|
private static class HookWrapper {
|
||||||
private long nativePtr;
|
private long nativePtr;
|
||||||
@@ -676,8 +688,9 @@ public class Unicorn
|
|||||||
private long registerHook(long val) {
|
private long registerHook(long val) {
|
||||||
HookWrapper wrapper = new HookWrapper();
|
HookWrapper wrapper = new HookWrapper();
|
||||||
wrapper.nativePtr = val;
|
wrapper.nativePtr = val;
|
||||||
hooks.put(val, wrapper);
|
long index = nextAllocCounter();
|
||||||
return val;
|
hooks.put(index, wrapper);
|
||||||
|
return index;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -982,9 +995,9 @@ public class Unicorn
|
|||||||
* @param hook The return value from any {@code hook_add} function.
|
* @param hook The return value from any {@code hook_add} function.
|
||||||
*/
|
*/
|
||||||
public void hook_del(long hook) throws UnicornException {
|
public void hook_del(long hook) throws UnicornException {
|
||||||
if (hooks.contains(hook)) {
|
if (hooks.containsKey(hook)) {
|
||||||
hooks.remove(hooks, hook);
|
HookWrapper wrapper = hooks.remove(hook);
|
||||||
_hook_del(nativePtr, hook);
|
_hook_del(nativePtr, wrapper.nativePtr);
|
||||||
} else {
|
} else {
|
||||||
throw new UnicornException("Hook is not registered!");
|
throw new UnicornException("Hook is not registered!");
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user