blob: 2794e667e69706a505113f26fb5035dd18bdda71 [file] [log] [blame]
Alexey Samsonove9541012012-06-06 13:11:29 +00001//===-- sanitizer_printf.cc -----------------------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file is shared between AddressSanitizer and ThreadSanitizer.
11//
12// Internal printf function, used inside run-time libraries.
13// We can't use libc printf because we intercept some of the functions used
14// inside it.
15//===----------------------------------------------------------------------===//
16
Alexey Samsonove9541012012-06-06 13:11:29 +000017#include "sanitizer_common.h"
Stephen Hines2d1fdb22014-05-28 23:58:16 -070018#include "sanitizer_flags.h"
Alexey Samsonove9541012012-06-06 13:11:29 +000019#include "sanitizer_libc.h"
20
21#include <stdio.h>
22#include <stdarg.h>
23
Stephen Hines6d186232014-11-26 17:56:19 -080024#if SANITIZER_WINDOWS && defined(_MSC_VER) && _MSC_VER < 1800 && \
25 !defined(va_copy)
Dmitry Vyukov6a72c9d2013-04-30 13:30:29 +000026# define va_copy(dst, src) ((dst) = (src))
27#endif
28
Alexey Samsonove9541012012-06-06 13:11:29 +000029namespace __sanitizer {
30
Alexey Samsonov7ed46ff2013-04-05 07:30:29 +000031StaticSpinMutex CommonSanitizerReportMutex;
32
Alexey Samsonove9541012012-06-06 13:11:29 +000033static int AppendChar(char **buff, const char *buff_end, char c) {
34 if (*buff < buff_end) {
35 **buff = c;
36 (*buff)++;
37 }
38 return 1;
39}
40
41// Appends number in a given base to buffer. If its length is less than
Sergey Matveev134da442013-06-27 15:30:44 +000042// |minimal_num_length|, it is padded with leading zeroes or spaces, depending
43// on the value of |pad_with_zero|.
44static int AppendNumber(char **buff, const char *buff_end, u64 absolute_value,
45 u8 base, u8 minimal_num_length, bool pad_with_zero,
46 bool negative) {
Alexey Samsonove9541012012-06-06 13:11:29 +000047 uptr const kMaxLen = 30;
48 RAW_CHECK(base == 10 || base == 16);
Sergey Matveev134da442013-06-27 15:30:44 +000049 RAW_CHECK(base == 10 || !negative);
50 RAW_CHECK(absolute_value || !negative);
Alexey Samsonove9541012012-06-06 13:11:29 +000051 RAW_CHECK(minimal_num_length < kMaxLen);
Sergey Matveev134da442013-06-27 15:30:44 +000052 int result = 0;
53 if (negative && minimal_num_length)
54 --minimal_num_length;
55 if (negative && pad_with_zero)
56 result += AppendChar(buff, buff_end, '-');
Alexey Samsonove9541012012-06-06 13:11:29 +000057 uptr num_buffer[kMaxLen];
Sergey Matveev134da442013-06-27 15:30:44 +000058 int pos = 0;
Alexey Samsonove9541012012-06-06 13:11:29 +000059 do {
Sergey Matveev134da442013-06-27 15:30:44 +000060 RAW_CHECK_MSG((uptr)pos < kMaxLen, "AppendNumber buffer overflow");
61 num_buffer[pos++] = absolute_value % base;
62 absolute_value /= base;
63 } while (absolute_value > 0);
Alexey Samsonove52e2802012-11-21 11:12:57 +000064 if (pos < minimal_num_length) {
65 // Make sure compiler doesn't insert call to memset here.
66 internal_memset(&num_buffer[pos], 0,
67 sizeof(num_buffer[0]) * (minimal_num_length - pos));
68 pos = minimal_num_length;
69 }
Sergey Matveev134da442013-06-27 15:30:44 +000070 RAW_CHECK(pos > 0);
71 pos--;
72 for (; pos >= 0 && num_buffer[pos] == 0; pos--) {
73 char c = (pad_with_zero || pos == 0) ? '0' : ' ';
74 result += AppendChar(buff, buff_end, c);
75 }
76 if (negative && !pad_with_zero) result += AppendChar(buff, buff_end, '-');
77 for (; pos >= 0; pos--) {
Timur Iskhodzhanov5e97ba32013-05-29 14:11:44 +000078 char digit = static_cast<char>(num_buffer[pos]);
Alexey Samsonove9541012012-06-06 13:11:29 +000079 result += AppendChar(buff, buff_end, (digit < 10) ? '0' + digit
80 : 'a' + digit - 10);
81 }
82 return result;
83}
84
Sergey Matveev134da442013-06-27 15:30:44 +000085static int AppendUnsigned(char **buff, const char *buff_end, u64 num, u8 base,
86 u8 minimal_num_length, bool pad_with_zero) {
87 return AppendNumber(buff, buff_end, num, base, minimal_num_length,
88 pad_with_zero, false /* negative */);
89}
90
Richard Smithf4932202012-11-13 23:42:05 +000091static int AppendSignedDecimal(char **buff, const char *buff_end, s64 num,
Sergey Matveev134da442013-06-27 15:30:44 +000092 u8 minimal_num_length, bool pad_with_zero) {
93 bool negative = (num < 0);
94 return AppendNumber(buff, buff_end, (u64)(negative ? -num : num), 10,
95 minimal_num_length, pad_with_zero, negative);
Alexey Samsonove9541012012-06-06 13:11:29 +000096}
97
Stephen Hines2d1fdb22014-05-28 23:58:16 -070098static int AppendString(char **buff, const char *buff_end, int precision,
99 const char *s) {
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -0800100 if (!s)
Alexey Samsonove9541012012-06-06 13:11:29 +0000101 s = "<null>";
102 int result = 0;
103 for (; *s; s++) {
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700104 if (precision >= 0 && result >= precision)
105 break;
Alexey Samsonove9541012012-06-06 13:11:29 +0000106 result += AppendChar(buff, buff_end, *s);
107 }
108 return result;
109}
110
111static int AppendPointer(char **buff, const char *buff_end, u64 ptr_value) {
112 int result = 0;
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700113 result += AppendString(buff, buff_end, -1, "0x");
Alexey Samsonove9541012012-06-06 13:11:29 +0000114 result += AppendUnsigned(buff, buff_end, ptr_value, 16,
Stephen Hines6d186232014-11-26 17:56:19 -0800115 SANITIZER_POINTER_FORMAT_LENGTH, true);
Alexey Samsonove9541012012-06-06 13:11:29 +0000116 return result;
117}
118
119int VSNPrintf(char *buff, int buff_length,
120 const char *format, va_list args) {
Richard Smithf4932202012-11-13 23:42:05 +0000121 static const char *kPrintfFormatsHelp =
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700122 "Supported Printf formats: %([0-9]*)?(z|ll)?{d,u,x}; %p; %(\\.\\*)?s; %c\n";
Alexey Samsonove9541012012-06-06 13:11:29 +0000123 RAW_CHECK(format);
124 RAW_CHECK(buff_length > 0);
125 const char *buff_end = &buff[buff_length - 1];
126 const char *cur = format;
127 int result = 0;
128 for (; *cur; cur++) {
129 if (*cur != '%') {
130 result += AppendChar(&buff, buff_end, *cur);
131 continue;
132 }
133 cur++;
Sergey Matveev134da442013-06-27 15:30:44 +0000134 bool have_width = (*cur >= '0' && *cur <= '9');
135 bool pad_with_zero = (*cur == '0');
Richard Smithf4932202012-11-13 23:42:05 +0000136 int width = 0;
137 if (have_width) {
138 while (*cur >= '0' && *cur <= '9') {
Richard Smithf4932202012-11-13 23:42:05 +0000139 width = width * 10 + *cur++ - '0';
140 }
141 }
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700142 bool have_precision = (cur[0] == '.' && cur[1] == '*');
143 int precision = -1;
144 if (have_precision) {
145 cur += 2;
146 precision = va_arg(args, int);
147 }
Alexey Samsonove9541012012-06-06 13:11:29 +0000148 bool have_z = (*cur == 'z');
149 cur += have_z;
Richard Smithf4932202012-11-13 23:42:05 +0000150 bool have_ll = !have_z && (cur[0] == 'l' && cur[1] == 'l');
151 cur += have_ll * 2;
Alexey Samsonove9541012012-06-06 13:11:29 +0000152 s64 dval;
153 u64 uval;
Richard Smithf4932202012-11-13 23:42:05 +0000154 bool have_flags = have_width | have_z | have_ll;
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700155 // Only %s supports precision for now
156 CHECK(!(precision >= 0 && *cur != 's'));
Alexey Samsonove9541012012-06-06 13:11:29 +0000157 switch (*cur) {
158 case 'd': {
Richard Smithf4932202012-11-13 23:42:05 +0000159 dval = have_ll ? va_arg(args, s64)
160 : have_z ? va_arg(args, sptr)
161 : va_arg(args, int);
Sergey Matveev134da442013-06-27 15:30:44 +0000162 result += AppendSignedDecimal(&buff, buff_end, dval, width,
163 pad_with_zero);
Alexey Samsonove9541012012-06-06 13:11:29 +0000164 break;
165 }
166 case 'u':
167 case 'x': {
Richard Smithf4932202012-11-13 23:42:05 +0000168 uval = have_ll ? va_arg(args, u64)
169 : have_z ? va_arg(args, uptr)
170 : va_arg(args, unsigned);
Alexey Samsonove9541012012-06-06 13:11:29 +0000171 result += AppendUnsigned(&buff, buff_end, uval,
Sergey Matveev134da442013-06-27 15:30:44 +0000172 (*cur == 'u') ? 10 : 16, width, pad_with_zero);
Alexey Samsonove9541012012-06-06 13:11:29 +0000173 break;
174 }
175 case 'p': {
Richard Smithf4932202012-11-13 23:42:05 +0000176 RAW_CHECK_MSG(!have_flags, kPrintfFormatsHelp);
Alexey Samsonove9541012012-06-06 13:11:29 +0000177 result += AppendPointer(&buff, buff_end, va_arg(args, uptr));
178 break;
179 }
180 case 's': {
Richard Smithf4932202012-11-13 23:42:05 +0000181 RAW_CHECK_MSG(!have_flags, kPrintfFormatsHelp);
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700182 result += AppendString(&buff, buff_end, precision, va_arg(args, char*));
Alexey Samsonove9541012012-06-06 13:11:29 +0000183 break;
184 }
Kostya Serebryany81dfbb72012-09-14 04:35:14 +0000185 case 'c': {
Richard Smithf4932202012-11-13 23:42:05 +0000186 RAW_CHECK_MSG(!have_flags, kPrintfFormatsHelp);
Kostya Serebryany01167e82012-09-14 06:52:46 +0000187 result += AppendChar(&buff, buff_end, va_arg(args, int));
Kostya Serebryany81dfbb72012-09-14 04:35:14 +0000188 break;
189 }
Alexey Samsonove9541012012-06-06 13:11:29 +0000190 case '%' : {
Richard Smithf4932202012-11-13 23:42:05 +0000191 RAW_CHECK_MSG(!have_flags, kPrintfFormatsHelp);
Alexey Samsonove9541012012-06-06 13:11:29 +0000192 result += AppendChar(&buff, buff_end, '%');
193 break;
194 }
195 default: {
196 RAW_CHECK_MSG(false, kPrintfFormatsHelp);
197 }
198 }
199 }
200 RAW_CHECK(buff <= buff_end);
201 AppendChar(&buff, buff_end + 1, '\0');
202 return result;
203}
204
Kostya Serebryany283c2962012-08-28 11:34:40 +0000205static void (*PrintfAndReportCallback)(const char *);
206void SetPrintfAndReportCallback(void (*callback)(const char *)) {
207 PrintfAndReportCallback = callback;
208}
209
Dmitry Vyukov13f62b22013-03-25 12:58:09 +0000210// Can be overriden in frontend.
Dmitry Vyukov6f2741c2013-10-14 09:52:40 +0000211#if SANITIZER_SUPPORTS_WEAK_HOOKS
Timur Iskhodzhanov3c80c6c2013-08-13 11:42:45 +0000212SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE
Dmitry Vyukov6f2741c2013-10-14 09:52:40 +0000213void OnPrint(const char *str) {
214 (void)str;
215}
216#elif defined(SANITIZER_GO) && defined(TSAN_EXTERNAL_HOOKS)
Alexey Samsonov78b580f2013-04-03 13:22:54 +0000217void OnPrint(const char *str);
Dmitry Vyukov6f2741c2013-10-14 09:52:40 +0000218#else
219void OnPrint(const char *str) {
220 (void)str;
221}
Dmitry Vyukov13f62b22013-03-25 12:58:09 +0000222#endif
223
224static void CallPrintfAndReportCallback(const char *str) {
Dmitry Vyukov6f2741c2013-10-14 09:52:40 +0000225 OnPrint(str);
Dmitry Vyukov13f62b22013-03-25 12:58:09 +0000226 if (PrintfAndReportCallback)
227 PrintfAndReportCallback(str);
228}
229
Alexey Samsonov99f1e202013-04-18 13:18:23 +0000230static void SharedPrintfCode(bool append_pid, const char *format,
231 va_list args) {
Dmitry Vyukov4f5e4bb2013-04-30 12:27:48 +0000232 va_list args2;
233 va_copy(args2, args);
Alexey Samsonovb1fe3022012-11-02 12:17:51 +0000234 const int kLen = 16 * 1024;
Alexey Samsonov99f1e202013-04-18 13:18:23 +0000235 // |local_buffer| is small enough not to overflow the stack and/or violate
236 // the stack limit enforced by TSan (-Wframe-larger-than=512). On the other
237 // hand, the bigger the buffer is, the more the chance the error report will
238 // fit into it.
239 char local_buffer[400];
240 int needed_length;
241 char *buffer = local_buffer;
242 int buffer_size = ARRAY_SIZE(local_buffer);
243 // First try to print a message using a local buffer, and then fall back to
244 // mmaped buffer.
245 for (int use_mmap = 0; use_mmap < 2; use_mmap++) {
246 if (use_mmap) {
Dmitry Vyukov4f5e4bb2013-04-30 12:27:48 +0000247 va_end(args);
248 va_copy(args, args2);
Alexey Samsonov99f1e202013-04-18 13:18:23 +0000249 buffer = (char*)MmapOrDie(kLen, "Report");
250 buffer_size = kLen;
251 }
252 needed_length = 0;
Pirama Arumuga Nainarcdce50b2015-07-01 12:26:56 -0700253 // Check that data fits into the current buffer.
254# define CHECK_NEEDED_LENGTH \
255 if (needed_length >= buffer_size) { \
256 if (!use_mmap) continue; \
257 RAW_CHECK_MSG(needed_length < kLen, \
258 "Buffer in Report is too short!\n"); \
259 }
Alexey Samsonov99f1e202013-04-18 13:18:23 +0000260 if (append_pid) {
Peter Collingbourne0b694fc2013-05-17 16:56:53 +0000261 int pid = internal_getpid();
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -0800262 const char *exe_name = GetProcessName();
Pirama Arumuga Nainarcdce50b2015-07-01 12:26:56 -0700263 if (common_flags()->log_exe_name && exe_name) {
264 needed_length += internal_snprintf(buffer, buffer_size,
265 "==%s", exe_name);
266 CHECK_NEEDED_LENGTH
Alexey Samsonov99f1e202013-04-18 13:18:23 +0000267 }
Pirama Arumuga Nainarcdce50b2015-07-01 12:26:56 -0700268 needed_length += internal_snprintf(buffer + needed_length,
269 buffer_size - needed_length,
270 "==%d==", pid);
271 CHECK_NEEDED_LENGTH
Alexey Samsonov99f1e202013-04-18 13:18:23 +0000272 }
273 needed_length += VSNPrintf(buffer + needed_length,
274 buffer_size - needed_length, format, args);
Pirama Arumuga Nainarcdce50b2015-07-01 12:26:56 -0700275 CHECK_NEEDED_LENGTH
Alexey Samsonov99f1e202013-04-18 13:18:23 +0000276 // If the message fit into the buffer, print it and exit.
277 break;
Pirama Arumuga Nainarcdce50b2015-07-01 12:26:56 -0700278# undef CHECK_NEEDED_LENGTH
Alexey Samsonov99f1e202013-04-18 13:18:23 +0000279 }
280 RawWrite(buffer);
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -0800281 if (common_flags()->log_to_syslog && ShouldLogAfterPrintf())
282 WriteToSyslog(buffer);
Alexey Samsonov99f1e202013-04-18 13:18:23 +0000283 CallPrintfAndReportCallback(buffer);
284 // If we had mapped any memory, clean up.
285 if (buffer != local_buffer)
286 UnmapOrDie((void *)buffer, buffer_size);
Dmitry Vyukov4f5e4bb2013-04-30 12:27:48 +0000287 va_end(args2);
Alexey Samsonov99f1e202013-04-18 13:18:23 +0000288}
289
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700290FORMAT(1, 2)
Alexey Samsonov99f1e202013-04-18 13:18:23 +0000291void Printf(const char *format, ...) {
Alexey Samsonove9541012012-06-06 13:11:29 +0000292 va_list args;
293 va_start(args, format);
Alexey Samsonov99f1e202013-04-18 13:18:23 +0000294 SharedPrintfCode(false, format, args);
Alexey Samsonove9541012012-06-06 13:11:29 +0000295 va_end(args);
Alexey Samsonov99f1e202013-04-18 13:18:23 +0000296}
297
298// Like Printf, but prints the current PID before the output string.
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700299FORMAT(1, 2)
Alexey Samsonov99f1e202013-04-18 13:18:23 +0000300void Report(const char *format, ...) {
301 va_list args;
302 va_start(args, format);
303 SharedPrintfCode(true, format, args);
304 va_end(args);
Alexey Samsonove9541012012-06-06 13:11:29 +0000305}
306
307// Writes at most "length" symbols to "buffer" (including trailing '\0').
308// Returns the number of symbols that should have been written to buffer
309// (not including trailing '\0'). Thus, the string is truncated
310// iff return value is not less than "length".
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700311FORMAT(3, 4)
Alexey Samsonovde08c022012-06-19 09:21:57 +0000312int internal_snprintf(char *buffer, uptr length, const char *format, ...) {
Alexey Samsonove9541012012-06-06 13:11:29 +0000313 va_list args;
314 va_start(args, format);
315 int needed_length = VSNPrintf(buffer, length, format, args);
316 va_end(args);
317 return needed_length;
318}
319
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700320FORMAT(2, 3)
Alexey Samsonovbb4697f2013-11-14 09:41:24 +0000321void InternalScopedString::append(const char *format, ...) {
322 CHECK_LT(length_, size());
323 va_list args;
324 va_start(args, format);
325 VSNPrintf(data() + length_, size() - length_, format, args);
326 va_end(args);
327 length_ += internal_strlen(data() + length_);
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700328 CHECK_LT(length_, size());
Alexey Samsonovbb4697f2013-11-14 09:41:24 +0000329}
330
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -0800331} // namespace __sanitizer