-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Add overflow detection with preview opt-in #7206
Add overflow detection with preview opt-in #7206
Conversation
Is there any way to make this multiple PRs? For example, a PR to add unsafe casts, a PR to update the stdlib, a PR to enable the option for standard operators to raise on overflow? Nearly 50 commits is a bit too much |
934042a
to
2a679bf
Compare
Once the build is happy I will split in probably 3 PRs.
The Int128 support for 32 bits or even in linux is causing some issues. |
Don't bother with |
fa087dc
to
efb0f0f
Compare
CircleCI is green! Travis has timed out. The arithmetic_specs are quite slow because prelude is included. I need to polish last commits but it seems that the only part of compiler-rt that is needed for 128 bits ints can be defined in crystal. Since needing it would depend on platform/arch/llvm(?) i think that a flag I can’t avoid supporting 128bits in 32bits unless lots of specs are disabled or the whole existence of 128bits are excluded on a flag. This is because Int will be a union of all Ints and some code will requiere int128 mults. So fixing the support of 128bits on 32 bits seems to be the best option. |
I didn't have time to review this yet, but I will during this week (but feel free to merge it if you all think it's good) |
@bcardiff Turns out that I'm worried that introducing this symbol ourselves will break linking on platforms which link compiler-rt by default. |
Actually, it's a 64bit multiply with overflow checking used for the i128 support, weird. |
a658b3f
to
b807ce6
Compare
5fd3064
to
f3171f4
Compare
First stage of the PR split at #7226 |
b59a051
to
d89e663
Compare
Second stage of the PR split at #7256. |
Third stage of the PR split at #7262 . The fourth and will be this PR after a merge & rebase. |
@bcardiff the CI failure happened twice now on different CIs, I think you should run the spec in a loop and see if you can get a repro. |
for exact location information of the exception in the caller context
* include overflow check before truncation
required for 64 bits overflow in 32 bits arch, unless compiler-rt is built and linked.
Running Int128 specs in linux requires more compiler_rt symbols than __mulodi4
Allow :cast primitive to infer unchecked conversion from method ! suffix Prepare :convert and :unsafe_convert primitives for better separation Leave a codegen_cast that is used from codegen/cast.cr
-D compiler_rt is not needed in all platforms actually but trying to keep configuration simpler
3f32aeb
to
b66c6e7
Compare
I squashed the compiler_rt fix. For the record the largest number I got was when creating debug info for LibC::PthreadMutexT an offset of 4299161654. Which is still far from the overflow point. Let's see what the CI thinks... 🍿 |
This PR adds overflow checks for
+
-
*
, to number conversionsto_iX
,to_uX
,to_fX
and so to constructs likeInt32.new(value)
.The raised exception will contain, when possible, the location of the method/operator that originate it.
It adds some new methods to be able to keep previous wrapping behaviour.(Split at: #7226)to_iX!
/Int32.new!(value)
. This methods should be used to be ready for upcoming breaking changes.It prepares some new primitives to be able to have cleaner compiler code in the future. That is drop the(Split at: #7226):cast
internal in favor of:convert
and:unchecked_convert
to reflect better the nature ofto_X
/to_X!
methods.There are many refactors to the stdlib(Split at #7256) andcompiler code(Split at #7262)where the wrapping/unsafe behaviour was needed. Although some changes might look odd they should be reviewed as keeping the current code since operators and methods will be changing in a future version. Some changes show a need for future refactor (JSON/YAML parsing for dealing with UInt64, Enum flags with UInt64 base types, conversions for big types). In future version the overflow check on Time and other can be simplified.In order to avoid breaking changes and to allow a preview of this feature, the overflow behavior is only enabled if compiled with
-D preview_overflow
.I checked locally that even a 2nd compiler generation pass the specs. I did catch some left overs with that.
Closes #6223
Closes #3103
Sample