Skip to content

Commit

Permalink
[libc] Template the printf / scanf parser class (#66277)
Browse files Browse the repository at this point in the history
Summary:
The parser class for stdio currently accepts different argument
providers. In-tree this is only used for a fuzzer test, however, the
proposed implementation of the GPU handling of printf / scanf will
require custom argument handlers. This makes the current approach of
using a preprocessor macro messier. This path proposed folding this
logic into a template instantiation. The downside to this is that
because the implementation of the parser class is placed into an
implementation file we need to manually instantiate the needed templates
which will slightly bloat binary size. Alternatively we could remove the
implementation file, or key off of the `libc` external packaging macro
so it is not present in the installed version.
  • Loading branch information
jhuber6 authored Sep 21, 2023
1 parent f66f354 commit e0be78b
Show file tree
Hide file tree
Showing 13 changed files with 637 additions and 757 deletions.
4 changes: 1 addition & 3 deletions libc/fuzzing/stdio/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@ add_libc_fuzzer(
SRCS
printf_parser_fuzz.cpp
DEPENDS
libc.src.stdio.printf_core.mock_parser
COMPILE_OPTIONS
-DLIBC_COPT_MOCK_ARG_LIST
libc.src.stdio.printf_core.parser
)

add_libc_fuzzer(
Expand Down
7 changes: 2 additions & 5 deletions libc/fuzzing/stdio/printf_parser_fuzz.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,6 @@
///
//===----------------------------------------------------------------------===//

#ifndef LIBC_COPT_MOCK_ARG_LIST
#error The printf Parser Fuzzer must be compiled with LIBC_COPT_MOCK_ARG_LIST, and the parser itself must also be compiled with that option when it's linked against the fuzzer.
#endif

#include "src/__support/arg_list.h"
#include "src/stdio/printf_core/parser.h"

Expand All @@ -37,7 +33,8 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {

auto mock_arg_list = internal::MockArgList();

auto parser = printf_core::Parser(in_str, mock_arg_list);
auto parser =
printf_core::Parser<internal::MockArgList>(in_str, mock_arg_list);

int str_percent_count = 0;

Expand Down
24 changes: 1 addition & 23 deletions libc/src/stdio/printf_core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,8 @@ add_header_library(
libc.src.__support.FPUtil.fp_bits
)

add_object_library(
add_header_library(
parser
SRCS
parser.cpp
HDRS
parser.h
DEPENDS
.core_structs
libc.src.__support.arg_list
libc.src.__support.ctype_utils
libc.src.__support.str_to_integer
libc.src.__support.CPP.bit
libc.src.__support.CPP.optional
libc.src.__support.CPP.string_view
libc.src.__support.CPP.type_traits
libc.src.__support.common
)

add_object_library(
mock_parser
SRCS
parser.cpp
HDRS
parser.h
DEPENDS
Expand All @@ -42,8 +22,6 @@ add_object_library(
libc.src.__support.CPP.string_view
libc.src.__support.CPP.type_traits
libc.src.__support.common
COMPILE_OPTIONS
-DLIBC_COPT_MOCK_ARG_LIST
)

add_object_library(
Expand Down
Loading

0 comments on commit e0be78b

Please sign in to comment.