未定义的行为是否适用于asm代码? [英] Does undefined behavior apply to asm code?

查看:144
本文介绍了未定义的行为是否适用于asm代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

比方说,您知道您的软件只能在定义了有符号溢出行为的二进制补码机上运行.在C和C ++中,签名溢出仍然是未定义的行为,并且编译器可以自由地用"ret"替换整个程序,发动核战争,格式化驱动器或让恶魔从你的鼻子里飞出来.

Let's say you know your software will only run on two's complement machines where signed overflow behavior is nicely defined. Signed overflow is still undefined behavior in C and C++ and the compiler is free to replace your entire program with "ret", start a nuclear war, format your drive, or make demons fly out of your nose.

假设您已经在内联asm中签名了溢出,那么程序是否仍然调用UB?

Suppose you have signed overflow in inline asm, does your program still invoke UB?

如果是,分别编译和链接的汇编程序如何?

If yes, What about separately compiled and linked assembler?

推荐答案

未定义的行为"表示C语言. C ++标准未定义程序的行为.如果您的程序包含内联汇编,则应该很清楚,C或C ++标准通常不会描述其行为.某些其他标准甚至可以定义行为,但是在C或C ++标准的上下文中,这仍然并不意味着定义的行为".

"Undefined behaviour" means the C resp. C++ standards don't define the behaviour of your program. If your program contains inline assembly, it should be pretty clear that its behaviour won't normally be described by either the C or the C++ standard. Some other standard might even define the behaviour, but that still doesn't mean "defined behaviour" in the context of the C or C++ standard.

也就是说,C标准确实需要支持的扩展的文档.如果可以从实现的文档中推断出程序的行为,并且实现使程序的行为有所不同,则说明实现不符合标准:

That said, the C standard does require documentation of supported extensions. If the behaviour of your program can be inferred from your implementation's documentation, and your implementation makes your program behave differently, that is a failure of your implementation to conform to the standard:

4.符合性

8一个实现应随附一个文档,该文档定义所有实现定义的和特定于语言环境的特征以及所有扩展.

8 An implementation shall be accompanied by a document that defines all implementation-defined and locale-specific characteristics and all extensions.

对于C ++,此要求已被削弱:

For C++, this requirement has been weakened:

1.4实施合规性[内部遵从性]

9每个实现都应包含文档,该文档标识它不支持的所有有条件支持的构造,并定义所有特定于语言环境的特征.

9 Each implementation shall include documentation that identifies all conditionally-supported constructs that it does not support and defines all locale-specific characteristics.

1.9程序执行[内部执行]

2在本国际标准中,抽象机的某些方面和操作被描述为实现定义的[...].每个实现都应包括描述这些方面的特征和行为的文档. [...]

2 Certain aspects and operations of the abstract machine are described in this International Standard as implementation-defined [...] Each implementation shall include documentation describing its characteristics and behavior in these respects. [...]

我找不到要记录扩展名(如果已记录)正确记录的要求.这表明在C ++中,即使您的实现将程序的行为定义为扩展,但事实证明文档是错误的,也太糟糕了.

I'm unable to find a requirement for extensions to be documented, and if documented, to be documented correctly. This would suggest that in C++, even if your implementation defines the behaviour of your program as an extension, if it turns out the documentation is wrong, that's just too bad.

对于C ++半标准的asm语句(如注释中所述,"asm声明是有条件支持的;其含义是实现定义的."),如果您的实现支持它,则必须有文档记录,但是实现以与C ++标准所暗示的方式不同的方式支持内联汇编,这当然是常见的做法,因此不会给您太多额外的负担.

For the C++ semi-standard asm statement (as mentioned in the comments, "The asm declaration is conditionally-supported; its meaning is implementation-defined."), if your implementation supports it it needs to be documented, but of course it's common practice for implementations to support inline assembly in a different manner than hinted by the C++ standard, so this doesn't give you much extra.

这篇关于未定义的行为是否适用于asm代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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