This is an archive of the discontinued LLVM Phabricator instance.

[builtins] Avoid undefined behavior when calculating absolute value in floatsidf.c and floatsisf.c
ClosedPublic

Authored by Ka-Ka on Mar 15 2023, 3:27 AM.

Details

Summary

When compiling compiler-rt with -fsanitize=undefined and running testcases you
end up with the following warning:

UBSan: floatsidf.c:32:9: negation of -2147483648 cannot be represented in type 'si_int' (aka 'long'); cast to an unsigned type to negate this value to itself

The same kind of pattern exists in floatsisf.c

This was found in an out of tree target.

Diff Detail

Event Timeline

Ka-Ka created this revision.Mar 15 2023, 3:27 AM
Herald added a project: Restricted Project. · View Herald TranscriptMar 15 2023, 3:27 AM
Herald added subscribers: Enna1, dberris. · View Herald Transcript
Ka-Ka requested review of this revision.Mar 15 2023, 3:27 AM
Herald added a project: Restricted Project. · View Herald TranscriptMar 15 2023, 3:27 AM
Herald added a subscriber: Restricted Project. · View Herald Transcript
Ka-Ka edited the summary of this revision. (Show Details)Mar 15 2023, 8:48 AM
MaskRay added inline comments.Aug 21 2023, 11:51 PM
compiler-rt/lib/builtins/floatsidf.c
33

Just use unary -: -(su_int)a

compiler-rt/lib/builtins/floatsisf.c
33

ditto

[compiler-rt]

[builtins] should be more specific. For many projects, we don't use the top-level project name as the tag (which can be simplified inferred from the toplevel path name, so less useful).

Ka-Ka updated this revision to Diff 552268.Aug 22 2023, 1:59 AM
Ka-Ka marked 2 inline comments as done.

The patch was rebased and updated according to comments.

Ka-Ka retitled this revision from [compiler-rt] Avoid undefined behavior when calculating absolute value in floatsidf.c and floatsisf.c to [builtins] Avoid undefined behavior when calculating absolute value in floatsidf.c and floatsisf.c.Aug 22 2023, 2:00 AM
Ka-Ka edited the summary of this revision. (Show Details)
MaskRay accepted this revision.Aug 22 2023, 10:53 AM

Sorry for just noticing the patch. https://llvm.org/docs/CodeReview.html#lgtm-how-a-patch-is-accepted feel free to ping a patch when there is no response after a week :)

This revision is now accepted and ready to land.Aug 22 2023, 10:53 AM
MaskRay added inline comments.Aug 22 2023, 10:55 AM
compiler-rt/lib/builtins/floatsidf.c
33

I think aAbs = -aAbs can be used as well, not sure which is clearer.
aAbs = -aAbs avoids a type conversion.

This revision was automatically updated to reflect the committed changes.
Ka-Ka marked an inline comment as done.

Sorry for just noticing the patch. https://llvm.org/docs/CodeReview.html#lgtm-how-a-patch-is-accepted feel free to ping a patch when there is no response after a week :)

No problem. I didn't ping the review as this patch was not that important for us, as we in our of tree target have started to, as a workaround, compile compiler-rt with -fwrapv to avoid these kind of problems. I still think there are several signed integer overflow problem left to fix in the builtins in compiler-rt. If it is important to fix those I can probably take another look at this, as we in the future want to remove the use of -fwrapv.

Sorry for just noticing the patch. https://llvm.org/docs/CodeReview.html#lgtm-how-a-patch-is-accepted feel free to ping a patch when there is no response after a week :)

No problem. I didn't ping the review as this patch was not that important for us, as we in our of tree target have started to, as a workaround, compile compiler-rt with -fwrapv to avoid these kind of problems. I still think there are several signed integer overflow problem left to fix in the builtins in compiler-rt. If it is important to fix those I can probably take another look at this, as we in the future want to remove the use of -fwrapv.

Sounds good! How do you build lib/builtins with -fsanitize=undefined?

compiler-rt provides runtime library of sanitizers and many components cannot be instrumented by sanitizers.

Building lib/builtins with non-trap-mode -fsanitize=undefined has a layering issue: sanitizer libraries depend on builtins, so builtins should not depend on sanitizer libraries.
Though with lld's archive semantics this violation is probably fine.

Ka-Ka added a comment.Aug 23 2023, 1:31 AM

Sorry for just noticing the patch. https://llvm.org/docs/CodeReview.html#lgtm-how-a-patch-is-accepted feel free to ping a patch when there is no response after a week :)

No problem. I didn't ping the review as this patch was not that important for us, as we in our of tree target have started to, as a workaround, compile compiler-rt with -fwrapv to avoid these kind of problems. I still think there are several signed integer overflow problem left to fix in the builtins in compiler-rt. If it is important to fix those I can probably take another look at this, as we in the future want to remove the use of -fwrapv.

Sounds good! How do you build lib/builtins with -fsanitize=undefined?

compiler-rt provides runtime library of sanitizers and many components cannot be instrumented by sanitizers.

Building lib/builtins with non-trap-mode -fsanitize=undefined has a layering issue: sanitizer libraries depend on builtins, so builtins should not depend on sanitizer libraries.
Though with lld's archive semantics this violation is probably fine.

I build in our out of tree target with -fsanitize=undefined and our own adapted -fsanitize-minimal-runtime.