diff options
author | Dylan Katz <dylan.katz@leviathansecurity.com> | 2020-05-11 15:44:01 -0700 |
---|---|---|
committer | Dylan Katz <dylan.katz@leviathansecurity.com> | 2020-05-15 10:30:16 -0700 |
commit | 9d5845bb5e19011fd0e8e9842f1aa16add138454 (patch) | |
tree | 73a7fd89625610816289f347b9dd98e28ef29ffc /libutils/Vector_fuzz.cpp | |
parent | fa8bf5be4e55e0d701222b28c5fa52b07149131d (diff) |
Add fuzzers for libutils classes
Adds fuzzers for BitSet, FileMap, String8, String16, and Vector.
Test: Ran fuzzers on Android Pixel 3a. Aggregate coverage was 1.2% (this is far lower than true coverage due to shared libraries being counted)
Change-Id: I739216fe88afa51dc2f73b857da91116853382f0
Removed unneeded cflags, moved libbase to defaults
Test: Built Android.bp successfully
Signed-off-by: Dylan Katz <dylan.katz@leviathansecurity.com>
Change-Id: I739216fe88afa51dc2f73b857da91116853382f0
Diffstat (limited to 'libutils/Vector_fuzz.cpp')
-rw-r--r-- | libutils/Vector_fuzz.cpp | 83 |
1 files changed, 83 insertions, 0 deletions
diff --git a/libutils/Vector_fuzz.cpp b/libutils/Vector_fuzz.cpp new file mode 100644 index 000000000..f6df05135 --- /dev/null +++ b/libutils/Vector_fuzz.cpp @@ -0,0 +1,83 @@ +/* + * Copyright 2020 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include "fuzzer/FuzzedDataProvider.h" +#include "utils/Vector.h" +static constexpr uint16_t MAX_VEC_SIZE = 5000; + +void runVectorFuzz(const uint8_t* data, size_t size) { + FuzzedDataProvider dataProvider(data, size); + android::Vector<uint8_t> vec = android::Vector<uint8_t>(); + // We want to test handling of sizeof as well. + android::Vector<uint32_t> vec32 = android::Vector<uint32_t>(); + + // We're going to generate two vectors of this size + size_t vectorSize = dataProvider.ConsumeIntegralInRange<size_t>(0, MAX_VEC_SIZE); + vec.setCapacity(vectorSize); + vec32.setCapacity(vectorSize); + for (size_t i = 0; i < vectorSize; i++) { + uint8_t count = dataProvider.ConsumeIntegralInRange<uint8_t>(1, 5); + vec.insertAt((uint8_t)i, i, count); + vec32.insertAt((uint32_t)i, i, count); + vec.push_front(i); + vec32.push(i); + } + + // Now we'll perform some test operations with any remaining data + // Index to perform operations at + size_t index = dataProvider.ConsumeIntegralInRange<size_t>(0, vec.size()); + std::vector<uint8_t> remainingVec = dataProvider.ConsumeRemainingBytes<uint8_t>(); + // Insert an array and vector + vec.insertArrayAt(remainingVec.data(), index, remainingVec.size()); + android::Vector<uint8_t> vecCopy = android::Vector<uint8_t>(vec); + vec.insertVectorAt(vecCopy, index); + // Same thing for 32 bit vector + android::Vector<uint32_t> vec32Copy = android::Vector<uint32_t>(vec32); + vec32.insertArrayAt(vec32Copy.array(), index, vec32.size()); + vec32.insertVectorAt(vec32Copy, index); + // Replace single character + if (remainingVec.size() > 0) { + vec.replaceAt(remainingVec[0], index); + vec32.replaceAt(static_cast<uint32_t>(remainingVec[0]), index); + } else { + vec.replaceAt(0, index); + vec32.replaceAt(0, index); + } + // Add any remaining bytes + for (uint8_t i : remainingVec) { + vec.add(i); + vec32.add(static_cast<uint32_t>(i)); + } + // Shrink capactiy + vec.setCapacity(remainingVec.size()); + vec32.setCapacity(remainingVec.size()); + // Iterate through each pointer + size_t sum = 0; + for (auto& it : vec) { + sum += it; + } + for (auto& it : vec32) { + sum += it; + } + // Cleanup + vec.clear(); + vecCopy.clear(); + vec32.clear(); + vec32Copy.clear(); +} +extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) { + runVectorFuzz(data, size); + return 0; +} |