-
Notifications
You must be signed in to change notification settings - Fork 470
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
Fix upAp7 signed integer overflow #735
Changes from 6 commits
c8aba9b
2bf71c7
1309956
91b84bf
d5e49e5
a845939
2f3952e
5e1e2cd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/* | ||
* Copyright 2022 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 internal functions in coordijk.c | ||
*/ | ||
|
||
#include "aflHarness.h" | ||
#include "algos.h" | ||
#include "h3api.h" | ||
#include "utility.h" | ||
|
||
typedef struct { | ||
CoordIJK ijk; | ||
} inputArgs; | ||
|
||
int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { | ||
if (size < sizeof(inputArgs)) { | ||
return 0; | ||
} | ||
const inputArgs *args = (const inputArgs *)data; | ||
|
||
// These functions require that input be non-negative | ||
if (args->ijk.i >= 0 && args->ijk.j >= 0 && args->ijk.k >= 0) { | ||
CoordIJK ijkCopy1 = args->ijk; | ||
_upAp7Checked(&ijkCopy1); | ||
CoordIJK ijkCopy2 = args->ijk; | ||
_upAp7rChecked(&ijkCopy2); | ||
} | ||
// This function needs a guard check to be safe and that guard | ||
// check assumes k = 0. | ||
CoordIJK ijkCopy3 = args->ijk; | ||
if (ijkCopy3.k == 0 && !_ijkNormalizeCouldOverflow(&ijkCopy3)) { | ||
_ijkNormalize(&ijkCopy3); | ||
} | ||
|
||
return 0; | ||
} | ||
|
||
AFL_HARNESS_MAIN(sizeof(inputArgs)); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
/* | ||
* Copyright 2022 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 tests functions and macros in mathExtensions.h | ||
* | ||
* usage: `testMathExtensions` | ||
*/ | ||
|
||
#include "mathExtensions.h" | ||
#include "test.h" | ||
#include "utility.h" | ||
|
||
#define ASSERT_SUB_OVERFLOW(a, b) \ | ||
t_assert(SUB_INT32S_OVERFLOWS((a), (b)), #a " - " #b " Overflows") | ||
#define ASSERT_SUB_DOES_NOT_OVERFLOW(a, b) \ | ||
t_assert(!SUB_INT32S_OVERFLOWS((a), (b)), #a " - " #b \ | ||
" Does Not " \ | ||
"Overflow") | ||
#define ASSERT_SUM_OVERFLOW(a, b) \ | ||
t_assert(SUM_INT32S_OVERFLOWS((a), (b)), #a " + " #b " Overflows") | ||
#define ASSERT_SUM_DOES_NOT_OVERFLOW(a, b) \ | ||
t_assert(!SUM_INT32S_OVERFLOWS((a), (b)), #a " + " #b \ | ||
" Does Not " \ | ||
"Overflow") | ||
|
||
SUITE(mathExtensions) { | ||
TEST(_ipow) { | ||
t_assert(_ipow(7, 0) == 1, "7 ** 0 == 1"); | ||
t_assert(_ipow(7, 1) == 7, "7 ** 1 == 7"); | ||
t_assert(_ipow(7, 2) == 49, "7 ** 2 == 49"); | ||
t_assert(_ipow(1, 20) == 1, "1 ** 20 == 1"); | ||
t_assert(_ipow(2, 5) == 32, "2 ** 5 == 32"); | ||
} | ||
|
||
TEST(subOverflows) { | ||
ASSERT_SUB_DOES_NOT_OVERFLOW(0, 0); | ||
ASSERT_SUB_DOES_NOT_OVERFLOW(INT32_MIN, 0); | ||
ASSERT_SUB_OVERFLOW(INT32_MIN, 1); | ||
ASSERT_SUB_DOES_NOT_OVERFLOW(INT32_MIN, -1); | ||
ASSERT_SUB_DOES_NOT_OVERFLOW(INT32_MIN + 1, 0); | ||
ASSERT_SUB_DOES_NOT_OVERFLOW(INT32_MIN + 1, 1); | ||
ASSERT_SUB_DOES_NOT_OVERFLOW(INT32_MIN + 1, -1); | ||
ASSERT_SUB_OVERFLOW(INT32_MIN + 1, 2); | ||
ASSERT_SUB_DOES_NOT_OVERFLOW(INT32_MIN + 1, -2); | ||
ASSERT_SUB_DOES_NOT_OVERFLOW(100, 10); | ||
ASSERT_SUB_DOES_NOT_OVERFLOW(INT32_MAX, 0); | ||
ASSERT_SUB_DOES_NOT_OVERFLOW(INT32_MAX, 1); | ||
ASSERT_SUB_OVERFLOW(INT32_MAX, -1); | ||
ASSERT_SUB_DOES_NOT_OVERFLOW(INT32_MAX - 1, 1); | ||
ASSERT_SUB_DOES_NOT_OVERFLOW(INT32_MAX - 1, -1); | ||
ASSERT_SUB_OVERFLOW(INT32_MAX - 1, -2); | ||
ASSERT_SUB_OVERFLOW(INT32_MAX - 1, -2); | ||
ASSERT_SUB_OVERFLOW(INT32_MIN, INT32_MAX); | ||
ASSERT_SUB_OVERFLOW(INT32_MAX, INT32_MIN); | ||
ASSERT_SUB_DOES_NOT_OVERFLOW(INT32_MIN, INT32_MIN); | ||
ASSERT_SUB_DOES_NOT_OVERFLOW(INT32_MAX, INT32_MAX); | ||
ASSERT_SUB_DOES_NOT_OVERFLOW(-1, 0); | ||
ASSERT_SUB_DOES_NOT_OVERFLOW(-1, 10); | ||
ASSERT_SUB_DOES_NOT_OVERFLOW(-1, -10); | ||
ASSERT_SUB_DOES_NOT_OVERFLOW(-1, INT32_MAX); | ||
ASSERT_SUB_OVERFLOW(-2, INT32_MAX); | ||
ASSERT_SUB_DOES_NOT_OVERFLOW(-1, INT32_MIN); | ||
ASSERT_SUB_OVERFLOW(0, INT32_MIN); | ||
} | ||
|
||
TEST(sumOverflows) { | ||
ASSERT_SUM_DOES_NOT_OVERFLOW(0, 0); | ||
ASSERT_SUM_DOES_NOT_OVERFLOW(INT32_MIN, 0); | ||
ASSERT_SUM_DOES_NOT_OVERFLOW(INT32_MIN, 1); | ||
ASSERT_SUM_OVERFLOW(INT32_MIN, -1); | ||
ASSERT_SUM_DOES_NOT_OVERFLOW(INT32_MIN + 1, 0); | ||
ASSERT_SUM_DOES_NOT_OVERFLOW(INT32_MIN + 1, 1); | ||
ASSERT_SUM_DOES_NOT_OVERFLOW(INT32_MIN + 1, -1); | ||
ASSERT_SUM_DOES_NOT_OVERFLOW(INT32_MIN + 1, 2); | ||
ASSERT_SUM_OVERFLOW(INT32_MIN + 1, -2); | ||
ASSERT_SUM_DOES_NOT_OVERFLOW(100, 10); | ||
ASSERT_SUM_DOES_NOT_OVERFLOW(INT32_MAX, 0); | ||
ASSERT_SUM_OVERFLOW(INT32_MAX, 1); | ||
ASSERT_SUM_DOES_NOT_OVERFLOW(INT32_MAX, -1); | ||
ASSERT_SUM_DOES_NOT_OVERFLOW(INT32_MAX - 1, 1); | ||
ASSERT_SUM_DOES_NOT_OVERFLOW(INT32_MAX - 1, -1); | ||
ASSERT_SUM_DOES_NOT_OVERFLOW(INT32_MAX - 1, -2); | ||
ASSERT_SUM_OVERFLOW(INT32_MAX - 1, 2); | ||
ASSERT_SUM_DOES_NOT_OVERFLOW(INT32_MIN, INT32_MAX); | ||
ASSERT_SUM_DOES_NOT_OVERFLOW(INT32_MAX, INT32_MIN); | ||
ASSERT_SUM_OVERFLOW(INT32_MAX, INT32_MAX); | ||
ASSERT_SUM_OVERFLOW(INT32_MIN, INT32_MIN); | ||
ASSERT_SUM_DOES_NOT_OVERFLOW(-1, 0); | ||
ASSERT_SUM_DOES_NOT_OVERFLOW(-1, 10); | ||
ASSERT_SUM_DOES_NOT_OVERFLOW(-1, -10); | ||
ASSERT_SUM_DOES_NOT_OVERFLOW(-1, INT32_MAX); | ||
ASSERT_SUM_DOES_NOT_OVERFLOW(-2, INT32_MAX); | ||
ASSERT_SUM_OVERFLOW(-1, INT32_MIN); | ||
ASSERT_SUM_DOES_NOT_OVERFLOW(0, INT32_MIN); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -1,5 +1,5 @@ | ||||||
/* | ||||||
* Copyright 2017-2018 Uber Technologies, Inc. | ||||||
* Copyright 2017-2018, 2022 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. | ||||||
|
@@ -27,6 +27,14 @@ | |||||
*/ | ||||||
#define MAX(a, b) (((a) > (b)) ? (a) : (b)) | ||||||
|
||||||
/** Evaluates to true if a + b would overflow for int32 */ | ||||||
#define SUM_INT32S_OVERFLOWS(a, b) \ | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit:
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's reasonable. Renamed to ADD_... |
||||||
((a) > 0 ? (INT32_MAX - (a) < (b)) : (INT32_MIN - (a) > (b))) | ||||||
|
||||||
/** Evaluates to true if a - b would overflow for int32 */ | ||||||
#define SUB_INT32S_OVERFLOWS(a, b) \ | ||||||
((a) >= 0 ? (INT32_MIN + (a) >= (b)) : (INT32_MAX + (a) + 1 < (b))) | ||||||
|
||||||
// Internal functions | ||||||
int64_t _ipow(int64_t base, int64_t exp); | ||||||
|
||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would it make more sense to actually set
ijkCopy3.k = 0
, rather than discarding all random input where this was not the case?