toolkit/crashreporter/google-breakpad/src/client/linux/handler/exception_handler.h

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 // Copyright (c) 2010 Google Inc.
     2 // All rights reserved.
     3 //
     4 // Redistribution and use in source and binary forms, with or without
     5 // modification, are permitted provided that the following conditions are
     6 // met:
     7 //
     8 //     * Redistributions of source code must retain the above copyright
     9 // notice, this list of conditions and the following disclaimer.
    10 //     * Redistributions in binary form must reproduce the above
    11 // copyright notice, this list of conditions and the following disclaimer
    12 // in the documentation and/or other materials provided with the
    13 // distribution.
    14 //     * Neither the name of Google Inc. nor the names of its
    15 // contributors may be used to endorse or promote products derived from
    16 // this software without specific prior written permission.
    17 //
    18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
    19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
    20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
    21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
    22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
    23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
    24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
    25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
    26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
    27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
    28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    30 #ifndef CLIENT_LINUX_HANDLER_EXCEPTION_HANDLER_H_
    31 #define CLIENT_LINUX_HANDLER_EXCEPTION_HANDLER_H_
    33 #include <string>
    34 #include <vector>
    36 #include <pthread.h>
    37 #include <signal.h>
    38 #include <stdint.h>
    39 #include <stdio.h>
    40 #include <sys/ucontext.h>
    42 #include "client/linux/crash_generation/crash_generation_client.h"
    43 #include "client/linux/handler/minidump_descriptor.h"
    44 #include "client/linux/minidump_writer/minidump_writer.h"
    45 #include "common/scoped_ptr.h"
    46 #include "common/using_std_string.h"
    47 #include "google_breakpad/common/minidump_format.h"
    49 namespace google_breakpad {
    51 // ExceptionHandler
    52 //
    53 // ExceptionHandler can write a minidump file when an exception occurs,
    54 // or when WriteMinidump() is called explicitly by your program.
    55 //
    56 // To have the exception handler write minidumps when an uncaught exception
    57 // (crash) occurs, you should create an instance early in the execution
    58 // of your program, and keep it around for the entire time you want to
    59 // have crash handling active (typically, until shutdown).
    60 // (NOTE): There should be only be one this kind of exception handler
    61 // object per process.
    62 //
    63 // If you want to write minidumps without installing the exception handler,
    64 // you can create an ExceptionHandler with install_handler set to false,
    65 // then call WriteMinidump.  You can also use this technique if you want to
    66 // use different minidump callbacks for different call sites.
    67 //
    68 // In either case, a callback function is called when a minidump is written,
    69 // which receives the full path or file descriptor of the minidump.  The
    70 // caller can collect and write additional application state to that minidump,
    71 // and launch an external crash-reporting application.
    72 //
    73 // Caller should try to make the callbacks as crash-friendly as possible,
    74 // it should avoid use heap memory allocation as much as possible.
    76 class ExceptionHandler {
    77  public:
    78   // A callback function to run before Breakpad performs any substantial
    79   // processing of an exception.  A FilterCallback is called before writing
    80   // a minidump.  |context| is the parameter supplied by the user as
    81   // callback_context when the handler was created.
    82   //
    83   // If a FilterCallback returns true, Breakpad will continue processing,
    84   // attempting to write a minidump.  If a FilterCallback returns false,
    85   // Breakpad  will immediately report the exception as unhandled without
    86   // writing a minidump, allowing another handler the opportunity to handle it.
    87   typedef bool (*FilterCallback)(void *context);
    89   // A callback function to run after the minidump has been written.
    90   // |descriptor| contains the file descriptor or file path containing the
    91   // minidump. |context| is the parameter supplied by the user as
    92   // callback_context when the handler was created.  |succeeded| indicates
    93   // whether a minidump file was successfully written.
    94   //
    95   // If an exception occurred and the callback returns true, Breakpad will
    96   // treat the exception as fully-handled, suppressing any other handlers from
    97   // being notified of the exception.  If the callback returns false, Breakpad
    98   // will treat the exception as unhandled, and allow another handler to handle
    99   // it. If there are no other handlers, Breakpad will report the exception to
   100   // the system as unhandled, allowing a debugger or native crash dialog the
   101   // opportunity to handle the exception.  Most callback implementations
   102   // should normally return the value of |succeeded|, or when they wish to
   103   // not report an exception of handled, false.  Callbacks will rarely want to
   104   // return true directly (unless |succeeded| is true).
   105   typedef bool (*MinidumpCallback)(const MinidumpDescriptor& descriptor,
   106                                    void* context,
   107                                    bool succeeded);
   109   // In certain cases, a user may wish to handle the generation of the minidump
   110   // themselves. In this case, they can install a handler callback which is
   111   // called when a crash has occurred. If this function returns true, no other
   112   // processing of occurs and the process will shortly be crashed. If this
   113   // returns false, the normal processing continues.
   114   typedef bool (*HandlerCallback)(const void* crash_context,
   115                                   size_t crash_context_size,
   116                                   void* context);
   118   // Creates a new ExceptionHandler instance to handle writing minidumps.
   119   // Before writing a minidump, the optional |filter| callback will be called.
   120   // Its return value determines whether or not Breakpad should write a
   121   // minidump.  The minidump content will be written to the file path or file
   122   // descriptor from |descriptor|, and the optional |callback| is called after
   123   // writing the dump file, as described above.
   124   // If install_handler is true, then a minidump will be written whenever
   125   // an unhandled exception occurs.  If it is false, minidumps will only
   126   // be written when WriteMinidump is called.
   127   // If |server_fd| is valid, the minidump is generated out-of-process.  If it
   128   // is -1, in-process generation will always be used.
   129   ExceptionHandler(const MinidumpDescriptor& descriptor,
   130                    FilterCallback filter,
   131                    MinidumpCallback callback,
   132                    void *callback_context,
   133                    bool install_handler,
   134                    const int server_fd);
   135   ~ExceptionHandler();
   137   const MinidumpDescriptor& minidump_descriptor() const {
   138     return minidump_descriptor_;
   139   }
   141   void set_minidump_descriptor(const MinidumpDescriptor& descriptor) {
   142     minidump_descriptor_ = descriptor;
   143   }
   145   void set_crash_handler(HandlerCallback callback) {
   146     crash_handler_ = callback;
   147   }
   149   // Writes a minidump immediately.  This can be used to capture the execution
   150   // state independently of a crash.
   151   // Returns true on success.
   152   // If the ExceptionHandler has been created with a path, a new file is
   153   // generated for each minidump.  The file path can be retrieved in the
   154   // MinidumpDescriptor passed to the MinidumpCallback or by accessing the
   155   // MinidumpDescriptor directly from the ExceptionHandler (with
   156   // minidump_descriptor()).
   157   // If the ExceptionHandler has been created with a file descriptor, the file
   158   // descriptor is repositioned to its beginning and the previous generated
   159   // minidump is overwritten.
   160   // Note that this method is not supposed to be called from a compromised
   161   // context as it uses the heap.
   162   bool WriteMinidump();
   164   // Convenience form of WriteMinidump which does not require an
   165   // ExceptionHandler instance.
   166   static bool WriteMinidump(const string& dump_path,
   167                             MinidumpCallback callback,
   168                             void* callback_context);
   170   // Write a minidump of |child| immediately.  This can be used to
   171   // capture the execution state of |child| independently of a crash.
   172   // Pass a meaningful |child_blamed_thread| to make that thread in
   173   // the child process the one from which a crash signature is
   174   // extracted.
   175   //
   176   // WARNING: the return of this function *must* happen before
   177   // the code that will eventually reap |child| executes.
   178   // Otherwise there's a pernicious race condition in which |child|
   179   // exits, is reaped, another process created with its pid, then that
   180   // new process dumped.
   181   static bool WriteMinidumpForChild(pid_t child,
   182                                     pid_t child_blamed_thread,
   183                                     const string& dump_path,
   184                                     MinidumpCallback callback,
   185                                     void* callback_context);
   187   // This structure is passed to minidump_writer.h:WriteMinidump via an opaque
   188   // blob. It shouldn't be needed in any user code.
   189   struct CrashContext {
   190     siginfo_t siginfo;
   191     pid_t tid;  // the crashing thread.
   192     struct ucontext context;
   193 #if !defined(__ARM_EABI__)
   194     // #ifdef this out because FP state is not part of user ABI for Linux ARM.
   195     struct _libc_fpstate float_state;
   196 #endif
   197   };
   199   // Returns whether out-of-process dump generation is used or not.
   200   bool IsOutOfProcess() const {
   201       return crash_generation_client_.get() != NULL;
   202   }
   204   // Add information about a memory mapping. This can be used if
   205   // a custom library loader is used that maps things in a way
   206   // that the linux dumper can't handle by reading the maps file.
   207   void AddMappingInfo(const string& name,
   208                       const uint8_t identifier[sizeof(MDGUID)],
   209                       uintptr_t start_address,
   210                       size_t mapping_size,
   211                       size_t file_offset);
   213   // Register a block of memory of length bytes starting at address ptr
   214   // to be copied to the minidump when a crash happens.
   215   void RegisterAppMemory(void* ptr, size_t length);
   217   // Unregister a block of memory that was registered with RegisterAppMemory.
   218   void UnregisterAppMemory(void* ptr);
   220   // Force signal handling for the specified signal.
   221   bool SimulateSignalDelivery(int sig);
   223   // Report a crash signal from an SA_SIGINFO signal handler.
   224   bool HandleSignal(int sig, siginfo_t* info, void* uc);
   225  private:
   226   // Save the old signal handlers and install new ones.
   227   static bool InstallHandlersLocked();
   228   // Restore the old signal handlers.
   229   static void RestoreHandlersLocked();
   231   void PreresolveSymbols();
   232   bool GenerateDump(CrashContext *context);
   233   void SendContinueSignalToChild();
   234   void WaitForContinueSignal();
   236   static void SignalHandler(int sig, siginfo_t* info, void* uc);
   237   static int ThreadEntry(void* arg);
   238   bool DoDump(pid_t crashing_process, const void* context,
   239               size_t context_size);
   241   const FilterCallback filter_;
   242   const MinidumpCallback callback_;
   243   void* const callback_context_;
   245   scoped_ptr<CrashGenerationClient> crash_generation_client_;
   247   MinidumpDescriptor minidump_descriptor_;
   249   HandlerCallback crash_handler_;
   251   // The global exception handler stack. This is need becuase there may exist
   252   // multiple ExceptionHandler instances in a process. Each will have itself
   253   // registered in this stack.
   254   static std::vector<ExceptionHandler*> *handler_stack_;
   255   static pthread_mutex_t handler_stack_mutex_;
   257   // We need to explicitly enable ptrace of parent processes on some
   258   // kernels, but we need to know the PID of the cloned process before we
   259   // can do this. We create a pipe which we can use to block the
   260   // cloned process after creating it, until we have explicitly enabled 
   261   // ptrace. This is used to store the file descriptors for the pipe
   262   int fdes[2];
   264   // Callers can add extra info about mappings for cases where the
   265   // dumper code cannot extract enough information from /proc/<pid>/maps.
   266   MappingList mapping_list_;
   268   // Callers can request additional memory regions to be included in
   269   // the dump.
   270   AppMemoryList app_memory_list_;
   271 };
   273 }  // namespace google_breakpad
   275 #endif  // CLIENT_LINUX_HANDLER_EXCEPTION_HANDLER_H_

mercurial