Skip to content

Commit

Permalink
Adds fuzzing target (#18)
Browse files Browse the repository at this point in the history
Also executed it and bugfixed all of the found issues, mostly crashes for malformed data.

Also added proper clang-format file.
  • Loading branch information
TinyTinni authored Nov 4, 2023
1 parent 46b4c40 commit 784cad8
Show file tree
Hide file tree
Showing 8 changed files with 895 additions and 656 deletions.
95 changes: 95 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
---
Language: Cpp
# BasedOnStyle: LLVM
AccessModifierOffset: -2
AlignAfterOpenBracket: Align
AlignConsecutiveAssignments: false
AlignConsecutiveDeclarations: false
AlignEscapedNewlinesLeft: false
AlignOperands: true
AlignTrailingComments: true
AllowAllParametersOfDeclarationOnNextLine: true
AllowShortBlocksOnASingleLine: false
AllowShortCaseLabelsOnASingleLine: false
AllowShortFunctionsOnASingleLine: All
AllowShortIfStatementsOnASingleLine: false
AllowShortLoopsOnASingleLine: false
AlwaysBreakAfterDefinitionReturnType: None
AlwaysBreakAfterReturnType: None
AlwaysBreakBeforeMultilineStrings: false
AlwaysBreakTemplateDeclarations: false
BinPackArguments: true
BinPackParameters: true
BraceWrapping:
AfterClass: false
AfterControlStatement: false
AfterEnum: false
AfterFunction: false
AfterNamespace: false
AfterObjCDeclaration: false
AfterStruct: false
AfterUnion: false
BeforeCatch: false
BeforeElse: false
IndentBraces: false
BreakBeforeBinaryOperators: None
BreakBeforeBraces: Allman
BreakBeforeTernaryOperators: true
BreakConstructorInitializersBeforeComma: false
BreakAfterJavaFieldAnnotations: false
BreakStringLiterals: true
ColumnLimit: 80
CommentPragmas: '^ IWYU pragma:'
ConstructorInitializerAllOnOneLineOrOnePerLine: false
ConstructorInitializerIndentWidth: 4
ContinuationIndentWidth: 4
Cpp11BracedListStyle: true
DerivePointerAlignment: false
DisableFormat: false
ExperimentalAutoDetectBinPacking: false
ForEachMacros: [ foreach, Q_FOREACH, BOOST_FOREACH ]
IncludeCategories:
- Regex: '^"(llvm|llvm-c|clang|clang-c)/'
Priority: 2
- Regex: '^(<|"(gtest|isl|json)/)'
Priority: 3
- Regex: '.*'
Priority: 1
IncludeIsMainRegex: '$'
IndentCaseLabels: false
IndentWidth: 4
IndentWrappedFunctionNames: false
JavaScriptQuotes: Leave
JavaScriptWrapImports: true
KeepEmptyLinesAtTheStartOfBlocks: true
MacroBlockBegin: ''
MacroBlockEnd: ''
MaxEmptyLinesToKeep: 1
NamespaceIndentation: None
ObjCBlockIndentWidth: 2
ObjCSpaceAfterProperty: false
ObjCSpaceBeforeProtocolList: true
PenaltyBreakBeforeFirstCallParameter: 19
PenaltyBreakComment: 300
PenaltyBreakFirstLessLess: 120
PenaltyBreakString: 1000
PenaltyExcessCharacter: 1000000
PenaltyReturnTypeOnItsOwnLine: 60
PointerAlignment: Right
ReflowComments: true
SortIncludes: true
SpaceAfterCStyleCast: false
SpaceAfterTemplateKeyword: true
SpaceBeforeAssignmentOperators: true
SpaceBeforeParens: ControlStatements
SpaceInEmptyParentheses: false
SpacesBeforeTrailingComments: 1
SpacesInAngles: false
SpacesInContainerLiterals: true
SpacesInCStyleCastParentheses: false
SpacesInParentheses: false
SpacesInSquareBrackets: false
Standard: Cpp11
TabWidth: 8
UseTab: Never

2 changes: 1 addition & 1 deletion .github/workflows/cmake.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:
runs-on: ${{ matrix.os }}

steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4

- name: Configure CMake
# Configure CMake in a 'build' subdirectory. `CMAKE_BUILD_TYPE` is only required if you are using a single-configuration generator such as make.
Expand Down
13 changes: 12 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,15 @@ cmake_minimum_required (VERSION 3.6)
project (vdf-Parser)

include(CTest)
add_subdirectory(./tests)
add_subdirectory(./tests)

## add fuzzing, if supported

set(ENABLE_FUZZING OFF)
if ("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang" OR "${CMAKE_CXX_COMPILER_ID}" MATCHES "MSVC" AND NOT APPLE)
set(ENABLE_FUZZING ON)
endif()

if (${ENABLE_FUZZING})
add_subdirectory(fuzzing)
endif()
14 changes: 14 additions & 0 deletions fuzzing/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
set(FUZZ_RUNTIME
10
CACHE STRING "Number of seconds to run fuzz tests during ctest run")

set (CMAKE_CXX_STANDARD 17)
add_executable(fuzzing main.cpp)
target_link_libraries(fuzzing PRIVATE -coverage -fsanitize=fuzzer)
if ("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang")
target_compile_options(fuzzing PRIVATE -fsanitize=fuzzer)
elseif("${CMAKE_CXX_COMPILER_ID}" MATCHES "MSVC")
target_compile_options(fuzzing PRIVATE /fsanitize=fuzzer)
endif()

add_test(NAME fuzzing_run COMMAND fuzzing -max_total_time=${FUZZ_RUNTIME} -timeout=${FUZZ_RUNTIME})
17 changes: 17 additions & 0 deletions fuzzing/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include <cstdint>
#include <string_view>

#include "../vdf_parser.hpp"
#include <iostream>

extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
{

std::string_view test_corpus{reinterpret_cast<const char *>(data), size};
bool ok;
tyti::vdf::Options opt;
opt.ignore_includes = true;
auto blub =
tyti::vdf::read(test_corpus.begin(), test_corpus.end(), &ok, opt);
return 0;
}
9 changes: 5 additions & 4 deletions tests/main.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include "catch.hpp"
#include <iostream>

const char* testdata_dir = SOURCE_DIR "/testdata/";
const char *testdata_dir = SOURCE_DIR "/testdata/";

#ifdef _WIN32
#include <direct.h>
Expand All @@ -13,12 +13,13 @@ const char* testdata_dir = SOURCE_DIR "/testdata/";
#define cd chdir
#endif


int main(int argc, char* argv[]) {
int main(int argc, char *argv[])
{

if (cd(testdata_dir) != 0)
{
std::cerr << "Cannot set working directory to " << testdata_dir << std::endl;
std::cerr << "Cannot set working directory to " << testdata_dir
<< std::endl;
return 1;
}

Expand Down
Loading

0 comments on commit 784cad8

Please sign in to comment.