* [PATCH] Backport ubsan fix to 4.9
@ 2015-03-10 17:11 Marek Polacek
2015-03-11 10:33 ` Richard Biener
0 siblings, 1 reply; 2+ messages in thread
From: Marek Polacek @ 2015-03-10 17:11 UTC (permalink / raw)
To: GCC Patches; +Cc: Richard Biener, Jakub Jelinek
I'd like to backport the following patch that suppresses bogus ubsan errors.
I had to tweak the testcase a bit since 4.9 doesn't know -fno-sanitize-recover.
Bootstrapped/regtested on x86_64-linux, ok for 4.9?
2015-03-10 Marek Polacek <polacek@redhat.com>
Backported from mainline
2014-12-04 Marek Polacek <polacek@redhat.com>
PR middle-end/56917
* fold-const.c (fold_unary_loc): Perform the negation in A's type
when transforming ~ (A - 1) or ~ (A + -1) to -A.
* c-c++-common/ubsan/pr56917.c: New test.
--- gcc/fold-const.c
+++ gcc/fold-const.c
@@ -8324,9 +8324,14 @@ fold_unary_loc (location_t loc, enum tree_code code, tree type, tree op0)
&& integer_onep (TREE_OPERAND (arg0, 1)))
|| (TREE_CODE (arg0) == PLUS_EXPR
&& integer_all_onesp (TREE_OPERAND (arg0, 1)))))
- return fold_build1_loc (loc, NEGATE_EXPR, type,
- fold_convert_loc (loc, type,
- TREE_OPERAND (arg0, 0)));
+ {
+ /* Perform the negation in ARG0's type and only then convert
+ to TYPE as to avoid introducing undefined behavior. */
+ tree t = fold_build1_loc (loc, NEGATE_EXPR,
+ TREE_TYPE (TREE_OPERAND (arg0, 0)),
+ TREE_OPERAND (arg0, 0));
+ return fold_convert_loc (loc, type, t);
+ }
/* Convert ~(X ^ Y) to ~X ^ Y or X ^ ~Y if ~X or ~Y simplify. */
else if (TREE_CODE (arg0) == BIT_XOR_EXPR
&& (tem = fold_unary_loc (loc, BIT_NOT_EXPR, type,
--- gcc/testsuite/c-c++-common/ubsan/pr56917.c
+++ gcc/testsuite/c-c++-common/ubsan/pr56917.c
@@ -0,0 +1,43 @@
+/* PR middle-end/56917 */
+/* { dg-do run } */
+/* { dg-options "-fsanitize=undefined" } */
+
+#include <stdio.h>
+
+#define INT_MIN (-__INT_MAX__ - 1)
+#define LONG_MIN (-__LONG_MAX__ - 1L)
+#define LLONG_MIN (-__LONG_LONG_MAX__ - 1LL)
+
+int __attribute__ ((noinline,noclone))
+fn1 (unsigned int u)
+{
+ return (-(int) (u - 1U)) - 1;
+}
+
+long __attribute__ ((noinline,noclone))
+fn2 (unsigned long int ul)
+{
+ return (-(long) (ul - 1UL)) - 1L;
+}
+
+long long __attribute__ ((noinline,noclone))
+fn3 (unsigned long long int ull)
+{
+ return (-(long long) (ull - 1ULL)) - 1LL;
+}
+
+int
+main (void)
+{
+ fputs ("UBSAN TEST START\n", stderr);
+
+ if (fn1 (__INT_MAX__ + 1U) != INT_MIN
+ || fn2 (__LONG_MAX__ + 1UL) != LONG_MIN
+ || fn3 (__LONG_LONG_MAX__ + 1ULL) != LLONG_MIN)
+ __builtin_abort ();
+
+ fputs ("UBSAN TEST END\n", stderr);
+ return 0;
+}
+
+/* { dg-output "UBSAN TEST START(\n|\r\n|\r)UBSAN TEST END" } */
Marek
^ permalink raw reply [flat|nested] 2+ messages in thread
* Re: [PATCH] Backport ubsan fix to 4.9
2015-03-10 17:11 [PATCH] Backport ubsan fix to 4.9 Marek Polacek
@ 2015-03-11 10:33 ` Richard Biener
0 siblings, 0 replies; 2+ messages in thread
From: Richard Biener @ 2015-03-11 10:33 UTC (permalink / raw)
To: Marek Polacek; +Cc: GCC Patches, Jakub Jelinek
On Tue, 10 Mar 2015, Marek Polacek wrote:
> I'd like to backport the following patch that suppresses bogus ubsan errors.
> I had to tweak the testcase a bit since 4.9 doesn't know -fno-sanitize-recover.
>
> Bootstrapped/regtested on x86_64-linux, ok for 4.9?
Ok.
Thanks,
Richard.
> 2015-03-10 Marek Polacek <polacek@redhat.com>
>
> Backported from mainline
> 2014-12-04 Marek Polacek <polacek@redhat.com>
>
> PR middle-end/56917
> * fold-const.c (fold_unary_loc): Perform the negation in A's type
> when transforming ~ (A - 1) or ~ (A + -1) to -A.
>
> * c-c++-common/ubsan/pr56917.c: New test.
>
> --- gcc/fold-const.c
> +++ gcc/fold-const.c
> @@ -8324,9 +8324,14 @@ fold_unary_loc (location_t loc, enum tree_code code, tree type, tree op0)
> && integer_onep (TREE_OPERAND (arg0, 1)))
> || (TREE_CODE (arg0) == PLUS_EXPR
> && integer_all_onesp (TREE_OPERAND (arg0, 1)))))
> - return fold_build1_loc (loc, NEGATE_EXPR, type,
> - fold_convert_loc (loc, type,
> - TREE_OPERAND (arg0, 0)));
> + {
> + /* Perform the negation in ARG0's type and only then convert
> + to TYPE as to avoid introducing undefined behavior. */
> + tree t = fold_build1_loc (loc, NEGATE_EXPR,
> + TREE_TYPE (TREE_OPERAND (arg0, 0)),
> + TREE_OPERAND (arg0, 0));
> + return fold_convert_loc (loc, type, t);
> + }
> /* Convert ~(X ^ Y) to ~X ^ Y or X ^ ~Y if ~X or ~Y simplify. */
> else if (TREE_CODE (arg0) == BIT_XOR_EXPR
> && (tem = fold_unary_loc (loc, BIT_NOT_EXPR, type,
> --- gcc/testsuite/c-c++-common/ubsan/pr56917.c
> +++ gcc/testsuite/c-c++-common/ubsan/pr56917.c
> @@ -0,0 +1,43 @@
> +/* PR middle-end/56917 */
> +/* { dg-do run } */
> +/* { dg-options "-fsanitize=undefined" } */
> +
> +#include <stdio.h>
> +
> +#define INT_MIN (-__INT_MAX__ - 1)
> +#define LONG_MIN (-__LONG_MAX__ - 1L)
> +#define LLONG_MIN (-__LONG_LONG_MAX__ - 1LL)
> +
> +int __attribute__ ((noinline,noclone))
> +fn1 (unsigned int u)
> +{
> + return (-(int) (u - 1U)) - 1;
> +}
> +
> +long __attribute__ ((noinline,noclone))
> +fn2 (unsigned long int ul)
> +{
> + return (-(long) (ul - 1UL)) - 1L;
> +}
> +
> +long long __attribute__ ((noinline,noclone))
> +fn3 (unsigned long long int ull)
> +{
> + return (-(long long) (ull - 1ULL)) - 1LL;
> +}
> +
> +int
> +main (void)
> +{
> + fputs ("UBSAN TEST START\n", stderr);
> +
> + if (fn1 (__INT_MAX__ + 1U) != INT_MIN
> + || fn2 (__LONG_MAX__ + 1UL) != LONG_MIN
> + || fn3 (__LONG_LONG_MAX__ + 1ULL) != LLONG_MIN)
> + __builtin_abort ();
> +
> + fputs ("UBSAN TEST END\n", stderr);
> + return 0;
> +}
> +
> +/* { dg-output "UBSAN TEST START(\n|\r\n|\r)UBSAN TEST END" } */
>
> Marek
>
>
--
Richard Biener <rguenther@suse.de>
SUSE LINUX GmbH, GF: Felix Imendoerffer, Jane Smithard, Jennifer Guild,
Dilip Upmanyu, Graham Norton HRB 21284 (AG Nuernberg)
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2015-03-11 10:33 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-03-10 17:11 [PATCH] Backport ubsan fix to 4.9 Marek Polacek
2015-03-11 10:33 ` Richard Biener
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).