widget/windows/LSPAnnotator.cpp

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

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

mercurial