gcc-4.9未定义的行为清洁剂 [英] gcc-4.9 Undefined Behavior Sanitizer

查看:737
本文介绍了gcc-4.9未定义的行为清洁剂的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

gcc-4.9更改中,它说:


UndefinedBehaviorSanitizer(ubsan),一个快速未定义的行为
检测器,已被添加,并且可以通过-fsanitize = undefined启用。
各种计算将在运行时检测未定义的行为
。 UndefinedBehaviorSanitizer目前可用于
C和C ++语言。

UndefinedBehaviorSanitizer (ubsan), a fast undefined behavior detector, has been added and can be enabled via -fsanitize=undefined. Various computations will be instrumented to detect undefined behavior at runtime. UndefinedBehaviorSanitizer is currently available for the C and C++ languages.

我看了这个问题(一个C ++实现,检测未定义的行为?),但似乎已经过时了。

I looked at this question (A C++ implementation that detects undefined behavior?) but it seems fairly outdated.

此链接( http:// gcc .gnu.org / ml / gcc-patches / 2013-06 / msg00264.html )有一些信息,但它有几个月了。

This link (http://gcc.gnu.org/ml/gcc-patches/2013-06/msg00264.html) has some information on it, but it's several months old.


这是一个尝试添加未定义的行为清洁剂到GCC。
注意它是非常alpha版本;到目前为止,它不这么做,在
的时刻,它应该处理零个案,INT_MIN / -1和
各种移位案例(移位一个负值,移动当
第二个操作数> =比TYPE_PRECISION(first_operand)等类似。
(在整数类型上,到目前为止)

This is an attempt to add the Undefined Behavior Sanitizer to GCC. Note that it's very alpha version; so far it doesn't do that much, at the moment it should handle division by zero cases, INT_MIN / -1, and various shift cases (shifting by a negative value, shifting when second operand is >= than TYPE_PRECISION (first_operand) and suchlike. (On integer types, so far.)

从我看过的,它被移植到 gcc LLVM

From what I've read it's being ported to gcc from LLVM.

我试过(5/0),唯一的区别似乎是这个输出:

I've tried it with (5 / 0) and the only difference seems to be this output:

main.cpp:5:19: runtime error: division by zero


b $ b

有没有人有更多的信息或它有什么特点?

Does anyone have any more information on it or what features it has?

推荐答案

添加此类检查比尝试检测所有形式的未定义行为(这在停止问题意义上几乎肯定是不可能的)。

This is more a framework for adding such checks than an attempt to detect all forms of undefined behavior (which is almost certainly impossible in the "halting problem" sense).

GCC文档将这些列为当前支持的检查:

The GCC documentation lists these as the currently supported checks:


-fsanitize = undefined
启用UndefinedBehaviorSanitizer,一个快速未定义的行为检测器。各种计算将被仪表化
以检测运行时的未定义行为。当前子选项是:

-fsanitize=undefined Enable UndefinedBehaviorSanitizer, a fast undefined behavior detector. Various computations will be instrumented to detect undefined behavior at runtime. Current suboptions are:

-fsanitize = shift 此选项启用检查移位操作的结果是否未定义。请注意,完全被认为是
未定义在C和C ++之间以及ISO
C90和C99等之间略有不同。

-fsanitize=shift This option enables checking that the result of a shift operation is not undefined. Note that what exactly is considered undefined differs slightly between C and C++, as well as between ISO C90 and C99, etc.

-fsanitize = integer-divide-by-zero 检测整数除以零以及INT_MIN / -1除法。

-fsanitize=integer-divide-by-zero Detect integer division by zero as well as INT_MIN / -1 division.

-fsanitize = unreachable 使用此选项,编译器会将__builtin_unreachable调用转为诊断消息调用。当到达__builtin_unreachable调用时,行为是
未定义。

-fsanitize=unreachable With this option, the compiler will turn the __builtin_unreachable call into a diagnostics message call instead. When reaching the __builtin_unreachable call, the behavior is undefined.

-fsanitize = vla-bound 选项指示编译器检查可变长度数组的大小是否为正。此选项不会在-std = c ++ 1y模式中产生任何影响,因为标准需要抛出
异常。

-fsanitize=vla-bound This option instructs the compiler to check that the size of a variable length array is positive. This option does not have any effect in -std=c++1y mode, as the standard requires the exception be thrown instead.

-fsanitize = null 此选项启用指针检查。特别是,当这个选项打开的应用程序尝试取消引用NULL指针时,或者如果引用
(可能是一个右值引用)被绑定到一个NULL指针,将发出一个错误
消息。

-fsanitize=null This option enables pointer checking. Particularly, the application built with this option turned on will issue an error message when it tries to dereference a NULL pointer, or if a reference (possibly an rvalue reference) is bound to a NULL pointer.

-fsanitize = return 此选项启用return语句检查。使用此选项打开的程序将在达到非空函数的结尾时发出错误消息
,而实际上不会返回值
。此选项仅在C ++中有效。

-fsanitize=return This option enables return statement checking. Programs built with this option turned on will issue an error message when the end of a non-void function is reached without actually returning a value. This option works in C++ only.

-fsanitize = signed-integer-overflow 此选项启用带符号整数溢出检查。我们检查+,*和一元
和二进制的结果不会在签名算术中溢出。注意,必须考虑
整数提升规则。也就是说,以下的
不是溢出:

-fsanitize=signed-integer-overflow This option enables signed integer overflow checking. We check that the result of +, *, and both unary and binary - does not overflow in the signed arithmetics. Note, integer promotion rules must be taken into account. That is, the following is not an overflow:



           signed char a = SCHAR_MAX;
           a++;




-ftrapv 导致发出带符号溢出的陷阱, -fsanitize = undefined 给出诊断消息。此
目前仅适用于C系列语言。

While -ftrapv causes traps for signed overflows to be emitted, -fsanitize=undefined gives a diagnostic message. This currently works only for the C family of languages.

这篇关于gcc-4.9未定义的行为清洁剂的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