From d2704242f22e41556627d324a28c140059aeed40 Mon Sep 17 00:00:00 2001 From: WerWolv Date: Mon, 10 Jan 2022 00:40:34 +0100 Subject: [PATCH] patterns: Fixed `pointer_base` attribute Hopefully takes care of #367 --- .../include/hex/pattern_language/ast_node.hpp | 25 ++++++++++--------- .../hex/pattern_language/pattern_data.hpp | 19 +++++++++++--- 2 files changed, 29 insertions(+), 15 deletions(-) diff --git a/plugins/libimhex/include/hex/pattern_language/ast_node.hpp b/plugins/libimhex/include/hex/pattern_language/ast_node.hpp index e815ee39b..cb3cf2d7c 100644 --- a/plugins/libimhex/include/hex/pattern_language/ast_node.hpp +++ b/plugins/libimhex/include/hex/pattern_language/ast_node.hpp @@ -718,16 +718,14 @@ namespace hex::pl { LogConsole::abortEvaluation("pointer base function needs exactly one parameter", node); if (auto pointerPattern = dynamic_cast(pattern)) { - u128 value = 0; - evaluator->getProvider()->read(pattern->getOffset(), &value, pattern->getSize()); - value = hex::changeEndianess(value, pattern->getSize(), pattern->getEndian()); + u128 pointerValue = pointerPattern->getPointedAtAddress(); - auto result = function.func(evaluator, { value }); + auto result = function.func(evaluator, { pointerValue }); if (!result.has_value()) LogConsole::abortEvaluation("pointer base function did not return a value", node); - pointerPattern->rebase(Token::literalToUnsigned(result.value())); + pointerPattern->setPointedAtAddress(Token::literalToUnsigned(result.value()) + pointerValue); } else { LogConsole::abortEvaluation("pointer_base attribute may only be applied to a pointer"); } @@ -1130,24 +1128,27 @@ namespace hex::pl { }, offset->getValue()); } - auto offset = evaluator->dataOffset(); + auto startOffset = evaluator->dataOffset(); auto sizePattern = this->m_sizeType->createPatterns(evaluator).front(); ON_SCOPE_EXIT { delete sizePattern; }; - auto pattern = new PatternDataPointer(offset, sizePattern->getSize(), evaluator); + auto pattern = new PatternDataPointer(startOffset, sizePattern->getSize(), evaluator); pattern->setVariableName(this->m_name); - applyVariableAttributes(evaluator, this, pattern); - - offset = evaluator->dataOffset(); + auto endOffset = evaluator->dataOffset(); { u128 pointerAddress = 0; evaluator->getProvider()->read(pattern->getOffset(), &pointerAddress, pattern->getSize()); pointerAddress = hex::changeEndianess(pointerAddress, sizePattern->getSize(), sizePattern->getEndian()); - evaluator->dataOffset() = pointerAddress; + evaluator->dataOffset() = startOffset; + + pattern->setPointedAtAddress(pointerAddress); + applyVariableAttributes(evaluator, this, pattern); + + evaluator->dataOffset() = pattern->getPointedAtAddress(); auto pointedAtPattern = this->m_type->createPatterns(evaluator).front(); @@ -1155,7 +1156,7 @@ namespace hex::pl { pattern->setEndian(sizePattern->getEndian()); } - evaluator->dataOffset() = offset; + evaluator->dataOffset() = endOffset; return { pattern }; } diff --git a/plugins/libimhex/include/hex/pattern_language/pattern_data.hpp b/plugins/libimhex/include/hex/pattern_language/pattern_data.hpp index efb72976b..8ab78ffc3 100644 --- a/plugins/libimhex/include/hex/pattern_language/pattern_data.hpp +++ b/plugins/libimhex/include/hex/pattern_language/pattern_data.hpp @@ -378,7 +378,7 @@ namespace hex::pl { ImGui::TableNextColumn(); ImGui::TextFormattedColored(ImColor(0xFF9BC64D), "{}", this->getFormattedName()); ImGui::TableNextColumn(); - ImGui::TextFormatted("*(0x{0})", data); + ImGui::TextFormatted("*(0x{0:X})", data); if (open) { this->m_pointedAt->createEntry(provider); @@ -423,6 +423,15 @@ namespace hex::pl { void setPointedAtPattern(PatternData *pattern) { this->m_pointedAt = pattern; this->m_pointedAt->setVariableName(hex::format("*({})", this->getVariableName())); + this->m_pointedAt->setOffset(this->m_pointedAtAddress); + } + + void setPointedAtAddress(u64 address) { + this->m_pointedAtAddress = address; + } + + [[nodiscard]] u64 getPointedAtAddress() const { + return this->m_pointedAtAddress; } [[nodiscard]] PatternData* getPointedAtPattern() { @@ -440,14 +449,18 @@ namespace hex::pl { } void rebase(u64 base) { - if (this->m_pointedAt != nullptr) - this->m_pointedAt->setOffset((this->m_pointedAt->getOffset() - this->m_pointerBase) + base); + if (this->m_pointedAt != nullptr) { + this->m_pointedAtAddress = (this->m_pointedAt->getOffset() - this->m_pointerBase) + base; + this->m_pointedAt->setOffset(this->m_pointedAtAddress); + } this->m_pointerBase = base; } private: PatternData *m_pointedAt = nullptr; + u64 m_pointedAtAddress = 0; + u64 m_pointerBase = 0; };