[Bug tree-optimization/95663] static_cast checks for null even when the pointer is dereferenced
jakub at gcc dot gnu.org
gcc-bugzilla@gcc.gnu.org
Sat Jun 27 15:57:24 GMT 2020
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=95663
--- Comment #15 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
In the particular case we are talking about, security/non-security, it doesn't
make sense to do anything but optimize it into straight line code, any
__builtin_trap or similar will just hurt. If you feel e.g. by default adding
__builtin_unreachable is too dangerous in some cases, it can just handle
similar cases manually and optimize away the conditional if there are no
side-effect statements in between.
We are talking about:
<bb 2> [local count: 1073741824]:
if (base_2(D) != 0B)
goto <bb 3>; [70.00%]
else
goto <bb 4>; [30.00%]
<bb 3> [local count: 751619281]:
iftmp.1_3 = base_2(D) + (sizetype)-4;
<bb 4> [local count: 1073741824]:
# iftmp.1_1 = PHI <iftmp.1_3(3), 0B(2)>
_5 = MEM[(const struct Derived *)iftmp.1_1].D.2340.y;
where without -fno-delete-null-pointer-checks and without -fwrapv-pointer, we
can assume: 1) pointers in valid programs don't wrap 2) the first page is not
mapped
As offsetof (Derived, D.2340.y) is >= 4 and < 4096 here, we don't need to even
care about pointer wrapping, just rely on accesses to 0 .. 4095 offsets to
trap.
If the offsetof would be 0, it would be about pointer wrapping, whether we are
ok if instead of dereferencing *(int *)0 we dereference *(int *)-4 instead.
More information about the Gcc-bugs
mailing list