程序堆栈真的会溢出吗?

2024-06-19

如果达到堆栈大小限制,处理器是否会导致操作系统出现 TRAP(从而防止堆栈溢出:P)


我相信 Windows 确实有一个堆栈,当您到达末尾时它会增长。

在 Visual Studio 编译器中,负责此操作的代码位于chkstk.obj module.

由于此代码是开源的,我可以将其发布在这里:

;***
;_chkstk - check stack upon procedure entry
;
;Purpose:
;       Provide stack checking on procedure entry. Method is to simply probe
;       each page of memory required for the stack in descending order. This
;       causes the necessary pages of memory to be allocated via the guard
;       page scheme, if possible. In the event of failure, the OS raises the
;       _XCPT_UNABLE_TO_GROW_STACK exception.
;
;       NOTE:  Currently, the (EAX < _PAGESIZE_) code path falls through
;       to the "lastpage" label of the (EAX >= _PAGESIZE_) code path.  This
;       is small; a minor speed optimization would be to special case
;       this up top.  This would avoid the painful save/restore of
;       ecx and would shorten the code path by 4-6 instructions.
;
;Entry:
;       EAX = size of local frame
;
;Exit:
;       ESP = new stackframe, if successful
;
;Uses:
;       EAX
;
;Exceptions:
;       _XCPT_GUARD_PAGE_VIOLATION - May be raised on a page probe. NEVER TRAP
;                                    THIS!!!! It is used by the OS to grow the
;                                    stack on demand.
;       _XCPT_UNABLE_TO_GROW_STACK - The stack cannot be grown. More precisely,
;                                    the attempt by the OS memory manager to
;                                    allocate another guard page in response
;                                    to a _XCPT_GUARD_PAGE_VIOLATION has
;                                    failed.
;
;*******************************************************************************

public  _alloca_probe

_chkstk proc

_alloca_probe    =  _chkstk

        push    ecx

; Calculate new TOS.

        lea     ecx, [esp] + 8 - 4      ; TOS before entering function + size for ret value
        sub     ecx, eax                ; new TOS

; Handle allocation size that results in wraparound.
; Wraparound will result in StackOverflow exception.

        sbb     eax, eax                ; 0 if CF==0, ~0 if CF==1
        not     eax                     ; ~0 if TOS did not wrapped around, 0 otherwise
        and     ecx, eax                ; set to 0 if wraparound

        mov     eax, esp                ; current TOS
        and     eax, not ( _PAGESIZE_ - 1) ; Round down to current page boundary

cs10:
        cmp     ecx, eax                ; Is new TOS
        jb      short cs20              ; in probed page?
        mov     eax, ecx                ; yes.
        pop     ecx
        xchg    esp, eax                ; update esp
        mov     eax, dword ptr [eax]    ; get return address
        mov     dword ptr [esp], eax    ; and put it at new TOS
        ret

; Find next lower page and probe
cs20:
        sub     eax, _PAGESIZE_         ; decrease by PAGESIZE
        test    dword ptr [eax],eax     ; probe page.
        jmp     short cs10

_chkstk endp

        end
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

程序堆栈真的会溢出吗? 的相关文章

