Skip to content

Commit 5ae3211

Browse files
committed
[builtins] Fix signed shift overflows in ashlti3.c, ashrti3.c, ashldi3.c and ashrdi3.c
When compiling the builtins with the undefined behavior sanitizer and running testcases you end up with the following warning: UBSan: ashlti3.c:33:35: left shift of 1 by 63 places cannot be represented in type 'di_int' (aka 'long long') UBSan: ashrti3.c:34:34: left shift of negative value -81985529216486891 This can be avoided by doing the shift in a matching unsigned variant of the type. The same kind of patterns are found in ashldi3.c and ashrdi3.c This was found in an out of tree target. Reviewed By: MaskRay Differential Revision: https://reviews.llvm.org/D158819
1 parent fb46377 commit 5ae3211

File tree

4 files changed

+8
-4
lines changed

4 files changed

+8
-4
lines changed

compiler-rt/lib/builtins/ashldi3.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ COMPILER_RT_ABI di_int __ashldi3(di_int a, int b) {
2828
if (b == 0)
2929
return a;
3030
result.s.low = input.s.low << b;
31-
result.s.high = (input.s.high << b) | (input.s.low >> (bits_in_word - b));
31+
result.s.high =
32+
((su_int)input.s.high << b) | (input.s.low >> (bits_in_word - b));
3233
}
3334
return result.all;
3435
}

compiler-rt/lib/builtins/ashlti3.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ COMPILER_RT_ABI ti_int __ashlti3(ti_int a, int b) {
3030
if (b == 0)
3131
return a;
3232
result.s.low = input.s.low << b;
33-
result.s.high = (input.s.high << b) | (input.s.low >> (bits_in_dword - b));
33+
result.s.high =
34+
((du_int)input.s.high << b) | (input.s.low >> (bits_in_dword - b));
3435
}
3536
return result.all;
3637
}

compiler-rt/lib/builtins/ashrdi3.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ COMPILER_RT_ABI di_int __ashrdi3(di_int a, int b) {
2929
if (b == 0)
3030
return a;
3131
result.s.high = input.s.high >> b;
32-
result.s.low = (input.s.high << (bits_in_word - b)) | (input.s.low >> b);
32+
result.s.low =
33+
((su_int)input.s.high << (bits_in_word - b)) | (input.s.low >> b);
3334
}
3435
return result.all;
3536
}

compiler-rt/lib/builtins/ashrti3.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ COMPILER_RT_ABI ti_int __ashrti3(ti_int a, int b) {
3131
if (b == 0)
3232
return a;
3333
result.s.high = input.s.high >> b;
34-
result.s.low = (input.s.high << (bits_in_dword - b)) | (input.s.low >> b);
34+
result.s.low =
35+
((du_int)input.s.high << (bits_in_dword - b)) | (input.s.low >> b);
3536
}
3637
return result.all;
3738
}

0 commit comments

Comments
 (0)