Message ID | 20170621075752.GM2123@tucnak |
---|---|
State | New |
Headers | show |
On Wed, 21 Jun 2017, Jakub Jelinek wrote: > 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. Yes, exactly. The 2nd patch is ok if it passes bootstrap/test. Richard.
--- 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),