blob: 63d8a28c92c7b240a112459698a06d03755c4a3b [file] [log] [blame]
Dylan Katz7168f272020-07-02 11:51:44 -07001/*
2 * Copyright 2020 The Android Open Source Project
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 "utils/StopWatch.h"
19
20static constexpr int MAX_OPERATIONS = 100;
21static constexpr int MAX_NAME_LEN = 2048;
22
23static const std::vector<std::function<void(android::StopWatch)>> operations = {
24 [](android::StopWatch stopWatch) -> void { stopWatch.reset(); },
25 [](android::StopWatch stopWatch) -> void { stopWatch.lap(); },
26 [](android::StopWatch stopWatch) -> void { stopWatch.elapsedTime(); },
27 [](android::StopWatch stopWatch) -> void { stopWatch.name(); },
28};
29
30extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
31 FuzzedDataProvider dataProvider(data, size);
32 std::string nameStr = dataProvider.ConsumeRandomLengthString(MAX_NAME_LEN);
33 int clockVal = dataProvider.ConsumeIntegral<int>();
34 android::StopWatch stopWatch = android::StopWatch(nameStr.c_str(), clockVal);
35 std::vector<uint8_t> opsToRun = dataProvider.ConsumeRemainingBytes<uint8_t>();
36 int opsRun = 0;
37 for (auto it : opsToRun) {
38 if (opsRun++ >= MAX_OPERATIONS) {
39 break;
40 }
41 it = it % operations.size();
42 operations[it](stopWatch);
43 }
44 return 0;
45}