Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Polygon to cells experimental fuzzer #800

Open
wants to merge 45 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 44 commits
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
de71b07
Add support for full containment mode in polygonToCells
Nov 4, 2023
45255d5
Add tests
Nov 4, 2023
61d3e5b
Implement OVERLAPPING mode, update tests to use mode flags
Nov 4, 2023
585f497
Rename containment enum values
Nov 6, 2023
1a44627
Fix and test for case where OVERLAPPING cell contains polygon
Nov 8, 2023
edaa901
Possibly fix test
Nov 8, 2023
fedaa5f
Fixture fix
Nov 8, 2023
b3eb114
add polygonToCellsExperimental fuzzers
isaacbrodsky Nov 16, 2023
a2adc04
Additional test coverage for error cases
Nov 25, 2023
f0f1b45
Test coverage for one more error, excluding unreachable block from co…
Nov 25, 2023
06ca7c0
Merge branch 'polyfill-contained' into polygon-to-cells-experimental-…
isaacbrodsky Nov 26, 2023
4226e43
fix fuzzer comment
isaacbrodsky Nov 26, 2023
da85fe5
Merge branch 'master' into polygon-to-cells-experimental-fuzzer
isaacbrodsky Dec 4, 2023
b055cd5
fix size estimation
isaacbrodsky Dec 4, 2023
674b427
Merge branch 'master' into polygon-to-cells-experimental-fuzzer
isaacbrodsky Dec 15, 2023
629c654
add test
isaacbrodsky Dec 15, 2023
641d481
add null test
isaacbrodsky Dec 15, 2023
7659732
add TODO
isaacbrodsky Dec 15, 2023
4f0341c
guard against numVerts = 0 for hole check
isaacbrodsky Dec 15, 2023
c6a21b8
add empty with null hole test
isaacbrodsky Dec 17, 2023
3975ff9
update test
isaacbrodsky Dec 17, 2023
cfe8816
actually cover
isaacbrodsky Dec 17, 2023
6730469
add fuzzer derived test case
isaacbrodsky Jan 28, 2024
29a9b01
simplify
isaacbrodsky Jan 28, 2024
85d5641
year
isaacbrodsky Jan 28, 2024
5ac48c5
actually pass flags
isaacbrodsky Jan 28, 2024
2778c08
Merge branch 'master' into polygon-to-cells-experimental-fuzzer
isaacbrodsky Feb 9, 2024
8f00a0b
Merge branch 'master' into polygon-to-cells-experimental-fuzzer
isaacbrodsky Feb 13, 2024
377777b
attempted fix
isaacbrodsky Aug 4, 2024
e25e8fa
Merge branch 'master' into polygon-to-cells-experimental-fuzzer
isaacbrodsky Aug 4, 2024
48f064e
remove ineffective fix
isaacbrodsky Aug 4, 2024
b4370cb
fix inequality
isaacbrodsky Aug 4, 2024
1c4cef0
fix for 0 length maxes
isaacbrodsky Aug 4, 2024
74f206e
fix memory test
isaacbrodsky Aug 4, 2024
572325c
fix for overlapping bbox inconsistency
isaacbrodsky Sep 22, 2024
942a54b
revert check
isaacbrodsky Sep 22, 2024
9b51221
revert memory test
isaacbrodsky Sep 22, 2024
9417bb0
more overlapping bbox changes
isaacbrodsky Sep 22, 2024
08bc56e
change assert to avoid triggering in test
isaacbrodsky Sep 22, 2024
6048f36
remove tests with unaligned reads
isaacbrodsky Sep 22, 2024
21c5a65
ignore extra build directories
isaacbrodsky Sep 22, 2024
c256faa
comment
isaacbrodsky Sep 22, 2024
2742464
Merge branch 'master' into polygon-to-cells-experimental-fuzzer
isaacbrodsky Sep 22, 2024
9dbea70
back out unneeded change
isaacbrodsky Sep 22, 2024
0b4f284
Merge branch 'master' into polygon-to-cells-experimental-fuzzer
isaacbrodsky Sep 30, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
/lib/
# Travis CI build directory
/build/
/build*/
# Local build directories
/Debug/
/Release/
Expand Down
4 changes: 4 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,9 @@ set(OTHER_SOURCE_FILES
src/apps/fuzzers/fuzzerDirectedEdge.c
src/apps/fuzzers/fuzzerLocalIj.c
src/apps/fuzzers/fuzzerPolygonToCells.c
src/apps/fuzzers/fuzzerPolygonToCellsExperimental.c
src/apps/fuzzers/fuzzerPolygonToCellsNoHoles.c
src/apps/fuzzers/fuzzerPolygonToCellsExperimentalNoHoles.c
src/apps/fuzzers/fuzzerCellToChildPos.c
src/apps/fuzzers/fuzzerInternalAlgos.c
src/apps/fuzzers/fuzzerInternalCoordIjk.c
Expand Down Expand Up @@ -560,7 +562,9 @@ if(BUILD_FUZZERS)
add_h3_fuzzer(fuzzerDirectedEdge src/apps/fuzzers/fuzzerDirectedEdge.c)
add_h3_fuzzer(fuzzerLocalIj src/apps/fuzzers/fuzzerLocalIj.c)
add_h3_fuzzer(fuzzerPolygonToCells src/apps/fuzzers/fuzzerPolygonToCells.c)
add_h3_fuzzer(fuzzerPolygonToCellsExperimental src/apps/fuzzers/fuzzerPolygonToCellsExperimental.c)
add_h3_fuzzer(fuzzerPolygonToCellsNoHoles src/apps/fuzzers/fuzzerPolygonToCellsNoHoles.c)
add_h3_fuzzer(fuzzerPolygonToCellsExperimentalNoHoles src/apps/fuzzers/fuzzerPolygonToCellsExperimentalNoHoles.c)
add_h3_fuzzer(fuzzerCellToChildPos src/apps/fuzzers/fuzzerCellToChildPos.c)
if(ENABLE_REQUIRES_ALL_SYMBOLS)
add_h3_fuzzer(fuzzerInternalAlgos src/apps/fuzzers/fuzzerInternalAlgos.c)
Expand Down
1 change: 1 addition & 0 deletions scripts/make_countries.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ for (int res = 0; res < MAX_RES + 1; res++) {
H3_EXPORT(maxPolygonToCellsSizeExperimental)(&COUNTRIES[index], res, CONTAINMENT_CENTER, &numHexagons);
hexagons = calloc(numHexagons, sizeof(H3Index));
H3_EXPORT(polygonToCellsExperimental)(&COUNTRIES[index], res, CONTAINMENT_FULL, hexagons);
free(hexagons);
}
});

