axbuild 0.4.19

An OS build lib toolkit used by arceos
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
use std::{
    collections::hash_map::DefaultHasher,
    fs,
    hash::{Hash, Hasher},
    path::{Path, PathBuf},
};

use ostool::{build::config::Cargo, run::qemu::QemuConfig};

pub(crate) const AXTEST_COVERAGE_RUSTFLAGS: &[&str] = &[
    "--cfg",
    "axtest_coverage",
    "--check-cfg",
    "cfg(axtest_coverage)",
    "-Cinstrument-coverage",
    "-Zno-profiler-runtime",
];

const COVERAGE_FEATURE: &str = "axtest/coverage";
const MARKER_PREFIX: &str = "AXTEST_COVERAGE status=ready";
const SUITE_OK_MARKER: &str = "AXTEST_SUITE_OK";
pub(crate) const COVERAGE_DONE_MARKER: &str = "AXTEST_COVERAGE_DONE";

pub(crate) fn enabled(cargo: &Cargo) -> bool {
    crate::build::env_truthy(&cargo.env, "AXTEST_COVERAGE")
}

pub(crate) fn prepare_cargo(cargo: &mut Cargo) {
    if !cargo
        .features
        .iter()
        .any(|feature| feature == COVERAGE_FEATURE)
    {
        cargo.features.push(COVERAGE_FEATURE.to_string());
    }
    crate::build::append_encoded_rustflags(cargo, AXTEST_COVERAGE_RUSTFLAGS);
}

#[derive(Debug, Clone)]
pub(crate) struct AxtestCoveragePaths {
    pub(crate) monitor_socket: PathBuf,
    pub(crate) profraw_path: PathBuf,
}

impl AxtestCoveragePaths {
    pub(crate) fn new(workspace_root: &Path, package: &str, target: &str) -> anyhow::Result<Self> {
        let arch_triple = Path::new(target)
            .file_stem()
            .map(|s| s.to_string_lossy().into_owned())
            .unwrap_or_else(|| sanitize_path_component(target));
        let profraw_filename = format!("{package}-{arch_triple}.profraw");
        let dir = workspace_root.join("coverage");
        fs::create_dir_all(&dir)?;
        let profraw_path = dir.join(profraw_filename);
        let mut hasher = DefaultHasher::new();
        profraw_path.hash(&mut hasher);
        let socket_name = format!("axcov-{}-{:016x}.sock", std::process::id(), hasher.finish());
        Ok(Self {
            monitor_socket: std::env::temp_dir().join(socket_name),
            profraw_path,
        })
    }
}

fn sanitize_path_component(value: &str) -> String {
    value
        .chars()
        .map(|ch| {
            if ch.is_ascii_alphanumeric() || matches!(ch, '-' | '_' | '.') {
                ch
            } else {
                '_'
            }
        })
        .collect()
}

pub(crate) fn apply_qemu_monitor(qemu: &mut QemuConfig, paths: &AxtestCoveragePaths) {
    let _ = fs::remove_file(&paths.monitor_socket);
    let monitor = format!("unix:{},server,nowait", paths.monitor_socket.display());
    qemu.args.extend([
        "-monitor".to_string(),
        monitor,
        "-D".to_string(),
        paths
            .profraw_path
            .with_file_name("qemu.log")
            .display()
            .to_string(),
    ]);
}

/// Replace the QEMU success regex so that ostool waits for coverage extraction
/// to complete (signaled by `AXTEST_COVERAGE_DONE`) instead of matching
/// `AXTEST_SUITE_OK` prematurely.
pub(crate) fn update_success_regex(qemu: &mut QemuConfig) {
    for regex in &mut qemu.success_regex {
        if regex.contains(SUITE_OK_MARKER) {
            *regex = regex.replace(SUITE_OK_MARKER, COVERAGE_DONE_MARKER);
        }
    }
    // If no success regex contained the marker, add one for coverage done.
    if !qemu
        .success_regex
        .iter()
        .any(|r| r.contains(COVERAGE_DONE_MARKER))
    {
        qemu.success_regex.push(COVERAGE_DONE_MARKER.to_string());
    }
}

#[cfg(unix)]
mod capture {
    use std::{
        fs,
        io::{self, Read, Write},
        os::{fd::FromRawFd, unix::net::UnixStream},
        path::{Path, PathBuf},
        sync::{Arc, Mutex},
        thread::JoinHandle,
        time::{Duration, Instant},
    };

    use anyhow::{Context, bail};
    use regex::Regex;

    use super::{AxtestCoveragePaths, COVERAGE_DONE_MARKER, MARKER_PREFIX, SUITE_OK_MARKER};

    pub(crate) struct AxtestCoverageCaptureGuard {
        saved_stdout: i32,
        saved_stderr: i32,
        reader: Option<JoinHandle<io::Result<()>>>,
        state: Arc<Mutex<AxtestCoverageState>>,
    }

