private static RuntimeMethodHandle IntPtrToMethodHandle(IntPtr intPtr) { var handle = new RuntimeMethodHandle(); handle.GetType().GetFields(BindingFlags.NonPublic).First().SetValue(handle, intPtr); return(handle); }
public static IntPtr PrepareMethod(this DynamicMethod meth) { Type t = meth.GetType(); RuntimeMethodHandle handle = t.GetMethod("GetMethodDescriptor", mflag0) is MethodInfo getmd ? (RuntimeMethodHandle)getmd.Invoke(meth, null) : t.GetField("m_method", mflag0) is FieldInfo mm ? (RuntimeMethodHandle)mm.GetValue(meth) : (RuntimeMethodHandle)t.GetField("mhandle", mflag0).GetValue(meth); Type ht = handle.GetType(); object result = ht.GetField("m_value", mflag0) is FieldInfo fd ? fd.GetValue(handle) : ht.GetMethod("GetMethodInfo", mflag0) is MethodInfo mi ? mi.Invoke(handle, null) : null; if (result != null) { try { // 2020 01 30 // dynamicmethod 코드에 전혀 문제가 없는 경우에 // 여기서 오류가 발생하지 않는다 m_compileMeth.Invoke(null, new object[1] { result }); return(handle.GetFunctionPointer()); } }
void RestoreMethodsModuleVariant(string pathFile) { int delegateNumber = 0; string declaringTypeName = string.Empty; string fieldName = string.Empty; foreach (var method in moduleDef.Types[0].Methods) { if (method.HasBody) { if (method.Body.HasInstructions) { if (method.Body.Instructions[0].OpCode == OpCodes.Ldsfld && method.Body.Instructions[1].OpCode.ToString().Contains("ldc")) { delegateNumber = method.Body.Instructions[1].GetLdcI4Value(); dynamic ldsfldOperand = method.Body.Instructions[0]?.Operand; if (ldsfldOperand is FieldDef) { declaringTypeName = ldsfldOperand.DeclaringType.Name; fieldName = ldsfldOperand.Name; } var delegatesArray = (object[])assembly.ManifestModule.GetType(declaringTypeName) .GetField(fieldName).GetValue(null); var currentDelegate = (Delegate)delegatesArray[delegateNumber]; var m_owner = currentDelegate.Method .GetType() .GetField("m_owner", BindingFlags.NonPublic | BindingFlags.Instance) ?.GetValue(currentDelegate.Method); if (m_owner != null) { var m_resolver = m_owner .GetType() .GetField("m_resolver", BindingFlags.NonPublic | BindingFlags.Instance) ?.GetValue(m_owner); if (m_resolver != null) { var m_scope = m_resolver.GetType() .GetField("m_scope", BindingFlags.NonPublic | BindingFlags.Instance) ?.GetValue(m_resolver); List <object> m_tokens = (List <object>)m_scope.GetType() .GetField("m_tokens", BindingFlags.NonPublic | BindingFlags.Instance) .GetValue(m_scope); if (m_tokens[m_tokens.Count - 1] is RuntimeMethodHandle) { RuntimeMethodHandle calledMethod = (RuntimeMethodHandle)m_tokens[m_tokens.Count - 1]; dynamic calledMethodMInfo = calledMethod.GetType() .GetField("m_value", BindingFlags.NonPublic | BindingFlags.Instance) ?.GetValue(calledMethod); if (calledMethodMInfo != null) { try { string fullName = calledMethodMInfo.GetType() .GetProperty("FullName", BindingFlags.Instance | BindingFlags.NonPublic) .GetValue(calledMethodMInfo).ToString(); if (fullName.Contains(".ctor") && !fullName.Contains("System.Windows.Forms.Form..ctor")) { method.Body.Instructions[method.Body.Instructions.Count - 2] = Instruction.Create(OpCodes.Newobj, moduleDef.Import(calledMethodMInfo)); method.Body.Instructions[0] = Instruction.Create(OpCodes.Nop); method.Body.Instructions[1] = Instruction.Create(OpCodes.Nop); method.Body.Instructions[2] = Instruction.Create(OpCodes.Nop); method.Body.UpdateInstructionOffsets(); } else { method.Body.Instructions[method.Body.Instructions.Count - 2] = Instruction.Create(OpCodes.Call, moduleDef.Import(calledMethodMInfo)); method.Body.Instructions[0] = Instruction.Create(OpCodes.Nop); method.Body.Instructions[1] = Instruction.Create(OpCodes.Nop); method.Body.Instructions[2] = Instruction.Create(OpCodes.Nop); method.Body.UpdateInstructionOffsets(); } } catch (Exception e) { } } // * - this runtime method Console.WriteLine(delegateNumber + "*: " + calledMethodMInfo.GetType() .GetProperty("FullName", BindingFlags.Instance | BindingFlags.NonPublic) .GetValue(calledMethodMInfo)); } else if (m_tokens[m_tokens.Count - 1] is RuntimeFieldHandle) { RuntimeFieldHandle calledField = (RuntimeFieldHandle)m_tokens[m_tokens.Count - 1]; dynamic calledFieldFInfo = calledField.GetType() .GetField("m_ptr", BindingFlags.NonPublic | BindingFlags.Instance) ?.GetValue(calledField); if (calledFieldFInfo != null) { method.Body.Instructions[method.Body.Instructions.Count - 2] = Instruction.Create(OpCodes.Ldsfld, moduleDef.Import(calledFieldFInfo)); method.Body.Instructions[0] = Instruction.Create(OpCodes.Nop); method.Body.Instructions[1] = Instruction.Create(OpCodes.Nop); method.Body.Instructions[2] = Instruction.Create(OpCodes.Nop); method.Body.UpdateInstructionOffsets(); } // * - this runtime field Console.WriteLine(delegateNumber + "*: " + calledFieldFInfo.GetType() .GetProperty("FullName", BindingFlags.Instance | BindingFlags.NonPublic) .GetValue(calledFieldFInfo)); } else { Console.ForegroundColor = ConsoleColor.Cyan; Console.WriteLine("UNKNOWN"); Console.ForegroundColor = ConsoleColor.Blue; } } } else { method.Body.Instructions[0] = Instruction.Create(OpCodes.Nop); method.Body.Instructions[1] = Instruction.Create(OpCodes.Nop); method.Body.Instructions[2] = Instruction.Create(OpCodes.Nop); method.Body.Instructions[method.Body.Instructions.Count - 2] = Instruction.Create(OpCodes.Call, moduleDef.Import(currentDelegate.Method)); method.Body.UpdateInstructionOffsets(); Console.WriteLine(delegateNumber + ": " + currentDelegate.Method); } } } } } }