【问题标题】:How to read the assembly code of this piece of Rust code?如何阅读这段 Rust 代码的汇编代码?
【发布时间】:2021-04-22 17:17:14
【问题描述】:

我正在尝试阅读一段 Rust 汇编代码,但实际上,它比 C/C++ 编译器生成的 ASM 代码更难阅读。那么,如何分析下面这段 Rust 代码的 ASM 代码呢?

fn main() { 
    let closure = |x| println!("{}", x);
    let x: fn(x: i32) -> () = closure; 
    println!("{}", x as i32);
}

对应的汇编代码如下所示,带有一些 cmets(我只粘贴了主要部分,完整版请使用此永久链接:https://play.rust-lang.org/?version=nightly&mode=release&edition=2018&gist=e7ba4844f1ce6e881912dc074152988d):

playground::main: # @playground::main
# %bb.0:
    subq    $72, %rsp
    leaq    core::ops::function::FnOnce::call_once(%rip), %rax
    movl    %eax, 4(%rsp)
    leaq    4(%rsp), %rax
    movq    %rax, 8(%rsp)
    movq    core::fmt::num::imp::<impl core::fmt::Display for i32>::fmt@GOTPCREL(%rip), %rax
    movq    %rax, 16(%rsp)
    leaq    .L__unnamed_2(%rip), %rax  # the contents of rdx come from .L__unnamed_2(%rip), how to evaluate this part?
    movq    %rax, 24(%rsp)  # the contents of rdi come from rax.
    movq    $2, 32(%rsp)
    movq    $0, 40(%rsp)
    leaq    8(%rsp), %rax
    movq    %rax, 56(%rsp)
    movq    $1, 64(%rsp)
    leaq    24(%rsp), %rdi  # rdi should be the register holding the value passed to println!.
    callq   *std::io::stdio::_print@GOTPCREL(%rip)
    addq    $72, %rsp
    retq
                                        # -- End function

main:                                   # @main
# %bb.0:
    subq    $8, %rsp
    movq    %rsi, %rcx
    movslq  %edi, %rdx
    leaq    playground::main(%rip), %rax
    movq    %rax, (%rsp)
    leaq    .L__unnamed_1(%rip), %rsi
    movq    %rsp, %rdi
    callq   *std::rt::lang_start_internal@GOTPCREL(%rip)
                                        # kill: def $eax killed $eax killed $rax
    popq    %rcx
    retq
                                        # -- End function

.L__unnamed_1:
    .quad   core::ptr::drop_in_place<std::rt::lang_start<()>::{{closure}}>
    .quad   8                               # 0x8
    .quad   8                               # 0x8
    .quad   std::rt::lang_start::{{closure}}
    .quad   std::rt::lang_start::{{closure}}
    .quad   core::ops::function::FnOnce::call_once{{vtable.shim}}

.L__unnamed_3:

.L__unnamed_4:
    .byte   10

.L__unnamed_2:
    .quad   .L__unnamed_3
    .zero   8
    .quad   .L__unnamed_4
    .asciz  "\001\000\000\000\000\000\000"

而且,我试图找出 Rust 编译器如何处理闭包函数指针与普通函数。因此,在这里我尝试使用闭包作为示例,但似乎找不到任何与使用变量“x”相对应的有效汇编代码。

【问题讨论】:

  • 你确定 playground::main 的顶部复制粘贴正确吗? leaq core::ops::function::FnOnce::call_once(%rip), %rax / movl %eax, 4(%rsp) ??编译器是否有某种原因会 LEA 一个 64 位地址,然后只将它的低 32 位存储到本地?嗯,godbolt.org/z/z8j8razx7 确认了代码生成。看起来很奇怪。至少错过了一次优化;如果以某种方式截断指针有意义,则可以在 LEA 中使用 32 位操作数大小。还是你用as i32 做的?
  • 是的,经过测试。如果您将as i32 更改为as i64,则movl %eax, 4(%rsp) 将更改为movq %rax, (%rsp)
  • 另请注意:# the contents of rdi come from rax. 不正确,因为“寄存器的内容 = 它的值”的正常含义。当_print 被调用时,RDI指向这个结构,它以指向.L__unnamed_2 的指针开头,从RAX 存储到24(%rsp)。请注意,它是 LEA 而不是 MOV 重新加载,因此它不会将此 RAX 复制到以后的 RDI 中。 (也许您的意思是“RDI 指向的内存内容”,而不是“RDI 的内容”?)
  • 那么,为了像我这样的 Rust 新手,这段代码实际上在做什么?它正在打印(地址?)一个闭包,转换为 i32?这似乎与 asm 一致,我猜编译器选择不优化闭包对象本身的内容,即使您的代码截断了指针,所以_print 实际上无法再取消它了。
  • YHSPY,一种您可能会发现在没有所有println! 内容的情况下分析汇编代码很有用的技术是编写一个函数,该函数接受一个函数指针并使用值调用它:example。由于函数指针可以做任何事情,这将阻止 LLVM 优化整个函数,但不会添加一堆与函数的其余部分混合的格式化代码。 (除非格式化代码是您问题的重点)

标签: assembly rust x86-64 reverse-engineering


【解决方案1】:

没有对闭包的实际调用,因此没有生成调用代码,但是 x 变量的使用实际上是在您的帖子中未包含的函数中,该函数在 ASM 输出中具有误导性名称 core::ops::function::FnOnce::call_once ,但在同一个 Playground 示例的 LLVM 输出中具有更混乱的名称 @_ZN4core3ops8function6FnOnce9call_once17hefa1aa47132c4122E。这是闭包的实际内容(println!("{}", x)

core::ops::function::FnOnce::call_once: # @core::ops::function::FnOnce::call_once
# %bb.0:
 # allocate a bunch of stack space for variables and print arguments
 subq   $72, %rsp

 # %edi has the value of x passed in to the closure, which we store in a new stack allocated variable
 movl   %edi, 4(%rsp)

 # we then load the address of that variable into another variable
 leaq   4(%rsp), %rax
 movq   %rax, 8(%rsp)

 # the following is mostly populating the std::fmt::Arguments struct which is passed to print
 movq   core::fmt::num::imp::<impl core::fmt::Display for i32>::fmt@GOTPCREL(%rip), %rax
 movq   %rax, 16(%rsp)
 leaq   .L__unnamed_2(%rip), %rax
 movq   %rax, 24(%rsp)
 movq   $2, 32(%rsp)
 movq   $0, 40(%rsp)

 # the address of the address of x is loaded into the arguments struct here
 leaq   8(%rsp), %rax
 movq   %rax, 56(%rsp)

 # finish populating the arguments and then call print
 movq   $1, 64(%rsp)
 leaq   24(%rsp), %rdi
 callq  *std::io::stdio::_print@GOTPCREL(%rip)
 addq   $72, %rsp
 retq

playground main 函数是创建闭包的地方,但它实际上并没有被调用,并且和上面的函数一样,主要是填充复杂的 std::fmt::Arguments 结构

playground::main: # @playground::main
# %bb.0:
subq    $72, %rsp

 # this creates the closure by storing a pointer to the closure's function
leaq    core::ops::function::FnOnce::call_once(%rip), %rax
movl    %eax, 4(%rsp)

 # this stores the closure in main's `x` variable (line 3 of the example)
leaq    4(%rsp), %rax
movq    %rax, 8(%rsp)

 # populate the std::fmt::Arguments struct
movq    core::fmt::num::imp::<impl core::fmt::Display for i32>::fmt@GOTPCREL(%rip), %rax
movq    %rax, 16(%rsp)
leaq    .L__unnamed_2(%rip), %rax  # the contents of rdx come from .L__unnamed_2(%rip), how to evaluate this part?
movq    %rax, 24(%rsp)  # the contents of rdi come from rax.
movq    $2, 32(%rsp)
movq    $0, 40(%rsp)

 # store the closure (stored in `x`) in the std::fmt::Arguments struct
leaq    8(%rsp), %rax
movq    %rax, 56(%rsp)

 # finish populating and call print
movq    $1, 64(%rsp)
leaq    24(%rsp), %rdi  # rdi should be the register holding the value passed to println!.
callq   *std::io::stdio::_print@GOTPCREL(%rip)
addq    $72, %rsp
retq

从 LLVM 输出中,std::fmt::Arguments 被定义为%"std::fmt::Arguments" = type { [0 x i64], { [0 x { [0 x i8]*, i64 }]*, i64 }, [0 x i64], { i64*, i64 }, [0 x i64], { [0 x { i8*, i64* }]*, i64 }, [0 x i64] },我不太了解内部细节,所以我不确定它究竟为什么引用静态内存区域.L__unnamed_2 但深入研究std::fmt::Arguments 可能会提供更多线索

【讨论】:

  • 执行流程如何重定向到这个“core::ops::function::FnOnce::call_once”部分?我的意思是起点应该是“主要”,对吗?但是“core::ops::function::FnOnce::call_once”不是“main”的一部分,我没有看到任何关于它的callq指令。
  • 没错,因为正如我所提到的,没有从 main() 调用闭包。如果您添加对闭包的调用并编译它,它将生成对core::ops::function::FnOnce::call_once 的调用,但通常它会被优化为从 main 直接调用,而不是使用闭包值本身,因为有问题的代码相当简单,而且优化器相当激进
猜你喜欢
  • 2021-10-07
  • 2023-03-14
  • 2019-10-11
  • 2023-03-26
  • 2015-05-12
  • 2014-02-20
  • 2010-12-02
  • 2012-06-01
  • 1970-01-01
相关资源
最近更新 更多