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("WARNING: unexpected format specifier in scanf interceptor: "
327 "%.*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 (dir->convSpecifier == 'm') {
437 return sizeof(char *);
440 if (char_is_one_of(dir->convSpecifier, "cCsS")) {
442 format_get_char_size(dir->convSpecifier, dir->lengthModifier);
445 if (char_is_one_of(dir->convSpecifier, "sS")) {
446 return (charSize == sizeof(char)) ? FSS_STRLEN : FSS_WCSLEN;
451 return format_get_value_size(dir->convSpecifier, dir->lengthModifier, true);
454 #define SKIP_SCALAR_ARG(aq, convSpecifier, size) \
456 if (format_is_float_conv(convSpecifier)) { \
459 va_arg(*aq, double); \
462 va_arg(*aq, long double); \
465 va_arg(*aq, long double); \
468 Report("WARNING: unexpected floating-point arg size" \
469 " in printf interceptor: %d\n", size); \
483 Report("WARNING: unexpected arg size" \
484 " in printf interceptor: %d\n", size); \
490 // Common part of *printf interceptors.
491 // Process format string and va_list, and report all load ranges.
492 static void printf_common(void *ctx, const char *format, va_list aq) {
493 COMMON_INTERCEPTOR_READ_RANGE(ctx, format, internal_strlen(format) + 1);
495 const char *p = format;
499 p = printf_parse_next(p, &dir);
502 if (dir.convSpecifier == 0) {
503 // This can only happen at the end of the format string.
507 // Here the directive is valid. Do what it says.
508 if (dir.argIdx != -1 || dir.precisionIdx != -1) {
512 if (dir.starredWidth) {
514 SKIP_SCALAR_ARG(&aq, 'd', sizeof(int));
516 if (dir.starredPrecision) {
518 SKIP_SCALAR_ARG(&aq, 'd', sizeof(int));
520 int size = printf_get_value_size(&dir);
521 if (size == FSS_INVALID) {
522 Report("WARNING: unexpected format specifier in printf "
523 "interceptor: %.*s\n", dir.end - dir.begin, dir.begin);
526 if (dir.convSpecifier == 'n') {
527 void *argp = va_arg(aq, void *);
528 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, argp, size);
530 } else if (size == FSS_STRLEN) {
531 if (void *argp = va_arg(aq, void *)) {
532 if (dir.starredPrecision) {
533 // FIXME: properly support starred precision for strings.
535 } else if (dir.fieldPrecision > 0) {
536 // Won't read more than "precision" symbols.
537 size = internal_strnlen((const char *)argp, dir.fieldPrecision);
538 if (size < dir.fieldPrecision) size++;
540 // Whole string will be accessed.
541 size = internal_strlen((const char *)argp) + 1;
543 COMMON_INTERCEPTOR_READ_RANGE(ctx, argp, size);
545 } else if (size == FSS_WCSLEN) {
546 if (void *argp = va_arg(aq, void *)) {
547 // FIXME: Properly support wide-character strings (via wcsrtombs).
549 COMMON_INTERCEPTOR_READ_RANGE(ctx, argp, size);
552 // Skip non-pointer args
553 SKIP_SCALAR_ARG(&aq, dir.convSpecifier, size);
558 #endif // SANITIZER_INTERCEPT_PRINTF