• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

xd009642 / tarpaulin / #316

pending completion
#316

push

xd009642
Update date

3013 of 3812 relevant lines covered (79.04%)

181974.77 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

38.64
/src/process_handling/linux.rs
1
use crate::config::types::Mode;
2
use crate::errors::*;
3
use crate::process_handling::execute_test;
4
use crate::ptrace_control::*;
5
use crate::Config;
6
use crate::TestBinary;
7
use crate::TestHandle;
8
use lazy_static::lazy_static;
9
use nix::sched::*;
10
use nix::sys::personality;
11
use nix::unistd::*;
12
use std::ffi::{CStr, CString};
13
use std::path::Path;
14
use tracing::{info, warn};
15

16
lazy_static! {
17
    static ref NUM_CPUS: usize = num_cpus::get();
18
}
19

20
/// Returns the coverage statistics for a test executable in the given workspace
21
pub fn get_test_coverage(
168✔
22
    test: &TestBinary,
23
    config: &Config,
24
    ignored: bool,
25
) -> Result<Option<TestHandle>, RunError> {
26
    if !test.path().exists() {
168✔
27
        warn!("Test at {} doesn't exist", test.path().display());
×
28
        return Ok(None);
×
29
    }
30

31
    // Solves CI issue when fixing #953 and #966 in PR #962
32
    let threads = if config.follow_exec { 1 } else { *NUM_CPUS };
504✔
33

34
    if let Err(e) = limit_affinity() {
168✔
35
        warn!("Failed to set processor affinity {}", e);
×
36
    }
37

38
    unsafe {
39
        match fork() {
168✔
40
            Ok(ForkResult::Parent { child }) => Ok(Some(TestHandle::Id(child))),
168✔
41
            Ok(ForkResult::Child) => {
42
                let bin_type = match config.command {
×
43
                    Mode::Test => "test",
×
44
                    Mode::Build => "binary",
×
45
                };
46
                info!("Launching {}", bin_type);
×
47
                execute_test(test, &[], ignored, config, Some(threads))?;
×
48
                Ok(None)
×
49
            }
50
            Err(err) => Err(RunError::TestCoverage(format!(
×
51
                "Failed to run test {}, Error: {}",
×
52
                test.path().display(),
×
53
                err
×
54
            ))),
55
        }
56
    }
57
}
58

59
fn disable_aslr() -> nix::Result<()> {
×
60
    let this = personality::get()?;
×
61
    personality::set(this | personality::Persona::ADDR_NO_RANDOMIZE).map(|_| ())
×
62
}
63

64
pub fn limit_affinity() -> nix::Result<()> {
168✔
65
    let this = Pid::this();
168✔
66
    // Get current affinity to be able to limit the cores to one of
67
    // those already in the affinity mask.
68
    let affinity = sched_getaffinity(this)?;
336✔
69
    let mut selected_cpu = 0;
168✔
70
    for i in 0..CpuSet::count() {
336✔
71
        if affinity.is_set(i)? {
168✔
72
            selected_cpu = i;
168✔
73
            break;
168✔
74
        }
75
    }
76
    let mut cpu_set = CpuSet::new();
168✔
77
    cpu_set.set(selected_cpu)?;
168✔
78
    sched_setaffinity(this, &cpu_set)
168✔
79
}
80

81
pub fn execute(
×
82
    test: &Path,
83
    argv: &[String],
84
    envar: &[(String, String)],
85
) -> Result<TestHandle, RunError> {
86
    let program = CString::new(test.display().to_string()).unwrap_or_default();
×
87
    disable_aslr().map_err(|e| RunError::TestRuntime(format!("ASLR disable failed: {e}")))?;
×
88

89
    request_trace().map_err(|e| RunError::Trace(e.to_string()))?;
×
90

91
    let envar = envar
×
92
        .iter()
93
        .map(|(k, v)| CString::new(format!("{k}={v}").as_str()).unwrap_or_default())
×
94
        .collect::<Vec<CString>>();
95

96
    let argv = argv
×
97
        .iter()
98
        .map(|x| CString::new(x.as_str()).unwrap_or_default())
×
99
        .collect::<Vec<CString>>();
100

101
    let arg_ref = argv.iter().map(AsRef::as_ref).collect::<Vec<&CStr>>();
×
102
    let env_ref = envar.iter().map(AsRef::as_ref).collect::<Vec<&CStr>>();
×
103
    execve(&program, &arg_ref, &env_ref).map_err(|_| RunError::Internal)?;
×
104

105
    unreachable!();
106
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc