blob: e075905499e3feaa6e98e6f5b79e4432ea280e96 [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#include <functional>
17
18#include "fuzzer/FuzzedDataProvider.h"
19#include "utils/RWLock.h"
20
21static constexpr int MAX_OPERATIONS = 100;
22static constexpr int MAX_NAME_LEN = 2048;
23
24static const std::vector<std::function<void(android::RWLock*)>> operations = {
25 [](android::RWLock* lock) -> void {
26 // This might return a non-zero value if already locked
27 // Either way we are definitely locked now.
28 lock->tryWriteLock();
29 },
30 [](android::RWLock* lock) -> void { lock->tryReadLock(); },
31 [](android::RWLock* lock) -> void { lock->unlock(); },
32};
33
34extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
35 FuzzedDataProvider dataProvider(data, size);
36 std::string nameStr = dataProvider.ConsumeRandomLengthString(MAX_NAME_LEN);
37 int type = dataProvider.ConsumeIntegral<int>();
38 android::RWLock rwLock = android::RWLock(type, nameStr.c_str());
39 std::vector<uint8_t> opsToRun = dataProvider.ConsumeRemainingBytes<uint8_t>();
40 int opsRun = 0;
41 for (auto it : opsToRun) {
42 if (opsRun++ >= MAX_OPERATIONS) {
43 break;
44 }
45 it = it % operations.size();
46 operations[it](&rwLock);
47 }
48 rwLock.unlock();
49 return 0;
50}