    #[derive(Debug)]
    struct AxtestCoverageState {
        monitor_socket: PathBuf,
        profraw_path: PathBuf,
        line_buf: String,
        dumped: bool,
        completion_signaled: bool,
        error: Option<String>,
        monitor_conn: Option<UnixStream>,
    }

    impl AxtestCoverageCaptureGuard {
        pub(crate) fn install(paths: &AxtestCoveragePaths) -> io::Result<Self> {
            let saved_stdout = unsafe { libc::dup(libc::STDOUT_FILENO) };
            let saved_stderr = unsafe { libc::dup(libc::STDERR_FILENO) };
            if saved_stdout < 0 || saved_stderr < 0 {
                return Err(io::Error::last_os_error());
            }

            let tee_stdout = unsafe { libc::dup(saved_stdout) };
            if tee_stdout < 0 {
                return Err(io::Error::last_os_error());
            }

            let mut fds = [0i32; 2];
            if unsafe { libc::pipe(fds.as_mut_ptr()) } != 0 {
                return Err(io::Error::last_os_error());
            }
            let read_fd = fds[0];
            let write_fd = fds[1];
            if unsafe { libc::dup2(write_fd, libc::STDOUT_FILENO) } < 0
                || unsafe { libc::dup2(write_fd, libc::STDERR_FILENO) } < 0
            {
                return Err(io::Error::last_os_error());
            }
            unsafe { libc::close(write_fd) };

            let state = Arc::new(Mutex::new(AxtestCoverageState {
                monitor_socket: paths.monitor_socket.clone(),
                profraw_path: paths.profraw_path.clone(),
                line_buf: String::new(),
                dumped: false,
                completion_signaled: false,
                error: None,
                monitor_conn: None,
            }));

            // Pre-connect to the QEMU monitor socket in a background thread.
            // This avoids a race condition where QEMU exits (after matching the
            // success pattern) before the reader thread can connect to the socket.
            let connector_state = state.clone();
            let socket_path = paths.monitor_socket.clone();
            std::thread::spawn(move || {
                if let Ok(conn) = wait_and_connect_monitor(&socket_path)
                    && let Ok(mut state) = connector_state.lock()
                {
                    state.monitor_conn = Some(conn);
                }
            });

            let reader_state = state.clone();
            let reader = std::thread::spawn(move || {
                let mut pipe = unsafe { fs::File::from_raw_fd(read_fd) };
                let mut terminal = unsafe { fs::File::from_raw_fd(tee_stdout) };
                let mut tee_buf = String::new();
                let mut buf = [0u8; 8192];
                loop {
                    match pipe.read(&mut buf) {
                        Ok(0) => break,
                        Ok(n) => {
                            let chunk = String::from_utf8_lossy(&buf[..n]);
                            tee_buf.push_str(&chunk);
                            // Flush complete lines to terminal, filtering out
                            // AXTEST_SUITE_OK so ostool doesn't kill QEMU before
                            // coverage extraction finishes.
                            while let Some(newline) = tee_buf.find('\n') {
                                let line = &tee_buf[..=newline];
                                if !line.contains(SUITE_OK_MARKER) {
                                    terminal.write_all(line.as_bytes())?;
                                }
                                tee_buf.drain(..=newline);
                            }
                            if let Ok(mut state) = reader_state.lock() {
                                state.push_bytes(&buf[..n]);
                                // If coverage was just extracted, signal completion
                                // to ostool so it can stop waiting.
                                if state.dumped && !state.completion_signaled {
                                    state.completion_signaled = true;
                                    let marker = format!("{COVERAGE_DONE_MARKER}\n");
                                    terminal.write_all(marker.as_bytes())?;
                                }
                            }
                        }
                        Err(err) if err.kind() == io::ErrorKind::Interrupted => {}
                        Err(err) => return Err(err),
                    }
                }
                // Flush any remaining partial line
                if !tee_buf.is_empty() && !tee_buf.contains(SUITE_OK_MARKER) {
                    terminal.write_all(tee_buf.as_bytes())?;
                }
                terminal.flush()
            });

            Ok(Self {
                saved_stdout,
                saved_stderr,
                reader: Some(reader),
                state,
            })
        }

        pub(crate) fn finish(mut self) -> anyhow::Result<()> {
            self.restore();
            if let Some(reader) = self.reader.take() {
                reader.join().map_err(|payload| {
                    let msg = payload
                        .downcast_ref::<&'static str>()
                        .map(|s| s.to_string())
                        .or_else(|| payload.downcast_ref::<String>().cloned())
                        .unwrap_or_else(|| "<non-string panic payload>".to_string());
                    anyhow::anyhow!("axtest coverage capture thread panicked: {msg}")
                })??;
            }
            let state = self.state.lock().unwrap();
            if let Some(error) = &state.error {
                bail!("{error}");
            }
            if state.dumped {
                println!("  coverage: {}", state.profraw_path.display());
            } else {
                bail!(
                    "axtest coverage was enabled but no coverage profile was captured at {}",
                    state.profraw_path.display()
                );
            }
            Ok(())
        }

