• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2019 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include "sandboxed_api/util/raw_logging.h"
16 
17 #include <unistd.h>
18 
19 #include <cstdlib>
20 #include <limits>
21 
22 #include "absl/strings/numbers.h"
23 
24 #ifndef SAPI_USE_ABSL_RAW_LOG
25 #include <syscall.h>
26 
27 #include <cstdarg>
28 #include <cstdio>
29 #include <cstring>
30 
31 #include "absl/base/attributes.h"
32 #include "absl/base/log_severity.h"
33 
34 namespace {
35 static const char kTruncated[] = " ... (message truncated)\n";
36 
37 // sprintf the format to the buffer, adjusting *buf and *size to reflect the
38 // consumed bytes, and return whether the message fit without truncation.  If
39 // truncation occurred, if possible leave room in the buffer for the message
40 // kTruncated[].
41 inline static bool VADoRawLog(char** buf, int* size, const char* format,
42                               va_list ap) ABSL_PRINTF_ATTRIBUTE(3, 0);
VADoRawLog(char ** buf,int * size,const char * format,va_list ap)43 inline static bool VADoRawLog(char** buf, int* size, const char* format,
44                               va_list ap) {
45   int n = vsnprintf(*buf, *size, format, ap);
46   bool result = true;
47   if (n < 0 || n > *size) {
48     result = false;
49     if (static_cast<size_t>(*size) > sizeof(kTruncated)) {
50       n = *size - sizeof(kTruncated);  // room for truncation message
51     } else {
52       n = 0;  // no room for truncation message
53     }
54   }
55   *size -= n;
56   *buf += n;
57   return result;
58 }
59 
60 // CAVEAT: vsnprintf called from *DoRawLog below has some (exotic) code paths
61 // that invoke malloc() and getenv() that might acquire some locks.
62 
63 // Helper for RawLog below.
64 // *DoRawLog writes to *buf of *size and move them past the written portion.
65 // It returns true iff there was no overflow or error.
66 bool DoRawLog(char** buf, int* size, const char* format, ...)
67     ABSL_PRINTF_ATTRIBUTE(3, 4);
DoRawLog(char ** buf,int * size,const char * format,...)68 bool DoRawLog(char** buf, int* size, const char* format, ...) {
69   va_list ap;
70   va_start(ap, format);
71   int n = vsnprintf(*buf, *size, format, ap);
72   va_end(ap);
73   if (n < 0 || n > *size) return false;
74   *size -= n;
75   *buf += n;
76   return true;
77 }
78 
79 // Writes the provided buffer directly to stderr, in a safe, low-level manner.
80 //
81 // In POSIX this means calling write(), which is async-signal safe and does
82 // not malloc.  If the platform supports the SYS_write syscall, we invoke that
83 // directly to side-step any libc interception.
SafeWriteToStderr(const char * s,size_t len)84 void SafeWriteToStderr(const char* s, size_t len) {
85   syscall(SYS_write, STDERR_FILENO, s, len);
86 }
87 
88 void RawLogVA(absl::LogSeverity severity, const char* file, int line,
89               const char* format, va_list ap) ABSL_PRINTF_ATTRIBUTE(4, 0);
RawLogVA(absl::LogSeverity severity,const char * file,int line,const char * format,va_list ap)90 void RawLogVA(absl::LogSeverity severity, const char* file, int line,
91               const char* format, va_list ap) {
92   char buffer[sapi::raw_logging_internal::kLogBufSize];
93   char* buf = buffer;
94   int size = sizeof(buffer);
95 
96   DoRawLog(&buf, &size, "[%s : %d] RAW: ", file, line);
97 
98   bool no_chop = VADoRawLog(&buf, &size, format, ap);
99   if (no_chop) {
100     DoRawLog(&buf, &size, "\n");
101   } else {
102     DoRawLog(&buf, &size, "%s", kTruncated);
103   }
104 
105   SafeWriteToStderr(buffer, strlen(buffer));
106 
107   // Abort the process after logging a FATAL message, even if the output itself
108   // was suppressed.
109   if (severity == absl::LogSeverity::kFatal) {
110     abort();
111   }
112 }
113 
114 }  // namespace
115 #endif
116 
117 namespace sapi::raw_logging_internal {
118 
119 #ifndef SAPI_USE_ABSL_RAW_LOG
120 void RawLog(absl::LogSeverity severity, const char* file, int line,
121             const char* format, ...) ABSL_PRINTF_ATTRIBUTE(4, 5);
RawLog(absl::LogSeverity severity,const char * file,int line,const char * format,...)122 void RawLog(absl::LogSeverity severity, const char* file, int line,
123             const char* format, ...) {
124   va_list ap;
125   va_start(ap, format);
126   RawLogVA(severity, file, line, format, ap);
127   va_end(ap);
128 }
129 #endif
130 
VLogIsOn(int verbose_level)131 bool VLogIsOn(int verbose_level) {
132   static int external_verbose_level = [] {
133     int external_verbose_level = std::numeric_limits<int>::min();
134     char* env_var = getenv("SAPI_VLOG_LEVEL");
135     if (!env_var) {
136       return external_verbose_level;
137     }
138     SAPI_RAW_CHECK(absl::SimpleAtoi(env_var, &external_verbose_level) &&
139                        external_verbose_level >= 0,
140                    "SAPI_VLOG_LEVEL needs to be an integer >= 0");
141     return external_verbose_level;
142   }();
143   return verbose_level <= external_verbose_level;
144 }
145 
146 }  // namespace sapi::raw_logging_internal
147