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 namespace __sanitizer {
25
AppendChar(char ** buff,const char * buff_end,char c)26 static int AppendChar(char **buff, const char *buff_end, char c) {
27 if (*buff < buff_end) {
28 **buff = c;
29 (*buff)++;
30 }
31 return 1;
32 }
33
34 // Appends number in a given base to buffer. If its length is less than
35 // "minimal_num_length", it is padded with leading zeroes.
AppendUnsigned(char ** buff,const char * buff_end,u64 num,u8 base,u8 minimal_num_length)36 static int AppendUnsigned(char **buff, const char *buff_end, u64 num,
37 u8 base, u8 minimal_num_length) {
38 uptr const kMaxLen = 30;
39 RAW_CHECK(base == 10 || base == 16);
40 RAW_CHECK(minimal_num_length < kMaxLen);
41 uptr num_buffer[kMaxLen];
42 uptr pos = 0;
43 do {
44 RAW_CHECK_MSG(pos < kMaxLen, "appendNumber buffer overflow");
45 num_buffer[pos++] = num % base;
46 num /= base;
47 } while (num > 0);
48 while (pos < minimal_num_length) num_buffer[pos++] = 0;
49 int result = 0;
50 while (pos-- > 0) {
51 uptr digit = num_buffer[pos];
52 result += AppendChar(buff, buff_end, (digit < 10) ? '0' + digit
53 : 'a' + digit - 10);
54 }
55 return result;
56 }
57
AppendSignedDecimal(char ** buff,const char * buff_end,s64 num)58 static int AppendSignedDecimal(char **buff, const char *buff_end, s64 num) {
59 int result = 0;
60 if (num < 0) {
61 result += AppendChar(buff, buff_end, '-');
62 num = -num;
63 }
64 result += AppendUnsigned(buff, buff_end, (u64)num, 10, 0);
65 return result;
66 }
67
AppendString(char ** buff,const char * buff_end,const char * s)68 static int AppendString(char **buff, const char *buff_end, const char *s) {
69 if (s == 0)
70 s = "<null>";
71 int result = 0;
72 for (; *s; s++) {
73 result += AppendChar(buff, buff_end, *s);
74 }
75 return result;
76 }
77
AppendPointer(char ** buff,const char * buff_end,u64 ptr_value)78 static int AppendPointer(char **buff, const char *buff_end, u64 ptr_value) {
79 int result = 0;
80 result += AppendString(buff, buff_end, "0x");
81 result += AppendUnsigned(buff, buff_end, ptr_value, 16,
82 (__WORDSIZE == 64) ? 12 : 8);
83 return result;
84 }
85
VSNPrintf(char * buff,int buff_length,const char * format,va_list args)86 int VSNPrintf(char *buff, int buff_length,
87 const char *format, va_list args) {
88 static const char *kPrintfFormatsHelp = "Supported Printf formats: "
89 "%%[z]{d,u,x}; %%p; %%s\n";
90 RAW_CHECK(format);
91 RAW_CHECK(buff_length > 0);
92 const char *buff_end = &buff[buff_length - 1];
93 const char *cur = format;
94 int result = 0;
95 for (; *cur; cur++) {
96 if (*cur != '%') {
97 result += AppendChar(&buff, buff_end, *cur);
98 continue;
99 }
100 cur++;
101 bool have_z = (*cur == 'z');
102 cur += have_z;
103 s64 dval;
104 u64 uval;
105 switch (*cur) {
106 case 'd': {
107 dval = have_z ? va_arg(args, sptr)
108 : va_arg(args, int);
109 result += AppendSignedDecimal(&buff, buff_end, dval);
110 break;
111 }
112 case 'u':
113 case 'x': {
114 uval = have_z ? va_arg(args, uptr)
115 : va_arg(args, unsigned);
116 result += AppendUnsigned(&buff, buff_end, uval,
117 (*cur == 'u') ? 10 : 16, 0);
118 break;
119 }
120 case 'p': {
121 RAW_CHECK_MSG(!have_z, kPrintfFormatsHelp);
122 result += AppendPointer(&buff, buff_end, va_arg(args, uptr));
123 break;
124 }
125 case 's': {
126 RAW_CHECK_MSG(!have_z, kPrintfFormatsHelp);
127 result += AppendString(&buff, buff_end, va_arg(args, char*));
128 break;
129 }
130 case '%' : {
131 RAW_CHECK_MSG(!have_z, kPrintfFormatsHelp);
132 result += AppendChar(&buff, buff_end, '%');
133 break;
134 }
135 default: {
136 RAW_CHECK_MSG(false, kPrintfFormatsHelp);
137 }
138 }
139 }
140 RAW_CHECK(buff <= buff_end);
141 AppendChar(&buff, buff_end + 1, '\0');
142 return result;
143 }
144
145 static void (*PrintfAndReportCallback)(const char *);
SetPrintfAndReportCallback(void (* callback)(const char *))146 void SetPrintfAndReportCallback(void (*callback)(const char *)) {
147 PrintfAndReportCallback = callback;
148 }
149
Printf(const char * format,...)150 void Printf(const char *format, ...) {
151 const int kLen = 1024 * 4;
152 InternalScopedBuffer<char> buffer(kLen);
153 va_list args;
154 va_start(args, format);
155 int needed_length = VSNPrintf(buffer.data(), kLen, format, args);
156 va_end(args);
157 RAW_CHECK_MSG(needed_length < kLen, "Buffer in Printf is too short!\n");
158 RawWrite(buffer.data());
159 if (PrintfAndReportCallback)
160 PrintfAndReportCallback(buffer.data());
161 }
162
163 // Writes at most "length" symbols to "buffer" (including trailing '\0').
164 // Returns the number of symbols that should have been written to buffer
165 // (not including trailing '\0'). Thus, the string is truncated
166 // iff return value is not less than "length".
internal_snprintf(char * buffer,uptr length,const char * format,...)167 int internal_snprintf(char *buffer, uptr length, const char *format, ...) {
168 va_list args;
169 va_start(args, format);
170 int needed_length = VSNPrintf(buffer, length, format, args);
171 va_end(args);
172 return needed_length;
173 }
174
175 // Like Printf, but prints the current PID before the output string.
Report(const char * format,...)176 void Report(const char *format, ...) {
177 const int kLen = 1024 * 4;
178 InternalScopedBuffer<char> buffer(kLen);
179 int needed_length = internal_snprintf(buffer.data(),
180 kLen, "==%d== ", GetPid());
181 RAW_CHECK_MSG(needed_length < kLen, "Buffer in Report is too short!\n");
182 va_list args;
183 va_start(args, format);
184 needed_length += VSNPrintf(buffer.data() + needed_length,
185 kLen - needed_length, format, args);
186 va_end(args);
187 RAW_CHECK_MSG(needed_length < kLen, "Buffer in Report is too short!\n");
188 RawWrite(buffer.data());
189 if (PrintfAndReportCallback)
190 PrintfAndReportCallback(buffer.data());
191 }
192
193 } // namespace __sanitizer
194