sanitizer_printf.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. //===-- sanitizer_printf.cpp ----------------------------------------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This file is shared between AddressSanitizer and ThreadSanitizer.
  10. //
  11. // Internal printf function, used inside run-time libraries.
  12. // We can't use libc printf because we intercept some of the functions used
  13. // inside it.
  14. //===----------------------------------------------------------------------===//
  15. #include "sanitizer_common.h"
  16. #include "sanitizer_flags.h"
  17. #include "sanitizer_libc.h"
  18. #include <stdio.h>
  19. #include <stdarg.h>
  20. #if SANITIZER_WINDOWS && defined(_MSC_VER) && _MSC_VER < 1800 && \
  21. !defined(va_copy)
  22. # define va_copy(dst, src) ((dst) = (src))
  23. #endif
  24. namespace __sanitizer {
  25. static int AppendChar(char **buff, const char *buff_end, char c) {
  26. if (*buff < buff_end) {
  27. **buff = c;
  28. (*buff)++;
  29. }
  30. return 1;
  31. }
  32. // Appends number in a given base to buffer. If its length is less than
  33. // |minimal_num_length|, it is padded with leading zeroes or spaces, depending
  34. // on the value of |pad_with_zero|.
  35. static int AppendNumber(char **buff, const char *buff_end, u64 absolute_value,
  36. u8 base, u8 minimal_num_length, bool pad_with_zero,
  37. bool negative, bool uppercase) {
  38. uptr const kMaxLen = 30;
  39. RAW_CHECK(base == 10 || base == 16);
  40. RAW_CHECK(base == 10 || !negative);
  41. RAW_CHECK(absolute_value || !negative);
  42. RAW_CHECK(minimal_num_length < kMaxLen);
  43. int result = 0;
  44. if (negative && minimal_num_length)
  45. --minimal_num_length;
  46. if (negative && pad_with_zero)
  47. result += AppendChar(buff, buff_end, '-');
  48. uptr num_buffer[kMaxLen];
  49. int pos = 0;
  50. do {
  51. RAW_CHECK_MSG((uptr)pos < kMaxLen, "AppendNumber buffer overflow");
  52. num_buffer[pos++] = absolute_value % base;
  53. absolute_value /= base;
  54. } while (absolute_value > 0);
  55. if (pos < minimal_num_length) {
  56. // Make sure compiler doesn't insert call to memset here.
  57. internal_memset(&num_buffer[pos], 0,
  58. sizeof(num_buffer[0]) * (minimal_num_length - pos));
  59. pos = minimal_num_length;
  60. }
  61. RAW_CHECK(pos > 0);
  62. pos--;
  63. for (; pos >= 0 && num_buffer[pos] == 0; pos--) {
  64. char c = (pad_with_zero || pos == 0) ? '0' : ' ';
  65. result += AppendChar(buff, buff_end, c);
  66. }
  67. if (negative && !pad_with_zero) result += AppendChar(buff, buff_end, '-');
  68. for (; pos >= 0; pos--) {
  69. char digit = static_cast<char>(num_buffer[pos]);
  70. digit = (digit < 10) ? '0' + digit : (uppercase ? 'A' : 'a') + digit - 10;
  71. result += AppendChar(buff, buff_end, digit);
  72. }
  73. return result;
  74. }
  75. static int AppendUnsigned(char **buff, const char *buff_end, u64 num, u8 base,
  76. u8 minimal_num_length, bool pad_with_zero,
  77. bool uppercase) {
  78. return AppendNumber(buff, buff_end, num, base, minimal_num_length,
  79. pad_with_zero, false /* negative */, uppercase);
  80. }
  81. static int AppendSignedDecimal(char **buff, const char *buff_end, s64 num,
  82. u8 minimal_num_length, bool pad_with_zero) {
  83. bool negative = (num < 0);
  84. return AppendNumber(buff, buff_end, (u64)(negative ? -num : num), 10,
  85. minimal_num_length, pad_with_zero, negative,
  86. false /* uppercase */);
  87. }
  88. // Use the fact that explicitly requesting 0 width (%0s) results in UB and
  89. // interpret width == 0 as "no width requested":
  90. // width == 0 - no width requested
  91. // width < 0 - left-justify s within and pad it to -width chars, if necessary
  92. // width > 0 - right-justify s, not implemented yet
  93. static int AppendString(char **buff, const char *buff_end, int width,
  94. int max_chars, const char *s) {
  95. if (!s)
  96. s = "<null>";
  97. int result = 0;
  98. for (; *s; s++) {
  99. if (max_chars >= 0 && result >= max_chars)
  100. break;
  101. result += AppendChar(buff, buff_end, *s);
  102. }
  103. // Only the left justified strings are supported.
  104. while (width < -result)
  105. result += AppendChar(buff, buff_end, ' ');
  106. return result;
  107. }
  108. static int AppendPointer(char **buff, const char *buff_end, u64 ptr_value) {
  109. int result = 0;
  110. result += AppendString(buff, buff_end, 0, -1, "0x");
  111. result += AppendUnsigned(buff, buff_end, ptr_value, 16,
  112. SANITIZER_POINTER_FORMAT_LENGTH,
  113. true /* pad_with_zero */, false /* uppercase */);
  114. return result;
  115. }
  116. int VSNPrintf(char *buff, int buff_length,
  117. const char *format, va_list args) {
  118. static const char *kPrintfFormatsHelp =
  119. "Supported Printf formats: %([0-9]*)?(z|l|ll)?{d,u,x,X}; %p; "
  120. "%[-]([0-9]*)?(\\.\\*)?s; %c\nProvided format: ";
  121. RAW_CHECK(format);
  122. RAW_CHECK(buff_length > 0);
  123. const char *buff_end = &buff[buff_length - 1];
  124. const char *cur = format;
  125. int result = 0;
  126. for (; *cur; cur++) {
  127. if (*cur != '%') {
  128. result += AppendChar(&buff, buff_end, *cur);
  129. continue;
  130. }
  131. cur++;
  132. bool left_justified = *cur == '-';
  133. if (left_justified)
  134. cur++;
  135. bool have_width = (*cur >= '0' && *cur <= '9');
  136. bool pad_with_zero = (*cur == '0');
  137. int width = 0;
  138. if (have_width) {
  139. while (*cur >= '0' && *cur <= '9') {
  140. width = width * 10 + *cur++ - '0';
  141. }
  142. }
  143. bool have_precision = (cur[0] == '.' && cur[1] == '*');
  144. int precision = -1;
  145. if (have_precision) {
  146. cur += 2;
  147. precision = va_arg(args, int);
  148. }
  149. bool have_z = (*cur == 'z');
  150. cur += have_z;
  151. bool have_l = cur[0] == 'l' && cur[1] != 'l';
  152. cur += have_l;
  153. bool have_ll = cur[0] == 'l' && cur[1] == 'l';
  154. cur += have_ll * 2;
  155. const bool have_length = have_z || have_l || have_ll;
  156. const bool have_flags = have_width || have_length;
  157. // At the moment only %s supports precision and left-justification.
  158. CHECK(!((precision >= 0 || left_justified) && *cur != 's'));
  159. switch (*cur) {
  160. case 'd': {
  161. s64 dval = have_ll ? va_arg(args, s64)
  162. : have_z ? va_arg(args, sptr)
  163. : have_l ? va_arg(args, long)
  164. : va_arg(args, int);
  165. result += AppendSignedDecimal(&buff, buff_end, dval, width,
  166. pad_with_zero);
  167. break;
  168. }
  169. case 'u':
  170. case 'x':
  171. case 'X': {
  172. u64 uval = have_ll ? va_arg(args, u64)
  173. : have_z ? va_arg(args, uptr)
  174. : have_l ? va_arg(args, unsigned long)
  175. : va_arg(args, unsigned);
  176. bool uppercase = (*cur == 'X');
  177. result += AppendUnsigned(&buff, buff_end, uval, (*cur == 'u') ? 10 : 16,
  178. width, pad_with_zero, uppercase);
  179. break;
  180. }
  181. case 'p': {
  182. RAW_CHECK_VA(!have_flags, kPrintfFormatsHelp, format);
  183. result += AppendPointer(&buff, buff_end, va_arg(args, uptr));
  184. break;
  185. }
  186. case 's': {
  187. RAW_CHECK_VA(!have_length, kPrintfFormatsHelp, format);
  188. // Only left-justified width is supported.
  189. CHECK(!have_width || left_justified);
  190. result += AppendString(&buff, buff_end, left_justified ? -width : width,
  191. precision, va_arg(args, char*));
  192. break;
  193. }
  194. case 'c': {
  195. RAW_CHECK_VA(!have_flags, kPrintfFormatsHelp, format);
  196. result += AppendChar(&buff, buff_end, va_arg(args, int));
  197. break;
  198. }
  199. case '%' : {
  200. RAW_CHECK_VA(!have_flags, kPrintfFormatsHelp, format);
  201. result += AppendChar(&buff, buff_end, '%');
  202. break;
  203. }
  204. default: {
  205. RAW_CHECK_VA(false, kPrintfFormatsHelp, format);
  206. }
  207. }
  208. }
  209. RAW_CHECK(buff <= buff_end);
  210. AppendChar(&buff, buff_end + 1, '\0');
  211. return result;
  212. }
  213. static void (*PrintfAndReportCallback)(const char *);
  214. void SetPrintfAndReportCallback(void (*callback)(const char *)) {
  215. PrintfAndReportCallback = callback;
  216. }
  217. // Can be overriden in frontend.
  218. #if SANITIZER_GO && defined(TSAN_EXTERNAL_HOOKS)
  219. // Implementation must be defined in frontend.
  220. extern "C" void __sanitizer_on_print(const char *str);
  221. #else
  222. SANITIZER_INTERFACE_WEAK_DEF(void, __sanitizer_on_print, const char *str) {
  223. (void)str;
  224. }
  225. #endif
  226. static void CallPrintfAndReportCallback(const char *str) {
  227. __sanitizer_on_print(str);
  228. if (PrintfAndReportCallback)
  229. PrintfAndReportCallback(str);
  230. }
  231. static void NOINLINE SharedPrintfCodeNoBuffer(bool append_pid,
  232. char *local_buffer,
  233. int buffer_size,
  234. const char *format,
  235. va_list args) {
  236. va_list args2;
  237. va_copy(args2, args);
  238. InternalMmapVector<char> v;
  239. int needed_length = 0;
  240. char *buffer = local_buffer;
  241. // First try to print a message using a local buffer, and then fall back to
  242. // mmaped buffer.
  243. for (int use_mmap = 0;; use_mmap++) {
  244. if (use_mmap) {
  245. va_end(args);
  246. va_copy(args, args2);
  247. v.resize(needed_length + 1);
  248. buffer_size = v.capacity();
  249. v.resize(buffer_size);
  250. buffer = &v[0];
  251. }
  252. needed_length = 0;
  253. // Fuchsia's logging infrastructure always keeps track of the logging
  254. // process, thread, and timestamp, so never prepend such information.
  255. if (!SANITIZER_FUCHSIA && append_pid) {
  256. int pid = internal_getpid();
  257. const char *exe_name = GetProcessName();
  258. if (common_flags()->log_exe_name && exe_name) {
  259. needed_length += internal_snprintf(buffer, buffer_size,
  260. "==%s", exe_name);
  261. if (needed_length >= buffer_size)
  262. continue;
  263. }
  264. needed_length += internal_snprintf(
  265. buffer + needed_length, buffer_size - needed_length, "==%d==", pid);
  266. if (needed_length >= buffer_size)
  267. continue;
  268. }
  269. needed_length += VSNPrintf(buffer + needed_length,
  270. buffer_size - needed_length, format, args);
  271. if (needed_length >= buffer_size)
  272. continue;
  273. // If the message fit into the buffer, print it and exit.
  274. break;
  275. }
  276. RawWrite(buffer);
  277. // Remove color sequences from the message.
  278. RemoveANSIEscapeSequencesFromString(buffer);
  279. CallPrintfAndReportCallback(buffer);
  280. LogMessageOnPrintf(buffer);
  281. va_end(args2);
  282. }
  283. static void NOINLINE SharedPrintfCode(bool append_pid, const char *format,
  284. va_list args) {
  285. // |local_buffer| is small enough not to overflow the stack and/or violate
  286. // the stack limit enforced by TSan (-Wframe-larger-than=512). On the other
  287. // hand, the bigger the buffer is, the more the chance the error report will
  288. // fit into it.
  289. char local_buffer[400];
  290. SharedPrintfCodeNoBuffer(append_pid, local_buffer, ARRAY_SIZE(local_buffer),
  291. format, args);
  292. }
  293. void Printf(const char *format, ...) {
  294. va_list args;
  295. va_start(args, format);
  296. SharedPrintfCode(false, format, args);
  297. va_end(args);
  298. }
  299. // Like Printf, but prints the current PID before the output string.
  300. void Report(const char *format, ...) {
  301. va_list args;
  302. va_start(args, format);
  303. SharedPrintfCode(true, format, args);
  304. va_end(args);
  305. }
  306. // Writes at most "length" symbols to "buffer" (including trailing '\0').
  307. // Returns the number of symbols that should have been written to buffer
  308. // (not including trailing '\0'). Thus, the string is truncated
  309. // iff return value is not less than "length".
  310. int internal_snprintf(char *buffer, uptr length, const char *format, ...) {
  311. va_list args;
  312. va_start(args, format);
  313. int needed_length = VSNPrintf(buffer, length, format, args);
  314. va_end(args);
  315. return needed_length;
  316. }
  317. void InternalScopedString::append(const char *format, ...) {
  318. uptr prev_len = length();
  319. while (true) {
  320. buffer_.resize(buffer_.capacity());
  321. va_list args;
  322. va_start(args, format);
  323. uptr sz = VSNPrintf(buffer_.data() + prev_len, buffer_.size() - prev_len,
  324. format, args);
  325. va_end(args);
  326. if (sz < buffer_.size() - prev_len) {
  327. buffer_.resize(prev_len + sz + 1);
  328. break;
  329. }
  330. buffer_.reserve(buffer_.capacity() * 2);
  331. }
  332. CHECK_EQ(buffer_[length()], '\0');
  333. }
  334. } // namespace __sanitizer