From 6f42c4b3b3e2ee5150d9ad8b384a98eddb27cf5c Mon Sep 17 00:00:00 2001 From: paxcut <53811119+paxcut@users.noreply.github.com> Date: Wed, 25 Jun 2025 03:53:17 -0700 Subject: [PATCH] fix: fixes for pattern language dissassembler support (#2314) Following the documentation (which is not being updated for this type) on using `hex::type::Instruction` fails to produce any patterns regardless of how you format the string that is passed to capstone to select architecture and options. The error is traced back to mishandling the input string so that the correct parts are not selected properly. Rather than manually selecting the parts of the input string from the result of find it is much simpler to use splitString() (which uses find internally) and does all the work for us with fewer chances for errors. There are still problems. The resulting string for the formatter doesn't return the disassembled instruction and prints the variable name with the @ used to place it. To view the instruction you need to unseal the pattern and open the child which then shows the instruction. That only happens after this fix has been applied. --- .../include/content/helpers/disassembler.hpp | 33 ++++++++----------- .../source/content/pl_builtin_types.cpp | 4 +++ 2 files changed, 17 insertions(+), 20 deletions(-) diff --git a/plugins/disassembler/include/content/helpers/disassembler.hpp b/plugins/disassembler/include/content/helpers/disassembler.hpp index c0ee288f1..b3f2e821f 100644 --- a/plugins/disassembler/include/content/helpers/disassembler.hpp +++ b/plugins/disassembler/include/content/helpers/disassembler.hpp @@ -97,16 +97,15 @@ namespace hex::plugin::disasm { // string has to be in the form of `arch;option1,option2,option3,no-option4` // Not all results might make sense for capstone static std::pair stringToSettings(std::string_view string) { - const auto archSeparator = string.find_first_of(';'); + const auto vectorString = wolv::util::splitString(std::string(string), ";"); - std::string_view archName; - std::string_view options; - if (archSeparator == std::string_view::npos) { - archName = wolv::util::trim(string); - options = ""; + std::string archName; + std::string options; + archName = wolv::util::trim(vectorString[0]); + if (vectorString.size() != 1) { + options = wolv::util::trim(vectorString[1]); } else { - archName = wolv::util::trim(string.substr(0, archSeparator - 1)); - options = wolv::util::trim(string.substr(archSeparator + 1)); + options = ""; } u32 arch = {}; @@ -114,10 +113,11 @@ namespace hex::plugin::disasm { if (archName.ends_with("be") || archName.ends_with("eb")) { mode |= CS_MODE_BIG_ENDIAN; - archName.remove_suffix(2); + archName.pop_back(); + archName.pop_back(); } else if (archName.ends_with("le") || archName.ends_with("el")) { - mode |= CS_MODE_LITTLE_ENDIAN; - archName.remove_suffix(2); + archName.pop_back(); + archName.pop_back(); } if (equalsIgnoreCase(archName, "arm")) { @@ -171,15 +171,8 @@ namespace hex::plugin::disasm { else throw std::runtime_error("Invalid disassembler architecture"); - while (!options.empty()) { - std::string_view option; - auto separatorPos = options.find_first_of(','); - if (separatorPos == std::string_view::npos) - option = options; - else - option = options.substr(0, separatorPos - 1); - - options.remove_prefix(option.size() + 1); + auto optionsVector = wolv::util::splitString(std::string(options), ","); + for (std::string_view option : optionsVector) { option = wolv::util::trim(option); bool shouldAdd = true; diff --git a/plugins/disassembler/source/content/pl_builtin_types.cpp b/plugins/disassembler/source/content/pl_builtin_types.cpp index c94130e60..4b56f49de 100644 --- a/plugins/disassembler/source/content/pl_builtin_types.cpp +++ b/plugins/disassembler/source/content/pl_builtin_types.cpp @@ -52,6 +52,10 @@ namespace hex::plugin::disasm { return m_instructionString; } + [[nodiscard]] std::string toString() override { + return m_instructionString; + } + private: std::string m_instructionString; };