Open haoxintu opened 4 years ago
Take a look at this case in float-divide-by-zero
test4.cc
include
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.
just print "inf" to "0/0"
Sorry, by a mistake, print "nan" to "0/0" and "inf" to "1/0".
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.
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.
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
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?)
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.
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.
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?
Extended Description
This code
test1.cc
Clang only detects one runtime error in for statement but leaves out detecting the statements following ones.
In
test2.cc
Should the main function return from
return 0
rather than exit directly from the for statement?I also file a bug report in GCC, they confirmed this limitation immediately. https://gcc.gnu.org/bugzilla/show_bug.cgi?id=95385