2016-07-01 14:01:06 +01:00
|
|
|
/* This file is part of the dynarmic project.
|
|
|
|
* Copyright (c) 2016 MerryMage
|
2020-04-23 15:25:11 +01:00
|
|
|
* SPDX-License-Identifier: 0BSD
|
2016-07-01 14:01:06 +01:00
|
|
|
*/
|
|
|
|
|
2016-08-17 15:53:36 +01:00
|
|
|
#include <algorithm>
|
|
|
|
#include <array>
|
2016-07-07 14:51:47 +01:00
|
|
|
#include <cinttypes>
|
2016-08-17 15:53:36 +01:00
|
|
|
#include <cstdio>
|
2016-07-07 12:01:47 +01:00
|
|
|
#include <cstring>
|
2016-07-12 13:25:33 +01:00
|
|
|
#include <functional>
|
2021-02-01 18:52:06 +00:00
|
|
|
#include <string_view>
|
2016-08-17 15:53:36 +01:00
|
|
|
#include <tuple>
|
2016-07-07 12:01:47 +01:00
|
|
|
|
2016-07-07 10:53:09 +01:00
|
|
|
#include <catch.hpp>
|
|
|
|
|
2021-05-19 17:28:35 +01:00
|
|
|
#include "../rand_int.h"
|
|
|
|
#include "../unicorn_emu/a32_unicorn.h"
|
|
|
|
#include "./testenv.h"
|
|
|
|
#include "dynarmic/common/bit_util.h"
|
|
|
|
#include "dynarmic/common/common_types.h"
|
|
|
|
#include "dynarmic/frontend/A32/FPSCR.h"
|
|
|
|
#include "dynarmic/frontend/A32/PSR.h"
|
2021-05-22 12:13:16 +01:00
|
|
|
#include "dynarmic/frontend/A32/disassembler/disassembler.h"
|
|
|
|
#include "dynarmic/frontend/A32/location_descriptor.h"
|
2021-05-19 17:28:35 +01:00
|
|
|
#include "dynarmic/frontend/A32/translate/translate.h"
|
|
|
|
#include "dynarmic/interface/A32/a32.h"
|
|
|
|
#include "dynarmic/ir/basic_block.h"
|
|
|
|
#include "dynarmic/ir/opt/passes.h"
|
2016-07-07 10:53:09 +01:00
|
|
|
|
2020-06-28 21:39:26 +01:00
|
|
|
using namespace Dynarmic;
|
|
|
|
|
|
|
|
static A32::UserConfig GetUserConfig(ThumbTestEnv* testenv) {
|
|
|
|
A32::UserConfig user_config;
|
|
|
|
user_config.optimizations &= ~OptimizationFlag::FastDispatch;
|
2018-01-27 22:36:55 +00:00
|
|
|
user_config.callbacks = testenv;
|
|
|
|
return user_config;
|
2016-07-07 12:01:47 +01:00
|
|
|
}
|
|
|
|
|
2018-01-27 22:36:55 +00:00
|
|
|
using WriteRecords = std::map<u32, u8>;
|
2016-07-04 10:22:11 +01:00
|
|
|
|
2016-07-18 15:11:16 +01:00
|
|
|
struct ThumbInstGen final {
|
2016-07-10 05:10:13 +01:00
|
|
|
public:
|
2021-05-22 14:51:20 +01:00
|
|
|
ThumbInstGen(
|
|
|
|
std::string_view format, std::function<bool(u32)> is_valid = [](u32) { return true; })
|
|
|
|
: is_valid(is_valid) {
|
2021-02-01 18:52:06 +00:00
|
|
|
REQUIRE((format.size() == 16 || format.size() == 32));
|
2016-07-10 05:10:13 +01:00
|
|
|
|
2021-02-01 18:52:06 +00:00
|
|
|
const auto bit_size = format.size();
|
|
|
|
|
|
|
|
for (size_t i = 0; i < bit_size; i++) {
|
|
|
|
const u32 bit = 1U << (bit_size - 1 - i);
|
2016-07-10 05:10:13 +01:00
|
|
|
switch (format[i]) {
|
2016-08-22 23:40:30 +01:00
|
|
|
case '0':
|
|
|
|
mask |= bit;
|
|
|
|
break;
|
|
|
|
case '1':
|
|
|
|
bits |= bit;
|
|
|
|
mask |= bit;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
// Do nothing
|
|
|
|
break;
|
2016-07-10 05:10:13 +01:00
|
|
|
}
|
2016-07-07 12:01:47 +01:00
|
|
|
}
|
|
|
|
}
|
2021-02-01 18:52:06 +00:00
|
|
|
|
|
|
|
u16 Generate16() const {
|
|
|
|
u32 inst;
|
|
|
|
|
|
|
|
do {
|
|
|
|
const auto random = RandInt<u16>(0, 0xFFFF);
|
|
|
|
inst = bits | (random & ~mask);
|
|
|
|
} while (!is_valid(inst));
|
|
|
|
|
|
|
|
ASSERT((inst & mask) == bits);
|
|
|
|
|
|
|
|
return static_cast<u16>(inst);
|
|
|
|
}
|
|
|
|
|
|
|
|
u32 Generate32() const {
|
|
|
|
u32 inst;
|
2016-07-18 15:11:16 +01:00
|
|
|
|
2016-07-10 05:10:13 +01:00
|
|
|
do {
|
2021-02-01 18:52:06 +00:00
|
|
|
const auto random = RandInt<u32>(0, 0xFFFFFFFF);
|
2016-07-10 05:10:13 +01:00
|
|
|
inst = bits | (random & ~mask);
|
|
|
|
} while (!is_valid(inst));
|
2016-07-18 15:11:16 +01:00
|
|
|
|
|
|
|
ASSERT((inst & mask) == bits);
|
|
|
|
|
2016-07-10 05:10:13 +01:00
|
|
|
return inst;
|
|
|
|
}
|
2021-02-01 18:52:06 +00:00
|
|
|
|
2016-07-10 05:10:13 +01:00
|
|
|
private:
|
2021-02-01 18:52:06 +00:00
|
|
|
u32 bits = 0;
|
|
|
|
u32 mask = 0;
|
|
|
|
std::function<bool(u32)> is_valid;
|
2016-07-10 05:10:13 +01:00
|
|
|
};
|
2016-07-07 12:01:47 +01:00
|
|
|
|
2021-05-22 14:51:20 +01:00
|
|
|
static bool DoesBehaviorMatch(const A32Unicorn<ThumbTestEnv>& uni, const A32::Jit& jit, const WriteRecords& interp_write_records, const WriteRecords& jit_write_records) {
|
2019-04-13 10:56:55 +01:00
|
|
|
const auto interp_regs = uni.GetRegisters();
|
2016-07-07 12:01:47 +01:00
|
|
|
const auto jit_regs = jit.Regs();
|
|
|
|
|
2021-05-22 14:51:20 +01:00
|
|
|
return std::equal(interp_regs.begin(), interp_regs.end(), jit_regs.begin(), jit_regs.end()) && uni.GetCpsr() == jit.Cpsr() && interp_write_records == jit_write_records;
|
2016-07-07 12:01:47 +01:00
|
|
|
}
|
|
|
|
|
2021-05-22 14:51:20 +01:00
|
|
|
static void RunInstance(size_t run_number, ThumbTestEnv& test_env, A32Unicorn<ThumbTestEnv>& uni, A32::Jit& jit, const ThumbTestEnv::RegisterArray& initial_regs, size_t instruction_count, size_t instructions_to_execute_count) {
|
2019-04-13 10:56:55 +01:00
|
|
|
uni.ClearPageCache();
|
2018-01-14 20:21:47 +00:00
|
|
|
jit.ClearCache();
|
|
|
|
|
|
|
|
// Setup initial state
|
|
|
|
|
2019-04-13 10:56:55 +01:00
|
|
|
uni.SetCpsr(0x000001F0);
|
|
|
|
uni.SetRegisters(initial_regs);
|
2018-01-14 20:21:47 +00:00
|
|
|
jit.SetCpsr(0x000001F0);
|
|
|
|
jit.Regs() = initial_regs;
|
|
|
|
|
|
|
|
// Run interpreter
|
2018-01-27 22:36:55 +00:00
|
|
|
test_env.modified_memory.clear();
|
2019-04-13 10:56:55 +01:00
|
|
|
test_env.ticks_left = instructions_to_execute_count;
|
|
|
|
uni.SetPC(uni.GetPC() | 1);
|
|
|
|
uni.Run();
|
|
|
|
const bool uni_code_memory_modified = test_env.code_mem_modified_by_guest;
|
|
|
|
const auto interp_write_records = test_env.modified_memory;
|
2018-01-14 20:21:47 +00:00
|
|
|
|
|
|
|
// Run jit
|
2019-04-13 10:56:55 +01:00
|
|
|
test_env.code_mem_modified_by_guest = false;
|
2018-01-27 22:36:55 +00:00
|
|
|
test_env.modified_memory.clear();
|
|
|
|
test_env.ticks_left = instructions_to_execute_count;
|
2018-01-14 20:21:47 +00:00
|
|
|
jit.Run();
|
2019-04-13 10:56:55 +01:00
|
|
|
const bool jit_code_memory_modified = test_env.code_mem_modified_by_guest;
|
|
|
|
const auto jit_write_records = test_env.modified_memory;
|
|
|
|
test_env.code_mem_modified_by_guest = false;
|
|
|
|
|
|
|
|
REQUIRE(uni_code_memory_modified == jit_code_memory_modified);
|
|
|
|
if (uni_code_memory_modified) {
|
|
|
|
return;
|
|
|
|
}
|
2018-01-14 20:21:47 +00:00
|
|
|
|
|
|
|
// Compare
|
2019-04-13 10:56:55 +01:00
|
|
|
if (!DoesBehaviorMatch(uni, jit, interp_write_records, jit_write_records)) {
|
2018-01-14 20:21:47 +00:00
|
|
|
printf("Failed at execution number %zu\n", run_number);
|
|
|
|
|
|
|
|
printf("\nInstruction Listing: \n");
|
|
|
|
for (size_t i = 0; i < instruction_count; i++) {
|
2020-06-28 21:39:26 +01:00
|
|
|
printf("%04x %s\n", test_env.code_mem[i], A32::DisassembleThumb16(test_env.code_mem[i]).c_str());
|
2018-01-14 20:21:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
printf("\nInitial Register Listing: \n");
|
2019-04-13 10:56:55 +01:00
|
|
|
for (size_t i = 0; i < initial_regs.size(); i++) {
|
|
|
|
printf("%4zu: %08x\n", i, initial_regs[i]);
|
2018-01-14 20:21:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
printf("\nFinal Register Listing: \n");
|
2019-04-13 10:56:55 +01:00
|
|
|
printf(" unicorn jit\n");
|
|
|
|
const auto uni_registers = uni.GetRegisters();
|
|
|
|
for (size_t i = 0; i < uni_registers.size(); i++) {
|
|
|
|
printf("%4zu: %08x %08x %s\n", i, uni_registers[i], jit.Regs()[i], uni_registers[i] != jit.Regs()[i] ? "*" : "");
|
2018-01-14 20:21:47 +00:00
|
|
|
}
|
2019-04-13 10:56:55 +01:00
|
|
|
printf("CPSR: %08x %08x %s\n", uni.GetCpsr(), jit.Cpsr(), uni.GetCpsr() != jit.Cpsr() ? "*" : "");
|
2018-01-14 20:21:47 +00:00
|
|
|
|
2019-04-13 10:56:55 +01:00
|
|
|
printf("\nUnicorn Write Records:\n");
|
|
|
|
for (const auto& record : interp_write_records) {
|
2018-01-27 22:36:55 +00:00
|
|
|
printf("[%08x] = %02x\n", record.first, record.second);
|
2018-01-14 20:21:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
printf("\nJIT Write Records:\n");
|
2019-04-13 10:56:55 +01:00
|
|
|
for (const auto& record : jit_write_records) {
|
2018-01-27 22:36:55 +00:00
|
|
|
printf("[%08x] = %02x\n", record.first, record.second);
|
2018-01-14 20:21:47 +00:00
|
|
|
}
|
|
|
|
|
2020-06-28 21:39:26 +01:00
|
|
|
A32::PSR cpsr;
|
2018-01-14 20:21:47 +00:00
|
|
|
cpsr.T(true);
|
|
|
|
|
|
|
|
size_t num_insts = 0;
|
|
|
|
while (num_insts < instructions_to_execute_count) {
|
2020-06-28 21:39:26 +01:00
|
|
|
A32::LocationDescriptor descriptor = {u32(num_insts * 4), cpsr, A32::FPSCR{}};
|
2021-05-22 12:13:16 +01:00
|
|
|
IR::Block ir_block = A32::Translate(descriptor, &test_env, {});
|
2020-06-28 21:39:26 +01:00
|
|
|
Optimization::A32GetSetElimination(ir_block);
|
|
|
|
Optimization::DeadCodeElimination(ir_block);
|
|
|
|
Optimization::A32ConstantMemoryReads(ir_block, &test_env);
|
|
|
|
Optimization::ConstantPropagation(ir_block);
|
|
|
|
Optimization::DeadCodeElimination(ir_block);
|
|
|
|
Optimization::VerificationPass(ir_block);
|
|
|
|
printf("\n\nIR:\n%s", IR::DumpBlock(ir_block).c_str());
|
2021-08-06 15:28:17 +01:00
|
|
|
printf("\n\nx86_64:\n");
|
|
|
|
jit.DumpDisassembly();
|
2018-01-14 20:21:47 +00:00
|
|
|
num_insts += ir_block.CycleCount();
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef _MSC_VER
|
|
|
|
__debugbreak();
|
|
|
|
#endif
|
|
|
|
FAIL();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-01 18:52:06 +00:00
|
|
|
void FuzzJitThumb16(const size_t instruction_count, const size_t instructions_to_execute_count, const size_t run_count, const std::function<u16()> instruction_generator) {
|
2018-01-27 22:36:55 +00:00
|
|
|
ThumbTestEnv test_env;
|
|
|
|
|
2019-04-13 10:56:55 +01:00
|
|
|
// Prepare memory.
|
2019-03-02 21:18:42 +00:00
|
|
|
test_env.code_mem.resize(instruction_count + 1);
|
2021-05-22 14:51:20 +01:00
|
|
|
test_env.code_mem.back() = 0xE7FE; // b +#0
|
2016-07-07 12:01:47 +01:00
|
|
|
|
|
|
|
// Prepare test subjects
|
2019-04-13 10:56:55 +01:00
|
|
|
A32Unicorn uni{test_env};
|
2020-06-28 21:39:26 +01:00
|
|
|
A32::Jit jit{GetUserConfig(&test_env)};
|
2016-07-07 12:01:47 +01:00
|
|
|
|
|
|
|
for (size_t run_number = 0; run_number < run_count; run_number++) {
|
2018-07-13 19:19:22 +01:00
|
|
|
ThumbTestEnv::RegisterArray initial_regs;
|
2021-05-22 14:51:20 +01:00
|
|
|
std::generate_n(initial_regs.begin(), initial_regs.size() - 1, [] { return RandInt<u32>(0, 0xFFFFFFFF); });
|
2016-07-07 12:01:47 +01:00
|
|
|
initial_regs[15] = 0;
|
|
|
|
|
2018-01-27 22:36:55 +00:00
|
|
|
std::generate_n(test_env.code_mem.begin(), instruction_count, instruction_generator);
|
2016-07-07 12:01:47 +01:00
|
|
|
|
2019-04-13 10:56:55 +01:00
|
|
|
RunInstance(run_number, test_env, uni, jit, initial_regs, instruction_count, instructions_to_execute_count);
|
2016-07-07 12:01:47 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-01 18:52:06 +00:00
|
|
|
void FuzzJitThumb32(const size_t instruction_count, const size_t instructions_to_execute_count, const size_t run_count, const std::function<u32()> instruction_generator) {
|
|
|
|
ThumbTestEnv test_env;
|
|
|
|
|
|
|
|
// Prepare memory.
|
|
|
|
// A Thumb-32 instruction is 32-bits so we multiply our count
|
|
|
|
test_env.code_mem.resize(instruction_count * 2 + 1);
|
2021-05-22 14:51:20 +01:00
|
|
|
test_env.code_mem.back() = 0xE7FE; // b +#0
|
2021-02-01 18:52:06 +00:00
|
|
|
|
|
|
|
// Prepare test subjects
|
|
|
|
A32Unicorn uni{test_env};
|
|
|
|
A32::Jit jit{GetUserConfig(&test_env)};
|
|
|
|
|
|
|
|
for (size_t run_number = 0; run_number < run_count; run_number++) {
|
|
|
|
ThumbTestEnv::RegisterArray initial_regs;
|
2021-05-22 14:51:20 +01:00
|
|
|
std::generate_n(initial_regs.begin(), initial_regs.size() - 1, [] { return RandInt<u32>(0, 0xFFFFFFFF); });
|
2021-02-01 18:52:06 +00:00
|
|
|
initial_regs[15] = 0;
|
|
|
|
|
|
|
|
for (size_t i = 0; i < instruction_count; i++) {
|
|
|
|
const auto instruction = instruction_generator();
|
|
|
|
const auto first_halfword = static_cast<u16>(Common::Bits<0, 15>(instruction));
|
|
|
|
const auto second_halfword = static_cast<u16>(Common::Bits<16, 31>(instruction));
|
|
|
|
|
|
|
|
test_env.code_mem[i * 2 + 0] = second_halfword;
|
|
|
|
test_env.code_mem[i * 2 + 1] = first_halfword;
|
|
|
|
}
|
|
|
|
|
|
|
|
RunInstance(run_number, test_env, uni, jit, initial_regs, instruction_count, instructions_to_execute_count);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-01 20:04:29 +00:00
|
|
|
TEST_CASE("Fuzz Thumb instructions set 1", "[JitX64][Thumb][Thumb16]") {
|
2019-04-13 10:56:55 +01:00
|
|
|
const std::array instructions = {
|
2021-05-22 14:51:20 +01:00
|
|
|
ThumbInstGen("00000xxxxxxxxxxx"), // LSL <Rd>, <Rm>, #<imm5>
|
|
|
|
ThumbInstGen("00001xxxxxxxxxxx"), // LSR <Rd>, <Rm>, #<imm5>
|
|
|
|
ThumbInstGen("00010xxxxxxxxxxx"), // ASR <Rd>, <Rm>, #<imm5>
|
|
|
|
ThumbInstGen("000110oxxxxxxxxx"), // ADD/SUB_reg
|
|
|
|
ThumbInstGen("000111oxxxxxxxxx"), // ADD/SUB_imm
|
|
|
|
ThumbInstGen("001ooxxxxxxxxxxx"), // ADD/SUB/CMP/MOV_imm
|
|
|
|
ThumbInstGen("010000ooooxxxxxx"), // Data Processing
|
|
|
|
ThumbInstGen("010001000hxxxxxx"), // ADD (high registers)
|
|
|
|
ThumbInstGen("0100010101xxxxxx", // CMP (high registers)
|
|
|
|
[](u32 inst) { return Common::Bits<3, 5>(inst) != 0b111; }), // R15 is UNPREDICTABLE
|
|
|
|
ThumbInstGen("0100010110xxxxxx", // CMP (high registers)
|
|
|
|
[](u32 inst) { return Common::Bits<0, 2>(inst) != 0b111; }), // R15 is UNPREDICTABLE
|
|
|
|
ThumbInstGen("010001100hxxxxxx"), // MOV (high registers)
|
|
|
|
ThumbInstGen("10110000oxxxxxxx"), // Adjust stack pointer
|
|
|
|
ThumbInstGen("10110010ooxxxxxx"), // SXT/UXT
|
|
|
|
ThumbInstGen("1011101000xxxxxx"), // REV
|
|
|
|
ThumbInstGen("1011101001xxxxxx"), // REV16
|
|
|
|
ThumbInstGen("1011101011xxxxxx"), // REVSH
|
|
|
|
ThumbInstGen("01001xxxxxxxxxxx"), // LDR Rd, [PC, #]
|
|
|
|
ThumbInstGen("0101oooxxxxxxxxx"), // LDR/STR Rd, [Rn, Rm]
|
|
|
|
ThumbInstGen("011xxxxxxxxxxxxx"), // LDR(B)/STR(B) Rd, [Rn, #]
|
|
|
|
ThumbInstGen("1000xxxxxxxxxxxx"), // LDRH/STRH Rd, [Rn, #offset]
|
|
|
|
ThumbInstGen("1001xxxxxxxxxxxx"), // LDR/STR Rd, [SP, #]
|
|
|
|
ThumbInstGen("1011010xxxxxxxxx", // PUSH
|
|
|
|
[](u32 inst) { return Common::Bits<0, 7>(inst) != 0; }), // Empty reg_list is UNPREDICTABLE
|
|
|
|
ThumbInstGen("10111100xxxxxxxx", // POP (P = 0)
|
|
|
|
[](u32 inst) { return Common::Bits<0, 7>(inst) != 0; }), // Empty reg_list is UNPREDICTABLE
|
|
|
|
ThumbInstGen("1100xxxxxxxxxxxx", // STMIA/LDMIA
|
2021-02-01 18:52:06 +00:00
|
|
|
[](u32 inst) {
|
2019-04-13 10:56:55 +01:00
|
|
|
// Ensure that the architecturally undefined case of
|
|
|
|
// the base register being within the list isn't hit.
|
2020-06-28 21:39:26 +01:00
|
|
|
const u32 rn = Common::Bits<8, 10>(inst);
|
|
|
|
return (inst & (1U << rn)) == 0 && Common::Bits<0, 7>(inst) != 0;
|
2019-04-13 10:56:55 +01:00
|
|
|
}),
|
2021-05-22 14:51:20 +01:00
|
|
|
// TODO: We should properly test against swapped
|
|
|
|
// endianness cases, however Unicorn doesn't
|
|
|
|
// expose the intended endianness of a load/store
|
|
|
|
// operation to memory through its hooks.
|
2019-04-13 10:56:55 +01:00
|
|
|
#if 0
|
2016-07-20 15:34:17 +01:00
|
|
|
ThumbInstGen("101101100101x000"), // SETEND
|
2019-04-13 10:56:55 +01:00
|
|
|
#endif
|
|
|
|
};
|
2016-07-07 12:01:47 +01:00
|
|
|
|
2019-04-13 10:56:55 +01:00
|
|
|
const auto instruction_select = [&]() -> u16 {
|
2021-02-01 18:52:06 +00:00
|
|
|
const auto inst_index = RandInt<size_t>(0, instructions.size() - 1);
|
2016-07-07 12:01:47 +01:00
|
|
|
|
2021-02-01 18:52:06 +00:00
|
|
|
return instructions[inst_index].Generate16();
|
2016-07-07 12:01:47 +01:00
|
|
|
};
|
|
|
|
|
2016-07-11 22:43:53 +01:00
|
|
|
SECTION("single instructions") {
|
2021-02-01 18:52:06 +00:00
|
|
|
FuzzJitThumb16(1, 2, 10000, instruction_select);
|
2016-07-11 22:43:53 +01:00
|
|
|
}
|
|
|
|
|
2016-07-07 12:01:47 +01:00
|
|
|
SECTION("short blocks") {
|
2021-02-01 18:52:06 +00:00
|
|
|
FuzzJitThumb16(5, 6, 3000, instruction_select);
|
2016-07-07 12:01:47 +01:00
|
|
|
}
|
2016-07-11 23:06:35 +01:00
|
|
|
|
2019-05-03 00:17:48 +01:00
|
|
|
// TODO: Test longer blocks when Unicorn can consistently
|
|
|
|
// run these without going into an infinite loop.
|
|
|
|
#if 0
|
2016-07-07 12:01:47 +01:00
|
|
|
SECTION("long blocks") {
|
2021-02-01 18:52:06 +00:00
|
|
|
FuzzJitThumb16(1024, 1025, 1000, instruction_select);
|
2016-07-07 12:01:47 +01:00
|
|
|
}
|
2019-05-03 00:17:48 +01:00
|
|
|
#endif
|
2016-07-07 12:01:47 +01:00
|
|
|
}
|
2016-07-12 10:58:57 +01:00
|
|
|
|
2021-02-01 20:04:29 +00:00
|
|
|
TEST_CASE("Fuzz Thumb instructions set 2 (affects PC)", "[JitX64][Thumb][Thumb16]") {
|
2019-04-13 10:56:55 +01:00
|
|
|
const std::array instructions = {
|
2021-05-22 14:51:20 +01:00
|
|
|
// TODO: We currently can't test BX/BLX as we have
|
|
|
|
// no way of preventing the unpredictable
|
|
|
|
// condition from occurring with the current interface.
|
|
|
|
// (bits zero and one within the specified register
|
|
|
|
// must not be address<1:0> == '10'.
|
2019-04-13 10:56:55 +01:00
|
|
|
#if 0
|
2016-07-18 15:11:16 +01:00
|
|
|
ThumbInstGen("01000111xmmmm000", // BLX/BX
|
2021-02-01 18:52:06 +00:00
|
|
|
[](u32 inst){
|
2020-06-28 21:39:26 +01:00
|
|
|
const u32 Rm = Common::Bits<3, 6>(inst);
|
2016-07-18 15:11:16 +01:00
|
|
|
return Rm != 15;
|
|
|
|
}),
|
2019-04-13 10:56:55 +01:00
|
|
|
#endif
|
2021-05-22 14:51:20 +01:00
|
|
|
ThumbInstGen("1010oxxxxxxxxxxx"), // add to pc/sp
|
|
|
|
ThumbInstGen("11100xxxxxxxxxxx"), // B
|
|
|
|
ThumbInstGen("01000100h0xxxxxx"), // ADD (high registers)
|
|
|
|
ThumbInstGen("01000110h0xxxxxx"), // MOV (high registers)
|
|
|
|
ThumbInstGen("1101ccccxxxxxxxx", // B<cond>
|
|
|
|
[](u32 inst) {
|
2020-06-28 21:39:26 +01:00
|
|
|
const u32 c = Common::Bits<9, 12>(inst);
|
2021-05-22 14:51:20 +01:00
|
|
|
return c < 0b1110; // Don't want SWI or undefined instructions.
|
2016-07-18 15:11:16 +01:00
|
|
|
}),
|
2021-05-22 14:51:20 +01:00
|
|
|
ThumbInstGen("1011o0i1iiiiinnn"), // CBZ/CBNZ
|
|
|
|
ThumbInstGen("10110110011x0xxx"), // CPS
|
2019-04-13 10:56:55 +01:00
|
|
|
|
2021-05-22 14:51:20 +01:00
|
|
|
// TODO: We currently have no control over the generated
|
|
|
|
// values when creating new pages, so we can't
|
|
|
|
// reliably test this yet.
|
2019-04-13 10:56:55 +01:00
|
|
|
#if 0
|
2016-07-18 20:05:35 +01:00
|
|
|
ThumbInstGen("10111101xxxxxxxx"), // POP (R = 1)
|
2019-04-13 10:56:55 +01:00
|
|
|
#endif
|
|
|
|
};
|
2016-07-12 10:58:57 +01:00
|
|
|
|
2019-04-13 10:56:55 +01:00
|
|
|
const auto instruction_select = [&]() -> u16 {
|
2021-02-01 18:52:06 +00:00
|
|
|
const auto inst_index = RandInt<size_t>(0, instructions.size() - 1);
|
|
|
|
|
|
|
|
return instructions[inst_index].Generate16();
|
|
|
|
};
|
2016-07-12 10:58:57 +01:00
|
|
|
|
2021-02-01 18:52:06 +00:00
|
|
|
FuzzJitThumb16(1, 1, 10000, instruction_select);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE("Fuzz Thumb32 instructions set", "[JitX64][Thumb][Thumb32]") {
|
2021-02-01 21:11:25 +00:00
|
|
|
const auto three_reg_not_r15 = [](u32 inst) {
|
|
|
|
const auto d = Common::Bits<8, 11>(inst);
|
|
|
|
const auto m = Common::Bits<0, 3>(inst);
|
|
|
|
const auto n = Common::Bits<16, 19>(inst);
|
|
|
|
return d != 15 && m != 15 && n != 15;
|
|
|
|
};
|
|
|
|
|
2021-02-01 18:52:06 +00:00
|
|
|
const std::array instructions = {
|
2021-05-22 14:51:20 +01:00
|
|
|
ThumbInstGen("111110101011nnnn1111dddd1000mmmm", // CLZ
|
2021-02-01 18:52:06 +00:00
|
|
|
[](u32 inst) {
|
|
|
|
const auto d = Common::Bits<8, 11>(inst);
|
|
|
|
const auto m = Common::Bits<0, 3>(inst);
|
|
|
|
const auto n = Common::Bits<16, 19>(inst);
|
|
|
|
return m == n && d != 15 && m != 15;
|
|
|
|
}),
|
2021-05-22 14:51:20 +01:00
|
|
|
ThumbInstGen("111110101000nnnn1111dddd1000mmmm", // QADD
|
2021-02-01 21:11:25 +00:00
|
|
|
three_reg_not_r15),
|
2021-05-22 14:51:20 +01:00
|
|
|
ThumbInstGen("111110101000nnnn1111dddd0001mmmm", // QADD8
|
2021-02-01 22:08:00 +00:00
|
|
|
three_reg_not_r15),
|
2021-05-22 14:51:20 +01:00
|
|
|
ThumbInstGen("111110101001nnnn1111dddd0001mmmm", // QADD16
|
2021-02-01 21:52:18 +00:00
|
|
|
three_reg_not_r15),
|
2021-05-22 14:51:20 +01:00
|
|
|
ThumbInstGen("111110101010nnnn1111dddd0001mmmm", // QASX
|
2021-02-01 21:57:48 +00:00
|
|
|
three_reg_not_r15),
|
2021-05-22 14:51:20 +01:00
|
|
|
ThumbInstGen("111110101000nnnn1111dddd1001mmmm", // QDADD
|
2021-02-01 21:11:25 +00:00
|
|
|
three_reg_not_r15),
|
2021-05-22 14:51:20 +01:00
|
|
|
ThumbInstGen("111110101000nnnn1111dddd1011mmmm", // QDSUB
|
2021-02-01 21:11:25 +00:00
|
|
|
three_reg_not_r15),
|
2021-05-22 14:51:20 +01:00
|
|
|
ThumbInstGen("111110101110nnnn1111dddd0001mmmm", // QSAX
|
2021-02-01 22:01:29 +00:00
|
|
|
three_reg_not_r15),
|
2021-05-22 14:51:20 +01:00
|
|
|
ThumbInstGen("111110101000nnnn1111dddd1010mmmm", // QSUB
|
2021-02-01 21:11:25 +00:00
|
|
|
three_reg_not_r15),
|
2021-05-22 14:51:20 +01:00
|
|
|
ThumbInstGen("111110101100nnnn1111dddd0001mmmm", // QSUB8
|
2021-02-01 22:10:17 +00:00
|
|
|
three_reg_not_r15),
|
2021-05-22 14:51:20 +01:00
|
|
|
ThumbInstGen("111110101101nnnn1111dddd0001mmmm", // QSUB16
|
2021-02-01 22:05:17 +00:00
|
|
|
three_reg_not_r15),
|
2021-05-22 14:51:20 +01:00
|
|
|
ThumbInstGen("111110101001nnnn1111dddd1010mmmm", // RBIT
|
2021-02-01 20:20:24 +00:00
|
|
|
[](u32 inst) {
|
|
|
|
const auto d = Common::Bits<8, 11>(inst);
|
|
|
|
const auto m = Common::Bits<0, 3>(inst);
|
|
|
|
const auto n = Common::Bits<16, 19>(inst);
|
|
|
|
return m == n && d != 15 && m != 15;
|
|
|
|
}),
|
2021-05-22 14:51:20 +01:00
|
|
|
ThumbInstGen("111110101001nnnn1111dddd1000mmmm", // REV
|
2021-02-01 20:29:52 +00:00
|
|
|
[](u32 inst) {
|
|
|
|
const auto d = Common::Bits<8, 11>(inst);
|
|
|
|
const auto m = Common::Bits<0, 3>(inst);
|
|
|
|
const auto n = Common::Bits<16, 19>(inst);
|
|
|
|
return m == n && d != 15 && m != 15;
|
|
|
|
}),
|
2021-05-22 14:51:20 +01:00
|
|
|
ThumbInstGen("111110101001nnnn1111dddd1001mmmm", // REV16
|
2021-02-01 20:24:12 +00:00
|
|
|
[](u32 inst) {
|
|
|
|
const auto d = Common::Bits<8, 11>(inst);
|
|
|
|
const auto m = Common::Bits<0, 3>(inst);
|
|
|
|
const auto n = Common::Bits<16, 19>(inst);
|
|
|
|
return m == n && d != 15 && m != 15;
|
|
|
|
}),
|
2021-05-22 14:51:20 +01:00
|
|
|
ThumbInstGen("111110101001nnnn1111dddd1011mmmm", // REVSH
|
2021-02-01 20:10:08 +00:00
|
|
|
[](u32 inst) {
|
|
|
|
const auto d = Common::Bits<8, 11>(inst);
|
|
|
|
const auto m = Common::Bits<0, 3>(inst);
|
|
|
|
const auto n = Common::Bits<16, 19>(inst);
|
|
|
|
return m == n && d != 15 && m != 15;
|
|
|
|
}),
|
2021-05-22 14:51:20 +01:00
|
|
|
ThumbInstGen("111110101000nnnn1111dddd0000mmmm", // SADD8
|
2021-02-01 21:43:35 +00:00
|
|
|
three_reg_not_r15),
|
2021-05-22 14:51:20 +01:00
|
|
|
ThumbInstGen("111110101001nnnn1111dddd0000mmmm", // SADD16
|
2021-02-01 21:11:25 +00:00
|
|
|
three_reg_not_r15),
|
2021-05-22 14:51:20 +01:00
|
|
|
ThumbInstGen("111110101010nnnn1111dddd0000mmmm", // SASX
|
2021-02-01 21:30:08 +00:00
|
|
|
three_reg_not_r15),
|
2021-05-22 14:51:20 +01:00
|
|
|
ThumbInstGen("111110101010nnnn1111dddd1000mmmm", // SEL
|
2021-02-01 21:11:25 +00:00
|
|
|
three_reg_not_r15),
|
2021-05-22 14:51:20 +01:00
|
|
|
ThumbInstGen("111110101000nnnn1111dddd0010mmmm", // SHADD8
|
2021-02-01 22:23:40 +00:00
|
|
|
three_reg_not_r15),
|
2021-05-22 14:51:20 +01:00
|
|
|
ThumbInstGen("111110101001nnnn1111dddd0010mmmm", // SHADD16
|
2021-02-01 22:13:44 +00:00
|
|
|
three_reg_not_r15),
|
2021-05-22 14:51:20 +01:00
|
|
|
ThumbInstGen("111110101010nnnn1111dddd0010mmmm", // SHASX
|
2021-02-01 22:16:28 +00:00
|
|
|
three_reg_not_r15),
|
2021-05-22 14:51:20 +01:00
|
|
|
ThumbInstGen("111110101110nnnn1111dddd0010mmmm", // SHSAX
|
2021-02-01 22:19:04 +00:00
|
|
|
three_reg_not_r15),
|
2021-05-22 14:51:20 +01:00
|
|
|
ThumbInstGen("111110101100nnnn1111dddd0010mmmm", // SHSUB8
|
2021-02-01 22:25:56 +00:00
|
|
|
three_reg_not_r15),
|
2021-05-22 14:51:20 +01:00
|
|
|
ThumbInstGen("111110101101nnnn1111dddd0010mmmm", // SHSUB16
|
2021-02-01 22:21:24 +00:00
|
|
|
three_reg_not_r15),
|
2021-05-22 14:51:20 +01:00
|
|
|
ThumbInstGen("111110101110nnnn1111dddd0000mmmm", // SSAX
|
2021-02-01 21:36:18 +00:00
|
|
|
three_reg_not_r15),
|
2021-05-22 14:51:20 +01:00
|
|
|
ThumbInstGen("111110101100nnnn1111dddd0000mmmm", // SSUB8
|
2021-02-01 21:47:12 +00:00
|
|
|
three_reg_not_r15),
|
2021-05-22 14:51:20 +01:00
|
|
|
ThumbInstGen("111110101101nnnn1111dddd0000mmmm", // SSUB16
|
2021-02-01 21:41:02 +00:00
|
|
|
three_reg_not_r15),
|
2021-05-22 14:51:20 +01:00
|
|
|
ThumbInstGen("111110101000nnnn1111dddd0100mmmm", // UADD8
|
2021-02-01 21:43:35 +00:00
|
|
|
three_reg_not_r15),
|
2021-05-22 14:51:20 +01:00
|
|
|
ThumbInstGen("111110101001nnnn1111dddd0100mmmm", // UADD16
|
2021-02-01 21:11:25 +00:00
|
|
|
three_reg_not_r15),
|
2021-05-22 14:51:20 +01:00
|
|
|
ThumbInstGen("111110101010nnnn1111dddd0100mmmm", // UASX
|
2021-02-01 21:30:08 +00:00
|
|
|
three_reg_not_r15),
|
2021-05-22 14:51:20 +01:00
|
|
|
ThumbInstGen("111110101000nnnn1111dddd0110mmmm", // UHADD8
|
2021-02-01 22:23:40 +00:00
|
|
|
three_reg_not_r15),
|
2021-05-22 14:51:20 +01:00
|
|
|
ThumbInstGen("111110101001nnnn1111dddd0110mmmm", // UHADD16
|
2021-02-01 22:13:44 +00:00
|
|
|
three_reg_not_r15),
|
2021-05-22 14:51:20 +01:00
|
|
|
ThumbInstGen("111110101010nnnn1111dddd0110mmmm", // UHASX
|
2021-02-01 22:16:28 +00:00
|
|
|
three_reg_not_r15),
|
2021-05-22 14:51:20 +01:00
|
|
|
ThumbInstGen("111110101110nnnn1111dddd0110mmmm", // UHSAX
|
2021-02-01 22:19:04 +00:00
|
|
|
three_reg_not_r15),
|
2021-05-22 14:51:20 +01:00
|
|
|
ThumbInstGen("111110101100nnnn1111dddd0110mmmm", // UHSUB8
|
2021-02-01 22:25:56 +00:00
|
|
|
three_reg_not_r15),
|
2021-05-22 14:51:20 +01:00
|
|
|
ThumbInstGen("111110101101nnnn1111dddd0110mmmm", // UHSUB16
|
2021-02-01 22:21:24 +00:00
|
|
|
three_reg_not_r15),
|
2021-05-22 14:51:20 +01:00
|
|
|
ThumbInstGen("111110101000nnnn1111dddd0101mmmm", // UQADD8
|
2021-02-01 22:08:00 +00:00
|
|
|
three_reg_not_r15),
|
2021-05-22 14:51:20 +01:00
|
|
|
ThumbInstGen("111110101001nnnn1111dddd0101mmmm", // UQADD16
|
2021-02-01 21:52:18 +00:00
|
|
|
three_reg_not_r15),
|
2021-05-22 14:51:20 +01:00
|
|
|
ThumbInstGen("111110101010nnnn1111dddd0101mmmm", // UQASX
|
2021-02-01 21:57:48 +00:00
|
|
|
three_reg_not_r15),
|
2021-05-22 14:51:20 +01:00
|
|
|
ThumbInstGen("111110101110nnnn1111dddd0101mmmm", // UQSAX
|
2021-02-01 22:01:29 +00:00
|
|
|
three_reg_not_r15),
|
2021-05-22 14:51:20 +01:00
|
|
|
ThumbInstGen("111110101100nnnn1111dddd0101mmmm", // UQSUB8
|
2021-02-01 22:10:17 +00:00
|
|
|
three_reg_not_r15),
|
2021-05-22 14:51:20 +01:00
|
|
|
ThumbInstGen("111110101101nnnn1111dddd0101mmmm", // UQSUB16
|
2021-02-01 22:05:17 +00:00
|
|
|
three_reg_not_r15),
|
2021-05-22 14:51:20 +01:00
|
|
|
ThumbInstGen("111110101110nnnn1111dddd0100mmmm", // USAX
|
2021-02-01 21:36:18 +00:00
|
|
|
three_reg_not_r15),
|
2021-05-22 14:51:20 +01:00
|
|
|
ThumbInstGen("111110101100nnnn1111dddd0100mmmm", // USUB8
|
2021-02-01 21:47:12 +00:00
|
|
|
three_reg_not_r15),
|
2021-05-22 14:51:20 +01:00
|
|
|
ThumbInstGen("111110101101nnnn1111dddd0100mmmm", // USUB16
|
2021-02-01 21:41:02 +00:00
|
|
|
three_reg_not_r15),
|
2021-02-01 18:52:06 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const auto instruction_select = [&]() -> u32 {
|
|
|
|
const auto inst_index = RandInt<size_t>(0, instructions.size() - 1);
|
|
|
|
|
|
|
|
return instructions[inst_index].Generate32();
|
2016-07-12 10:58:57 +01:00
|
|
|
};
|
|
|
|
|
2021-02-01 18:52:06 +00:00
|
|
|
SECTION("single instructions") {
|
|
|
|
FuzzJitThumb32(1, 2, 10000, instruction_select);
|
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("short blocks") {
|
|
|
|
FuzzJitThumb32(5, 6, 3000, instruction_select);
|
|
|
|
}
|
2016-07-12 10:58:57 +01:00
|
|
|
}
|
2018-01-14 20:21:47 +00:00
|
|
|
|
2021-02-01 20:04:29 +00:00
|
|
|
TEST_CASE("Verify fix for off by one error in MemoryRead32 worked", "[Thumb][Thumb16]") {
|
2018-01-27 22:36:55 +00:00
|
|
|
ThumbTestEnv test_env;
|
|
|
|
|
2018-01-14 20:21:47 +00:00
|
|
|
// Prepare test subjects
|
2019-04-13 10:56:55 +01:00
|
|
|
A32Unicorn<ThumbTestEnv> uni{test_env};
|
2020-06-28 21:39:26 +01:00
|
|
|
A32::Jit jit{GetUserConfig(&test_env)};
|
2018-01-14 20:21:47 +00:00
|
|
|
|
2021-05-22 14:51:20 +01:00
|
|
|
constexpr ThumbTestEnv::RegisterArray initial_regs{
|
2018-01-14 20:21:47 +00:00
|
|
|
0xe90ecd70,
|
|
|
|
0x3e3b73c3,
|
|
|
|
0x571616f9,
|
|
|
|
0x0b1ef45a,
|
|
|
|
0xb3a829f2,
|
|
|
|
0x915a7a6a,
|
|
|
|
0x579c38f4,
|
|
|
|
0xd9ffe391,
|
|
|
|
0x55b6682b,
|
|
|
|
0x458d8f37,
|
|
|
|
0x8f3eb3dc,
|
|
|
|
0xe18c0e7d,
|
|
|
|
0x6752657a,
|
|
|
|
0x00001766,
|
|
|
|
0xdbbf23e3,
|
|
|
|
0x00000000,
|
|
|
|
};
|
|
|
|
|
2019-03-02 21:18:42 +00:00
|
|
|
test_env.code_mem = {
|
2021-05-22 14:51:20 +01:00
|
|
|
0x40B8, // lsls r0, r7, #0
|
|
|
|
0x01CA, // lsls r2, r1, #7
|
|
|
|
0x83A1, // strh r1, [r4, #28]
|
|
|
|
0x708A, // strb r2, [r1, #2]
|
|
|
|
0xBCC4, // pop {r2, r6, r7}
|
|
|
|
0xE7FE, // b +#0
|
2019-03-02 21:18:42 +00:00
|
|
|
};
|
2018-01-14 20:21:47 +00:00
|
|
|
|
2019-04-13 10:56:55 +01:00
|
|
|
RunInstance(1, test_env, uni, jit, initial_regs, 5, 5);
|
2018-01-14 20:21:47 +00:00
|
|
|
}
|