testtrim 0.14.8

Intelligently select automated tests to run via code coverage analysis
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
465
466
467
468
469
470
471
472
473
474
475
476
// SPDX-FileCopyrightText: 2024 Mathieu Fenniak <mathieu@fenniak.net>
//
// SPDX-License-Identifier: GPL-3.0-or-later

// Can't attach this specifically to the `TwolineSyscall` struct because the relevant field is part of the macro output.
#![allow(clippy::ref_option)]

use std::collections::HashMap;

use anyhow::Result;
use ouroboros::self_referencing;

use super::tokenizer::{
    Argument, CallOutcome, ProcessExit, Retval, SyscallSegment, TokenizerOutput, tokenize,
};

#[self_referencing]
#[derive(Debug, PartialEq)]
pub struct TraceLine {
    pub input: String,
    #[borrows(input)]
    #[covariant]
    pub output: TokenizerOutput<'this>,
}

#[self_referencing]
#[derive(Debug, PartialEq)]
pub struct TraceLineProcessExit {
    trace_line: TraceLine,
    #[borrows(trace_line)]
    #[covariant]
    process_exit: &'this ProcessExit<'this>,
}

impl TraceLineProcessExit {
    fn make(trace_line: TraceLine) -> Self {
        TraceLineProcessExit::new(trace_line, |trace_line| match trace_line.borrow_output() {
            TokenizerOutput::Exit(process_exit) => process_exit,
            _ => panic!(
                "TraceLineProcessExit must be constructed with a TokenizerOutput::Exit, but was: {trace_line:?}"
            ),
        })
    }

    pub fn pid(&self) -> &str {
        self.borrow_process_exit().pid
    }
}

pub trait DebugOriginalTraceOutput {
    fn original_trace_output(&self) -> String;
}

pub trait CompleteSyscall: DebugOriginalTraceOutput {
    fn pid(&self) -> &str;
    fn function(&self) -> &str;
    fn arguments(&self) -> &Vec<&Argument<'_>>;
    fn retval(&self) -> &Retval<'_>;
}

#[self_referencing]
#[derive(Debug, PartialEq)]
pub struct OnelineSyscall {
    pub complete: TraceLine,
    #[borrows(complete)]
    #[covariant]
    arguments: Vec<&'this Argument<'this>>,
}

impl OnelineSyscall {
    fn make(complete: TraceLine) -> Self {
        OnelineSyscall::new(complete, |complete| match complete.borrow_output() {
            TokenizerOutput::Syscall(SyscallSegment {
                arguments,
                outcome: CallOutcome::Complete { .. },
                ..
            }) => {
                let mut ret = Vec::with_capacity(arguments.len());
                for a in arguments {
                    ret.push(a);
                }
                ret
            }
            _ => panic!(
                "OnelineSyscall must be constructored with a Complete SyscallSegment, but was: {complete:?}"
            ),
        })
    }
}

impl DebugOriginalTraceOutput for OnelineSyscall {
    fn original_trace_output(&self) -> String {
        self.borrow_complete().borrow_input().clone()
    }
}

impl CompleteSyscall for OnelineSyscall {
    fn pid(&self) -> &str {
        self.borrow_complete().with_output(|tokenizer_output| match tokenizer_output {
            TokenizerOutput::Syscall(SyscallSegment {
                pid,
                outcome: CallOutcome::Complete { .. },
                ..
            }) => pid,
            _ => panic!("OnelineSyscall should never be constructed with tokenizer output that isn't a complete syscall; but was: {tokenizer_output:?}"),
        })
    }

    fn function(&self) -> &str {
        self.borrow_complete().with_output(|tokenizer_output| match tokenizer_output {
            TokenizerOutput::Syscall(SyscallSegment {
                function,
                outcome: CallOutcome::Complete { .. },
                ..
            }) => function,
            _ => panic!("OnelineSyscall should never be constructed with tokenizer output that isn't a complete syscall; but was: {tokenizer_output:?}"),
        })
    }

    fn arguments(&self) -> &Vec<&Argument<'_>> {
        self.borrow_arguments()
    }

    fn retval(&self) -> &Retval<'_> {
        self.borrow_complete().with_output(|tokenizer_output| match tokenizer_output {
            TokenizerOutput::Syscall(SyscallSegment {
                outcome: CallOutcome::Complete { retval },
                ..
            }) => retval,
            _ => panic!("OnelineSyscall should never be constructed with tokenizer output that isn't a complete syscall; but was: {tokenizer_output:?}"),
        })
    }
}