Expand Down
5 changes: 3 additions & 2 deletions src/apps/fuzzers/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ such as the H3 core library.

The public API of H3 is covered in the following fuzzers:

| Function | File or status
| -------- | --------------
| Function | File
| -------- | ----
| areNeighborCells | [fuzzerDirectedEdge](./fuzzerDirectedEdge.c)
| cellArea | [fuzzerCellArea](./fuzzerCellArea.c)
| cellToBoundary | [fuzzerCellToLatLng](./fuzzerCellToLatLng.c)
Expand Down Expand Up @@ -60,6 +60,7 @@ The public API of H3 is covered in the following fuzzers:
| localIjToCell | [fuzzerLocalIj](./fuzzerLocalIj.c)
| originToDirectedEdges | [fuzzerDirectedEdge](./fuzzerDirectedEdge.c)
| polygonToCells | [fuzzerPoylgonToCells](./fuzzerPolygonToCells.c)
| polygonToCellsExperimental | [fuzzerPoylgonToCellsExperimental](./fuzzerPolygonToCellsExperimental.c) [fuzzerPoylgonToCellsExperimentalNoHoles](./fuzzerPolygonToCellsExperimentalNoHoles.c)
| radsToDegs | Trivial
| stringToH3 | [fuzzerIndexIO](./fuzzerIndexIO.c)
| uncompactCells | [fuzzerCompact](./fuzzerCompact.c)
Expand Down
16 changes: 10 additions & 6 deletions src/apps/fuzzers/fuzzerPolygonToCells.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2022 Uber Technologies, Inc.
* Copyright 2022-2024 Uber Technologies, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -19,6 +19,7 @@

#include "aflHarness.h"
#include "h3api.h"
#include "polygon.h"
#include "utility.h"

