|
| 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