delay_load_failure_hook_win.cc 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. // Copyright 2022 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "chrome/app/delay_load_failure_hook_win.h"
  5. // windows.h needs to be included before delayimp.h.
  6. #include <windows.h>
  7. #include <delayimp.h>
  8. #include "base/check.h"
  9. #include "base/debug/alias.h"
  10. #include "base/strings/string_util.h"
  11. namespace chrome {
  12. namespace {
  13. bool g_hooks_enabled = true;
  14. // Delay load failure hook that generates a crash report. By default a failure
  15. // to delay load will trigger an exception handled by the delay load runtime and
  16. // this won't generate a crash report.
  17. FARPROC WINAPI DelayLoadFailureHookEXE(unsigned reason,
  18. DelayLoadInfo* dll_info) {
  19. if (!g_hooks_enabled)
  20. return 0;
  21. char dll_name[MAX_PATH];
  22. base::strlcpy(dll_name, dll_info->szDll, std::size(dll_name));
  23. base::debug::Alias(&dll_name);
  24. CHECK(false);
  25. return 0;
  26. }
  27. } // namespace
  28. void DisableDelayLoadFailureHooksForMainExecutable() {
  29. g_hooks_enabled = false;
  30. }
  31. } // namespace chrome
  32. // Set the delay load failure hook to the function above.
  33. //
  34. // The |__pfnDliFailureHook2| failure notification hook gets called
  35. // automatically by the delay load runtime in case of failure, see
  36. // https://docs.microsoft.com/en-us/cpp/build/reference/failure-hooks?view=vs-2019
  37. // for more information about this.
  38. extern "C" const PfnDliHook __pfnDliFailureHook2 =
  39. chrome::DelayLoadFailureHookEXE;