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 <syscall.h>
18 #include <unistd.h>
19
20 #include <cstdarg>
21 #include <cstdio>
22 #include <cstdlib>
23 #include <cstring>
24 #include <limits>
25
26 #include "absl/base/attributes.h"
27 #include "absl/base/log_severity.h"
28 #include "absl/strings/numbers.h"
29
30 #ifdef __ANDROID__
31 #include "android/log.h"
32 #endif
33
34 static const char kTruncated[] = " ... (message truncated)\n";
35
36 // sprintf the format to the buffer, adjusting *buf and *size to reflect the
37 // consumed bytes, and return whether the message fit without truncation. If
38 // truncation occurred, if possible leave room in the buffer for the message
39 // kTruncated[].
40 inline static bool VADoRawLog(char** buf, int* size, const char* format,
41 va_list ap) ABSL_PRINTF_ATTRIBUTE(3, 0);
VADoRawLog(char ** buf,int * size,const char * format,va_list ap)42 inline static bool VADoRawLog(char** buf, int* size, const char* format,
43 va_list ap) {
44 int n = vsnprintf(*buf, *size, format, ap);
45 bool result = true;
46 if (n < 0 || n > *size) {
47 result = false;
48 if (static_cast<size_t>(*size) > sizeof(kTruncated)) {
49 n = *size - sizeof(kTruncated); // room for truncation message
50 } else {
51 n = 0; // no room for truncation message
52 }
53 }
54 *size -= n;
55 *buf += n;
56 return result;
57 }
58
59 namespace {
60
61 // CAVEAT: vsnprintf called from *DoRawLog below has some (exotic) code paths
62 // that invoke malloc() and getenv() that might acquire some locks.
63
64 // Helper for RawLog below.
65 // *DoRawLog writes to *buf of *size and move them past the written portion.
66 // It returns true iff there was no overflow or error.
67 bool DoRawLog(char** buf, int* size, const char* format, ...)
68 ABSL_PRINTF_ATTRIBUTE(3, 4);
DoRawLog(char ** buf,int * size,const char * format,...)69 bool DoRawLog(char** buf, int* size, const char* format, ...) {
70 va_list ap;
71 va_start(ap, format);
72 int n = vsnprintf(*buf, *size, format, ap);
73 va_end(ap);
74 if (n < 0 || n > *size) return false;
75 *size -= n;
76 *buf += n;
77 return true;
78 }
79
80 #ifdef __ANDROID__
ConvertSeverity(absl::LogSeverity severity)81 android_LogPriority ConvertSeverity(absl::LogSeverity severity) {
82 switch (severity) {
83 case absl::LogSeverity::kInfo:
84 return ANDROID_LOG_INFO;
85 case absl::LogSeverity::kWarning:
86 return ANDROID_LOG_WARN;
87 case absl::LogSeverity::kError:
88 return ANDROID_LOG_ERROR;
89 case absl::LogSeverity::kFatal:
90 return ANDROID_LOG_FATAL;
91 default:
92 return ANDROID_LOG_INFO;
93 }
94 }
95 #endif
96
97 void RawLogVA(absl::LogSeverity severity, const char* file, int line,
98 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)99 void RawLogVA(absl::LogSeverity severity, const char* file, int line,
100 const char* format, va_list ap) {
101 char buffer[sapi::raw_logging_internal::kLogBufSize];
102 char* buf = buffer;
103 int size = sizeof(buffer);
104
105 DoRawLog(&buf, &size, "[%s : %d] RAW: ", file, line);
106
107 bool no_chop = VADoRawLog(&buf, &size, format, ap);
108 if (no_chop) {
109 DoRawLog(&buf, &size, "\n");
110 } else {
111 DoRawLog(&buf, &size, "%s", kTruncated);
112 }
113 #ifndef __ANDROID__
114 sapi::raw_logging_internal::SafeWriteToStderr(buffer, strlen(buffer));
115 #else
116 // Logs to Android's logcat with the TAG SAPI and the log line containing
117 // the code location and the log output.
118 __android_log_print(ConvertSeverity(severity), "SAPI", "%s", buffer);
119 #endif
120
121 // Abort the process after logging a FATAL message, even if the output itself
122 // was suppressed.
123 if (severity == absl::LogSeverity::kFatal) {
124 abort();
125 }
126 }
127
128 } // namespace
129
130 namespace sapi::raw_logging_internal {
131
132 void RawLog(absl::LogSeverity severity, const char* file, int line,
133 const char* format, ...) ABSL_PRINTF_ATTRIBUTE(4, 5);
RawLog(absl::LogSeverity severity,const char * file,int line,const char * format,...)134 void RawLog(absl::LogSeverity severity, const char* file, int line,
135 const char* format, ...) {
136 va_list ap;
137 va_start(ap, format);
138 RawLogVA(severity, file, line, format, ap);
139 va_end(ap);
140 }
141
SafeWriteToStderr(const char * s,size_t len)142 void SafeWriteToStderr(const char* s, size_t len) {
143 syscall(SYS_write, STDERR_FILENO, s, len);
144 }
145
VLogIsOn(int verbose_level)146 bool VLogIsOn(int verbose_level) {
147 static int external_verbose_level = [] {
148 int external_verbose_level = std::numeric_limits<int>::min();
149 char* env_var = getenv("SAPI_VLOG_LEVEL");
150 if (!env_var) {
151 return external_verbose_level;
152 }
153 SAPI_RAW_CHECK(absl::SimpleAtoi(env_var, &external_verbose_level) &&
154 external_verbose_level >= 0,
155 "SAPI_VLOG_LEVEL needs to be an integer >= 0");
156 return external_verbose_level;
157 }();
158 return verbose_level <= external_verbose_level;
159 }
160
161 } // namespace sapi::raw_logging_internal
162