Port more code
This commit is contained in:
parent
04e1568c61
commit
301a2fab97
|
@ -19,7 +19,7 @@
|
||||||
|
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using Mono.Cecil.Cil;
|
using dot10.DotNet.Emit;
|
||||||
|
|
||||||
namespace de4dot.blocks {
|
namespace de4dot.blocks {
|
||||||
public class Block : BaseBlock {
|
public class Block : BaseBlock {
|
||||||
|
@ -269,7 +269,7 @@ namespace de4dot.blocks {
|
||||||
for (int i = 0; i < instrs.Count; i++) {
|
for (int i = 0; i < instrs.Count; i++) {
|
||||||
var instr = instrs[i];
|
var instr = instrs[i];
|
||||||
if (instr.OpCode != OpCodes.Nop)
|
if (instr.OpCode != OpCodes.Nop)
|
||||||
dest.Add(clone ? new Instr(DotNetUtils.clone(instr.Instruction)) : instr);
|
dest.Add(clone ? new Instr(instr.Instruction.Clone()) : instr);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -19,35 +19,35 @@
|
||||||
|
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using Mono.Cecil;
|
using dot10.DotNet;
|
||||||
using Mono.Cecil.Cil;
|
using dot10.DotNet.Emit;
|
||||||
|
|
||||||
namespace de4dot.blocks {
|
namespace de4dot.blocks {
|
||||||
public class Blocks {
|
public class Blocks {
|
||||||
MethodDefinition method;
|
MethodDef method;
|
||||||
IList<VariableDefinition> locals;
|
IList<Local> locals;
|
||||||
MethodBlocks methodBlocks;
|
MethodBlocks methodBlocks;
|
||||||
|
|
||||||
public MethodBlocks MethodBlocks {
|
public MethodBlocks MethodBlocks {
|
||||||
get { return methodBlocks; }
|
get { return methodBlocks; }
|
||||||
}
|
}
|
||||||
|
|
||||||
public IList<VariableDefinition> Locals {
|
public IList<Local> Locals {
|
||||||
get { return locals; }
|
get { return locals; }
|
||||||
}
|
}
|
||||||
|
|
||||||
public MethodDefinition Method {
|
public MethodDef Method {
|
||||||
get { return method; }
|
get { return method; }
|
||||||
}
|
}
|
||||||
|
|
||||||
public Blocks(MethodDefinition method) {
|
public Blocks(MethodDef method) {
|
||||||
this.method = method;
|
this.method = method;
|
||||||
updateBlocks();
|
updateBlocks();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void updateBlocks() {
|
public void updateBlocks() {
|
||||||
var body = method.Body;
|
var body = method.CilBody;
|
||||||
locals = body.Variables;
|
locals = body.LocalList;
|
||||||
methodBlocks = new InstructionListParser(body.Instructions, body.ExceptionHandlers).parse();
|
methodBlocks = new InstructionListParser(body.Instructions, body.ExceptionHandlers).parse();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -79,11 +79,11 @@ namespace de4dot.blocks {
|
||||||
if (locals.Count == 0)
|
if (locals.Count == 0)
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
var usedLocals = new Dictionary<VariableDefinition, List<LocalVariableInfo>>();
|
var usedLocals = new Dictionary<Local, List<LocalVariableInfo>>();
|
||||||
foreach (var block in methodBlocks.getAllBlocks()) {
|
foreach (var block in methodBlocks.getAllBlocks()) {
|
||||||
for (int i = 0; i < block.Instructions.Count; i++) {
|
for (int i = 0; i < block.Instructions.Count; i++) {
|
||||||
var instr = block.Instructions[i];
|
var instr = block.Instructions[i];
|
||||||
VariableDefinition local;
|
Local local;
|
||||||
switch (instr.OpCode.Code) {
|
switch (instr.OpCode.Code) {
|
||||||
case Code.Ldloc:
|
case Code.Ldloc:
|
||||||
case Code.Ldloc_S:
|
case Code.Ldloc_S:
|
||||||
|
@ -102,7 +102,7 @@ namespace de4dot.blocks {
|
||||||
|
|
||||||
case Code.Ldloca_S:
|
case Code.Ldloca_S:
|
||||||
case Code.Ldloca:
|
case Code.Ldloca:
|
||||||
local = (VariableDefinition)instr.Operand;
|
local = (Local)instr.Operand;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
|
@ -122,7 +122,7 @@ namespace de4dot.blocks {
|
||||||
}
|
}
|
||||||
|
|
||||||
int newIndex = -1;
|
int newIndex = -1;
|
||||||
var newLocals = new List<VariableDefinition>(usedLocals.Count);
|
var newLocals = new List<Local>(usedLocals.Count);
|
||||||
foreach (var local in usedLocals.Keys) {
|
foreach (var local in usedLocals.Keys) {
|
||||||
newIndex++;
|
newIndex++;
|
||||||
newLocals.Add(local);
|
newLocals.Add(local);
|
||||||
|
@ -137,7 +137,7 @@ namespace de4dot.blocks {
|
||||||
return numRemoved;
|
return numRemoved;
|
||||||
}
|
}
|
||||||
|
|
||||||
static Instruction optimizeLocalInstr(Instr instr, VariableDefinition local, uint newIndex) {
|
static Instruction optimizeLocalInstr(Instr instr, Local local, uint newIndex) {
|
||||||
switch (instr.OpCode.Code) {
|
switch (instr.OpCode.Code) {
|
||||||
case Code.Ldloc:
|
case Code.Ldloc:
|
||||||
case Code.Ldloc_S:
|
case Code.Ldloc_S:
|
||||||
|
@ -194,7 +194,7 @@ namespace de4dot.blocks {
|
||||||
}
|
}
|
||||||
catch (NullReferenceException) {
|
catch (NullReferenceException) {
|
||||||
//TODO: Send this message to the log
|
//TODO: Send this message to the log
|
||||||
Console.WriteLine("Null ref exception! Invalid metadata token in code? Method: {0:X8}: {1}", method.MetadataToken.ToUInt32(), method.FullName);
|
Console.WriteLine("Null ref exception! Invalid metadata token in code? Method: {0:X8}: {1}", method.MDToken.Raw, method.FullName);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -19,8 +19,8 @@
|
||||||
|
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using Mono.Cecil;
|
using dot10.DotNet;
|
||||||
using Mono.Cecil.Cil;
|
using dot10.DotNet.Emit;
|
||||||
|
|
||||||
namespace de4dot.blocks {
|
namespace de4dot.blocks {
|
||||||
class CodeGenerator {
|
class CodeGenerator {
|
||||||
|
@ -45,11 +45,11 @@ namespace de4dot.blocks {
|
||||||
public int filterStart;
|
public int filterStart;
|
||||||
public int handlerStart;
|
public int handlerStart;
|
||||||
public int handlerEnd;
|
public int handlerEnd;
|
||||||
public TypeReference catchType;
|
public ITypeDefOrRef catchType;
|
||||||
public ExceptionHandlerType handlerType;
|
public ExceptionClause handlerType;
|
||||||
public ExceptionInfo(int tryStart, int tryEnd, int filterStart,
|
public ExceptionInfo(int tryStart, int tryEnd, int filterStart,
|
||||||
int handlerStart, int handlerEnd, TypeReference catchType,
|
int handlerStart, int handlerEnd, ITypeDefOrRef catchType,
|
||||||
ExceptionHandlerType handlerType) {
|
ExceptionClause handlerType) {
|
||||||
if (tryStart > tryEnd || filterStart > handlerStart ||
|
if (tryStart > tryEnd || filterStart > handlerStart ||
|
||||||
tryStart < 0 || tryEnd < 0 || filterStart < 0 || handlerStart < 0 || handlerEnd < 0)
|
tryStart < 0 || tryEnd < 0 || filterStart < 0 || handlerStart < 0 || handlerEnd < 0)
|
||||||
throw new ApplicationException("Invalid start/end/filter/handler indexes");
|
throw new ApplicationException("Invalid start/end/filter/handler indexes");
|
||||||
|
@ -99,10 +99,10 @@ namespace de4dot.blocks {
|
||||||
}
|
}
|
||||||
|
|
||||||
void recalculateInstructionOffsets(IList<Instruction> allInstructions) {
|
void recalculateInstructionOffsets(IList<Instruction> allInstructions) {
|
||||||
int offset = 0;
|
uint offset = 0;
|
||||||
foreach (var instr in allInstructions) {
|
foreach (var instr in allInstructions) {
|
||||||
instr.Offset = offset;
|
instr.Offset = offset;
|
||||||
offset += instr.GetSize();
|
offset += (uint)instr.GetSize();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -137,7 +137,7 @@ namespace de4dot.blocks {
|
||||||
if (getShortBranch(instruction, out opcode)) {
|
if (getShortBranch(instruction, out opcode)) {
|
||||||
const int instrSize = 5; // It's a long branch instruction
|
const int instrSize = 5; // It's a long branch instruction
|
||||||
var target = (Instruction)instruction.Operand;
|
var target = (Instruction)instruction.Operand;
|
||||||
int distance = target == null ? int.MaxValue : target.Offset - (instruction.Offset + instrSize);
|
int distance = target == null ? int.MaxValue : (int)(target.Offset - (instruction.Offset + instrSize));
|
||||||
if (-0x80 <= distance && distance <= 0x7F) {
|
if (-0x80 <= distance && distance <= 0x7F) {
|
||||||
instruction.OpCode = opcode;
|
instruction.OpCode = opcode;
|
||||||
changed = true;
|
changed = true;
|
||||||
|
|
|
@ -52,7 +52,7 @@ namespace de4dot.blocks {
|
||||||
|
|
||||||
int stack = stackStart;
|
int stack = stackStart;
|
||||||
foreach (var instr in block.Instructions)
|
foreach (var instr in block.Instructions)
|
||||||
DotNetUtils.updateStack(instr.Instruction, ref stack, false);
|
instr.Instruction.UpdateStack(ref stack, false);
|
||||||
stackEnd = stack;
|
stackEnd = stack;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -18,8 +18,8 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
using System;
|
using System;
|
||||||
using Mono.Cecil.Cil;
|
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using dot10.DotNet.Emit;
|
||||||
|
|
||||||
namespace de4dot.blocks {
|
namespace de4dot.blocks {
|
||||||
public class Instr {
|
public class Instr {
|
||||||
|
@ -44,8 +44,8 @@ namespace de4dot.blocks {
|
||||||
|
|
||||||
// Returns the variable or null if it's not a ldloc/stloc instruction. It does not return
|
// Returns the variable or null if it's not a ldloc/stloc instruction. It does not return
|
||||||
// a local variable if it's a ldloca/ldloca.s instruction.
|
// a local variable if it's a ldloca/ldloca.s instruction.
|
||||||
public static VariableDefinition getLocalVar(IList<VariableDefinition> locals, Instr instr) {
|
public static Local getLocalVar(IList<Local> locals, Instr instr) {
|
||||||
return DotNetUtils.getLocalVar(locals, instr.Instruction);
|
return instr.Instruction.GetLocal(locals);
|
||||||
}
|
}
|
||||||
|
|
||||||
static public bool isFallThrough(OpCode opCode) {
|
static public bool isFallThrough(OpCode opCode) {
|
||||||
|
@ -104,23 +104,23 @@ namespace de4dot.blocks {
|
||||||
}
|
}
|
||||||
|
|
||||||
public bool isLdcI4() {
|
public bool isLdcI4() {
|
||||||
return DotNetUtils.isLdcI4(OpCode.Code);
|
return instruction.IsLdcI4();
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getLdcI4Value() {
|
public int getLdcI4Value() {
|
||||||
return DotNetUtils.getLdcI4Value(instruction);
|
return instruction.GetLdcI4Value();
|
||||||
}
|
}
|
||||||
|
|
||||||
public bool isLdarg() {
|
public bool isLdarg() {
|
||||||
return DotNetUtils.isLdarg(instruction);
|
return instruction.IsLdarg();
|
||||||
}
|
}
|
||||||
|
|
||||||
public bool isStloc() {
|
public bool isStloc() {
|
||||||
return DotNetUtils.isStloc(instruction);
|
return instruction.IsStloc();
|
||||||
}
|
}
|
||||||
|
|
||||||
public bool isLdloc() {
|
public bool isLdloc() {
|
||||||
return DotNetUtils.isLdloc(instruction);
|
return instruction.IsLdloc();
|
||||||
}
|
}
|
||||||
|
|
||||||
public bool isNop() {
|
public bool isNop() {
|
||||||
|
@ -132,23 +132,23 @@ namespace de4dot.blocks {
|
||||||
}
|
}
|
||||||
|
|
||||||
public bool isLeave() {
|
public bool isLeave() {
|
||||||
return DotNetUtils.isLeave(instruction);
|
return instruction.IsLeave();
|
||||||
}
|
}
|
||||||
|
|
||||||
public bool isBr() {
|
public bool isBr() {
|
||||||
return DotNetUtils.isBr(instruction);
|
return instruction.IsBr();
|
||||||
}
|
}
|
||||||
|
|
||||||
public bool isBrfalse() {
|
public bool isBrfalse() {
|
||||||
return DotNetUtils.isBrfalse(instruction);
|
return instruction.IsBrfalse();
|
||||||
}
|
}
|
||||||
|
|
||||||
public bool isBrtrue() {
|
public bool isBrtrue() {
|
||||||
return DotNetUtils.isBrtrue(instruction);
|
return instruction.IsBrtrue();
|
||||||
}
|
}
|
||||||
|
|
||||||
public bool isConditionalBranch() {
|
public bool isConditionalBranch() {
|
||||||
return DotNetUtils.isConditionalBranch(OpCode.Code);
|
return instruction.IsConditionalBranch();
|
||||||
}
|
}
|
||||||
|
|
||||||
public bool getFlippedBranchOpCode(out OpCode opcode) {
|
public bool getFlippedBranchOpCode(out OpCode opcode) {
|
||||||
|
|
|
@ -19,7 +19,7 @@
|
||||||
|
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using Mono.Cecil.Cil;
|
using dot10.DotNet.Emit;
|
||||||
|
|
||||||
namespace de4dot.blocks {
|
namespace de4dot.blocks {
|
||||||
class InstructionListParser {
|
class InstructionListParser {
|
||||||
|
|
|
@ -19,7 +19,7 @@
|
||||||
|
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using Mono.Cecil.Cil;
|
using dot10.DotNet.Emit;
|
||||||
|
|
||||||
namespace de4dot.blocks {
|
namespace de4dot.blocks {
|
||||||
// A normal branch may not transfer out of a protected block (try block), filter handler,
|
// A normal branch may not transfer out of a protected block (try block), filter handler,
|
||||||
|
|
|
@ -17,8 +17,8 @@
|
||||||
along with de4dot. If not, see <http://www.gnu.org/licenses/>.
|
along with de4dot. If not, see <http://www.gnu.org/licenses/>.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
using Mono.Cecil;
|
using dot10.DotNet;
|
||||||
using Mono.Cecil.Cil;
|
using dot10.DotNet.Emit;
|
||||||
|
|
||||||
namespace de4dot.blocks {
|
namespace de4dot.blocks {
|
||||||
// Contains the filter handler block and the catch handler block.
|
// Contains the filter handler block and the catch handler block.
|
||||||
|
@ -27,14 +27,14 @@ namespace de4dot.blocks {
|
||||||
HandlerBlock handlerBlock = new HandlerBlock();
|
HandlerBlock handlerBlock = new HandlerBlock();
|
||||||
|
|
||||||
// State for an ExceptionHandler instance
|
// State for an ExceptionHandler instance
|
||||||
TypeReference catchType;
|
ITypeDefOrRef catchType;
|
||||||
ExceptionHandlerType handlerType;
|
ExceptionClause handlerType;
|
||||||
|
|
||||||
public TypeReference CatchType {
|
public ITypeDefOrRef CatchType {
|
||||||
get { return catchType; }
|
get { return catchType; }
|
||||||
}
|
}
|
||||||
|
|
||||||
public ExceptionHandlerType HandlerType {
|
public ExceptionClause HandlerType {
|
||||||
get { return handlerType; }
|
get { return handlerType; }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
2
dot10
2
dot10
|
@ -1 +1 @@
|
||||||
Subproject commit ec48311025fd4e9a56da7881857d90eaa0e3b5ca
|
Subproject commit 526604c81df777accde4e45327cffcba954c356c
|
Loading…
Reference in New Issue
Block a user