锈弦容量低于弦长

逆向工程 调试 记忆
2021-06-24 19:51:43

根据 RUST 文档,字符串以这种方式存储:

在此处输入图片说明

这是我可以在反转 Rust 二进制文件时验证的声明。问题是,当我反转 Rust 二进制文件时,我可能会遇到容量小于字符串长度的情况,例如:

[stack]:00007FFC8BE97218 str_ABCED dq offset ABCED               ; DATA XREF: XX
[stack]:00007FFC8BE97218                                         ; (len=10) ABCEDFGHIJ
[stack]:00007FFC8BE97220 dq 28h                                  ; String len: 40
[stack]:00007FFC8BE97228 cap_ABCED dq 25h                        ; String 'capacity'

这样的事情怎么可能?是否存在解释 Rust 内部结构的资源?

1个回答

您是否考虑过它可能使用str
而不是只有长度而没有容量String并且
您正在查看虚假的第三个值的可能性

:\>cat main.rs
fn main() {
        let s1 = String::from("Hello, std::world!");
    println!("{}",s1);
        println!("{} {}",s1.capacity() , s1.len());
        let s2 = "Hello, std::world!o";
    println!("{}",s2);
        println!("{} {}",s2.capacity() , s2.len());
}
:\>cargo build
   Compiling hello_world v0.1.0 
error[E0599]: no method named `capacity` found for reference `&str` in the current scope
 --> main.rs:7:22
  |
7 |     println!("{} {}",s2.capacity() , s2.len());
  |                         ^^^^^^^^ method not found in `&str`

如下所示的第一个字符串

0:000> dv /v 
00000064`f0cff920              s2 = struct str*
00000064`f0cff8c0              s1 = "Hello, std::world!"
0:000> dpa 00000064`f0cff920 l3
00000064`f0cff920  00007ff6`65cf2638 "Hello, std::world!o"
00000064`f0cff928  00000000`00000013
00000064`f0cff930  00000000`00000010  <<<<<<<  bogus garbage 
0:000> dpa 00000064`f0cff8c0 l3
00000064`f0cff8c0  00000174`116191f0 "Hello, std::world!.............................."
00000064`f0cff8c8  00000000`00000012
00000064`f0cff8d0  00000000`00000012  <<<<<<<<<<<<< correct