Use singleton class

This commit is contained in:
offtkp 2024-09-17 17:24:57 +03:00
parent 6cc61fc27d
commit 1cd0616186
4 changed files with 16 additions and 17 deletions

View File

@ -6,14 +6,14 @@
namespace Common { namespace Common {
Decoder::Decoder() { DecoderImpl::DecoderImpl() {
ZydisDecoderInit(&m_decoder, ZYDIS_MACHINE_MODE_LONG_64, ZYDIS_STACK_WIDTH_64); ZydisDecoderInit(&m_decoder, ZYDIS_MACHINE_MODE_LONG_64, ZYDIS_STACK_WIDTH_64);
ZydisFormatterInit(&m_formatter, ZYDIS_FORMATTER_STYLE_INTEL); 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; ZydisDecodedInstruction instruction;
ZydisDecodedOperand operands[ZYDIS_MAX_OPERAND_COUNT_VISIBLE]; ZydisDecodedOperand operands[ZYDIS_MAX_OPERAND_COUNT_VISIBLE];
ZyanStatus status = ZyanStatus status =
@ -25,7 +25,8 @@ 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; const int bufLen = 256;
char szBuffer[bufLen]; char szBuffer[bufLen];
ZydisFormatterFormatInstruction(&m_formatter, &inst, operands, inst.operand_count_visible, ZydisFormatterFormatInstruction(&m_formatter, &inst, operands, inst.operand_count_visible,
@ -33,8 +34,8 @@ void Decoder::printInst(ZydisDecodedInstruction& inst, ZydisDecodedOperand* oper
fmt::print("instruction: {}\n", szBuffer); fmt::print("instruction: {}\n", szBuffer);
} }
ZyanStatus Decoder::decodeInstruction(ZydisDecodedInstruction& inst, ZydisDecodedOperand* operands, ZyanStatus DecoderImpl::decodeInstruction(ZydisDecodedInstruction& inst,
void* data, u64 size) { ZydisDecodedOperand* operands, void* data, u64 size) {
return ZydisDecoderDecodeFull(&m_decoder, data, size, &inst, operands); return ZydisDecoderDecodeFull(&m_decoder, data, size, &inst, operands);
} }

View File

@ -4,28 +4,26 @@
#pragma once #pragma once
#include <Zydis/Zydis.h> #include <Zydis/Zydis.h>
#include "common/singleton.h"
#include "common/types.h" #include "common/types.h"
namespace Common { namespace Common {
class Decoder { class DecoderImpl {
public: public:
Decoder(); DecoderImpl();
~Decoder(); ~DecoderImpl();
void printInst(ZydisDecodedInstruction& inst, ZydisDecodedOperand* operands, u64 address); void printInst(ZydisDecodedInstruction& inst, ZydisDecodedOperand* operands, u64 address);
void printInstruction(void* code, u64 address); void printInstruction(void* code, u64 address);
ZyanStatus decodeInstruction(ZydisDecodedInstruction& inst, ZydisDecodedOperand* operands, ZyanStatus decodeInstruction(ZydisDecodedInstruction& inst, ZydisDecodedOperand* operands,
void* data, u64 size = 15); void* data, u64 size = 15);
static Decoder& Instance() {
static Decoder instance;
return instance;
}
private: private:
ZydisDecoder m_decoder; ZydisDecoder m_decoder;
ZydisFormatter m_formatter; ZydisFormatter m_formatter;
}; };
using Decoder = Common::Singleton<DecoderImpl>;
} // namespace Common } // namespace Common

View File

@ -663,8 +663,8 @@ static PatchModule* GetModule(const void* ptr) {
static std::pair<bool, u64> TryPatch(u8* code, PatchModule* module) { static std::pair<bool, u64> TryPatch(u8* code, PatchModule* module) {
ZydisDecodedInstruction instruction; ZydisDecodedInstruction instruction;
ZydisDecodedOperand operands[ZYDIS_MAX_OPERAND_COUNT]; ZydisDecodedOperand operands[ZYDIS_MAX_OPERAND_COUNT];
const auto status = Common::Decoder::Instance().decodeInstruction(instruction, operands, code, const auto status = Common::Decoder::Instance()->decodeInstruction(instruction, operands, code,
module->end - code); module->end - code);
if (!ZYAN_SUCCESS(status)) { if (!ZYAN_SUCCESS(status)) {
return std::make_pair(false, 1); return std::make_pair(false, 1);
} }

View File

@ -69,7 +69,7 @@ static std::string DisassembleInstruction(void* code_address) {
ZydisDecodedInstruction instruction; ZydisDecodedInstruction instruction;
ZydisDecodedOperand operands[ZYDIS_MAX_OPERAND_COUNT]; ZydisDecodedOperand operands[ZYDIS_MAX_OPERAND_COUNT];
const auto status = const auto status =
Common::Decoder::Instance().decodeInstruction(instruction, operands, code_address); Common::Decoder::Instance()->decodeInstruction(instruction, operands, code_address);
if (ZYAN_SUCCESS(status)) { if (ZYAN_SUCCESS(status)) {
ZydisFormatter formatter; ZydisFormatter formatter;
ZydisFormatterInit(&formatter, ZYDIS_FORMATTER_STYLE_INTEL); ZydisFormatterInit(&formatter, ZYDIS_FORMATTER_STYLE_INTEL);