Coverage Report

Created: 2023-09-25 06:54

/src/h3/src/apps/fuzzers/fuzzerGridDisk.c
Line
Count
Source
1
/*
2
 * Copyright 2021 Uber Technologies, Inc.
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
/** @file
17
 * @brief Fuzzer program for gridDisk
18
 */
19
20
#include "aflHarness.h"
21
#include "h3api.h"
22
#include "utility.h"
23
24
typedef struct {
25
    H3Index index;
26
    int k;
27
} inputArgs;
28
29
// This is limited to avoid timeouts due to the runtime of gridDisk growing with
30
// k
31
const int64_t MAX_GRID_DISK_SIZE = 10000;
32
33
476
int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
34
476
    if (size < sizeof(inputArgs)) {
35
6
        return 0;
36
6
    }
37
470
    const inputArgs *args = (const inputArgs *)data;
38
39
470
    int64_t sz;
40
470
    H3Error err = H3_EXPORT(maxGridDiskSize)(args->k, &sz);
41
470
    if (err || sz > MAX_GRID_DISK_SIZE || sz < 0) {
42
        // Can't allocate - do not test for cases where sz is extremely large
43
        // because this is expected to hang, and do not test for cases where
44
        // sizeof(H3Index) * sz overflows (this is undefined behavior per C
45
        // and will stop the fuzzer from making progress).
46
88
        return 0;
47
88
    }
48
382
    H3Index *results = calloc(sizeof(H3Index), sz);
49
382
    if (results != NULL) {
50
382
        H3_EXPORT(gridDisk)(args->index, args->k, results);
51
382
    }
52
382
    free(results);
53
54
382
    results = calloc(sizeof(H3Index), sz);
55
382
    if (results != NULL) {
56
382
        H3_EXPORT(gridDiskUnsafe)(args->index, args->k, results);
57
382
    }
58
382
    free(results);
59
60
    // TODO: use int64_t
61
382
    int *distances = calloc(sizeof(int), sz);
62
382
    results = calloc(sizeof(H3Index), sz);
63
382
    if (results != NULL && distances != NULL) {
64
382
        H3_EXPORT(gridDiskDistancesUnsafe)
65
382
        (args->index, args->k, results, distances);
66
382
    }
67
382
    free(results);
68
382
    free(distances);
69
70
382
    distances = calloc(sizeof(int), sz);
71
382
    results = calloc(sizeof(H3Index), sz);
72
382
    if (results != NULL && distances != NULL) {
73
382
        H3_EXPORT(gridDiskDistancesSafe)
74
382
        (args->index, args->k, results, distances);
75
382
    }
76
382
    free(results);
77
382
    free(distances);
78
79
382
    distances = calloc(sizeof(int), sz);
80
382
    results = calloc(sizeof(H3Index), sz);
81
382
    if (results != NULL && distances != NULL) {
82
382
        H3_EXPORT(gridDiskDistances)(args->index, args->k, results, distances);
83
382
    }
84
382
    free(results);
85
382
    free(distances);
86
87
382
    results = calloc(sizeof(H3Index), sz);
88
382
    if (results != NULL) {
89
382
        H3_EXPORT(gridRingUnsafe)(args->index, args->k, results);
90
382
    }
91
382
    free(results);
92
93
382
    size_t length = (size - sizeof(inputArgs)) / sizeof(H3Index);
94
382
    if (sz * length > MAX_GRID_DISK_SIZE) {
95
        // Can't allocate.
96
25
        return 0;
97
25
    }
98
357
    results = calloc(sizeof(H3Index), sz * length);
99
357
    if (results != NULL) {
100
357
        H3Index *h3Set = (H3Index *)(data + sizeof(inputArgs));
101
357
        H3_EXPORT(gridDisksUnsafe)(h3Set, length, args->k, results);
102
357
    }
103
357
    free(results);
104
357
    return 0;
105
382
}
106
107
AFL_HARNESS_MAIN(sizeof(inputArgs));