hermit-run 0.4.0

Hermit: a deterministic Linux execution engine for reproducible runs, record and replay, and concurrency-race search.
Documentation
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
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
/*
 * Copyright (c) Meta Platforms, Inc. and affiliates.
 * All rights reserved.
 *
 * This source code is licensed under the BSD-style license found in the
 * LICENSE file in the root directory of this source tree.
 */

//! Debug-info resolution for happens-before anchors.
//!
//! The anchor/edge *model* lives in [`detcore_model::happens_before`]; it leaves
//! `func`/`file:line` code locations unresolved (a [`Position::Rip`] with
//! `addr: None`). This module turns those human-legible locations into concrete
//! instruction pointers by reading the target binary's symbol table (for
//! function entry addresses) and DWARF line program (for source lines), and
//! provides the reverse mapping (address → function/file/line) used by
//! introspection / `--list-events`.
//!
//! It builds *owned* indices in a single pass and drops the mmap/DWARF context,
//! so the resolver has no self-referential lifetimes and is cheap to keep around.

use std::borrow::Cow;
use std::collections::BTreeMap;
use std::path::Path;

use addr2line::gimli;
use anyhow::Context as _;
use detcore_model::happens_before::Anchor;
use detcore_model::happens_before::CodeLocation;
use detcore_model::happens_before::HappensBeforeProgram;
use detcore_model::happens_before::HappensBeforeSpec;
use detcore_model::happens_before::Position;
use object::Object;
use object::ObjectSection;
use object::ObjectSymbol;
use object::SymbolKind;

/// The virtual-address extent of a function symbol.
#[derive(Clone, Copy, Debug)]
struct FuncExtent {
    /// Entry virtual address.
    addr: u64,
    /// Size in bytes (0 when the symbol table does not record one).
    size: u64,
}

impl FuncExtent {
    /// True when `addr` falls within `[self.addr, self.addr + self.size)`. A
    /// zero-sized symbol only matches its exact entry address.
    fn contains(&self, addr: u64) -> bool {
        if self.size == 0 {
            addr == self.addr
        } else {
            addr >= self.addr && addr < self.addr + self.size
        }
    }
}

/// One resolved source-line row: an address and the file/line it maps to.
#[derive(Clone, Debug)]
struct LineRow {
    addr: u64,
    file: Option<String>,
    line: u32,
}

/// A resolved source location for introspection output.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct ResolvedLocation {
    /// Function name containing the address, if known.
    pub function: Option<String>,
    /// Source file basename, if known.
    pub file: Option<String>,
    /// Source line, if known.
    pub line: Option<u32>,
}

/// Owned debug-info indices for one binary.
pub struct DebugInfoResolver {
    /// Function name → extent (from the ELF symbol table).
    functions: BTreeMap<String, FuncExtent>,
    /// Line rows sorted ascending by address.
    lines: Vec<LineRow>,
}

impl DebugInfoResolver {
    /// Read `path` and build the symbol and line-number indices.
    pub fn open(path: &Path) -> anyhow::Result<Self> {
        let data = std::fs::read(path)
            .with_context(|| format!("reading binary for debug info: {}", path.display()))?;
        Self::from_bytes(&data)
            .with_context(|| format!("parsing debug info from {}", path.display()))
    }

    /// Build the indices from in-memory object bytes.
    pub fn from_bytes(data: &[u8]) -> anyhow::Result<Self> {
        let object = object::File::parse(data)?;

        // --- function symbols -------------------------------------------------
        let mut functions: BTreeMap<String, FuncExtent> = BTreeMap::new();
        for sym in object.symbols() {
            if sym.kind() != SymbolKind::Text {
                continue;
            }
            let addr = sym.address();
            if addr == 0 {
                continue;
            }
            if let Ok(name) = sym.name() {
                if name.is_empty() {
                    continue;
                }
                // Prefer the first definition (typically the primary symbol) but
                // upgrade a zero-sized entry if we later learn a real size.
                functions
                    .entry(name.to_string())
                    .and_modify(|e| {
                        if e.size == 0 && sym.size() > 0 {
                            *e = FuncExtent {
                                addr,
                                size: sym.size(),
                            };
                        }
                    })
                    .or_insert(FuncExtent {
                        addr,
                        size: sym.size(),
                    });
            }
        }

        // --- DWARF line rows --------------------------------------------------
        let lines = Self::load_line_rows(&object).unwrap_or_default();

        Ok(Self { functions, lines })
    }

