-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathunwind_data.rs
More file actions
443 lines (395 loc) · 15.4 KB
/
Copy pathunwind_data.rs
File metadata and controls
443 lines (395 loc) · 15.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
//! Serialized unwind data captured per shared object, used to unwind stacks
//! during post-processing.
//!
//! [`UnwindData`] aliases the current version ([`UnwindDataV4`]). On-disk
//! artifacts are wrapped in `UnwindDataCompat` so older versions can still be
//! deserialized and (where possible) upgraded via `From` impls.
//!
//! # Version history
//!
//! - **V1** — Original format. Carries both the pid-agnostic unwind tables
//! (`eh_frame`/`eh_frame_hdr`) and per-pid load info (`avma_range`,
//! `base_avma`) in a single struct.
//! - **V2** — Adds `timestamp: Option<u64>` to mark when the data was captured
//! (`None` = valid for the whole execution). Upgradable from V1.
//! - **V3** — Splits per-pid load info out into [`ProcessUnwindData`], leaving
//! only the deduplicated, pid-agnostic tables. *Breaking*: cannot be parsed
//! as V2 (the per-pid fields are gone). Upgradable from V2.
//! - **V4** — Makes `eh_frame_hdr`/`eh_frame_hdr_svma` optional. The hdr is just
//! a binary-search index into `.eh_frame`; some binaries (e.g. Valgrind's
//! statically-linked tools, linked without `ld --eh-frame-hdr`) omit it and
//! the parser rebuilds the index from `.eh_frame`. Upgradable from V3.
//!
//! When adding a version: add a `UnwindDataV{N}` struct, a `From<V{N-1}>` impl
//! (if non-breaking), a `UnwindDataCompat` variant, update the `UnwindData`
//! alias and `parse`/`save_to`, and append an entry above.
use core::{
fmt::Debug,
hash::{Hash, Hasher},
};
use serde::{Deserialize, Serialize};
use std::io::BufWriter;
use std::{hash::DefaultHasher, ops::Range};
pub const UNWIND_FILE_EXT: &str = "unwind_data";
pub type UnwindData = UnwindDataV4;
impl UnwindData {
pub fn parse(reader: &[u8]) -> anyhow::Result<Self> {
let compat: UnwindDataCompat = bincode::deserialize(reader)?;
match compat {
UnwindDataCompat::V1(_) => {
anyhow::bail!("Cannot parse V1 unwind data as V4 (breaking changes)")
}
UnwindDataCompat::V2(_) => {
anyhow::bail!("Cannot parse V2 unwind data as V4 (breaking changes)")
}
UnwindDataCompat::V3(v3) => Ok(v3.into()),
UnwindDataCompat::V4(v4) => Ok(v4),
}
}
pub fn save_to<P: AsRef<std::path::Path>>(&self, folder: P, key: &str) -> anyhow::Result<()> {
let path = folder.as_ref().join(format!("{key}.{UNWIND_FILE_EXT}"));
let compat = UnwindDataCompat::V4(self.clone());
let file = std::fs::File::create(&path)?;
const BUFFER_SIZE: usize = 256 * 1024;
let writer = BufWriter::with_capacity(BUFFER_SIZE, file);
bincode::serialize_into(writer, &compat)?;
Ok(())
}
}
/// A versioned enum for `UnwindData` to allow for future extensions while maintaining backward compatibility.
#[derive(Serialize, Deserialize)]
enum UnwindDataCompat {
V1(UnwindDataV1),
V2(UnwindDataV2),
V3(UnwindDataV3),
V4(UnwindDataV4),
}
#[doc(hidden)]
#[derive(Serialize, Deserialize, Clone)]
struct UnwindDataV1 {
pub path: String,
pub avma_range: Range<u64>,
pub base_avma: u64,
pub base_svma: u64,
pub eh_frame_hdr: Vec<u8>,
pub eh_frame_hdr_svma: Range<u64>,
pub eh_frame: Vec<u8>,
pub eh_frame_svma: Range<u64>,
}
#[doc(hidden)]
#[derive(Serialize, Deserialize, Clone, PartialEq)]
pub struct UnwindDataV2 {
pub path: String,
/// The monotonic timestamp when the unwind data was captured.
/// Is `None` if unwind data is valid for the whole program execution
pub timestamp: Option<u64>,
pub avma_range: Range<u64>,
pub base_avma: u64,
pub base_svma: u64,
pub eh_frame_hdr: Vec<u8>,
pub eh_frame_hdr_svma: Range<u64>,
pub eh_frame: Vec<u8>,
pub eh_frame_svma: Range<u64>,
}
impl UnwindDataV2 {
/// Parse unwind data bytes, converting V1 to V2 but erroring on V3
/// (since V3 doesn't have the per-pid fields needed for V2).
pub fn parse(reader: &[u8]) -> anyhow::Result<Self> {
let compat: UnwindDataCompat = bincode::deserialize(reader)?;
match compat {
UnwindDataCompat::V1(v1) => Ok(v1.into()),
UnwindDataCompat::V2(v2) => Ok(v2),
UnwindDataCompat::V3(_) => {
anyhow::bail!("Cannot parse V3 unwind data as V2 (missing per-pid fields)")
}
UnwindDataCompat::V4(_) => {
anyhow::bail!("Cannot parse V4 unwind data as V2 (missing per-pid fields)")
}
}
}
}
impl From<UnwindDataV1> for UnwindDataV2 {
fn from(v1: UnwindDataV1) -> Self {
Self {
path: v1.path,
timestamp: None,
avma_range: v1.avma_range,
base_avma: v1.base_avma,
base_svma: v1.base_svma,
eh_frame_hdr: v1.eh_frame_hdr,
eh_frame_hdr_svma: v1.eh_frame_hdr_svma,
eh_frame: v1.eh_frame,
eh_frame_svma: v1.eh_frame_svma,
}
}
}
/// Pid-agnostic unwind data.
/// Contains only the data that is common across all PIDs loading the same shared library.
#[derive(Serialize, Deserialize, Clone, PartialEq, Eq, Hash)]
pub struct UnwindDataV3 {
pub path: String,
pub base_svma: u64,
pub eh_frame_hdr: Vec<u8>,
pub eh_frame_hdr_svma: Range<u64>,
pub eh_frame: Vec<u8>,
pub eh_frame_svma: Range<u64>,
}
impl From<UnwindDataV2> for UnwindDataV3 {
fn from(v2: UnwindDataV2) -> Self {
Self {
path: v2.path,
base_svma: v2.base_svma,
eh_frame_hdr: v2.eh_frame_hdr,
eh_frame_hdr_svma: v2.eh_frame_hdr_svma,
eh_frame: v2.eh_frame,
eh_frame_svma: v2.eh_frame_svma,
}
}
}
/// Pid-agnostic unwind data with an optional `.eh_frame_hdr`.
///
/// The hdr is only a binary-search index into `.eh_frame` — some binaries
/// (e.g. Valgrind's statically-linked tools) are linked without
/// `ld --eh-frame-hdr` and don't carry it. The parser rebuilds the index from
/// `.eh_frame` in that case.
#[derive(Serialize, Deserialize, Clone, PartialEq, Eq, Hash)]
pub struct UnwindDataV4 {
pub path: String,
pub base_svma: u64,
pub eh_frame_hdr: Option<Vec<u8>>,
pub eh_frame_hdr_svma: Option<Range<u64>>,
pub eh_frame: Vec<u8>,
pub eh_frame_svma: Range<u64>,
}
impl From<UnwindDataV3> for UnwindDataV4 {
fn from(v3: UnwindDataV3) -> Self {
Self {
path: v3.path,
base_svma: v3.base_svma,
eh_frame_hdr: Some(v3.eh_frame_hdr),
eh_frame_hdr_svma: Some(v3.eh_frame_hdr_svma),
eh_frame: v3.eh_frame,
eh_frame_svma: v3.eh_frame_svma,
}
}
}
impl Debug for UnwindDataV2 {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let eh_frame_hdr_hash = {
let mut hasher = DefaultHasher::new();
self.eh_frame_hdr.hash(&mut hasher);
hasher.finish()
};
let eh_frame_hash = {
let mut hasher = DefaultHasher::new();
self.eh_frame.hash(&mut hasher);
hasher.finish()
};
f.debug_struct("UnwindData")
.field("path", &self.path)
.field("timestamp", &self.timestamp)
.field("avma_range", &format_args!("{:x?}", self.avma_range))
.field("base_avma", &format_args!("{:x}", self.base_avma))
.field("base_svma", &format_args!("{:x}", self.base_svma))
.field(
"eh_frame_hdr_svma",
&format_args!("{:x?}", self.eh_frame_hdr_svma),
)
.field("eh_frame_hdr_hash", &format_args!("{eh_frame_hdr_hash:x}"))
.field("eh_frame_hash", &format_args!("{eh_frame_hash:x}"))
.field("eh_frame_svma", &format_args!("{:x?}", self.eh_frame_svma))
.finish()
}
}
impl Debug for UnwindDataV3 {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let eh_frame_hdr_hash = {
let mut hasher = DefaultHasher::new();
self.eh_frame_hdr.hash(&mut hasher);
hasher.finish()
};
let eh_frame_hash = {
let mut hasher = DefaultHasher::new();
self.eh_frame.hash(&mut hasher);
hasher.finish()
};
f.debug_struct("UnwindData")
.field("path", &self.path)
.field("base_svma", &format_args!("{:x}", self.base_svma))
.field(
"eh_frame_hdr_svma",
&format_args!("{:x?}", self.eh_frame_hdr_svma),
)
.field("eh_frame_hdr_hash", &format_args!("{eh_frame_hdr_hash:x}"))
.field("eh_frame_hash", &format_args!("{eh_frame_hash:x}"))
.field("eh_frame_svma", &format_args!("{:x?}", self.eh_frame_svma))
.finish()
}
}
impl Debug for UnwindDataV4 {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let eh_frame_hdr_hash = self.eh_frame_hdr.as_ref().map(|eh_frame_hdr| {
let mut hasher = DefaultHasher::new();
eh_frame_hdr.hash(&mut hasher);
hasher.finish()
});
let eh_frame_hash = {
let mut hasher = DefaultHasher::new();
self.eh_frame.hash(&mut hasher);
hasher.finish()
};
f.debug_struct("UnwindData")
.field("path", &self.path)
.field("base_svma", &format_args!("{:x}", self.base_svma))
.field(
"eh_frame_hdr_svma",
&format_args!("{:x?}", self.eh_frame_hdr_svma),
)
.field("eh_frame_hdr_hash", &format_args!("{eh_frame_hdr_hash:x?}"))
.field("eh_frame_hash", &format_args!("{eh_frame_hash:x}"))
.field("eh_frame_svma", &format_args!("{:x?}", self.eh_frame_svma))
.finish()
}
}
/// Per-pid mounting info referencing a deduplicated unwind data entry.
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct MappedProcessUnwindData {
pub unwind_data_key: String,
#[serde(flatten)]
pub inner: ProcessUnwindData,
}
#[derive(Serialize, Deserialize, Clone)]
pub struct ProcessUnwindData {
pub timestamp: Option<u64>,
pub avma_range: Range<u64>,
pub base_avma: u64,
}
impl Debug for ProcessUnwindData {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("ProcessUnwindData")
.field("timestamp", &self.timestamp)
.field("avma_range", &format_args!("{:x?}", self.avma_range))
.field("base_avma", &format_args!("{:x}", self.base_avma))
.finish()
}
}
#[cfg(test)]
mod tests {
use super::*;
const V2_BINARY: &[u8] = include_bytes!("../testdata/unwind_data_v2.bin");
const V3_BINARY: &[u8] = include_bytes!("../testdata/unwind_data_v3.bin");
const V4_BINARY: &[u8] = include_bytes!("../testdata/unwind_data_v4.bin");
fn create_sample_v2() -> UnwindDataV2 {
UnwindDataV2 {
path: "/lib/test.so".to_string(),
timestamp: Some(12345),
avma_range: 0x1000..0x2000,
base_avma: 0x1000,
base_svma: 0x0,
eh_frame_hdr: vec![1, 2, 3, 4],
eh_frame_hdr_svma: 0x100..0x200,
eh_frame: vec![5, 6, 7, 8],
eh_frame_svma: 0x200..0x300,
}
}
fn create_sample_v3() -> UnwindDataV3 {
UnwindDataV3 {
path: "/lib/test.so".to_string(),
base_svma: 0x0,
eh_frame_hdr: vec![1, 2, 3, 4],
eh_frame_hdr_svma: 0x100..0x200,
eh_frame: vec![5, 6, 7, 8],
eh_frame_svma: 0x200..0x300,
}
}
fn create_sample_v4() -> UnwindDataV4 {
UnwindDataV4 {
path: "/lib/test.so".to_string(),
base_svma: 0x0,
// No `.eh_frame_hdr`, like Valgrind's statically-linked tools
eh_frame_hdr: None,
eh_frame_hdr_svma: None,
eh_frame: vec![5, 6, 7, 8],
eh_frame_svma: 0x200..0x300,
}
}
#[test]
#[ignore = "one-off generator for the V4 testdata artifact"]
fn generate_v4_testdata() {
let compat = UnwindDataCompat::V4(create_sample_v4());
let bytes = bincode::serialize(&compat).unwrap();
std::fs::write("testdata/unwind_data_v4.bin", bytes).unwrap();
}
#[test]
fn test_parse_v2_as_v4_should_error() {
// Try to parse V2 binary artifact as V4 using UnwindData::parse
let result = UnwindData::parse(V2_BINARY);
// Should error due to breaking changes between V2 and V4
assert!(result.is_err());
let err = result.unwrap_err();
assert!(
err.to_string()
.contains("Cannot parse V2 unwind data as V4"),
"Expected error message about V2->V4 incompatibility, got: {err}"
);
}
#[test]
fn test_parse_v3_as_v2_should_error() {
// Try to parse V3 binary artifact as V2 using UnwindDataV2::parse
let result = UnwindDataV2::parse(V3_BINARY);
// Should error with specific message about missing per-pid fields
assert!(result.is_err());
let err = result.unwrap_err();
assert!(
err.to_string()
.contains("Cannot parse V3 unwind data as V2"),
"Expected error message about V3->V2 incompatibility, got: {err}"
);
}
#[test]
fn test_parse_v4_as_v2_should_error() {
// Try to parse V4 binary artifact as V2 using UnwindDataV2::parse
let result = UnwindDataV2::parse(V4_BINARY);
// Should error with specific message about missing per-pid fields
assert!(result.is_err());
let err = result.unwrap_err();
assert!(
err.to_string()
.contains("Cannot parse V4 unwind data as V2"),
"Expected error message about V4->V2 incompatibility, got: {err}"
);
}
#[test]
fn test_parse_v3_as_v4() {
// Parse V3 binary artifact using UnwindData::parse — it converts to V4
let parsed = UnwindData::parse(V3_BINARY).expect("Failed to parse V3 data as V4");
// Should match the V3 data with the hdr fields wrapped in `Some`
let expected: UnwindDataV4 = create_sample_v3().into();
assert_eq!(parsed, expected);
assert!(parsed.eh_frame_hdr.is_some());
}
#[test]
fn test_parse_v4_as_v4() {
// Parse V4 binary artifact as V4 using UnwindData::parse
let parsed_v4 = UnwindData::parse(V4_BINARY).expect("Failed to parse V4 data as V4");
// Should match expected V4 data (without an eh_frame_hdr)
let expected_v4 = create_sample_v4();
assert_eq!(parsed_v4, expected_v4);
}
#[test]
fn test_parse_v2_as_v2() {
// Parse V2 binary artifact as V2 using UnwindDataV2::parse
let parsed_v2 = UnwindDataV2::parse(V2_BINARY).expect("Failed to parse V2 data as V2");
// Should match expected V2 data
let expected_v2 = create_sample_v2();
assert_eq!(parsed_v2.path, expected_v2.path);
assert_eq!(parsed_v2.timestamp, expected_v2.timestamp);
assert_eq!(parsed_v2.avma_range, expected_v2.avma_range);
assert_eq!(parsed_v2.base_avma, expected_v2.base_avma);
assert_eq!(parsed_v2.base_svma, expected_v2.base_svma);
assert_eq!(parsed_v2.eh_frame_hdr, expected_v2.eh_frame_hdr);
assert_eq!(parsed_v2.eh_frame_hdr_svma, expected_v2.eh_frame_hdr_svma);
assert_eq!(parsed_v2.eh_frame, expected_v2.eh_frame);
assert_eq!(parsed_v2.eh_frame_svma, expected_v2.eh_frame_svma);
}
}