1 //===-- printf_parser_fuzz.cpp --------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 /// Fuzzing test for llvm-libc qsort implementation.
11 //===----------------------------------------------------------------------===//
13 #include "src/__support/arg_list.h"
14 #include "src/stdio/printf_core/parser.h"
19 using namespace LIBC_NAMESPACE
;
21 // The design for the printf parser fuzzer is fairly simple. The parser uses a
22 // mock arg list that will never fail, and is passed a randomized string. The
23 // format sections it outputs are checked against a count of the number of '%'
24 // signs are in the original string. This is a fairly basic test, and the main
25 // intent is to run this under sanitizers, which will check for buffer overruns.
26 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data
, size_t size
) {
27 char *in_str
= new char[size
+ 1];
29 for (size_t i
= 0; i
< size
; ++i
)
34 auto mock_arg_list
= internal::MockArgList();
37 printf_core::Parser
<internal::MockArgList
>(in_str
, mock_arg_list
);
39 int str_percent_count
= 0;
41 for (size_t i
= 0; i
< size
&& in_str
[i
] != '\0'; ++i
) {
42 if (in_str
[i
] == '%') {
47 int section_percent_count
= 0;
49 for (printf_core::FormatSection cur_section
= parser
.get_next_section();
50 !cur_section
.raw_string
.empty();
51 cur_section
= parser
.get_next_section()) {
52 if (cur_section
.has_conv
) {
53 ++section_percent_count
;
54 if (cur_section
.conv_name
== '%') {
55 ++section_percent_count
;
57 } else if (cur_section
.raw_string
[0] == '%') {
58 // If the conversion would be undefined, it's instead raw, but it still
60 ++section_percent_count
;
64 if (str_percent_count
!= section_percent_count
) {