Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
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.
12 */
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
25 {
26 public:
27 NS_DECL_THREADSAFE_ISUPPORTS
28 NS_DECL_NSIRUNNABLE
30 void Annotate();
31 nsCString mString;
32 nsCOMPtr<nsIThread> mThread;
33 };
35 NS_IMPL_ISUPPORTS(LSPAnnotationGatherer, nsIRunnable)
37 void
38 LSPAnnotationGatherer::Annotate()
39 {
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);
45 }
46 mThread->Shutdown();
47 }
49 NS_IMETHODIMP
50 LSPAnnotationGatherer::Run()
51 {
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(nullptr, nullptr, &size, &err) ||
60 err != WSAENOBUFS) {
61 // Er, what?
62 NS_NOTREACHED("WSCEnumProtocols suceeded when it should have failed ...");
63 return NS_ERROR_FAILURE;
64 }
66 nsAutoArrayPtr<char> byteArray(new char[size]);
67 WSAPROTOCOL_INFOW* providers =
68 reinterpret_cast<WSAPROTOCOL_INFOW*>(byteArray.get());
70 int n = WSCEnumProtocols(nullptr, 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;
75 }
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);
91 }
93 if (i + 1 != n) {
94 str.AppendLiteral(" \n ");
95 }
96 }
98 mString = str;
99 NS_DispatchToMainThread(NS_NewRunnableMethod(this, &LSPAnnotationGatherer::Annotate));
100 return NS_OK;
101 }
103 void LSPAnnotate()
104 {
105 nsCOMPtr<nsIThread> thread;
106 nsCOMPtr<nsIRunnable> runnable =
107 do_QueryObject(new LSPAnnotationGatherer());
108 NS_NewThread(getter_AddRefs(thread), runnable);
109 }
111 } // namespace crashreporter
112 } // namespace mozilla