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/. */
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"
22 namespace crashreporter
{
24 class LSPAnnotationGatherer
: public nsRunnable
26 ~LSPAnnotationGatherer() {}
29 NS_DECL_THREADSAFE_ISUPPORTS
34 nsCOMPtr
<nsIThread
> mThread
;
37 NS_IMPL_ISUPPORTS(LSPAnnotationGatherer
, nsIRunnable
)
40 LSPAnnotationGatherer::Annotate()
42 nsCOMPtr
<nsICrashReporter
> cr
=
43 do_GetService("@mozilla.org/toolkit/crash-reporter;1");
45 if (cr
&& NS_SUCCEEDED(cr
->GetEnabled(&enabled
)) && enabled
) {
46 cr
->AnnotateCrashReport(NS_LITERAL_CSTRING("Winsock_LSP"), mString
);
52 LSPAnnotationGatherer::Run()
54 PR_SetCurrentThreadName("LSP Annotator");
56 mThread
= NS_GetCurrentThread();
60 // Get the size of the buffer we need
61 if (SOCKET_ERROR
!= WSCEnumProtocols(nullptr, nullptr, &size
, &err
) ||
64 NS_NOTREACHED("WSCEnumProtocols suceeded when it should have failed ...");
65 return NS_ERROR_FAILURE
;
68 nsAutoArrayPtr
<char> byteArray(new char[size
]);
69 WSAPROTOCOL_INFOW
* providers
=
70 reinterpret_cast<WSAPROTOCOL_INFOW
*>(byteArray
.get());
72 int n
= WSCEnumProtocols(nullptr, providers
, &size
, &err
);
73 if (n
== SOCKET_ERROR
) {
74 // Lame. We provided the right size buffer; we'll just give up now.
75 NS_WARNING("Could not get LSP list");
76 return NS_ERROR_FAILURE
;
80 for (int i
= 0; i
< n
; i
++) {
81 AppendUTF16toUTF8(nsDependentString(providers
[i
].szProtocol
), str
);
82 str
.AppendLiteral(" : ");
83 str
.AppendInt(providers
[i
].iVersion
);
84 str
.AppendLiteral(" : ");
85 str
.AppendInt(providers
[i
].iSocketType
);
86 str
.AppendLiteral(" : ");
88 wchar_t path
[MAX_PATH
];
90 if (!WSCGetProviderPath(&providers
[i
].ProviderId
, path
,
92 AppendUTF16toUTF8(nsDependentString(path
), str
);
96 str
.AppendLiteral(" \n ");
101 NS_DispatchToMainThread(NS_NewRunnableMethod(this, &LSPAnnotationGatherer::Annotate
));
107 nsCOMPtr
<nsIThread
> thread
;
108 nsCOMPtr
<nsIRunnable
> runnable
=
109 do_QueryObject(new LSPAnnotationGatherer());
110 NS_NewThread(getter_AddRefs(thread
), runnable
);
113 } // namespace crashreporter
114 } // namespace mozilla