summaryrefslogtreecommitdiff
path: root/languagerepresentation.cpp
diff options
context:
space:
mode:
authorMason Reed <mason@vector35.com>2025-02-11 13:57:47 -0500
committerMason Reed <mason@vector35.com>2025-02-11 22:13:56 -0500
commitb546d0c8d8957064424f37f1cae7bc5fc3695f4a (patch)
tree809bad573aa08ba6611697cc9c2d4d4464b3c747 /languagerepresentation.cpp
parent127ab740841f6938e359650c742314a7f1185904 (diff)
Fix partial initialization of `DisassemblyTextLine`
The usage of `DisassemblyTextLine` in the FFI was unsound, we would forget to initialize some fields causing a myriad of issues where round-tripping through the FFI was losing information.
Diffstat (limited to 'languagerepresentation.cpp')
-rw-r--r--languagerepresentation.cpp79
1 files changed, 7 insertions, 72 deletions
diff --git a/languagerepresentation.cpp b/languagerepresentation.cpp
index 1af38b4b..8194a57f 100644
--- a/languagerepresentation.cpp
+++ b/languagerepresentation.cpp
@@ -1,4 +1,5 @@
#include "binaryninjaapi.h"
+#include "ffi.h"
#include "highlevelilinstruction.h"
using namespace BinaryNinja;
@@ -39,19 +40,7 @@ vector<DisassemblyTextLine> LanguageRepresentationFunction::GetExprText(
BNDisassemblyTextLine* lines = BNGetLanguageRepresentationFunctionExprText(m_object, instr.function->GetObject(),
instr.exprIndex, settings ? settings->GetObject() : nullptr, instr.ast, precedence, statement, &count);
- vector<DisassemblyTextLine> result;
- result.reserve(count);
- for (size_t i = 0; i < count; i++)
- {
- DisassemblyTextLine line;
- line.addr = lines[i].addr;
- line.instrIndex = lines[i].instrIndex;
- line.highlight = lines[i].highlight;
- line.tokens = InstructionTextToken::ConvertInstructionTextTokenList(lines[i].tokens, lines[i].count);
- line.tags = Tag::ConvertTagList(lines[i].tags, lines[i].tagCount);
- result.push_back(line);
- }
-
+ vector<DisassemblyTextLine> result = ParseAPIObjectList<DisassemblyTextLine>(lines, count);
BNFreeDisassemblyTextLines(lines, count);
return result;
}
@@ -64,19 +53,7 @@ vector<DisassemblyTextLine> LanguageRepresentationFunction::GetLinearLines(
BNDisassemblyTextLine* lines = BNGetLanguageRepresentationFunctionLinearLines(m_object, instr.function->GetObject(),
instr.exprIndex, settings ? settings->GetObject() : nullptr, instr.ast, &count);
- vector<DisassemblyTextLine> result;
- result.reserve(count);
- for (size_t i = 0; i < count; i++)
- {
- DisassemblyTextLine line;
- line.addr = lines[i].addr;
- line.instrIndex = lines[i].instrIndex;
- line.highlight = lines[i].highlight;
- line.tokens = InstructionTextToken::ConvertInstructionTextTokenList(lines[i].tokens, lines[i].count);
- line.tags = Tag::ConvertTagList(lines[i].tags, lines[i].tagCount);
- result.push_back(line);
- }
-
+ vector<DisassemblyTextLine> result = ParseAPIObjectList<DisassemblyTextLine>(lines, count);
BNFreeDisassemblyTextLines(lines, count);
return result;
}
@@ -89,19 +66,7 @@ vector<DisassemblyTextLine> LanguageRepresentationFunction::GetBlockLines(
BNDisassemblyTextLine* lines = BNGetLanguageRepresentationFunctionBlockLines(
m_object, block->GetObject(), settings ? settings->GetObject() : nullptr, &count);
- vector<DisassemblyTextLine> result;
- result.reserve(count);
- for (size_t i = 0; i < count; i++)
- {
- DisassemblyTextLine line;
- line.addr = lines[i].addr;
- line.instrIndex = lines[i].instrIndex;
- line.highlight = lines[i].highlight;
- line.tokens = InstructionTextToken::ConvertInstructionTextTokenList(lines[i].tokens, lines[i].count);
- line.tags = Tag::ConvertTagList(lines[i].tags, lines[i].tagCount);
- result.push_back(line);
- }
-
+ vector<DisassemblyTextLine> result = ParseAPIObjectList<DisassemblyTextLine>(lines, count);
BNFreeDisassemblyTextLines(lines, count);
return result;
}
@@ -388,31 +353,13 @@ BNDisassemblyTextLine* LanguageRepresentationFunctionType::GetFunctionTypeTokens
Ref<Function> funcObj = new Function(BNNewFunctionReference(func));
Ref<DisassemblySettings> settingsObj = settings ? new DisassemblySettings(BNNewDisassemblySettingsReference(settings)) : nullptr;
auto lines = type->GetFunctionTypeTokens(funcObj, settingsObj);
- *count = lines.size();
- BNDisassemblyTextLine* buf = new BNDisassemblyTextLine[lines.size()];
- for (size_t i = 0; i < lines.size(); i++)
- {
- const DisassemblyTextLine& line = lines[i];
- buf[i].addr = line.addr;
- buf[i].instrIndex = line.instrIndex;
- buf[i].highlight = line.highlight;
- buf[i].tokens = InstructionTextToken::CreateInstructionTextTokenList(line.tokens);
- buf[i].count = line.tokens.size();
- buf[i].tags = Tag::CreateTagList(line.tags, &(buf[i].tagCount));
- }
-
- return buf;
+ return AllocAPIObjectList<DisassemblyTextLine>(lines, count);
}
void LanguageRepresentationFunctionType::FreeLinesCallback(void*, BNDisassemblyTextLine* lines, size_t count)
{
- for (size_t i = 0; i < count; i++)
- {
- InstructionTextToken::FreeInstructionTextTokenList(lines[i].tokens, lines[i].count);
- Tag::FreeTagList(lines[i].tags, lines[i].tagCount);
- }
- delete[] lines;
+ FreeAPIObjectList<DisassemblyTextLine>(lines, count);
}
@@ -506,19 +453,7 @@ vector<DisassemblyTextLine> CoreLanguageRepresentationFunctionType::GetFunctionT
BNDisassemblyTextLine* lines = BNGetLanguageRepresentationFunctionTypeFunctionTypeTokens(m_object,
func->GetObject(), settings ? settings->GetObject() : nullptr, &count);
- vector<DisassemblyTextLine> result;
- result.reserve(count);
- for (size_t i = 0; i < count; i++)
- {
- DisassemblyTextLine line;
- line.addr = lines[i].addr;
- line.instrIndex = lines[i].instrIndex;
- line.highlight = lines[i].highlight;
- line.tokens = InstructionTextToken::ConvertInstructionTextTokenList(lines[i].tokens, lines[i].count);
- line.tags = Tag::ConvertTagList(lines[i].tags, lines[i].tagCount);
- result.push_back(line);
- }
-
+ vector<DisassemblyTextLine> result = ParseAPIObjectList<DisassemblyTextLine>(lines, count);
BNFreeDisassemblyTextLines(lines, count);
return result;
}