-
Notifications
You must be signed in to change notification settings - Fork 0
/
fuzz_from.cpp
95 lines (77 loc) · 3.42 KB
/
fuzz_from.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/*
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 <cassert>
#include "flatbush.h"
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;
}
std::vector<size_t> calculateNumNodesPerLevel(uint32_t iNumItems, uint32_t iNodeSize) {
size_t wCount = iNumItems;
size_t wNumNodes = iNumItems;
std::vector<size_t> wLevelBounds{wNumNodes};
do {
wCount = (wCount + iNodeSize - 1) / iNodeSize;
wNumNodes += wCount;
wLevelBounds.push_back(wNumNodes);
} while (wCount > 1);
return wLevelBounds;
}
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 = calculateNumNodesPerLevel(wNumItems, wNodeSize);
const auto wNumNodes = wLevelBounds.empty() ? wNumItems : wLevelBounds.back();
const auto wIndicesByteSize =
wNumNodes * ((wNumNodes > flatbush::gMaxNumNodes) ? 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;
}