/// <summary>This is the method marked with the SecurityCriticalAttribute</summary> /// <remarks>This method makes the call into the profiler</remarks> HRESULT CCodeCoverage::AddCriticalCuckooBody(ModuleID moduleId) { ATLTRACE(_T("::AddCriticalCuckooBody => Adding VisitedCritical...")); mdSignature pvsig = GetMethodSignatureToken_I4(moduleId); void(__fastcall *pt)(ULONG) = GetInstrumentPointVisit(); BYTE data[] = { (0x01 << 2) | CorILMethod_TinyFormat, CEE_RET }; Method criticalMethod((IMAGE_COR_ILMETHOD*)data); InstructionList instructions; instructions.push_back(new Instruction(CEE_LDARG_0)); #if _WIN64 instructions.push_back(new Instruction(CEE_LDC_I8, (ULONGLONG)pt)); #else instructions.push_back(new Instruction(CEE_LDC_I4, (ULONG)pt)); #endif instructions.push_back(new Instruction(CEE_CALLI, pvsig)); criticalMethod.InsertInstructionsAtOffset(0, instructions); //criticalMethod.DumpIL(); InstrumentMethodWith(moduleId, m_cuckooCriticalToken, instructions); ATLTRACE(_T("::AddCriticalCuckooBody => Adding VisitedCritical - Done!")); return S_OK; }
void CCodeCoverage::InstrumentMethod(ModuleID moduleId, Method& method, std::vector<SequencePoint> seqPoints, std::vector<BranchPoint> brPoints) { if (m_useOldStyle) { mdSignature pvsig = GetMethodSignatureToken_I4(moduleId); void (__fastcall *pt)(ULONG) = &InstrumentPointVisit ; CoverageInstrumentation::AddSequenceCoverage([pvsig, pt](InstructionList& instructions, ULONG uniqueId)->Instruction* { return CoverageInstrumentation::InsertFunctionCall(instructions, pvsig, (FPTR)pt, uniqueId); }, method, seqPoints); CoverageInstrumentation::AddBranchCoverage([pvsig, pt](InstructionList& instructions, ULONG uniqueId)->Instruction* { return CoverageInstrumentation::InsertFunctionCall(instructions, pvsig, (FPTR)pt, uniqueId); }, method, brPoints); } else { mdMethodDef injectedVisitedMethod = RegisterSafeCuckooMethod(moduleId); CoverageInstrumentation::AddSequenceCoverage([injectedVisitedMethod](InstructionList& instructions, ULONG uniqueId)->Instruction* { return CoverageInstrumentation::InsertInjectedMethod(instructions, injectedVisitedMethod, uniqueId); }, method, seqPoints); CoverageInstrumentation::AddBranchCoverage([injectedVisitedMethod](InstructionList& instructions, ULONG uniqueId)->Instruction* { return CoverageInstrumentation::InsertInjectedMethod(instructions, injectedVisitedMethod, uniqueId); }, method, brPoints); } }
/// <summary>This is the method marked with the SecurityCriticalAttribute</summary> /// <remarks>This method makes the call into the profiler</remarks> HRESULT CCodeCoverage::AddCriticalCuckooBody(ModuleID moduleId) { ATLTRACE(_T("::AddCriticalCuckooBody => Adding VisitedCritical...")); // our profiler hook mdSignature pvsig = GetMethodSignatureToken_I4(moduleId); void (__fastcall *pt)(ULONG) = &InstrumentPointVisit ; BYTE data[] = {(0x01 << 2) | CorILMethod_TinyFormat, CEE_RET}; Method criticalMethod((IMAGE_COR_ILMETHOD*)data); InstructionList instructions; instructions.push_back(new Instruction(CEE_LDARG_0)); #if _WIN64 instructions.push_back(new Instruction(CEE_LDC_I8, (ULONGLONG)pt)); #else instructions.push_back(new Instruction(CEE_LDC_I4, (ULONG)pt)); #endif instructions.push_back(new Instruction(CEE_CALLI, pvsig)); criticalMethod.InsertInstructionsAtOffset(0, instructions); criticalMethod.DumpIL(); CComPtr<IMethodMalloc> methodMalloc; COM_FAIL_RETURNMSG(m_profilerInfo->GetILFunctionBodyAllocator(moduleId, &methodMalloc), _T(" ::AddCriticalCuckooBody => Failed GetILFunctionBodyAllocator(0x%x)")); void* pMethodBody = methodMalloc->Alloc(criticalMethod.GetMethodSize()); criticalMethod.WriteMethod((IMAGE_COR_ILMETHOD*)pMethodBody); COM_FAIL_RETURNMSG(m_profilerInfo->SetILFunctionBody(moduleId, m_cuckooCriticalToken, (LPCBYTE)pMethodBody), _T(" ::AddCriticalCuckooBody => SetILFunctionBody(0x%x)")); ATLTRACE(_T("::AddCriticalCuckooBody => Adding VisitedCritical - Done!")); return S_OK; }