LONG WINAPI DelayLoadDllExceptionFilter(PEXCEPTION_POINTERS excpointers) { LONG disposition = EXCEPTION_EXECUTE_HANDLER; PDelayLoadInfo delayloadinfo = (PDelayLoadInfo)(excpointers->ExceptionRecord->ExceptionInformation[0]); switch (excpointers->ExceptionRecord->ExceptionCode) { case VcppException(ERROR_SEVERITY_ERROR, ERROR_MOD_NOT_FOUND): zabbix_log(LOG_LEVEL_DEBUG, "function %s was not found in %s", delayloadinfo->dlp.szProcName, delayloadinfo->szDll); break; case VcppException(ERROR_SEVERITY_ERROR, ERROR_PROC_NOT_FOUND): if (delayloadinfo->dlp.fImportByName) { zabbix_log(LOG_LEVEL_DEBUG, "function %s was not found in %sn", delayloadinfo->dlp.szProcName, delayloadinfo->szDll); } else { zabbix_log(LOG_LEVEL_DEBUG, "function ordinal %d was not found in %sn", delayloadinfo->dlp.dwOrdinal, delayloadinfo->szDll); } break; default: zabbix_log(LOG_LEVEL_DEBUG, "unexpected exception [%08X] in process_eventlog()", excpointers->ExceptionRecord->ExceptionCode); disposition = EXCEPTION_CONTINUE_SEARCH; break; } return(disposition); }
static LONG WINAPI DelayLoadDllExceptionFilter(PEXCEPTION_POINTERS pExcPointers, wxString *err) { LONG lDisposition = EXCEPTION_EXECUTE_HANDLER; PDelayLoadInfo pdli = PDelayLoadInfo(pExcPointers->ExceptionRecord->ExceptionInformation[0]); switch (pExcPointers->ExceptionRecord->ExceptionCode) { case VcppException(ERROR_SEVERITY_ERROR, ERROR_MOD_NOT_FOUND): { // ASICamera2.dll depends on the VC++ 2008 runtime, check for that HMODULE hm = LoadLibraryEx(_T("MSVCR90.DLL"), NULL, LOAD_LIBRARY_AS_DATAFILE); if (hm) { FreeLibrary(hm); *err = wxString::Format(_("Could not load DLL %s"), pdli->szDll); } else *err = _("The ASI camera library requires the Microsoft Visual C++ 2008 Redistributable Package (x86), available at http://www.microsoft.com/en-us/download/details.aspx?id=29"); break; } case VcppException(ERROR_SEVERITY_ERROR, ERROR_PROC_NOT_FOUND): if (pdli->dlp.fImportByName) *err = wxString::Format("Function %s was not found in %s", pdli->dlp.szProcName, pdli->szDll); else *err = wxString::Format("Function ordinal %d was not found in %s", pdli->dlp.dwOrdinal, pdli->szDll); break; default: // Exception is not related to delay loading lDisposition = EXCEPTION_CONTINUE_SEARCH; break; } return lDisposition; }
/** This function is used as a SEH filter to catch only delay load exceptions. */ static bool IsDelayLoadException(PEXCEPTION_POINTERS ExceptionPointers) { #if WINVER > 0x502 // Windows SDK 7.1 doesn't define VcppException switch(ExceptionPointers->ExceptionRecord->ExceptionCode) { case VcppException(ERROR_SEVERITY_ERROR, ERROR_MOD_NOT_FOUND): case VcppException(ERROR_SEVERITY_ERROR, ERROR_PROC_NOT_FOUND): return EXCEPTION_EXECUTE_HANDLER; default: return EXCEPTION_CONTINUE_SEARCH; } #else return EXCEPTION_EXECUTE_HANDLER; #endif }
LONG g_exception_filter_delay_load(PEXCEPTION_POINTERS pExcPointers) { LONG lDisposition = EXCEPTION_EXECUTE_HANDLER; PDelayLoadInfo pDelayLoadInfo = PDelayLoadInfo(pExcPointers->ExceptionRecord->ExceptionInformation[0]); switch (pExcPointers->ExceptionRecord->ExceptionCode) { case VcppException(ERROR_SEVERITY_ERROR, ERROR_MOD_NOT_FOUND): break; case VcppException(ERROR_SEVERITY_ERROR, ERROR_PROC_NOT_FOUND): break; default: lDisposition = EXCEPTION_CONTINUE_SEARCH; break; } return(lDisposition); }
LONG WINAPI delay_load_dll_exception_filter (PEXCEPTION_POINTERS pep) { switch (pep->ExceptionRecord->ExceptionCode) { case VcppException (ERROR_SEVERITY_ERROR, ERROR_MOD_NOT_FOUND): case VcppException (ERROR_SEVERITY_ERROR, ERROR_PROC_NOT_FOUND): er_set (ER_ERROR_SEVERITY, ARG_FILE_LINE, ER_SP_JVM_LIB_NOT_FOUND, 1, "jvm.dll"); break; default: break; } return EXCEPTION_EXECUTE_HANDLER; }
LONG WINAPI DelayLoadExceptionFilter(DWORD exceptioncode) { switch (exceptioncode) { case VcppException(ERROR_SEVERITY_ERROR, ERROR_MOD_NOT_FOUND): return EXCEPTION_EXECUTE_HANDLER; } return EXCEPTION_CONTINUE_SEARCH; }
int myDebugFilter(unsigned int code, PEXCEPTION_POINTERS ep) { if (code == VcppException(ERROR_SEVERITY_ERROR, ERROR_MOD_NOT_FOUND) || code == VcppException(ERROR_SEVERITY_ERROR, ERROR_PROC_NOT_FOUND)) { PDelayLoadInfo dlld = (PDelayLoadInfo)ep->ExceptionRecord->ExceptionInformation[0]; char str[256]; int off = mir_snprintf(str, SIZEOF(str), "dbghelp.dll v.5.0 or later required to provide a crash report\n"); off += mir_snprintf(str+off, SIZEOF(str)-off, "Missing Module: %s ", dlld->szDll); if (dlld->dlp.fImportByName) mir_snprintf(str+off, SIZEOF(str)-off, "Function: %s ", dlld->dlp.szProcName); else mir_snprintf(str+off, SIZEOF(str)-off, "Ordinal: %x ", dlld->dlp.dwOrdinal); MessageBoxA(NULL, str, "Miranda Crash Dumper", MB_OK | MB_ICONERROR | MB_TASKMODAL | MB_TOPMOST); } return EXCEPTION_EXECUTE_HANDLER; }
void _cdecl SET::ExceptionTranslator (unsigned int exCode, PEXCEPTION_POINTERS ep) { // For all structured exceptions we throw Win::ExitException if (exCode == VcppException (ERROR_SEVERITY_ERROR, ERROR_MOD_NOT_FOUND) || exCode == VcppException (ERROR_SEVERITY_ERROR, ERROR_PROC_NOT_FOUND)) { if (ep != 0) { // If this is a Delay-load problem, ExceptionInformation[0] points // to a DelayLoadInfo structure that has detailed error info PDelayLoadInfo pdli = reinterpret_cast<PDelayLoadInfo>(ep->ExceptionRecord->ExceptionInformation [0]); std::string info ("The required dynamic link library "); info += pdli->szDll; if (exCode == VcppException (ERROR_SEVERITY_ERROR, ERROR_MOD_NOT_FOUND)) { // The DLL module was not found at runtime info += " was not found"; } else { // The DLL module was found but it doesn't contain the function info += " does not contain function "; if (pdli->dlp.fImportByName) { info += pdli->dlp.szProcName; } else { info += "with ordinal "; info += ToString (pdli->dlp.dwOrdinal); } } info += '.'; throw Win::ExitException ("Fatal error: missing or un-useable component.", info.c_str ()); } throw Win::ExitException ("Fatal error: missing or un-useable Dynamic Link Library."); } else if (exCode == EXCEPTION_ACCESS_VIOLATION) { if (ep != 0) { PEXCEPTION_RECORD exRec = ep->ExceptionRecord; if (exRec != 0) { std::ostringstream info; info << "The instruction at address '0x"; info << std::hex << std::setw (8) << std::setfill ('0') << exRec->ExceptionAddress; info << "' referenced memory at address '0x"; info << std::hex << std::setw (8) << std::setfill ('0') << exRec->ExceptionInformation [1]; info << "'. The memory could not be "; if (exRec->ExceptionInformation [0] == 0) info << "read."; else info << "written."; throw Win::ExitException ("Fatal error: access violation.", info.str ().c_str ()); } } throw Win::ExitException ("Fatal error: access violation."); } // For all other structured exception codes throw this one ExitException throw Win::ExitException ("Fatal error: unrecoverable error occurred during program execution."); }
FARPROC WINAPI __delayLoadHelper2( PCImgDelayDescr pidd, FARPROC * ppfnIATEntry ) { // Set up some data we use for the hook procs but also useful for // our own use // InternalImgDelayDescr idd = { pidd->grAttrs, PFromRva<LPCSTR>(pidd->rvaDLLName), PFromRva<HMODULE*>(pidd->rvaHmod), PFromRva<PImgThunkData>(pidd->rvaIAT), PFromRva<PCImgThunkData>(pidd->rvaINT), PFromRva<PCImgThunkData>(pidd->rvaBoundIAT), PFromRva<PCImgThunkData>(pidd->rvaUnloadIAT), pidd->dwTimeStamp }; DelayLoadInfo dli = { sizeof DelayLoadInfo, pidd, ppfnIATEntry, idd.szName, { 0 }, 0, 0, 0 }; if (0 == (idd.grAttrs & dlattrRva)) { PDelayLoadInfo rgpdli[1] = { &dli }; RaiseException( VcppException(ERROR_SEVERITY_ERROR, ERROR_INVALID_PARAMETER), 0, 1, PULONG_PTR(rgpdli) ); return 0; } HMODULE hmod = *idd.phmod; // Calculate the index for the IAT entry in the import address table // N.B. The INT entries are ordered the same as the IAT entries so // the calculation can be done on the IAT side. // const unsigned iIAT = IndexFromPImgThunkData(PCImgThunkData(ppfnIATEntry), idd.pIAT); const unsigned iINT = iIAT; PCImgThunkData pitd = &(idd.pINT[iINT]); dli.dlp.fImportByName = !IMAGE_SNAP_BY_ORDINAL(pitd->u1.Ordinal); if (dli.dlp.fImportByName) { dli.dlp.szProcName = LPCSTR(PFromRva<PIMAGE_IMPORT_BY_NAME>(RVA(UINT_PTR(pitd->u1.AddressOfData)))->Name); } else { dli.dlp.dwOrdinal = DWORD(IMAGE_ORDINAL(pitd->u1.Ordinal)); } // Call the initial hook. If it exists and returns a function pointer, // abort the rest of the processing and just return it for the call. // FARPROC pfnRet = NULL; if (__pfnDliNotifyHook2) { pfnRet = ((*__pfnDliNotifyHook2)(dliStartProcessing, &dli)); if (pfnRet != NULL) { goto HookBypass; } } // Check to see if we need to try to load the library. // if (hmod == 0) { if (__pfnDliNotifyHook2) { hmod = HMODULE(((*__pfnDliNotifyHook2)(dliNotePreLoadLibrary, &dli))); } if (hmod == 0) { hmod = ::LoadLibraryEx(dli.szDll, NULL, 0); } if (hmod == 0) { dli.dwLastError = ::GetLastError(); if (__pfnDliFailureHook2) { // when the hook is called on LoadLibrary failure, it will // return 0 for failure and an hmod for the lib if it fixed // the problem. // hmod = HMODULE((*__pfnDliFailureHook2)(dliFailLoadLib, &dli)); } if (hmod == 0) { PDelayLoadInfo rgpdli[1] = { &dli }; RaiseException( VcppException(ERROR_SEVERITY_ERROR, ERROR_MOD_NOT_FOUND), 0, 1, PULONG_PTR(rgpdli) ); // If we get to here, we blindly assume that the handler of the exception // has magically fixed everything up and left the function pointer in // dli.pfnCur. // return dli.pfnCur; } } // Store the library handle. If it is already there, we infer // that another thread got there first, and we need to do a // FreeLibrary() to reduce the refcount // HMODULE hmodT = HMODULE(InterlockedExchangePointer((PVOID *) idd.phmod, PVOID(hmod))); if (hmodT == hmod) { ::FreeLibrary(hmod); } } // Go for the procedure now. // dli.hmodCur = hmod; if (__pfnDliNotifyHook2) { pfnRet = (*__pfnDliNotifyHook2)(dliNotePreGetProcAddress, &dli); } if (pfnRet == 0) { if (pidd->rvaBoundIAT && pidd->dwTimeStamp) { // bound imports exist...check the timestamp from the target image // PIMAGE_NT_HEADERS pinh(PinhFromImageBase(hmod)); if (pinh->Signature == IMAGE_NT_SIGNATURE && TimeStampOfImage(pinh) == idd.dwTimeStamp && FLoadedAtPreferredAddress(pinh, hmod)) { // Everything is good to go, if we have a decent address // in the bound IAT! // pfnRet = FARPROC(UINT_PTR(idd.pBoundIAT[iIAT].u1.Function)); if (pfnRet != 0) { goto SetEntryHookBypass; } } } pfnRet = ::GetProcAddress(hmod, dli.dlp.szProcName); } if (pfnRet == 0) { dli.dwLastError = ::GetLastError(); if (__pfnDliFailureHook2) { // when the hook is called on GetProcAddress failure, it will // return 0 on failure and a valid proc address on success // pfnRet = (*__pfnDliFailureHook2)(dliFailGetProc, &dli); } if (pfnRet == 0) { PDelayLoadInfo rgpdli[1] = { &dli }; RaiseException( VcppException(ERROR_SEVERITY_ERROR, ERROR_PROC_NOT_FOUND), 0, 1, PULONG_PTR(rgpdli) ); // If we get to here, we blindly assume that the handler of the exception // has magically fixed everything up and left the function pointer in // dli.pfnCur. // pfnRet = dli.pfnCur; } } SetEntryHookBypass: *ppfnIATEntry = pfnRet; HookBypass: if (__pfnDliNotifyHook2) { dli.dwLastError = 0; dli.hmodCur = hmod; dli.pfnCur = pfnRet; (*__pfnDliNotifyHook2)(dliNoteEndProcessing, &dli); } return pfnRet; }