private void NewExceptionHandlerEditor(EditExceptionHandlerMode mode) { NewExceptionHandler = null; _editExceptionHandlerMode = mode; DataGridViewSelectedRowCollection selectedRows = dgBody.SelectedRows; if (selectedRows.Count > 0) { EditedExceptionHandlerIndex = dgBody.SelectedRows.TopmostRow().Index - CurrentAssembly.Method.Body.Instructions.Count; } if (mode == EditExceptionHandlerMode.Edit) { var form = new EditExceptionHandlerForm( CurrentAssembly.Method.Body.ExceptionHandlers[ (dgBody.SelectedRows.TopmostRow().Index - CurrentAssembly.Method.Body.Instructions.Count)]); form.FormClosed += EditExceptionHandlerForm_FormClosed; form.ShowDialog(); } else { var form = new EditExceptionHandlerForm(); form.FormClosed += EditExceptionHandlerForm_FormClosed; form.ShowDialog(); } }
public ExceptionHandlerOptions(Dictionary<object, object> ops, ExceptionHandler eh) { this.TryStart = (InstructionVM)BodyUtils.TryGetVM(ops, eh.TryStart); this.TryEnd = (InstructionVM)BodyUtils.TryGetVM(ops, eh.TryEnd); this.FilterStart = (InstructionVM)BodyUtils.TryGetVM(ops, eh.FilterStart); this.HandlerStart = (InstructionVM)BodyUtils.TryGetVM(ops, eh.HandlerStart); this.HandlerEnd = (InstructionVM)BodyUtils.TryGetVM(ops, eh.HandlerEnd); this.CatchType = eh.CatchType; this.HandlerType = eh.HandlerType; }
public EditExceptionHandlerForm(ExceptionHandler exceptionHandler) { InitializeComponent(); cbHandlerType.SelectedIndex = 0; _exceptionHandler = exceptionHandler; ProcessComboBoxes(); RestoreExceptionHandler(); }
public ExceptionHandler CopyTo(Dictionary<object, object> ops, ExceptionHandler eh) { eh.TryStart = BodyUtils.TryGetModel(ops, this.TryStart) as Instruction; eh.TryEnd = BodyUtils.TryGetModel(ops, this.TryEnd) as Instruction; eh.FilterStart = BodyUtils.TryGetModel(ops, this.FilterStart) as Instruction; eh.HandlerStart = BodyUtils.TryGetModel(ops, this.HandlerStart) as Instruction; eh.HandlerEnd = BodyUtils.TryGetModel(ops, this.HandlerEnd) as Instruction; eh.CatchType = this.CatchType; eh.HandlerType = this.HandlerType; return eh; }
private void btnOk_Click(object sender, EventArgs e) { if (!SelectionsOk()) return; var exceptionHandler = new ExceptionHandler(); switch (cbHandlerType.Text) { case "Catch": exceptionHandler.HandlerType = ExceptionHandlerType.Catch; break; case "Duplicated": exceptionHandler.HandlerType = ExceptionHandlerType.Duplicated; break; case "Fault": exceptionHandler.HandlerType = ExceptionHandlerType.Fault; break; case "Filter": exceptionHandler.HandlerType = ExceptionHandlerType.Filter; break; case "Finally": exceptionHandler.HandlerType = ExceptionHandlerType.Finally; break; } exceptionHandler.TryStart = MainForm.CurrentAssembly.Method.Body.Instructions[cbTryStart.SelectedIndex]; exceptionHandler.TryEnd = MainForm.CurrentAssembly.Method.Body.Instructions[cbTryEnd.SelectedIndex]; exceptionHandler.HandlerStart = MainForm.CurrentAssembly.Method.Body.Instructions[cbHandlerStart.SelectedIndex]; exceptionHandler.HandlerEnd = MainForm.CurrentAssembly.Method.Body.Instructions[cbHandlerEnd.SelectedIndex]; if (exceptionHandler.HandlerType == ExceptionHandlerType.Catch) { if (cbCatchType.SelectedItem == null) return; exceptionHandler.CatchType = (ITypeDefOrRef) cbCatchType.SelectedItem; } else if (exceptionHandler.HandlerType == ExceptionHandlerType.Filter) { if (cbFilterStart.SelectedItem == null) return; exceptionHandler.FilterStart = MainForm.CurrentAssembly.Method.Body.Instructions[cbFilterStart.SelectedIndex]; } MainForm.NewExceptionHandler = exceptionHandler; Close(); }
string getExceptionString(ExceptionHandler ex) { var sb = new StringBuilder(); if (ex.TryStart != null) sb.Append(string.Format("TRY: {0}-{1}", getLabel(ex.TryStart), getLabel(ex.TryEnd))); if (ex.FilterStart != null) sb.Append(string.Format(", FILTER: {0}", getLabel(ex.FilterStart))); if (ex.HandlerStart != null) sb.Append(string.Format(", HANDLER: {0}-{1}", getLabel(ex.HandlerStart), getLabel(ex.HandlerEnd))); sb.Append(string.Format(", TYPE: {0}", ex.HandlerType)); if (ex.CatchType != null) sb.Append(string.Format(", CATCH: {0}", ex.CatchType)); return sb.ToString(); }
List<ExceptionHandler> ReadExceptions(MethodDef cilMethod, CsvmMethodData csvmMethod) { var reader = new BinaryReader(new MemoryStream(csvmMethod.Exceptions)); var ehs = new List<ExceptionHandler>(); if (reader.BaseStream.Length == 0) return ehs; int numExceptions = reader.ReadInt32(); if (numExceptions < 0) throw new ApplicationException("Invalid number of exception handlers"); var gpContext = GenericParamContext.Create(cilMethod); for (int i = 0; i < numExceptions; i++) { var eh = new ExceptionHandler((ExceptionHandlerType)reader.ReadInt32()); eh.TryStart = GetInstruction(reader.ReadInt32()); eh.TryEnd = GetInstructionEnd(reader.ReadInt32()); eh.HandlerStart = GetInstruction(reader.ReadInt32()); eh.HandlerEnd = GetInstructionEnd(reader.ReadInt32()); if (eh.HandlerType == ExceptionHandlerType.Catch) eh.CatchType = module.ResolveToken(reader.ReadUInt32(), gpContext) as ITypeDefOrRef; else if (eh.HandlerType == ExceptionHandlerType.Filter) eh.FilterStart = GetInstruction(reader.ReadInt32()); ehs.Add(eh); } return ehs; }
public ScopeBlock(BlockType type, ExceptionHandler handler) : base(type) { Handler = handler; Children = new List<BlockBase>(); }
private void NewExceptionHandlerEditor(EditExceptionHandlerMode mode) { NewExceptionHandler = null; _editExceptionHandlerMode = mode; DataGridViewSelectedRowCollection selectedRows = dgBody.SelectedRows; if (selectedRows.Count > 0) EditedExceptionHandlerIndex = dgBody.SelectedRows.TopmostRow().Index - CurrentAssembly.Method.Body.Instructions.Count; if (mode == EditExceptionHandlerMode.Edit) { var form = new EditExceptionHandlerForm( CurrentAssembly.Method.Body.ExceptionHandlers[ (dgBody.SelectedRows.TopmostRow().Index - CurrentAssembly.Method.Body.Instructions.Count)]); form.FormClosed += EditExceptionHandlerForm_FormClosed; form.ShowDialog(); } else { var form = new EditExceptionHandlerForm(); form.FormClosed += EditExceptionHandlerForm_FormClosed; form.ShowDialog(); } }
void ReadSmallExceptionHandlers(IBinaryReader ehReader) { int num = GetNumberOfExceptionHandlers((uint)ehReader.ReadByte() / 12); ehReader.Position += 2; for (int i = 0; i < num; i++) { var eh = new ExceptionHandler((ExceptionHandlerType)ehReader.ReadUInt16()); uint offs = ehReader.ReadUInt16(); eh.TryStart = GetInstruction(offs); eh.TryEnd = GetInstruction(offs + ehReader.ReadByte()); offs = ehReader.ReadUInt16(); eh.HandlerStart = GetInstruction(offs); eh.HandlerEnd = GetInstruction(offs + ehReader.ReadByte()); if (eh.HandlerType == ExceptionHandlerType.Catch) eh.CatchType = opResolver.ResolveToken(ehReader.ReadUInt32(), gpContext) as ITypeDefOrRef; else if (eh.HandlerType == ExceptionHandlerType.Filter) eh.FilterStart = GetInstruction(ehReader.ReadUInt32()); else ehReader.ReadUInt32(); Add(eh); } }
ExceptionHandler ReadExceptionHandler() { var eh = new ExceptionHandler((ExceptionHandlerType)reader.ReadUInt32()); uint tryOffset = reader.ReadUInt32(); eh.TryStart = GetInstructionThrow(tryOffset); eh.TryEnd = GetInstruction(tryOffset + reader.ReadUInt32()); uint handlerOffset = reader.ReadUInt32(); eh.HandlerStart = GetInstructionThrow(handlerOffset); eh.HandlerEnd = GetInstruction(handlerOffset + reader.ReadUInt32()); switch (eh.HandlerType) { case ExceptionHandlerType.Catch: eh.CatchType = module.ResolveToken(reader.ReadUInt32()) as ITypeDefOrRef; break; case ExceptionHandlerType.Filter: eh.FilterStart = GetInstructionThrow(reader.ReadUInt32()); break; case ExceptionHandlerType.Finally: case ExceptionHandlerType.Fault: default: reader.ReadUInt32(); break; } return eh; }
public ILStructure(ILStructureType type, int startOffset, int endOffset, ExceptionHandler handler = null) { Debug.Assert(startOffset < endOffset); this.Type = type; this.StartOffset = startOffset; this.EndOffset = endOffset; this.ExceptionHandler = handler; }
ExceptionHandler ReadExceptionHandler() { var ehType = (ExceptionHandlerType)reader.ReadByte(); uint tryOffset = imageReader.ReadVariableLengthUInt32(); uint tryLength = imageReader.ReadVariableLengthUInt32(); uint handlerOffset = imageReader.ReadVariableLengthUInt32(); uint handlerLength = imageReader.ReadVariableLengthUInt32(); var catchType = imageReader.ReadTypeSig().ToTypeDefOrRef(); uint filterOffset = imageReader.ReadVariableLengthUInt32(); var eh = new ExceptionHandler(ehType); eh.TryStart = GetInstructionThrow(tryOffset); eh.TryEnd = GetInstruction(tryOffset + tryLength); if (ehType == ExceptionHandlerType.Filter) eh.FilterStart = GetInstructionThrow(filterOffset); eh.HandlerStart = GetInstructionThrow(handlerOffset); eh.HandlerEnd = GetInstruction(handlerOffset + handlerLength); eh.CatchType = catchType; return eh; }
private void CreateExceptionHandlers() { bool flag = this.ehHeader != null && this.ehHeader.Length != 0; bool flag2 = flag; if (flag2) { BinaryReader binaryReader = new BinaryReader(new MemoryStream(this.ehHeader)); byte b = binaryReader.ReadByte(); bool flag3 = (b & 64) == 0; bool flag4 = flag3; if (flag4) { int num = (int)((ushort)((binaryReader.ReadByte() - 2) / 12)); binaryReader.ReadInt16(); for (int i = 0; i < num; i++) { dnlib.DotNet.Emit.ExceptionHandler exceptionHandler = new dnlib.DotNet.Emit.ExceptionHandler(); exceptionHandler.HandlerType = (ExceptionHandlerType)binaryReader.ReadInt16(); int num2 = (int)binaryReader.ReadUInt16(); exceptionHandler.TryStart = base.GetInstructionThrow((uint)num2); exceptionHandler.TryEnd = base.GetInstruction((uint)((int)binaryReader.ReadSByte() + num2)); num2 = (int)binaryReader.ReadUInt16(); exceptionHandler.HandlerStart = base.GetInstructionThrow((uint)num2); exceptionHandler.HandlerEnd = base.GetInstruction((uint)((int)binaryReader.ReadSByte() + num2)); bool flag5 = exceptionHandler.HandlerType == ExceptionHandlerType.Catch; bool flag6 = flag5; if (flag6) { exceptionHandler.CatchType = (this.ReadToken(binaryReader.ReadUInt32()) as ITypeDefOrRef); } else { bool flag7 = exceptionHandler.HandlerType == ExceptionHandlerType.Filter; bool flag8 = flag7; if (flag8) { exceptionHandler.FilterStart = base.GetInstruction(binaryReader.ReadUInt32()); } else { binaryReader.ReadUInt32(); } } this.exceptionHandlers.Add(exceptionHandler); } } else { Stream baseStream = binaryReader.BaseStream; long position = baseStream.Position; baseStream.Position = position - 1L; int num3 = (int)((ushort)(((binaryReader.ReadUInt32() >> 8) - 4U) / 24U)); for (int j = 0; j < num3; j++) { dnlib.DotNet.Emit.ExceptionHandler exceptionHandler2 = new dnlib.DotNet.Emit.ExceptionHandler(); exceptionHandler2.HandlerType = (ExceptionHandlerType)binaryReader.ReadInt32(); int num4 = binaryReader.ReadInt32(); exceptionHandler2.TryStart = base.GetInstructionThrow((uint)num4); exceptionHandler2.TryEnd = base.GetInstruction((uint)(binaryReader.ReadInt32() + num4)); num4 = binaryReader.ReadInt32(); exceptionHandler2.HandlerStart = base.GetInstructionThrow((uint)num4); exceptionHandler2.HandlerEnd = base.GetInstruction((uint)(binaryReader.ReadInt32() + num4)); bool flag9 = exceptionHandler2.HandlerType == ExceptionHandlerType.Catch; bool flag10 = flag9; if (flag10) { exceptionHandler2.CatchType = (this.ReadToken(binaryReader.ReadUInt32()) as ITypeDefOrRef); } else { bool flag11 = exceptionHandler2.HandlerType == ExceptionHandlerType.Filter; bool flag12 = flag11; if (flag12) { exceptionHandler2.FilterStart = base.GetInstruction(binaryReader.ReadUInt32()); } else { binaryReader.ReadUInt32(); } } this.exceptionHandlers.Add(exceptionHandler2); } } } else { bool flag13 = this.ehInfos != null; bool flag14 = flag13; if (flag14) { foreach (SuperDynamicReader.ExceptionInfo exceptionInfo in SuperDynamicReader.CreateExceptionInfos(this.ehInfos)) { Instruction instructionThrow = base.GetInstructionThrow((uint)exceptionInfo.StartAddr); Instruction instruction = base.GetInstruction((uint)exceptionInfo.EndAddr); Instruction instruction2 = (exceptionInfo.EndFinally < 0) ? null : base.GetInstruction((uint)exceptionInfo.EndFinally); for (int k = 0; k < exceptionInfo.CurrentCatch; k++) { dnlib.DotNet.Emit.ExceptionHandler exceptionHandler3 = new dnlib.DotNet.Emit.ExceptionHandler(); exceptionHandler3.HandlerType = (ExceptionHandlerType)exceptionInfo.Type[k]; exceptionHandler3.TryStart = instructionThrow; exceptionHandler3.TryEnd = ((exceptionHandler3.HandlerType == ExceptionHandlerType.Finally) ? instruction2 : instruction); exceptionHandler3.FilterStart = null; exceptionHandler3.HandlerStart = base.GetInstructionThrow((uint)exceptionInfo.CatchAddr[k]); exceptionHandler3.HandlerEnd = base.GetInstruction((uint)exceptionInfo.CatchEndAddr[k]); exceptionHandler3.CatchType = this.importer.Import(exceptionInfo.CatchClass[k]); this.exceptionHandlers.Add(exceptionHandler3); } } } } }
void LayOutInstructions(out IList<Instruction> allInstructions, out IList<ExceptionHandler> allExceptionHandlers) { allInstructions = new List<Instruction>(); allExceptionHandlers = new List<ExceptionHandler>(); var blockInfos = new List<BlockInfo>(); for (int i = 0; i < blocks.Count; i++) { var block = blocks[i]; int startIndex = allInstructions.Count; for (int j = 0; j < block.Instructions.Count - 1; j++) allInstructions.Add(block.Instructions[j].Instruction); if (block.Targets != null) { var targets = new List<Instr>(); foreach (var target in block.Targets) targets.Add(target.FirstInstr); block.LastInstr.UpdateTargets(targets); } allInstructions.Add(block.LastInstr.Instruction); var next = i + 1 < blocks.Count ? blocks[i + 1] : null; // If eg. ble next, then change it to bgt XYZ and fall through to next. if (block.Targets != null && block.CanFlipConditionalBranch() && block.Targets[0] == next) { block.FlipConditionalBranch(); block.LastInstr.UpdateTargets(new List<Instr> { block.Targets[0].FirstInstr }); } else if (block.FallThrough != null && block.FallThrough != next) { var instr = new Instr(OpCodes.Br.ToInstruction(block.FallThrough.FirstInstr.Instruction)); instr.UpdateTargets(new List<Instr> { block.FallThrough.FirstInstr }); allInstructions.Add(instr.Instruction); } int endIndex = allInstructions.Count - 1; blockInfos.Add(new BlockInfo(startIndex, endIndex)); } foreach (var ex in exceptions) { var tryStart = GetBlockInfo(blockInfos, ex.tryStart).start; var tryEnd = GetBlockInfo(blockInfos, ex.tryEnd).end; var filterStart = ex.filterStart == -1 ? -1 : GetBlockInfo(blockInfos, ex.filterStart).start; var handlerStart = GetBlockInfo(blockInfos, ex.handlerStart).start; var handlerEnd = GetBlockInfo(blockInfos, ex.handlerEnd).end; var eh = new ExceptionHandler(ex.handlerType); eh.CatchType = ex.catchType; eh.TryStart = GetInstruction(allInstructions, tryStart); eh.TryEnd = GetInstruction(allInstructions, tryEnd + 1); eh.FilterStart = filterStart == -1 ? null : GetInstruction(allInstructions, filterStart); eh.HandlerStart = GetInstruction(allInstructions, handlerStart); eh.HandlerEnd = GetInstruction(allInstructions, handlerEnd + 1); allExceptionHandlers.Add(eh); } }
void ReadFatExceptionHandlers(IBinaryReader ehReader) { ehReader.Position--; int num = GetNumberOfExceptionHandlers((ehReader.ReadUInt32() >> 8) / 24); for (int i = 0; i < num; i++) { var eh = new ExceptionHandler((ExceptionHandlerType)ehReader.ReadUInt32()); uint offs = ehReader.ReadUInt32(); eh.TryStart = GetInstruction(offs); eh.TryEnd = GetInstruction(offs + ehReader.ReadUInt32()); offs = ehReader.ReadUInt32(); eh.HandlerStart = GetInstruction(offs); eh.HandlerEnd = GetInstruction(offs + ehReader.ReadUInt32()); if (eh.HandlerType == ExceptionHandlerType.Catch) eh.CatchType = opResolver.ResolveToken(ehReader.ReadUInt32()) as ITypeDefOrRef; else if (eh.HandlerType == ExceptionHandlerType.Filter) eh.FilterStart = GetInstruction(ehReader.ReadUInt32()); else ehReader.ReadUInt32(); Add(eh); } }
public void Obfuscating(ref ModuleDef moduleDef, MethodDef[] methodDef) { foreach (var methodDef_Enumerator in methodDef) { if (methodDef_Enumerator.Body == null) { continue; } Importer importer = new Importer(moduleDef); ITypeDefOrRef int32Import = importer.Import(typeof(System.Int32)); ITypeDefOrRef booleanImport = importer.Import(typeof(System.Boolean)); Local int32Importing = new Local(int32Import.ToTypeSig()); Local booleanImporting = new Local(booleanImport.ToTypeSig()); methodDef_Enumerator.Body.Variables.Locals.Add(int32Importing); methodDef_Enumerator.Body.Variables.Locals.Add(booleanImporting); int i = 0; var lengthILBody = methodDef_Enumerator.Body.Instructions.Count; var instructionLdcI41 = new Instruction(OpCodes.Ldc_I4_1); var instructionLdloc0 = new Instruction(OpCodes.Ldloc_S, int32Importing); methodDef_Enumerator.Body.KeepOldMaxStack = true; methodDef_Enumerator.Body.Instructions.Insert(i, new Instruction(OpCodes.Ldc_I4_0)); methodDef_Enumerator.Body.Instructions.Insert(i + 1, new Instruction(OpCodes.Stloc_S, int32Importing)); methodDef_Enumerator.Body.Instructions.Insert(i + 2, new Instruction(OpCodes.Br_S, instructionLdcI41)); methodDef_Enumerator.Body.Instructions.Insert(i + 3, instructionLdloc0); methodDef_Enumerator.Body.Instructions.Insert(i + 4, new Instruction(OpCodes.Ldc_I4_0)); methodDef_Enumerator.Body.Instructions.Insert(i + 5, new Instruction(OpCodes.Ceq)); methodDef_Enumerator.Body.Instructions.Insert(i + 6, new Instruction(OpCodes.Ldc_I4_1)); methodDef_Enumerator.Body.Instructions.Insert(i + 7, new Instruction(OpCodes.Ceq)); methodDef_Enumerator.Body.Instructions.Insert(i + 8, new Instruction(OpCodes.Stloc_S, booleanImporting)); methodDef_Enumerator.Body.Instructions.Insert(i + 9, new Instruction(OpCodes.Ldloc_S, booleanImporting)); methodDef_Enumerator.Body.Instructions.Insert(i + 10, new Instruction(OpCodes.Brtrue_S, methodDef_Enumerator.Body.Instructions[10])); methodDef_Enumerator.Body.Instructions.Insert(i + 11, new Instruction(OpCodes.Ret)); methodDef_Enumerator.Body.Instructions.Insert(i + 12, new Instruction(OpCodes.Calli, null)); methodDef_Enumerator.Body.Instructions.Insert(i + 13, new Instruction(OpCodes.Sizeof, null)); methodDef_Enumerator.Body.Instructions.Insert(i + lengthILBody + 14, instructionLdcI41); methodDef_Enumerator.Body.Instructions.Insert(i + lengthILBody + 15, new Instruction(OpCodes.Stloc_S, booleanImporting)); methodDef_Enumerator.Body.Instructions.Insert(i + lengthILBody + 16, new Instruction(OpCodes.Br_S, instructionLdloc0)); methodDef_Enumerator.Body.Instructions.Insert(i + lengthILBody + 17, new Instruction(OpCodes.Ret)); var exceptionHandler = new ExceptionHandler(); exceptionHandler.TryStart = methodDef_Enumerator.Body.Instructions[12]; exceptionHandler.TryEnd = methodDef_Enumerator.Body.Instructions[14]; exceptionHandler.FilterStart = null; exceptionHandler.HandlerStart = methodDef_Enumerator.Body.Instructions[10]; exceptionHandler.HandlerEnd = methodDef_Enumerator.Body.Instructions[11]; exceptionHandler.HandlerType = ExceptionHandlerType.Finally; exceptionHandler.CatchType = null; methodDef_Enumerator.Body.ExceptionHandlers.Add(exceptionHandler); //method.Body.OptimizeBranches(); //method.Body.SimplifyBranches(); } }
/// <summary> /// Add an exception handler if it appears valid /// </summary> /// <param name="eh">The exception handler</param> /// <returns><c>true</c> if it was added, <c>false</c> otherwise</returns> protected bool Add(ExceptionHandler eh) { uint tryStart = GetOffset(eh.TryStart); uint tryEnd = GetOffset(eh.TryEnd); if (tryEnd <= tryStart) return false; uint handlerStart = GetOffset(eh.HandlerStart); uint handlerEnd = GetOffset(eh.HandlerEnd); if (handlerEnd <= handlerStart) return false; if (eh.HandlerType == ExceptionHandlerType.Filter) { if (eh.FilterStart == null) return false; if (eh.FilterStart.Offset >= handlerStart) return false; } if (handlerStart <= tryStart && tryStart < handlerEnd) return false; if (handlerStart < tryEnd && tryEnd <= handlerEnd) return false; if (tryStart <= handlerStart && handlerStart < tryEnd) return false; if (tryStart < handlerEnd && handlerEnd <= tryEnd) return false; // It's probably valid, so let's add it. exceptionHandlers.Add(eh); return true; }
/// <summary> /// Convert the SerializedExceptionHandler at some index to a dnlib ExceptionHandler /// and return it. /// </summary> /// <param name="index">Index</param> /// <returns>ExceptionHandler</returns> ExceptionHandler GetExceptionHandler(Int32 index) { var deserialized = _exceptionHandlers[index]; ExceptionHandler handler = new ExceptionHandler(deserialized.HandlerType); if (deserialized.HasCatchType) handler.CatchType = this.Resolver.ResolveType(deserialized.VirtualCatchType); handler.TryStart = GetInstruction(this.GetRealOffset(deserialized.VirtualTryStart)); // VirtualTryEnd actually points to virtual instruction before the actual end handler.TryEnd = GetInstruction(this.GetRealOffset(deserialized.VirtualTryEnd)); if (GetInstructionAfter(handler.TryEnd) != null) handler.TryEnd = GetInstructionAfter(handler.TryEnd); handler.HandlerStart = GetInstruction(this.GetRealOffset(deserialized.VirtualHandlerStart)); // VirtualHandlerEnd does the same as VirtualTryEnd handler.HandlerEnd = GetInstruction(this.GetRealOffset(deserialized.VirtualHandlerEnd)); if (GetInstructionAfter(handler.HandlerEnd) != null) handler.HandlerEnd = GetInstructionAfter(handler.HandlerEnd); handler.FilterStart = GetInstruction(this.GetRealOffset(deserialized.VirtualFilterStart)); return handler; }
static bool IsSameTryBlock(dnlib.DotNet.Emit.ExceptionHandler ex1, dnlib.DotNet.Emit.ExceptionHandler ex2) => ex1.TryStart == ex2.TryStart && ex1.TryEnd == ex2.TryEnd;
void ReadExceptionHandlers(int numExceptionHandlers) { exceptionHandlers = new ExceptionHandler[numExceptionHandlers]; for (int i = 0; i < exceptionHandlers.Count; i++) exceptionHandlers[i] = ReadExceptionHandler(); }
public TryHandlerBlock(ExceptionHandler handler) { this.catchType = handler.CatchType; this.handlerType = handler.HandlerType; }
public EHInfo(ExceptionHandler eh) { this.eh = eh; }
internal ControlFlowNode(int blockIndex, ExceptionHandler exceptionHandler, ControlFlowNode endFinallyOrFaultNode) { this.BlockIndex = blockIndex; this.NodeType = endFinallyOrFaultNode != null ? ControlFlowNodeType.FinallyOrFaultHandler : ControlFlowNodeType.CatchHandler; this.ExceptionHandler = exceptionHandler; this.EndFinallyOrFaultNode = endFinallyOrFaultNode; Debug.Assert((exceptionHandler.HandlerType == ExceptionHandlerType.Finally || exceptionHandler.HandlerType == ExceptionHandlerType.Fault) == (endFinallyOrFaultNode != null)); this.Offset = exceptionHandler.HandlerStart.GetOffset(); }
public static void CopyBody(MethodDef method, out IList<Instruction> instructions, out IList<ExceptionHandler> exceptionHandlers) { if (method == null || !method.HasBody) { instructions = new List<Instruction>(); exceptionHandlers = new List<ExceptionHandler>(); return; } var oldInstrs = method.Body.Instructions; var oldExHandlers = method.Body.ExceptionHandlers; instructions = new List<Instruction>(oldInstrs.Count); exceptionHandlers = new List<ExceptionHandler>(oldExHandlers.Count); var oldToIndex = Utils.CreateObjectToIndexDictionary(oldInstrs); foreach (var oldInstr in oldInstrs) instructions.Add(oldInstr.Clone()); foreach (var newInstr in instructions) { var operand = newInstr.Operand; if (operand is Instruction) newInstr.Operand = instructions[oldToIndex[(Instruction)operand]]; else if (operand is IList<Instruction>) { var oldArray = (IList<Instruction>)operand; var newArray = new Instruction[oldArray.Count]; for (int i = 0; i < oldArray.Count; i++) newArray[i] = instructions[oldToIndex[oldArray[i]]]; newInstr.Operand = newArray; } } foreach (var oldEx in oldExHandlers) { var newEx = new ExceptionHandler(oldEx.HandlerType) { TryStart = GetInstruction(instructions, oldToIndex, oldEx.TryStart), TryEnd = GetInstruction(instructions, oldToIndex, oldEx.TryEnd), FilterStart = GetInstruction(instructions, oldToIndex, oldEx.FilterStart), HandlerStart = GetInstruction(instructions, oldToIndex, oldEx.HandlerStart), HandlerEnd = GetInstruction(instructions, oldToIndex, oldEx.HandlerEnd), CatchType = oldEx.CatchType, }; exceptionHandlers.Add(newEx); } }
void CreateExceptionHandlers() { if (ehHeader != null && ehHeader.Length != 0) { var reader = new BinaryReader(new MemoryStream(ehHeader)); byte b = (byte)reader.ReadByte(); if ((b & 0x40) == 0) { // DynamicResolver only checks bit 6 // Calculate num ehs exactly the same way that DynamicResolver does int numHandlers = (ushort)((reader.ReadByte() - 2) / 12); reader.ReadInt16(); for (int i = 0; i < numHandlers; i++) { var eh = new dnlib.DotNet.Emit.ExceptionHandler(); eh.HandlerType = (ExceptionHandlerType)reader.ReadInt16(); int offs = reader.ReadUInt16(); eh.TryStart = GetInstructionThrow((uint)offs); eh.TryEnd = GetInstruction((uint)(reader.ReadSByte() + offs)); offs = reader.ReadUInt16(); eh.HandlerStart = GetInstructionThrow((uint)offs); eh.HandlerEnd = GetInstruction((uint)(reader.ReadSByte() + offs)); if (eh.HandlerType == ExceptionHandlerType.Catch) { eh.CatchType = ReadToken(reader.ReadUInt32()) as ITypeDefOrRef; } else if (eh.HandlerType == ExceptionHandlerType.Filter) { eh.FilterStart = GetInstruction(reader.ReadUInt32()); } else { reader.ReadUInt32(); } exceptionHandlers.Add(eh); } } else { reader.BaseStream.Position--; int numHandlers = (ushort)(((reader.ReadUInt32() >> 8) - 4) / 24); for (int i = 0; i < numHandlers; i++) { var eh = new dnlib.DotNet.Emit.ExceptionHandler(); eh.HandlerType = (ExceptionHandlerType)reader.ReadInt32(); int offs = reader.ReadInt32(); eh.TryStart = GetInstructionThrow((uint)offs); eh.TryEnd = GetInstruction((uint)(reader.ReadInt32() + offs)); offs = reader.ReadInt32(); eh.HandlerStart = GetInstructionThrow((uint)offs); eh.HandlerEnd = GetInstruction((uint)(reader.ReadInt32() + offs)); if (eh.HandlerType == ExceptionHandlerType.Catch) { eh.CatchType = ReadToken(reader.ReadUInt32()) as ITypeDefOrRef; } else if (eh.HandlerType == ExceptionHandlerType.Filter) { eh.FilterStart = GetInstruction(reader.ReadUInt32()); } else { reader.ReadUInt32(); } exceptionHandlers.Add(eh); } } } else if (ehInfos != null) { foreach (var ehInfo in CreateExceptionInfos(ehInfos)) { var tryStart = GetInstructionThrow((uint)ehInfo.StartAddr); var tryEnd = GetInstruction((uint)ehInfo.EndAddr); var endFinally = ehInfo.EndFinally < 0 ? null : GetInstruction((uint)ehInfo.EndFinally); for (int i = 0; i < ehInfo.CurrentCatch; i++) { var eh = new dnlib.DotNet.Emit.ExceptionHandler(); eh.HandlerType = (ExceptionHandlerType)ehInfo.Type[i]; eh.TryStart = tryStart; eh.TryEnd = eh.HandlerType == ExceptionHandlerType.Finally ? endFinally : tryEnd; eh.FilterStart = null; // not supported by DynamicMethod.ILGenerator eh.HandlerStart = GetInstructionThrow((uint)ehInfo.CatchAddr[i]); eh.HandlerEnd = GetInstruction((uint)ehInfo.CatchEndAddr[i]); eh.CatchType = importer.Import(ehInfo.CatchClass[i]); exceptionHandlers.Add(eh); } } } }