Skip to content

Commit a6f64bf

Browse files
committed
Fix pseudo C/Rust absent lhs on comparison
This issue was caused because HLIL_SPLIT was not being handled on the left and right expressions of HLIL_IF instructions
1 parent 13445ba commit a6f64bf

File tree

4 files changed

+60
-24
lines changed

4 files changed

+60
-24
lines changed

lang/c/pseudoc.cpp

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -235,9 +235,15 @@ void PseudoCFunction::AppendComparison(const string& comparison, const HighLevel
235235
const auto leftExpr = instr.GetLeftExpr();
236236
const auto rightExpr = instr.GetRightExpr();
237237

238-
GetExprTextInternal(leftExpr, emitter, settings, precedence, false, signedHint);
238+
if (leftExpr.operation == HLIL_SPLIT)
239+
AppendDefaultSplitExpr(leftExpr, emitter, settings, precedence);
240+
else
241+
GetExprTextInternal(leftExpr, emitter, settings, precedence, false, signedHint);
239242
emitter.Append(OperationToken, comparison);
240-
GetExprTextInternal(rightExpr, emitter, settings, precedence, false, signedHint);
243+
if (rightExpr.operation == HLIL_SPLIT)
244+
AppendDefaultSplitExpr(rightExpr, emitter, settings, precedence);
245+
else
246+
GetExprTextInternal(rightExpr, emitter, settings, precedence, false, signedHint);
241247
}
242248

243249

@@ -436,6 +442,25 @@ PseudoCFunction::FieldDisplayType PseudoCFunction::GetFieldDisplayType(
436442
}
437443

438444

