Skip to content

Commit

Permalink
fix: Crashes when disassembling data
Browse files Browse the repository at this point in the history
Fixes #2025
  • Loading branch information
WerWolv committed Dec 25, 2024
1 parent 010025c commit 0454a36
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 16 deletions.
16 changes: 9 additions & 7 deletions plugins/disassembler/source/content/pl_builtin_types.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,21 +104,23 @@ namespace hex::plugin::disasm {
std::vector<u8> data(std::min<u64>(32, evaluator->getSectionSize(sectionId) - address));
evaluator->readData(address, data.data(), data.size(), sectionId);

cs_insn instruction;
auto *instruction = cs_malloc(capstone);
ON_SCOPE_EXIT { cs_free(instruction, 1); };

const u8 *code = data.data();
size_t dataSize = data.size();
if (!cs_disasm_iter(capstone, &code, &dataSize, &instructionLoadAddress, &instruction)) {
if (!cs_disasm_iter(capstone, &code, &dataSize, &instructionLoadAddress, instruction)) {
err::E0012.throwError("Failed to disassemble instruction");
}

auto result = std::make_unique<PatternInstruction>(evaluator, address, instruction.size, 0);
auto result = std::make_unique<PatternInstruction>(evaluator, address, instruction->size, 0);

std::string instructionString;
if (instruction.mnemonic[0] != '\x00')
instructionString += instruction.mnemonic;
if (instruction.op_str[0] != '\x00') {
if (instruction->mnemonic[0] != '\x00')
instructionString += instruction->mnemonic;
if (instruction->op_str[0] != '\x00') {
instructionString += ' ';
instructionString += instruction.op_str;
instructionString += instruction->op_str;
}
result->setInstructionString(instructionString);

Expand Down
20 changes: 11 additions & 9 deletions plugins/disassembler/source/content/views/view_disassembler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,14 @@ namespace hex::plugin::disasm {

m_disassemblerTask = TaskManager::createTask("hex.disassembler.view.disassembler.disassembling"_lang, m_regionToDisassemble.getSize(), [this](auto &task) {
csh capstoneHandle;
cs_insn instruction;

cs_mode mode = m_mode;

// Create a capstone disassembler instance
if (cs_open(Disassembler::toCapstoneArchitecture(m_architecture), mode, &capstoneHandle) == CS_ERR_OK) {
auto *instruction = cs_malloc(capstoneHandle);
ON_SCOPE_EXIT { cs_free(instruction, 1); };


// Tell capstone to skip data bytes
cs_option(capstoneHandle, CS_OPT_SKIPDATA, CS_OPT_ON);
Expand All @@ -75,24 +77,24 @@ namespace hex::plugin::disasm {

// Ask capstone to disassemble the data
const u8 *code = buffer.data();
while (cs_disasm_iter(capstoneHandle, &code, &bufferSize, &instructionLoadAddress, &instruction)) {
while (cs_disasm_iter(capstoneHandle, &code, &bufferSize, &instructionLoadAddress, instruction)) {
task.update(instructionDataAddress);

// Convert the capstone instructions to our disassembly format
Disassembly disassembly = { };
disassembly.address = instruction.address;
disassembly.address = instruction->address;
disassembly.offset = instructionDataAddress - m_imageBaseAddress;
disassembly.size = instruction.size;
disassembly.mnemonic = instruction.mnemonic;
disassembly.operators = instruction.op_str;
disassembly.size = instruction->size;
disassembly.mnemonic = instruction->mnemonic;
disassembly.operators = instruction->op_str;

for (u16 j = 0; j < instruction.size; j++)
disassembly.bytes += hex::format("{0:02X} ", instruction.bytes[j]);
for (u16 j = 0; j < instruction->size; j++)
disassembly.bytes += hex::format("{0:02X} ", instruction->bytes[j]);
disassembly.bytes.pop_back();

m_disassembly.push_back(disassembly);

instructionDataAddress += instruction.size;
instructionDataAddress += instruction->size;
hadError = false;
}

Expand Down

0 comments on commit 0454a36

Please sign in to comment.