1 //===-- sanitizer_common_interceptors_format.inc ----------------*- C++ -*-===//
3 // This file is distributed under the University of Illinois Open Source
4 // License. See LICENSE.TXT for details.
6 //===----------------------------------------------------------------------===//
8 // Scanf/printf implementation for use in *Sanitizer interceptors.
9 // Follows http://pubs.opengroup.org/onlinepubs/9699919799/functions/fscanf.html
10 // and http://pubs.opengroup.org/onlinepubs/9699919799/functions/fprintf.html
11 // with a few common GNU extensions.
13 //===----------------------------------------------------------------------===//
17 static const char *parse_number(const char *p, int *out) {
18 *out = internal_atoll(p);
19 while (*p >= '0' && *p <= '9')
24 static const char *maybe_parse_param_index(const char *p, int *out) {
26 if (*p >= '0' && *p <= '9') {
28 const char *q = parse_number(p, &number);
36 // Otherwise, do not change p. This will be re-parsed later as the field
41 static bool char_is_one_of(char c, const char *s) {
42 return !!internal_strchr(s, c);
45 static const char *maybe_parse_length_modifier(const char *p, char ll[2]) {
46 if (char_is_one_of(*p, "jztLq")) {
49 } else if (*p == 'h') {
56 } else if (*p == 'l') {
67 // Returns true if the character is an integer conversion specifier.
68 static bool format_is_integer_conv(char c) {
69 return char_is_one_of(c, "diouxXn");
72 // Returns true if the character is an floating point conversion specifier.
73 static bool format_is_float_conv(char c) {
74 return char_is_one_of(c, "aAeEfFgG");
77 // Returns string output character size for string-like conversions,
78 // or 0 if the conversion is invalid.
79 static int format_get_char_size(char convSpecifier,
80 const char lengthModifier[2]) {
81 if (char_is_one_of(convSpecifier, "CS")) {
82 return sizeof(wchar_t);
85 if (char_is_one_of(convSpecifier, "cs[")) {
86 if (lengthModifier[0] == 'l' && lengthModifier[1] == '\0')
87 return sizeof(wchar_t);
88 else if (lengthModifier[0] == '\0')
95 enum FormatStoreSize {
96 // Store size not known in advance; can be calculated as wcslen() of the
97 // destination buffer.
99 // Store size not known in advance; can be calculated as strlen() of the
100 // destination buffer.
102 // Invalid conversion specifier.
106 // Returns the memory size of a format directive (if >0), or a value of
108 static int format_get_value_size(char convSpecifier,
109 const char lengthModifier[2],
110 bool promote_float) {
111 if (format_is_integer_conv(convSpecifier)) {
112 switch (lengthModifier[0]) {
114 return lengthModifier[1] == 'h' ? sizeof(char) : sizeof(short);
116 return lengthModifier[1] == 'l' ? sizeof(long long) : sizeof(long);
118 return sizeof(long long);
120 return sizeof(long long);
122 return sizeof(INTMAX_T);
124 return sizeof(SIZE_T);
126 return sizeof(PTRDIFF_T);
134 if (format_is_float_conv(convSpecifier)) {
135 switch (lengthModifier[0]) {
138 return sizeof(long double);
140 return lengthModifier[1] == 'l' ? sizeof(long double)
143 // Printf promotes floats to doubles but scanf does not
144 return promote_float ? sizeof(double) : sizeof(float);
150 if (convSpecifier == 'p') {
151 if (lengthModifier[0] != 0)
153 return sizeof(void *);
159 struct ScanfDirective {
160 int argIdx; // argument index, or -1 if not specified ("%n$")
164 bool suppressed; // suppress assignment ("*")
165 bool allocate; // allocate space ("m")
166 char lengthModifier[2];
171 // Parse scanf format string. If a valid directive in encountered, it is
172 // returned in dir. This function returns the pointer to the first
173 // unprocessed character, or 0 in case of error.
174 // In case of the end-of-string, a pointer to the closing \0 is returned.
175 static const char *scanf_parse_next(const char *p, bool allowGnuMalloc,
176 ScanfDirective *dir) {
177 internal_memset(dir, 0, sizeof(*dir));
196 p = maybe_parse_param_index(p, &dir->argIdx);
200 dir->suppressed = true;
204 if (*p >= '0' && *p <= '9') {
205 p = parse_number(p, &dir->fieldWidth);
207 if (dir->fieldWidth <= 0) // Width if at all must be non-zero
212 dir->allocate = true;
216 p = maybe_parse_length_modifier(p, dir->lengthModifier);
217 // Conversion specifier.
218 dir->convSpecifier = *p++;
219 // Consume %[...] expression.
220 if (dir->convSpecifier == '[') {
225 while (*p && *p != ']')
228 return nullptr; // unexpected end of string
229 // Consume the closing ']'.
232 // This is unfortunately ambiguous between old GNU extension
233 // of %as, %aS and %a[...] and newer POSIX %a followed by
234 // letters s, S or [.
235 if (allowGnuMalloc && dir->convSpecifier == 'a' &&
236 !dir->lengthModifier[0]) {
237 if (*p == 's' || *p == 'S') {
238 dir->maybeGnuMalloc = true;
240 } else if (*p == '[') {
241 // Watch for %a[h-j%d], if % appears in the
242 // [...] range, then we need to give up, we don't know
243 // if scanf will parse it as POSIX %a [h-j %d ] or
244 // GNU allocation of string with range dh-j plus %.
245 const char *q = p + 1;
250 while (*q && *q != ']' && *q != '%')
252 if (*q == 0 || *q == '%')
254 p = q + 1; // Consume the closing ']'.
255 dir->maybeGnuMalloc = true;
264 static int scanf_get_value_size(ScanfDirective *dir) {
266 if (!char_is_one_of(dir->convSpecifier, "cCsS["))
268 return sizeof(char *);
271 if (dir->maybeGnuMalloc) {
272 if (dir->convSpecifier != 'a' || dir->lengthModifier[0])
274 // This is ambiguous, so check the smaller size of char * (if it is
275 // a GNU extension of %as, %aS or %a[...]) and float (if it is
276 // POSIX %a followed by s, S or [ letters).
277 return sizeof(char *) < sizeof(float) ? sizeof(char *) : sizeof(float);
280 if (char_is_one_of(dir->convSpecifier, "cCsS[")) {
281 bool needsTerminator = char_is_one_of(dir->convSpecifier, "sS[");
283 format_get_char_size(dir->convSpecifier, dir->lengthModifier);
286 if (dir->fieldWidth == 0) {
287 if (!needsTerminator)
289 return (charSize == sizeof(char)) ? FSS_STRLEN : FSS_WCSLEN;
291 return (dir->fieldWidth + needsTerminator) * charSize;
294 return format_get_value_size(dir->convSpecifier, dir->lengthModifier, false);
297 // Common part of *scanf interceptors.
298 // Process format string and va_list, and report all store ranges.
299 // Stops when "consuming" n_inputs input items.
300 static void scanf_common(void *ctx, int n_inputs, bool allowGnuMalloc,
301 const char *format, va_list aq) {
302 CHECK_GT(n_inputs, 0);
303 const char *p = format;
305 COMMON_INTERCEPTOR_READ_RANGE(ctx, format, internal_strlen(format) + 1);
309 p = scanf_parse_next(p, allowGnuMalloc, &dir);
312 if (dir.convSpecifier == 0) {
313 // This can only happen at the end of the format string.
317 // Here the directive is valid. Do what it says.
318 if (dir.argIdx != -1) {
324 int size = scanf_get_value_size(&dir);
325 if (size == FSS_INVALID) {
326 Report("%s: WARNING: unexpected format specifier in scanf interceptor: ",
327 SanitizerToolName, "%.*s\n", dir.end - dir.begin, dir.begin);
330 void *argp = va_arg(aq, void *);
331 if (dir.convSpecifier != 'n')
335 if (size == FSS_STRLEN) {
336 size = internal_strlen((const char *)argp) + 1;
337 } else if (size == FSS_WCSLEN) {
338 // FIXME: actually use wcslen() to calculate it.
341 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, argp, size);
345 #if SANITIZER_INTERCEPT_PRINTF
347 struct PrintfDirective {
350 int argIdx; // width argument index, or -1 if not specified ("%*n$")
351 int precisionIdx; // precision argument index, or -1 if not specified (".*n$")
355 bool starredPrecision;
356 char lengthModifier[2];
360 static const char *maybe_parse_number(const char *p, int *out) {
361 if (*p >= '0' && *p <= '9')
362 p = parse_number(p, out);
366 static const char *maybe_parse_number_or_star(const char *p, int *out,
373 p = maybe_parse_number(p, out);
378 // Parse printf format string. Same as scanf_parse_next.
379 static const char *printf_parse_next(const char *p, PrintfDirective *dir) {
380 internal_memset(dir, 0, sizeof(*dir));
382 dir->precisionIdx = -1;
400 p = maybe_parse_param_index(p, &dir->precisionIdx);
403 while (char_is_one_of(*p, "'-+ #0")) {
407 p = maybe_parse_number_or_star(p, &dir->fieldWidth,
414 // Actual precision is optional (surprise!)
415 p = maybe_parse_number_or_star(p, &dir->fieldPrecision,
416 &dir->starredPrecision);
420 if (dir->starredPrecision) {
421 p = maybe_parse_param_index(p, &dir->precisionIdx);
426 p = maybe_parse_length_modifier(p, dir->lengthModifier);
427 // Conversion specifier.
428 dir->convSpecifier = *p++;
435 static int printf_get_value_size(PrintfDirective *dir) {
436 if (char_is_one_of(dir->convSpecifier, "cCsS")) {
438 format_get_char_size(dir->convSpecifier, dir->lengthModifier);
441 if (char_is_one_of(dir->convSpecifier, "sS")) {
442 return (charSize == sizeof(char)) ? FSS_STRLEN : FSS_WCSLEN;
447 return format_get_value_size(dir->convSpecifier, dir->lengthModifier, true);
450 #define SKIP_SCALAR_ARG(aq, convSpecifier, size) \
452 if (format_is_float_conv(convSpecifier)) { \
455 va_arg(*aq, double); \
458 va_arg(*aq, long double); \
461 va_arg(*aq, long double); \
464 Report("WARNING: unexpected floating-point arg size" \
465 " in printf interceptor: %d\n", size); \
479 Report("WARNING: unexpected arg size" \
480 " in printf interceptor: %d\n", size); \
486 // Common part of *printf interceptors.
487 // Process format string and va_list, and report all load ranges.
488 static void printf_common(void *ctx, const char *format, va_list aq) {
489 COMMON_INTERCEPTOR_READ_RANGE(ctx, format, internal_strlen(format) + 1);
491 const char *p = format;
495 p = printf_parse_next(p, &dir);
498 if (dir.convSpecifier == 0) {
499 // This can only happen at the end of the format string.
503 // Here the directive is valid. Do what it says.
504 if (dir.argIdx != -1 || dir.precisionIdx != -1) {
508 if (dir.starredWidth) {
510 SKIP_SCALAR_ARG(&aq, 'd', sizeof(int));
512 if (dir.starredPrecision) {
514 SKIP_SCALAR_ARG(&aq, 'd', sizeof(int));
516 // %m does not require an argument: strlen(errno).
517 if (dir.convSpecifier == 'm')
519 int size = printf_get_value_size(&dir);
520 if (size == FSS_INVALID) {
521 static int ReportedOnce;
524 "%s: WARNING: unexpected format specifier in printf "
525 "interceptor: %.*s (reported once per process)\n",
526 SanitizerToolName, dir.end - dir.begin, dir.begin);
529 if (dir.convSpecifier == 'n') {
530 void *argp = va_arg(aq, void *);
531 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, argp, size);
533 } else if (size == FSS_STRLEN) {
534 if (void *argp = va_arg(aq, void *)) {
535 if (dir.starredPrecision) {
536 // FIXME: properly support starred precision for strings.
538 } else if (dir.fieldPrecision > 0) {
539 // Won't read more than "precision" symbols.
540 size = internal_strnlen((const char *)argp, dir.fieldPrecision);
541 if (size < dir.fieldPrecision) size++;
543 // Whole string will be accessed.
544 size = internal_strlen((const char *)argp) + 1;
546 COMMON_INTERCEPTOR_READ_RANGE(ctx, argp, size);
548 } else if (size == FSS_WCSLEN) {
549 if (void *argp = va_arg(aq, void *)) {
550 // FIXME: Properly support wide-character strings (via wcsrtombs).
552 COMMON_INTERCEPTOR_READ_RANGE(ctx, argp, size);
555 // Skip non-pointer args
556 SKIP_SCALAR_ARG(&aq, dir.convSpecifier, size);
561 #endif // SANITIZER_INTERCEPT_PRINTF