typedef struct {
Expand Down Expand Up @@ -71,7 +72,8 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
int res = args->res % (MAX_RES + 1);

GeoPolygon geoPolygon;
geoPolygon.numHoles = args->numHoles % MAX_HOLES;
int originalNumHoles = args->numHoles % MAX_HOLES;
geoPolygon.numHoles = originalNumHoles;
if (geoPolygon.numHoles < 0) {
return 0;
}
Expand All @@ -88,10 +90,12 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
}
}

// TODO: Fuzz the `flags` input as well when it has meaningful input
run(&geoPolygon, 0, res);
geoPolygon.numHoles = 0;
run(&geoPolygon, 0, res);
for (uint32_t flags = 0; flags < CONTAINMENT_INVALID; flags++) {
geoPolygon.numHoles = originalNumHoles;
run(&geoPolygon, 0, res);
geoPolygon.numHoles = 0;
run(&geoPolygon, 0, res);
}
free(geoPolygon.holes);

return 0;
Expand Down
106 changes: 106 additions & 0 deletions src/apps/fuzzers/fuzzerPolygonToCellsExperimental.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
/*
* Copyright 2023-2024 Uber Technologies, Inc.
*
* 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.
*/
/** @file
* @brief Fuzzer program for polygonToCells2 and related functions
*/

#include "aflHarness.h"
#include "h3api.h"
#include "polyfill.h"
#include "polygon.h"
#include "utility.h"

typedef struct {
int res;
int numHoles;
// repeating: num verts, verts
// We add a large fixed buffer so our test case generator for AFL
// knows how large to make the file.
uint8_t buffer[1024];
} inputArgs;

const int MAX_RES = 15;
const int MAX_SZ = 4000000;
const int MAX_HOLES = 100;

int populateGeoLoop(GeoLoop *g, const uint8_t *data, size_t *offset,
size_t size) {
if (size < *offset + sizeof(int)) {
return 1;
}
int numVerts = *(const int *)(data + *offset);
*offset = *offset + sizeof(int);
g->numVerts = numVerts;
if (size < *offset + sizeof(LatLng) * numVerts) {
return 1;
}
g->verts = (LatLng *)(data + *offset);
*offset = *offset + sizeof(LatLng) * numVerts;
return 0;
}

void run(GeoPolygon *geoPolygon, uint32_t flags, int res) {
int64_t sz;
H3Error err = H3_EXPORT(maxPolygonToCellsSizeExperimental)(geoPolygon, res,
flags, &sz);
if (!err && sz < MAX_SZ) {
H3Index *out = calloc(sz, sizeof(H3Index));
H3_EXPORT(polygonToCellsExperimental)(geoPolygon, res, flags, out);
free(out);
}
}

int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
// TODO: It is difficult for the fuzzer to generate inputs that are
// considered valid by this fuzzer. fuzzerPolygonToCellsNoHoles.c
// is a workaround for that.
if (size < sizeof(inputArgs)) {
return 0;
}
const inputArgs *args = (const inputArgs *)data;
int res = args->res % (MAX_RES + 1);

GeoPolygon geoPolygon;
int originalNumHoles = args->numHoles % MAX_HOLES;
geoPolygon.numHoles = originalNumHoles;
if (geoPolygon.numHoles < 0) {
return 0;
}
geoPolygon.holes = calloc(geoPolygon.numHoles, sizeof(GeoLoop));
size_t offset = sizeof(inputArgs) - sizeof(args->buffer);
if (populateGeoLoop(&geoPolygon.geoloop, data, &offset, size)) {
free(geoPolygon.holes);
return 0;
}
for (int i = 0; i < geoPolygon.numHoles; i++) {
if (populateGeoLoop(&geoPolygon.holes[i], data, &offset, size)) {
free(geoPolygon.holes);
return 0;
}
}

for (uint32_t flags = 0; flags < CONTAINMENT_INVALID; flags++) {
geoPolygon.numHoles = originalNumHoles;
run(&geoPolygon, flags, res);
geoPolygon.numHoles = 0;
run(&geoPolygon, flags, res);
}
free(geoPolygon.holes);

return 0;
}

