Skip to content

Commit

Permalink
Add clang and a bit of fuzzing
Browse files Browse the repository at this point in the history
  • Loading branch information
chusitoo committed Jul 1, 2023
1 parent d15347d commit f672933
Show file tree
Hide file tree
Showing 5 changed files with 237 additions and 4 deletions.
19 changes: 17 additions & 2 deletions .vscode/compile_commands.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,27 @@
[
{
"command": "g++ test.cpp -lstdc++ -std=c++11 -Wall -Wextra -Wold-style-cast -Wdelete-incomplete -Wformat-nonliteral -Wno-varargs -Wconversion -O2 -DFLATBUSH_SPAN -o test",
"command": "clang++ test.cpp -lstdc++ -std=c++11 -Wall -Wextra -pedantic -Wold-style-cast -Wdelete-incomplete -Wformat-nonliteral -Wno-varargs -Wconversion -O2 -DFLATBUSH_SPAN -o test",
"directory": "/flatbush",
"file": "/flatbush/test.cpp"
},
{
"command": "g++ bench.cpp -lstdc++ -std=c++11 -Wall -Wextra -Wold-style-cast -Wdelete-incomplete -Wformat-nonliteral -Wno-varargs -Wconversion -O2 -DFLATBUSH_SPAN -o bench",
"command": "clang++ bench.cpp -lstdc++ -std=c++11 -Wall -Wextra -pedantic -Wold-style-cast -Wdelete-incomplete -Wformat-nonliteral -Wno-varargs -Wconversion -O2 -DFLATBUSH_SPAN -o bench",
"directory": "/flatbush",
"file": "/flatbush/bench.cpp"
},
{
"command": "clang++ fuzz_from.cpp -lstdc++ -std=c++11 -O1 -g -fsanitize=fuzzer,address -DFLATBUSH_SPAN -o fuzz_from",
"directory": "/flatbush",
"file": "/flatbush/fuzz_from.cpp"
},
{
"command": "clang++ fuzz_search.cpp -lstdc++ -std=c++11 -O1 -g -fsanitize=fuzzer,address -DFLATBUSH_SPAN -o fuzz_search",
"directory": "/flatbush",
"file": "/flatbush/fuzz_search.cpp"
},
{
"command": "clang++ fuzz_neighbors.cpp -lstdc++ -std=c++11 -O1 -g -fsanitize=fuzzer,address -DFLATBUSH_SPAN -o fuzz_neighbors",
"directory": "/flatbush",
"file": "/flatbush/fuzz_neighbors.cpp"
}
]
5 changes: 3 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
FROM mcr.microsoft.com/cbl-mariner/base/core:2.0

RUN tdnf update -y \
&& tdnf install -y build-essential tar \
&& tdnf clean all
&& tdnf install -y build-essential ca-certificates clang++ curl git tar unzip zip \
&& tdnf clean all \
&& rm -rf /var/cache/tdnf
84 changes: 84 additions & 0 deletions fuzz_from.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*
MIT License
Copyright (c) 2023 Alex Emirov
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

#include "flatbush.h"

#include <cassert>

template <typename ArrayType>
flatbush::Flatbush<ArrayType> createIndex(uint32_t iNumItems, uint16_t iNodeSize)
{
flatbush::FlatbushBuilder<ArrayType> wBuilder(iNumItems, iNodeSize);

auto wSize = static_cast<size_t>(iNumItems);
for (size_t wIdx = 0; wIdx < wSize; ++wIdx)
{
auto coord = static_cast<ArrayType>(wIdx);
wBuilder.add({ coord, coord, coord, coord });
}
auto wIndex = wBuilder.finish();

return wIndex;
}

