Skip to content

Commit b5a006c

Browse files
add parameters for select_parser_fuzzer, set default threshoulds lower
1 parent d23cf75 commit b5a006c

1 file changed

Lines changed: 110 additions & 5 deletions

File tree

‎src/Parsers/fuzzers/select_parser_fuzzer.cpp‎

Lines changed: 110 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,116 @@
44
#include <Parsers/ParserQueryWithOutput.h>
55
#include <Parsers/parseQuery.h>
66

7+
/// This fuzzer supports its own arguments:
8+
/// - `-max_parser_depth=N` sets the maximum parser depth (default: 150 for debug/sanitizer, 300 otherwise)
9+
/// - `-max_parser_backtracks=N` sets the maximum parser backtracks (default: DBMS_DEFAULT_MAX_PARSER_BACKTRACKS)
10+
/// - `-max_ast_depth=N` sets the maximum depth of the AST tree (default: 1000)
11+
/// - `-max_ast_elements=N` sets the maximum number of elements in AST tree (default: 50000)
12+
/// These arguments must be passed after `-ignore_remaining_args=1` to avoid interference with libFuzzer options.
13+
using namespace DB;
14+
15+
16+
#if defined(SANITIZER) || !defined(NDEBUG)
17+
size_t max_parser_depth = 150;
18+
#else
19+
size_t max_parser_depth = 300;
20+
#endif
21+
size_t max_parser_backtracks = DBMS_DEFAULT_MAX_PARSER_BACKTRACKS;
22+
size_t max_ast_depth = 1000;
23+
size_t max_ast_elements = 50000;
24+
25+
26+
// Helper function to check if this is a merge run
27+
bool isMerge(int argc, char ** argv)
28+
{
29+
for (int i = 1; i < argc; ++i)
30+
{
31+
std::string_view arg{argv[i]};
32+
if (std::string_view{arg.begin(), std::ranges::find(arg, '=')} == "-ignore_remaining_args")
33+
break;
34+
if (std::string_view{arg.begin(), std::ranges::find(arg, '=')} == "-merge")
35+
return true;
36+
}
37+
return false;
38+
}
39+
40+
// Helper function to parse settings from command line arguments
41+
std::unordered_map<std::string, std::string> parseSettingsFromArgs(int argc, char ** argv) // STYLE_CHECK_ALLOW_STD_CONTAINERS
42+
{
43+
std::unordered_map<std::string, std::string> settings; // STYLE_CHECK_ALLOW_STD_CONTAINERS
44+
bool ignore_remaining = false;
45+
46+
for (int i = 1; i < argc; ++i)
47+
{
48+
std::string arg{argv[i]};
49+
50+
if (!ignore_remaining)
51+
{
52+
// Check for -ignore_remaining_args
53+
if (arg.starts_with("-ignore_remaining_args"))
54+
{
55+
ignore_remaining = true;
56+
continue;
57+
}
58+
}
59+
else
60+
{
61+
// Parse settings after -ignore_remaining_args
62+
size_t eq_pos = arg.find('=');
63+
if (eq_pos != std::string::npos)
64+
{
65+
// Skip leading dashes to get the setting name
66+
size_t key_start = 0;
67+
while (key_start < arg.length() && arg[key_start] == '-')
68+
++key_start;
69+
70+
std::string key = arg.substr(key_start, eq_pos - key_start);
71+
std::string value = arg.substr(eq_pos + 1);
72+
settings[key] = value;
73+
}
74+
}
75+
}
76+
77+
return settings;
78+
}
79+
80+
extern "C" int LLVMFuzzerInitialize(const int *argc, char ***argv)
81+
{
82+
// Check if this is a merge run and skip initialization if so
83+
if (isMerge(*argc, *argv))
84+
return 0;
85+
86+
auto settings = parseSettingsFromArgs(*argc, *argv);
87+
auto parse_setting = [&settings](const std::string & key, size_t & out)
88+
{
89+
if (auto it = settings.find(key); it != settings.end())
90+
{
91+
try
92+
{
93+
size_t pos = 0;
94+
uint64_t val = std::stoul(it->second, &pos);
95+
if (pos != it->second.size())
96+
{
97+
std::cerr << "Invalid value for " << key << ": " << it->second << std::endl;
98+
exit(1);
99+
}
100+
out = static_cast<size_t>(val);
101+
}
102+
catch (const std::exception & e)
103+
{
104+
std::cerr << "Invalid value for " << key << ": " << it->second << " (" << e.what() << ")" << std::endl;
105+
exit(1);
106+
}
107+
}
108+
};
109+
110+
parse_setting("max_parser_depth", max_parser_depth);
111+
parse_setting("max_parser_backtracks", max_parser_backtracks);
112+
parse_setting("max_ast_depth", max_ast_depth);
113+
parse_setting("max_ast_elements", max_ast_elements);
114+
115+
return 0;
116+
}
7117

8118
extern "C" int LLVMFuzzerTestOneInput(const uint8_t * data, size_t size)
9119
{
@@ -13,14 +123,9 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t * data, size_t size)
13123

14124
DB::ParserQueryWithOutput parser(input.data() + input.size());
15125

16-
const UInt64 max_parser_depth = 500;
17-
const UInt64 max_parser_backtracks = 5000;
18126
DB::ASTPtr ast = parseQuery(parser, input.data(), input.data() + input.size(), "", 0, max_parser_depth, max_parser_backtracks);
19127

20-
const UInt64 max_ast_depth = 1000;
21128
ast->checkDepth(max_ast_depth);
22-
23-
const UInt64 max_ast_elements = 50000;
24129
ast->checkSize(max_ast_elements);
25130

26131
std::cerr << ast->formatWithSecretsOneLine() << std::endl;

0 commit comments

Comments
 (0)