Skip to content

Commit

Permalink
Implement underlying work for arm64 attribute restoration
Browse files Browse the repository at this point in the history
  • Loading branch information
Sam Byass committed Oct 20, 2021
1 parent e7a7b8d commit 3e4268b
Show file tree
Hide file tree
Showing 7 changed files with 128 additions and 75 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ namespace Cpp2IL.Core.Analysis.Actions.ARM64
{
public class Arm64ImmediateToFieldAction : AbstractFieldWriteAction<Arm64Instruction>
{
private long _immValue;
public readonly long ImmValue;

public Arm64ImmediateToFieldAction(MethodAnalysis<Arm64Instruction> context, Arm64Instruction instruction) : base(context, instruction)
{
var memReg = Utils.GetRegisterNameNew(instruction.MemoryBase()!.Id);
InstanceBeingSetOn = context.GetLocalInReg(memReg);

_immValue = instruction.Details.Operands[0].Immediate;
ImmValue = instruction.Details.Operands[0].Immediate;

if(InstanceBeingSetOn?.Type == null)
return;
Expand All @@ -24,13 +24,13 @@ public Arm64ImmediateToFieldAction(MethodAnalysis<Arm64Instruction> context, Arm
FieldWritten = FieldUtils.GetFieldBeingAccessed(InstanceBeingSetOn.Type, (ulong)instruction.MemoryOffset(), false);
}

protected override string? GetValueSummary() => _immValue.ToString();
protected override string? GetValueSummary() => ImmValue.ToString();

protected override string? GetValuePseudocode() => _immValue.ToString();
protected override string? GetValuePseudocode() => ImmValue.ToString();

protected override Instruction[] GetIlToLoadValue(MethodAnalysis<Arm64Instruction> context, ILProcessor processor) => new[]
{
processor.Create(OpCodes.Ldc_I4, (int) _immValue),
processor.Create(OpCodes.Ldc_I4, (int) ImmValue),
};
}
}
12 changes: 6 additions & 6 deletions Cpp2IL.Core/Analysis/Actions/ARM64/Arm64RegisterToFieldAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,31 +8,31 @@ namespace Cpp2IL.Core.Analysis.Actions.ARM64
{
public class Arm64RegisterToFieldAction : AbstractFieldWriteAction<Arm64Instruction>
{
private IAnalysedOperand? _sourceOperand;
public readonly IAnalysedOperand? SourceOperand;

public Arm64RegisterToFieldAction(MethodAnalysis<Arm64Instruction> context, Arm64Instruction instruction) : base(context, instruction)
{
var memReg = Utils.GetRegisterNameNew(instruction.MemoryBase()!.Id);
InstanceBeingSetOn = context.GetLocalInReg(memReg);

var sourceReg = Utils.GetRegisterNameNew(instruction.Details.Operands[0].Register.Id);
_sourceOperand = context.GetOperandInRegister(sourceReg);
SourceOperand = context.GetOperandInRegister(sourceReg);

if(InstanceBeingSetOn?.Type == null)
return;

RegisterUsedLocal(InstanceBeingSetOn, context);

if(_sourceOperand is LocalDefinition l)
if(SourceOperand is LocalDefinition l)
RegisterUsedLocal(l, context);

FieldWritten = FieldUtils.GetFieldBeingAccessed(InstanceBeingSetOn.Type, (ulong)instruction.MemoryOffset(), sourceReg[0] == 'v');
}

protected override string? GetValueSummary() => _sourceOperand?.ToString();
protected override string? GetValueSummary() => SourceOperand?.ToString();

protected override string? GetValuePseudocode() => _sourceOperand?.GetPseudocodeRepresentation();
protected override string? GetValuePseudocode() => SourceOperand?.GetPseudocodeRepresentation();

protected override Instruction[] GetIlToLoadValue(MethodAnalysis<Arm64Instruction> context, ILProcessor processor) => _sourceOperand?.GetILToLoad(context, processor) ?? Array.Empty<Instruction>();
protected override Instruction[] GetIlToLoadValue(MethodAnalysis<Arm64Instruction> context, ILProcessor processor) => SourceOperand?.GetILToLoad(context, processor) ?? Array.Empty<Instruction>();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using Cpp2IL.Core.Analysis.ResultModels;
using Mono.Cecil;
using Mono.Cecil.Cil;

namespace Cpp2IL.Core.Analysis.Actions.Base
{
public abstract class AbstractAttributeLoadFromListAction<T> : BaseAction<T>
{
public LocalDefinition? LocalMade;
public long OffsetInList;
protected TypeDefinition? _attributeType;

protected AbstractAttributeLoadFromListAction(MethodAnalysis<T> context, T instruction) : base(context, instruction) { }

public sealed override Instruction[] ToILInstructions(MethodAnalysis<T> context, ILProcessor processor) => throw new System.InvalidOperationException("Should not be attempting to generate IL for this type of instruction!");

public sealed override string? ToPsuedoCode() => throw new System.InvalidOperationException("Should not be attempting to generate pseudocode for this type of instruction!");

public sealed override string ToTextSummary() => $"[!] Loads the attribute instance at offset {OffsetInList} which is of type {_attributeType}, and stores in new local {LocalMade}";

public sealed override bool IsImportant() => false;

public sealed override bool PseudocodeNeedsLinebreakBefore() => false;
}
}
Original file line number Diff line number Diff line change
@@ -1,44 +1,23 @@
using System.Collections.Generic;
using Cpp2IL.Core.Analysis.Actions.Base;
using Cpp2IL.Core.Analysis.ResultModels;
using Iced.Intel;
using LibCpp2IL;
using Mono.Cecil;
using Mono.Cecil.Cil;
using Instruction = Iced.Intel.Instruction;

namespace Cpp2IL.Core.Analysis.Actions.x86
{
public class LoadAttributeFromAttributeListAction : BaseAction<Instruction>
public class LoadAttributeFromAttributeListAction : AbstractAttributeLoadFromListAction<Instruction>
{
public LocalDefinition? LocalMade;
private string? _destReg;
public TypeDefinition? AttributeType;
public long OffsetInList;

public LoadAttributeFromAttributeListAction(MethodAnalysis<Instruction> context, Instruction instruction, List<TypeDefinition> attributes) : base(context, instruction)
{
var ptrSize = LibCpp2IlMain.Binary!.is32Bit ? 4 : 8;
OffsetInList = instruction.MemoryDisplacement32 / ptrSize;

AttributeType = attributes[(int) OffsetInList];

_destReg = Utils.GetRegisterNameNew(instruction.Op0Register);
LocalMade = context.MakeLocal(AttributeType, reg: _destReg);
}

public override Mono.Cecil.Cil.Instruction[] ToILInstructions(MethodAnalysis<Instruction> context, ILProcessor processor)
{
throw new System.NotImplementedException();
}

public override string? ToPsuedoCode()
{
throw new System.NotImplementedException();
}
_attributeType = attributes[(int) OffsetInList];

public override string ToTextSummary()
{
return $"[!] Loads the attribute instance at offset {OffsetInList} which is of type {AttributeType}, and stores in new local {LocalMade} in {_destReg}";
var destReg = Utils.GetRegisterNameNew(instruction.Op0Register);
LocalMade = context.MakeLocal(_attributeType, reg: destReg);
}
}
}
1 change: 1 addition & 0 deletions Cpp2IL.Core/Analysis/IAsmAnalyzer.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Text;
using Cpp2IL.Core.Analysis.Actions.Base;

namespace Cpp2IL.Core.Analysis
{
Expand Down
Loading

0 comments on commit 3e4268b

Please sign in to comment.