#[self_referencing]
#[derive(Debug, PartialEq)]
pub struct TwolineSyscall {
    unfinished: TraceLine,
    resumed: TraceLine,
    #[borrows(unfinished, resumed)]
    #[covariant]
    written_structure: Option<Argument<'this>>,
    #[borrows(unfinished, resumed, written_structure)]
    #[covariant]
    arguments: Vec<&'this Argument<'this>>,
}

impl TwolineSyscall {
    fn make(unfinished: TraceLine, resumed: TraceLine) -> Self {
        TwolineSyscall::new(
            unfinished,
            resumed,
            // written_structure is populated as Some(x) when the first argument in the "resumed" syscall is a write
            // argument (eg. "=> {..}"). In this case we create our own owned Argument, referencing the two original
            // structures, which we'll put into `arguments` later in place of the two partial arguments.
            |unfinished, resumed| match (unfinished.borrow_output(), resumed.borrow_output()) {
                (
                    TokenizerOutput::Syscall(SyscallSegment {
                        arguments: first_arguments,
                        outcome: CallOutcome::Unfinished,
                        ..
                    }),
                    TokenizerOutput::Syscall(SyscallSegment {
                        arguments: second_arguments,
                        outcome: CallOutcome::Resumed { .. },
                        ..
                    }),
                ) => {
                    if first_arguments.is_empty() || second_arguments.is_empty() {
                        return None;
                    }
                    match (first_arguments.last(), second_arguments.first()) {
                        (Some(original), Some(Argument::WrittenArgumentResumed(update))) => {
                            Some(Argument::WrittenArgumentReference(original, update))
                        }
                        _ => None,
                    }
                }
                (_, _) => panic!(
                    "TwolineSyscall must be constructored with an Unfinished and Resumed SyscallSegment"
                ),
            },
            |unfinished, resumed, written_structure| {
                match (unfinished.borrow_output(), resumed.borrow_output()) {
                    (
                        TokenizerOutput::Syscall(SyscallSegment {
                            arguments: first_arguments,
                            outcome: CallOutcome::Unfinished,
                            ..
                        }),
                        TokenizerOutput::Syscall(SyscallSegment {
                            arguments: second_arguments,
                            outcome: CallOutcome::Resumed { .. },
                            ..
                        }),
                    ) => {
                        let mut ret =
                            Vec::with_capacity(first_arguments.len() + second_arguments.len());
                        for a in first_arguments {
                            ret.push(a);
                        }
                        for a in second_arguments {
                            ret.push(a);
                        }
                        // Slip written_structure in the middle of the arguments if present.
                        if let Some(written_structure) = written_structure {
                            ret[first_arguments.len() - 1] = written_structure;
                            ret.remove(first_arguments.len());
                        }
                        ret
                    }
                    (_, _) => panic!(
                        "TwolineSyscall must be constructored with an Unfinished and Resumed SyscallSegment"
                    ),
                }
            },
        )
    }
}

impl DebugOriginalTraceOutput for TwolineSyscall {
    fn original_trace_output(&self) -> String {
        format!(
            "line1: {:?}, line2: {:?}",
            self.borrow_unfinished().borrow_input().clone(),
            self.borrow_resumed().borrow_input().clone(),
        )
    }
}

impl CompleteSyscall for TwolineSyscall {
    fn pid(&self) -> &str {
        self.borrow_unfinished().with_output(|tokenizer_output| match tokenizer_output {
            TokenizerOutput::Syscall(SyscallSegment {
                pid,
                outcome: CallOutcome::Unfinished,
                ..
            }) => pid,
            _ => panic!("TwolineSyscall cannot be constructed with 0 tokenizer output that isn't a syscall; but was: {tokenizer_output:?}"),
        })
    }

    fn function(&self) -> &str {
        self.borrow_unfinished().with_output(|tokenizer_output| match tokenizer_output {
            TokenizerOutput::Syscall(SyscallSegment {
                function,
                outcome: CallOutcome::Unfinished,
                ..
            }) => function,
            _ => panic!("TwolineSyscall cannot be constructed with 0 tokenizer output that isn't a syscall; but was: {tokenizer_output:?}"),
        })
    }

    fn arguments(&self) -> &Vec<&Argument<'_>> {
        self.borrow_arguments()
    }

    fn retval(&self) -> &Retval<'_> {
        self.borrow_resumed().with_output(|tokenizer_output| match tokenizer_output {
            TokenizerOutput::Syscall(SyscallSegment {
                outcome: CallOutcome::Resumed { retval },
                ..
            }) => retval,
            _ => panic!("TwolineSyscall cannot be constructed with 1 tokenizer output that isn't a syscall; but was: {tokenizer_output:?}"),
        })
    }
}

