陷阱重新presentation,unsigned char型和IA64的NaT [英] Trap Representation, unsigned char and IA64 NaT

查看:115
本文介绍了陷阱重新presentation,unsigned char型和IA64的NaT的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

来源:ia64上未初始化的垃圾可能是致命的


  

在IA64的,每个64位寄存器实际上是65位。额外的位
  被称为的NAT表示没有的事。该位被置时
  寄存器不包含有效值。想想它作为
  浮点整数版本为NaN。


  
  

在NAT位会从投机执行设置最常用的。那里
  是装入指令在其上试图将IA64的特殊形式
  从内存中加载的价值,但在加载失败(因为内存
  调出或地址是无效的),然后,而不是养的
  页面错误,所发生的一切是NaT位被设置,并且执行
  继续。


  
  

NAT的所有数学运算只需再次产生的NAT。


源文章接着解释寄存器如何最终不得不投机加载期间在NAT重新presentation,并提出以下备注:


  

有关你看,如果你有一个寄存器的值是NAT和你这么多
  因为它呼吸的错误的方式(例如,尝试它的值保存到
  显存),处理器将抛出一个STATUS_REG_NAT_CONSUMPTION
  例外。


从其他堆栈溢出的答案似乎陷阱重新presentations的是,结果
任何类型的(除无符号字符)可能有陷阱重新presentations。

链接说,


  

的唯一保证该标准给出了有关访问未初始化
  数据是unsigned char类型有没有陷阱再presentations和
  该填充有没有陷阱再presentations。


如果这样的寄存器(使用NAT寄存器位设置)被分配用于存储未初始化的无符号字符(类似于从下面的缺陷报告的code片段),这是怎么按照ISO C11处理?

下面是个缺陷报告,同样的问题,是它的ISO C11解决?

如果没有这种特殊情况如何处理?


  

如果左值指定自动存储持续时间的对象,
  可能已被宣布与寄存器存储类(从未有过的
  地址获取),并且该对象是未初始化(未与声明
  初始化,并没有分配给它之前已经执行的
  使用),这种行为是未定义


做上面除了在为C1X变处理这种情况?在节中的缺陷报告的结尾

defect_report


  

下面的函数是未定义C90下的行为,但似乎
  以C99下严格符合


INT富(无效){
      unsigned char型UC;
      返回UC +1 GT; = 0;
  }


解决方案

第一件事,如果你还没有看到它自己,你可以抓住从的这里(的又见的)。

从DR文本确实加入到6.3.2.1节P2,这使得code剪断未定义根据C11。

在约陷阱重新presentations标准的部分继续排除这种可能性 unsigned char型可以拥有一个陷阱重新presentation - 但没有按' Ť问题。这里要注意的一点是,随着2008年春季记在DR提到,从标准的角度来看,这实际上并不需要涉及到陷阱重新presentations自己在所有的(他们只是可能的机制,使UB会导致在金属为你的问题)。这个问题实际上是关于未初始化的自动值;修正段落通过澄清 unsigned char型应该从通用型UB因为根本的有一个的考虑豁免解决了这个EM>其特定类型的属性(不通过增加更多的复杂的属性)。

您可以想像,就像的NaT位在IA64整数的实现细节,没有陷阱再presentation的是一个实现细节C类型的一般家庭中的一个特殊类型。变量的实际类型是次要的更一般的规则,你不应该感到安全访问未初始化任何变量;加阐明了precedence。

Source:Uninitialized garbage on ia64 can be deadly

On the ia64, each 64-bit register is actually 65 bits. The extra bit is called "NaT" which stands for "not a thing". The bit is set when the register does not contain a valid value. Think of it as the integer version of the floating point NaN.

The NaT bit gets set most commonly from speculative execution. There is a special form of load instruction on the ia64 which attempts to load the value from memory, but if the load fails (because the memory is paged out or the address is invalid), then instead of raising a page fault, all that happens is that NaT bit gets set, and execution continues.

All mathematical operations on NaT just produce NaT again.

The source article went on to explain how a register could end up having a NaT representation during speculative loading and makes the following remark:

For you see, if you have a register whose value is NaT and you so much as breathe on it the wrong way (for example, try to save its value to memory), the processor will raise a STATUS_REG_NAT_CONSUMPTION exception.

it seems from other stack overflow answers to Trap representations that,
"Any type (except unsigned char) may have trap representations".

This link says that

The only guarantees the standard gives about accessing uninitialized data are that the unsigned char type has no trap representations, and that padding has no trap representations.

If such a register ( a register with NaT bit Set) is allocated for storing an uninitialized unsigned char ( similar to the code fragment from the defect report below), how is this handled according to ISO C11?

Is the defect report below points to the same problem and is it rectified in ISO C11?

If not how this special case is handled?

If the lvalue designates an object of automatic storage duration that could have been declared with register storage class (never had its address taken), and that object is uninitialized (not declared with an initializer, and no assignment to it has been performed prior to the use), the behavior is undefined

does the above addition at the end of the defect report in section "change for C1X" handle this case?

defect_report

The following function has undefined behavior under C90, but appears to be strictly conforming under C99

  int foo(void) {
      unsigned char uc;
      return uc + 1 >= 0;
  }

解决方案

First thing, if you haven't seen it for yourself, you can grab the final draft of the C11 standard from here (see also).

The text from the DR was indeed added to section 6.3.2.1 p2, which makes the code snipped undefined according to C11.

The sections in the standard about trap representations continue to exclude the possibility that unsigned char can have a trap representation - but that doesn't matter. The thing to note here is that, as the Spring 2008 note in the DR mentions, from a standard perspective this doesn't actually need to involve trap representations themselves at all (they're just the likely mechanism by which the UB would cause a problem for you on the metal). The issue is really about uninitialized automatic values; the amended paragraph resolves this by clarifying that unsigned char should not be considered exempt from a general type of UB simply because of one of its type-specific properties (not by adding more complexity to that property).

You could imagine that just as NaT bits are an implementation detail of integers on IA64, the absence of a trap representation is an "implementation detail" of one particular type within the general family of C types. The actual type of the variable is secondary to the more general rule that you shouldn't feel safe accessing any variable uninitialized; the addition clarifies that precedence.

这篇关于陷阱重新presentation,unsigned char型和IA64的NaT的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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