Open Quuxplusone opened 4 years ago
Bugzilla Link | PR46124 |
Status | NEW |
Importance | P enhancement |
Reported by | Haoxin Tu (haoxintu@gmail.com) |
Reported on | 2020-05-28 08:56:13 -0700 |
Last modified on | 2020-05-29 09:37:22 -0700 |
Version | trunk |
Hardware | PC All |
CC | blitzrakete@gmail.com, dblaikie@gmail.com, dgregor@apple.com, efriedma@quicinc.com, erik.pilkington@gmail.com, llvm-bugs@lists.llvm.org, richard-llvm@metafoo.co.uk |
Fixed by commit(s) | |
Attachments | |
Blocks | |
Blocked by | |
See also |
UndefinedBehaviorSanitizer: FPE on unknown address 0x000000431817
This means your program crashed... which is what naturally happens on x86 when a "div" instruction that divides by zero. Are you suggesting we should return some arbitrary value instead? What value would you suggest?
(In reply to Eli Friedman from comment #1)
> > UndefinedBehaviorSanitizer: FPE on unknown address 0x000000431817
>
> This means your program crashed... which is what naturally happens on x86
> when a "div" instruction that divides by zero. Are you suggesting we should
> return some arbitrary value instead? What value would you suggest?
Hi, Eli, what value should be returned is not my concern.
look this case test3.cc
#include<iostream>
int main () {
0 / 0;
0 / 0;
std::cout << "ok" << std::endl;
return 0;
}
in clang-trunk
$clang++-trunk -fsanitize=integer-divide-by-zero test3.cc ; ./a.out
test3.cc:3:7: runtime error: division by zero
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior example.cpp:3:7 in
test3.cc:4:7: runtime error: division by zero
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior example.cpp:4:7 in
ok
I mean test1.cc and test2.cc should be reproduced like test3.cc, detecting all
the UBs and has a normal return rather than abort.
(In reply to Haoxin Tu from comment #2)
> Hi, Eli, what value should be returned is not my concern.
Do you mean it doesn't matter to the program? Or that you personally don't
care what value the compiler chooses?
If you run test1 without any -fsanitize flag, you get exactly the same FPE
that's reported by ubsan.
(In reply to Haoxin Tu from comment #2)
> (In reply to Eli Friedman from comment #1)
> > > UndefinedBehaviorSanitizer: FPE on unknown address 0x000000431817
> >
> > This means your program crashed... which is what naturally happens on x86
> > when a "div" instruction that divides by zero. Are you suggesting we should
> > return some arbitrary value instead? What value would you suggest?
>
> Hi, Eli, what value should be returned is not my concern.
>
> look this case test3.cc
> #include<iostream>
> int main () {
> 0 / 0;
> 0 / 0;
> std::cout << "ok" << std::endl;
> return 0;
> }
>
> in clang-trunk
> $clang++-trunk -fsanitize=integer-divide-by-zero test3.cc ; ./a.out
> test3.cc:3:7: runtime error: division by zero
> SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior example.cpp:3:7 in
> test3.cc:4:7: runtime error: division by zero
> SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior example.cpp:4:7 in
> ok
>
> I mean test1.cc and test2.cc should be reproduced like test3.cc, detecting
> all the UBs and has a normal return rather than abort.
In this case the dead code (to perform the division) was never emitted - so the
program printed out the diagnostic, but continued.
In your first case, the code isn't dead - the code is used and actually
performs the division, and produces a floating point signal/exception. That's
just what the program does (that's the behavior of the undefined behavior the
program invokes).
All UBSan did was transform the way that failure was printed. If you run the
program without UBSan, you get this:
$ clang++ -w div.cpp && ./a.out
Floating point exception
$
UBSan can't make the program continue beyond that point - there's no value that
could be produced by the division that would allow the program to continue in
any defined way. (that's what Eli was getting at - what value should 0/0
produce to put into the variable to let the program continue to use?)
(In reply to Eli Friedman from comment #3)
> Do you mean it doesn't matter to the program? Or that you personally don't
> care what value the compiler chooses?
I think I get your point.
The reason is that GCC treats "0/0" produce 0 but "1/0" as an exception, and
Clang treats all of them as an exception. I am so sorry I have no idea about
what "0/0" should be produced, but I hold the view that Clang and GCC should
produce consistent results.
Take a look at this case in float-divide-by-zero
test4.cc
#include<iostream>
int main () {
float a = 1/0.0;
std::cout << a << std::endl;
return 0;
}
$clang++-trunk -w -fsanitize=float-divide-by-zero test4.cc ; ./a.out
test4.cc:3:16: runtime error: division by zero
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior example.cpp:3:16 in
inf
I think the results in integer-divide-by-zero might be treated similarly to
float-divide-by-zero, just print "inf" to "0/0" rather than trigger a dead
signal to stop following statements.
(In reply to Haoxin Tu from comment #6)
> just print "inf" to "0/0"
Sorry, by a mistake, print "nan" to "0/0" and "inf" to "1/0".
(In reply to Haoxin Tu from comment #6)
> Take a look at this case in float-divide-by-zero
>
> test4.cc
> #include<iostream>
> int main () {
> float a = 1/0.0;
> std::cout << a << std::endl;
> return 0;
> }
>
> $clang++-trunk -w -fsanitize=float-divide-by-zero test4.cc ; ./a.out
> test4.cc:3:16: runtime error: division by zero
> SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior example.cpp:3:16 in
> inf
>
> I think the results in integer-divide-by-zero might be treated similarly to
> float-divide-by-zero, just print "inf" to "0/0" rather than trigger a dead
> signal to stop following statements.
Nature of undefined behavior - it can vary between compilers. "inf" can't be
printed, because "inf" doesn't fit in an integer. Floating point values have
representations of infinity, integers do not.