Line | Count | Source |
1 | | /* |
2 | | * Copyright 2026 Google LLC |
3 | | * |
4 | | * Licensed under the Apache License, Version 2.0 (the "License"); |
5 | | * you may not use this file except in compliance with the License. |
6 | | * You may obtain a copy of the License at |
7 | | * |
8 | | * http://www.apache.org/licenses/LICENSE-2.0 |
9 | | * |
10 | | * Unless required by applicable law or agreed to in writing, software |
11 | | * distributed under the License is distributed on an "AS IS" BASIS, |
12 | | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
13 | | * See the License for the specific language governing permissions and |
14 | | * limitations under the License. |
15 | | */ |
16 | | |
17 | | #include <fuzzer/FuzzedDataProvider.h> |
18 | | #include <cstddef> |
19 | | #include <cstdint> |
20 | | #include <string> |
21 | | #include <vector> |
22 | | |
23 | | #include "Utils.hpp" |
24 | | #include "Thresholds.hpp" |
25 | | #include "SensorPaths.hpp" |
26 | | #include "DeviceMgmt.hpp" |
27 | | |
28 | 2.87k | extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) { |
29 | 2.87k | FuzzedDataProvider fdp(data, size); |
30 | | |
31 | | // 1. Fuzz getDeviceBusAddr |
32 | 2.87k | std::string dev_name = fdp.ConsumeRandomLengthString(128); |
33 | 2.87k | size_t bus = 0; |
34 | 2.87k | size_t addr = 0; |
35 | 2.87k | uint64_t bus64 = 0; |
36 | 2.87k | uint64_t addr64 = 0; |
37 | 2.87k | (void)getDeviceBusAddr(dev_name, bus, addr); |
38 | 2.87k | (void)getDeviceBusAddr(dev_name, bus64, addr64); |
39 | | |
40 | | // 2. Fuzz escapePathForDbus |
41 | 2.87k | std::string path_str = fdp.ConsumeRandomLengthString(128); |
42 | 2.87k | (void)sensor_paths::escapePathForDbus(path_str); |
43 | | |
44 | | // 3. Fuzz SensorPaths helper |
45 | 2.87k | std::string sensor_type = fdp.ConsumeRandomLengthString(32); |
46 | 2.87k | (void)sensor_paths::getPathForUnits(sensor_type); |
47 | | |
48 | | // 4. Fuzz Config Maps & Threshold Parsing |
49 | 2.87k | SensorBaseConfigMap config_map; |
50 | 2.87k | config_map["Direction"] = fdp.ConsumeRandomLengthString(32); |
51 | 2.87k | config_map["Severity"] = fdp.ConsumeIntegral<uint64_t>(); |
52 | 2.87k | config_map["Value"] = fdp.ConsumeFloatingPoint<double>(); |
53 | 2.87k | config_map["Hysteresis"] = fdp.ConsumeFloatingPoint<double>(); |
54 | 2.87k | config_map["Label"] = fdp.ConsumeRandomLengthString(32); |
55 | 2.87k | config_map["Index"] = fdp.ConsumeIntegral<int64_t>(); |
56 | 2.87k | config_map["PowerState"] = fdp.ConsumeRandomLengthString(32); |
57 | 2.87k | config_map["PollRate"] = fdp.ConsumeFloatingPoint<float>(); |
58 | | |
59 | 2.87k | (void)getPowerState(config_map); |
60 | 2.87k | (void)getPollRate(config_map, 1.0f); |
61 | | |
62 | | // 3. Fuzz Utils string & path utilities |
63 | 2.87k | std::string name_str = fdp.ConsumeRandomLengthString(64); |
64 | 2.87k | (void)escapeName(name_str); |
65 | 2.87k | (void)configInterfaceName(name_str); |
66 | | |
67 | 2.87k | std::string file_path_str = fdp.ConsumeRandomLengthString(128); |
68 | 2.87k | (void)splitFileName(file_path_str); |
69 | | |
70 | 2.87k | PowerState p_state = PowerState::always; |
71 | 2.87k | setReadState(name_str, p_state); |
72 | 2.87k | try { |
73 | 2.87k | (void)readingStateGood(p_state); |
74 | 2.87k | } catch (...) {} |
75 | | |
76 | | // 4. Fuzz Config Maps & Threshold Parsing |
77 | 2.87k | config_map["MinReading"] = fdp.ConsumeFloatingPoint<double>(); |
78 | 2.87k | config_map["MaxReading"] = fdp.ConsumeFloatingPoint<double>(); |
79 | | |
80 | 2.87k | std::vector<std::string> labels; |
81 | 2.87k | size_t label_count = fdp.ConsumeIntegralInRange<size_t>(0, 5); |
82 | 4.94k | for (size_t i = 0; i < label_count; ++i) { |
83 | 2.07k | labels.push_back(fdp.ConsumeRandomLengthString(16)); |
84 | 2.07k | } |
85 | 2.87k | config_map["Labels"] = labels; |
86 | | |
87 | 2.87k | (void)getPermitSet(config_map); |
88 | 2.87k | (void)getPowerState(config_map); |
89 | 2.87k | (void)getPollRate(config_map, 1.0f); |
90 | | |
91 | 2.87k | std::pair<double, double> limits{0.0, 0.0}; |
92 | 2.87k | SensorBaseConfiguration sensor_base_config{"TestSensor", config_map}; |
93 | 2.87k | findLimits(limits, &sensor_base_config); |
94 | | |
95 | 2.87k | try { |
96 | 2.87k | (void)loadVariant<double>(config_map, "Value"); |
97 | 2.87k | } catch (...) {} |
98 | 2.87k | try { |
99 | 2.87k | (void)loadVariant<uint64_t>(config_map, "Severity"); |
100 | 2.87k | } catch (...) {} |
101 | 2.87k | try { |
102 | 2.87k | (void)loadVariant<std::string>(config_map, "Direction"); |
103 | 2.87k | } catch (...) {} |
104 | | |
105 | 2.87k | SensorData sensor_data; |
106 | 2.87k | std::string intf_name = "xyz.openbmc_project.Configuration.Thresholds." + |
107 | 2.87k | fdp.ConsumeRandomLengthString(32); |
108 | 2.87k | sensor_data[intf_name] = config_map; |
109 | | |
110 | 2.87k | std::vector<thresholds::Threshold> threshold_vector; |
111 | 2.87k | std::string match_label = fdp.ConsumeRandomLengthString(32); |
112 | 2.87k | int sensor_index = fdp.ConsumeIntegral<int>(); |
113 | | |
114 | 2.87k | (void)thresholds::parseThresholdsFromConfig(sensor_data, threshold_vector, |
115 | 2.87k | &match_label, &sensor_index); |
116 | 2.87k | (void)thresholds::parseThresholdsFromConfig(sensor_data, threshold_vector, |
117 | 2.87k | nullptr, nullptr); |
118 | | |
119 | | // 5. Fuzz thresholds interfaces |
120 | 20.0k | for (int i = 0; i <= static_cast<int>(thresholds::Level::ERROR); ++i) { |
121 | 17.2k | (void)thresholds::getInterface(static_cast<thresholds::Level>(i)); |
122 | 17.2k | } |
123 | | |
124 | | // 6. Fuzz DeviceMgmt helpers |
125 | 2.87k | std::string full_name = fdp.ConsumeRandomLengthString(32); |
126 | 2.87k | std::string partial_name = fdp.ConsumeRandomLengthString(32); |
127 | 2.87k | (void)sensorNameFind(full_name, partial_name); |
128 | | |
129 | 2.87k | return 0; |
130 | 2.87k | } |