首页 时政热点 科技头条 智能AI 安全攻防 数码硬件 开发者生态 汽车 游戏 社会热点 开源推荐 医疗健康 归档 标签 关于

像 alloca 这样的函数如何从堆栈分配内存?

摘要

A little while ago, I talked about how compilers ensure that large stack allocations do not skip over the guard page . Shawn Van Ness was curious how this works with _alloca . “Does it do the necessar...

the rcx rbp rsp lea stack for void local frame
2026-08-18 1 阅读 约2分钟阅读 ingve
分享:
字号:
不久前,我谈到了编译器如何确保大型堆栈分配不会跳过保护页。 Shawn Van Ness 很好奇这如何与 _alloca 配合使用。 “它是否进行了必要的 _chkstk() 探测?”是的,在调整已分配内存的堆栈指针之前,_alloca() 函数会调用相同的 _chkstk() 函数来探测堆栈。这是一个人为的例子:#include void Consumer(void*,void*); void f(int n) { 字符缓冲区[16384];消耗(分配(n),缓冲区);在 x86-64 上,这会导致 push rbp mov eax, 16416 ;探测本地帧调用 __chkstk sub rsp, rax ;创建本地帧 lea rbp, [rsp+32] movsxd rax, ecx ; n lea rcx, [rax+15] ;四舍五入为 16 的倍数和 rcx, -16 mov rax, rcx ;特殊的 __chkstk 调用约定 call __chkstk sub rsp, rcx ;分配n个字节 lea rdx, [rbp] ; rdx -> 缓冲区 lea rcx, [rsp+32] ; rcx -> 分配的内存调用消耗 lea rsp, [rbp+16384] ; clean up local frame pop rbp ret 0 请注意,相同的 __chkstk 函数既用于在创建本地帧时执行初始堆栈探测,也用于 alloca() 。
这篇文章对您有帮助吗?

订阅66必读

每日精选科技资讯,直达你的邮箱