什么是_chkstk()函数的目的是什么? [英] What is the purpose of the _chkstk() function?

查看:1398
本文介绍了什么是_chkstk()函数的目的是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近使用的 / FAsu 的Visual C ++编译器选项输出源+组件特别长的成员函数的定义。在装配输出,堆栈帧建立后,有一个神秘的 _chkstk()功能单一的电话。

I recently used the /FAsu Visual C++ compiler option to output the source + assembly of a particularly long member function definition. In the assembly output, after the stack frame is set up, there is a single call to a mysterious _chkstk() function.

_chkstk的MSDN网页() 并没有解释为什么这个函数被调用的原因。我也看到堆栈溢出的问题,堆栈会损坏内存?,但我分配更多页面大小的缓冲区不明白的OP和接受的答案都在谈论。

The MSDN page on _chkstk() does not explain the reason why this function is called. I have also seen the Stack Overflow question Allocating a buffer of more a page size on stack will corrupt memory?, but I do not understand what the OP and the accepted answer are talking about.

什么是 _chkstk() CRT功能的目的是什么?它有什么作用?

What is the purpose of the _chkstk() CRT function? What does it do?

推荐答案

页的Windows在你的线程堆栈外,因为它被使用。在堆栈的末尾,有映射为无法访问的内存一名警卫页 - 如果该程序访问它(因为它试图使用比目前更映射栈),有一个访问冲突。操作系统捕获故障,映射在堆的另一页在同一地址的老后卫页面,创建一个新的保护页刚刚超越旧人,并从导致违规的指示继续。

Windows pages in extra stack for your thread as it is used. At the end of the stack, there is one guard page mapped as inaccessible memory -- if the program accesses it (because it is trying to use more stack than is currently mapped), there's an access violation. The OS catches the fault, maps in another page of stack at the same address as the old guard page, creates a new guard page just beyond the old one, and resumes from the instruction that caused the violation.

如果一个函数具有局部变量超过一页,则它访问第一地址可能是多个页面超出堆的当前端。因此,它会想念保护页面并引发访问冲突,该操作系统不知道的是,因为需要更多的堆栈。如果需要的总栈特别巨大的,它可以甚至达到超出保护页,超出分配给堆栈的虚拟地址空间的端部,并进入存储器这实际上是在用别的东西。

If a function has more than one page of local variables, then the first address it accesses might be more than one page beyond the current end of the stack. Hence it would miss the guard page and trigger an access violation that the OS doesn't realise is because more stack is needed. If the total stack required is particularly huge, it could perhaps even reach beyond the guard page, beyond the end of the virtual address space assigned to stack, and into memory that's actually in use for something else.

因此​​, _chkstk 确保有本地变量足够的空间。你可以想像,它通过在页面大小间隔,在触摸局部变量的内存,增加订单,以确保它不会错过保护页面做这个(所谓的堆栈探测)。我不知道是否实际上做的是,虽然,可能需要一个更直接的途径,并指示操作系统在一定量的堆栈的映射。无论哪种方式,如果需要的总大于可用堆栈虚拟地址空间大,那么OS可以抱怨代替做某事未定义关于它

So, _chkstk ensures that there is enough space for the local variables. You can imagine that it does this by touching the memory for the local variables at page-sized intervals, in increasing order, to ensure that it doesn't miss the guard page (so-called "stack probes"). I don't know whether it actually does that, though, possibly it takes a more direct route and instructs the OS to map in a certain amount of stack. Either way, if the total required is greater than the virtual address space available for stack, then the OS can complain about it instead of doing something undefined.

这篇关于什么是_chkstk()函数的目的是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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