    /// Parse the DWARF line programs into an address-sorted list of rows.
    fn load_line_rows(object: &object::File<'_>) -> anyhow::Result<Vec<LineRow>> {
        let endian = if object.is_little_endian() {
            gimli::RunTimeEndian::Little
        } else {
            gimli::RunTimeEndian::Big
        };

        let load_section = |id: gimli::SectionId| -> Result<Cow<'_, [u8]>, gimli::Error> {
            match object.section_by_name(id.name()) {
                Some(section) => Ok(section
                    .uncompressed_data()
                    .unwrap_or(Cow::Borrowed(&[][..]))),
                None => Ok(Cow::Borrowed(&[][..])),
            }
        };

        let dwarf_sections = gimli::DwarfSections::load(load_section)?;
        let dwarf = dwarf_sections.borrow(|section| gimli::EndianSlice::new(section, endian));

        let mut rows: Vec<LineRow> = Vec::new();
        let mut units = dwarf.units();
        while let Some(header) = units.next()? {
            let unit = dwarf.unit(header)?;
            let Some(program) = unit.line_program.clone() else {
                continue;
            };
            let mut state = program.rows();
            while let Some((header, row)) = state.next_row()? {
                if row.end_sequence() {
                    continue;
                }
                let Some(line) = row.line() else {
                    continue;
                };
                let file = row.file(header).and_then(|file_entry| {
                    dwarf
                        .attr_string(&unit, file_entry.path_name())
                        .ok()
                        .and_then(|r| r.to_string().ok().map(basename))
                });
                rows.push(LineRow {
                    addr: row.address(),
                    file,
                    line: line.get() as u32,
                });
            }
        }
        rows.sort_by_key(|r| r.addr);
        Ok(rows)
    }

    /// True when no debug/symbol information was found at all.
    pub fn is_empty(&self) -> bool {
        self.functions.is_empty() && self.lines.is_empty()
    }

    /// The entry address of a named function, if present in the symbol table.
    pub fn function_addr(&self, name: &str) -> Option<u64> {
        self.functions.get(name).map(|e| e.addr)
    }

    /// The lowest address whose line row matches `line` (and `file`, when given),
    /// optionally constrained to a `(addr, size)` extent (a zero size matches only
    /// the exact entry address).
    pub fn line_addr(
        &self,
        file: Option<&str>,
        line: u32,
        within: Option<(u64, u64)>,
    ) -> Option<u64> {
        let file_base = file.map(basename);
        let extent = within.map(|(addr, size)| FuncExtent { addr, size });
        self.lines
            .iter()
            .filter(|r| r.line == line)
            .filter(|r| match (&file_base, &r.file) {
                (Some(want), Some(have)) => want == have,
                (Some(_), None) => false,
                (None, _) => true,
            })
            .filter(|r| extent.map(|f| f.contains(r.addr)).unwrap_or(true))
            .map(|r| r.addr)
            .min()
    }

    /// Resolve a [`CodeLocation`] to a concrete address, preferring
    /// function+line (the address of `line` inside `function`), then bare
    /// function entry, then file:line.
    pub fn resolve_location(&self, loc: &CodeLocation) -> Option<u64> {
        match (&loc.function, loc.line) {
            (Some(func), Some(line)) => {
                let extent = self.functions.get(func).copied();
                self.line_addr(loc.file.as_deref(), line, extent.map(|e| (e.addr, e.size)))
                    // Fall back to the function entry if the exact line row is
                    // absent (e.g. no DWARF, or inlined line).
                    .or(extent.map(|e| e.addr))
            }
            (Some(func), None) => self.function_addr(func),
            (None, Some(line)) => self.line_addr(loc.file.as_deref(), line, None),
            (None, None) => None,
        }
    }

    /// Describe an address in source terms (nearest preceding line row and the
    /// enclosing function), for introspection output.
    pub fn describe(&self, addr: u64) -> ResolvedLocation {
        // Enclosing function: the entry whose extent contains `addr`, else the
        // closest preceding entry.
        let function = self
            .functions
            .iter()
            .filter(|(_, e)| e.contains(addr) || e.addr <= addr)
            .max_by_key(|(_, e)| e.addr)
            .map(|(name, _)| name.clone());

        // Nearest preceding line row.
        let idx = self.lines.partition_point(|r| r.addr <= addr);
        let line_row = if idx > 0 {
            self.lines.get(idx - 1)
        } else {
            None
        };

        ResolvedLocation {
            function,
            file: line_row.and_then(|r| r.file.clone()),
            line: line_row.map(|r| r.line),
        }
    }
}