#[derive(Debug, PartialEq)]
pub enum SequencerOutput {
    /// An strace line that included both invocation and completion of the syscall.
    OnelineSyscall(OnelineSyscall),
    /// A pair of strace lines representing the invocation, and then completion, of the syscall.
    TwolineSyscall(TwolineSyscall),
    /// An strace line that started a syscall that hasn't been completed.
    IncompleteSyscall,
    ProcessExit(TraceLineProcessExit),
    Junk(TraceLine),
}

impl SequencerOutput {
    pub fn trace_lines(&self) -> (Option<&TraceLine>, Option<&TraceLine>) {
        match self {
            SequencerOutput::OnelineSyscall(one) => (Some(one.borrow_complete()), None),
            SequencerOutput::TwolineSyscall(two) => {
                (Some(two.borrow_unfinished()), Some(two.borrow_resumed()))
            }
            SequencerOutput::IncompleteSyscall => (None, None),
            SequencerOutput::ProcessExit(exit) => (Some(exit.borrow_trace_line()), None),
            SequencerOutput::Junk(trace_line) => (Some(trace_line), None),
        }
    }
}

/// Sequencer tokenizes lines in an strace output file and merges together the arguments of "unfinished" and "resumed"
/// syscalls, providing an output which, through the `CompleteSyscall` trait, can be used the same whether the syscall
/// was interrupted during tracing or not.
pub struct Sequencer {
    unfinished: HashMap<String, TraceLine>,
}

impl Sequencer {
    #[must_use]
    pub fn new() -> Self {
        Self {
            unfinished: HashMap::new(),
        }
    }

    pub fn tokenize(&mut self, input: String) -> Result<SequencerOutput> {
        let trace_line = TraceLine::try_new(input, |input| {
            let mut tokenizer_input: &str = input;
            tokenize(&mut tokenizer_input)
        })?;

        match trace_line.borrow_output() {
            TokenizerOutput::Syscall(SyscallSegment {
                outcome: CallOutcome::Complete { .. },
                ..
            }) => Ok(SequencerOutput::OnelineSyscall(OnelineSyscall::make(
                trace_line,
            ))),
            TokenizerOutput::Syscall(SyscallSegment {
                pid,
                outcome: CallOutcome::Unfinished,
                ..
            }) => {
                self.unfinished.insert((*pid).to_string(), trace_line);
                Ok(SequencerOutput::IncompleteSyscall)
            }
            TokenizerOutput::Syscall(SyscallSegment {
                pid,
                outcome: CallOutcome::Resumed { .. },
                ..
            }) => {
                if let Some(unfinished) = self.unfinished.remove(*pid) {
                    Ok(SequencerOutput::TwolineSyscall(TwolineSyscall::make(
                        unfinished, trace_line,
                    )))
                } else {
                    Err(anyhow::anyhow!(
                        "Found resumed syscall without matching unfinished call; content was: {:?}",
                        trace_line.borrow_input()
                    ))
                }
            }
            TokenizerOutput::Exit(_) => Ok(SequencerOutput::ProcessExit(
                TraceLineProcessExit::make(trace_line),
            )),
            TokenizerOutput::Signal(_)
            | TokenizerOutput::Syscall(SyscallSegment {
                outcome: CallOutcome::ResumedUnfinished,
                ..
            }) => Ok(SequencerOutput::Junk(trace_line)),
        }
    }
}

#[cfg(test)]
mod tests {
    use anyhow::Result;

    use crate::sys_trace::strace::{
        sequencer::CompleteSyscall as _,
        tokenizer::{Argument, ArgumentStructure, Retval},
    };

    use super::{Sequencer, SequencerOutput};

    #[test]
    fn complete() -> Result<()> {
        let mut seq = Sequencer::new();

        let t = seq.tokenize(String::from(r"2177902 close(3)                        = 0"))?;
        let SequencerOutput::OnelineSyscall(t) = t else {
            panic!("expected OnelineSyscall; was {t:?}");
        };
        assert_eq!(t.pid(), "2177902");
        assert_eq!(t.function(), "close");
        assert_eq!(*t.arguments(), vec![&Argument::Numeric("3")]);
        assert_eq!(*t.retval(), Retval::Success(0));

        Ok(())
    }

