summaryrefslogtreecommitdiffstats
path: root/toolkit/crashreporter/InjectCrashReporter.cpp
blob: 1b79f2cd13e7b1fe8898f5390e7c9ba1227979b6 (plain)
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
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "InjectCrashReporter.h"
#include "nsDirectoryServiceUtils.h"
#include "nsDirectoryServiceDefs.h"
#include "client/windows/crash_generation/crash_generation_client.h"
#include "nsExceptionHandler.h"
#include "LoadLibraryRemote.h"
#include "nsWindowsHelpers.h"

using google_breakpad::CrashGenerationClient;
using CrashReporter::GetChildNotificationPipe;

namespace mozilla {

InjectCrashRunnable::InjectCrashRunnable(DWORD pid)
  : mPID(pid)
{
  nsCOMPtr<nsIFile> dll;
  nsresult rv = NS_GetSpecialDirectory(NS_GRE_DIR, getter_AddRefs(dll));
  if (NS_SUCCEEDED(rv)) {
    dll->Append(NS_LITERAL_STRING("breakpadinjector.dll"));
    dll->GetPath(mInjectorPath);
  }
}

NS_IMETHODIMP
InjectCrashRunnable::Run()
{
  if (mInjectorPath.IsEmpty())
    return NS_OK;

  nsAutoHandle hProcess(
    OpenProcess(PROCESS_CREATE_THREAD |
                PROCESS_QUERY_INFORMATION |
                PROCESS_DUP_HANDLE |
                PROCESS_VM_OPERATION |
                PROCESS_VM_WRITE |
                PROCESS_VM_READ, FALSE, mPID));
  if (!hProcess) {
    NS_WARNING("Unable to open remote process handle for crashreporter injection.");
    return NS_OK;
  }

  void* proc = LoadRemoteLibraryAndGetAddress(hProcess, mInjectorPath.get(),
                                              "Start");
  if (!proc) {
    NS_WARNING("Unable to inject crashreporter DLL.");
    return NS_OK;
  }

  HANDLE hRemotePipe =
    CrashGenerationClient::DuplicatePipeToClientProcess(
      NS_ConvertASCIItoUTF16(GetChildNotificationPipe()).get(),
      hProcess);
  if (INVALID_HANDLE_VALUE == hRemotePipe) {
    NS_WARNING("Unable to duplicate crash reporter pipe to process.");
    return NS_OK;
  }

  nsAutoHandle hThread(CreateRemoteThread(hProcess, nullptr, 0,
                                          (LPTHREAD_START_ROUTINE) proc,
                                          (void*) hRemotePipe, 0, nullptr));
  if (!hThread) {
    NS_WARNING("Unable to CreateRemoteThread");

    // We have to close the remote pipe or else our crash generation client
    // will be stuck unable to accept other remote requests.
    HANDLE toClose = INVALID_HANDLE_VALUE;
    if (DuplicateHandle(hProcess, hRemotePipe, ::GetCurrentProcess(),
                        &toClose, 0, FALSE,
                        DUPLICATE_CLOSE_SOURCE | DUPLICATE_SAME_ACCESS)) {
      CloseHandle(toClose);
      return NS_OK;
    }
  }

  return NS_OK;
}

} // namespace mozilla