AFL_HARNESS_MAIN(sizeof(inputArgs));
64 changes: 64 additions & 0 deletions src/apps/fuzzers/fuzzerPolygonToCellsExperimentalNoHoles.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* Copyright 2023-2024 Uber Technologies, Inc.
*
* 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.
*/
/** @file
* @brief Fuzzer program for polygonToCellsExperimental and related functions,
* without holes
*/

#include "aflHarness.h"
#include "h3api.h"
#include "polyfill.h"
#include "polygon.h"
#include "utility.h"

const int MAX_RES = 15;
const int MAX_SZ = 4000000;

void run(GeoPolygon *geoPolygon, uint32_t flags, int res) {
int64_t sz;
H3Error err = H3_EXPORT(maxPolygonToCellsSizeExperimental)(geoPolygon, res,
flags, &sz);
if (!err && sz < MAX_SZ) {
H3Index *out = calloc(sz, sizeof(H3Index));
H3_EXPORT(polygonToCellsExperimental)(geoPolygon, res, flags, out);
free(out);
}
}

int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
if (size < sizeof(int)) {
return 0;
}

uint8_t res = *data;
size_t vertsSize = size - 1;
int numVerts = vertsSize / sizeof(LatLng);

GeoPolygon geoPolygon;
geoPolygon.numHoles = 0;
geoPolygon.holes = NULL;
geoPolygon.geoloop.numVerts = numVerts;
// Offset by 1 since *data was used for `res`, above.
geoPolygon.geoloop.verts = (LatLng *)(data + 1);

for (uint32_t flags = 0; flags < CONTAINMENT_INVALID; flags++) {
run(&geoPolygon, flags, res);
}

return 0;
}

AFL_HARNESS_MAIN(sizeof(H3Index) * 1024);
6 changes: 4 additions & 2 deletions src/apps/fuzzers/fuzzerPolygonToCellsNoHoles.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

#include "aflHarness.h"
#include "h3api.h"
#include "polygon.h"
#include "utility.h"

const int MAX_RES = 15;
Expand Down Expand Up @@ -50,8 +51,9 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
// Offset by 1 since *data was used for `res`, above.
geoPolygon.geoloop.verts = (LatLng *)(data + 1);

// TODO: Fuzz the `flags` input as well when it has meaningful input
run(&geoPolygon, 0, res);
for (uint32_t flags = 0; flags < CONTAINMENT_INVALID; flags++) {
run(&geoPolygon, flags, res);
}

return 0;
}
Expand Down
30 changes: 30 additions & 0 deletions src/apps/testapps/testPolygonToCellsReportedExperimental.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,36 @@
// Tests for specific polygonToCells examples

