File: delayload_hook_browsertest.cc

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (121 lines) | stat: -rw-r--r-- 4,055 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <windows.h>

#include <delayimp.h>
#include <roerrorapi.h>

#include "base/compiler_specific.h"
#include "base/run_loop.h"
#include "chrome/common/win/delay_load_notify_hook.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "content/public/browser/browser_child_process_observer.h"
#include "content/public/browser/child_process_data.h"
#include "content/public/browser/child_process_termination_info.h"
#include "content/public/browser/service_process_host.h"
#include "content/public/browser/service_process_info.h"
#include "content/public/test/browser_test.h"
#include "services/test/echo/public/mojom/echo.mojom.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/abseil-cpp/absl/cleanup/cleanup.h"

namespace chrome {

using ChromeDelayLoadHookTest = InProcessBrowserTest;

namespace {

bool g_callback_ran = false;
void WINAPI RoFailFastWithErrorContextPatch(HRESULT error) {
  g_callback_ran = true;
}

FARPROC TestDelayLoadCallbackFunction(unsigned delay_load_event,
                                      DelayLoadInfo* delay_load_info) {
  if (delay_load_event == dliNotePreGetProcAddress &&
      base::EqualsCaseInsensitiveASCII(
          delay_load_info->szDll, "api-ms-win-core-winrt-error-l1-1-0.dll") &&
      UNSAFE_TODO(strcmp(delay_load_info->dlp.szProcName,
                         "RoFailFastWithErrorContext")) == 0) {
    return reinterpret_cast<FARPROC>(RoFailFastWithErrorContextPatch);
  }

  return nullptr;
}

class EchoServiceProcessObserver : public content::ServiceProcessHost::Observer,
                                   public content::BrowserChildProcessObserver {
 public:
  EchoServiceProcessObserver() {
    content::ServiceProcessHost::AddObserver(this);
    content::BrowserChildProcessObserver::Add(this);
  }

  EchoServiceProcessObserver(const EchoServiceProcessObserver&) = delete;
  EchoServiceProcessObserver& operator=(const EchoServiceProcessObserver&) =
      delete;

  ~EchoServiceProcessObserver() override {
    content::BrowserChildProcessObserver::Remove(this);
    content::ServiceProcessHost::RemoveObserver(this);
  }

  void WaitForLaunch() { launch_loop_.Run(); }

  int WaitForCrash() {
    crash_loop_.Run();
    return exit_code_;
  }

 private:
  // content::ServiceProcessHost::Observer:
  void OnServiceProcessLaunched(
      const content::ServiceProcessInfo& info) override {
    if (info.IsService<echo::mojom::EchoService>())
      launch_loop_.Quit();
  }

  // content::BrowserChildProcessObserver:
  void BrowserChildProcessCrashed(
      const content::ChildProcessData& data,
      const content::ChildProcessTerminationInfo& info) override {
    if (data.metrics_name == echo::mojom::EchoService::Name_) {
      exit_code_ = info.exit_code;
      crash_loop_.Quit();
    }
  }

  int exit_code_;
  base::RunLoop launch_loop_;
  base::RunLoop crash_loop_;
};

}  // namespace

IN_PROC_BROWSER_TEST_F(ChromeDelayLoadHookTest, ObserveDelayLoadFailure) {
  EchoServiceProcessObserver observer;
  auto echo_service =
      content::ServiceProcessHost::Launch<echo::mojom::EchoService>();
  observer.WaitForLaunch();
  echo_service->DelayLoad();
  int exit_code = observer.WaitForCrash();
  EXPECT_EQ(EXCEPTION_BREAKPOINT, static_cast<DWORD>(exit_code));
}

// Override delayload behavior to redirect a Windows API call to our own
// implementation. Note we can only override functions that haven't been
// called or statically resolved before this point.
IN_PROC_BROWSER_TEST_F(ChromeDelayLoadHookTest, OverrideDelayloadBehavior) {
  absl::Cleanup reset_callback = [] { SetDelayLoadHookCallback(nullptr); };

  SetDelayLoadHookCallback(TestDelayLoadCallbackFunction);
  g_callback_ran = false;
  // Normally this API will crash the process. Since we've patched it, we
  // won't see a crash.
  ::RoFailFastWithErrorContext(E_FAIL);
  EXPECT_EQ(g_callback_ran, true);
}

}  // namespace chrome