/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 | 2.72M | YGFlexDirection fuzzedFlexDirection(FuzzedDataProvider& fdp) { |
13 | 2.72M | return fdp.PickValueInArray({ |
14 | 2.72M | YGFlexDirectionColumn, |
15 | 2.72M | YGFlexDirectionColumnReverse, |
16 | 2.72M | YGFlexDirectionRow, |
17 | 2.72M | YGFlexDirectionRowReverse, |
18 | 2.72M | }); |
19 | 2.72M | } |
20 | | |
21 | | void fillFuzzedTree( |
22 | | FuzzedDataProvider& fdp, |
23 | | YGConfigConstRef config, |
24 | | YGNodeRef root, |
25 | 2.72M | size_t depth = 0) { |
26 | 2.72M | constexpr size_t kMaxDepth = 20; |
27 | 2.72M | constexpr size_t kMaxChildren = 20; |
28 | | |
29 | 2.72M | if (depth > kMaxDepth) { |
30 | 2.22M | return; |
31 | 2.22M | } |
32 | | |
33 | 499k | size_t children = fdp.ConsumeIntegralInRange<size_t>(0, kMaxChildren); |
34 | 3.22M | for (size_t i = 0; i < children; i++) { |
35 | 2.72M | YGNodeRef child = YGNodeNewWithConfig(config); |
36 | 2.72M | YGNodeStyleSetFlexDirection(root, fuzzedFlexDirection(fdp)); |
37 | 2.72M | YGNodeStyleSetWidth(child, fdp.ConsumeFloatingPoint<float>()); |
38 | 2.72M | YGNodeStyleSetGap( |
39 | 2.72M | child, YGGutterAll, fdp.ConsumeProbability<float>() * 100); |
40 | 2.72M | YGNodeStyleSetHeight(child, fdp.ConsumeFloatingPoint<float>()); |
41 | 2.72M | YGNodeInsertChild(root, child, i); |
42 | 2.72M | fillFuzzedTree(fdp, config, child, depth + 1); |
43 | 2.72M | } |
44 | 499k | } |
45 | | |
46 | 1.05k | extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) { |
47 | 1.05k | FuzzedDataProvider fdp(data, size); |
48 | 1.05k | YGConfigRef config = YGConfigNew(); |
49 | 1.05k | YGNodeRef root = YGNodeNewWithConfig(config); |
50 | 1.05k | fillFuzzedTree(fdp, config, root); |
51 | | |
52 | 1.05k | YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR); |
53 | | |
54 | 1.05k | YGNodeFreeRecursive(root); |
55 | 1.05k | YGConfigFree(config); |
56 | 1.05k | return 0; |
57 | 1.05k | } |