随机推荐

  • 如何从另一个片段运行一个片段

    我有一个主要活动 活动 1 它会膨胀导航抽屉 带有列表视图列出项目 抽屉由片段 MenuFragment 运行 以便我可以根据选择的项目来扩展任何 xml 布局 现在我的问题是当用户从抽屉中选择一个项目时如何运行另一个活动 活动 2 因为抽
  • const_cast 的自动类型推导不起作用

    在我的工作中使用const cast在某些情况下是不可避免的 现在我必须const cast一些非常复杂的类型 实际上我不想在const cast
  • 删除 arrayList 中的项目,java.lang.UnsupportedOperationException

    我想删除数组列表选择位置中的项目 我的代码是 List
  • 按列表中(不在)中的索引值对 Pandas 数据帧进行切片

    我有一个pandas数据框 df 我想选择所有索引df那是not在列表中 blacklist 现在 我使用列表理解来创建所需的切片标签 ix i for i in df index if i not in blacklist df sele
  • 如何向 ExtJS 应用添加自定义字体?

    同事 请建议如何向 ExtJS 添加自定义字体 以便它与应用程序一起加载 如果用户的系统中没有安装此字体 必须将其加载到哪个文件夹 以及应将其包含在何处以及如何包含 谢谢各位的解答 您只需添加新的字体文件即可myApp resources
  • 如何让 PowerShell 等待 Invoke-Item 完成?

    如何让 PowerShell 等待 Invoke Item 调用完成 我正在调用一个不可执行的项目 因此我需要使用 Invoke Item 来打开它 只需使用Start Process wait 例如Start Process wait c
  • tkinter - 使用按钮在帧之间来回切换

    我需要功能 最好是一个功能 当按下下一页和后退按钮时可以在页面之间来回切换 我想这可以通过将布尔变量分配给后退和下一页按钮来完成 不确定是否可以这样做 来确定您是否要前进或后退所有页面的有序列表 需要知道当前升高的框架的索引 索引可用于找出
  • 在 Chrome 中,应用于包含图像的锚点的轮廓高度不正确

    对于我正在开发的网站 我希望当链接聚焦 悬停 活动时 链接周围会出现虚线轮廓 我希望文本和图像链接发生这种情况 我遇到的问题是 虽然我的代码在 Firefox 和 IE 中运行良好 但在 Chrome 7 0 517 41 中 虚线轮廓与我
  • 带数据透视表的 Laravel 查询生成器

    我有两个带有数据透视表的表 Table tours id name country id featured Table countries id name 数据透视表country tour id country id tour id 我想
  • Jasmine 测试中模拟 window.location.reload

    我创建了一个window location reload我的 JavaScript 中的函数 我需要在 Jasmine 中测试时模拟重新加载函数 因为它不断循环 当我跑步时测试进展顺利grunt jenkins 但在浏览器 mozilla
  • 如何通过 COM 将长数组从 VB6 传递到 C#

    我需要将 int 或 long 数组 无关紧要 从 VB6 应用程序传递到 C COM Visible 类 我尝试在 C 中声明接口 如下所示 void Subscribe MarshalAs UnmanagedType SafeArray
  • 如何强制串行端口写入方法在发送数据之前等待线路清除?

    以下是我正在尝试做的一些背景 打开从移动设备到蓝牙打印机的串行端口 将 EPL 2 表格发送到蓝牙打印机 以便它了解如何处理即将接收的数据 收到表格后 将一些数据发送到打印机 这些数据将打印在标签纸上 根据需要多次重复步骤 3 打印每个标签
  • 即使没有结果也返回一个值

    我有这种简单的查询 它返回给定 id 的非空整数字段 SELECT field1 FROM table WHERE id 123 LIMIT 1 问题是如果找不到 id 结果集就是空的 我需要查询始终返回一个值 即使没有结果 我有这个东西工
  • 如何使用div绘制曲线?

    我需要使用 CSS 绘制两条曲线 我尝试过组装一些divs 使用CSSborder radius绘制弧形面板 但结果很糟糕 还有更好的算术吗 正如我之前在评论中提到的 请不要使用CSS用于实现复杂的曲线和形状 虽然仍然可以使用 CSS 来实
  • 添加 Google Play Services 9.0.0 后 Dex 文件超过 64k

    我按照 Firebase 指南添加 FCM 因此我将以下依赖项添加到我的应用程序 gradle 中 compile com google android gms play services 9 0 0 apply plugin com go
  • 重新渲染列表模板导致页面滚动到顶部

    我有一些模板 大致如下所示
  • 只获取倒数第二条记录 - mysql-query

    我有一个如下表记录 my table id rating description 1 0 0 bed 2 1 0 good 3 0 0 bed 4 1 0 good 5 0 0 bed 6 0 0 bed 7 0 0 bed 现在我通过评级
  • SSE:如何将 _m128i._i32[4] 减少到 _m128i._i8

    我对 SSE 编码非常陌生 我想将 int32 类型的 m128i 4 的结果存储到 int8 类型的 m128i 中 m128i j i32 k 的值均在 127 和 127 之间 我认为伪代码如下 result i8 vec1 i8 0
  • Ruby 在 Windows 上找不到 sqlite3 驱动程序

    我正在尝试在 Windows 上设置 Ruby on Rails 我正在使用看起来不错的 Flash Rails 发行版 但是 sqlite3 有一个问题 我发现线程告诉我安装版本 1 2 3 安装得很好 我使用的是 ruby 1 9 0
  • 程序堆栈真的会溢出吗?

    如果达到堆栈大小限制 处理器是否会导致操作系统出现 TRAP 从而防止堆栈溢出 P 我相信 Windows 确实有一个堆栈 当您到达末尾时它会增长 在 Visual Studio 编译器中 负责此操作的代码位于chkstk obj modu