Skip to content

Commit

Permalink
Use singleton class
Browse files Browse the repository at this point in the history
  • Loading branch information
OFFTKP committed Sep 17, 2024
1 parent 74c9eb7 commit 44b3dd6
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 17 deletions.
13 changes: 7 additions & 6 deletions src/common/decoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@

namespace Common {

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

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

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

void Decoder::printInst(ZydisDecodedInstruction& inst, ZydisDecodedOperand* operands, u64 address) {
void DecoderImpl::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) {
ZyanStatus DecoderImpl::decodeInstruction(ZydisDecodedInstruction& inst,
ZydisDecodedOperand* operands, void* data, u64 size) {
return ZydisDecoderDecodeFull(&m_decoder, data, size, &inst, operands);
}

Expand Down
14 changes: 6 additions & 8 deletions src/common/decoder.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,26 @@
#pragma once

#include <Zydis/Zydis.h>
#include "common/singleton.h"
#include "common/types.h"

namespace Common {

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

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;
ZydisFormatter m_formatter;
};

using Decoder = Common::Singleton<DecoderImpl>;

} // namespace Common
4 changes: 2 additions & 2 deletions src/core/cpu_patches.cpp
Original file line number Diff line number Diff line change
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 = Common::Decoder::Instance().decodeInstruction(instruction, operands, code,
module->end - code);
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
2 changes: 1 addition & 1 deletion src/core/signals.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ static std::string DisassembleInstruction(void* code_address) {
ZydisDecodedInstruction instruction;
ZydisDecodedOperand operands[ZYDIS_MAX_OPERAND_COUNT];
const auto status =
Common::Decoder::Instance().decodeInstruction(instruction, operands, code_address);
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 44b3dd6

Please sign in to comment.