即使指针从未被取消引用,过三数指针中的硬件陷阱是如何发生的?

发布于 2024-07-26 03:16:08 字数 841 浏览 5 评论 0原文

Herb Sutter 在他的2005 年 11 月 1 日 C++ 专栏中写道...

int A[17];
int* endA = A + 17;
for( int* ptr = A; ptr < endA; ptr += 5 )
{
  // ...
}

[O]一些 CPU 架构,包括 当前的,上述代码 可能会导致硬件陷阱发生在 过了三点的点 指针被创建,无论是 指针是否被取消引用。

CPU 如何捕获位模式? 那么……

int A[17];

// (i) hardware will trap this ?
int *pUgly = A + 18; 

// (ii) hardware will trap this, too?
int *pEnd = A + 17;
++pEnd;  

// (iii) will this fool it?
int *precious = A + 17;
unsigned long tricksy = reinterpret_cast<unsigned long>(precious) ; 
++tricksy;
int *pHobbits = reinterpret_cast<int *>(tricksy); 

额外问题:“某些当前的 CPU 架构”一词通常是否应该被理解为仅指运输产品,或者如果描述或暗示它们的小说作品具有虚构的架构,那么它是否也包括虚构的架构?最近的出版日期?

In his November 1, 2005 C++ column, Herb Sutter writes ...

int A[17];
int* endA = A + 17;
for( int* ptr = A; ptr < endA; ptr += 5 )
{
  // ...
}

[O]n some CPU architectures, including
current ones, the aforementioned code
can cause a hardware trap to occur at
the point where the three-past-the-end
pointer is created, whether that
pointer is ever dereferenced or not.

How does a CPU trap on a bitpattern? What about ...

int A[17];

// (i) hardware will trap this ?
int *pUgly = A + 18; 

// (ii) hardware will trap this, too?
int *pEnd = A + 17;
++pEnd;  

// (iii) will this fool it?
int *precious = A + 17;
unsigned long tricksy = reinterpret_cast<unsigned long>(precious) ; 
++tricksy;
int *pHobbits = reinterpret_cast<int *>(tricksy); 

Bonus question: Should the phrase "some current CPU architectures" be ordinarily understood to refer to shipping products only, or does it include imaginary architectures as well if the work of fiction in which they are described or alluded to has a recent publication date?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

冰之心 2024-08-02 03:16:08

指针操作依赖于实现。

可能会发生这样的情况:在某些平台上,仅允许特定寄存器存储指针值(仅特定寄存器可以用作索引寄存器),并且立即检查由非特权程序代码写入此类寄存器的值是否为有效地址。 在这种情况下,如果指针值对应的地址不存在于程序的地址空间中,则肯定会发生硬件陷阱。

如果是这种情况,编译器未优化的任何为指针分配新值的代码都可能导致陷阱。

Pointer operations are implementation-dependent.

It can happen that on some platform only specific registers are allowed for storing pointer values (only specific registers can serve as index registers) and the value written into such register by a non-priviledged program code is immediately checked for being a valid address. In this case if the pointer value corresponds to an address not present in the address space of the program the hardware trap will certainly occur.

If that's the case any code not optimized out by the compiler that assigns a new value to a pointer can potentially cause a trap.

痕至 2024-08-02 03:16:08

你可以用谷歌搜索“推测性阅读”。 一旦地址形成,高速缓存架构就可以智能地将相应的数据线放入高速缓存中。 通常,这应该是无害的,但如果你明显超出范围(例如进入下一页),这可能不再是真的。

You might to google "speculative reading". As soon as an address is formed, it may be smart for the cache architecture to bring the corresponding dataline into cache. Normally, this should be harmless, but if you're significantly out of bounds (e.g. onto the next page) this might no longer be true.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文