public MethodXrefScanCache(string filePath) { myMapFile = MemoryMappedFile.CreateFromFile(filePath, FileMode.Open, null, 0, MemoryMappedFileAccess.Read); var headerView = myMapFile.CreateViewAccessor(0, 0, MemoryMappedFileAccess.Read); myAccessor = headerView; headerView.Read(0, out Header); if (Header.Magic != Magic) { myMapFile.Dispose(); throw new FileLoadException($"File magic mismatched for {filePath}; Expected {Magic:X}, got {Header.Magic:X}"); } if (Header.Version != Version) { myMapFile.Dispose(); throw new FileLoadException($"File version mismatched for {filePath}; Expected {Version}, got {Header.Version}"); } var offset = Marshal.SizeOf <FileHeader>(); unsafe { byte *pointersPointer = null; myAccessor.SafeMemoryMappedViewHandle.AcquirePointer(ref pointersPointer); myData = (MethodData *)(pointersPointer + offset); } }
private void ReleaseUnmanagedResources() { myAccessor.SafeMemoryMappedViewHandle.ReleasePointer(); unsafe { myData = null; } }
//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)); } }