/// Resolve every anchor in `program` whose position is an unresolved RIP carrying
/// a code location. Returns the names of anchors that could not be resolved.
pub fn resolve_program(
    program: &mut HappensBeforeProgram,
    resolver: &DebugInfoResolver,
) -> Vec<String> {
    let mut unresolved = Vec::new();
    for (name, anchor) in program.anchors.iter_mut() {
        if let Position::Rip { addr: None, nth } = &anchor.position {
            let nth = *nth;
            if let Some(resolved) = resolver.resolve_location(&anchor.location) {
                anchor.position = Position::Rip {
                    addr: Some(resolved),
                    nth,
                };
            } else {
                unresolved.push(name.clone());
            }
        }
    }
    unresolved
}

/// Load a happens-before specification from a JSON file and normalize it into a
/// [`HappensBeforeProgram`]. Code locations remain unresolved until
/// [`resolve_program`] runs against a [`DebugInfoResolver`].
pub fn load_program(path: &Path) -> anyhow::Result<HappensBeforeProgram> {
    let json = std::fs::read_to_string(path)
        .with_context(|| format!("reading happens-before spec: {}", path.display()))?;
    let spec = HappensBeforeSpec::from_json(&json)
        .with_context(|| format!("parsing happens-before spec: {}", path.display()))?;
    let program = spec
        .normalize()
        .with_context(|| format!("normalizing happens-before spec: {}", path.display()))?;
    Ok(program)
}

/// Render one anchor for `--list-events`-style introspection, showing the
/// resolved address and source description when available.
pub fn describe_anchor(anchor: &Anchor, resolver: Option<&DebugInfoResolver>) -> String {
    let mut out = format!("{}", anchor);
    if let (Position::Rip { addr: Some(a), .. }, Some(r)) = (&anchor.position, resolver) {
        let d = r.describe(*a);
        let where_ = match (&d.function, &d.file, d.line) {
            (Some(f), _, Some(l)) => format!(" -> {:#x} ({}:{})", a, f, l),
            (Some(f), _, None) => format!(" -> {:#x} ({})", a, f),
            _ => format!(" -> {:#x}", a),
        };
        out.push_str(&where_);
    }
    out
}

/// The final path component of a possibly-slashed path string.
fn basename(s: &str) -> String {
    match s.rsplit_once('/') {
        Some((_, base)) => base.to_string(),
        None => s.to_string(),
    }
}

#[cfg(test)]
mod tests {
    use std::process::Command;

    use detcore_model::happens_before::HappensBeforeSpec;

    use super::*;

