tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

SandboxLogging.h (3869B)


      1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
      2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
      3 /* This Source Code Form is subject to the terms of the Mozilla Public
      4 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
      5 * You can obtain one at http://mozilla.org/MPL/2.0/. */
      6 
      7 #ifndef mozilla_SandboxLogging_h
      8 #define mozilla_SandboxLogging_h
      9 
     10 // This header defines the SANDBOX_LOG macro used in the Linux
     11 // sandboxing code.  It uses Android logging on Android and writes to
     12 // stderr otherwise.  Android logging has severity levels; currently
     13 // only "error" severity is exposed here, and this isn't marked when
     14 // writing to stderr.
     15 //
     16 // The format strings are processed by Chromium SafeSPrintf, which
     17 // doesn't accept size modifiers or %u because it uses C++11 variadic
     18 // templates to obtain the actual argument types; all decimal integer
     19 // formatting uses %d.  See safe_sprintf.h for more details.
     20 
     21 // Build SafeSPrintf without assertions to avoid a dependency on
     22 // Chromium logging.  This doesn't affect safety; it just means that
     23 // type mismatches (pointer vs. integer) always result in unexpanded
     24 // %-directives instead of crashing.  See also the moz.build files,
     25 // which apply NDEBUG to the .cc file.
     26 #ifndef NDEBUG
     27 #  define NDEBUG 1
     28 #  include "base/strings/safe_sprintf.h"
     29 #  undef NDEBUG
     30 #else
     31 #  include "base/strings/safe_sprintf.h"
     32 #endif
     33 
     34 #include <errno.h>
     35 
     36 #include "SandboxProfiler.h"
     37 
     38 namespace mozilla {
     39 // Logs the formatted string (marked with "error" severity, if supported).
     40 void SandboxLogError(const char* aMessage);
     41 
     42 // Writes into aBuf the identifier for an error number (e.g., "EINVAL"
     43 // rather than "Invalid argument"); may fall back to "error N" (with
     44 // the number) for unhandled errors.
     45 //
     46 // Bounds are handled like snprintf: the return value is the length
     47 // the string would have (not counting the null terminator) ignoring
     48 // buffer size, and the string written into the buffer may be
     49 // truncated to fit but is always null terminated.
     50 ssize_t GetLibcErrorName(char* aBuf, size_t aSize, int aErr);
     51 }  // namespace mozilla
     52 
     53 #define SANDBOX_LOG_LEN 256
     54 
     55 // Formats a log message and logs it (with "error" severity, if supported).
     56 //
     57 // Note that SafeSPrintf doesn't accept size modifiers or %u; all
     58 // decimal integers are %d, because it uses C++11 variadic templates
     59 // to use the actual argument type.
     60 #define SANDBOX_LOG(fmt, args...)                              \
     61  do {                                                         \
     62    char _sandboxLogBuf[SANDBOX_LOG_LEN];                      \
     63    ::base::strings::SafeSPrintf(_sandboxLogBuf, fmt, ##args); \
     64    ::mozilla::SandboxLogError(_sandboxLogBuf);                \
     65    ::mozilla::SandboxProfiler::ReportLog(_sandboxLogBuf);     \
     66  } while (0)
     67 
     68 #define SANDBOX_LOG_WITH_ERROR(errnum, fmt, args...)                       \
     69  do {                                                                     \
     70    char _sandboxLogBuf[SANDBOX_LOG_LEN];                                  \
     71    ssize_t _sandboxLogOff =                                               \
     72        ::base::strings::SafeSPrintf(_sandboxLogBuf, fmt ": ", ##args);    \
     73    if (static_cast<size_t>(_sandboxLogOff) < sizeof(_sandboxLogBuf)) {    \
     74      ::mozilla::GetLibcErrorName(_sandboxLogBuf + _sandboxLogOff,         \
     75                                  sizeof(_sandboxLogBuf) - _sandboxLogOff, \
     76                                  errnum);                                 \
     77    }                                                                      \
     78    ::mozilla::SandboxLogError(_sandboxLogBuf);                            \
     79    ::mozilla::SandboxProfiler::ReportLog(_sandboxLogBuf);                 \
     80  } while (0)
     81 
     82 #define SANDBOX_LOG_ERRNO(fmt, args...) \
     83  SANDBOX_LOG_WITH_ERROR(errno, fmt, ##args)
     84 
     85 #endif  // mozilla_SandboxLogging_h