template <typename ArrayType>
int from(const uint8_t *iData, size_t iSize)
{
if (iSize < flatbush::gHeaderByteSize) return 0;
if (iData[0] != flatbush::gValidityFlag) return 0;
if ((iData[1] >> 4) != flatbush::gVersion) return 0;
if ((iData[1] & 0x0f) != flatbush::detail::arrayTypeIndex<ArrayType>()) return 0;
const auto wNodeSize = *flatbush::detail::bit_cast<uint16_t*>(&iData[2]);
if (wNodeSize < 2) return 0;

const auto wNumItems = *flatbush::detail::bit_cast<uint32_t*>(&iData[4]);
const auto& wLevelBounds = flatbush::detail::calculateNumNodesPerLevel(wNumItems, wNodeSize);
const auto wNumNodes = wLevelBounds.empty() ? wNumItems : wLevelBounds.back();
const auto wIndicesByteSize = wNumNodes * ((wNumNodes >= 16384) ? sizeof(uint32_t) : sizeof(uint16_t));
const auto wNodesByteSize = wNumNodes * sizeof(flatbush::Box<ArrayType>);
const auto wSize = flatbush::gHeaderByteSize + wNodesByteSize + wIndicesByteSize;
if (wSize != iSize) return 0;

auto wIndex = flatbush::FlatbushBuilder<ArrayType>::from(iData, iSize);

assert(wIndex.data().size() == iSize);
assert(wIndex.nodeSize() == wNodeSize);
assert(wIndex.numItems() == wNumItems);
assert(wIndex.indexSize() == wNumNodes);

return 0;
}

extern "C" int LLVMFuzzerTestOneInput(const uint8_t *iData, size_t iSize)
{
from<int8_t>(iData, iSize);
from<uint8_t>(iData, iSize);
from<int16_t>(iData, iSize);
from<uint16_t>(iData, iSize);
from<int32_t>(iData, iSize);
from<uint32_t>(iData, iSize);
from<float>(iData, iSize);
from<double>(iData, iSize);
return 0;
}
70 changes: 70 additions & 0 deletions fuzz_neighbors.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
MIT License
Copyright (c) 2023 Alex Emirov
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

#include "flatbush.h"

#include <cassert>

flatbush::Flatbush<double> createIndex()
{
flatbush::FlatbushBuilder<double> wBuilder;

wBuilder.add({ 42, 0, 42, 0 });
auto wIndex = wBuilder.finish();

return wIndex;
}

extern "C" int LLVMFuzzerTestOneInput(const uint8_t *iData, size_t iSize)
{
static auto sIndex = createIndex();

if (iSize == 32)
{
const auto wX = *flatbush::detail::bit_cast<const double*>(&iData[0]);
const auto wY = *flatbush::detail::bit_cast<const double*>(&iData[8]);
const flatbush::Point<double> wPoint { wX, wY };

const auto wMaxResults = *flatbush::detail::bit_cast<const size_t*>(&iData[16]);
const auto wMaxDistance = *flatbush::detail::bit_cast<const double*>(&iData[24]);

auto result = sIndex.neighbors(wPoint, wMaxResults, wMaxDistance);

if (wMaxResults > 0)
{
const auto wDistance = std::pow(wX - 42, 2.0) + std::pow(wY, 2.0);

if (wMaxDistance >= 0 && wDistance <= std::pow(wMaxDistance, 2.0))
{
assert(result.size() == 1);
}
else
{
assert(result.size() == 0);
}
}
}

return 0;
}
63 changes: 63 additions & 0 deletions fuzz_search.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
MIT License
Copyright (c) 2023 Alex Emirov
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

#include "flatbush.h"

#include <cassert>

flatbush::Flatbush<double> createIndex()
{
flatbush::FlatbushBuilder<double> wBuilder;

wBuilder.add({ 42, 0, 42, 0 });
auto wIndex = wBuilder.finish();

return wIndex;
}

extern "C" int LLVMFuzzerTestOneInput(const uint8_t *iData, size_t iSize)
{
static auto sIndex = createIndex();

if (iSize == 32)
{
const auto wMinX = *flatbush::detail::bit_cast<const double*>(&iData[0]);
const auto wMinY = *flatbush::detail::bit_cast<const double*>(&iData[8]);
const auto wMaxX = *flatbush::detail::bit_cast<const double*>(&iData[16]);
const auto wMaxY = *flatbush::detail::bit_cast<const double*>(&iData[24]);

auto result = sIndex.search({ wMinX, wMinY, wMaxX, wMaxY });

if (wMinX <= 42 && wMaxX >= 42 && wMinY <= 0 && wMaxY >= 0)
{
assert(result.size() == 1);
}
else
{
assert(result.size() == 0);
}
}

return 0;
}

0 comments on commit f672933

Please sign in to comment.