This is an archive of the discontinued LLVM Phabricator instance.

[compiler-rt] Avoid signed shift overflow in __muloXi4 and __mulvXi3
ClosedPublic

Authored by Ka-Ka on Mar 8 2023, 1:27 AM.

Details

Summary

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

UBSan: int_mulo_impl.inc:21:36: left shift of 1 by 63 places cannot be represented in type 'di_int' (aka 'long long')

This can be avoided by simply doing the shift in a matching unsigned variant of
the type.

The same kind of pattern seems to exist in int_mulv_impl.inc

This was found in an out of tree target.

Diff Detail

Event Timeline

Ka-Ka created this revision.Mar 8 2023, 1:27 AM
Herald added a project: Restricted Project. · View Herald TranscriptMar 8 2023, 1:27 AM
Herald added subscribers: Enna1, dberris. · View Herald Transcript
Ka-Ka requested review of this revision.Mar 8 2023, 1:27 AM
Herald added a project: Restricted Project. · View Herald TranscriptMar 8 2023, 1:27 AM
Herald added a subscriber: Restricted Project. · View Herald Transcript
uabelho added a subscriber: uabelho.Mar 8 2023, 1:44 AM
bjope added a subscriber: bjope.Mar 8 2023, 2:09 AM
Ka-Ka added inline comments.Mar 8 2023, 2:24 AM
compiler-rt/lib/builtins/int_mulo_impl.inc
20–22

An alternative implementation would be to change MIN and MAX into:

const fixint_t MAX = (fixint_t)((~(fixuint_t)0) / 2);
const fixint_t MIN = -MAX - 1;

as this code pattern is used in another part of compiler-rt in compiler-rt/lib/builtins/fp_fixint_impl.inc

MaskRay accepted this revision.Mar 8 2023, 4:02 PM
This revision is now accepted and ready to land.Mar 8 2023, 4:02 PM