/src/yoga/fuzz/FuzzLayout.cpp
Line | Count | Source |
1 | | /* |
2 | | * Copyright (c) Meta Platforms, Inc. and affiliates. |
3 | | * |
4 | | * This source code is licensed under the MIT license found in the |
5 | | * LICENSE file in the root directory of this source tree. |
6 | | */ |
7 | | |
8 | | #include <fuzzer/FuzzedDataProvider.h> |
9 | | #include <yoga/Yoga.h> |
10 | | #include <cstdint> |
11 | | |
12 | 4.00M | YGFlexDirection fuzzedFlexDirection(FuzzedDataProvider& fdp) { |
13 | 4.00M | return fdp.PickValueInArray({ |
14 | 4.00M | YGFlexDirectionColumn, |
15 | 4.00M | YGFlexDirectionColumnReverse, |
16 | 4.00M | YGFlexDirectionRow, |
17 | 4.00M | YGFlexDirectionRowReverse, |
18 | 4.00M | }); |
19 | 4.00M | } |
20 | | |
21 | | void fillFuzzedTree( |
22 | | FuzzedDataProvider& fdp, |
23 | | YGConfigConstRef config, |
24 | | YGNodeRef root, |
25 | 4.00M | size_t depth = 0) { |
26 | 4.00M | constexpr size_t kMaxDepth = 20; |
27 | 4.00M | constexpr size_t kMaxChildren = 20; |
28 | | |
29 | 4.00M | if (depth > kMaxDepth) { |
30 | 3.39M | return; |
31 | 3.39M | } |
32 | | |
33 | 613k | size_t children = fdp.ConsumeIntegralInRange<size_t>(0, kMaxChildren); |
34 | 4.61M | for (size_t i = 0; i < children; i++) { |
35 | 4.00M | YGNodeRef child = YGNodeNewWithConfig(config); |
36 | 4.00M | YGNodeStyleSetFlexDirection(root, fuzzedFlexDirection(fdp)); |
37 | 4.00M | YGNodeStyleSetWidth(child, fdp.ConsumeFloatingPoint<float>()); |
38 | 4.00M | YGNodeStyleSetGap( |
39 | 4.00M | child, YGGutterAll, fdp.ConsumeProbability<float>() * 100); |
40 | 4.00M | YGNodeStyleSetHeight(child, fdp.ConsumeFloatingPoint<float>()); |
41 | 4.00M | YGNodeInsertChild(root, child, i); |
42 | 4.00M | fillFuzzedTree(fdp, config, child, depth + 1); |
43 | 4.00M | } |
44 | 613k | } |
45 | | |
46 | 1.03k | extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) { |
47 | 1.03k | FuzzedDataProvider fdp(data, size); |
48 | 1.03k | YGConfigRef config = YGConfigNew(); |
49 | 1.03k | YGNodeRef root = YGNodeNewWithConfig(config); |
50 | 1.03k | fillFuzzedTree(fdp, config, root); |
51 | | |
52 | 1.03k | YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR); |
53 | | |
54 | 1.03k | YGNodeFreeRecursive(root); |
55 | 1.03k | YGConfigFree(config); |
56 | 1.03k | return 0; |
57 | 1.03k | } |