Skip to content

Commit

Permalink
Add simple fuzz-harness
Browse files Browse the repository at this point in the history
  • Loading branch information
nathaniel-brough committed Jan 10, 2024
1 parent 94e7336 commit 1274d3b
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 0 deletions.
6 changes: 6 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/project-defaults.cmake)
add_subdirectory(yoga)
add_subdirectory(tests)

option(BUILD_FUZZ_TESTS "Build fuzz tests" OFF)

if ('${CMAKE_CXX_COMPILER_ID}' MATCHES 'Clang' AND BUILD_FUZZ_TESTS)
add_subdirectory(fuzz)
endif()

# cmake install config
include(GNUInstallDirs)
include(CMakePackageConfigHelpers)
Expand Down
16 changes: 16 additions & 0 deletions fuzz/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# If google/oss-fuzz has set the fuzzing engine
if(DEFINED ENV{LIB_FUZZING_ENGINE})
set(FUZZING_ENGINE $ENV{LIB_FUZZING_ENGINE})
set(FUZZING_COMPILE_FLAGS "")
set(FUZZING_LINK_FLAGS "${FUZZING_ENGINE}")
else()
set(FUZZING_COMPILE_FLAGS "-fsanitize=fuzzer")
set(FUZZING_LINK_FLAGS "-fsanitize=fuzzer")
endif()

add_executable(fuzz_layout fuzz_layout.cpp)
set_target_properties(fuzz_layout PROPERTIES
COMPILE_FLAGS "${FUZZING_COMPILE_FLAGS}"
LINK_FLAGS "${FUZZING_LINK_FLAGS}"
)
target_link_libraries(fuzz_layout yogacore)
50 changes: 50 additions & 0 deletions fuzz/fuzz_layout.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#include <fuzzer/FuzzedDataProvider.h>
#include <yoga/Yoga.h>
#include <cstdint>

YGFlexDirection fuzzed_flex_direction(FuzzedDataProvider& fdp) {
return fdp.PickValueInArray({
YGFlexDirectionColumn,
YGFlexDirectionColumnReverse,
YGFlexDirectionRow,
YGFlexDirectionRowReverse,
});
}

void FillFuzzedTree(
FuzzedDataProvider& fdp,
const YGConfigRef& config,
const YGNodeRef& root,
size_t depth = 0) {
constexpr size_t kMaxDepth = 20;
constexpr size_t kMaxChildren = 20;

if (depth > kMaxDepth) {
return;
}

size_t children = fdp.ConsumeIntegralInRange<size_t>(0, kMaxChildren);
for (size_t i = 0; i < children; i++) {
const YGNodeRef child = YGNodeNewWithConfig(config);
YGNodeStyleSetFlexDirection(root, fuzzed_flex_direction(fdp));
YGNodeStyleSetWidth(child, fdp.ConsumeFloatingPoint<float>());
YGNodeStyleSetGap(
child, YGGutterAll, fdp.ConsumeProbability<float>() * 100);
YGNodeStyleSetHeight(child, fdp.ConsumeFloatingPoint<float>());
YGNodeInsertChild(root, child, i);
FillFuzzedTree(fdp, config, child, depth + 1);
}
}

extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
FuzzedDataProvider fdp(data, size);
const YGConfigRef config = YGConfigNew();
const YGNodeRef root = YGNodeNewWithConfig(config);
FillFuzzedTree(fdp, config, root);

YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR);

YGNodeFreeRecursive(root);
YGConfigFree(config);
return 0;
}

0 comments on commit 1274d3b

Please sign in to comment.