-
Notifications
You must be signed in to change notification settings - Fork 457
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
Merged
isaacbrodsky
merged 8 commits into
uber:master
from
isaacbrodsky:fix-upap7-signed-integer-overflow
Dec 22, 2022
Merged
Changes from 7 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
c8aba9b
Fix possible signed integer overflow in ijToIjk
isaacbrodsky 2bf71c7
Update changelog
isaacbrodsky 1309956
reorder
isaacbrodsky 91b84bf
add _upAp7Checked and _upAp7rChecked for processing user input
isaacbrodsky d5e49e5
Add PR number to changelog
isaacbrodsky a845939
update with macro tests
isaacbrodsky 2f3952e
force k to 0 in fuzzer
isaacbrodsky 5e1e2cd
Change per review: add an overall bounds check around the overflow ch…
isaacbrodsky File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/* | ||
* 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; | ||
ijkCopy3.k = 0; | ||
if (!_ijkNormalizeCouldOverflow(&ijkCopy3)) { | ||
_ijkNormalize(&ijkCopy3); | ||
} | ||
|
||
return 0; | ||
} | ||
|
||
AFL_HARNESS_MAIN(sizeof(inputArgs)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Nit:
SUM
andSUB
look nearly identical so I didn't even notice they were different macros in the conditionals below at first. Maybe this one can be: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.
That's reasonable. Renamed to ADD_...