This is the mail archive of the mailing list for the GCC project.

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: [PATCH] Implement -fsanitize=float-divide-by-zero

Marc Glisse wrote:
> On Mon, 28 Apr 2014, Marek Polacek wrote:
> > This patch implements -fsanitize=float-divide-by-zero option that can
> > be used to detect division by zero even when using floating types.
> > Most of the code in ubsan_instrument_division was ready for this
> > so this was mainly about handling REAL_TYPE there.
> Ideally this would all be unneeded, you would compile your program with
> pragma stdc fenv_access on, on glibc you would call feenableexcept(FE_DIVBYZERO)
> at the beginning of the program, done (I may have listed the wrong function,
> I am always confused by those names).
> But I guess we won't be there anytime soon, so in the mean time...

I think there is difference between enabling the detection of division by zero
or invalid in the command line and doing so in the source code. The command line
option will be useful for detecting problems with the code while the source code
version (using fenv.h on C/C++ and the IEEE modules in Fortran) is useful for
handling exceptions explicitly.

gfortran users would simply use:

$ gfortran -ffpe-trap=zero -g  foo.f90  && ./a.out
Program received signal SIGFPE: Floating-point exception - erroneous arithmetic operation.

Backtrace for this error:
#0  0x7F8FF0C103F7
#1  0x7F8FF0C10A0E
#2  0x3C4FA3299F
#3  0x40070D in MAIN__ at foo.f90:3
Floating point exception

Thus, I think Fortran users would also prefer not to have
-fsanitize=undefined implying trapping dividing by zero.

In addition, having trapping enabled in the CPU should generate faster code
than having the additional checks explicitly.

The biggest advantage of having some -f* option is that one does not need to
modify the source code and that it provides some backtracing. (Actually, UBSAN
doesn't show a trace - but just the file and line; that's not very helpful if
it triggers in a very often called inline function in a header file.)

Thus, I wonder whether it wouldn't be more useful to provide a command-line option
to make the floating-point exceptions signalling - and to remind the users to link
libbacktrace to get the trace.


PS: gfortran uses libgfortran/config/fpu-*.c to enable the trapping, with versions
for sysv (solaris/FreeBSD), glibc, aix and x86*. It currently uses a home-grown
backtrace support instead of libbacktrace, cf. PR fortran/54572.

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]