Skip to content

Commit 0cb4656

Browse files
committed
luzer: support command-line options
The patch brings support of LibFuzzer command-line options [1]. The options passed to `Fuzz()` function will be merged with command-line options, but command-line options have higher priority. ``` LUA_PATH="./luzer/?.lua;;" LUA_CPATH="./build/luzer/?.so;;" \ tarantool examples/example_basic.lua -runs=100 -max_len=1024 ``` 1. https://llvm.org/docs/LibFuzzer.html#options
1 parent 86b26c9 commit 0cb4656

File tree

7 files changed

+100
-16
lines changed

7 files changed

+100
-16
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1414
- Integration with libFuzzer's `FuzzedDataProvider`.
1515
- Examples with tests.
1616
- Documentation with usecases, API etc.
17+
- Support command-line options.
1718

1819
### Changed
1920

luzer/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ set(custom_mutator_lib_source ${CMAKE_CURRENT_SOURCE_DIR}/custom_mutator_lib.c)
4141
add_library(custom_mutator SHARED ${custom_mutator_lib_source})
4242
target_include_directories(custom_mutator PRIVATE ${LUA_INCLUDE_DIR})
4343
target_link_libraries(custom_mutator PRIVATE ${LUA_LIBRARIES})
44+
target_link_libraries(custom_mutator PRIVATE luzer_impl)
4445
set_target_properties(custom_mutator PROPERTIES VERSION ${PROJECT_VERSION})
4546
set_target_properties(custom_mutator PROPERTIES SOVERSION 1)
4647

luzer/init.lua

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,53 @@ if not ok then
33
error(("error on loading luzer_impl: %s"):format(luzer_impl))
44
end
55

6+
local function starts_with(str, prefix)
7+
return string.sub(str, 1, string.len(prefix)) == prefix
8+
end
9+
10+
-- The fuzzing test based on LibFuzzer optionally accept a number
11+
-- of flags and zero or more paths to corpus directories as
12+
-- command line arguments:
13+
-- ./fuzzer [-flag1=val1 [-flag2=val2 ...] ] [dir1 [dir2 ...] ]
14+
--
15+
-- 1. https://llvm.org/docs/LibFuzzer.html#options
16+
local function parse_flag(str)
17+
local flag_name, flag_val = string.match(str, "-([%l%p]+)=(%w+)")
18+
if not flag_name or
19+
not flag_val then
20+
error(("bad flag: %s"):format(str))
21+
end
22+
return flag_name, flag_val
23+
end
24+
25+
local function build_flags(arg, func_args)
26+
local flags = {}
27+
for _, arg_str in ipairs(arg) do
28+
local name, value
29+
if starts_with(arg_str, "-") then
30+
name, value = parse_flag(arg_str)
31+
else
32+
name, value = "corpus", arg_str
33+
end
34+
flags[name] = value
35+
end
36+
37+
for flag_name, flag_val in pairs(func_args) do
38+
if not flags[flag_name] then
39+
flags[flag_name] = flag_val
40+
end
41+
end
42+
43+
return flags
44+
end
45+
46+
local function Fuzz(test_one_input, custom_mutator, func_args)
47+
local flags = build_flags(arg, func_args)
48+
luzer_impl.Fuzz(test_one_input, custom_mutator, flags)
49+
end
50+
651
return {
7-
Fuzz = luzer_impl.Fuzz,
52+
Fuzz = Fuzz,
853
FuzzedDataProvider = luzer_impl.FuzzedDataProvider,
954

1055
_LLVM_VERSION = luzer_impl._LLVM_VERSION,

luzer/tests/CMakeLists.txt

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,47 @@ set_tests_properties(luzer_e2e_test PROPERTIES
3131
)
3232

3333
add_test(
34-
NAME luzer_options_test
35-
COMMAND ${LUA_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/test_options.lua
36-
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
34+
NAME luzer_options_corpus_path_via_table_test
35+
COMMAND ${LUA_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/test_options_1.lua
36+
-max_total_time=1
3737
)
38-
set_tests_properties(luzer_options_test PROPERTIES
38+
set_tests_properties(luzer_options_corpus_path_via_table_test PROPERTIES
3939
ENVIRONMENT "LUA_CPATH=${LUA_CPATH};LUA_PATH='${LUA_PATH}'"
40-
PASS_REGULAR_EXPRESSION "ERROR: The required directory \"undefined\" does not exist"
40+
PASS_REGULAR_EXPRESSION
41+
"ERROR: The required directory \"undefined\" does not exist"
42+
)
43+
44+
file(MAKE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/seed_corpus)
45+
add_test(
46+
NAME luzer_options_corpus_path_via_option_test
47+
COMMAND ${LUA_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/test_options_1.lua
48+
-runs=1 seed_corpus
49+
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
50+
)
51+
set_tests_properties(luzer_options_corpus_path_via_option_test PROPERTIES
52+
ENVIRONMENT "LUA_CPATH=${LUA_CPATH};LUA_PATH=${LUA_PATH}"
53+
PASS_REGULAR_EXPRESSION "0 files found in seed_corpus"
54+
)
55+
56+
add_test(
57+
NAME luzer_options_seed_via_option_test
58+
COMMAND ${LUA_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/test_options_2.lua
59+
-runs=1 -seed=50
60+
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
61+
)
62+
set_tests_properties(luzer_options_seed_via_option_test PROPERTIES
63+
ENVIRONMENT "LUA_CPATH=${LUA_CPATH};LUA_PATH=${LUA_PATH}"
64+
PASS_REGULAR_EXPRESSION "Seed: 50"
65+
)
66+
67+
add_test(
68+
NAME luzer_options_help_test
69+
COMMAND ${LUA_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/test_options_2.lua
70+
-help=1
71+
)
72+
set_tests_properties(luzer_options_help_test PROPERTIES
73+
ENVIRONMENT "LUA_CPATH=${LUA_CPATH};LUA_PATH=${LUA_PATH}"
74+
PASS_REGULAR_EXPRESSION "Usage:"
4175
)
4276

4377
add_test(

luzer/tests/test_options.lua

Lines changed: 0 additions & 10 deletions
This file was deleted.

luzer/tests/test_options_1.lua

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
local luzer = require("luzer")
2+
3+
local args = {
4+
corpus = "undefined",
5+
max_total_time = 1,
6+
}
7+
luzer.Fuzz(function() os.execute('sleep 0.1') end, nil, args)

luzer/tests/test_options_2.lua

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
local luzer = require("luzer")
2+
3+
local args = {
4+
seed = 12345,
5+
}
6+
luzer.Fuzz(function() end, nil, args)

0 commit comments

Comments
 (0)