Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion resources/asmtext_filter_tests.ce-bug-3963.approved.txt

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions resources/asmtext_filter_tests.ce-bug-8809.approved.txt

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

134 changes: 134 additions & 0 deletions resources/ce-bug-8809.asm

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

108 changes: 78 additions & 30 deletions src/objdump/parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -152,22 +152,85 @@ void AsmParser::ObjDumpParser::label()

void AsmParser::ObjDumpParser::labelref()
{
if (!this->state.ignoreUntilNextLabel)
if (this->state.ignoreUntilNextLabel)
{
this->state.currentLabelReference = {};
return;
}

std::size_t lastBracketPos = this->state.text.find_last_of('>');
if (lastBracketPos == std::string::npos)
{
this->state.currentLabelReference = {};
return;
}

std::size_t startBracketPos = std::string::npos;
for (std::size_t i = lastBracketPos; i-- > 0;)
{
if (this->state.text[i] == '<')
{
if (i == 0 || is_whitespace(this->state.text[i - 1]))
{
startBracketPos = i;
}
}
}

if (startBracketPos != std::string::npos && startBracketPos < lastBracketPos)
{
this->state.currentLabelReference.range.end_col = static_cast<uint16_t>(ustrlen(this->state.text));
try
std::size_t labelEndPos = lastBracketPos;

std::size_t lastPlusPos = this->state.text.find_last_of('+', lastBracketPos);

if (lastPlusPos != std::string::npos && lastPlusPos > startBracketPos)
{
this->state.currentLabelReference.name = this->state.text.substr(this->state.currentLabelReference.range.start_col);
std::size_t offsetLen = lastBracketPos - lastPlusPos - 1;

if (!AssemblyTextParserUtils::shouldIgnoreFunction(this->state.currentLabelReference.name, this->filter))
if (offsetLen >= 3 && this->state.text[lastPlusPos + 1] == '0' && this->state.text[lastPlusPos + 2] == 'x')
{
this->state.currentLine.labels.push_back(this->state.currentLabelReference);
bool validHex = true;
for (std::size_t i = 3; i <= offsetLen; ++i)
{
if (!is_hex(this->state.text[lastPlusPos + i]))
{
validHex = false;
break;
}
}

if (validHex)
{
labelEndPos = lastPlusPos;
}
}
}
catch (...)

if (labelEndPos > startBracketPos + 1)
{
// ignore erroneous nonsense
this->state.currentLabelReference.name = "";
try
{
std::string_view text_view(this->state.text);

this->state.currentLabelReference.range.start_col =
static_cast<uint16_t>(ustrlen(text_view.substr(0, startBracketPos)) + 1);

this->state.currentLabelReference.range.end_col =
static_cast<uint16_t>(ustrlen(text_view.substr(0, labelEndPos)));

std::size_t nameLen = labelEndPos - startBracketPos - 1;
this->state.currentLabelReference.name = text_view.substr(startBracketPos + 1, nameLen);

if (!AssemblyTextParserUtils::shouldIgnoreFunction(this->state.currentLabelReference.name, this->filter))
{
this->state.currentLine.labels.push_back(this->state.currentLabelReference);
}
}
catch (...)
{
// ignore erroneous nonsense
this->state.currentLabelReference.name = "";
}
}
}

Expand Down Expand Up @@ -316,6 +379,10 @@ void AsmParser::ObjDumpParser::fromStream(std::istream &in)
}
else if (c == '\n')
{
if (!this->state.inComment && !this->state.inLabel)
{
this->labelref();
}
this->eol();
continue;
}
Expand Down Expand Up @@ -451,30 +518,11 @@ void AsmParser::ObjDumpParser::fromStream(std::istream &in)
{
if (c == '#')
{
this->state.inComment = true;
}
else if (c == '<')
{
this->state.inSomethingWithALabel = true;
this->state.currentLabelReference.range =
asm_range{ .start_col = static_cast<uint16_t>(ustrlen(this->state.text) + 1),
.end_col = static_cast<uint16_t>(0) };
}
else if (this->state.inSomethingWithALabel)
{
if (c == '>')
if (!this->state.inLabel)
{
this->state.inSomethingWithALabel = false;
if (this->state.currentLabelReference.name.empty())
{
this->labelref();
}
}
else if (c == '+')
{
this->state.inSomethingWithALabel = false;
this->labelref();
}
this->state.inComment = true;
}
}

Expand Down
33 changes: 33 additions & 0 deletions src/test/asmtext_filter_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -269,3 +269,36 @@ TEST_CASE("example-llvm-objdump")

ApprovalTests::Approvals::verify(ss.str());
}

TEST_CASE("ce-bug-8809")
{
AsmParser::Filter filter;
filter.binary = true;
filter.plt = true;
filter.library_functions = true;
filter.unused_labels = true;

std::string asmpath;
if (std::filesystem::current_path().string().ends_with("test"))
{
asmpath = "../../../resources/ce-bug-8809.asm";
}
else
{
asmpath = "../../resources/ce-bug-8809.asm";
}

AsmParser::ObjDumpParser parser(filter);
parser.setReproducible();

std::fstream fs;
fs.open(asmpath, std::fstream::in);
REQUIRE(fs.is_open() == true);

parser.fromStream(fs);

std::stringstream ss;
parser.outputJson(ss);

ApprovalTests::Approvals::verify(ss.str());
}