2016-07-01 15:01:06 +02:00
|
|
|
/* This file is part of the dynarmic project.
|
|
|
|
* Copyright (c) 2016 MerryMage
|
|
|
|
* This software may be used and distributed according to the terms of the GNU
|
|
|
|
* General Public License version 2 or any later version.
|
|
|
|
*/
|
|
|
|
|
2016-08-17 16:53:36 +02:00
|
|
|
#include <algorithm>
|
|
|
|
#include <array>
|
2016-07-07 15:51:47 +02:00
|
|
|
#include <cinttypes>
|
2016-08-17 16:53:36 +02:00
|
|
|
#include <cstdio>
|
2016-07-07 13:01:47 +02:00
|
|
|
#include <cstring>
|
2016-07-12 14:25:33 +02:00
|
|
|
#include <functional>
|
2016-08-17 16:53:36 +02:00
|
|
|
#include <tuple>
|
2016-07-07 13:01:47 +02:00
|
|
|
|
2016-07-07 11:53:09 +02:00
|
|
|
#include <catch.hpp>
|
|
|
|
|
2018-01-04 22:12:02 +01:00
|
|
|
#include <dynarmic/A32/a32.h>
|
2016-08-25 19:22:08 +02:00
|
|
|
|
2016-07-12 14:25:33 +02:00
|
|
|
#include "common/bit_util.h"
|
2016-07-07 11:53:09 +02:00
|
|
|
#include "common/common_types.h"
|
2018-01-01 16:23:56 +01:00
|
|
|
#include "frontend/A32/disassembler/disassembler.h"
|
|
|
|
#include "frontend/A32/FPSCR.h"
|
|
|
|
#include "frontend/A32/location_descriptor.h"
|
|
|
|
#include "frontend/A32/PSR.h"
|
|
|
|
#include "frontend/A32/translate/translate.h"
|
2016-08-17 16:53:36 +02:00
|
|
|
#include "frontend/ir/basic_block.h"
|
|
|
|
#include "ir_opt/passes.h"
|
2016-07-07 13:01:47 +02:00
|
|
|
#include "rand_int.h"
|
2018-01-27 23:36:55 +01:00
|
|
|
#include "testenv.h"
|
2018-01-06 22:15:25 +01:00
|
|
|
#include "A32/skyeye_interpreter/dyncom/arm_dyncom_interpreter.h"
|
|
|
|
#include "A32/skyeye_interpreter/skyeye_common/armstate.h"
|
2016-07-07 11:53:09 +02:00
|
|
|
|
2018-01-27 23:36:55 +01:00
|
|
|
static Dynarmic::A32::UserConfig GetUserConfig(ThumbTestEnv* testenv) {
|
|
|
|
Dynarmic::A32::UserConfig user_config;
|
|
|
|
user_config.callbacks = testenv;
|
|
|
|
return user_config;
|
2016-07-07 13:01:47 +02:00
|
|
|
}
|
|
|
|
|
2018-01-27 23:36:55 +01:00
|
|
|
using WriteRecords = std::map<u32, u8>;
|
2016-07-04 11:22:11 +02:00
|
|
|
|
2016-07-18 16:11:16 +02:00
|
|
|
struct ThumbInstGen final {
|
2016-07-10 06:10:13 +02:00
|
|
|
public:
|
2016-07-18 16:11:16 +02:00
|
|
|
ThumbInstGen(const char* format, std::function<bool(u16)> is_valid = [](u16){ return true; }) : is_valid(is_valid) {
|
2016-07-10 06:10:13 +02:00
|
|
|
REQUIRE(strlen(format) == 16);
|
|
|
|
|
|
|
|
for (int i = 0; i < 16; i++) {
|
|
|
|
const u16 bit = 1 << (15 - i);
|
|
|
|
switch (format[i]) {
|
2016-08-23 00:40:30 +02:00
|
|
|
case '0':
|
|
|
|
mask |= bit;
|
|
|
|
break;
|
|
|
|
case '1':
|
|
|
|
bits |= bit;
|
|
|
|
mask |= bit;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
// Do nothing
|
|
|
|
break;
|
2016-07-10 06:10:13 +02:00
|
|
|
}
|
2016-07-07 13:01:47 +02:00
|
|
|
}
|
|
|
|
}
|
2016-07-10 06:10:13 +02:00
|
|
|
u16 Generate() const {
|
|
|
|
u16 inst;
|
2016-07-18 16:11:16 +02:00
|
|
|
|
2016-07-10 06:10:13 +02:00
|
|
|
do {
|
|
|
|
u16 random = RandInt<u16>(0, 0xFFFF);
|
|
|
|
inst = bits | (random & ~mask);
|
|
|
|
} while (!is_valid(inst));
|
2016-07-18 16:11:16 +02:00
|
|
|
|
|
|
|
ASSERT((inst & mask) == bits);
|
|
|
|
|
2016-07-10 06:10:13 +02:00
|
|
|
return inst;
|
|
|
|
}
|
|
|
|
private:
|
|
|
|
u16 bits = 0;
|
|
|
|
u16 mask = 0;
|
|
|
|
std::function<bool(u16)> is_valid;
|
|
|
|
};
|
2016-07-07 13:01:47 +02:00
|
|
|
|
2018-01-27 23:36:55 +01:00
|
|
|
static bool DoesBehaviorMatch(const ARMul_State& interp, const Dynarmic::A32::Jit& jit, WriteRecords& interp_write_records, WriteRecords& jit_write_records) {
|
2016-07-07 13:01:47 +02:00
|
|
|
const auto interp_regs = interp.Reg;
|
|
|
|
const auto jit_regs = jit.Regs();
|
|
|
|
|
|
|
|
return std::equal(interp_regs.begin(), interp_regs.end(), jit_regs.begin(), jit_regs.end())
|
2016-07-10 07:29:15 +02:00
|
|
|
&& interp.Cpsr == jit.Cpsr()
|
|
|
|
&& interp_write_records == jit_write_records;
|
2016-07-07 13:01:47 +02:00
|
|
|
}
|
|
|
|
|
2018-01-27 23:36:55 +01:00
|
|
|
static void RunInstance(size_t run_number, ThumbTestEnv& test_env, ARMul_State& interp, Dynarmic::A32::Jit& jit, const std::array<u32, 16>& initial_regs, size_t instruction_count, size_t instructions_to_execute_count) {
|
2018-01-14 21:21:47 +01:00
|
|
|
interp.instruction_cache.clear();
|
|
|
|
InterpreterClearCache();
|
|
|
|
jit.ClearCache();
|
|
|
|
|
|
|
|
// Setup initial state
|
|
|
|
|
|
|
|
interp.Cpsr = 0x000001F0;
|
|
|
|
interp.Reg = initial_regs;
|
|
|
|
jit.SetCpsr(0x000001F0);
|
|
|
|
jit.Regs() = initial_regs;
|
|
|
|
|
|
|
|
// Run interpreter
|
2018-01-27 23:36:55 +01:00
|
|
|
test_env.modified_memory.clear();
|
2018-01-14 21:21:47 +01:00
|
|
|
interp.NumInstrsToExecute = static_cast<unsigned>(instructions_to_execute_count);
|
|
|
|
InterpreterMainLoop(&interp);
|
2018-01-27 23:36:55 +01:00
|
|
|
auto interp_write_records = test_env.modified_memory;
|
2018-01-14 21:21:47 +01:00
|
|
|
{
|
|
|
|
bool T = Dynarmic::Common::Bit<5>(interp.Cpsr);
|
|
|
|
interp.Reg[15] &= T ? 0xFFFFFFFE : 0xFFFFFFFC;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run jit
|
2018-01-27 23:36:55 +01:00
|
|
|
test_env.modified_memory.clear();
|
|
|
|
test_env.ticks_left = instructions_to_execute_count;
|
2018-01-14 21:21:47 +01:00
|
|
|
jit.Run();
|
2018-01-27 23:36:55 +01:00
|
|
|
auto jit_write_records = test_env.modified_memory;
|
2018-01-14 21:21:47 +01:00
|
|
|
|
|
|
|
// Compare
|
|
|
|
if (!DoesBehaviorMatch(interp, jit, interp_write_records, jit_write_records)) {
|
|
|
|
printf("Failed at execution number %zu\n", run_number);
|
|
|
|
|
|
|
|
printf("\nInstruction Listing: \n");
|
|
|
|
for (size_t i = 0; i < instruction_count; i++) {
|
2018-01-27 23:36:55 +01:00
|
|
|
printf("%04x %s\n", test_env.code_mem[i], Dynarmic::A32::DisassembleThumb16(test_env.code_mem[i]).c_str());
|
2018-01-14 21:21:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
printf("\nInitial Register Listing: \n");
|
|
|
|
for (int i = 0; i <= 15; i++) {
|
|
|
|
printf("%4i: %08x\n", i, initial_regs[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("\nFinal Register Listing: \n");
|
|
|
|
printf(" interp jit\n");
|
|
|
|
for (int i = 0; i <= 15; i++) {
|
|
|
|
printf("%4i: %08x %08x %s\n", i, interp.Reg[i], jit.Regs()[i], interp.Reg[i] != jit.Regs()[i] ? "*" : "");
|
|
|
|
}
|
|
|
|
printf("CPSR: %08x %08x %s\n", interp.Cpsr, jit.Cpsr(), interp.Cpsr != jit.Cpsr() ? "*" : "");
|
|
|
|
|
|
|
|
printf("\nInterp Write Records:\n");
|
|
|
|
for (auto& record : interp_write_records) {
|
2018-01-27 23:36:55 +01:00
|
|
|
printf("[%08x] = %02x\n", record.first, record.second);
|
2018-01-14 21:21:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
printf("\nJIT Write Records:\n");
|
|
|
|
for (auto& record : jit_write_records) {
|
2018-01-27 23:36:55 +01:00
|
|
|
printf("[%08x] = %02x\n", record.first, record.second);
|
2018-01-14 21:21:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Dynarmic::A32::PSR cpsr;
|
|
|
|
cpsr.T(true);
|
|
|
|
|
|
|
|
size_t num_insts = 0;
|
|
|
|
while (num_insts < instructions_to_execute_count) {
|
|
|
|
Dynarmic::A32::LocationDescriptor descriptor = {u32(num_insts * 4), cpsr, Dynarmic::A32::FPSCR{}};
|
2018-01-27 23:36:55 +01:00
|
|
|
Dynarmic::IR::Block ir_block = Dynarmic::A32::Translate(descriptor, [&test_env](u32 vaddr) { return test_env.MemoryReadCode(vaddr); });
|
2018-01-14 21:21:47 +01:00
|
|
|
Dynarmic::Optimization::A32GetSetElimination(ir_block);
|
|
|
|
Dynarmic::Optimization::DeadCodeElimination(ir_block);
|
2018-01-27 23:36:55 +01:00
|
|
|
Dynarmic::Optimization::A32ConstantMemoryReads(ir_block, &test_env);
|
2018-01-14 21:21:47 +01:00
|
|
|
Dynarmic::Optimization::ConstantPropagation(ir_block);
|
|
|
|
Dynarmic::Optimization::DeadCodeElimination(ir_block);
|
|
|
|
Dynarmic::Optimization::VerificationPass(ir_block);
|
|
|
|
printf("\n\nIR:\n%s", Dynarmic::IR::DumpBlock(ir_block).c_str());
|
|
|
|
printf("\n\nx86_64:\n%s", jit.Disassemble(descriptor).c_str());
|
|
|
|
num_insts += ir_block.CycleCount();
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef _MSC_VER
|
|
|
|
__debugbreak();
|
|
|
|
#endif
|
|
|
|
FAIL();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-07 13:01:47 +02:00
|
|
|
void FuzzJitThumb(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 23:36:55 +01:00
|
|
|
ThumbTestEnv test_env;
|
|
|
|
|
2016-07-07 13:01:47 +02:00
|
|
|
// Prepare memory
|
2018-01-27 23:36:55 +01:00
|
|
|
test_env.code_mem.fill(0xE7FE); // b +#0
|
2016-07-07 13:01:47 +02:00
|
|
|
|
|
|
|
// Prepare test subjects
|
|
|
|
ARMul_State interp{USER32MODE};
|
2018-01-27 23:36:55 +01:00
|
|
|
interp.user_callbacks = &test_env;
|
|
|
|
Dynarmic::A32::Jit jit{GetUserConfig(&test_env)};
|
2016-07-07 13:01:47 +02:00
|
|
|
|
|
|
|
for (size_t run_number = 0; run_number < run_count; run_number++) {
|
|
|
|
std::array<u32, 16> initial_regs;
|
|
|
|
std::generate_n(initial_regs.begin(), 15, []{ return RandInt<u32>(0, 0xFFFFFFFF); });
|
|
|
|
initial_regs[15] = 0;
|
|
|
|
|
2018-01-27 23:36:55 +01:00
|
|
|
std::generate_n(test_env.code_mem.begin(), instruction_count, instruction_generator);
|
2016-07-07 13:01:47 +02:00
|
|
|
|
2018-01-27 23:36:55 +01:00
|
|
|
RunInstance(run_number, test_env, interp, jit, initial_regs, instruction_count, instructions_to_execute_count);
|
2016-07-07 13:01:47 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE("Fuzz Thumb instructions set 1", "[JitX64][Thumb]") {
|
2016-07-20 16:34:17 +02:00
|
|
|
const std::array<ThumbInstGen, 25> instructions = {{
|
2016-07-18 16:11:16 +02: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)
|
|
|
|
[](u16 inst){ return Dynarmic::Common::Bits<3, 5>(inst) != 0b111; }), // R15 is UNPREDICTABLE
|
|
|
|
ThumbInstGen("0100010110xxxxxx", // CMP (high registers)
|
|
|
|
[](u16 inst){ return Dynarmic::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, #]
|
2016-07-18 21:05:35 +02:00
|
|
|
ThumbInstGen("1011010xxxxxxxxx", // PUSH
|
2016-07-18 16:11:16 +02:00
|
|
|
[](u16 inst){ return Dynarmic::Common::Bits<0, 7>(inst) != 0; }), // Empty reg_list is UNPREDICTABLE
|
2016-07-18 21:05:35 +02:00
|
|
|
ThumbInstGen("10111100xxxxxxxx", // POP (P = 0)
|
2016-07-18 18:37:48 +02:00
|
|
|
[](u16 inst){ return Dynarmic::Common::Bits<0, 7>(inst) != 0; }), // Empty reg_list is UNPREDICTABLE
|
2016-07-18 16:11:16 +02:00
|
|
|
ThumbInstGen("1100xxxxxxxxxxxx"), // STMIA/LDMIA
|
2016-07-20 16:34:17 +02:00
|
|
|
ThumbInstGen("101101100101x000"), // SETEND
|
2016-07-07 13:01:47 +02:00
|
|
|
}};
|
|
|
|
|
|
|
|
auto instruction_select = [&]() -> u16 {
|
|
|
|
size_t inst_index = RandInt<size_t>(0, instructions.size() - 1);
|
|
|
|
|
2016-07-10 06:10:13 +02:00
|
|
|
return instructions[inst_index].Generate();
|
2016-07-07 13:01:47 +02:00
|
|
|
};
|
|
|
|
|
2016-07-11 23:43:53 +02:00
|
|
|
SECTION("single instructions") {
|
|
|
|
FuzzJitThumb(1, 2, 10000, instruction_select);
|
|
|
|
}
|
|
|
|
|
2016-07-07 13:01:47 +02:00
|
|
|
SECTION("short blocks") {
|
2016-07-10 07:29:15 +02:00
|
|
|
FuzzJitThumb(5, 6, 3000, instruction_select);
|
2016-07-07 13:01:47 +02:00
|
|
|
}
|
2016-07-12 00:06:35 +02:00
|
|
|
|
2016-07-07 13:01:47 +02:00
|
|
|
SECTION("long blocks") {
|
2016-08-26 16:23:38 +02:00
|
|
|
FuzzJitThumb(1024, 1025, 1000, instruction_select);
|
2016-07-07 13:01:47 +02:00
|
|
|
}
|
|
|
|
}
|
2016-07-12 11:58:57 +02:00
|
|
|
|
|
|
|
TEST_CASE("Fuzz Thumb instructions set 2 (affects PC)", "[JitX64][Thumb]") {
|
2016-07-18 21:05:35 +02:00
|
|
|
const std::array<ThumbInstGen, 8> instructions = {{
|
2016-07-18 16:11:16 +02:00
|
|
|
ThumbInstGen("01000111xmmmm000", // BLX/BX
|
|
|
|
[](u16 inst){
|
|
|
|
u32 Rm = Dynarmic::Common::Bits<3, 6>(inst);
|
|
|
|
return Rm != 15;
|
|
|
|
}),
|
|
|
|
ThumbInstGen("1010oxxxxxxxxxxx"), // add to pc/sp
|
|
|
|
ThumbInstGen("11100xxxxxxxxxxx"), // B
|
|
|
|
ThumbInstGen("01000100h0xxxxxx"), // ADD (high registers)
|
|
|
|
ThumbInstGen("01000110h0xxxxxx"), // MOV (high registers)
|
|
|
|
ThumbInstGen("1101ccccxxxxxxxx", // B<cond>
|
|
|
|
[](u16 inst){
|
|
|
|
u32 c = Dynarmic::Common::Bits<9, 12>(inst);
|
|
|
|
return c < 0b1110; // Don't want SWI or undefined instructions.
|
|
|
|
}),
|
|
|
|
ThumbInstGen("10110110011x0xxx"), // CPS
|
2016-07-18 21:05:35 +02:00
|
|
|
ThumbInstGen("10111101xxxxxxxx"), // POP (R = 1)
|
2016-07-12 11:58:57 +02:00
|
|
|
}};
|
|
|
|
|
|
|
|
auto instruction_select = [&]() -> u16 {
|
|
|
|
size_t inst_index = RandInt<size_t>(0, instructions.size() - 1);
|
|
|
|
|
|
|
|
return instructions[inst_index].Generate();
|
|
|
|
};
|
|
|
|
|
|
|
|
FuzzJitThumb(1, 1, 10000, instruction_select);
|
|
|
|
}
|
2018-01-14 21:21:47 +01:00
|
|
|
|
|
|
|
TEST_CASE("Verify fix for off by one error in MemoryRead32 worked", "[Thumb]") {
|
2018-01-27 23:36:55 +01:00
|
|
|
ThumbTestEnv test_env;
|
|
|
|
|
2018-01-14 21:21:47 +01:00
|
|
|
// Prepare memory
|
2018-01-27 23:36:55 +01:00
|
|
|
test_env.code_mem.fill(0xE7FE); // b +#0
|
2018-01-14 21:21:47 +01:00
|
|
|
|
|
|
|
// Prepare test subjects
|
|
|
|
ARMul_State interp{USER32MODE};
|
2018-01-27 23:36:55 +01:00
|
|
|
interp.user_callbacks = &test_env;
|
|
|
|
Dynarmic::A32::Jit jit{GetUserConfig(&test_env)};
|
2018-01-14 21:21:47 +01:00
|
|
|
|
|
|
|
std::array<u32, 16> initial_regs {
|
|
|
|
0xe90ecd70,
|
|
|
|
0x3e3b73c3,
|
|
|
|
0x571616f9,
|
|
|
|
0x0b1ef45a,
|
|
|
|
0xb3a829f2,
|
|
|
|
0x915a7a6a,
|
|
|
|
0x579c38f4,
|
|
|
|
0xd9ffe391,
|
|
|
|
0x55b6682b,
|
|
|
|
0x458d8f37,
|
|
|
|
0x8f3eb3dc,
|
|
|
|
0xe18c0e7d,
|
|
|
|
0x6752657a,
|
|
|
|
0x00001766,
|
|
|
|
0xdbbf23e3,
|
|
|
|
0x00000000,
|
|
|
|
};
|
|
|
|
|
2018-01-27 23:36:55 +01:00
|
|
|
test_env.code_mem[0] = 0x40B8; // lsls r0, r7, #0
|
|
|
|
test_env.code_mem[1] = 0x01CA; // lsls r2, r1, #7
|
|
|
|
test_env.code_mem[2] = 0x83A1; // strh r1, [r4, #28]
|
|
|
|
test_env.code_mem[3] = 0x708A; // strb r2, [r1, #2]
|
|
|
|
test_env.code_mem[4] = 0xBCC4; // pop {r2, r6, r7}
|
2018-01-14 21:21:47 +01:00
|
|
|
|
2018-01-27 23:36:55 +01:00
|
|
|
RunInstance(1, test_env, interp, jit, initial_regs, 5, 5);
|
2018-01-14 21:21:47 +01:00
|
|
|
}
|