Files
unicorn/bindings/rust/src/tests/mips.rs
mio bd5a8c5146 Squashed commit of the following:
commit 520c6647c32f02d83083d969d416154aa95e922c
Merge: 6bb29b12 b999f507
Author: mio <mio@lazym.io>
Date:   Sun Apr 13 00:14:23 2025 +0800

    merge dev

commit 6bb29b12f1d9f452365cc9cb5bc2d65ef376af30
Author: mio <mio@lazym.io>
Date:   Sun Apr 13 00:13:12 2025 +0800

    enable test

commit bcb8b363ef12ac295cf4fe4f1645416e5f0ea6ae
Author: mio <mio@lazym.io>
Date:   Sun Apr 13 00:13:06 2025 +0800

    also logging

commit 5972fc156b7379d09582c745d6d597e07555f2f4
Author: mio <mio@lazym.io>
Date:   Sun Apr 13 00:12:58 2025 +0800

    no unlimited translation

commit 7d600feebf9055505918e50d0af8b529a3eba542
Author: mio <mio@lazym.io>
Date:   Sun Apr 13 00:12:47 2025 +0800

    Ignore bindings.rs

commit dde4d50f2c7713156ac3bc284287480e4d92005f
Author: Amaan Qureshi <amaanq12@gmail.com>
Date:   Sun Apr 6 03:26:22 2025 -0400

    alias `uc_mips_reg` to `UC_MIPS_REG`

commit 04234ae01ba7c82d9717eaae64cdda289ce3b832
Author: Amaan Qureshi <amaanq12@gmail.com>
Date:   Sun Apr 6 01:13:00 2025 -0400

    remove bindings.rs

commit edec1300cd7c2d8ef4babbd51f6bcba2e126bdd7
Author: Amaan Qureshi <amaanq12@gmail.com>
Date:   Sat Apr 5 14:29:40 2025 -0400

    address review

commit feb157b28b6c262c5dc3d810ec54de55a25bcd6e
Author: Amaan Qureshi <amaanq12@gmail.com>
Date:   Sat Mar 29 22:40:53 2025 -0400

    ci(rust): rework workflow

    The notable changes are migrating to
    `actions-rust-lang/setup-rust-toolchain` for setting up Rust as it's
    maintained, and using `katyo/publish-crates` for publishing crates in a
    workspace

commit c1c7a8f8ed841b6ec5b4abe57013a1c2c9748c60
Author: Amaan Qureshi <amaanq12@gmail.com>
Date:   Sat Mar 29 22:40:06 2025 -0400

    build(rust): set `rust-version` to 1.85

commit 8df938c9f8b478160213707674157103b0893caf
Author: Amaan Qureshi <amaanq12@gmail.com>
Date:   Sat Mar 29 21:53:21 2025 -0400

    fix(rust): correct unsound pointer cast

    The size of `T` is not guaranteed to be the size of `i32` - all we know
    is that `T` is `Into<i32>`, so we should first copy them over into an
    `i32` array

commit 3059b2583a60aa0cac9278afc945ed87f7ddb65e
Author: Amaan Qureshi <amaanq12@gmail.com>
Date:   Sat Mar 29 20:13:26 2025 -0400

    docs(rust): update readme

commit 7db69a888e58a4bda20083e4e0771d26a327ad13
Author: Amaan Qureshi <amaanq12@gmail.com>
Date:   Sat Mar 29 13:58:30 2025 -0400

    feat(rust): add comprehensive tests

    These tests are copied over from the C tests

commit 78f2207f0e0481aef4de6d5908f8dc699a39a8d5
Author: Amaan Qureshi <amaanq12@gmail.com>
Date:   Sat Mar 29 13:57:27 2025 -0400

    feat(rust): add tcg hook

commit 46e53328531ec3279dadbf18c16b493432227b31
Author: Amaan Qureshi <amaanq12@gmail.com>
Date:   Sat Mar 29 13:56:55 2025 -0400

    feat(rust): add a hook for arm64 sys instructions

commit d1b58ee8282bf1eeeefbf68c87c2cf7c50c90320
Author: Amaan Qureshi <amaanq12@gmail.com>
Date:   Sat Mar 29 13:56:35 2025 -0400

    feat(rust): add the ability to read the arm coprocessor register

commit d304da18b9e6741042b2a70657437be8f39f5c7c
Author: Amaan Qureshi <amaanq12@gmail.com>
Date:   Sat Mar 29 13:55:29 2025 -0400

    feat(rust): add missing `Context` methods

commit 0dd87833081ac9db1feaf5bae8c839a7a2ae4947
Author: Amaan Qureshi <amaanq12@gmail.com>
Date:   Sat Mar 29 13:44:51 2025 -0400

    refactor(rust): remove unnecessary code

    `unicorn-engine-sys` will provide the necessary constants & types

