2012-06-06 21:11:29 +08:00
|
|
|
//===-- sanitizer_printf.cc -----------------------------------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file is shared between AddressSanitizer and ThreadSanitizer.
|
|
|
|
//
|
|
|
|
// Internal printf function, used inside run-time libraries.
|
|
|
|
// We can't use libc printf because we intercept some of the functions used
|
|
|
|
// inside it.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
|
|
|
|
#include "sanitizer_common.h"
|
2013-12-05 20:04:51 +08:00
|
|
|
#include "sanitizer_flags.h"
|
2012-06-06 21:11:29 +08:00
|
|
|
#include "sanitizer_libc.h"
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
|
2013-06-10 18:02:02 +08:00
|
|
|
#if SANITIZER_WINDOWS && !defined(va_copy)
|
2013-04-30 21:30:29 +08:00
|
|
|
# define va_copy(dst, src) ((dst) = (src))
|
|
|
|
#endif
|
|
|
|
|
2012-06-06 21:11:29 +08:00
|
|
|
namespace __sanitizer {
|
|
|
|
|
2013-04-05 15:30:29 +08:00
|
|
|
StaticSpinMutex CommonSanitizerReportMutex;
|
|
|
|
|
2012-06-06 21:11:29 +08:00
|
|
|
static int AppendChar(char **buff, const char *buff_end, char c) {
|
|
|
|
if (*buff < buff_end) {
|
|
|
|
**buff = c;
|
|
|
|
(*buff)++;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Appends number in a given base to buffer. If its length is less than
|
2013-06-27 23:30:44 +08:00
|
|
|
// |minimal_num_length|, it is padded with leading zeroes or spaces, depending
|
|
|
|
// on the value of |pad_with_zero|.
|
|
|
|
static int AppendNumber(char **buff, const char *buff_end, u64 absolute_value,
|
|
|
|
u8 base, u8 minimal_num_length, bool pad_with_zero,
|
|
|
|
bool negative) {
|
2012-06-06 21:11:29 +08:00
|
|
|
uptr const kMaxLen = 30;
|
|
|
|
RAW_CHECK(base == 10 || base == 16);
|
2013-06-27 23:30:44 +08:00
|
|
|
RAW_CHECK(base == 10 || !negative);
|
|
|
|
RAW_CHECK(absolute_value || !negative);
|
2012-06-06 21:11:29 +08:00
|
|
|
RAW_CHECK(minimal_num_length < kMaxLen);
|
2013-06-27 23:30:44 +08:00
|
|
|
int result = 0;
|
|
|
|
if (negative && minimal_num_length)
|
|
|
|
--minimal_num_length;
|
|
|
|
if (negative && pad_with_zero)
|
|
|
|
result += AppendChar(buff, buff_end, '-');
|
2012-06-06 21:11:29 +08:00
|
|
|
uptr num_buffer[kMaxLen];
|
2013-06-27 23:30:44 +08:00
|
|
|
int pos = 0;
|
2012-06-06 21:11:29 +08:00
|
|
|
do {
|
2013-06-27 23:30:44 +08:00
|
|
|
RAW_CHECK_MSG((uptr)pos < kMaxLen, "AppendNumber buffer overflow");
|
|
|
|
num_buffer[pos++] = absolute_value % base;
|
|
|
|
absolute_value /= base;
|
|
|
|
} while (absolute_value > 0);
|
2012-11-21 19:12:57 +08:00
|
|
|
if (pos < minimal_num_length) {
|
|
|
|
// Make sure compiler doesn't insert call to memset here.
|
|
|
|
internal_memset(&num_buffer[pos], 0,
|
|
|
|
sizeof(num_buffer[0]) * (minimal_num_length - pos));
|
|
|
|
pos = minimal_num_length;
|
|
|
|
}
|
2013-06-27 23:30:44 +08:00
|
|
|
RAW_CHECK(pos > 0);
|
|
|
|
pos--;
|
|
|
|
for (; pos >= 0 && num_buffer[pos] == 0; pos--) {
|
|
|
|
char c = (pad_with_zero || pos == 0) ? '0' : ' ';
|
|
|
|
result += AppendChar(buff, buff_end, c);
|
|
|
|
}
|
|
|
|
if (negative && !pad_with_zero) result += AppendChar(buff, buff_end, '-');
|
|
|
|
for (; pos >= 0; pos--) {
|
2013-05-29 22:11:44 +08:00
|
|
|
char digit = static_cast<char>(num_buffer[pos]);
|
2012-06-06 21:11:29 +08:00
|
|
|
result += AppendChar(buff, buff_end, (digit < 10) ? '0' + digit
|
|
|
|
: 'a' + digit - 10);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2013-06-27 23:30:44 +08:00
|
|
|
static int AppendUnsigned(char **buff, const char *buff_end, u64 num, u8 base,
|
|
|
|
u8 minimal_num_length, bool pad_with_zero) {
|
|
|
|
return AppendNumber(buff, buff_end, num, base, minimal_num_length,
|
|
|
|
pad_with_zero, false /* negative */);
|
|
|
|
}
|
|
|
|
|
2012-11-14 07:42:05 +08:00
|
|
|
static int AppendSignedDecimal(char **buff, const char *buff_end, s64 num,
|
2013-06-27 23:30:44 +08:00
|
|
|
u8 minimal_num_length, bool pad_with_zero) {
|
|
|
|
bool negative = (num < 0);
|
|
|
|
return AppendNumber(buff, buff_end, (u64)(negative ? -num : num), 10,
|
|
|
|
minimal_num_length, pad_with_zero, negative);
|
2012-06-06 21:11:29 +08:00
|
|
|
}
|
|
|
|
|
2014-01-21 18:59:44 +08:00
|
|
|
static int AppendString(char **buff, const char *buff_end, int precision,
|
|
|
|
const char *s) {
|
2012-06-06 21:11:29 +08:00
|
|
|
if (s == 0)
|
|
|
|
s = "<null>";
|
|
|
|
int result = 0;
|
|
|
|
for (; *s; s++) {
|
2014-01-21 18:59:44 +08:00
|
|
|
if (precision >= 0 && result >= precision)
|
|
|
|
break;
|
2012-06-06 21:11:29 +08:00
|
|
|
result += AppendChar(buff, buff_end, *s);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int AppendPointer(char **buff, const char *buff_end, u64 ptr_value) {
|
|
|
|
int result = 0;
|
2014-01-21 18:59:44 +08:00
|
|
|
result += AppendString(buff, buff_end, -1, "0x");
|
2012-06-06 21:11:29 +08:00
|
|
|
result += AppendUnsigned(buff, buff_end, ptr_value, 16,
|
2013-06-27 23:30:44 +08:00
|
|
|
(SANITIZER_WORDSIZE == 64) ? 12 : 8, true);
|
2012-06-06 21:11:29 +08:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
int VSNPrintf(char *buff, int buff_length,
|
|
|
|
const char *format, va_list args) {
|
2012-11-14 07:42:05 +08:00
|
|
|
static const char *kPrintfFormatsHelp =
|
2014-01-21 18:59:44 +08:00
|
|
|
"Supported Printf formats: %([0-9]*)?(z|ll)?{d,u,x}; %p; %(\\.\\*)?s; %c\n";
|
2012-06-06 21:11:29 +08:00
|
|
|
RAW_CHECK(format);
|
|
|
|
RAW_CHECK(buff_length > 0);
|
|
|
|
const char *buff_end = &buff[buff_length - 1];
|
|
|
|
const char *cur = format;
|
|
|
|
int result = 0;
|
|
|
|
for (; *cur; cur++) {
|
|
|
|
if (*cur != '%') {
|
|
|
|
result += AppendChar(&buff, buff_end, *cur);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
cur++;
|
2013-06-27 23:30:44 +08:00
|
|
|
bool have_width = (*cur >= '0' && *cur <= '9');
|
|
|
|
bool pad_with_zero = (*cur == '0');
|
2012-11-14 07:42:05 +08:00
|
|
|
int width = 0;
|
|
|
|
if (have_width) {
|
|
|
|
while (*cur >= '0' && *cur <= '9') {
|
|
|
|
width = width * 10 + *cur++ - '0';
|
|
|
|
}
|
|
|
|
}
|
2014-01-21 18:59:44 +08:00
|
|
|
bool have_precision = (cur[0] == '.' && cur[1] == '*');
|
|
|
|
int precision = -1;
|
|
|
|
if (have_precision) {
|
|
|
|
cur += 2;
|
|
|
|
precision = va_arg(args, int);
|
|
|
|
}
|
2012-06-06 21:11:29 +08:00
|
|
|
bool have_z = (*cur == 'z');
|
|
|
|
cur += have_z;
|
2012-11-14 07:42:05 +08:00
|
|
|
bool have_ll = !have_z && (cur[0] == 'l' && cur[1] == 'l');
|
|
|
|
cur += have_ll * 2;
|
2012-06-06 21:11:29 +08:00
|
|
|
s64 dval;
|
|
|
|
u64 uval;
|
2012-11-14 07:42:05 +08:00
|
|
|
bool have_flags = have_width | have_z | have_ll;
|
2014-01-21 18:59:44 +08:00
|
|
|
// Only %s supports precision for now
|
|
|
|
CHECK(!(precision >= 0 && *cur != 's'));
|
2012-06-06 21:11:29 +08:00
|
|
|
switch (*cur) {
|
|
|
|
case 'd': {
|
2012-11-14 07:42:05 +08:00
|
|
|
dval = have_ll ? va_arg(args, s64)
|
|
|
|
: have_z ? va_arg(args, sptr)
|
|
|
|
: va_arg(args, int);
|
2013-06-27 23:30:44 +08:00
|
|
|
result += AppendSignedDecimal(&buff, buff_end, dval, width,
|
|
|
|
pad_with_zero);
|
2012-06-06 21:11:29 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'u':
|
|
|
|
case 'x': {
|
2012-11-14 07:42:05 +08:00
|
|
|
uval = have_ll ? va_arg(args, u64)
|
|
|
|
: have_z ? va_arg(args, uptr)
|
|
|
|
: va_arg(args, unsigned);
|
2012-06-06 21:11:29 +08:00
|
|
|
result += AppendUnsigned(&buff, buff_end, uval,
|
2013-06-27 23:30:44 +08:00
|
|
|
(*cur == 'u') ? 10 : 16, width, pad_with_zero);
|
2012-06-06 21:11:29 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'p': {
|
2012-11-14 07:42:05 +08:00
|
|
|
RAW_CHECK_MSG(!have_flags, kPrintfFormatsHelp);
|
2012-06-06 21:11:29 +08:00
|
|
|
result += AppendPointer(&buff, buff_end, va_arg(args, uptr));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 's': {
|
2012-11-14 07:42:05 +08:00
|
|
|
RAW_CHECK_MSG(!have_flags, kPrintfFormatsHelp);
|
2014-01-21 18:59:44 +08:00
|
|
|
result += AppendString(&buff, buff_end, precision, va_arg(args, char*));
|
2012-06-06 21:11:29 +08:00
|
|
|
break;
|
|
|
|
}
|
2012-09-14 12:35:14 +08:00
|
|
|
case 'c': {
|
2012-11-14 07:42:05 +08:00
|
|
|
RAW_CHECK_MSG(!have_flags, kPrintfFormatsHelp);
|
2012-09-14 14:52:46 +08:00
|
|
|
result += AppendChar(&buff, buff_end, va_arg(args, int));
|
2012-09-14 12:35:14 +08:00
|
|
|
break;
|
|
|
|
}
|
2012-06-06 21:11:29 +08:00
|
|
|
case '%' : {
|
2012-11-14 07:42:05 +08:00
|
|
|
RAW_CHECK_MSG(!have_flags, kPrintfFormatsHelp);
|
2012-06-06 21:11:29 +08:00
|
|
|
result += AppendChar(&buff, buff_end, '%');
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default: {
|
|
|
|
RAW_CHECK_MSG(false, kPrintfFormatsHelp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
RAW_CHECK(buff <= buff_end);
|
|
|
|
AppendChar(&buff, buff_end + 1, '\0');
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2012-08-28 19:34:40 +08:00
|
|
|
static void (*PrintfAndReportCallback)(const char *);
|
|
|
|
void SetPrintfAndReportCallback(void (*callback)(const char *)) {
|
|
|
|
PrintfAndReportCallback = callback;
|
|
|
|
}
|
|
|
|
|
2013-03-25 20:58:09 +08:00
|
|
|
// Can be overriden in frontend.
|
2013-10-14 17:52:40 +08:00
|
|
|
#if SANITIZER_SUPPORTS_WEAK_HOOKS
|
2013-08-13 19:42:45 +08:00
|
|
|
SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE
|
2013-10-14 17:52:40 +08:00
|
|
|
void OnPrint(const char *str) {
|
|
|
|
(void)str;
|
|
|
|
}
|
|
|
|
#elif defined(SANITIZER_GO) && defined(TSAN_EXTERNAL_HOOKS)
|
2013-04-03 21:22:54 +08:00
|
|
|
void OnPrint(const char *str);
|
2013-10-14 17:52:40 +08:00
|
|
|
#else
|
|
|
|
void OnPrint(const char *str) {
|
|
|
|
(void)str;
|
|
|
|
}
|
2013-03-25 20:58:09 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
static void CallPrintfAndReportCallback(const char *str) {
|
2013-10-14 17:52:40 +08:00
|
|
|
OnPrint(str);
|
2013-03-25 20:58:09 +08:00
|
|
|
if (PrintfAndReportCallback)
|
|
|
|
PrintfAndReportCallback(str);
|
|
|
|
}
|
|
|
|
|
2013-04-18 21:18:23 +08:00
|
|
|
static void SharedPrintfCode(bool append_pid, const char *format,
|
|
|
|
va_list args) {
|
2013-04-30 20:27:48 +08:00
|
|
|
va_list args2;
|
|
|
|
va_copy(args2, args);
|
2012-11-02 20:17:51 +08:00
|
|
|
const int kLen = 16 * 1024;
|
2013-03-11 21:36:39 +08:00
|
|
|
// |local_buffer| is small enough not to overflow the stack and/or violate
|
|
|
|
// the stack limit enforced by TSan (-Wframe-larger-than=512). On the other
|
|
|
|
// hand, the bigger the buffer is, the more the chance the error report will
|
|
|
|
// fit into it.
|
|
|
|
char local_buffer[400];
|
2013-03-11 19:47:43 +08:00
|
|
|
int needed_length;
|
|
|
|
char *buffer = local_buffer;
|
2013-04-18 21:18:23 +08:00
|
|
|
int buffer_size = ARRAY_SIZE(local_buffer);
|
|
|
|
// First try to print a message using a local buffer, and then fall back to
|
|
|
|
// mmaped buffer.
|
2013-03-11 19:47:43 +08:00
|
|
|
for (int use_mmap = 0; use_mmap < 2; use_mmap++) {
|
2013-04-18 21:18:23 +08:00
|
|
|
if (use_mmap) {
|
2013-04-30 20:27:48 +08:00
|
|
|
va_end(args);
|
|
|
|
va_copy(args, args2);
|
2013-04-18 21:18:23 +08:00
|
|
|
buffer = (char*)MmapOrDie(kLen, "Report");
|
|
|
|
buffer_size = kLen;
|
|
|
|
}
|
|
|
|
needed_length = 0;
|
|
|
|
if (append_pid) {
|
2013-05-18 00:56:53 +08:00
|
|
|
int pid = internal_getpid();
|
2013-04-18 21:18:23 +08:00
|
|
|
needed_length += internal_snprintf(buffer, buffer_size, "==%d==", pid);
|
|
|
|
if (needed_length >= buffer_size) {
|
|
|
|
// The pid doesn't fit into the current buffer.
|
|
|
|
if (!use_mmap)
|
|
|
|
continue;
|
2013-03-11 19:47:43 +08:00
|
|
|
RAW_CHECK_MSG(needed_length < kLen, "Buffer in Report is too short!\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
needed_length += VSNPrintf(buffer + needed_length,
|
2013-04-18 21:18:23 +08:00
|
|
|
buffer_size - needed_length, format, args);
|
|
|
|
if (needed_length >= buffer_size) {
|
|
|
|
// The message doesn't fit into the current buffer.
|
|
|
|
if (!use_mmap)
|
2013-03-11 19:47:43 +08:00
|
|
|
continue;
|
2013-04-18 21:18:23 +08:00
|
|
|
RAW_CHECK_MSG(needed_length < kLen, "Buffer in Report is too short!\n");
|
2013-03-11 19:47:43 +08:00
|
|
|
}
|
2013-04-18 21:18:23 +08:00
|
|
|
// If the message fit into the buffer, print it and exit.
|
|
|
|
break;
|
2013-03-11 19:47:43 +08:00
|
|
|
}
|
2013-04-18 21:18:23 +08:00
|
|
|
RawWrite(buffer);
|
2014-01-23 19:34:41 +08:00
|
|
|
AndroidLogWrite(buffer);
|
2013-04-18 21:18:23 +08:00
|
|
|
CallPrintfAndReportCallback(buffer);
|
2013-03-11 19:47:43 +08:00
|
|
|
// If we had mapped any memory, clean up.
|
2013-04-18 21:18:23 +08:00
|
|
|
if (buffer != local_buffer)
|
|
|
|
UnmapOrDie((void *)buffer, buffer_size);
|
2013-04-30 20:27:48 +08:00
|
|
|
va_end(args2);
|
2013-04-18 21:18:23 +08:00
|
|
|
}
|
|
|
|
|
2014-01-23 16:58:34 +08:00
|
|
|
FORMAT(1, 2)
|
2013-04-18 21:18:23 +08:00
|
|
|
void Printf(const char *format, ...) {
|
|
|
|
va_list args;
|
|
|
|
va_start(args, format);
|
|
|
|
SharedPrintfCode(false, format, args);
|
|
|
|
va_end(args);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Like Printf, but prints the current PID before the output string.
|
2014-01-23 16:58:34 +08:00
|
|
|
FORMAT(1, 2)
|
2013-04-18 21:18:23 +08:00
|
|
|
void Report(const char *format, ...) {
|
|
|
|
va_list args;
|
|
|
|
va_start(args, format);
|
|
|
|
SharedPrintfCode(true, format, args);
|
|
|
|
va_end(args);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Writes at most "length" symbols to "buffer" (including trailing '\0').
|
|
|
|
// Returns the number of symbols that should have been written to buffer
|
|
|
|
// (not including trailing '\0'). Thus, the string is truncated
|
|
|
|
// iff return value is not less than "length".
|
2014-01-23 16:58:34 +08:00
|
|
|
FORMAT(3, 4)
|
2013-04-18 21:18:23 +08:00
|
|
|
int internal_snprintf(char *buffer, uptr length, const char *format, ...) {
|
|
|
|
va_list args;
|
|
|
|
va_start(args, format);
|
|
|
|
int needed_length = VSNPrintf(buffer, length, format, args);
|
|
|
|
va_end(args);
|
|
|
|
return needed_length;
|
2012-06-06 21:11:29 +08:00
|
|
|
}
|
|
|
|
|
2014-01-23 16:58:34 +08:00
|
|
|
FORMAT(2, 3)
|
2013-11-14 17:41:24 +08:00
|
|
|
void InternalScopedString::append(const char *format, ...) {
|
|
|
|
CHECK_LT(length_, size());
|
|
|
|
va_list args;
|
|
|
|
va_start(args, format);
|
|
|
|
VSNPrintf(data() + length_, size() - length_, format, args);
|
|
|
|
va_end(args);
|
|
|
|
length_ += internal_strlen(data() + length_);
|
2014-01-23 19:01:07 +08:00
|
|
|
CHECK_LT(length_, size());
|
2013-11-14 17:41:24 +08:00
|
|
|
}
|
|
|
|
|
2012-06-06 21:11:29 +08:00
|
|
|
} // namespace __sanitizer
|