Bug 867104 - Add a crashtest. r=ehsan
[gecko.git] / widget / windows / LSPAnnotator.cpp
blobe6b300e8723da5d05457bdd4b6258b610d2431b1
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 /**
6 * LSPs are evil little bits of code that are allowed to inject into our
7 * networking stack by Windows. Once they have wormed into our process
8 * they gnaw at our innards until we crash. Here we force the buggers
9 * into the light by recording them in our crash information.
10 * We do the enumeration on a thread because I'm concerned about startup perf
11 * on machines with several LSPs.
14 #include "nsICrashReporter.h"
15 #include "nsISupportsImpl.h"
16 #include "nsServiceManagerUtils.h"
17 #include "nsThreadUtils.h"
18 #include <windows.h>
19 #include <ws2spi.h>
21 namespace mozilla {
22 namespace crashreporter {
24 class LSPAnnotationGatherer : public nsRunnable
26 public:
27 NS_DECL_ISUPPORTS
28 NS_DECL_NSIRUNNABLE
30 void Annotate();
31 nsCString mString;
32 nsCOMPtr<nsIThread> mThread;
35 NS_IMPL_THREADSAFE_ISUPPORTS1(LSPAnnotationGatherer, nsIRunnable)
37 void
38 LSPAnnotationGatherer::Annotate()
40 nsCOMPtr<nsICrashReporter> cr =
41 do_GetService("@mozilla.org/toolkit/crash-reporter;1");
42 bool enabled;
43 if (cr && NS_SUCCEEDED(cr->GetEnabled(&enabled)) && enabled) {
44 cr->AnnotateCrashReport(NS_LITERAL_CSTRING("Winsock_LSP"), mString);
46 mThread->Shutdown();
49 NS_IMETHODIMP
50 LSPAnnotationGatherer::Run()
52 PR_SetCurrentThreadName("LSP Annotator");
54 mThread = NS_GetCurrentThread();
56 DWORD size = 0;
57 int err;
58 // Get the size of the buffer we need
59 if (SOCKET_ERROR != WSCEnumProtocols(NULL, NULL, &size, &err) ||
60 err != WSAENOBUFS) {
61 // Er, what?
62 NS_NOTREACHED("WSCEnumProtocols suceeded when it should have failed ...");
63 return NS_ERROR_FAILURE;
66 nsAutoArrayPtr<char> byteArray(new char[size]);
67 WSAPROTOCOL_INFOW* providers =
68 reinterpret_cast<WSAPROTOCOL_INFOW*>(byteArray.get());
70 int n = WSCEnumProtocols(NULL, providers, &size, &err);
71 if (n == SOCKET_ERROR) {
72 // Lame. We provided the right size buffer; we'll just give up now.
73 NS_WARNING("Could not get LSP list");
74 return NS_ERROR_FAILURE;
77 nsCString str;
78 for (int i = 0; i < n; i++) {
79 AppendUTF16toUTF8(nsDependentString(providers[i].szProtocol), str);
80 str.AppendLiteral(" : ");
81 str.AppendInt(providers[i].iVersion);
82 str.AppendLiteral(" : ");
83 str.AppendInt(providers[i].iSocketType);
84 str.AppendLiteral(" : ");
86 wchar_t path[MAX_PATH];
87 int dummy;
88 if (!WSCGetProviderPath(&providers[i].ProviderId, path,
89 &dummy, &err)) {
90 AppendUTF16toUTF8(nsDependentString(path), str);
93 if (i + 1 != n) {
94 str.AppendLiteral(" \n ");
98 mString = str;
99 NS_DispatchToMainThread(NS_NewRunnableMethod(this, &LSPAnnotationGatherer::Annotate));
100 return NS_OK;
103 void LSPAnnotate()
105 nsCOMPtr<nsIThread> thread;
106 nsCOMPtr<nsIRunnable> runnable =
107 do_QueryObject(new LSPAnnotationGatherer());
108 NS_NewThread(getter_AddRefs(thread), runnable);
111 } // namespace crashreporter
112 } // namespace mozilla