xpcom/base/nsDebugImpl.cpp

Sat, 03 Jan 2015 20:18:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Sat, 03 Jan 2015 20:18:00 +0100
branch
TOR_BUG_3246
changeset 7
129ffea94266
permissions
-rw-r--r--

Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.

     1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     2 /* This Source Code Form is subject to the terms of the Mozilla Public
     3  * License, v. 2.0. If a copy of the MPL was not distributed with this
     4  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     6 // Chromium headers must come before Mozilla headers.
     7 #include "base/process_util.h"
     9 #include "mozilla/Atomics.h"
    11 #include "nsDebugImpl.h"
    12 #include "nsDebug.h"
    13 #ifdef MOZ_CRASHREPORTER
    14 # include "nsExceptionHandler.h"
    15 #endif
    16 #include "nsString.h"
    17 #include "prprf.h"
    18 #include "prlog.h"
    19 #include "nsError.h"
    20 #include "prerror.h"
    21 #include "prerr.h"
    22 #include "prenv.h"
    24 #ifdef ANDROID
    25 #include <android/log.h>
    26 #endif
    28 #ifdef _WIN32
    29 /* for getenv() */
    30 #include <stdlib.h>
    31 #endif
    33 #include "nsTraceRefcnt.h"
    35 #if defined(XP_UNIX)
    36 #include <signal.h>
    37 #endif
    39 #if defined(XP_WIN)
    40 #include <tchar.h>
    41 #include "nsString.h"
    42 #ifdef MOZ_METRO
    43 #include "nsWindowsHelpers.h"
    44 #endif
    45 #endif
    47 #if defined(XP_MACOSX)
    48 #include <stdbool.h>
    49 #include <unistd.h>
    50 #include <sys/sysctl.h>
    51 #endif
    53 #include "mozilla/mozalloc_abort.h"
    55 static void
    56 Abort(const char *aMsg);
    58 static void
    59 RealBreak();
    61 static void
    62 Break(const char *aMsg);
    64 #if defined(_WIN32)
    65 #include <windows.h>
    66 #include <signal.h>
    67 #include <malloc.h> // for _alloca
    68 #elif defined(XP_UNIX)
    69 #include <stdlib.h>
    70 #endif
    72 using namespace mozilla;
    74 static const char *sMultiprocessDescription = nullptr;
    76 static Atomic<int32_t> gAssertionCount;
    78 NS_IMPL_QUERY_INTERFACE(nsDebugImpl, nsIDebug, nsIDebug2)
    80 NS_IMETHODIMP_(MozExternalRefCountType)
    81 nsDebugImpl::AddRef()
    82 {
    83   return 2;
    84 }
    86 NS_IMETHODIMP_(MozExternalRefCountType)
    87 nsDebugImpl::Release()
    88 {
    89   return 1;
    90 }
    92 NS_IMETHODIMP
    93 nsDebugImpl::Assertion(const char *aStr, const char *aExpr,
    94                        const char *aFile, int32_t aLine)
    95 {
    96   NS_DebugBreak(NS_DEBUG_ASSERTION, aStr, aExpr, aFile, aLine);
    97   return NS_OK;
    98 }
   100 NS_IMETHODIMP
   101 nsDebugImpl::Warning(const char *aStr, const char *aFile, int32_t aLine)
   102 {
   103   NS_DebugBreak(NS_DEBUG_WARNING, aStr, nullptr, aFile, aLine);
   104   return NS_OK;
   105 }
   107 NS_IMETHODIMP
   108 nsDebugImpl::Break(const char *aFile, int32_t aLine)
   109 {
   110   NS_DebugBreak(NS_DEBUG_BREAK, nullptr, nullptr, aFile, aLine);
   111   return NS_OK;
   112 }
   114 NS_IMETHODIMP
   115 nsDebugImpl::Abort(const char *aFile, int32_t aLine)
   116 {
   117   NS_DebugBreak(NS_DEBUG_ABORT, nullptr, nullptr, aFile, aLine);
   118   return NS_OK;
   119 }
   121 NS_IMETHODIMP
   122 nsDebugImpl::GetIsDebugBuild(bool* aResult)
   123 {
   124 #ifdef DEBUG
   125   *aResult = true;
   126 #else
   127   *aResult = false;
   128 #endif
   129   return NS_OK;
   130 }
   132 NS_IMETHODIMP
   133 nsDebugImpl::GetAssertionCount(int32_t* aResult)
   134 {
   135   *aResult = gAssertionCount;
   136   return NS_OK;
   137 }
   139 NS_IMETHODIMP
   140 nsDebugImpl::GetIsDebuggerAttached(bool* aResult)
   141 {
   142   *aResult = false;
   144 #if defined(XP_WIN)
   145   *aResult = ::IsDebuggerPresent();
   146 #elif defined(XP_MACOSX)
   147   // Specify the info we're looking for
   148   int mib[4];
   149   mib[0] = CTL_KERN;
   150   mib[1] = KERN_PROC;
   151   mib[2] = KERN_PROC_PID;
   152   mib[3] = getpid();
   153   size_t mibSize = sizeof(mib) / sizeof(int);
   155   struct kinfo_proc info;
   156   size_t infoSize = sizeof(info);
   157   memset(&info, 0, infoSize);
   159   if (sysctl(mib, mibSize, &info, &infoSize, nullptr, 0)) {
   160     // if the call fails, default to false
   161     *aResult = false;
   162     return NS_OK;
   163   }
   165   if (info.kp_proc.p_flag & P_TRACED) {
   166     *aResult = true;
   167   }
   168 #endif
   170   return NS_OK;
   171 }
   173 /* static */ void
   174 nsDebugImpl::SetMultiprocessMode(const char *aDesc)
   175 {
   176   sMultiprocessDescription = aDesc;
   177 }
   179 /**
   180  * Implementation of the nsDebug methods. Note that this code is
   181  * always compiled in, in case some other module that uses it is
   182  * compiled with debugging even if this library is not.
   183  */
   184 static PRLogModuleInfo* gDebugLog;
   186 static void InitLog(void)
   187 {
   188   if (0 == gDebugLog) {
   189     gDebugLog = PR_NewLogModule("nsDebug");
   190   }
   191 }
   193 enum nsAssertBehavior {
   194   NS_ASSERT_UNINITIALIZED,
   195   NS_ASSERT_WARN,
   196   NS_ASSERT_SUSPEND,
   197   NS_ASSERT_STACK,
   198   NS_ASSERT_TRAP,
   199   NS_ASSERT_ABORT,
   200   NS_ASSERT_STACK_AND_ABORT
   201 };
   203 static nsAssertBehavior GetAssertBehavior()
   204 {
   205   static nsAssertBehavior gAssertBehavior = NS_ASSERT_UNINITIALIZED;
   206   if (gAssertBehavior != NS_ASSERT_UNINITIALIZED)
   207     return gAssertBehavior;
   209 #if defined(XP_WIN) && defined(MOZ_METRO)
   210   if (IsRunningInWindowsMetro())
   211     gAssertBehavior = NS_ASSERT_WARN;
   212   else
   213     gAssertBehavior = NS_ASSERT_TRAP;
   214 #elif defined(XP_WIN)
   215   gAssertBehavior = NS_ASSERT_TRAP;
   216 #else
   217   gAssertBehavior = NS_ASSERT_WARN;
   218 #endif
   220   const char *assertString = PR_GetEnv("XPCOM_DEBUG_BREAK");
   221   if (!assertString || !*assertString)
   222     return gAssertBehavior;
   224    if (!strcmp(assertString, "warn"))
   225      return gAssertBehavior = NS_ASSERT_WARN;
   227    if (!strcmp(assertString, "suspend"))
   228      return gAssertBehavior = NS_ASSERT_SUSPEND;
   230    if (!strcmp(assertString, "stack"))
   231      return gAssertBehavior = NS_ASSERT_STACK;
   233    if (!strcmp(assertString, "abort"))
   234      return gAssertBehavior = NS_ASSERT_ABORT;
   236    if (!strcmp(assertString, "trap") || !strcmp(assertString, "break"))
   237      return gAssertBehavior = NS_ASSERT_TRAP;
   239    if (!strcmp(assertString, "stack-and-abort"))
   240      return gAssertBehavior = NS_ASSERT_STACK_AND_ABORT;
   242    fprintf(stderr, "Unrecognized value of XPCOM_DEBUG_BREAK\n");
   243    return gAssertBehavior;
   244 }
   246 struct FixedBuffer
   247 {
   248   FixedBuffer() : curlen(0) { buffer[0] = '\0'; }
   250   char buffer[1000];
   251   uint32_t curlen;
   252 };
   254 static int
   255 StuffFixedBuffer(void *closure, const char *buf, uint32_t len)
   256 {
   257   if (!len)
   258     return 0;
   260   FixedBuffer *fb = (FixedBuffer*) closure;
   262   // strip the trailing null, we add it again later
   263   if (buf[len - 1] == '\0')
   264     --len;
   266   if (fb->curlen + len >= sizeof(fb->buffer))
   267     len = sizeof(fb->buffer) - fb->curlen - 1;
   269   if (len) {
   270     memcpy(fb->buffer + fb->curlen, buf, len);
   271     fb->curlen += len;
   272     fb->buffer[fb->curlen] = '\0';
   273   }
   275   return len;
   276 }
   278 EXPORT_XPCOM_API(void)
   279 NS_DebugBreak(uint32_t aSeverity, const char *aStr, const char *aExpr,
   280               const char *aFile, int32_t aLine)
   281 {
   282    InitLog();
   284    FixedBuffer buf;
   285    PRLogModuleLevel ll = PR_LOG_WARNING;
   286    const char *sevString = "WARNING";
   288    switch (aSeverity) {
   289    case NS_DEBUG_ASSERTION:
   290      sevString = "###!!! ASSERTION";
   291      ll = PR_LOG_ERROR;
   292      break;
   294    case NS_DEBUG_BREAK:
   295      sevString = "###!!! BREAK";
   296      ll = PR_LOG_ALWAYS;
   297      break;
   299    case NS_DEBUG_ABORT:
   300      sevString = "###!!! ABORT";
   301      ll = PR_LOG_ALWAYS;
   302      break;
   304    default:
   305      aSeverity = NS_DEBUG_WARNING;
   306    };
   308 #  define PrintToBuffer(...) PR_sxprintf(StuffFixedBuffer, &buf, __VA_ARGS__)
   310    // Print "[PID]" or "[Desc PID]" at the beginning of the message.
   311    PrintToBuffer("[");
   312    if (sMultiprocessDescription) {
   313      PrintToBuffer("%s ", sMultiprocessDescription);
   314    }
   315    PrintToBuffer("%d] ", base::GetCurrentProcId());
   317    PrintToBuffer("%s: ", sevString);
   319    if (aStr)
   320      PrintToBuffer("%s: ", aStr);
   322    if (aExpr)
   323      PrintToBuffer("'%s', ", aExpr);
   325    if (aFile)
   326      PrintToBuffer("file %s, ", aFile);
   328    if (aLine != -1)
   329      PrintToBuffer("line %d", aLine);
   331 #  undef PrintToBuffer
   333    // Write out the message to the debug log
   334    PR_LOG(gDebugLog, ll, ("%s", buf.buffer));
   335    PR_LogFlush();
   337    // errors on platforms without a debugdlg ring a bell on stderr
   338 #if !defined(XP_WIN)
   339    if (ll != PR_LOG_WARNING)
   340      fprintf(stderr, "\07");
   341 #endif
   343 #ifdef ANDROID
   344    __android_log_print(ANDROID_LOG_INFO, "Gecko", "%s", buf.buffer);
   345 #endif
   347    // Write the message to stderr unless it's a warning and MOZ_IGNORE_WARNINGS
   348    // is set.
   349    if (!(PR_GetEnv("MOZ_IGNORE_WARNINGS") && aSeverity == NS_DEBUG_WARNING)) {
   350      fprintf(stderr, "%s\n", buf.buffer);
   351      fflush(stderr);
   352    }
   354    switch (aSeverity) {
   355    case NS_DEBUG_WARNING:
   356      return;
   358    case NS_DEBUG_BREAK:
   359      Break(buf.buffer);
   360      return;
   362    case NS_DEBUG_ABORT: {
   363 #if defined(MOZ_CRASHREPORTER)
   364      nsCString note("xpcom_runtime_abort(");
   365      note += buf.buffer;
   366      note += ")";
   367      CrashReporter::AppendAppNotesToCrashReport(note);
   368      CrashReporter::AnnotateCrashReport(NS_LITERAL_CSTRING("AbortMessage"),
   369                                         nsDependentCString(buf.buffer));
   370 #endif  // MOZ_CRASHREPORTER
   372 #if defined(DEBUG) && defined(_WIN32)
   373      RealBreak();
   374 #endif
   375 #ifdef DEBUG
   376      nsTraceRefcnt::WalkTheStack(stderr);
   377 #endif
   378      Abort(buf.buffer);
   379      return;
   380    }
   381    }
   383    // Now we deal with assertions
   384    gAssertionCount++;
   386    switch (GetAssertBehavior()) {
   387    case NS_ASSERT_WARN:
   388      return;
   390    case NS_ASSERT_SUSPEND:
   391 #ifdef XP_UNIX
   392       fprintf(stderr, "Suspending process; attach with the debugger.\n");
   393       kill(0, SIGSTOP);
   394 #else
   395       Break(buf.buffer);
   396 #endif
   397       return;
   399    case NS_ASSERT_STACK:
   400      nsTraceRefcnt::WalkTheStack(stderr);
   401      return;
   403    case NS_ASSERT_STACK_AND_ABORT:
   404      nsTraceRefcnt::WalkTheStack(stderr);
   405      // Fall through to abort
   407    case NS_ASSERT_ABORT:
   408      Abort(buf.buffer);
   409      return;
   411    case NS_ASSERT_TRAP:
   412    case NS_ASSERT_UNINITIALIZED: // Default to "trap" behavior
   413      Break(buf.buffer);
   414      return;
   415    }   
   416 }
   418 static void
   419 Abort(const char *aMsg)
   420 {
   421   mozalloc_abort(aMsg);
   422 }
   424 static void
   425 RealBreak()
   426 {
   427 #if defined(_WIN32)
   428   ::DebugBreak();
   429 #elif defined(XP_MACOSX)
   430    raise(SIGTRAP);
   431 #elif defined(__GNUC__) && (defined(__i386__) || defined(__i386) || defined(__x86_64__))
   432    asm("int $3");
   433 #elif defined(__arm__)
   434    asm(
   435 #ifdef __ARM_ARCH_4T__
   436 /* ARMv4T doesn't support the BKPT instruction, so if the compiler target
   437  * is ARMv4T, we want to ensure the assembler will understand that ARMv5T
   438  * instruction, while keeping the resulting object tagged as ARMv4T.
   439  */
   440        ".arch armv5t\n"
   441        ".object_arch armv4t\n"
   442 #endif
   443        "BKPT #0");
   444 #elif defined(SOLARIS)
   445 #if defined(__i386__) || defined(__i386) || defined(__x86_64__)
   446    asm("int $3");
   447 #else
   448    raise(SIGTRAP);
   449 #endif
   450 #else
   451 #warning do not know how to break on this platform
   452 #endif
   453 }
   455 // Abort() calls this function, don't call it!
   456 static void
   457 Break(const char *aMsg)
   458 {
   459 #if defined(_WIN32)
   460   static int ignoreDebugger;
   461   if (!ignoreDebugger) {
   462     const char *shouldIgnoreDebugger = getenv("XPCOM_DEBUG_DLG");
   463     ignoreDebugger = 1 + (shouldIgnoreDebugger && !strcmp(shouldIgnoreDebugger, "1"));
   464   }
   465   if ((ignoreDebugger == 2) || !::IsDebuggerPresent()) {
   466     DWORD code = IDRETRY;
   468     /* Create the debug dialog out of process to avoid the crashes caused by 
   469      * Windows events leaking into our event loop from an in process dialog.
   470      * We do this by launching windbgdlg.exe (built in xpcom/windbgdlg).
   471      * See http://bugzilla.mozilla.org/show_bug.cgi?id=54792
   472      */
   473     PROCESS_INFORMATION pi;
   474     STARTUPINFOW si;
   475     wchar_t executable[MAX_PATH];
   476     wchar_t* pName;
   478     memset(&pi, 0, sizeof(pi));
   480     memset(&si, 0, sizeof(si));
   481     si.cb          = sizeof(si);
   482     si.wShowWindow = SW_SHOW;
   484     // 2nd arg of CreateProcess is in/out
   485     wchar_t *msgCopy = (wchar_t*) _alloca((strlen(aMsg) + 1)*sizeof(wchar_t));
   486     wcscpy(msgCopy, NS_ConvertUTF8toUTF16(aMsg).get());
   488     if(GetModuleFileNameW(GetModuleHandleW(L"xpcom.dll"), executable, MAX_PATH) &&
   489        nullptr != (pName = wcsrchr(executable, '\\')) &&
   490        nullptr != wcscpy(pName + 1, L"windbgdlg.exe") &&
   491        CreateProcessW(executable, msgCopy, nullptr, nullptr,
   492                       false, DETACHED_PROCESS | NORMAL_PRIORITY_CLASS,
   493                       nullptr, nullptr, &si, &pi)) {
   494       WaitForSingleObject(pi.hProcess, INFINITE);
   495       GetExitCodeProcess(pi.hProcess, &code);
   496       CloseHandle(pi.hProcess);
   497       CloseHandle(pi.hThread);
   498     }
   500     switch(code) {
   501     case IDABORT:
   502       //This should exit us
   503       raise(SIGABRT);
   504       //If we are ignored exit this way..
   505       _exit(3);
   507     case IDIGNORE:
   508       return;
   509     }
   510   }
   512   RealBreak();
   513 #elif defined(XP_MACOSX)
   514    /* Note that we put this Mac OS X test above the GNUC/x86 test because the
   515     * GNUC/x86 test is also true on Intel Mac OS X and we want the PPC/x86
   516     * impls to be the same.
   517     */
   518    RealBreak();
   519 #elif defined(__GNUC__) && (defined(__i386__) || defined(__i386) || defined(__x86_64__))
   520    RealBreak();
   521 #elif defined(__arm__)
   522    RealBreak();
   523 #elif defined(SOLARIS)
   524    RealBreak();
   525 #else
   526 #warning do not know how to break on this platform
   527 #endif
   528 }
   530 static const nsDebugImpl kImpl;
   532 nsresult
   533 nsDebugImpl::Create(nsISupports* outer, const nsIID& aIID, void* *aInstancePtr)
   534 {
   535   if (NS_WARN_IF(outer))
   536     return NS_ERROR_NO_AGGREGATION;
   538   return const_cast<nsDebugImpl*>(&kImpl)->
   539     QueryInterface(aIID, aInstancePtr);
   540 }
   542 ////////////////////////////////////////////////////////////////////////////////
   544 nsresult
   545 NS_ErrorAccordingToNSPR()
   546 {
   547     PRErrorCode err = PR_GetError();
   548     switch (err) {
   549       case PR_OUT_OF_MEMORY_ERROR:              return NS_ERROR_OUT_OF_MEMORY;
   550       case PR_WOULD_BLOCK_ERROR:                return NS_BASE_STREAM_WOULD_BLOCK;
   551       case PR_FILE_NOT_FOUND_ERROR:             return NS_ERROR_FILE_NOT_FOUND;
   552       case PR_READ_ONLY_FILESYSTEM_ERROR:       return NS_ERROR_FILE_READ_ONLY;
   553       case PR_NOT_DIRECTORY_ERROR:              return NS_ERROR_FILE_NOT_DIRECTORY;
   554       case PR_IS_DIRECTORY_ERROR:               return NS_ERROR_FILE_IS_DIRECTORY;
   555       case PR_LOOP_ERROR:                       return NS_ERROR_FILE_UNRESOLVABLE_SYMLINK;
   556       case PR_FILE_EXISTS_ERROR:                return NS_ERROR_FILE_ALREADY_EXISTS;
   557       case PR_FILE_IS_LOCKED_ERROR:             return NS_ERROR_FILE_IS_LOCKED;
   558       case PR_FILE_TOO_BIG_ERROR:               return NS_ERROR_FILE_TOO_BIG;
   559       case PR_NO_DEVICE_SPACE_ERROR:            return NS_ERROR_FILE_NO_DEVICE_SPACE;
   560       case PR_NAME_TOO_LONG_ERROR:              return NS_ERROR_FILE_NAME_TOO_LONG;
   561       case PR_DIRECTORY_NOT_EMPTY_ERROR:        return NS_ERROR_FILE_DIR_NOT_EMPTY;
   562       case PR_NO_ACCESS_RIGHTS_ERROR:           return NS_ERROR_FILE_ACCESS_DENIED;
   563       default:                                  return NS_ERROR_FAILURE;
   564     }
   565 }
   567 void
   568 NS_ABORT_OOM(size_t size)
   569 {
   570 #ifdef MOZ_CRASHREPORTER
   571   CrashReporter::AnnotateOOMAllocationSize(size);
   572 #endif
   573   MOZ_CRASH();
   574 }

mercurial