        fn restore(&self) {
            let _ = io::stdout().flush();
            let _ = io::stderr().flush();
            unsafe {
                libc::dup2(self.saved_stdout, libc::STDOUT_FILENO);
                libc::dup2(self.saved_stderr, libc::STDERR_FILENO);
            }
        }
    }

    impl Drop for AxtestCoverageCaptureGuard {
        fn drop(&mut self) {
            self.restore();
            unsafe {
                libc::close(self.saved_stdout);
                libc::close(self.saved_stderr);
            }
            if let Some(reader) = self.reader.take() {
                let _ = reader.join();
            }
        }
    }

    impl AxtestCoverageState {
        fn push_bytes(&mut self, bytes: &[u8]) {
            self.line_buf.push_str(&String::from_utf8_lossy(bytes));
            while let Some(newline) = self.line_buf.find('\n') {
                let line = self.line_buf[..newline].trim_end_matches('\r').to_string();
                self.line_buf.drain(..=newline);
                self.process_line(&line);
            }
        }

        fn process_line(&mut self, line: &str) {
            if self.dumped || !line.starts_with(MARKER_PREFIX) {
                return;
            }
            match parse_coverage_marker(line).and_then(|(addr, size)| {
                self.dump_coverage(addr, size)
                    .map_err(|err| err.to_string())
            }) {
                Ok(()) => self.dumped = true,
                Err(err) => self.error = Some(err),
            }
        }

        fn dump_coverage(&mut self, addr: u64, size: usize) -> anyhow::Result<()> {
            let mut stream = self
                .monitor_conn
                .take()
                .or_else(|| {
                    // Fallback: connect on demand if pre-connection wasn't ready.
                    wait_and_connect_monitor(&self.monitor_socket).ok()
                })
                .with_context(|| {
                    format!(
                        "QEMU monitor socket was not available at {}",
                        self.monitor_socket.display()
                    )
                })?;
            let command = format!(
                "memsave 0x{addr:x} {size} \"{}\"\n",
                self.profraw_path.display()
            );
            stream
                .write_all(command.as_bytes())
                .context("failed to send QEMU memsave command")?;
            stream.flush().ok();
            Ok(())
        }
    }

    fn wait_and_connect_monitor(socket: &Path) -> anyhow::Result<UnixStream> {
        let deadline = Instant::now() + Duration::from_secs(10);
        while Instant::now() < deadline {
            if socket.exists() {
                return UnixStream::connect(socket).with_context(|| {
                    format!("failed to connect QEMU monitor at {}", socket.display())
                });
            }
            std::thread::sleep(Duration::from_millis(20));
        }
        bail!(
            "QEMU monitor socket was not created at {}",
            socket.display()
        )
    }

    fn parse_coverage_marker(line: &str) -> Result<(u64, usize), String> {
        let regex = Regex::new(r"\baddr=0x([0-9a-fA-F]+)\s+size=([0-9]+)\b").unwrap();
        let caps = regex
            .captures(line)
            .ok_or_else(|| format!("invalid axtest coverage marker: {line}"))?;
        let addr = u64::from_str_radix(&caps[1], 16)
            .map_err(|err| format!("invalid coverage address in `{line}`: {err}"))?;
        let size = caps[2]
            .parse::<usize>()
            .map_err(|err| format!("invalid coverage size in `{line}`: {err}"))?;
        if size == 0 {
            return Err("coverage profile size is zero".to_string());
        }
        Ok((addr, size))
    }

    #[cfg(test)]
    mod tests {
        use super::*;

        #[test]
        fn parse_marker_extracts_address_and_size() {
            assert_eq!(
                parse_coverage_marker("AXTEST_COVERAGE status=ready addr=0x1234abcd size=4096"),
                Ok((0x1234abcd, 4096))
            );
        }
    }
}

#[cfg(not(unix))]
mod capture {
    use super::AxtestCoveragePaths;

    pub(crate) struct AxtestCoverageCaptureGuard;

    impl AxtestCoverageCaptureGuard {
        pub(crate) fn install(_paths: &AxtestCoveragePaths) -> std::io::Result<Self> {
            Err(std::io::Error::new(
                std::io::ErrorKind::Unsupported,
                "axtest coverage capture requires Unix QEMU monitor sockets",
            ))
        }

        pub(crate) fn finish(self) -> anyhow::Result<()> {
            Ok(())
        }
    }
}

pub(crate) use capture::AxtestCoverageCaptureGuard;