    #[test]
    fn sequence() -> Result<()> {
        let mut seq = Sequencer::new();

        // 337651 clone(child_stack=NULL, flags=CLONE_CHILD_CLEARTID|CLONE_CHILD_SETTID|SIGCHLD <unfinished ...>
        // 337653 openat(AT_FDCWD, "/dev/null", O_RDONLY|O_CLOEXEC) = 9
        // 337651 <... clone resumed>, child_tidptr=0x7f9f93f88a10) = 337654

        let t = seq.tokenize(String::from(r"337651 clone(child_stack=NULL, flags=CLONE_CHILD_CLEARTID|CLONE_CHILD_SETTID|SIGCHLD <unfinished ...>"))?;
        assert_eq!(t, SequencerOutput::IncompleteSyscall);

        let t = seq.tokenize(String::from(
            r"337651 <... clone resumed>, child_tidptr=0x7f9f93f88a10) = 337654",
        ))?;
        let SequencerOutput::TwolineSyscall(t) = t else {
            panic!("expected TwolineSyscall; was {t:?}");
        };
        assert_eq!(t.pid(), "337651");
        assert_eq!(t.function(), "clone");
        assert_eq!(
            *t.arguments(),
            vec![
                &Argument::Named("child_stack", Box::new(Argument::Null)),
                &Argument::Named(
                    "flags",
                    Box::new(Argument::Enum(
                        "CLONE_CHILD_CLEARTID|CLONE_CHILD_SETTID|SIGCHLD"
                    ))
                ),
                &Argument::Named(
                    "child_tidptr",
                    Box::new(Argument::Pointer("0x7f9f93f88a10"))
                ),
            ]
        );
        assert_eq!(*t.retval(), Retval::Success(337_654));

        Ok(())
    }

    #[test]
    fn sequence_structure_write_merge() -> Result<()> {
        let mut seq = Sequencer::new();

        let t = seq.tokenize(String::from(r"337652 clone3({flags=CLONE_VM|CLONE_FS|CLONE_FILES|CLONE_SIGHAND|CLONE_THREAD|CLONE_SYSVSEM|CLONE_SETTLS|CLONE_PARENT_SETTID|CLONE_CHILD_CLEARTID, child_tid=0x7f67099ff990, parent_tid=0x7f67099ff990, exit_signal=0, stack=0x7f67091ff000, stack_size=0x7fff80, tls=0x7f67099ff6c0} <unfinished ...>"))?;
        assert_eq!(t, SequencerOutput::IncompleteSyscall);

        let t = seq.tokenize(String::from(
            r"337652 <... clone3 resumed> => {parent_tid=[0]}, 88) = 15620",
        ))?;
        let SequencerOutput::TwolineSyscall(t) = t else {
            panic!("expected TwolineSyscall; was {t:?}");
        };
        assert_eq!(t.pid(), "337652");
        assert_eq!(t.function(), "clone3");
        assert_eq!(
            *t.arguments(),
            vec![
                &Argument::WrittenArgumentReference(
                    &Argument::Structure(ArgumentStructure::new(vec![
                        Argument::Named(
                            "flags",
                            Box::new(Argument::Enum(
                                "CLONE_VM|CLONE_FS|CLONE_FILES|CLONE_SIGHAND|CLONE_THREAD|CLONE_SYSVSEM|CLONE_SETTLS|CLONE_PARENT_SETTID|CLONE_CHILD_CLEARTID"
                            ))
                        ),
                        Argument::Named("child_tid", Box::new(Argument::Pointer("0x7f67099ff990"))),
                        Argument::Named(
                            "parent_tid",
                            Box::new(Argument::Pointer("0x7f67099ff990"))
                        ),
                        Argument::Named("exit_signal", Box::new(Argument::Numeric("0"))),
                        Argument::Named("stack", Box::new(Argument::Pointer("0x7f67091ff000"))),
                        Argument::Named("stack_size", Box::new(Argument::Pointer("0x7fff80"))),
                        Argument::Named("tls", Box::new(Argument::Pointer("0x7f67099ff6c0"))),
                    ])),
                    &Argument::Structure(ArgumentStructure::new(vec![Argument::Named(
                        "parent_tid",
                        Box::new(Argument::Structure(ArgumentStructure::new(vec![
                            Argument::Numeric("0")
                        ])))
                    )])),
                ),
                &Argument::Numeric("88"),
            ]
        );
        assert_eq!(*t.retval(), Retval::Success(15_620));

        Ok(())
    }
}