    /// Compile a tiny -g fixture to a temp path; returns None if no C compiler.
    /// Each call gets a unique directory so parallel tests never collide.
    fn compile_fixture() -> Option<std::path::PathBuf> {
        use std::sync::atomic::AtomicUsize;
        use std::sync::atomic::Ordering;
        static COUNTER: AtomicUsize = AtomicUsize::new(0);

        let cc = ["cc", "gcc", "clang"]
            .into_iter()
            .find(|c| Command::new(c).arg("--version").output().is_ok())?;

        let uniq = COUNTER.fetch_add(1, Ordering::Relaxed);
        let dir = std::env::temp_dir().join(format!("hb_fixture_{}_{}", std::process::id(), uniq));
        std::fs::create_dir_all(&dir).ok()?;
        let src = dir.join("fixture.c");
        let bin = dir.join("fixture");
        std::fs::write(
            &src,
            "int free_buffer(int x) { return x + 1; }\n\
             int read_buffer(int x) { return x * 2; }\n\
             int main(void) { return free_buffer(read_buffer(3)); }\n",
        )
        .ok()?;
        let status = Command::new(cc)
            .args(["-g", "-O0", "-o"])
            .arg(&bin)
            .arg(&src)
            .status()
            .ok()?;
        if status.success() { Some(bin) } else { None }
    }

    #[test]
    fn resolves_function_and_line_from_dwarf() {
        let Some(bin) = compile_fixture() else {
            eprintln!("skipping: no C compiler available");
            return;
        };
        let resolver = DebugInfoResolver::open(&bin).unwrap();
        assert!(!resolver.is_empty(), "expected symbols/lines in -g binary");

        // Function entry resolves to a nonzero address.
        let free_addr = resolver.function_addr("free_buffer");
        assert!(free_addr.is_some(), "free_buffer should resolve");
        assert_ne!(free_addr.unwrap(), 0);

        // free_buffer is defined on line 1; its line row should fall within the
        // function extent.
        let loc = CodeLocation {
            function: Some("free_buffer".to_string()),
            file: None,
            line: Some(1),
        };
        let addr = resolver.resolve_location(&loc);
        assert!(addr.is_some(), "func+line should resolve");

        // Reverse description of the entry address names the function.
        let d = resolver.describe(free_addr.unwrap());
        assert_eq!(d.function.as_deref(), Some("free_buffer"));

        let _ = std::fs::remove_dir_all(bin.parent().unwrap());
    }

    #[test]
    fn resolve_program_fills_rips() {
        let Some(bin) = compile_fixture() else {
            eprintln!("skipping: no C compiler available");
            return;
        };
        let resolver = DebugInfoResolver::open(&bin).unwrap();

        let json = r#"{
          "version": 1,
          "threads": { "t": {"label": "t"} },
          "events": {
            "A": {"thread": "t", "func": "free_buffer"},
            "B": {"thread": "t", "func": "read_buffer", "line": 2}
          },
          "edges": [ {"before": "A", "after": "B"} ]
        }"#;
        let mut prog = HappensBeforeSpec::from_json(json)
            .unwrap()
            .normalize()
            .unwrap();
        assert_eq!(prog.unresolved_locations().count(), 2);

        let missing = resolve_program(&mut prog, &resolver);
        assert!(
            missing.is_empty(),
            "all locations should resolve: {:?}",
            missing
        );
        assert_eq!(prog.unresolved_locations().count(), 0);

        for anchor in prog.anchors.values() {
            match &anchor.position {
                Position::Rip { addr: Some(a), .. } => assert_ne!(*a, 0),
                other => panic!("expected resolved RIP, got {:?}", other),
            }
        }

        let _ = std::fs::remove_dir_all(bin.parent().unwrap());
    }

    #[test]
    fn missing_function_reported() {
        let Some(bin) = compile_fixture() else {
            eprintln!("skipping: no C compiler available");
            return;
        };
        let resolver = DebugInfoResolver::open(&bin).unwrap();
        let json = r#"{
          "version": 1,
          "events": { "A": {"thread": "1", "func": "no_such_function"} },
          "edges": []
        }"#;
        let mut prog = HappensBeforeSpec::from_json(json)
            .unwrap()
            .normalize()
            .unwrap();
        let missing = resolve_program(&mut prog, &resolver);
        assert_eq!(missing, vec!["A".to_string()]);

        let _ = std::fs::remove_dir_all(bin.parent().unwrap());
    }
}