445+
void PseudoCFunction::AppendDefaultSplitExpr(const BinaryNinja::HighLevelILInstruction& instr,
446+
BinaryNinja::HighLevelILTokenEmitter& tokens, DisassemblySettings* settings, BNOperatorPrecedence precedence)
447+
{
448+
const auto high = instr.GetHighExpr<HLIL_SPLIT>();
449+
const auto low = instr.GetLowExpr<HLIL_SPLIT>();
450+
if (precedence == EqualityOperatorPrecedence)
451+
tokens.AppendOpenParen();
452+
tokens.AppendOpenParen();
453+
GetExprTextInternal(high, tokens, settings, precedence);
454+
tokens.Append(OperationToken, " << ");
455+
tokens.Append(IntegerToken, std::to_string(low.size * 8));
456+
tokens.AppendCloseParen();
457+
tokens.Append(OperationToken, " | ");
458+
GetExprTextInternal(low, tokens, settings, precedence);
459+
if (precedence == EqualityOperatorPrecedence)
460+
tokens.AppendCloseParen();
461+
}
462+
463+
439464
void PseudoCFunction::AppendFieldTextTokens(const HighLevelILInstruction& var, uint64_t offset,
440465
size_t memberIndex, size_t size, HighLevelILTokenEmitter& tokens, bool deref, bool displayDeref)
441466
{
@@ -1330,19 +1355,10 @@ void PseudoCFunction::GetExprTextInternal(const HighLevelILInstruction& instr, H
13301355
}
13311356
else if (srcExpr.operation == HLIL_SPLIT)
13321357
{
1333-
const auto high = srcExpr.GetHighExpr<HLIL_SPLIT>();
1334-
const auto low = srcExpr.GetLowExpr<HLIL_SPLIT>();
13351358
GetExprTextInternal(destExpr, tokens, settings, precedence);
13361359
tokens.Append(OperationToken, " = ");
1337-
tokens.AppendOpenParen();
1338-
GetExprTextInternal(high, tokens, settings, precedence);
1339-
tokens.Append(OperationToken, " << ");
1340-
tokens.Append(IntegerToken, std::to_string(low.size * 8));
1341-
tokens.AppendCloseParen();
1342-
tokens.Append(OperationToken, " | ");
1343-
GetExprTextInternal(low, tokens, settings, precedence);
1360+
AppendDefaultSplitExpr(srcExpr, tokens, settings, precedence);
13441361
tokens.AppendSemicolon();
1345-
tokens.NewLine();
13461362
return;
13471363
}
13481364
else

lang/c/pseudoc.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ class PseudoCFunction: public BinaryNinja::LanguageRepresentationFunction
3535
BinaryNinja::HighLevelILTokenEmitter& tokens, BinaryNinja::DisassemblySettings* settings);
3636
void AppendFieldTextTokens(const BinaryNinja::HighLevelILInstruction& var, uint64_t offset, size_t memberIndex, size_t size,
3737
BinaryNinja::HighLevelILTokenEmitter& tokens, bool deref, bool displayDeref = true);
38+
void AppendDefaultSplitExpr(const BinaryNinja::HighLevelILInstruction& instr, BinaryNinja::HighLevelILTokenEmitter& tokens,
39+
BinaryNinja::DisassemblySettings* settings, BNOperatorPrecedence precedence);
3840
void GetExprTextInternal(const BinaryNinja::HighLevelILInstruction& instr,
3941
BinaryNinja::HighLevelILTokenEmitter& tokens, BinaryNinja::DisassemblySettings* settings,
4042
BNOperatorPrecedence precedence = TopLevelOperatorPrecedence, bool statement = false,

lang/rust/pseudorust.cpp

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -258,9 +258,15 @@ void PseudoRustFunction::AppendComparison(const string& comparison, const HighLe
258258
const auto leftExpr = instr.GetLeftExpr();
259259
const auto rightExpr = instr.GetRightExpr();
260260

261-
GetExprText(leftExpr, emitter, settings, precedence, InnerExpression, signedHint);
261+
if (leftExpr.operation == HLIL_SPLIT)
262+
AppendDefaultSplitExpr(leftExpr, emitter, settings, precedence);
263+
else
264+
GetExprText(leftExpr, emitter, settings, precedence, InnerExpression, signedHint);
262265
emitter.Append(OperationToken, comparison);
263-
GetExprText(rightExpr, emitter, settings, precedence, InnerExpression, signedHint);
266+
if (rightExpr.operation == HLIL_SPLIT)
267+
AppendDefaultSplitExpr(rightExpr, emitter, settings, precedence);
268+
else
269+
GetExprText(rightExpr, emitter, settings, precedence, InnerExpression, signedHint);
264270
}
265271

266272

@@ -551,6 +557,25 @@ void PseudoRustFunction::AppendFieldTextTokens(const HighLevelILInstruction& var
551557
}
552558

553559

560+
void PseudoRustFunction::AppendDefaultSplitExpr(const BinaryNinja::HighLevelILInstruction& instr,
561+
BinaryNinja::HighLevelILTokenEmitter& tokens, DisassemblySettings* settings, BNOperatorPrecedence precedence)
562+
{
563+
const auto high = instr.GetHighExpr<HLIL_SPLIT>();
564+
const auto low = instr.GetLowExpr<HLIL_SPLIT>();
565+
if (precedence == EqualityOperatorPrecedence)
566+
tokens.AppendOpenParen();
567+
tokens.AppendOpenParen();
568+
GetExprText(high, tokens, settings, precedence);
569+
tokens.Append(OperationToken, " << ");
570+
tokens.Append(IntegerToken, std::to_string(low.size * 8));
571+
tokens.AppendCloseParen();
572+
tokens.Append(OperationToken, " | ");
573+
GetExprText(low, tokens, settings, precedence);
574+
if (precedence == EqualityOperatorPrecedence)
575+
tokens.AppendCloseParen();
576+
}
577+
578+
554579
bool PseudoRustFunction::IsMutable(const Variable& var) const
555580
{
556581
for (auto i : GetHighLevelILFunction()->GetVariableDefinitions(var))
@@ -1391,19 +1416,10 @@ void PseudoRustFunction::GetExprText(const HighLevelILInstruction& instr, HighLe
13911416
}
13921417
else if (srcExpr.operation == HLIL_SPLIT)
13931418
{
1394-
const auto high = srcExpr.GetHighExpr<HLIL_SPLIT>();
1395-
const auto low = srcExpr.GetLowExpr<HLIL_SPLIT>();
13961419
GetExprText(destExpr, tokens, settings, precedence);
13971420
tokens.Append(OperationToken, " = ");
1398-
tokens.AppendOpenParen();
1399-
GetExprText(high, tokens, settings, precedence);
1400-
tokens.Append(OperationToken, " << ");
1401-
tokens.Append(IntegerToken, std::to_string(low.size * 8));
1402-
tokens.AppendCloseParen();
1403-
tokens.Append(OperationToken, " | ");
1404-
GetExprText(low, tokens, settings, precedence);
1421+
AppendDefaultSplitExpr(srcExpr, tokens, settings, precedence);
14051422
tokens.AppendSemicolon();
1406-
tokens.NewLine();
14071423
return;
14081424
}
14091425
else

lang/rust/pseudorust.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ class PseudoRustFunction: public BinaryNinja::LanguageRepresentationFunction
4444
BinaryNinja::HighLevelILTokenEmitter& tokens, BinaryNinja::DisassemblySettings* settings);
4545
void AppendFieldTextTokens(const BinaryNinja::HighLevelILInstruction& var, uint64_t offset, size_t memberIndex, size_t size,
4646
BinaryNinja::HighLevelILTokenEmitter& tokens, bool deref);
47+
void AppendDefaultSplitExpr(const BinaryNinja::HighLevelILInstruction& instr, BinaryNinja::HighLevelILTokenEmitter& tokens,
48+
BinaryNinja::DisassemblySettings* settings, BNOperatorPrecedence precedence);
4749
bool IsMutable(const BinaryNinja::Variable& var) const;
4850

4951
void GetExprText(const BinaryNinja::HighLevelILInstruction& instr, BinaryNinja::HighLevelILTokenEmitter& tokens,

0 commit comments

Comments
 (0)