From: Jakub Jelinek <jakub@redhat.com>
To: Richard Biener <rguenther@suse.de>
Cc: "Martin Liška" <mliska@suse.cz>, gcc-patches@gcc.gnu.org
Subject: Re: [RFC PATCH] -fsanitize=pointer-overflow support (PR sanitizer/80998)
Date: Wed, 21 Jun 2017 07:58:00 -0000 [thread overview]
Message-ID: <20170621075752.GM2123@tucnak> (raw)
In-Reply-To: <alpine.LSU.2.20.1706201015531.22867@zhemvz.fhfr.qr>
[-- Attachment #1: Type: text/plain, Size: 2971 bytes --]
On Tue, Jun 20, 2017 at 10:18:20AM +0200, Richard Biener wrote:
> > It would be an attempt to avoid sanitizing int foo (int *p) { return p[10] + p[-5]; }
> > (when the offset is constant and small and we dereference it).
> > If there is no page mapped at NULL or at the highest page in the virtual
> > address space, then the above will crash in case p + 10 or p - 5 wraps
> > around.
>
> Ah, so merely an optimization to avoid excessive instrumentation then,
> yes, this might work (maybe make 4096 a --param configurable to be able
> to disable it).
Yes. And I think it can be implemented incrementally.
> > > > I've bootstrapped/regtested the patch on x86_64-linux and i686-linux
> > > > and additionally bootstrapped/regtested with bootstrap-ubsan on both too.
> > > > The latter revealed a couple of issues I'd like to discuss:
> > > >
> > > > 1) libcpp/symtab.c contains a couple of spots reduced into:
> > > > #define DELETED ((char *) -1)
> > > > void bar (char *);
> > > > void
> > > > foo (char *p)
> > > > {
> > > > if (p && p != DELETED)
> > > > bar (p);
> > > > }
> > > > where we fold it early into if ((p p+ -1) <= (char *) -3)
> > > > and as the instrumentation is done during ubsan pass, if p is NULL,
> > > > we diagnose this as invalid pointer overflow from NULL to 0xffff*f.
> > > > Shall we change the folder so that during GENERIC folding it
> > > > actually does the addition and comparison in pointer_sized_int
> > > > instead (my preference), or shall I move the UBSAN_PTR instrumentation
> > > > earlier into the FEs (but then I still risk stuff is folded earlier)?
> > >
> > > Aww, so we turn the pointer test into a range test ;) That it uses
> > > a pointer type rather than an unsigned integer type is a bug, probably
> > > caused by pointers being TYPE_UNSIGNED.
> > >
> > > Not sure if the folding itself is worthwhile to keep though, thus an
> > > option would be to not generate range tests from pointers?
> >
> > I'll have a look. Maybe only do it during reassoc and not earlier.
>
> It certainly looks somewhat premature in fold-const.c.
So for this, I have right now 2 variant patches:
The first one keeps doing what we were except for the
-fsanitize=pointer-overflow case and has been bootstrap-ubsan
bootstrapped/regtested on x86_64-linux and i686-linux.
The second one performs the addition and comparison in pointer sized
unsigned type instead (not bootstrapped yet).
I think the second one would be my preference. Note build_range_check
is used not just during early folding, but e.g. during ifcombine, reassoc
etc.
Martin is contemplating instrumentation of pointer <=/</>=/> comparisons
and in that case we'd need some further build_range_check changes,
because while ptr == (void *) 0 || ptr == (void *) 1 || ptr == (void *) 2
would be without UB, ptr <= (void *) 2 would be UB, so we'd need to perform
all pointer range checks in integral type except the ones where we just do
EQ_EXPR/NE_EXPR.
Jakub
[-- Attachment #2: U606j_ --]
[-- Type: text/plain, Size: 1128 bytes --]
2017-06-21 Jakub Jelinek <jakub@redhat.com>
PR sanitizer/80998
* fold-const.c: Include asan.h.
(build_range_check): For -fsanitize=pointer-overflow don't
add pointer arithmetics for range test.
--- gcc/fold-const.c.jj 2017-06-14 18:07:47.000000000 +0200
+++ gcc/fold-const.c 2017-06-20 17:05:44.351608513 +0200
@@ -79,6 +79,7 @@ along with GCC; see the file COPYING3.
#include "tree-vrp.h"
#include "tree-ssanames.h"
#include "selftest.h"
+#include "asan.h"
/* Nonzero if we are folding constants inside an initializer; zero
otherwise. */
@@ -4906,6 +4907,14 @@ build_range_check (location_t loc, tree
{
if (value != 0 && !TREE_OVERFLOW (value))
{
+ /* Avoid creating pointer arithmetics that is not present
+ in the source when sanitizing. */
+ if (!integer_zerop (low)
+ && current_function_decl
+ && sanitize_flags_p (SANITIZE_POINTER_OVERFLOW,
+ current_function_decl))
+ return 0;
+
low = fold_build1_loc (loc, NEGATE_EXPR, TREE_TYPE (low), low);
return build_range_check (loc, type,
fold_build_pointer_plus_loc (loc, exp, low),
[-- Attachment #3: U625 --]
[-- Type: text/plain, Size: 2274 bytes --]
2017-06-21 Jakub Jelinek <jakub@redhat.com>
* fold-const.c (build_range_check): Compute pointer range check in
integral type if pointer arithmetics would be needed. Formatting
fixes.
--- gcc/fold-const.c.jj 2017-06-20 21:38:04.000000000 +0200
+++ gcc/fold-const.c 2017-06-21 09:23:00.572404964 +0200
@@ -4818,21 +4818,21 @@ build_range_check (location_t loc, tree
if (low == 0)
return fold_build2_loc (loc, LE_EXPR, type, exp,
- fold_convert_loc (loc, etype, high));
+ fold_convert_loc (loc, etype, high));
if (high == 0)
return fold_build2_loc (loc, GE_EXPR, type, exp,
- fold_convert_loc (loc, etype, low));
+ fold_convert_loc (loc, etype, low));
if (operand_equal_p (low, high, 0))
return fold_build2_loc (loc, EQ_EXPR, type, exp,
- fold_convert_loc (loc, etype, low));
+ fold_convert_loc (loc, etype, low));
if (TREE_CODE (exp) == BIT_AND_EXPR
&& maskable_range_p (low, high, etype, &mask, &value))
return fold_build2_loc (loc, EQ_EXPR, type,
fold_build2_loc (loc, BIT_AND_EXPR, etype,
- exp, mask),
+ exp, mask),
value);
if (integer_zerop (low))
@@ -4864,7 +4864,7 @@ build_range_check (location_t loc, tree
exp = fold_convert_loc (loc, etype, exp);
}
return fold_build2_loc (loc, GT_EXPR, type, exp,
- build_int_cst (etype, 0));
+ build_int_cst (etype, 0));
}
}
@@ -4895,25 +4895,15 @@ build_range_check (location_t loc, tree
return 0;
}
+ if (POINTER_TYPE_P (etype))
+ etype = unsigned_type_for (etype);
+
high = fold_convert_loc (loc, etype, high);
low = fold_convert_loc (loc, etype, low);
exp = fold_convert_loc (loc, etype, exp);
value = const_binop (MINUS_EXPR, high, low);
-
- if (POINTER_TYPE_P (etype))
- {
- if (value != 0 && !TREE_OVERFLOW (value))
- {
- low = fold_build1_loc (loc, NEGATE_EXPR, TREE_TYPE (low), low);
- return build_range_check (loc, type,
- fold_build_pointer_plus_loc (loc, exp, low),
- 1, build_int_cst (etype, 0), value);
- }
- return 0;
- }
-
if (value != 0 && !TREE_OVERFLOW (value))
return build_range_check (loc, type,
fold_build2_loc (loc, MINUS_EXPR, etype, exp, low),
next prev parent reply other threads:[~2017-06-21 7:58 UTC|newest]
Thread overview: 21+ messages / expand[flat|nested] mbox.gz Atom feed top
2017-06-19 18:25 Jakub Jelinek
2017-06-20 7:41 ` Richard Biener
2017-06-20 8:14 ` Jakub Jelinek
2017-06-20 8:18 ` Richard Biener
2017-06-21 7:58 ` Jakub Jelinek [this message]
2017-06-21 8:04 ` Richard Biener
2017-06-21 14:40 ` [RFC PATCH] Fix pointer diff (was: -fsanitize=pointer-overflow support (PR sanitizer/80998)) Jakub Jelinek
2017-06-21 15:17 ` Jakub Jelinek
2017-06-21 16:27 ` Marc Glisse
2017-06-22 8:31 ` Richard Biener
2017-06-22 9:29 ` Marc Glisse
2017-06-22 9:46 ` Richard Biener
2017-07-01 16:41 ` Marc Glisse
2017-07-03 12:37 ` Richard Biener
2017-10-09 11:01 ` Marc Glisse
2017-10-19 15:11 ` Richard Biener
2017-10-28 13:04 ` Marc Glisse
2017-10-28 17:13 ` Richard Biener
2017-07-04 8:53 ` [RFC PATCH] -fsanitize=pointer-overflow support (PR sanitizer/80998) Jakub Jelinek
2017-06-21 8:00 ` Jakub Jelinek
2017-06-21 8:05 ` Richard Biener
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20170621075752.GM2123@tucnak \
--to=jakub@redhat.com \
--cc=gcc-patches@gcc.gnu.org \
--cc=mliska@suse.cz \
--cc=rguenther@suse.de \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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).