Skip to content

Commit f051125

Browse files
Add simple fuzz-harness
1 parent 94e7336 commit f051125

File tree

3 files changed

+67
-0
lines changed

3 files changed

+67
-0
lines changed

CMakeLists.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@ include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/project-defaults.cmake)
1212
add_subdirectory(yoga)
1313
add_subdirectory(tests)
1414

15+
option(BUILD_FUZZ_TESTS "Build fuzz tests" OFF)
16+
17+
if ('${CMAKE_CXX_COMPILER_ID}' MATCHES 'Clang' AND BUILD_FUZZ_TESTS)
18+
add_subdirectory(fuzz)
19+
endif()
20+
1521
# cmake install config
1622
include(GNUInstallDirs)
1723
include(CMakePackageConfigHelpers)

fuzz/CMakeLists.txt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# If google/oss-fuzz has set the fuzzing engine
2+
if(DEFINED ENV{LIB_FUZZING_ENGINE})
3+
set(FUZZING_ENGINE $ENV{LIB_FUZZING_ENGINE})
4+
set(FUZZING_COMPILE_FLAGS "")
5+
set(FUZZING_LINK_FLAGS "${FUZZING_ENGINE}")
6+
else()
7+
set(FUZZING_COMPILE_FLAGS "-fsanitize=fuzzer")
8+
set(FUZZING_LINK_FLAGS "-fsanitize=fuzzer")
9+
endif()
10+
11+
add_executable(fuzz_layout fuzz_layout.cpp)
12+
set_target_properties(fuzz_layout PROPERTIES
13+
COMPILE_FLAGS "${FUZZING_COMPILE_FLAGS}"
14+
LINK_FLAGS "${FUZZING_LINK_FLAGS}"
15+
)
16+
target_link_libraries(fuzz_layout yogacore)

fuzz/fuzz_layout.cpp

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#include <fuzzer/FuzzedDataProvider.h>
2+
#include <cstdint>
3+
#include <yoga/Yoga.h>
4+
5+
YGFlexDirection fuzzed_flex_direction(FuzzedDataProvider &fdp) {
6+
return fdp.PickValueInArray({
7+
YGFlexDirectionColumn,
8+
YGFlexDirectionColumnReverse,
9+
YGFlexDirectionRow,
10+
YGFlexDirectionRowReverse,
11+
});
12+
}
13+
14+
void FillFuzzedTree(FuzzedDataProvider &fdp, const YGConfigRef &config, const YGNodeRef &root, size_t depth = 0) {
15+
constexpr size_t kMaxDepth = 20;
16+
constexpr size_t kMaxChildren= 20;
17+
18+
if(depth > kMaxDepth) {
19+
return;
20+
}
21+
22+
size_t children = fdp.ConsumeIntegralInRange<size_t>(0, kMaxChildren);
23+
for(size_t i = 0; i < children; i++) {
24+
const YGNodeRef child = YGNodeNewWithConfig(config);
25+
YGNodeStyleSetFlexDirection(root, fuzzed_flex_direction(fdp));
26+
YGNodeStyleSetWidth(child, fdp.ConsumeFloatingPoint<float>());
27+
YGNodeStyleSetGap(child, YGGutterAll, fdp.ConsumeProbability<float>()*100);
28+
YGNodeStyleSetHeight(child, fdp.ConsumeFloatingPoint<float>());
29+
YGNodeInsertChild(root, child, i);
30+
FillFuzzedTree(fdp, config, child, depth + 1);
31+
}
32+
}
33+
34+
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
35+
FuzzedDataProvider fdp(data,size);
36+
const YGConfigRef config = YGConfigNew();
37+
const YGNodeRef root = YGNodeNewWithConfig(config);
38+
FillFuzzedTree(fdp, config, root);
39+
40+
YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR);
41+
42+
YGNodeFreeRecursive(root);
43+
YGConfigFree(config);
44+
return 0;
45+
}

0 commit comments

Comments
 (0)