Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fuzzer Framework #5940

Merged
merged 168 commits into from
Feb 2, 2023
Merged
Show file tree
Hide file tree
Changes from 52 commits
Commits
Show all changes
168 commits
Select commit Hold shift + click to select a range
0fa075e
first commit fuzzer
nicola-cab Oct 12, 2022
ddd2c1a
fix script
nicola-cab Oct 12, 2022
19027a1
restore parallel fuzzer
nicola-cab Oct 13, 2022
85c999f
code refactory
nicola-cab Oct 13, 2022
1fe1da0
code re-org
nicola-cab Oct 14, 2022
70d7bf4
support input file from cmd line
nicola-cab Oct 14, 2022
7cf6411
comment crashing commands in order to verify them
nicola-cab Oct 14, 2022
73a28f0
Merge branch 'master' of github.com:realm/realm-core into nc/fuzzer
nicola-cab Oct 17, 2022
d111728
code cleanup and logging
nicola-cab Oct 17, 2022
0b16041
added fuzz logger
nicola-cab Oct 18, 2022
d8cc443
added new constructor for libfuzz
nicola-cab Oct 19, 2022
30cb356
ctro for libfuzz
nicola-cab Oct 19, 2022
51b9b3f
add support for libfuzzer
nicola-cab Oct 19, 2022
bbd3cfe
appease format checks
nicola-cab Oct 19, 2022
89c3381
lib fuzzer support and defines
nicola-cab Oct 19, 2022
15a61cf
added support for libfuzzer
nicola-cab Oct 20, 2022
4c569da
Merge branch 'master' of github.com:realm/realm-core into nc/fuzzer
nicola-cab Oct 21, 2022
a574d88
unify how to launch fuzzer
nicola-cab Oct 21, 2022
a5a3e31
fix forgotten file
nicola-cab Oct 21, 2022
22379e1
adding open/close to commands
nicola-cab Oct 21, 2022
dfa6646
Merge branch 'master' of github.com:realm/realm-core into nc/fuzzer
nicola-cab Oct 24, 2022
c978ad2
Fix logging
nicola-cab Oct 24, 2022
a65267e
catch some exceptions during fuzzing
nicola-cab Oct 24, 2022
6763a8a
exception unification catching logic
nicola-cab Oct 24, 2022
a5e49db
Make realm-libfuzz compile on Linux
jedelbo Oct 25, 2022
4ad5408
Merge branch 'je/fuzzer' into nc/fuzzer
nicola-cab Oct 25, 2022
dcf2267
Merge branch 'master' of github.com:realm/realm-core into nc/fuzzer
nicola-cab Oct 25, 2022
9e50175
restore encryption
nicola-cab Oct 25, 2022
48f79c6
fix parent node verification function
nicola-cab Oct 26, 2022
7c6a2b8
fix string cmp
nicola-cab Oct 26, 2022
d6156eb
Merge branch 'master' of github.com:realm/realm-core into nc/fuzzer
nicola-cab Nov 29, 2022
98a7e77
Merge branch 'master' of github.com:realm/realm-core into nc/fuzzer
nicola-cab Dec 13, 2022
aec4c77
Attempt to fix compilation issues for non apple builds
nicola-cab Dec 13, 2022
91e4c2d
attempt to fix compilation
nicola-cab Dec 14, 2022
585e0ea
fix compilation issues
nicola-cab Dec 14, 2022
6fbf9ca
attempt to fix compilation issue
nicola-cab Dec 15, 2022
3a8af67
attempt to fix compilation
nicola-cab Dec 15, 2022
8535fe3
clean up
nicola-cab Dec 15, 2022
97f65b4
cleanup
nicola-cab Dec 15, 2022
3ef6e22
cleanup
nicola-cab Dec 15, 2022
1753b20
attempt to fix compilation
nicola-cab Dec 15, 2022
1efb701
test
nicola-cab Dec 15, 2022
a29f4c3
revert specialityBuilds cmake changes
nicola-cab Dec 15, 2022
b1dea82
Merge branch 'master' of github.com:realm/realm-core into nc/fuzzer
nicola-cab Dec 15, 2022
8082cb2
Add Readme
nicola-cab Dec 15, 2022
8b0f4ed
changelog entry
nicola-cab Dec 15, 2022
3b26962
removed stale testing folder
nicola-cab Dec 15, 2022
b097258
better formatting
nicola-cab Dec 15, 2022
66d6791
better formatting
nicola-cab Dec 15, 2022
bdf1517
restore support for afl++ and fix clang libfuzzer command line
nicola-cab Dec 15, 2022
6de6c68
make linux builders happy
nicola-cab Dec 15, 2022
56b41c3
really testing things
nicola-cab Dec 15, 2022
2caa486
add cstring header
nicola-cab Dec 16, 2022
b468cbb
test building
nicola-cab Dec 16, 2022
f86bf68
fix libuv finding packages
nicola-cab Dec 16, 2022
e8078d8
fix libuv finding cmake
nicola-cab Dec 16, 2022
b0300c2
Update CMakeLists.txt
fealebenpae Dec 16, 2022
1be6f6b
Update CMakeLists.txt
fealebenpae Dec 16, 2022
3e3f757
Update CMakeLists.txt
fealebenpae Dec 16, 2022
d7034e3
pull master and fix conflicts
nicola-cab Dec 19, 2022
908cc1b
fix changelog and readme
nicola-cab Dec 19, 2022
26b1cfb
add evergreen running logic for fuzzer
nicola-cab Dec 19, 2022
58d86fa
attempt to fix evergreen cnf
nicola-cab Dec 19, 2022
dfc76b1
fuzzer cnf
nicola-cab Dec 19, 2022
dfaa03d
attempt to fix evergreen cnf
nicola-cab Dec 19, 2022
88c2405
run fuzzer for macos builder
nicola-cab Dec 19, 2022
f4855a6
test
nicola-cab Dec 19, 2022
cc75925
test
nicola-cab Dec 19, 2022
4a7ccc4
test
nicola-cab Dec 19, 2022
7848dbe
test enable macos-fuzzer
nicola-cab Dec 19, 2022
92c6de2
add fuzzer to evergreen
nicola-cab Dec 19, 2022
45c2d25
removed redundant cmake build mode
nicola-cab Dec 19, 2022
9c65f6b
compile and fuzzer for macos fuzzing build
nicola-cab Dec 19, 2022
417792b
test
nicola-cab Dec 19, 2022
7836c8c
test
nicola-cab Dec 19, 2022
76b3690
test on ubuntu
nicola-cab Dec 19, 2022
c944d82
test
nicola-cab Dec 20, 2022
e6a99e3
test fuzzer
nicola-cab Dec 20, 2022
e7d6c2b
pull master and fix conflicts
nicola-cab Dec 20, 2022
e04aa57
compile and test fuzzer
nicola-cab Dec 20, 2022
4232bca
compile test
nicola-cab Dec 20, 2022
d900dd0
test fuzzer as task
nicola-cab Dec 20, 2022
4558c36
test macos fuzzer
nicola-cab Dec 20, 2022
0c1a355
build fuzzer
nicola-cab Dec 20, 2022
bb8727b
last test before to try things locally
nicola-cab Dec 20, 2022
4d8ad3c
fix param
nicola-cab Dec 20, 2022
e47ccb0
changes to cnf file
nicola-cab Dec 20, 2022
c3cbf06
fix spaces
nicola-cab Dec 20, 2022
1a155ab
pull master and fix conflicts
nicola-cab Dec 21, 2022
5868931
fix after evergreen validate check
nicola-cab Dec 21, 2022
216f42f
re-org a bit fuzzer builder
nicola-cab Dec 21, 2022
695e33d
remove quotes
nicola-cab Dec 21, 2022
ed60633
test
nicola-cab Dec 21, 2022
6f73ae2
remove fuzzer from task-group
nicola-cab Dec 21, 2022
fd9fd42
add fuzzer build
nicola-cab Dec 21, 2022
ee241ae
test compilation and if compile-rt works
nicola-cab Dec 21, 2022
8ca4cdf
fix generator
nicola-cab Dec 21, 2022
0053aa4
xcode generator
nicola-cab Dec 21, 2022
93863ec
test if fixes linking issues
nicola-cab Dec 21, 2022
9832d74
test
nicola-cab Dec 21, 2022
810729c
test
nicola-cab Dec 21, 2022
0dcc548
test building and linking
nicola-cab Dec 21, 2022
71deb5b
fix cnf
nicola-cab Dec 21, 2022
e57c557
test xcode 14.1
nicola-cab Dec 21, 2022
003a536
test
nicola-cab Dec 21, 2022
96d6c03
pull master
nicola-cab Jan 10, 2023
5e47421
test fuzzer
nicola-cab Jan 10, 2023
a6bd9ee
unify include logic
nicola-cab Jan 10, 2023
a773bde
add linking instruction for address sanitizer
nicola-cab Jan 10, 2023
4c42fe6
add linker flags to correct builder
nicola-cab Jan 10, 2023
65d28d5
link fuzzer
nicola-cab Jan 10, 2023
8a52a89
test only fuzzer
nicola-cab Jan 10, 2023
1a6a44b
Merge branch 'master' of github.com:realm/realm-core into nc/fuzzer
nicola-cab Jan 19, 2023
dfd16ae
test running the fuzzer on ubuntu
nicola-cab Jan 19, 2023
3561b15
no extra flags
nicola-cab Jan 19, 2023
4f66ee2
add toolchain file for LLVM via homebrew
fealebenpae Jan 19, 2023
4bc951a
test pre keyword
nicola-cab Jan 19, 2023
40d7218
Merge branch 'nc/fuzzer' of github.com:realm/realm-core into nc/fuzzer
nicola-cab Jan 19, 2023
e090c4a
fix file format
nicola-cab Jan 19, 2023
106ba7c
test pre condition
nicola-cab Jan 19, 2023
0b37563
Revert "add toolchain file for LLVM via homebrew"
nicola-cab Jan 19, 2023
6e943ce
try daily setup
nicola-cab Jan 19, 2023
690ec81
try daily setup
nicola-cab Jan 19, 2023
26e6232
restructure a little bit evergreen file
nicola-cab Jan 20, 2023
fe367ee
Merge branch 'master' of github.com:realm/realm-core into nc/fuzzer
nicola-cab Jan 20, 2023
bc3cf97
added support for upload crash input strings
nicola-cab Jan 20, 2023
0fdd865
added support for publishing to s3 fuzzer crash dumps
nicola-cab Jan 20, 2023
4f2c57b
test crash
nicola-cab Jan 23, 2023
db8cbb0
fix typo
nicola-cab Jan 23, 2023
2f6cdde
test
nicola-cab Jan 23, 2023
200573a
fix running fuzzer exe
nicola-cab Jan 23, 2023
1f03f05
several fixes
nicola-cab Jan 23, 2023
d87484f
better folder naming and fix path
nicola-cab Jan 23, 2023
7461d5a
delete prev folder
nicola-cab Jan 23, 2023
772d825
appease clang format checks
nicola-cab Jan 23, 2023
33b1fbe
test exe path
nicola-cab Jan 23, 2023
6c7ea4f
test launcher
nicola-cab Jan 23, 2023
990acf1
fix path
nicola-cab Jan 23, 2023
cc37f08
fix path in order to generate crash report
nicola-cab Jan 24, 2023
49b978d
test path
nicola-cab Jan 24, 2023
937a36b
remove archive file after upload
nicola-cab Jan 24, 2023
54b3943
test cpack find
nicola-cab Jan 24, 2023
52f1369
remove cpack machinery and test
nicola-cab Jan 24, 2023
d0115c4
test
nicola-cab Jan 24, 2023
34a75a4
revert cnf to prev working
nicola-cab Jan 24, 2023
6d2fae3
debug
nicola-cab Jan 24, 2023
af0ed29
fix crash file detection
nicola-cab Jan 24, 2023
165ba21
run the fuzzer daily
nicola-cab Jan 24, 2023
bdf8e91
remove fake exception + remove support for async tr
nicola-cab Jan 24, 2023
e6223aa
Update test/realm-fuzzer/afl_runner.cpp
nicola-cab Jan 26, 2023
847c8c1
Merge branch 'master' of github.com:realm/realm-core into nc/fuzzer
nicola-cab Jan 26, 2023
e67002e
code review
nicola-cab Jan 26, 2023
a7f9cad
better build folder names
nicola-cab Jan 26, 2023
14bedb6
code review
nicola-cab Jan 26, 2023
a43147a
fix changelog
nicola-cab Jan 26, 2023
5d071e1
pull master and fix conflicts
nicola-cab Jan 27, 2023
d702196
Merge branch 'master' of github.com:realm/realm-core into nc/fuzzer
nicola-cab Jan 27, 2023
27d7d1e
update scripts
nicola-cab Jan 27, 2023
2f05d63
merge master and fix conflicts
nicola-cab Jan 30, 2023
1406fe1
update entry in the changelog
nicola-cab Jan 30, 2023
714677b
Fix compile warnings
jedelbo Jan 31, 2023
d1d292a
fix conflicts with master
nicola-cab Jan 31, 2023
f37edc1
delete .gitignore
nicola-cab Jan 31, 2023
0f86d13
code review
nicola-cab Jan 31, 2023
89f1fa7
test compilation
nicola-cab Feb 1, 2023
86adb58
test
nicola-cab Feb 1, 2023
ce14bb4
restore configuration
nicola-cab Feb 1, 2023
117ea1c
fix configuration file
nicola-cab Feb 2, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
### Internals
* Updates for upcoming Platform Networking feature, including new SyncSocketProvider class. ([PR #6096](https://github.com/realm/realm-core/pull/6096))
* Updated namespaces for files moved to realm/sync/network ([PR #6109](https://github.com/realm/realm-core/pull/6109))
* Create the fuzzer framework project in order to run fuzz testing on evergreen.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There does not appear to be anything running on evergreen?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@fealebenpae helped me to fix the compilation issues, going to add the logic to the evergreen configuration file in order to run it.


----------------------------------------------

Expand Down
1 change: 1 addition & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ add_subdirectory(object-store)
# AFL not yet supported by Windows
if(NOT CMAKE_SYSTEM_NAME MATCHES "^Windows")
add_subdirectory(fuzzy)
add_subdirectory(fuzzy_object_store)
endif()

add_subdirectory(benchmark-common-tasks)
Expand Down
5 changes: 5 additions & 0 deletions test/fuzzy_object_store/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
findings/*
fuzzy-tests*
generate-fuzzy-input*
*.d
*.o
81 changes: 81 additions & 0 deletions test/fuzzy_object_store/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
set(TEST_AFL_SOURCES
afl_runner.cpp
fuzz_engine.cpp
fuzz_object.cpp
fuzz_configurator.cpp
) # TEST_AFL_SOURCES_OBJECT_STORE

set(TEST_LIBFUZZER_SOURCES
libfuzzer_runner.cpp
fuzz_engine.cpp
fuzz_object.cpp
fuzz_configurator.cpp
) # TEST_LIBFUZZER_SOURCES_OBJECT_STORE

file(GLOB FUZZER_RUN_SCRIPTS
"scripts/start_fuzz_afl.sh"
"scripts/start_lib_fuzzer.sh")

file(COPY ${FUZZER_RUN_SCRIPTS}
DESTINATION ${CMAKE_CURRENT_BINARY_DIR})
file(GLOB AFL_SEEDS "testcases/*")
file(MAKE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/testcases)
file(COPY ${AFL_SEEDS}
DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/testcases)

add_executable(realm-afl++ ${TEST_AFL_SOURCES})
target_link_libraries(realm-afl++ TestUtil ObjectStore)

if(REALM_LIBFUZZER)
if(${CMAKE_CXX_COMPILER_ID} MATCHES "Clang")
add_executable(realm-libfuzz ${TEST_LIBFUZZER_SOURCES})
target_link_libraries(realm-libfuzz TestUtil ObjectStore)
endif()
endif()

# on Apple platforms we use the built-in CFRunLoop
# everywhere else it's libuv, except UWP where it doesn't build
if(NOT APPLE AND NOT WINDOWS_STORE)
if(REALM_FETCH_MISSING_DEPENDENCIES)
find_package(LibUV)
else()
find_package(LibUV REQUIRED)
endif()

if(LibUV_FOUND)
set(libuv_target LibUV::LibUV)
elseif(REALM_FETCH_MISSING_DEPENDENCIES)
message(STATUS "LibUV not found, building from source with FetchContent")
include(FetchContent)
set(libUV_Git_TAG "v1.35.0")

if(MSVC)
set(liUV_Git_TAG "v1.43.0")
endif()

FetchContent_Declare(
libuv
GIT_REPOSITORY https://github.com/libuv/libuv.git
GIT_TAG ${libUV_Git_TAG}
)

# Don't use FetchContent_MakeAvailable since it wants to build libuv.so as well
FetchContent_Populate(libuv)
add_subdirectory(${libuv_SOURCE_DIR} ${libuv_BINARY_DIR} EXCLUDE_FROM_ALL)
set(libuv_target uv_a)
endif()

target_link_libraries(realm-afl++ ${libuv_target})
target_compile_definitions(realm-afl++ PUBLIC REALM_HAVE_UV=1)
get_property(libuv_include_dir TARGET ${libuv_target} PROPERTY INCLUDE_DIRECTORIES)
target_include_directories(realm-afl++ PRIVATE ${libuv_include_dir})

if(REALM_LIBFUZZER)
if(${CMAKE_CXX_COMPILER_ID} MATCHES "Clang")
target_link_libraries(realm-libfuzz ${libuv_target})
target_compile_definitions(realm-libfuzz PUBLIC REALM_HAVE_UV=1)
get_property(libuv_include_dir TARGET ${libuv_target} PROPERTY INCLUDE_DIRECTORIES)
target_include_directories(realm-libfuzz PRIVATE ${libuv_include_dir})
endif()
endif()
endif()
77 changes: 77 additions & 0 deletions test/fuzzy_object_store/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
# The Fuzz Framework project

This project is an attempt to put together all the small fuzzers we have already scattered around the code.
There are two goals:
1. To be able to run all the fuzzers, collect crashes reports and fix possible bugs that the fuzzer might find.
2. To be able to replace libfuzzer with google fuzz test (https://github.com/google/fuzztest) at some point.

AFL++ support is not dropped yet, but since we want to integrate things inside evergreen and follow the same approach we implement for address/thread sanitazer we prefer to use libfuzzer and clang.
## Prerequisites

In case you want to use AFL++, then you should install the latest version of the American Fuzzy Lop ++ (AFL++).
Please use this quick guide: https://aflplus.plus/building/ it requires llvm >= 9.0.

For using libfuzzer, the only pre-requisite is having a recent version of clang.
## Running
Note REALM_MAX_BPNODE_SIZE is the max number of nodes contained in the b+tree. It determines the depth of the tree and its fanout. \
This number should be random generated.
bmunkholm marked this conversation as resolved.
Show resolved Hide resolved

If you don't want to build manually, you can skip this section and jump to the `Scripts` section. \
Run the fuzzer via AFL++:

```
cd <realm-core-src>
mkdir build
cd build
cmake -D CMAKE_BUILD_TYPE=${build_mode}
-D CMAKE_C_COMPILER=afl-cc
-D CMAKE_CXX_COMPILER=afl-c++
-D REALM_MAX_BPNODE_SIZE="${REALM_MAX_BPNODE_SIZE}"
-D REALM_ENABLE_ENCRYPTION=ON
-G Ninja
..
cmake --build . --target realm-afl++
afl-fuzz -t "$time_out"
-m "$memory"
-i "${ROOT_DIR}/test/fuzzy_object_store/testcases"
-o "${FINDINGS_DIR}"
realm-afl++ @@
```

Run the fuzzer via libFuzzer (only with Clang)
```
cd <realm-core-src>
mkdir build
cd build
cmake -D REALM_LIBFUZZER=ON
-D CMAKE_BUILD_TYPE=${build_mode}
-D CMAKE_C_COMPILER=clang
-D CMAKE_CXX_COMPILER=clang++
-D REALM_MAX_BPNODE_SIZE="${REALM_MAX_BPNODE_SIZE}"
-D REALM_ENABLE_ENCRYPTION=ON
-G Ninja
..
cmake --build . --target realm-libfuzz
./realm_libfuzz <corpus>
```

## Scripts

`sh start_fuzz_afl.sh`
Builds `realm-core` and `object-store` in `Debug` mode using the afl++ compiler `afl-cc` and starts 1 instance of `afl-fuzz`.
It expects `AFLPlusPlus` to be installed in your system and in general added to your `PATH`.
Optionally, the following arguments can be passed to the script:
1) `<num_fuzzers>` the number of fuzzers to launch (by default 1).
2) `<build_mode>` either `Release` or `Debug`.

`sh start_lib_fuzzer.sh`
Builds `realm-core` and `object-store` in `Debug` mode using the clang compiler and starts `realm-libfuzz`.
Optionally, the following arguments can be passed to the script:
1) `<build_mode>` either `Release` or `Debug`.
2) `<corpus>` essentially initial set of inputs for improving fuzzer efficiency.

## See Also

[AFL++ github](https://github.com/AFLplusplus/AFLplusplus) \
[LibFuzzer](https://github.com/google/fuzzing/blob/master/tutorial/libFuzzerTutorial.md) \
[Google Fuzz Test](https://github.com/google/fuzztest)
43 changes: 43 additions & 0 deletions test/fuzzy_object_store/afl_runner.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*************************************************************************
*
* Copyright 2022 Realm Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
**************************************************************************/

#include "fuzz_engine.hpp"
#include <vector>
#include <string>
#include <iostream>

#if REALM_USE_UV
#include <uv.h>
#endif

int main(int argc, const char* argv[])
{
FuzzEngine fuzz_engine;
bool enable_logging = false;
std::string path = "real-afl.txt";
size_t input_index = 0;
for (size_t i = 0; i < (size_t)argc; ++i) {
if (strcmp(argv[i], "--log") == 0) {
enable_logging = true;
}
else {
input_index = i;
}
}
return fuzz_engine.run_fuzzer(argv[input_index], "realm_afl", enable_logging, path);
}
109 changes: 109 additions & 0 deletions test/fuzzy_object_store/fuzz_configurator.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
/*************************************************************************
*
* Copyright 2022 Realm Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
**************************************************************************/
#include "fuzz_configurator.hpp"
#include "fuzz_object.hpp"
#include "../util/test_path.hpp"
#include <iostream>

FuzzConfigurator::FuzzConfigurator(FuzzObject& fuzzer, const std::string& input, bool use_input_file,
const std::string& name)
: m_used_input_file(use_input_file)
, m_fuzzer(fuzzer)
, m_fuzz_name(name)
{
realm::disable_sync_to_disk();
init(input);
setup_realm_config();
}

void FuzzConfigurator::setup_realm_config()
{
m_config.path = m_path;
m_config.schema_version = 0;
if (m_use_encryption) {
const char* key = m_fuzzer.get_encryption_key();
const char* i = key;
while (*i != '\0') {
m_config.encryption_key.push_back(*i);
i++;
}
}
}

const realm::Realm::Config& FuzzConfigurator::get_config() const
{
return m_config;
}

FuzzObject& FuzzConfigurator::get_fuzzer()
{
return m_fuzzer;
}

const std::string& FuzzConfigurator::get_realm_path() const
{
return m_path;
}

FuzzLog& FuzzConfigurator::get_logger()
{
return m_log;
}

State& FuzzConfigurator::get_state()
{
return m_state;
}

void FuzzConfigurator::init(const std::string& input)
{
std::string db_name = "fuzz-test";
realm::test_util::RealmPathInfo test_context{db_name};
SHARED_GROUP_TEST_PATH(path);
m_path = path.c_str();
if (m_used_input_file) {
std::ifstream in(input, std::ios::in | std::ios::binary);
if (!in.is_open()) {
std::cerr << "Could not open file for reading: " << input << "\n";
throw;
}
std::string contents((std::istreambuf_iterator<char>(in)), (std::istreambuf_iterator<char>()));
set_state(contents);
}
else {
set_state(input);
}
}

void FuzzConfigurator::set_state(const std::string& input)
{
m_state = State{input, 0};
m_use_encryption = m_fuzzer.get_next_token(m_state) % 2 == 0;
}

void FuzzConfigurator::print_cnf()
{
m_log << "// Fuzzer: " << m_fuzz_name << "\n";
m_log << "// Test case generated in " REALM_VER_CHUNK " on " << m_fuzzer.get_current_time_stamp() << ".\n";
m_log << "// REALM_MAX_BPNODE_SIZE is " << REALM_MAX_BPNODE_SIZE << "\n";
m_log << "// ----------------------------------------------------------------------\n";
const auto& printable_key =
!m_use_encryption ? "nullptr" : std::string("\"") + m_config.encryption_key.data() + "\"";
m_log << "// const char* key = " << printable_key << ";\n";
m_log << "\n";
}
52 changes: 52 additions & 0 deletions test/fuzzy_object_store/fuzz_configurator.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*************************************************************************
*
* Copyright 2022 Realm Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
**************************************************************************/
#ifndef FUZZ_CONFIG_HPP
#define FUZZ_CONFIG_HPP

#include "util.hpp"
#include "fuzz_logger.hpp"
#include <realm/object-store/shared_realm.hpp>
#include <string>
#include <vector>

class FuzzObject;
class FuzzConfigurator {
public:
FuzzConfigurator(FuzzObject& fuzzer, const std::string& input, bool use_input_file, const std::string& name);
const realm::Realm::Config& get_config() const;
FuzzObject& get_fuzzer();
const std::string& get_realm_path() const;
FuzzLog& get_logger();
State& get_state();
void set_state(const std::string& input);
void print_cnf();

private:
void init(const std::string&);
void setup_realm_config();

realm::Realm::Config m_config;
std::string m_path;
FuzzLog m_log;
bool m_use_encryption{false};
bool m_used_input_file{false};
FuzzObject& m_fuzzer;
State m_state;
std::string m_fuzz_name;
};
#endif
Loading