Alexey Samsonov | e954101 | 2012-06-06 13:11:29 +0000 | [diff] [blame] | 1 | //===-- 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 | |
| 17 | |
| 18 | #include "sanitizer_common.h" |
| 19 | #include "sanitizer_libc.h" |
| 20 | |
| 21 | #include <stdio.h> |
| 22 | #include <stdarg.h> |
| 23 | |
Dmitry Vyukov | c528867 | 2013-06-10 10:02:02 +0000 | [diff] [blame] | 24 | #if SANITIZER_WINDOWS && !defined(va_copy) |
Dmitry Vyukov | 6a72c9d | 2013-04-30 13:30:29 +0000 | [diff] [blame] | 25 | # define va_copy(dst, src) ((dst) = (src)) |
| 26 | #endif |
| 27 | |
Alexey Samsonov | e954101 | 2012-06-06 13:11:29 +0000 | [diff] [blame] | 28 | namespace __sanitizer { |
| 29 | |
Alexey Samsonov | 7ed46ff | 2013-04-05 07:30:29 +0000 | [diff] [blame] | 30 | StaticSpinMutex CommonSanitizerReportMutex; |
| 31 | |
Alexey Samsonov | e954101 | 2012-06-06 13:11:29 +0000 | [diff] [blame] | 32 | static int AppendChar(char **buff, const char *buff_end, char c) { |
| 33 | if (*buff < buff_end) { |
| 34 | **buff = c; |
| 35 | (*buff)++; |
| 36 | } |
| 37 | return 1; |
| 38 | } |
| 39 | |
| 40 | // Appends number in a given base to buffer. If its length is less than |
Sergey Matveev | 134da44 | 2013-06-27 15:30:44 +0000 | [diff] [blame] | 41 | // |minimal_num_length|, it is padded with leading zeroes or spaces, depending |
| 42 | // on the value of |pad_with_zero|. |
| 43 | static int AppendNumber(char **buff, const char *buff_end, u64 absolute_value, |
| 44 | u8 base, u8 minimal_num_length, bool pad_with_zero, |
| 45 | bool negative) { |
Alexey Samsonov | e954101 | 2012-06-06 13:11:29 +0000 | [diff] [blame] | 46 | uptr const kMaxLen = 30; |
| 47 | RAW_CHECK(base == 10 || base == 16); |
Sergey Matveev | 134da44 | 2013-06-27 15:30:44 +0000 | [diff] [blame] | 48 | RAW_CHECK(base == 10 || !negative); |
| 49 | RAW_CHECK(absolute_value || !negative); |
Alexey Samsonov | e954101 | 2012-06-06 13:11:29 +0000 | [diff] [blame] | 50 | RAW_CHECK(minimal_num_length < kMaxLen); |
Sergey Matveev | 134da44 | 2013-06-27 15:30:44 +0000 | [diff] [blame] | 51 | int result = 0; |
| 52 | if (negative && minimal_num_length) |
| 53 | --minimal_num_length; |
| 54 | if (negative && pad_with_zero) |
| 55 | result += AppendChar(buff, buff_end, '-'); |
Alexey Samsonov | e954101 | 2012-06-06 13:11:29 +0000 | [diff] [blame] | 56 | uptr num_buffer[kMaxLen]; |
Sergey Matveev | 134da44 | 2013-06-27 15:30:44 +0000 | [diff] [blame] | 57 | int pos = 0; |
Alexey Samsonov | e954101 | 2012-06-06 13:11:29 +0000 | [diff] [blame] | 58 | do { |
Sergey Matveev | 134da44 | 2013-06-27 15:30:44 +0000 | [diff] [blame] | 59 | RAW_CHECK_MSG((uptr)pos < kMaxLen, "AppendNumber buffer overflow"); |
| 60 | num_buffer[pos++] = absolute_value % base; |
| 61 | absolute_value /= base; |
| 62 | } while (absolute_value > 0); |
Alexey Samsonov | e52e280 | 2012-11-21 11:12:57 +0000 | [diff] [blame] | 63 | if (pos < minimal_num_length) { |
| 64 | // Make sure compiler doesn't insert call to memset here. |
| 65 | internal_memset(&num_buffer[pos], 0, |
| 66 | sizeof(num_buffer[0]) * (minimal_num_length - pos)); |
| 67 | pos = minimal_num_length; |
| 68 | } |
Sergey Matveev | 134da44 | 2013-06-27 15:30:44 +0000 | [diff] [blame] | 69 | RAW_CHECK(pos > 0); |
| 70 | pos--; |
| 71 | for (; pos >= 0 && num_buffer[pos] == 0; pos--) { |
| 72 | char c = (pad_with_zero || pos == 0) ? '0' : ' '; |
| 73 | result += AppendChar(buff, buff_end, c); |
| 74 | } |
| 75 | if (negative && !pad_with_zero) result += AppendChar(buff, buff_end, '-'); |
| 76 | for (; pos >= 0; pos--) { |
Timur Iskhodzhanov | 5e97ba3 | 2013-05-29 14:11:44 +0000 | [diff] [blame] | 77 | char digit = static_cast<char>(num_buffer[pos]); |
Alexey Samsonov | e954101 | 2012-06-06 13:11:29 +0000 | [diff] [blame] | 78 | result += AppendChar(buff, buff_end, (digit < 10) ? '0' + digit |
| 79 | : 'a' + digit - 10); |
| 80 | } |
| 81 | return result; |
| 82 | } |
| 83 | |
Sergey Matveev | 134da44 | 2013-06-27 15:30:44 +0000 | [diff] [blame] | 84 | static int AppendUnsigned(char **buff, const char *buff_end, u64 num, u8 base, |
| 85 | u8 minimal_num_length, bool pad_with_zero) { |
| 86 | return AppendNumber(buff, buff_end, num, base, minimal_num_length, |
| 87 | pad_with_zero, false /* negative */); |
| 88 | } |
| 89 | |
Richard Smith | f493220 | 2012-11-13 23:42:05 +0000 | [diff] [blame] | 90 | static int AppendSignedDecimal(char **buff, const char *buff_end, s64 num, |
Sergey Matveev | 134da44 | 2013-06-27 15:30:44 +0000 | [diff] [blame] | 91 | u8 minimal_num_length, bool pad_with_zero) { |
| 92 | bool negative = (num < 0); |
| 93 | return AppendNumber(buff, buff_end, (u64)(negative ? -num : num), 10, |
| 94 | minimal_num_length, pad_with_zero, negative); |
Alexey Samsonov | e954101 | 2012-06-06 13:11:29 +0000 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | static int AppendString(char **buff, const char *buff_end, const char *s) { |
| 98 | if (s == 0) |
| 99 | s = "<null>"; |
| 100 | int result = 0; |
| 101 | for (; *s; s++) { |
| 102 | result += AppendChar(buff, buff_end, *s); |
| 103 | } |
| 104 | return result; |
| 105 | } |
| 106 | |
| 107 | static int AppendPointer(char **buff, const char *buff_end, u64 ptr_value) { |
| 108 | int result = 0; |
| 109 | result += AppendString(buff, buff_end, "0x"); |
| 110 | result += AppendUnsigned(buff, buff_end, ptr_value, 16, |
Sergey Matveev | 134da44 | 2013-06-27 15:30:44 +0000 | [diff] [blame] | 111 | (SANITIZER_WORDSIZE == 64) ? 12 : 8, true); |
Alexey Samsonov | e954101 | 2012-06-06 13:11:29 +0000 | [diff] [blame] | 112 | return result; |
| 113 | } |
| 114 | |
| 115 | int VSNPrintf(char *buff, int buff_length, |
| 116 | const char *format, va_list args) { |
Richard Smith | f493220 | 2012-11-13 23:42:05 +0000 | [diff] [blame] | 117 | static const char *kPrintfFormatsHelp = |
Sergey Matveev | 134da44 | 2013-06-27 15:30:44 +0000 | [diff] [blame] | 118 | "Supported Printf formats: %([0-9]*)?(z|ll)?{d,u,x}; %p; %s; %c\n"; |
Alexey Samsonov | e954101 | 2012-06-06 13:11:29 +0000 | [diff] [blame] | 119 | RAW_CHECK(format); |
| 120 | RAW_CHECK(buff_length > 0); |
| 121 | const char *buff_end = &buff[buff_length - 1]; |
| 122 | const char *cur = format; |
| 123 | int result = 0; |
| 124 | for (; *cur; cur++) { |
| 125 | if (*cur != '%') { |
| 126 | result += AppendChar(&buff, buff_end, *cur); |
| 127 | continue; |
| 128 | } |
| 129 | cur++; |
Sergey Matveev | 134da44 | 2013-06-27 15:30:44 +0000 | [diff] [blame] | 130 | bool have_width = (*cur >= '0' && *cur <= '9'); |
| 131 | bool pad_with_zero = (*cur == '0'); |
Richard Smith | f493220 | 2012-11-13 23:42:05 +0000 | [diff] [blame] | 132 | int width = 0; |
| 133 | if (have_width) { |
| 134 | while (*cur >= '0' && *cur <= '9') { |
Richard Smith | f493220 | 2012-11-13 23:42:05 +0000 | [diff] [blame] | 135 | width = width * 10 + *cur++ - '0'; |
| 136 | } |
| 137 | } |
Alexey Samsonov | e954101 | 2012-06-06 13:11:29 +0000 | [diff] [blame] | 138 | bool have_z = (*cur == 'z'); |
| 139 | cur += have_z; |
Richard Smith | f493220 | 2012-11-13 23:42:05 +0000 | [diff] [blame] | 140 | bool have_ll = !have_z && (cur[0] == 'l' && cur[1] == 'l'); |
| 141 | cur += have_ll * 2; |
Alexey Samsonov | e954101 | 2012-06-06 13:11:29 +0000 | [diff] [blame] | 142 | s64 dval; |
| 143 | u64 uval; |
Richard Smith | f493220 | 2012-11-13 23:42:05 +0000 | [diff] [blame] | 144 | bool have_flags = have_width | have_z | have_ll; |
Alexey Samsonov | e954101 | 2012-06-06 13:11:29 +0000 | [diff] [blame] | 145 | switch (*cur) { |
| 146 | case 'd': { |
Richard Smith | f493220 | 2012-11-13 23:42:05 +0000 | [diff] [blame] | 147 | dval = have_ll ? va_arg(args, s64) |
| 148 | : have_z ? va_arg(args, sptr) |
| 149 | : va_arg(args, int); |
Sergey Matveev | 134da44 | 2013-06-27 15:30:44 +0000 | [diff] [blame] | 150 | result += AppendSignedDecimal(&buff, buff_end, dval, width, |
| 151 | pad_with_zero); |
Alexey Samsonov | e954101 | 2012-06-06 13:11:29 +0000 | [diff] [blame] | 152 | break; |
| 153 | } |
| 154 | case 'u': |
| 155 | case 'x': { |
Richard Smith | f493220 | 2012-11-13 23:42:05 +0000 | [diff] [blame] | 156 | uval = have_ll ? va_arg(args, u64) |
| 157 | : have_z ? va_arg(args, uptr) |
| 158 | : va_arg(args, unsigned); |
Alexey Samsonov | e954101 | 2012-06-06 13:11:29 +0000 | [diff] [blame] | 159 | result += AppendUnsigned(&buff, buff_end, uval, |
Sergey Matveev | 134da44 | 2013-06-27 15:30:44 +0000 | [diff] [blame] | 160 | (*cur == 'u') ? 10 : 16, width, pad_with_zero); |
Alexey Samsonov | e954101 | 2012-06-06 13:11:29 +0000 | [diff] [blame] | 161 | break; |
| 162 | } |
| 163 | case 'p': { |
Richard Smith | f493220 | 2012-11-13 23:42:05 +0000 | [diff] [blame] | 164 | RAW_CHECK_MSG(!have_flags, kPrintfFormatsHelp); |
Alexey Samsonov | e954101 | 2012-06-06 13:11:29 +0000 | [diff] [blame] | 165 | result += AppendPointer(&buff, buff_end, va_arg(args, uptr)); |
| 166 | break; |
| 167 | } |
| 168 | case 's': { |
Richard Smith | f493220 | 2012-11-13 23:42:05 +0000 | [diff] [blame] | 169 | RAW_CHECK_MSG(!have_flags, kPrintfFormatsHelp); |
Alexey Samsonov | e954101 | 2012-06-06 13:11:29 +0000 | [diff] [blame] | 170 | result += AppendString(&buff, buff_end, va_arg(args, char*)); |
| 171 | break; |
| 172 | } |
Kostya Serebryany | 81dfbb7 | 2012-09-14 04:35:14 +0000 | [diff] [blame] | 173 | case 'c': { |
Richard Smith | f493220 | 2012-11-13 23:42:05 +0000 | [diff] [blame] | 174 | RAW_CHECK_MSG(!have_flags, kPrintfFormatsHelp); |
Kostya Serebryany | 01167e8 | 2012-09-14 06:52:46 +0000 | [diff] [blame] | 175 | result += AppendChar(&buff, buff_end, va_arg(args, int)); |
Kostya Serebryany | 81dfbb7 | 2012-09-14 04:35:14 +0000 | [diff] [blame] | 176 | break; |
| 177 | } |
Alexey Samsonov | e954101 | 2012-06-06 13:11:29 +0000 | [diff] [blame] | 178 | case '%' : { |
Richard Smith | f493220 | 2012-11-13 23:42:05 +0000 | [diff] [blame] | 179 | RAW_CHECK_MSG(!have_flags, kPrintfFormatsHelp); |
Alexey Samsonov | e954101 | 2012-06-06 13:11:29 +0000 | [diff] [blame] | 180 | result += AppendChar(&buff, buff_end, '%'); |
| 181 | break; |
| 182 | } |
| 183 | default: { |
| 184 | RAW_CHECK_MSG(false, kPrintfFormatsHelp); |
| 185 | } |
| 186 | } |
| 187 | } |
| 188 | RAW_CHECK(buff <= buff_end); |
| 189 | AppendChar(&buff, buff_end + 1, '\0'); |
| 190 | return result; |
| 191 | } |
| 192 | |
Kostya Serebryany | 283c296 | 2012-08-28 11:34:40 +0000 | [diff] [blame] | 193 | static void (*PrintfAndReportCallback)(const char *); |
| 194 | void SetPrintfAndReportCallback(void (*callback)(const char *)) { |
| 195 | PrintfAndReportCallback = callback; |
| 196 | } |
| 197 | |
Alexey Samsonov | 78b580f | 2013-04-03 13:22:54 +0000 | [diff] [blame] | 198 | #if SANITIZER_SUPPORTS_WEAK_HOOKS |
Dmitry Vyukov | 13f62b2 | 2013-03-25 12:58:09 +0000 | [diff] [blame] | 199 | // Can be overriden in frontend. |
Alexey Samsonov | 78b580f | 2013-04-03 13:22:54 +0000 | [diff] [blame] | 200 | SANITIZER_WEAK_ATTRIBUTE SANITIZER_INTERFACE_ATTRIBUTE |
| 201 | void OnPrint(const char *str); |
Dmitry Vyukov | 13f62b2 | 2013-03-25 12:58:09 +0000 | [diff] [blame] | 202 | #endif |
| 203 | |
| 204 | static void CallPrintfAndReportCallback(const char *str) { |
Alexey Samsonov | 78b580f | 2013-04-03 13:22:54 +0000 | [diff] [blame] | 205 | #if SANITIZER_SUPPORTS_WEAK_HOOKS |
| 206 | if (&OnPrint != NULL) |
| 207 | OnPrint(str); |
Dmitry Vyukov | 13f62b2 | 2013-03-25 12:58:09 +0000 | [diff] [blame] | 208 | #endif |
| 209 | if (PrintfAndReportCallback) |
| 210 | PrintfAndReportCallback(str); |
| 211 | } |
| 212 | |
Alexey Samsonov | 99f1e20 | 2013-04-18 13:18:23 +0000 | [diff] [blame] | 213 | static void SharedPrintfCode(bool append_pid, const char *format, |
| 214 | va_list args) { |
Dmitry Vyukov | 4f5e4bb | 2013-04-30 12:27:48 +0000 | [diff] [blame] | 215 | va_list args2; |
| 216 | va_copy(args2, args); |
Alexey Samsonov | b1fe302 | 2012-11-02 12:17:51 +0000 | [diff] [blame] | 217 | const int kLen = 16 * 1024; |
Alexey Samsonov | 99f1e20 | 2013-04-18 13:18:23 +0000 | [diff] [blame] | 218 | // |local_buffer| is small enough not to overflow the stack and/or violate |
| 219 | // the stack limit enforced by TSan (-Wframe-larger-than=512). On the other |
| 220 | // hand, the bigger the buffer is, the more the chance the error report will |
| 221 | // fit into it. |
| 222 | char local_buffer[400]; |
| 223 | int needed_length; |
| 224 | char *buffer = local_buffer; |
| 225 | int buffer_size = ARRAY_SIZE(local_buffer); |
| 226 | // First try to print a message using a local buffer, and then fall back to |
| 227 | // mmaped buffer. |
| 228 | for (int use_mmap = 0; use_mmap < 2; use_mmap++) { |
| 229 | if (use_mmap) { |
Dmitry Vyukov | 4f5e4bb | 2013-04-30 12:27:48 +0000 | [diff] [blame] | 230 | va_end(args); |
| 231 | va_copy(args, args2); |
Alexey Samsonov | 99f1e20 | 2013-04-18 13:18:23 +0000 | [diff] [blame] | 232 | buffer = (char*)MmapOrDie(kLen, "Report"); |
| 233 | buffer_size = kLen; |
| 234 | } |
| 235 | needed_length = 0; |
| 236 | if (append_pid) { |
Peter Collingbourne | 0b694fc | 2013-05-17 16:56:53 +0000 | [diff] [blame] | 237 | int pid = internal_getpid(); |
Alexey Samsonov | 99f1e20 | 2013-04-18 13:18:23 +0000 | [diff] [blame] | 238 | needed_length += internal_snprintf(buffer, buffer_size, "==%d==", pid); |
| 239 | if (needed_length >= buffer_size) { |
| 240 | // The pid doesn't fit into the current buffer. |
| 241 | if (!use_mmap) |
| 242 | continue; |
| 243 | RAW_CHECK_MSG(needed_length < kLen, "Buffer in Report is too short!\n"); |
| 244 | } |
| 245 | } |
| 246 | needed_length += VSNPrintf(buffer + needed_length, |
| 247 | buffer_size - needed_length, format, args); |
| 248 | if (needed_length >= buffer_size) { |
| 249 | // The message doesn't fit into the current buffer. |
| 250 | if (!use_mmap) |
| 251 | continue; |
| 252 | RAW_CHECK_MSG(needed_length < kLen, "Buffer in Report is too short!\n"); |
| 253 | } |
| 254 | // If the message fit into the buffer, print it and exit. |
| 255 | break; |
| 256 | } |
| 257 | RawWrite(buffer); |
| 258 | CallPrintfAndReportCallback(buffer); |
| 259 | // If we had mapped any memory, clean up. |
| 260 | if (buffer != local_buffer) |
| 261 | UnmapOrDie((void *)buffer, buffer_size); |
Dmitry Vyukov | 4f5e4bb | 2013-04-30 12:27:48 +0000 | [diff] [blame] | 262 | va_end(args2); |
Alexey Samsonov | 99f1e20 | 2013-04-18 13:18:23 +0000 | [diff] [blame] | 263 | } |
| 264 | |
| 265 | void Printf(const char *format, ...) { |
Alexey Samsonov | e954101 | 2012-06-06 13:11:29 +0000 | [diff] [blame] | 266 | va_list args; |
| 267 | va_start(args, format); |
Alexey Samsonov | 99f1e20 | 2013-04-18 13:18:23 +0000 | [diff] [blame] | 268 | SharedPrintfCode(false, format, args); |
Alexey Samsonov | e954101 | 2012-06-06 13:11:29 +0000 | [diff] [blame] | 269 | va_end(args); |
Alexey Samsonov | 99f1e20 | 2013-04-18 13:18:23 +0000 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | // Like Printf, but prints the current PID before the output string. |
| 273 | void Report(const char *format, ...) { |
| 274 | va_list args; |
| 275 | va_start(args, format); |
| 276 | SharedPrintfCode(true, format, args); |
| 277 | va_end(args); |
Alexey Samsonov | e954101 | 2012-06-06 13:11:29 +0000 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | // Writes at most "length" symbols to "buffer" (including trailing '\0'). |
| 281 | // Returns the number of symbols that should have been written to buffer |
| 282 | // (not including trailing '\0'). Thus, the string is truncated |
| 283 | // iff return value is not less than "length". |
Alexey Samsonov | de08c02 | 2012-06-19 09:21:57 +0000 | [diff] [blame] | 284 | int internal_snprintf(char *buffer, uptr length, const char *format, ...) { |
Alexey Samsonov | e954101 | 2012-06-06 13:11:29 +0000 | [diff] [blame] | 285 | va_list args; |
| 286 | va_start(args, format); |
| 287 | int needed_length = VSNPrintf(buffer, length, format, args); |
| 288 | va_end(args); |
| 289 | return needed_length; |
| 290 | } |
| 291 | |
Alexey Samsonov | e954101 | 2012-06-06 13:11:29 +0000 | [diff] [blame] | 292 | } // namespace __sanitizer |