SUITE(polygonToCells_reported) {
// fuzzer crash due to inconsistent handling of CONTAINMENT_OVERLAPPING
TEST(fuzzer_crash) {
uint8_t data[] = {
0xff, 0xff, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xa, 0x0, 0x0, 0xff,
0xff, 0x0, 0x0, 0x0, 0xa, 0xa, 0xa, 0xa, 0xa, 0xff,
};

uint8_t res = 0;
size_t vertsSize = sizeof(data);
int numVerts = vertsSize / sizeof(LatLng);

GeoPolygon geoPolygon;
geoPolygon.numHoles = 0;
geoPolygon.holes = NULL;
geoPolygon.geoloop.numVerts = numVerts;
// Offset by 1 since *data was used for `res`, above.
geoPolygon.geoloop.verts = (LatLng *)(data);

uint32_t flags = CONTAINMENT_OVERLAPPING;
int64_t sz;
t_assertSuccess(H3_EXPORT(maxPolygonToCellsSizeExperimental)(
&geoPolygon, res, flags, &sz));
t_assert(sz == 1, "Expected output count");
H3Index *out = calloc(sz, sizeof(H3Index));
t_assertSuccess(H3_EXPORT(polygonToCellsExperimental)(&geoPolygon, res,
flags, out));
free(out);
}

// https://github.com/uber/h3-js/issues/76#issuecomment-561204505
TEST(entireWorld) {
// TODO: Fails for a single worldwide polygon
Expand Down
30 changes: 26 additions & 4 deletions src/h3lib/lib/polyfill.c
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,8 @@ void iterStepPolygonCompact(IterCellsPolygonCompact *iter) {

// Target res: Do a fine-grained check
if (cellRes == iter->_res) {
if (mode == CONTAINMENT_CENTER || mode == CONTAINMENT_OVERLAPPING) {
if (mode == CONTAINMENT_CENTER || mode == CONTAINMENT_OVERLAPPING ||
mode == CONTAINMENT_OVERLAPPING_BBOX) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm. These additional checks might get the fuzzer to pass, but they miss the point of CONTAINMENT_OVERLAPPING_BBOX, which is to do a much faster check than CONTAINMENT_OVERLAPPING. The perf impact here is likely significant, since we're going from a fast bbox check to a slow set of polygon-based checks.

Are all of these additional checks needed to make the fuzzer pass? Or can we narrow down to find the check that's actually missed by CONTAINMENT_OVERLAPPING_BBOX?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All but the change around line 511 seem to be necessary to prevent crashes.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I admit that I'm not seeing any significant differences in the benchmarks after this change, assuming I'm running them correctly. I think I'm ok with this to get the new algo out the door, though I'd really like to look through the logic here and understand what's missing in the bbox check - it doesn't make a lot of sense to me conceptually that these would work when the bbox doesn't, so we must be messing up the bbox check in some way.

// Check if the cell center is inside the polygon
LatLng center;
H3Error centerErr = H3_EXPORT(cellToLatLng)(cell, &center);
Expand All @@ -448,7 +449,8 @@ void iterStepPolygonCompact(IterCellsPolygonCompact *iter) {
return;
}
}
if (mode == CONTAINMENT_OVERLAPPING) {
if (mode == CONTAINMENT_OVERLAPPING ||
mode == CONTAINMENT_OVERLAPPING_BBOX) {
// For overlapping, we need to do a quick check to determine
// whether the polygon is wholly contained by the cell. We
// check the first polygon vertex, which if it is contained
Expand Down Expand Up @@ -477,7 +479,8 @@ void iterStepPolygonCompact(IterCellsPolygonCompact *iter) {
}
}
}
if (mode == CONTAINMENT_FULL || mode == CONTAINMENT_OVERLAPPING) {
if (mode == CONTAINMENT_FULL || mode == CONTAINMENT_OVERLAPPING ||
mode == CONTAINMENT_OVERLAPPING_BBOX) {
CellBoundary boundary;
H3Error boundaryErr =
H3_EXPORT(cellToBoundary)(cell, &boundary);
Expand All @@ -494,7 +497,8 @@ void iterStepPolygonCompact(IterCellsPolygonCompact *iter) {
return;
}
// Check if the cell is fully contained by the polygon
if (mode == CONTAINMENT_FULL &&
if ((mode == CONTAINMENT_FULL ||
mode == CONTAINMENT_OVERLAPPING_BBOX) &&
cellBoundaryInsidePolygon(iter->_polygon, iter->_bboxes,
&boundary, &bbox)) {
// Set to next output
Expand Down Expand Up @@ -692,9 +696,27 @@ void iterDestroyPolygon(IterCellsPolygon *iter) {
H3Error H3_EXPORT(polygonToCellsExperimental)(const GeoPolygon *polygon,
int res, uint32_t flags,
H3Index *out) {
#ifdef H3_POLYGON_TO_CELLS_ASSERT
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we comment on how to set this variable? Can we use the TESTONLY macro here instead?

// TODO: This is incompatible with testH3Memory, since it will make more
// allocations. This is just for debugging that the algorithm is not
// exceeding its buffer size.
int64_t maxSize;
H3Error sizeError = H3_EXPORT(maxPolygonToCellsSizeExperimental)(
polygon, res, flags, &maxSize);
if (sizeError) {
return sizeError;
}
#endif

IterCellsPolygon iter = iterInitPolygon(polygon, res, flags);
int64_t i = 0;
for (; iter.cell; iterStepPolygon(&iter)) {
#ifdef H3_POLYGON_TO_CELLS_ASSERT
if (NEVER(i >= maxSize)) {
iterDestroyPolygon(&iter);
return E_FAILED;
}
#endif
out[i++] = iter.cell;
}
return iter.error;
Expand Down
Loading
Loading