4
0

run.rs 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545
  1. use std::{
  2. env::consts::{ARCH, OS},
  3. ffi::OsString,
  4. fmt::Write as _,
  5. fs::{copy, create_dir_all, metadata, File},
  6. io::{BufRead as _, BufReader, ErrorKind, Write as _},
  7. path::{Path, PathBuf},
  8. process::{Child, ChildStdin, Command, Output, Stdio},
  9. sync::{Arc, Mutex},
  10. thread,
  11. };
  12. use anyhow::{anyhow, bail, Context as _, Result};
  13. use cargo_metadata::{Artifact, ArtifactProfile, CompilerMessage, Message, Target};
  14. use clap::Parser;
  15. use xtask::{exec, Errors, AYA_BUILD_INTEGRATION_BPF};
  16. #[derive(Parser)]
  17. enum Environment {
  18. /// Runs the integration tests locally.
  19. Local {
  20. /// The command used to wrap your application.
  21. #[clap(short, long, default_value = "sudo -E")]
  22. runner: String,
  23. },
  24. /// Runs the integration tests in a VM.
  25. VM {
  26. /// The kernel images to use.
  27. ///
  28. /// You can download some images with:
  29. ///
  30. /// wget --accept-regex '.*/linux-image-[0-9\.-]+-cloud-.*-unsigned*' \
  31. /// --recursive ftp://ftp.us.debian.org/debian/pool/main/l/linux/
  32. ///
  33. /// You can then extract them with:
  34. ///
  35. /// find . -name '*.deb' -print0 \
  36. /// | xargs -0 -I {} sh -c "dpkg --fsys-tarfile {} \
  37. /// | tar --wildcards --extract '*vmlinuz*' --file -"
  38. #[clap(required = true)]
  39. kernel_image: Vec<PathBuf>,
  40. },
  41. }
  42. #[derive(Parser)]
  43. pub struct Options {
  44. #[clap(subcommand)]
  45. environment: Environment,
  46. /// Arguments to pass to your application.
  47. #[clap(global = true, last = true)]
  48. run_args: Vec<OsString>,
  49. }
  50. pub fn build<F>(target: Option<&str>, f: F) -> Result<Vec<(String, PathBuf)>>
  51. where
  52. F: FnOnce(&mut Command) -> &mut Command,
  53. {
  54. // Always use rust-lld and -Zbuild-std in case we're cross-compiling.
  55. let mut cmd = Command::new("cargo");
  56. cmd.args(["build", "--message-format=json"]);
  57. if let Some(target) = target {
  58. let config = format!("target.{target}.linker = \"rust-lld\"");
  59. cmd.args(["--target", target, "--config", &config]);
  60. }
  61. f(&mut cmd);
  62. let mut child = cmd
  63. .stdout(Stdio::piped())
  64. .spawn()
  65. .with_context(|| format!("failed to spawn {cmd:?}"))?;
  66. let Child { stdout, .. } = &mut child;
  67. let stdout = stdout.take().unwrap();
  68. let stdout = BufReader::new(stdout);
  69. let mut executables = Vec::new();
  70. for message in Message::parse_stream(stdout) {
  71. #[allow(clippy::collapsible_match)]
  72. match message.context("valid JSON")? {
  73. Message::CompilerArtifact(Artifact {
  74. executable,
  75. target: Target { name, .. },
  76. profile: ArtifactProfile { test, .. },
  77. ..
  78. }) => {
  79. if test {
  80. if let Some(executable) = executable {
  81. executables.push((name, executable.into()));
  82. }
  83. }
  84. }
  85. Message::CompilerMessage(CompilerMessage { message, .. }) => {
  86. println!("{message}");
  87. }
  88. Message::TextLine(line) => {
  89. println!("{line}");
  90. }
  91. _ => {}
  92. }
  93. }
  94. let status = child
  95. .wait()
  96. .with_context(|| format!("failed to wait for {cmd:?}"))?;
  97. if status.code() != Some(0) {
  98. bail!("{cmd:?} failed: {status:?}")
  99. }
  100. Ok(executables)
  101. }
  102. /// Build and run the project.
  103. pub fn run(opts: Options) -> Result<()> {
  104. let Options {
  105. environment,
  106. run_args,
  107. } = opts;
  108. type Binary = (String, PathBuf);
  109. fn binaries(target: Option<&str>) -> Result<Vec<(&str, Vec<Binary>)>> {
  110. ["dev", "release"]
  111. .into_iter()
  112. .map(|profile| {
  113. let binaries = build(target, |cmd| {
  114. cmd.env(AYA_BUILD_INTEGRATION_BPF, "true").args([
  115. "--package",
  116. "integration-test",
  117. "--tests",
  118. "--profile",
  119. profile,
  120. ])
  121. })?;
  122. anyhow::Ok((profile, binaries))
  123. })
  124. .collect()
  125. }
  126. // Use --test-threads=1 to prevent tests from interacting with shared
  127. // kernel state due to the lack of inter-test isolation.
  128. let default_args = [OsString::from("--test-threads=1")];
  129. let run_args = default_args.iter().chain(run_args.iter());
  130. match environment {
  131. Environment::Local { runner } => {
  132. let mut args = runner.trim().split_terminator(' ');
  133. let runner = args.next().ok_or(anyhow!("no first argument"))?;
  134. let args = args.collect::<Vec<_>>();
  135. let binaries = binaries(None)?;
  136. let mut failures = String::new();
  137. for (profile, binaries) in binaries {
  138. for (name, binary) in binaries {
  139. let mut cmd = Command::new(runner);
  140. let cmd = cmd.args(args.iter()).arg(binary).args(run_args.clone());
  141. println!("{profile}:{name} running {cmd:?}");
  142. let status = cmd
  143. .status()
  144. .with_context(|| format!("failed to run {cmd:?}"))?;
  145. if status.code() != Some(0) {
  146. writeln!(&mut failures, "{profile}:{name} failed: {status:?}")
  147. .context("String write failed")?
  148. }
  149. }
  150. }
  151. if failures.is_empty() {
  152. Ok(())
  153. } else {
  154. Err(anyhow!("failures:\n{}", failures))
  155. }
  156. }
  157. Environment::VM { kernel_image } => {
  158. // The user has asked us to run the tests on a VM. This is involved; strap in.
  159. //
  160. // We need tools to build the initramfs; we use gen_init_cpio from the Linux repository,
  161. // taking care to cache it.
  162. //
  163. // Then we iterate the kernel images, using the `file` program to guess the target
  164. // architecture. We then build the init program and our test binaries for that
  165. // architecture, and use gen_init_cpio to build an initramfs containing the test
  166. // binaries. We're almost ready to run the VM.
  167. //
  168. // We consult our OS, our architecture, and the target architecture to determine if
  169. // hardware acceleration is available, and then start QEMU with the provided kernel
  170. // image and the initramfs we built.
  171. //
  172. // We consume the output of QEMU, looking for the output of our init program. This is
  173. // the only way to distinguish success from failure. We batch up the errors across all
  174. // VM images and report to the user. The end.
  175. let cache_dir = Path::new("test/.tmp");
  176. create_dir_all(cache_dir).context("failed to create cache dir")?;
  177. let gen_init_cpio = cache_dir.join("gen_init_cpio");
  178. if !gen_init_cpio
  179. .try_exists()
  180. .context("failed to check existence of gen_init_cpio")?
  181. {
  182. let mut curl = Command::new("curl");
  183. curl.args([
  184. "-sfSL",
  185. "https://raw.githubusercontent.com/torvalds/linux/master/usr/gen_init_cpio.c",
  186. ]);
  187. let mut curl_child = curl
  188. .stdout(Stdio::piped())
  189. .spawn()
  190. .with_context(|| format!("failed to spawn {curl:?}"))?;
  191. let Child { stdout, .. } = &mut curl_child;
  192. let curl_stdout = stdout.take().unwrap();
  193. let mut clang = Command::new("clang");
  194. let clang = exec(
  195. clang
  196. .args(["-g", "-O2", "-x", "c", "-", "-o"])
  197. .arg(&gen_init_cpio)
  198. .stdin(curl_stdout),
  199. );
  200. let output = curl_child
  201. .wait_with_output()
  202. .with_context(|| format!("failed to wait for {curl:?}"))?;
  203. let Output { status, .. } = &output;
  204. if status.code() != Some(0) {
  205. bail!("{curl:?} failed: {output:?}")
  206. }
  207. // Check the result of clang *after* checking curl; in case the download failed,
  208. // only curl's output will be useful.
  209. clang?;
  210. }
  211. let mut errors = Vec::new();
  212. for kernel_image in kernel_image {
  213. // Guess the guest architecture.
  214. let mut cmd = Command::new("file");
  215. let output = cmd
  216. .arg("--brief")
  217. .arg(&kernel_image)
  218. .output()
  219. .with_context(|| format!("failed to run {cmd:?}"))?;
  220. let Output { status, .. } = &output;
  221. if status.code() != Some(0) {
  222. bail!("{cmd:?} failed: {output:?}")
  223. }
  224. let Output { stdout, .. } = output;
  225. // Now parse the output of the file command, which looks something like
  226. //
  227. // - Linux kernel ARM64 boot executable Image, little-endian, 4K pages
  228. //
  229. // - Linux kernel x86 boot executable bzImage, version 6.1.0-10-cloud-amd64 [..]
  230. let stdout = String::from_utf8(stdout)
  231. .with_context(|| format!("invalid UTF-8 in {cmd:?} stdout"))?;
  232. let (_, stdout) = stdout
  233. .split_once("Linux kernel")
  234. .ok_or_else(|| anyhow!("failed to parse {cmd:?} stdout: {stdout}"))?;
  235. let (guest_arch, _) = stdout
  236. .split_once("boot executable")
  237. .ok_or_else(|| anyhow!("failed to parse {cmd:?} stdout: {stdout}"))?;
  238. let guest_arch = guest_arch.trim();
  239. let (guest_arch, machine, cpu) = match guest_arch {
  240. "ARM64" => ("aarch64", Some("virt"), Some("cortex-a57")),
  241. "x86" => ("x86_64", Some("q35"), Some("qemu64")),
  242. guest_arch => (guest_arch, None, None),
  243. };
  244. let target = format!("{guest_arch}-unknown-linux-musl");
  245. // Build our init program. The contract is that it will run anything it finds in /bin.
  246. let init = build(Some(&target), |cmd| {
  247. cmd.args(["--package", "init", "--profile", "release"])
  248. })
  249. .context("building init program failed")?;
  250. let init = match &*init {
  251. [(name, init)] => {
  252. if name != "init" {
  253. bail!("expected init program to be named init, found {name}")
  254. }
  255. init
  256. }
  257. init => bail!("expected exactly one init program, found {init:?}"),
  258. };
  259. let binaries = binaries(Some(&target))?;
  260. let tmp_dir = tempfile::tempdir().context("tempdir failed")?;
  261. let initrd_image = tmp_dir.path().join("qemu-initramfs.img");
  262. let initrd_image_file = File::create(&initrd_image).with_context(|| {
  263. format!("failed to create {} for writing", initrd_image.display())
  264. })?;
  265. let mut gen_init_cpio = Command::new(&gen_init_cpio);
  266. let mut gen_init_cpio_child = gen_init_cpio
  267. .arg("-")
  268. .stdin(Stdio::piped())
  269. .stdout(initrd_image_file)
  270. .spawn()
  271. .with_context(|| format!("failed to spawn {gen_init_cpio:?}"))?;
  272. let Child { stdin, .. } = &mut gen_init_cpio_child;
  273. let mut stdin = stdin.take().unwrap();
  274. use std::os::unix::ffi::OsStrExt as _;
  275. // Send input into gen_init_cpio which looks something like
  276. //
  277. // file /init path-to-init 0755 0 0
  278. // dir /bin 0755 0 0
  279. // file /bin/foo path-to-foo 0755 0 0
  280. // file /bin/bar path-to-bar 0755 0 0
  281. for bytes in [
  282. "file /init ".as_bytes(),
  283. init.as_os_str().as_bytes(),
  284. " 0755 0 0\n".as_bytes(),
  285. "dir /bin 0755 0 0\n".as_bytes(),
  286. ] {
  287. stdin.write_all(bytes).expect("write");
  288. }
  289. for (profile, binaries) in binaries {
  290. for (name, binary) in binaries {
  291. let name = format!("{}-{}", profile, name);
  292. let path = tmp_dir.path().join(&name);
  293. copy(&binary, &path).with_context(|| {
  294. format!("copy({}, {}) failed", binary.display(), path.display())
  295. })?;
  296. for bytes in [
  297. "file /bin/".as_bytes(),
  298. name.as_bytes(),
  299. " ".as_bytes(),
  300. path.as_os_str().as_bytes(),
  301. " 0755 0 0\n".as_bytes(),
  302. ] {
  303. stdin.write_all(bytes).expect("write");
  304. }
  305. }
  306. }
  307. // Must explicitly close to signal EOF.
  308. drop(stdin);
  309. let output = gen_init_cpio_child
  310. .wait_with_output()
  311. .with_context(|| format!("failed to wait for {gen_init_cpio:?}"))?;
  312. let Output { status, .. } = &output;
  313. if status.code() != Some(0) {
  314. bail!("{gen_init_cpio:?} failed: {output:?}")
  315. }
  316. let mut qemu = Command::new(format!("qemu-system-{guest_arch}"));
  317. if let Some(machine) = machine {
  318. qemu.args(["-machine", machine]);
  319. }
  320. if guest_arch == ARCH {
  321. match OS {
  322. "linux" => match metadata("/dev/kvm") {
  323. Ok(metadata) => {
  324. use std::os::unix::fs::FileTypeExt as _;
  325. if metadata.file_type().is_char_device() {
  326. qemu.args(["-accel", "kvm"]);
  327. }
  328. }
  329. Err(error) => {
  330. if error.kind() != ErrorKind::NotFound {
  331. Err(error).context("failed to check existence of /dev/kvm")?;
  332. }
  333. }
  334. },
  335. "macos" => {
  336. qemu.args(["-accel", "hvf"]);
  337. }
  338. os => bail!("unsupported OS: {os}"),
  339. }
  340. } else if let Some(cpu) = cpu {
  341. qemu.args(["-cpu", cpu]);
  342. }
  343. let console = OsString::from("ttyS0");
  344. let mut kernel_args = std::iter::once(("console", &console))
  345. .chain(run_args.clone().map(|run_arg| ("init.arg", run_arg)))
  346. .enumerate()
  347. .fold(OsString::new(), |mut acc, (i, (k, v))| {
  348. if i != 0 {
  349. acc.push(" ");
  350. }
  351. acc.push(k);
  352. acc.push("=");
  353. acc.push(v);
  354. acc
  355. });
  356. // We sometimes see kernel panics containing:
  357. //
  358. // [ 0.064000] Kernel panic - not syncing: IO-APIC + timer doesn't work! Boot with apic=debug and send a report. Then try booting with the 'noapic' option.
  359. //
  360. // Heed the advice and boot with noapic. We don't know why this happens.
  361. kernel_args.push(" noapic");
  362. qemu.args(["-no-reboot", "-nographic", "-m", "512M", "-smp", "2"])
  363. .arg("-append")
  364. .arg(kernel_args)
  365. .arg("-kernel")
  366. .arg(&kernel_image)
  367. .arg("-initrd")
  368. .arg(&initrd_image);
  369. if guest_arch == "aarch64" {
  370. match OS {
  371. "linux" => {
  372. let mut cmd = Command::new("locate");
  373. let output = cmd
  374. .arg("QEMU_EFI.fd")
  375. .output()
  376. .with_context(|| format!("failed to run {cmd:?}"))?;
  377. let Output { status, .. } = &output;
  378. if status.code() != Some(0) {
  379. bail!("{qemu:?} failed: {output:?}")
  380. }
  381. let Output { stdout, .. } = output;
  382. let bios = String::from_utf8(stdout)
  383. .with_context(|| format!("failed to parse output of {cmd:?}"))?;
  384. qemu.args(["-bios", bios.trim()]);
  385. }
  386. "macos" => {
  387. let mut cmd = Command::new("brew");
  388. let output = cmd
  389. .args(["list", "qemu", "-1", "-v"])
  390. .output()
  391. .with_context(|| format!("failed to run {cmd:?}"))?;
  392. let Output { status, .. } = &output;
  393. if status.code() != Some(0) {
  394. bail!("{qemu:?} failed: {output:?}")
  395. }
  396. let Output { stdout, .. } = output;
  397. let output = String::from_utf8(stdout)
  398. .with_context(|| format!("failed to parse output of {cmd:?}"))?;
  399. const NAME: &str = "edk2-aarch64-code.fd";
  400. let bios = output.lines().find(|line| line.contains(NAME)).ok_or_else(
  401. || anyhow!("failed to find {NAME} in output of {cmd:?}: {output}"),
  402. )?;
  403. qemu.args(["-bios", bios.trim()]);
  404. }
  405. os => bail!("unsupported OS: {os}"),
  406. };
  407. }
  408. let mut qemu_child = qemu
  409. .stdin(Stdio::piped())
  410. .stdout(Stdio::piped())
  411. .stderr(Stdio::piped())
  412. .spawn()
  413. .with_context(|| format!("failed to spawn {qemu:?}"))?;
  414. let Child {
  415. stdin,
  416. stdout,
  417. stderr,
  418. ..
  419. } = &mut qemu_child;
  420. let stdin = stdin.take().unwrap();
  421. let stdin = Arc::new(Mutex::new(stdin));
  422. let stdout = stdout.take().unwrap();
  423. let stdout = BufReader::new(stdout);
  424. let stderr = stderr.take().unwrap();
  425. let stderr = BufReader::new(stderr);
  426. const TERMINATE_AFTER_COUNT: &[(&str, usize)] =
  427. &[("end Kernel panic", 0), ("watchdog: BUG: soft lockup", 1)];
  428. let mut counts = [0; TERMINATE_AFTER_COUNT.len()];
  429. let mut terminate_if_kernel_hang =
  430. move |line: &str, stdin: &Arc<Mutex<ChildStdin>>| -> anyhow::Result<()> {
  431. if let Some(i) = TERMINATE_AFTER_COUNT
  432. .iter()
  433. .position(|(marker, _)| line.contains(marker))
  434. {
  435. counts[i] += 1;
  436. let (marker, max) = TERMINATE_AFTER_COUNT[i];
  437. if counts[i] > max {
  438. println!("{marker} detected > {max} times; terminating QEMU");
  439. let mut stdin = stdin.lock().unwrap();
  440. stdin
  441. .write_all(&[0x01, b'x'])
  442. .context("failed to write to stdin")?;
  443. println!("waiting for QEMU to terminate");
  444. }
  445. }
  446. Ok(())
  447. };
  448. let stderr = {
  449. let stdin = stdin.clone();
  450. thread::Builder::new()
  451. .spawn(move || {
  452. for line in stderr.lines() {
  453. let line = line.context("failed to read line from stderr")?;
  454. eprintln!("{}", line);
  455. terminate_if_kernel_hang(&line, &stdin)?;
  456. }
  457. anyhow::Ok(())
  458. })
  459. .unwrap()
  460. };
  461. let mut outcome = None;
  462. for line in stdout.lines() {
  463. let line = line.context("failed to read line from stdout")?;
  464. println!("{}", line);
  465. terminate_if_kernel_hang(&line, &stdin)?;
  466. // The init program will print "init: success" or "init: failure" to indicate
  467. // the outcome of running the binaries it found in /bin.
  468. if let Some(line) = line.strip_prefix("init: ") {
  469. let previous = match line {
  470. "success" => outcome.replace(Ok(())),
  471. "failure" => outcome.replace(Err(())),
  472. line => bail!("unexpected init output: {}", line),
  473. };
  474. if let Some(previous) = previous {
  475. bail!("multiple exit status: previous={previous:?}, current={line}");
  476. }
  477. }
  478. }
  479. let output = qemu_child
  480. .wait_with_output()
  481. .with_context(|| format!("failed to wait for {qemu:?}"))?;
  482. let Output { status, .. } = &output;
  483. if status.code() != Some(0) {
  484. bail!("{qemu:?} failed: {output:?}")
  485. }
  486. stderr.join().unwrap()?;
  487. let outcome = outcome.ok_or(anyhow!("init did not exit"))?;
  488. match outcome {
  489. Ok(()) => {}
  490. Err(()) => {
  491. errors.push(anyhow!("VM binaries failed on {}", kernel_image.display()))
  492. }
  493. }
  494. }
  495. if errors.is_empty() {
  496. Ok(())
  497. } else {
  498. Err(Errors::new(errors).into())
  499. }
  500. }
  501. }
  502. }