Commit Graph

3068 Commits

Author SHA1 Message Date
Robert Xiao
edd80ddeda Port sample_x86_32_gdt_and_seg_regs over to Sample_x86_mmr 2023-06-17 14:19:10 -07:00
Robert Xiao
4f563490e2 Update Java samples to match C samples.
Also add all of the samples as Java tests, referencing the output of the C
samples.
2023-06-17 14:19:10 -07:00
Robert Xiao
3739c7e3e0 Write some code to test out ARM64 CP register handling. 2023-06-17 14:19:10 -07:00
Robert Xiao
910bb572d3 Accept unsigned BigIntegers, and produce unsigned BigIntegers by default.
Unsigned BigIntegers are a bit more ergonomic, particularly for bitwise
operations. reg_write still accepts negative BigIntegers (and will automatically
sign extend them), but reg_read will produce unsigned BigIntegers by default.
2023-06-17 14:19:10 -07:00
Robert Xiao
d4df61b4c5 Refactor tests and add a few more 2023-06-17 14:19:10 -07:00
Robert Xiao
77d4a1d8b1 Fix definition of uc_version 2023-06-17 14:19:10 -07:00
Robert Xiao
32e638dcf4 Add more deprecated APIs for backwards compat 2023-06-17 14:19:10 -07:00
Robert Xiao
48870c4cc3 Reintroduce hook_del(Hook), since it seems useful.
This also improves backwards compatibility a bit.
2023-06-17 14:19:10 -07:00
Robert Xiao
e787f49d21 Use an incrementing handle instead of returning a raw pointer to the user. 2023-06-17 14:19:10 -07:00
Robert Xiao
4764d54250 Javadoc updates 2023-06-17 14:19:10 -07:00
Robert Xiao
3fab8abca7 Restore some of the less problematic old APIs for backwards compatibility. 2023-06-17 14:19:10 -07:00
Robert Xiao
b8bd25030e Javadoc updates 2023-06-17 14:19:10 -07:00
Robert Xiao
78de584409 Switch samples to use long instead of Long for registers 2023-06-17 14:19:10 -07:00
Robert Xiao
aa430587cc 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-06-17 14:19:10 -07:00
Robert Xiao
8777bb6ae6 Make close() idempotent and fix Unicorn memory leak. 2023-06-17 14:19:10 -07:00
Robert Xiao
66c8965f96 Set up testing infrastructure ("make test") 2023-06-17 14:19:10 -07:00
Robert Xiao
4b471e16e9 Remove redundant Makefile 2023-06-17 14:19:10 -07:00
Robert Xiao
9cdb5cb745 Reformat Java bindings. 2023-06-17 14:17:57 -07:00
lazymio
c5ae96536b
Merge pull request #1835 from nneonneo/reg_ops2
Implement reg_{read,write}2 API
2023-06-17 21:47:26 +02:00
Robert Xiao
2b80ab425b Return new UC_ERR_OVERFLOW instead of UC_ERR_NOMEM when reg buffer is too small 2023-06-16 15:30:59 -07:00
Robert Xiao
b041345a73 Fix RISCV test_riscv32_fp_move test
RISCV FP registers are 64-bit in size, even in 32-bit mode, because they can
hold doubles. The test even uses the double-precision instruction fmv.d. Thus,
the reads should be reading 64-bit registers.
2023-06-16 15:23:43 -07:00
Robert Xiao
30d202b89e Simplify reg_read/reg_write, obtaining a perf boost.
Single reg_read/reg_write is now about 25% faster.
2023-06-16 15:23:42 -07:00
Robert Xiao
074566cf69 Slight refactoring to reduce code duplication.
This also comes with a performance bump due to inlining of reg_read/reg_write
(as they're only called once now) and the unlikely() on CHECK_REG_TYPE.
2023-06-16 15:23:42 -07:00
Robert Xiao
4055a5ab10 Implement uc_reg_{read,write}{,_batch}2 APIs.
These APIs take size parameters, which can be used to properly bounds-check the
inputs and outputs for various registers. Additionally, all backends now throw
UC_ERR_ARG if the input register numbers are invalid.

Completes #1831.
2023-06-16 15:23:42 -07:00
Robert Xiao
d7a806c026 Reformat code with format.sh 2023-06-16 15:23:41 -07:00
mio
fa1f26138e
Fix missing stdint
Co-authored-by: ζeh Matt <5415177+ZehMatt@users.noreply.github.com>
2023-06-10 23:48:18 +02:00
mio
49ccbde2d0
Leave out essential files
Co-authored-by: ζeh Matt <5415177+ZehMatt@users.noreply.github.com>
2023-06-10 23:44:05 +02:00
mio
8dffbc159c
Add uc_ctl_get/set_tcg_buffer_size
We still need this API because the virtual memory address space of

32 bits os is only 4GB and we default need 1G per instance

Credits to @ZehMatt for original idea

Co-authored-by: ζeh Matt <5415177+ZehMatt@users.noreply.github.com>
2023-06-10 23:36:02 +02:00
mio
f8c7969d65
Revert "Add uc_ctl_get/set_tcg_buffer_size"
This reverts commit 3145e3c426 because not
properly co-authoer-ed.
2023-06-10 23:29:56 +02:00
mio
3145e3c426
Add uc_ctl_get/set_tcg_buffer_size 2023-06-10 16:08:29 +02:00
mio
5057f9925b
Fix typo 2023-06-10 15:26:29 +02:00
mio
9de80cb625
Correct calling convention 2023-06-10 15:03:59 +02:00
mio
3d5b2643f0
Support demand paging via closures and seh
Reverts 12a79192ee which exploits normal tcg mechanism

This uses a trampoline to pass extra data to seh handlers
2023-06-10 14:04:56 +02:00
Choongwoo Han
cfaa5be912
Comment out more unused page lock functions 2023-05-26 12:52:25 -07:00
lazymio
7b4dc569cc
Merge pull request #1836 from PhilippTakacs/UC_MEM_WRITE_PROT
fix UC_MEM_WRITE_PROT callback
2023-05-25 22:41:58 +02:00
lazymio
cd11aed350
Merge pull request #1837 from tunz/apt-update
Run apt update in nuget publishing workflow
2023-05-25 22:41:18 +02:00
Choongwoo Han
67f0386299
Add apt update 2023-05-23 13:18:57 -07:00
Choongwoo Han
75d26b7707
Ignore page_collection_lock 2023-05-23 13:11:36 -07:00
Takacs, Philipp
fa457a3a97 fix UC_MEM_WRITE_PROT callback
callbacks work on the physical address.
2023-05-22 15:38:37 +02:00
mio
994813a0e5
Also check cpu->stopped 2023-05-19 23:24:42 +02:00
mio
be2f092179
Merge remote-tracking branch 'phl/issuevtlb' into dev 2023-05-19 23:22:23 +02:00
mio
a24e53d794
Rebuild flags after writing to cp registers
This is buggy as this momemt per https://github.com/unicorn-engine/unicorn/issues/1789#issuecomment-1546807410

We need either doc this or save more information for a context
2023-05-14 13:35:31 +02:00
Takacs, Philipp
4a7b3b7a3a fixup! load_helper only call cpu_loop_exit() when emulation is running 2023-05-12 12:36:16 +02:00
lazymio
f65f8f8380
Merge pull request #1830 from nneonneo/fix-python-ctl
Fix sample_ctl.py
2023-05-11 13:10:01 +02:00
Robert Xiao
06a76e98c4 Add __repr__ to all ctypes.Structure subclasses 2023-05-10 12:58:25 -07:00
Takacs, Philipp
073c4b74ca load_helper only call cpu_loop_exit() when emulation is running
The load_helper is sometimes called from register writes. When the load
fails check if emulation is running before jump out of the emulated code.
2023-05-09 14:58:40 +02:00
lazymio
1d9c5c7653
Merge pull request #1832 from PhilippTakacs/cleanup
Some clean-up for the vtlb merge
2023-05-09 14:43:07 +02:00
Takacs, Philipp
227e578660 move typedef definition of enum uc_mem_type
forword references to enum types are forbidden in C. Also C++ will
not build if this is used
2023-05-08 15:38:43 +02:00
Takacs, Philipp
54870cca0e remove unused function cmp_vaddr 2023-05-08 15:32:58 +02:00
Robert Xiao
d27ca4530b Minor Python fixes.
- Match the types of UC_HOOK_CODE_CB and UC_HOOK_INSN_SYS_CB to C
- Avoid building a new namedtuple class in every call to _hook_insn_sys_cb
2023-05-07 09:08:20 -07:00