Skip to content

Commit

Permalink
Use a singleton for instruction decoding
Browse files Browse the repository at this point in the history
  • Loading branch information
OFFTKP committed Sep 17, 2024
1 parent b9850b2 commit 74c9eb7
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 22 deletions.
4 changes: 2 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -350,8 +350,8 @@ set(COMMON src/common/logging/backend.cpp
src/common/config.cpp
src/common/config.h
src/common/debug.h
src/common/disassembler.cpp
src/common/disassembler.h
src/common/decoder.cpp
src/common/decoder.h
src/common/endian.h
src/common/enum.h
src/common/io_file.cpp
Expand Down
16 changes: 10 additions & 6 deletions src/common/disassembler.cpp → src/common/decoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@
// SPDX-License-Identifier: GPL-2.0-or-later

#include <fmt/format.h>
#include "common/disassembler.h"
#include "common/decoder.h"

namespace Common {

Disassembler::Disassembler() {
Decoder::Decoder() {
ZydisDecoderInit(&m_decoder, ZYDIS_MACHINE_MODE_LONG_64, ZYDIS_STACK_WIDTH_64);
ZydisFormatterInit(&m_formatter, ZYDIS_FORMATTER_STYLE_INTEL);
}

Disassembler::~Disassembler() = default;
Decoder::~Decoder() = default;

void Disassembler::printInstruction(void* code, u64 address) {
void Decoder::printInstruction(void* code, u64 address) {
ZydisDecodedInstruction instruction;
ZydisDecodedOperand operands[ZYDIS_MAX_OPERAND_COUNT_VISIBLE];
ZyanStatus status =
Expand All @@ -25,13 +25,17 @@ void Disassembler::printInstruction(void* code, u64 address) {
}
}

void Disassembler::printInst(ZydisDecodedInstruction& inst, ZydisDecodedOperand* operands,
u64 address) {
void Decoder::printInst(ZydisDecodedInstruction& inst, ZydisDecodedOperand* operands, u64 address) {
const int bufLen = 256;
char szBuffer[bufLen];
ZydisFormatterFormatInstruction(&m_formatter, &inst, operands, inst.operand_count_visible,
szBuffer, sizeof(szBuffer), address, ZYAN_NULL);
fmt::print("instruction: {}\n", szBuffer);
}

ZyanStatus Decoder::decodeInstruction(ZydisDecodedInstruction& inst, ZydisDecodedOperand* operands,
void* data, u64 size) {
return ZydisDecoderDecodeFull(&m_decoder, data, size, &inst, operands);
}

} // namespace Common
13 changes: 10 additions & 3 deletions src/common/disassembler.h → src/common/decoder.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,20 @@

namespace Common {

class Disassembler {
class Decoder {
public:
Disassembler();
~Disassembler();
Decoder();
~Decoder();

void printInst(ZydisDecodedInstruction& inst, ZydisDecodedOperand* operands, u64 address);
void printInstruction(void* code, u64 address);
ZyanStatus decodeInstruction(ZydisDecodedInstruction& inst, ZydisDecodedOperand* operands,
void* data, u64 size = 15);

static Decoder& Instance() {
static Decoder instance;
return instance;
}

private:
ZydisDecoder m_decoder;
Expand Down
8 changes: 3 additions & 5 deletions src/core/cpu_patches.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <xbyak/xbyak.h>
#include "common/alignment.h"
#include "common/assert.h"
#include "common/decoder.h"
#include "common/types.h"
#include "core/signals.h"
#include "core/tls.h"
Expand Down Expand Up @@ -622,7 +623,6 @@ static const std::unordered_map<ZydisMnemonic, PatchInfo> Patches = {
};

static std::once_flag init_flag;
static ZydisDecoder instr_decoder;

struct PatchModule {
/// Mutex controlling access to module code regions.
Expand Down Expand Up @@ -663,8 +663,8 @@ static PatchModule* GetModule(const void* ptr) {
static std::pair<bool, u64> TryPatch(u8* code, PatchModule* module) {
ZydisDecodedInstruction instruction;
ZydisDecodedOperand operands[ZYDIS_MAX_OPERAND_COUNT];
const auto status =
ZydisDecoderDecodeFull(&instr_decoder, code, module->end - code, &instruction, operands);
const auto status = Common::Decoder::Instance().decodeInstruction(instruction, operands, code,
module->end - code);
if (!ZYAN_SUCCESS(status)) {
return std::make_pair(false, 1);
}
Expand Down Expand Up @@ -755,8 +755,6 @@ static bool PatchesIllegalInstructionHandler(void* code_address) {
}

static void PatchesInit() {
ZydisDecoderInit(&instr_decoder, ZYDIS_MACHINE_MODE_LONG_64, ZYDIS_STACK_WIDTH_64);

if (!Patches.empty()) {
auto* signals = Signals::Instance();
// Should be called last.
Expand Down
8 changes: 2 additions & 6 deletions src/core/signals.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@

#include "common/arch.h"
#include "common/assert.h"
#include "common/decoder.h"
#include "core/signals.h"

#ifdef _WIN32
#include <windows.h>
#else
#include <csignal>
#ifdef ARCH_X86_64
#include <Zydis/Decoder.h>
#include <Zydis/Formatter.h>
#endif
#endif
Expand Down Expand Up @@ -66,14 +66,10 @@ static std::string DisassembleInstruction(void* code_address) {
char buffer[256] = "<unable to decode>";

#ifdef ARCH_X86_64
ZydisDecoder decoder;
ZydisDecoderInit(&decoder, ZYDIS_MACHINE_MODE_LONG_64, ZYDIS_STACK_WIDTH_64);

ZydisDecodedInstruction instruction;
ZydisDecodedOperand operands[ZYDIS_MAX_OPERAND_COUNT];
static constexpr u64 max_length = 0x20;
const auto status =
ZydisDecoderDecodeFull(&decoder, code_address, max_length, &instruction, operands);
Common::Decoder::Instance().decodeInstruction(instruction, operands, code_address);
if (ZYAN_SUCCESS(status)) {
ZydisFormatter formatter;
ZydisFormatterInit(&formatter, ZYDIS_FORMATTER_STYLE_INTEL);
Expand Down

0 comments on commit 74c9eb7

Please sign in to comment.