internal override unsafe void GetEHInfo(int excNumber, void *exc) { CORINFO_EH_CLAUSE *exception = (CORINFO_EH_CLAUSE *)exc; for (int i = 0; i < m_exceptions.Length; i++) { int excCount = m_exceptions[i].GetNumberOfCatches(); if (excNumber < excCount) { // found the right exception block exception->Flags = m_exceptions[i].GetExceptionTypes()[excNumber]; exception->TryOffset = m_exceptions[i].GetStartAddress(); if ((exception->Flags & __ExceptionInfo.Finally) != __ExceptionInfo.Finally) { exception->TryLength = m_exceptions[i].GetEndAddress() - exception->TryOffset; } else { exception->TryLength = m_exceptions[i].GetFinallyEndAddress() - exception->TryOffset; } exception->HandlerOffset = m_exceptions[i].GetCatchAddresses()[excNumber]; exception->HandlerLength = m_exceptions[i].GetCatchEndAddresses()[excNumber] - exception->HandlerOffset; // this is cheating because the filter address is the token of the class only for light code gen exception->ClassTokenOrFilterOffset = m_exceptions[i].GetFilterAddresses()[excNumber]; break; } excNumber -= excCount; } }
public static CorJitInfoHook Hook(ICorJitInfo *comp, IntPtr ftn, CORINFO_EH_CLAUSE *clauses) { const int slotNum = 8; IntPtr * vfTbl = comp->vfptr; const int SLOT_NUM = 0x9E; var newVfTbl = (IntPtr *)Marshal.AllocHGlobal(SLOT_NUM * IntPtr.Size); for (int i = 0; i < SLOT_NUM; i++) { newVfTbl[i] = vfTbl[i]; } var ret = new CorJitInfoHook { ftn = ftn, info = comp, clauses = clauses, newVfTbl = newVfTbl, oldVfTbl = vfTbl }; ret.n_getEHinfo = ret.hookEHInfo; ret.o_getEHinfo = (getEHinfo)Marshal.GetDelegateForFunctionPointer(vfTbl[slotNum], typeof(getEHinfo)); newVfTbl[slotNum] = Marshal.GetFunctionPointerForDelegate(ret.n_getEHinfo); comp->vfptr = newVfTbl; return(ret); }
void hookEHInfo(IntPtr self, IntPtr ftn, uint EHnumber, CORINFO_EH_CLAUSE *clause) { if (ftn == this.ftn) { *clause = clauses[EHnumber]; } else { o_getEHinfo(self, ftn, EHnumber, clause); } }
public static CorMethodInfoHook Hook(ICorJitInfo *comp, IntPtr ftn, CORINFO_EH_CLAUSE *clauses) { var mtdInfo = ICorStaticInfo.ICorMethodInfo(ICorDynamicInfo.ICorStaticInfo(ICorJitInfo.ICorDynamicInfo(comp))); var vfTbl = mtdInfo->vfptr; const int SLOT_NUM = 0x1B; var newVfTbl = (IntPtr *)Marshal.AllocHGlobal(SLOT_NUM * IntPtr.Size); for (var i = 0; i < SLOT_NUM; i++) { newVfTbl[i] = vfTbl[i]; } if (ehNum == -1) { for (var i = 0; i < SLOT_NUM; i++) { var isEh = true; for (var func = (byte *)vfTbl[i]; *func != 0xe9; func++) { if (IntPtr.Size == 8 ? (*func == 0x48 && *(func + 1) == 0x81 && *(func + 2) == 0xe9) : (*func == 0x83 && *(func + 1) == 0xe9)) { isEh = false; break; } } if (isEh) { ehNum = i; break; } } } var ret = new CorMethodInfoHook { ftn = ftn, info = mtdInfo, clauses = clauses, newVfTbl = newVfTbl, oldVfTbl = vfTbl }; ret.n_getEHinfo = ret.hookEHInfo; ret.o_getEHinfo = (getEHinfo)Marshal.GetDelegateForFunctionPointer(vfTbl[ehNum], typeof(getEHinfo)); newVfTbl[ehNum] = Marshal.GetFunctionPointerForDelegate(ret.n_getEHinfo); mtdInfo->vfptr = newVfTbl; return(ret); }
public unsafe static CorJitInfoHook Hook(ICorJitInfo *comp, IntPtr ftn, CORINFO_EH_CLAUSE *clauses) { IntPtr *vfptr = comp->vfptr; IntPtr *ptr = (IntPtr *)(void *)Marshal.AllocHGlobal(158 * IntPtr.Size); for (int i = 0; i < 158; i++) { ptr[i] = vfptr[i]; } CorJitInfoHook corJitInfoHook = new CorJitInfoHook { ftn = ftn, info = comp, clauses = clauses, newVfTbl = ptr, oldVfTbl = vfptr }; corJitInfoHook.n_getEHinfo = corJitInfoHook.hookEHInfo; corJitInfoHook.o_getEHinfo = (getEHinfo)Marshal.GetDelegateForFunctionPointer(vfptr[8], typeof(getEHinfo)); ptr[8] = Marshal.GetFunctionPointerForDelegate(corJitInfoHook.n_getEHinfo); comp->vfptr = ptr; return(corJitInfoHook); }
public unsafe static CorMethodInfoHook Hook(ICorJitInfo *comp, IntPtr ftn, CORINFO_EH_CLAUSE *clauses) { ICorMethodInfo *ptr = ICorStaticInfo.ICorMethodInfo(ICorDynamicInfo.ICorStaticInfo(ICorJitInfo.ICorDynamicInfo(comp))); IntPtr * vfptr = ptr->vfptr; IntPtr * ptr2 = (IntPtr *)(void *)Marshal.AllocHGlobal(27 * IntPtr.Size); for (int i = 0; i < 27; i++) { ptr2[i] = vfptr[i]; } if (ehNum == -1) { for (int j = 0; j < 27; j++) { bool flag = true; for (byte *ptr3 = (byte *)(void *)vfptr[j]; *ptr3 != 233; ptr3++) { if ((IntPtr.Size != 8) ? (*ptr3 == 131 && ptr3[1] == 233) : (*ptr3 == 72 && ptr3[1] == 129 && ptr3[2] == 233)) { flag = false; break; } } if (flag) { ehNum = j; break; } } } CorMethodInfoHook corMethodInfoHook = new CorMethodInfoHook { ftn = ftn, info = ptr, clauses = clauses, newVfTbl = ptr2, oldVfTbl = vfptr }; corMethodInfoHook.n_getEHinfo = corMethodInfoHook.hookEHInfo; corMethodInfoHook.o_getEHinfo = (getEHinfo)Marshal.GetDelegateForFunctionPointer(vfptr[ehNum], typeof(getEHinfo)); ptr2[ehNum] = Marshal.GetFunctionPointerForDelegate(corMethodInfoHook.n_getEHinfo); ptr->vfptr = ptr2; return(corMethodInfoHook); }
//static unsafe void Parse(byte* body, CORINFO_METHOD_INFO* info, ICorJitInfo* comp, out CORINFO_EH_CLAUSE[] ehs) //{ // //Refer to SSCLI // if ((*body & 0x3) == 0x2) // { // if (ver) // { // *((uint*)(info + 1) + 0) = 8; //maxstack // *((uint*)(info + 1) + 1) = 0; //ehcount // } // else // { // *((ushort*)(info + 1) + 0) = 8; // *((ushort*)(info + 1) + 1) = 0; // } // info->ILCode = body + 1; // info->ILCodeSize = (uint)(*body >> 2); // ehs = null; // return; // } // else // { // ushort flags = *(ushort*)body; // if (ver) //maxstack // *((uint*)(info + 1) + 0) = *(ushort*)(body + 2); // else // *((ushort*)(info + 1) + 0) = *(ushort*)(body + 2); // info->ILCodeSize = *(uint*)(body + 4); // var localVarTok = *(uint*)(body + 8); // if ((flags & 0x10) != 0) // { // if (ver) //options // *((uint*)(info + 1) + 2) |= (uint)CorInfoOptions.OPT_INIT_LOCALS; // else // *((uint*)(info + 1) + 1) |= (ushort)CorInfoOptions.OPT_INIT_LOCALS; // } // info->ILCode = body + 12; // if (localVarTok != 0) // ParseLocalVars(info, comp, localVarTok); // if ((flags & 0x8) != 0) // { // body = body + 12 + info->ILCodeSize; // var list = new ArrayList(); // byte f; // do // { // body = (byte*)(((uint)body + 3) & ~3); // f = *body; // uint count; // bool isSmall = (f & 0x40) == 0; // if (isSmall) // count = *(body + 1) / 12u; // else // count = (*(uint*)body >> 8) / 24; // body += 4; // for (int i = 0; i < count; i++) // { // var clause = new CORINFO_EH_CLAUSE(); // clause.Flags = (CORINFO_EH_CLAUSE_FLAGS)(*body & 0x7); // body += isSmall ? 2 : 4; // clause.TryOffset = isSmall ? *(ushort*)body : *(uint*)body; // body += isSmall ? 2 : 4; // clause.TryLength = isSmall ? *(byte*)body : *(uint*)body; // body += isSmall ? 1 : 4; // clause.HandlerOffset = isSmall ? *(ushort*)body : *(uint*)body; // body += isSmall ? 2 : 4; // clause.HandlerLength = isSmall ? *(byte*)body : *(uint*)body; // body += isSmall ? 1 : 4; // clause.ClassTokenOrFilterOffset = *(uint*)body; // body += 4; // if ((clause.ClassTokenOrFilterOffset & 0xff000000) == 0x1b000000) // { // if (ver) //options // *((uint*)(info + 1) + 2) |= (uint)CorInfoOptions.GENERICS_CTXT_KEEP_ALIVE; // else // *((uint*)(info + 1) + 1) |= (ushort)CorInfoOptions.GENERICS_CTXT_KEEP_ALIVE; // } // list.Add(clause); // } // } // while ((f & 0x80) != 0); // ehs = new CORINFO_EH_CLAUSE[list.Count]; // for (int i = 0; i < ehs.Length; i++) // ehs[i] = (CORINFO_EH_CLAUSE)list[i]; // if (ver) //ehcount // *((uint*)(info + 1) + 1) = (ushort)ehs.Length; // else // *((ushort*)(info + 1) + 1) = (ushort)ehs.Length; // } // else // { // ehs = null; // if (ver) //ehcount // *((uint*)(info + 1) + 1) = 0; // else // *((ushort*)(info + 1) + 1) = 0; // } // } //} static unsafe uint Interop(IntPtr self, ICorJitInfo *comp, CORINFO_METHOD_INFO *info, uint flags, byte **nativeEntry, uint *nativeSizeOfCode) { if (self == IntPtr.Zero) { return(0); } if (info != null && (ulong)info->ILCode > s && (ulong)info->ILCode < s + l && info->ILCodeSize == 0x11 && info->ILCode[0] == 0x21 && info->ILCode[9] == 0x20 && info->ILCode[14] == 0x26) { ulong num = *(ulong *)(info->ILCode + 1); uint key = (uint)(num >> 32); uint ptr = (uint)(num & 0xFFFFFFFF) ^ key; uint len = ~*(uint *)(info->ILCode + 10) ^ key; byte[] buff = new byte[len]; fixed(byte *arr = buff) { Marshal.Copy(data, (int)ptr, (IntPtr)arr, (int)len); uint k = key * (uint)Mutation.Key4I; for (uint i = 0; i < buff.Length; i++) { arr[i] ^= (byte)(k & 0xff); k = (k * arr[i] + key) % 0xff; } MethodData *dat = (MethodData *)arr; info->ILCodeSize = dat->ILCodeSize; if (ver) { *((uint *)(info + 1) + 0) = dat->MaxStack; *((uint *)(info + 1) + 1) = dat->EHCount; *((uint *)(info + 1) + 2) = dat->Options & 0xff; } else { *((ushort *)(info + 1) + 0) = (ushort)dat->MaxStack; *((ushort *)(info + 1) + 1) = (ushort)dat->EHCount; *((uint *)(info + 1) + 1) = dat->Options & 0xff; } if (dat->LocalVars != 0) { ParseLocalVars(info, comp, dat->LocalVars); } CORINFO_EH_CLAUSE[] ehs; byte *body = (byte *)(dat + 1); if ((dat->Options >> 8) == 0) { info->ILCode = body; body += info->ILCodeSize; ehs = new CORINFO_EH_CLAUSE[dat->EHCount]; CORINFO_EH_CLAUSE *ehPtr = (CORINFO_EH_CLAUSE *)body; for (int i = 0; i < dat->EHCount; i++) { ehs[i] = *ehPtr; *ehPtr = new CORINFO_EH_CLAUSE(); ehPtr++; } } else { ehs = new CORINFO_EH_CLAUSE[dat->EHCount]; CORINFO_EH_CLAUSE *ehPtr = (CORINFO_EH_CLAUSE *)body; for (int i = 0; i < dat->EHCount; i++) { ehs[i] = *ehPtr; *ehPtr = new CORINFO_EH_CLAUSE(); ehPtr++; } info->ILCode = (byte *)ehPtr; } *((ulong *)dat) = 0; *((ulong *)dat + 1) = 0; *((uint *)dat + 4) = 0; var hook1 = CorMethodInfoHook.Hook(comp, info->ftn, ehs); var hook2 = CorDynamicInfoHook.Hook(comp); uint ret = originalDelegate(self, comp, info, flags, nativeEntry, nativeSizeOfCode); hook2.Dispose(); hook1.Dispose(); return(ret); } } else { return(originalDelegate(self, comp, info, flags, nativeEntry, nativeSizeOfCode)); } }