rust - 为什么 Rust 堆栈帧这么大?
问题描述
我遇到了意外的早期堆栈溢出,并创建了以下程序来测试该问题:
#![feature(asm)]
#[inline(never)]
fn get_rsp() -> usize {
let rsp: usize;
unsafe {
asm! {
"mov {}, rsp",
out(reg) rsp
}
}
rsp
}
fn useless_function(x: usize) {
if x > 0 {
println!("{:x}", get_rsp());
useless_function(x - 1);
}
}
fn main() {
useless_function(10);
}
这是get_rsp
拆卸的(根据cargo-asm):
tests::get_rsp:
push rax
#APP
mov rax, rsp
#NO_APP
pop rcx
ret
我不确定什么#APP
和#NO_APP
做什么或为什么rax
被推送然后弹出rcx
,但似乎该函数确实返回了堆栈指针。
我惊讶地发现,在调试模式下,连续打印两次之间的差异rsp
是 192(!),即使在发布模式下也是 128。据我了解,每次调用需要存储的useless_function
只是一个usize
和一个返回地址,所以我希望每个堆栈帧大约 16 个字节大。
我在rustc 1.46.0
64 位 Windows 机器上运行它。
我的结果在机器上是否一致?这是如何解释的?
似乎使用println!
有相当显着的效果。为了避免这种情况,我更改了程序(感谢@Shepmaster 的想法)以将值存储在静态数组中:
static mut RSPS: [usize; 10] = [0; 10];
#[inline(never)]
fn useless_function(x: usize) {
unsafe { RSPS[x] = get_rsp() };
if x == 0 {
return;
}
useless_function(x - 1);
}
fn main() {
useless_function(9);
println!("{:?}", unsafe { RSPS });
}
递归在发布模式下得到了优化,但在调试模式下,每帧仍然需要 80 个字节,这比我预期的要多得多。这只是堆栈帧在 x86 上的工作方式吗?其他语言做得更好吗?这似乎有点低效。
解决方案
使用格式化机制,如println!
在堆栈上创建许多东西。扩展代码中使用的宏:
fn useless_function(x: usize) {
if x > 0 {
{
::std::io::_print(::core::fmt::Arguments::new_v1(
&["", "\n"],
&match (&get_rsp(),) {
(arg0,) => [::core::fmt::ArgumentV1::new(
arg0,
::core::fmt::LowerHex::fmt,
)],
},
));
};
useless_function(x - 1);
}
}
我相信这些结构占用了大部分空间。为了证明这一点,我打印了由 创建的值的大小,由format_args
使用println!
:
let sz = std::mem::size_of_val(&format_args!("{:x}", get_rsp()));
println!("{}", sz);
这表明它是 48 个字节。
也可以看看:
像这样的东西应该从等式中删除打印,但是编译器/优化器会忽略inline(never)
这里的提示并内联它,导致顺序值都相同。
/// SAFETY:
/// The length of `rsp` and the value of `x` must always match
#[inline(never)]
unsafe fn useless_function(x: usize, rsp: &mut [usize]) {
if x > 0 {
*rsp.get_unchecked_mut(0) = get_rsp();
useless_function(x - 1, rsp.get_unchecked_mut(1..));
}
}
fn main() {
unsafe {
let mut rsp = [0; 10];
useless_function(rsp.len(), &mut rsp);
for w in rsp.windows(2) {
println!("{}", w[0] - w[1]);
}
}
}
也就是说,您可以公开该函数并查看它的程序集(稍微清理一下):
playground::useless_function:
pushq %r15
pushq %r14
pushq %rbx
testq %rdi, %rdi
je .LBB6_3
movq %rsi, %r14
movq %rdi, %r15
xorl %ebx, %ebx
.LBB6_2:
callq playground::get_rsp
movq %rax, (%r14,%rbx,8)
addq $1, %rbx
cmpq %rbx, %r15
jne .LBB6_2
.LBB6_3:
popq %rbx
popq %r14
popq %r15
retq
但在调试模式下,每帧仍然需要 80 个字节
比较未优化的程序集:
playground::useless_function:
subq $104, %rsp
movq %rdi, 80(%rsp)
movq %rsi, 88(%rsp)
movq %rdx, 96(%rsp)
cmpq $0, %rdi
movq %rdi, 56(%rsp) # 8-byte Spill
movq %rsi, 48(%rsp) # 8-byte Spill
movq %rdx, 40(%rsp) # 8-byte Spill
ja .LBB44_2
jmp .LBB44_8
.LBB44_2:
callq playground::get_rsp
movq %rax, 32(%rsp) # 8-byte Spill
xorl %eax, %eax
movl %eax, %edx
movq 48(%rsp), %rdi # 8-byte Reload
movq 40(%rsp), %rsi # 8-byte Reload
callq core::slice::<impl [T]>::get_unchecked_mut
movq %rax, 24(%rsp) # 8-byte Spill
movq 24(%rsp), %rax # 8-byte Reload
movq 32(%rsp), %rcx # 8-byte Reload
movq %rcx, (%rax)
movq 56(%rsp), %rdx # 8-byte Reload
subq $1, %rdx
setb %sil
testb $1, %sil
movq %rdx, 16(%rsp) # 8-byte Spill
jne .LBB44_9
movq $1, 72(%rsp)
movq 72(%rsp), %rdx
movq 48(%rsp), %rdi # 8-byte Reload
movq 40(%rsp), %rsi # 8-byte Reload
callq core::slice::<impl [T]>::get_unchecked_mut
movq %rax, 8(%rsp) # 8-byte Spill
movq %rdx, (%rsp) # 8-byte Spill
movq 16(%rsp), %rdi # 8-byte Reload
movq 8(%rsp), %rsi # 8-byte Reload
movq (%rsp), %rdx # 8-byte Reload
callq playground::useless_function
jmp .LBB44_8
.LBB44_8:
addq $104, %rsp
retq
.LBB44_9:
leaq str.0(%rip), %rdi
leaq .L__unnamed_7(%rip), %rdx
movq core::panicking::panic@GOTPCREL(%rip), %rax
movl $33, %esi
callq *%rax
ud2
推荐阅读
- php - 更改发件人姓名和电子邮件 laravel
- javascript - 当组件有自己的 vuex 存储设置时,无法将组件用作插件
- python - ValueError: 不支持或无效的 CSS 选择器“div[class='card-content””
- javascript - 在 setTimeout 函数期间未更新 React Hooks 状态变量
- qt - qt qml 将图片从web服务器显示到我的qt快应用
- c - 为什么我不能使用 C 打印此文件中的整个消息?
- python - 运行 solov2 代码时 google colab 中的错误
- android - 一旦设备启动/重启,如何使应用程序在后台运行
- time - 为什么两个计时时间点的差异会导致秒数?
- c++ - 调试延迟加载dll