From 1cd06161865c9bfaae702c25cd945416a1238b36 Mon Sep 17 00:00:00 2001 From: offtkp Date: Tue, 17 Sep 2024 17:24:57 +0300 Subject: [PATCH] Use singleton class --- src/common/decoder.cpp | 13 +++++++------ src/common/decoder.h | 14 ++++++-------- src/core/cpu_patches.cpp | 4 ++-- src/core/signals.cpp | 2 +- 4 files changed, 16 insertions(+), 17 deletions(-) diff --git a/src/common/decoder.cpp b/src/common/decoder.cpp index d8eddbcc6..249907419 100644 --- a/src/common/decoder.cpp +++ b/src/common/decoder.cpp @@ -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 = @@ -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; char szBuffer[bufLen]; 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); } -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); } diff --git a/src/common/decoder.h b/src/common/decoder.h index 5d2175ba9..1f2219596 100644 --- a/src/common/decoder.h +++ b/src/common/decoder.h @@ -4,28 +4,26 @@ #pragma once #include +#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; + } // namespace Common diff --git a/src/core/cpu_patches.cpp b/src/core/cpu_patches.cpp index 819855ae3..3f4e4c0df 100644 --- a/src/core/cpu_patches.cpp +++ b/src/core/cpu_patches.cpp @@ -663,8 +663,8 @@ static PatchModule* GetModule(const void* ptr) { static std::pair 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); } diff --git a/src/core/signals.cpp b/src/core/signals.cpp index ab18e1c1c..df3b28b97 100644 --- a/src/core/signals.cpp +++ b/src/core/signals.cpp @@ -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);