在gcc-4.9 changes中说:
UndefinedBehaviorSanitizer(ubsan),一种快速未定义的行为 检测器已添加,可通过-fsanitize = undefined启用。 将检测各种计算以检测未定义的行为 在运行时。 UndefinedBehaviorSanitizer目前可用于 C和C ++语言。
我看了这个问题(A C++ implementation that detects undefined behavior?),但似乎已经过时了。
此链接(http://gcc.gnu.org/ml/gcc-patches/2013-06/msg00264.html)上有一些信息,但已有几个月了。
这是尝试将未定义的行为消毒剂添加到GCC。 请注意,它是非常alpha版本;到目前为止它没有那么多,在 它应该处理除零的情况,INT_MIN / -1和 各种换档情况(换一个负值,换档时 第二个操作数是> =比TYPE_PRECISION(first_operand)等。 (到目前为止,在整数类型上。)
从我读过的内容被移植到gcc
LLVM
。
我用(5 / 0)
尝试了它,唯一的区别似乎是这个输出:
main.cpp:5:19: runtime error: division by zero
有没有人有关于它的更多信息或它有什么功能?
答案 0 :(得分:40)
这是一个添加此类检查的框架,而不是尝试检测所有形式的未定义行为(在“暂停问题”意义上几乎肯定不可能)。
GCC documentation将这些列为当前支持的支票:
-fsanitize=undefined
启用UndefinedBehaviorSanitizer,一个快速未定义的行为检测器。将对各种计算进行检测 在运行时检测未定义的行为。目前的子选项是:
-fsanitize=shift
此选项可以检查移位操作的结果是否未定义。请注意,究竟考虑了什么 undefined在C和C ++之间以及ISO之间略有不同 C90和C99等
-fsanitize=integer-divide-by-zero
检测整数除以零以及INT_MIN / -1除法。
-fsanitize=unreachable
使用此选项,编译器会将__builtin_unreachable调用转换为诊断消息调用。到达__builtin_unreachable调用时,行为是 未定义。
-fsanitize=vla-bound
此选项指示编译器检查可变长度数组的大小是否为正。此选项不会 在-std = c ++ 1y模式中有任何影响,因为标准需要 反而抛出异常。
-fsanitize=null
此选项启用指针检查。特别是,打开此选项时构建的应用程序将发出错误 尝试取消引用NULL指针或引用时的错误消息 (可能是右值引用)绑定到NULL指针。
-fsanitize=return
此选项启用返回语句检查。打开此选项构建的程序将发出错误消息 当没有实际到达非空函数的结束时 返回一个值。此选项仅适用于C ++。
-fsanitize=signed-integer-overflow
此选项启用有符号整数溢出检查。我们检查+,*和两者的结果 和二进制 - 在签名的算术中不会溢出。注意, 必须考虑整数提升规则。那就是 以下不是溢出:signed char a = SCHAR_MAX; a++;
虽然
-ftrapv
导致发出签名溢出的陷阱,但-fsanitize=undefined
会发出诊断消息。这个 目前仅适用于C系列语言。
答案 1 :(得分:1)
从下面列出的GCC 5 Release Series : Changes, New Features, and Fixes中提取的最新GCC 5.0新增内容;
UndefinedBehaviorSanitizer获得了一些新的清理选项:
-fsanitize=float-divide-by-zero: detect floating-point division by zero; -fsanitize=float-cast-overflow: check that the result of floating-point type to integer conversions do not overflow; -fsanitize=bounds: enable instrumentation of array bounds and detect out-of-bounds accesses; -fsanitize=alignment: enable alignment checking, detect various misaligned objects; -fsanitize=object-size: enable object size checking, detect various out-of-bounds accesses. -fsanitize=vptr: enable checking of C++ member function calls, member accesses and some conversions between pointers to base and derived classes, detect if the referenced object does not have the correct dynamic type.