commit da3d2fa7c3ecd3ae8fdb6672b6c5ea23da4570ff
Author: Amaan Qureshi <amaanq12@gmail.com>
Date:   Sat Mar 29 13:43:57 2025 -0400

    feat(rust): add a workspace `Cargo.toml`, and use `unicorn-engine-sys`

commit b27a2a93e4ac43aa2079e936df4dd30a1f8f329a
Author: Amaan Qureshi <amaanq12@gmail.com>
Date:   Sat Mar 29 13:38:06 2025 -0400

    feat(rust): introduce `unicorn-engine-sys` crate

    This crate contains generated Rust bindings to the C library via
    bindgen. It is independent from the main `unicorn-engine` bindings,
    which will leverage this

commit bcec87a3f6e316e328683c303ccfa89e530a6c56
Author: Amaan Qureshi <amaanq12@gmail.com>
Date:   Sat Mar 29 13:31:24 2025 -0400

    test(m68k): actually assert an expectation

    This test did not actually test for anything before

commit bc7e65ca96164496eb2e250b1f296a33a8aa58ee
Author: Amaan Qureshi <amaanq12@gmail.com>
Date:   Sat Mar 29 13:31:09 2025 -0400

    style(test): use bitflag shorthands

commit 0ab4b7fefb3ca17b0b5977d7b204291c5de184ad
Author: Amaan Qureshi <amaanq12@gmail.com>
Date:   Sat Mar 29 13:22:13 2025 -0400

    fix(mips): lowercase enum name `uc_mips_reg`

    This aligns with other architectures

Co-authored-by: Amaan Qureshi <amaanq12@gmail.com>
2025-04-13 00:17:55 +08:00

228 lines
5.9 KiB
Rust

