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