Skip to content
Merged
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
18 changes: 2 additions & 16 deletions Handle/BNAnalysisContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace BinaryNinja
/// Analysis passes use the context to retrieve IL functions, inspect lifted code, and
/// communicate structured requests back to the analysis pipeline via Inform().
/// </summary>
public sealed class AnalysisContext : AbstractSafeHandle<AnalysisContext>
public sealed partial class AnalysisContext : AbstractSafeHandle<AnalysisContext>
{
/// <summary>
/// Initializes a new AnalysisContext wrapper around an existing native handle.
Expand Down Expand Up @@ -357,21 +357,7 @@ public static AnalysisContext Create()
/// <param name="mlilFunction">The Medium Level IL function to set.</param>
public void SetMediumLevelILFunction(MediumLevelILFunction mlilFunction)
{
// 1. Validate the required parameter.
if (null == mlilFunction)
{
throw new ArgumentNullException(nameof(mlilFunction));
}

// 2. Forward to the native API with empty mapping arrays.
NativeMethods.BNSetMediumLevelILFunction(
this.handle ,
mlilFunction.DangerousGetHandle() ,
IntPtr.Zero ,
0 ,
IntPtr.Zero ,
0
);
this.SetMediumLevelILFunction(mlilFunction, null, null);
}

// ---------------------------------------------------------------------
Expand Down
44 changes: 33 additions & 11 deletions Handle/BNMediumLevelILFunction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

namespace BinaryNinja
{
public sealed class MediumLevelILFunction : AbstractSafeHandle<MediumLevelILFunction>
public sealed partial class MediumLevelILFunction : AbstractSafeHandle<MediumLevelILFunction>
{
private readonly bool isSSAForm;

Expand Down Expand Up @@ -541,6 +541,12 @@ Variable[] knownAliases

public void PrepareToCopyFunction(MediumLevelILFunction source)
{
if (null == source)
{
throw new ArgumentNullException(nameof(source));
}

this.BeginTranslation(source);
NativeMethods.BNPrepareToCopyMediumLevelILFunction(this.handle , source.DangerousGetHandle());
}

Expand Down Expand Up @@ -759,18 +765,20 @@ MediumLevelILExpressionIndex mediumExpr
{
return null;
}

LowLevelILFunction llilSSA = llil.SSAForm;

LowLevelILExpressionIndex lowExpr = NativeMethods.BNGetLowLevelILExprIndex(
this.handle,
mediumExpr
);

if ((ulong)lowExpr >= llil.ExpressionCount)
if ((ulong)lowExpr >= llilSSA.ExpressionCount)
{
return null;
}

return llil.GetExpression(lowExpr);
return llilSSA.GetExpression(lowExpr);
}

public LowLevelILInstruction? GetLowLevelILInstruction(
Expand All @@ -783,18 +791,20 @@ MediumLevelILInstructionIndex mediumInstr
{
return null;
}

LowLevelILFunction llilSSA = llil.SSAForm;

LowLevelILInstructionIndex lowInstr = NativeMethods.BNGetLowLevelILInstructionIndex(
this.handle,
mediumInstr
);

if ((ulong)lowInstr >= llil.InstructionCount)
if ((ulong)lowInstr >= llilSSA.InstructionCount)
{
return null;
}

return llil.GetInstruction(lowInstr);
return llilSSA.GetInstruction(lowInstr);
}

public LowLevelILInstruction[] GetLowLevelILExpressions(
Expand All @@ -807,6 +817,8 @@ MediumLevelILExpressionIndex mediumExpr
{
return Array.Empty<LowLevelILInstruction>();
}

LowLevelILFunction llilSSA = llil.SSAForm;

IntPtr arrayPointer = NativeMethods.BNGetLowLevelILExprIndexes(
this.handle,
Expand All @@ -825,7 +837,7 @@ out ulong arrayLength
foreach (LowLevelILExpressionIndex lowExpr in lowExprs)
{
expressions.Add(
llil.MustGetExpression(lowExpr)
llilSSA.MustGetExpression(lowExpr)
);
}

Expand Down Expand Up @@ -1102,9 +1114,15 @@ public void SetExpressionAttributes(MediumLevelILExpressionIndex expr , uint att
NativeMethods.BNSetMediumLevelILExprAttributes(this.handle , expr , attributes);
}

public MediumLevelILInstructionIndex AddInstruction(MediumLevelILExpressionIndex expr)
public MediumLevelILInstructionIndex AddInstruction(
MediumLevelILExpressionIndex expr,
SourceLocation? location = null)
{
return NativeMethods.BNMediumLevelILAddInstruction(this.handle , expr);
MediumLevelILInstructionIndex result =
NativeMethods.BNMediumLevelILAddInstruction(this.handle , expr);
this.RecordInstructionTranslation(result, location);

return result;
}

public PossibleValueSet GetSSAVariablePossibleValues(
Expand Down Expand Up @@ -1555,6 +1573,7 @@ public MediumLevelILExpressionIndex AddExpression(
params ulong[] operands
)
{
MediumLevelILExpressionIndex result;
ulong a = 0;
ulong b = 0;
ulong c = 0;
Expand Down Expand Up @@ -1588,7 +1607,7 @@ params ulong[] operands

if (null == location)
{
return NativeMethods.BNMediumLevelILAddExpr(
result = NativeMethods.BNMediumLevelILAddExpr(
this.handle ,
operation,
size ,
Expand All @@ -1601,7 +1620,7 @@ params ulong[] operands
}
else
{
return NativeMethods.BNMediumLevelILAddExprWithLocation(
result = NativeMethods.BNMediumLevelILAddExprWithLocation(
this.handle ,
operation,
location.Address,
Expand All @@ -1614,7 +1633,10 @@ params ulong[] operands
e
);
}


this.RecordExpressionTranslation(result, location);

return result;
}

public MediumLevelILExpressionIndex EmitNop(SourceLocation? location = null)
Expand Down
92 changes: 92 additions & 0 deletions IL/SourceLocation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,26 @@ public sealed class SourceLocation : IEquatable<SourceLocation>,

public OperandIndex Operand { get; } = 0;

/// <summary>
/// Gets the source LLIL instruction when this location was derived from LLIL.
/// </summary>
public LowLevelILInstruction? SourceLowLevelILInstruction { get; private set; }

/// <summary>
/// Gets the source MLIL instruction when this location was derived from MLIL.
/// </summary>
public MediumLevelILInstruction? SourceMediumLevelILInstruction { get; private set; }

/// <summary>
/// Gets the source HLIL instruction when this location was derived from HLIL.
/// </summary>
public HighLevelILInstruction? SourceHighLevelILInstruction { get; private set; }

/// <summary>
/// Gets whether the source IL instruction maps directly to the generated expression.
/// </summary>
public bool ILDirect { get; private set; } = true;

public SourceLocation()
{

Expand All @@ -19,6 +39,78 @@ public SourceLocation(ulong address, OperandIndex operand)
this.Address = address;
this.Operand = operand;
}

private SourceLocation(
ulong address,
OperandIndex operand,
bool ilDirect)
: this(address, operand)
{
this.ILDirect = ilDirect;
}

/// <summary>
/// Creates a source location derived from an LLIL instruction.
/// </summary>
public static SourceLocation FromInstruction(
LowLevelILInstruction instruction,
bool ilDirect = true)
{
if (null == instruction)
{
throw new ArgumentNullException(nameof(instruction));
}

SourceLocation result = new SourceLocation(
instruction.Address,
instruction.SourceOperand,
ilDirect);
result.SourceLowLevelILInstruction = instruction;

return result;
}

/// <summary>
/// Creates a source location derived from an MLIL instruction.
/// </summary>
public static SourceLocation FromInstruction(
MediumLevelILInstruction instruction,
bool ilDirect = true)
{
if (null == instruction)
{
throw new ArgumentNullException(nameof(instruction));
}

SourceLocation result = new SourceLocation(
instruction.Address,
instruction.SourceOperand,
ilDirect);
result.SourceMediumLevelILInstruction = instruction;

return result;
}

/// <summary>
/// Creates a source location derived from an HLIL instruction.
/// </summary>
public static SourceLocation FromInstruction(
HighLevelILInstruction instruction,
bool ilDirect = true)
{
if (null == instruction)
{
throw new ArgumentNullException(nameof(instruction));
}

SourceLocation result = new SourceLocation(
instruction.Address,
instruction.SourceOperand,
ilDirect);
result.SourceHighLevelILInstruction = instruction;

return result;
}

public override bool Equals(object? other)
{
Expand Down
Loading
Loading