use unicorn_engine_sys::RegisterMIPS;
use super::*;
const CODE_START: u64 = 0x10000000;
const CODE_LEN: usize = 0x4000;
fn uc_common_setup<T>(
arch: Arch,
mode: Mode,
cpu_model: Option<i32>,
code: &[u8],
data: T,
) -> Unicorn<'_, T> {
let mut uc = Unicorn::new_with_data(arch, mode, data).unwrap();
if let Some(cpu_model) = cpu_model {
uc.ctl_set_cpu_model(cpu_model).unwrap();
}
uc.mem_map(CODE_START, CODE_LEN, Prot::ALL).unwrap();
uc.mem_write(CODE_START, code).unwrap();
uc
}
#[test]
fn test_mips_el_ori() {
let code = [
0x56, 0x34, 0x21, 0x34, // ori $at, $at, 0x3456;
];
let r1 = 0x6789;
let mut uc = uc_common_setup(
Arch::MIPS,
Mode::MIPS32 | Mode::LITTLE_ENDIAN,
None,
&code,
(),
);
uc.reg_write(RegisterMIPS::R1, r1).unwrap();
uc.emu_start(CODE_START, CODE_START + code.len() as u64, 0, 0)
.unwrap();
let r1 = uc.reg_read(RegisterMIPS::R1).unwrap();
assert_eq!(r1, 0x77df);
}
#[test]
fn test_mips_eb_ori() {
let code = [
0x34, 0x21, 0x34, 0x56, // ori $at, $at, 0x3456;
];
let r1 = 0x6789;
let mut uc = uc_common_setup(Arch::MIPS, Mode::MIPS32 | Mode::BIG_ENDIAN, None, &code, ());
uc.reg_write(RegisterMIPS::R1, r1).unwrap();
uc.emu_start(CODE_START, CODE_START + code.len() as u64, 0, 0)
.unwrap();
let r1 = uc.reg_read(RegisterMIPS::R1).unwrap();
assert_eq!(r1, 0x77df);
}
#[test]
fn test_mips_stop_at_branch() {
let code = [
0x02, 0x00, 0x00, 0x08, // j 0x8
0x21, 0x10, 0x62, 0x00, // addu $v0, $v1, $v0
];
let v1 = 5;
let mut uc = uc_common_setup(
Arch::MIPS,
Mode::MIPS32 | Mode::LITTLE_ENDIAN,
None,
&code,
(),
);
uc.reg_write(RegisterMIPS::V1, v1).unwrap();
// Execute one instruction with branch delay slot.
uc.emu_start(CODE_START, CODE_START + code.len() as u64, 0, 1)
.unwrap();
let pc = uc.reg_read(RegisterMIPS::PC).unwrap();
let v1 = uc.reg_read(RegisterMIPS::V0).unwrap();
// Even if we just execute one instruction, the instruction in the
// delay slot would also be executed.
assert_eq!(pc, CODE_START + 0x8);
assert_eq!(v1, 0x5);
}
#[test]
fn test_mips_stop_at_delay_slot() {
let code = [
0x02, 0x00, 0x00, 0x08, // j 0x8
0x00, 0x00, 0x00, 0x00, // nop
0x00, 0x00, 0x00, 0x00, // nop
];
let mut uc = uc_common_setup(
Arch::MIPS,
Mode::MIPS32 | Mode::LITTLE_ENDIAN,
None,
&code,
(),
);
// Stop at the delay slot by design.
uc.emu_start(CODE_START, CODE_START + 4, 0, 0).unwrap();
let pc = uc.reg_read(RegisterMIPS::PC).unwrap();
// The branch instruction isn't committed and the PC is not updated.
// The user is responsible for restarting emulation at the branch instruction.
assert_eq!(pc, CODE_START);
}
#[test]
fn test_mips_stop_at_delay_slot_2() {
let code = [
0x24, 0x06, 0x00, 0x03, // addiu $a2, $zero, 3
0x10, 0xa6, 0x00, 0x79, // beq $a1, $a2, 0x1e8
0x30, 0x42, 0x00, 0xfc, // andi $v0, $v0, 0xfc
0x10, 0x40, 0x00, 0x32, // beqz $v0, 0x47c8c90
0x24, 0xab, 0xff, 0xda, // addiu $t3, $a1, -0x26
0x2d, 0x62, 0x00, 0x02, // sltiu $v0, $t3, 2
0x10, 0x40, 0x00, 0x32, // beqz $v0, 0x47c8c9c
0x00, 0x00, 0x00, 0x00, // nop
];
let v0 = 0xff;
let a1 = 0x3;
let mut uc = uc_common_setup(Arch::MIPS, Mode::MIPS32 | Mode::BIG_ENDIAN, None, &code, ());
uc.reg_write(RegisterMIPS::V0, v0).unwrap();
uc.reg_write(RegisterMIPS::A1, a1).unwrap();
uc.emu_start(CODE_START, CODE_START + code.len() as u64 + 16, 0, 2)
.unwrap();
let pc = uc.reg_read(RegisterMIPS::PC).unwrap();
let v0 = uc.reg_read(RegisterMIPS::V0).unwrap();
assert_eq!(pc, CODE_START + 4 + 0x1e8);
assert_eq!(v0, 0xfc);
}
#[test]
fn test_mips_lwx_exception_issue_1314() {
let code = [
0x0a, 0xc8, 0x79, 0x7e, // lwx $t9, $t9($s3)
];
let mut uc = uc_common_setup(
Arch::MIPS,
Mode::MIPS32 | Mode::LITTLE_ENDIAN,
None,
&code,
(),
);
uc.mem_map(0x10000, 0x4000, Prot::ALL).unwrap();
// Enable DSP
// https://s3-eu-west-1.amazonaws.com/downloads-mips/documents/MD00090-2B-MIPS32PRA-AFP-06.02.pdf
let mut reg = uc.reg_read(RegisterMIPS::CP0_STATUS).unwrap();
reg |= 1 << 24;
uc.reg_write(RegisterMIPS::CP0_STATUS, reg).unwrap();
reg = 0;
uc.reg_write(RegisterMIPS::R1, reg).unwrap();
uc.reg_write(RegisterMIPS::T9, reg).unwrap();
reg = 0xdeadbeef;
uc.mem_write(0x10000, &(reg as u32).to_le_bytes()).unwrap();
reg = 0x10000;
uc.reg_write(RegisterMIPS::S3, reg).unwrap();
uc.emu_start(CODE_START, CODE_START + code.len() as u64, 0, 0)
.unwrap();
reg = uc.reg_read(RegisterMIPS::T9).unwrap();
assert_eq!(reg, 0xdeadbeef);
}
#[test]
fn test_mips_mips16() {
let code = [
0xC4, 0x6B, 0x49, 0xE3, // sc $t1, 0x6bc4($k0)
];
let v0 = 0x6789;
let mips16_lowbit = 1;
let mut uc = uc_common_setup(
Arch::MIPS,
Mode::MIPS32 | Mode::LITTLE_ENDIAN,
None,
&code,
(),
);
uc.reg_write(RegisterMIPS::V0, v0).unwrap();
uc.emu_start(
CODE_START | mips16_lowbit,
CODE_START + code.len() as u64,
0,
0,
)
.unwrap();
let v0 = uc.reg_read(RegisterMIPS::V0).unwrap();
assert_eq!(v0, 0x684D);
}
#[test]
fn test_mips_mips_fpr() {
#[rustfmt::skip]
let code = [
0xf6, 0x42, 0x09, 0x3c, 0x79, 0xe9, 0x29, 0x35, // li $t1, 0x42f6e979
0x00, 0x08, 0x89, 0x44, // mtc1 $t1, $f1
];
let mut uc = uc_common_setup(Arch::MIPS, Mode::MIPS32, None, &code, ());
uc.emu_start(CODE_START, CODE_START + code.len() as u64, 0, 0)
.unwrap();
let f1 = uc.reg_read(RegisterMIPS::F1).unwrap();
assert_eq!(f1, 0x42f6e979);
}