1 /******************************************************************************
2 *
3 * Copyright (C) 2022 The Android Open Source Project
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at:
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 *****************************************************************************
18 */
19
20 #include <fuzzer/FuzzedDataProvider.h>
21 #include <android-base/unique_fd.h>
22 #include <cputimeinstate.h>
23 #include <functional>
24
25 using namespace android::bpf;
26
27 static const uint16_t MAX_VEC_SIZE = 500;
28
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)29 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
30 FuzzedDataProvider fdp(data, size);
31
32 uint32_t uid = fdp.ConsumeIntegral<uint32_t>();
33 uint64_t lastUpdate = fdp.ConsumeIntegral<uint64_t>();
34 uint16_t aggregationKey = fdp.ConsumeIntegral<uint16_t>();
35 pid_t pid = fdp.ConsumeIntegral<pid_t>();
36 std::vector<uint16_t> aggregationKeys;
37 uint16_t aggregationKeysSize = fdp.ConsumeIntegralInRange<size_t>(0, MAX_VEC_SIZE);
38 for (uint16_t i = 0; i < aggregationKeysSize; i++) {
39 aggregationKeys.push_back(fdp.ConsumeIntegral<uint16_t>());
40 }
41
42 // To randomize the API calls
43 while (fdp.remaining_bytes() > 0) {
44 auto func = fdp.PickValueInArray<const std::function<void()>>({
45 [&]() { getUidCpuFreqTimes(uid); },
46 [&]() { getUidsUpdatedCpuFreqTimes(&lastUpdate); },
47 [&]() { getUidConcurrentTimes(uid);},
48 [&]() { getUidsUpdatedConcurrentTimes(&lastUpdate); },
49 [&]() { startAggregatingTaskCpuTimes(pid, aggregationKey); },
50 [&]() { getAggregatedTaskCpuFreqTimes(pid, aggregationKeys); },
51 });
52
53 func();
54 }
55
56 return 0;
57 }
58