dynarmic/tests/A64/fuzz_with_unicorn.cpp

113 lines
3.9 KiB
C++
Raw Normal View History

2018-01-13 18:04:19 +00:00
/* This file is part of the dynarmic project.
* Copyright (c) 2018 MerryMage
* This software may be used and distributed according to the terms of the GNU
* General Public License version 2 or any later version.
*/
#include <cstring>
#include <catch.hpp>
#include "frontend/A64/location_descriptor.h"
#include "frontend/A64/translate/translate.h"
#include "frontend/ir/basic_block.h"
#include "inst_gen.h"
#include "rand_int.h"
#include "testenv.h"
#include "unicorn_emu/unicorn.h"
using namespace Dynarmic;
2018-01-21 17:55:26 +00:00
static Vector RandomVector() {
return {RandInt<u64>(0, ~u64(0)), RandInt<u64>(0, ~u64(0))};
}
2018-01-13 18:04:19 +00:00
static std::vector<InstructionGenerator> instruction_generators = []{
const std::vector<std::tuple<const char*, const char*>> list {
#define INST(fn, name, bitstring) {#fn, bitstring},
#include "frontend/A64/decoder/a64.inc"
#undef INST
};
std::vector<InstructionGenerator> result;
for (const auto& [fn, bitstring] : list) {
if (std::strcmp(fn, "UnallocatedEncoding") == 0) {
InstructionGenerator::AddInvalidInstruction(bitstring);
continue;
}
result.emplace_back(InstructionGenerator{bitstring});
}
return result;
}();
static u32 GenRandomInst(u64 pc, bool is_last_inst) {
2018-01-13 18:04:19 +00:00
const A64::LocationDescriptor location{pc, {}};
restart:
const size_t index = RandInt<size_t>(0, instruction_generators.size() - 1);
const u32 instruction = instruction_generators[index].Generate();
IR::Block block{location};
bool should_continue = A64::TranslateSingleInstruction(block, location, instruction);
if (!should_continue && !is_last_inst)
2018-01-13 18:04:19 +00:00
goto restart;
for (const auto& ir_inst : block)
2018-01-25 23:56:57 +00:00
if (ir_inst.GetOpcode() == IR::Opcode::A64ExceptionRaised || ir_inst.GetOpcode() == IR::Opcode::A64CallSupervisor)
2018-01-13 18:04:19 +00:00
goto restart;
return instruction;
}
2018-01-21 17:55:26 +00:00
static void RunTestInstance(const std::array<u64, 31>& regs, const std::array<Vector, 32>& vecs, const size_t instructions_offset, const std::vector<u32>& instructions, const u32 pstate) {
2018-01-13 18:04:19 +00:00
TestEnv jit_env;
TestEnv uni_env;
std::copy(instructions.begin(), instructions.end(), jit_env.code_mem.begin() + instructions_offset);
std::copy(instructions.begin(), instructions.end(), uni_env.code_mem.begin() + instructions_offset);
jit_env.code_mem[instructions.size() + instructions_offset] = 0x14000000; // B .
uni_env.code_mem[instructions.size() + instructions_offset] = 0x14000000; // B .
2018-01-13 18:04:19 +00:00
Dynarmic::A64::Jit jit{Dynarmic::A64::UserConfig{&jit_env}};
Unicorn uni{uni_env};
jit.SetRegisters(regs);
2018-01-21 17:55:26 +00:00
jit.SetVectors(vecs);
jit.SetPC(instructions_offset * 4);
2018-01-13 18:04:19 +00:00
jit.SetSP(0x8000000);
2018-01-13 21:51:50 +00:00
jit.SetPstate(pstate);
2018-01-13 18:04:19 +00:00
uni.SetRegisters(regs);
2018-01-21 17:55:26 +00:00
uni.SetVectors(vecs);
uni.SetPC(instructions_offset * 4);
2018-01-13 18:04:19 +00:00
uni.SetSP(0x8000000);
2018-01-13 21:51:50 +00:00
uni.SetPstate(pstate);
2018-01-13 18:04:19 +00:00
jit_env.ticks_left = instructions.size();
jit.Run();
uni_env.ticks_left = instructions.size();
uni.Run();
2018-01-25 23:56:57 +00:00
REQUIRE(uni.GetPC() == jit.GetPC());
2018-01-13 18:04:19 +00:00
REQUIRE(uni.GetRegisters() == jit.GetRegisters());
2018-01-23 17:45:34 +00:00
REQUIRE(uni.GetVectors() == jit.GetVectors());
2018-01-13 18:04:19 +00:00
REQUIRE(uni.GetSP() == jit.GetSP());
REQUIRE((uni.GetPstate() & 0xF0000000) == (jit.GetPstate() & 0xF0000000));
2018-01-25 23:56:57 +00:00
REQUIRE(uni_env.modified_memory == jit_env.modified_memory);
2018-01-13 18:04:19 +00:00
}
TEST_CASE("A64: Single random instruction", "[a64]") {
2018-01-17 00:10:28 +00:00
for (size_t iteration = 0; iteration < 100000; ++iteration) {
2018-01-13 18:04:19 +00:00
std::array<u64, 31> regs;
std::generate_n(regs.begin(), 31, []{ return RandInt<u64>(0, ~u64(0)); });
2018-01-21 17:55:26 +00:00
std::array<Vector, 32> vecs;
std::generate_n(vecs.begin(), 32, []{ return RandomVector(); });
2018-01-13 18:04:19 +00:00
std::vector<u32> instructions;
instructions.push_back(GenRandomInst(0, true));
2018-01-13 21:51:50 +00:00
u32 pstate = RandInt<u32>(0, 0xF) << 28;
2018-01-13 18:04:19 +00:00
INFO("Instruction: 0x" << std::hex << instructions[0]);
2018-01-17 00:10:28 +00:00
2018-01-21 17:55:26 +00:00
RunTestInstance(regs, vecs, 100, instructions, pstate);
2018-01-13 18:04:19 +00:00
}
2018-01-13 21:51:50 +00:00
}