2023-05-06 01:31:10 +03:00
|
|
|
all: jar lib samples
|
2015-08-29 06:54:05 +03:00
|
|
|
|
2023-05-06 01:31:10 +03:00
|
|
|
JC=javac
|
2015-08-29 06:54:05 +03:00
|
|
|
|
2023-05-06 01:31:10 +03:00
|
|
|
JAVA_HOME := $(shell readlink -f `which $(JC)` | sed "s:/bin/$(JC)::")
|
2015-08-25 13:21:47 +03:00
|
|
|
|
2023-05-06 01:31:10 +03:00
|
|
|
JAVA_INC := $(shell realpath $(JAVA_HOME)/include)
|
2015-08-25 13:21:47 +03:00
|
|
|
|
2023-05-06 01:31:10 +03:00
|
|
|
JAVA_PLATFORM_INC := $(shell dirname `find $(JAVA_INC) -name jni_md.h`)
|
|
|
|
|
|
|
|
UNICORN_INC=../../include
|
|
|
|
|
|
|
|
SAMPLES := $(shell ls samples/*.java)
|
2023-05-06 02:24:45 +03:00
|
|
|
TESTS := $(shell ls tests/*.java)
|
2023-05-06 01:31:10 +03:00
|
|
|
SRC := $(shell ls unicorn/*.java)
|
|
|
|
|
|
|
|
OS := $(shell uname)
|
|
|
|
ifeq ($(OS),Darwin)
|
|
|
|
LIB_EXT=.dylib
|
|
|
|
else ifeq ($(OS),Linux)
|
|
|
|
LIB_EXT=.so
|
|
|
|
else
|
|
|
|
LIB_EXT=.dll
|
|
|
|
endif
|
|
|
|
|
|
|
|
CC=gcc
|
|
|
|
CFLAGS=-fPIC
|
|
|
|
LDFLAGS=-shared -fPIC
|
|
|
|
LIBS=-lunicorn
|
|
|
|
LIBDIR=-L../../
|
|
|
|
INCS=-I$(JAVA_INC) -I$(JAVA_PLATFORM_INC) -I$(UNICORN_INC)
|
|
|
|
|
|
|
|
CLASSPATH=./
|
|
|
|
|
|
|
|
.SUFFIXES: .java .class
|
|
|
|
|
2023-05-06 02:24:45 +03:00
|
|
|
tests/%.class: tests/%.java
|
2023-05-13 07:25:34 +03:00
|
|
|
$(JC) -Xlint:deprecation -classpath .:unicorn.jar:testdep/junit-4.13.2.jar $(JFLAGS) $<
|
2023-05-06 02:24:45 +03:00
|
|
|
|
2023-05-06 01:31:10 +03:00
|
|
|
%.class: %.java
|
2023-05-13 07:25:34 +03:00
|
|
|
$(JC) -Xlint:deprecation -classpath .:unicorn.jar $(JFLAGS) $<
|
2023-05-06 01:31:10 +03:00
|
|
|
|
|
|
|
OBJS=unicorn_Unicorn.o
|
|
|
|
|
|
|
|
JARFILE=unicorn.jar
|
|
|
|
|
|
|
|
%.o: %.c
|
|
|
|
$(CC) -c $(CFLAGS) $(INCS) $< -o $@
|
|
|
|
|
|
|
|
unicorn_Unicorn.h: unicorn/Unicorn.java
|
2023-05-13 07:25:34 +03:00
|
|
|
$(JC) -h . $<
|
2023-05-06 01:31:10 +03:00
|
|
|
|
|
|
|
unicorn_Unicorn.o: unicorn_Unicorn.c unicorn_Unicorn.h
|
Rewrite the Java bindings.
This brings the Java API up to par with Python feature-wise and substantially
simplifies the hook implementation, enabling proper bounds-checked hooks.
The rewrite strives for compatibility with the previous API, but there are some
breaking changes. It is possible to push closer to full backwards compatibility
if required, at the cost of reintroducing some of the suboptimal designs. Here
are the main points of breakage:
- ReadHook and WriteHook are gone, replaced simply by MemHook. Hooking valid
memory accesses now requires a type parameter. This enables fetch and
read-after hooks with a unified API and a single callback object.
- mem_read now takes an int, not a long. We are unable to allocate more than 2GB
in a single request anyway (Java limitation).
- Instruction hooks now require specifying the instruction explicitly, instead
of guessing based on the hook type. This is necessary to distinguish
sysenter/syscall and ARM64 mrs/msr/sys/sysl, without excessively bloating the
library with redundant hook types. Bounds must also be specified, to support
bounds-checked instruction hooks.
- Reading object-type registers (any register larger than 64 bits, or registers
with special formats) requires a second argument to reg_read. This allows us
to provide a fast reg_read that returns a long for the common cases, while
still supporting a more general reg_read for other registers.
- mem_map_ptr is rewritten to take a *direct* java.nio.Buffer, which enables
many more use cases than a simple byte array, and improves performance (a
byte array cannot really be used as a mapped buffer without GC-pinning it,
which hurts the GC performance).
- Context handling API is redesigned to be safer and more object-oriented.
A lot of bugs are fixed with this implementation:
- Unicorn instances can be properly garbage-collected, instead of hanging around
forever in the Unicorn.unicorns table.
- Hooks no longer fire outside of their bounds (#1164), and in fact, hook bounds
are properly respected (previously, all hooks were just registered globally to
all addresses).
- Hooks are substantially faster, as they are now dispatched directly via a
single method call rather than being indirected through invokeCallbacks.
- Loading vector registers works now, rather than crashing the VM (#1539).
Several features are now enabled in the Java implementation:
- All of the current ctl_* calls are implemented.
- mmio_map is implemented.
- New virtual TLB mode is implemented.
- reading/writing Context registers is implemented.
- New hook types are added: TcgOpcodeHook, EdgeGeneratedHook,
InvalidInstructionHook, TlbFillHook, and the instruction hooks Arm64SysHook,
CpuidHook.
- All known special registers are supported.
2023-05-07 03:49:41 +03:00
|
|
|
$(CC) -O2 -Wall -Wextra -Wno-unused-parameter -c $(CFLAGS) $(INCS) $< -o $@
|
2023-05-06 01:31:10 +03:00
|
|
|
|
|
|
|
libunicorn_java$(LIB_EXT): unicorn_Unicorn.o
|
|
|
|
|
|
|
|
lib: libunicorn_java$(LIB_EXT) unicorn_Unicorn.h
|
|
|
|
$(CC) -o $< $(LDFLAGS) $(OBJS) $(LIBDIR) $(LIBS)
|
|
|
|
|
|
|
|
samples: $(SAMPLES:.java=.class)
|
2023-05-06 02:24:45 +03:00
|
|
|
tests: $(TESTS:.java=.class)
|
2023-05-06 01:31:10 +03:00
|
|
|
jarfiles: $(SRC:.java=.class)
|
|
|
|
|
|
|
|
jar: jarfiles
|
|
|
|
jar cf $(JARFILE) unicorn/*.class
|
2015-08-25 13:21:47 +03:00
|
|
|
|
2023-05-06 02:24:45 +03:00
|
|
|
test: lib samples tests
|
Rewrite the Java bindings.
This brings the Java API up to par with Python feature-wise and substantially
simplifies the hook implementation, enabling proper bounds-checked hooks.
The rewrite strives for compatibility with the previous API, but there are some
breaking changes. It is possible to push closer to full backwards compatibility
if required, at the cost of reintroducing some of the suboptimal designs. Here
are the main points of breakage:
- ReadHook and WriteHook are gone, replaced simply by MemHook. Hooking valid
memory accesses now requires a type parameter. This enables fetch and
read-after hooks with a unified API and a single callback object.
- mem_read now takes an int, not a long. We are unable to allocate more than 2GB
in a single request anyway (Java limitation).
- Instruction hooks now require specifying the instruction explicitly, instead
of guessing based on the hook type. This is necessary to distinguish
sysenter/syscall and ARM64 mrs/msr/sys/sysl, without excessively bloating the
library with redundant hook types. Bounds must also be specified, to support
bounds-checked instruction hooks.
- Reading object-type registers (any register larger than 64 bits, or registers
with special formats) requires a second argument to reg_read. This allows us
to provide a fast reg_read that returns a long for the common cases, while
still supporting a more general reg_read for other registers.
- mem_map_ptr is rewritten to take a *direct* java.nio.Buffer, which enables
many more use cases than a simple byte array, and improves performance (a
byte array cannot really be used as a mapped buffer without GC-pinning it,
which hurts the GC performance).
- Context handling API is redesigned to be safer and more object-oriented.
A lot of bugs are fixed with this implementation:
- Unicorn instances can be properly garbage-collected, instead of hanging around
forever in the Unicorn.unicorns table.
- Hooks no longer fire outside of their bounds (#1164), and in fact, hook bounds
are properly respected (previously, all hooks were just registered globally to
all addresses).
- Hooks are substantially faster, as they are now dispatched directly via a
single method call rather than being indirected through invokeCallbacks.
- Loading vector registers works now, rather than crashing the VM (#1539).
Several features are now enabled in the Java implementation:
- All of the current ctl_* calls are implemented.
- mmio_map is implemented.
- New virtual TLB mode is implemented.
- reading/writing Context registers is implemented.
- New hook types are added: TcgOpcodeHook, EdgeGeneratedHook,
InvalidInstructionHook, TlbFillHook, and the instruction hooks Arm64SysHook,
CpuidHook.
- All known special registers are supported.
2023-05-07 03:49:41 +03:00
|
|
|
java -Xcheck:jni -cp .:testdep/hamcrest-2.2.jar:testdep/junit-4.13.2.jar org.junit.runner.JUnitCore $(subst /,.,$(TESTS:.java=))
|
2023-05-06 02:24:45 +03:00
|
|
|
|
2015-08-25 13:21:47 +03:00
|
|
|
install: lib jar
|
2023-05-06 01:31:10 +03:00
|
|
|
cp libunicorn_java$(LIB_EXT) /usr/lib
|
|
|
|
cp $(JARFILE) /usr/share/java
|
2015-08-29 06:54:05 +03:00
|
|
|
|
2018-03-12 16:40:40 +03:00
|
|
|
uninstall:
|
2023-05-13 07:25:34 +03:00
|
|
|
rm -f /usr/lib/libunicorn_java$(LIB_EXT)
|
|
|
|
rm -f /usr/share/java/$(JARFILE)
|
2018-03-12 16:40:40 +03:00
|
|
|
|
2015-08-29 06:54:05 +03:00
|
|
|
gen_const:
|
2021-10-26 14:10:59 +03:00
|
|
|
cd .. && python3 const_generator.py java
|
2015-08-29 06:54:05 +03:00
|
|
|
|
|
|
|
clean:
|
2023-05-13 07:25:34 +03:00
|
|
|
rm -f unicorn/*.class
|
|
|
|
rm -f samples/*.class
|
|
|
|
rm -f *.so
|
|
|
|
rm -f *.dylib
|
|
|
|
rm -f *.dll
|
2023-05-06 01:31:10 +03:00
|
|
|
|
|
|
|
.PHONY: all lib samples jar install uninstall gen_const clean
|