This repository was archived by the owner on Jan 23, 2023. It is now read-only.
Repository navigation
Use safe arithmetic for memory allocation in Unix shims - #13553
Merged
Merged
Conversation
We'd like to be able to use builtins for overflow checking, but the relevant builtins (e.g. __builtin_mul_overflow) aren't available with the compiler versions we currently use on all platforms. In the meantime, we'll use the ClrSafeInt implementation from coreclr. This commit brings over the files, but they don't yet compile.
Made a minimal set of changes to get safemath/debugmacrosext to compile on Linux and macOS with the compiler versions being used in the repo, the default warnings enabled, and the lack of other files that coreclr depends on.
Use the builtins when available, otherwise fall back to using ClrSafeInt.
Let's the language handle overflow detection.
jkotas
approved these changes
Nov 11, 2016
Member
|
LGTM :) |
This file contains hidden or 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
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
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 are a handful of places in the corefx shims where we use malloc with the result of some arithmetic operation. This PR fixes those to ensure we're not overflowing.
Where I could, I simply switched to using new to allocate an array, letting the compiler handle any overflows. For the rest of the cases, I added two helpers, multiply_s and add_s, that are used to do the operations safely. Where possible, these are implemented using __builtin_mul_overflow and __builtin_add_overflow. For when we're using too old a version of clang/gcc to have those, I've copied over coreclr's ClrSafeInt (with minimal changes to get it to compile, e.g. using static_cast instead of C-style casts) and use that... I kept the original copy and the changes to the copy in separate commits to make it easy to see the diff from coreclr.
cc: @jkotas, @bartonjs
Fixes https://github.com/dotnet/corefx/issues/13385