diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..767dae2 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,10 @@ +# Generated by Cargo +# will have compiled files and executables +target/ + +# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries +# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html +Cargo.lock + +# These are backup files generated by rustfmt +**/*.rs.bk diff --git a/.github/.gitignore b/.github/.gitignore new file mode 100644 index 0000000..cd3d225 --- /dev/null +++ b/.github/.gitignore @@ -0,0 +1 @@ +logs \ No newline at end of file diff --git a/.github/rsrc/.gitignore b/.github/rsrc/.gitignore new file mode 100644 index 0000000..ad30bfe --- /dev/null +++ b/.github/rsrc/.gitignore @@ -0,0 +1 @@ +*.yml \ No newline at end of file diff --git a/.github/rsrc/id_rsa b/.github/rsrc/id_rsa new file mode 100644 index 0000000..5f13cfc --- /dev/null +++ b/.github/rsrc/id_rsa @@ -0,0 +1,38 @@ +-----BEGIN OPENSSH PRIVATE KEY----- +b3BlbnNzaC1rZXktdjEAAAAABG5vbmUAAAAEbm9uZQAAAAAAAAABAAABlwAAAAdzc2gtcn +NhAAAAAwEAAQAAAYEA2bW01Jm9ujWY0VoDuL3D4XmQ09D1Tkyg98MQoXNg7nZ6IJP2F8PW +N4OHRwrd6/flcDj5c8YfgkwqCryu8f/qq+nlgaQ8dskJIUnaLa3YtVax2KrQu/2zHH6StI +abiQYKM+6L0OPZxwrbX+uSfs/QEAF0KCxcXVDjvXjpvOla+MWunm9B+s11yj+qesSc+MWp +gZKvZYH8FmIIy+P77cnnjDgXx51vHHdO8rI4uLuoNpaMQjqbtpGP3pFYbqAId1lrQSBzVJ +4ybrhZw/+pfzhoXrHDCd4lTd6fihj0IHQkPmuxgd09EZONfsiDGX7iUoTrRvGt9WjCVClz +vjPgCIy/0hpUG4/+1GAtX4V3rN84gT1e+qThFL8GimiPzUXL1abT5YQiWCbN/eJ90kd/Tj +fhsAhD1xOOK8TlHi/28COF+czhSektncm0Wrng5wWYpZkJG7yX4GE5MKWikgCXB4SvCW81 +0+Ievgk1g7nL2c8oynYixG2JfE4q3gcFmOf9UwixAAAFiE9S/jRPUv40AAAAB3NzaC1yc2 +EAAAGBANm1tNSZvbo1mNFaA7i9w+F5kNPQ9U5MoPfDEKFzYO52eiCT9hfD1jeDh0cK3ev3 +5XA4+XPGH4JMKgq8rvH/6qvp5YGkPHbJCSFJ2i2t2LVWsdiq0Lv9sxx+krSGm4kGCjPui9 +Dj2ccK21/rkn7P0BABdCgsXF1Q47146bzpWvjFrp5vQfrNdco/qnrEnPjFqYGSr2WB/BZi +CMvj++3J54w4F8edbxx3TvKyOLi7qDaWjEI6m7aRj96RWG6gCHdZa0Egc1SeMm64WcP/qX +84aF6xwwneJU3en4oY9CB0JD5rsYHdPRGTjX7Igxl+4lKE60bxrfVowlQpc74z4AiMv9Ia +VBuP/tRgLV+Fd6zfOIE9Xvqk4RS/Bopoj81Fy9Wm0+WEIlgmzf3ifdJHf0434bAIQ9cTji +vE5R4v9vAjhfnM4UnpLZ3JtFq54OcFmKWZCRu8l+BhOTClopIAlweErwlvNdPiHr4JNYO5 +y9nPKMp2IsRtiXxOKt4HBZjn/VMIsQAAAAMBAAEAAAGAB2nBl1zoDgRz0HqcvnRXPHjsyJ +qglLbFIySdLwswR3RpMI1rO3hNlbi0wTN2dqnLciqde17JXUDhzFlsCVVcELgjWoqMrMSI +Cx4yN7yYAMY7wm+AEauoBvoMHamo94WpMKcgc4ejp2x4J3QsegUSXg4nnfPTPhHyXEX3Cw +nE4UxNZ6uNCwjGmFE6SUmDOREVdlIX5vhh7KLwTt2Dqz9VEyQokqFqqnTpTzgoeGla5ydx +jZKEcXczx3nKYaNxQFue/ElJw9KabSAZ3v3T9rzw0z3rP7n0Bvn/xs7+i2cKJhJIkQtPUa +Z1r/GMrE+yp3BMYw5vcqKx+OOvNPkoIiRSzuyJ3qJK4okibtDCkYfPk/FxuCX/7nVlL/Ak +n3CaVWWWGTGOyohW6+MxJ/z16gqgXNy8gwKRnsIEUtpc7RoGIO8MT5fVrMOhlwmJHnPnEm +2/185hraTHLrb8V6suTwCvbzB65e3TSQaJXyMqOyYSdOXM3fmpZrSmHw7y8DqJ74lhAAAA +wDIxqOfvRc7j736AKgp2qDhJftc/mb8osZsbPgApCbVLZmI9Zvy1PFDEcNfDunU06Mue+2 +yaWhaeQ180UAqJ4E3vgE1OkBMae+bPyk/v5HeGmZfxvcFhxP87/zMxqARxVLy2ECDS2NR5 +A/G5bL4VJatmFJiAa5N137X5FDlctvy5h+ZEhMJdy/LYCJld5mM7EMoa3AKErkSnhdI+mH +v9x34mWSzeB198qGb7QMqvhGLu7ZEVqRAIaAZ5HhkIx8oO0QAAAMEA97+ZMXEbNQFTyFMO +GuT42A3wU0RJmKqRD3wQDeW5Ua/RF+WeaMxAWRZG5Pj8X0p4WAsupJEfU4FZFsQhmu0Kzr +vvTEHmFBmupnYdwmQSDk4SkJLrXNQSpkFoxTdSDZ3/P+WgDAdC1pU1/ClAXS7gQZhWLzCo +IGDIFkUEvcUCxh0Jf8YszFWWb3WZ7lOBFdQCyS6SmvQtyq+M8ghi0Ce+btU3TvpFPRKa0p +LT9vw3LMhjeYidBrzZu/gvWWuhJb3hAAAAwQDg9fyPweE++XeHXtyzqDZIRRw5O/fsEGow +cgSDfII2zJhiPOzO55HuguS92uak7oLsoFx5+Ulw9jdFu+spn2W2+iAaSlg9CTQVSt/Z/W +gyN48rYziYtoC0eKcKNdXoMX+ovAdwRA7LfGVc3MKm1jX+tlJ6lcWT5ZAodGdDX+mzMJfA +8U6K4Z/lgId5D/xZjNMm5n+dZ7rnLcQoUsa7nnrJ6wob4nDDEYDIGx4FvDIyyEupRmesmt +2S9f5i0kz7hNEAAAAQZmVkb3JhQGxvY2FsaG9zdAECAw== +-----END OPENSSH PRIVATE KEY----- diff --git a/.github/rsrc/id_rsa.pub b/.github/rsrc/id_rsa.pub new file mode 100644 index 0000000..b742645 --- /dev/null +++ b/.github/rsrc/id_rsa.pub @@ -0,0 +1 @@ +ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQDZtbTUmb26NZjRWgO4vcPheZDT0PVOTKD3wxChc2Dudnogk/YXw9Y3g4dHCt3r9+VwOPlzxh+CTCoKvK7x/+qr6eWBpDx2yQkhSdotrdi1VrHYqtC7/bMcfpK0hpuJBgoz7ovQ49nHCttf65J+z9AQAXQoLFxdUOO9eOm86Vr4xa6eb0H6zXXKP6p6xJz4xamBkq9lgfwWYgjL4/vtyeeMOBfHnW8cd07ysji4u6g2loxCOpu2kY/ekVhuoAh3WWtBIHNUnjJuuFnD/6l/OGhescMJ3iVN3p+KGPQgdCQ+a7GB3T0Rk41+yIMZfuJShOtG8a31aMJUKXO+M+AIjL/SGlQbj/7UYC1fhXes3ziBPV76pOEUvwaKaI/NRcvVptPlhCJYJs394n3SR39ON+GwCEPXE44rxOUeL/bwI4X5zOFJ6S2dybRaueDnBZilmQkbvJfgYTkwpaKSAJcHhK8JbzXT4h6+CTWDucvZzyjKdiLEbYl8TireBwWY5/1TCLE= fedora@localhost diff --git a/.github/rsrc/seed.img b/.github/rsrc/seed.img new file mode 100644 index 0000000..6f41b8e Binary files /dev/null and b/.github/rsrc/seed.img differ diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 6816735..fbe1ad9 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -10,26 +10,84 @@ env: CARGO_TERM_COLOR: always jobs: - build: + test_plugins_linux: + name: Build and Test Plugins (Linux) runs-on: ubuntu-latest + container: ubuntu:22.04 + steps: + - name: Install QEMU User + run: | + apt-get -y update && \ + apt-get -y install git curl qemu-user build-essential + - uses: dtolnay/rust-toolchain@nightly + - uses: actions/checkout@v4 + - name: Test QEMU Install + run: | + qemu-x86_64 --help + - name: Build and Test Tracer + run: | + cd plugins/tracer + cargo build -r || exit 0 + cargo build -r + cargo run -r --bin tracer -- -a /bin/ls -- -lah + cd ../.. + - name: Build and Test Tiny + run: | + cd plugins/tiny + cargo build -r + qemu-x86_64 -plugin ../../target/release/libtiny.so /bin/ls -lah + cd ../.. + + test_plugins_windows: + name: Build and Test Plugins (Windows) + runs-on: windows-latest + env: + # QEMU 8.1.0 + QEMU_URL: "https://qemu.weilnetz.de/w64/2023/qemu-w64-setup-20230822.exe" + RUSTUP_URL: "https://win.rustup.rs/x86_64" + FEDORA_CLOUDIMG_URL: "https://download.fedoraproject.org/pub/fedora/linux/releases/39/Cloud/x86_64/images/Fedora-Cloud-Base-39-1.5.x86_64.qcow2" steps: - - name: Free build space - uses: easimon/maximize-build-space@master + - uses: msys2/setup-msys2@v2 with: - root-reserve-mb: 2048 - temp-reserve-mb: 256 - swap-size-mb: 1024 - remove-dotnet: 'true' - remove-android: 'true' - remove-haskell: 'true' - remove-codeql: 'true' - remove-docker-images: 'true' - - name: Install QEMU Build Dependencies - run: | - sudo apt-get -y install git libglib2.0-dev libfdt-dev \ - libpixman-1-dev zlib1g-dev ninja-build - - uses: dtolnay/rust-toolchain@nightly - - uses: actions/checkout@v3 - - name: Build - run: cargo build -vv -p tracer + msystem: UCRT64 + update: true + install: git mingw-w64-ucrt-x86_64-gcc + location: C:\msys-custom + + - name: Download and Install Rust + run: | + $ProgressPreference = 'SilentlyContinue' + Invoke-WebRequest -Uri ${{ env.RUSTUP_URL }} -OutFile rustup-init.exe + ./rustup-init.exe --default-toolchain nightly --default-host x86_64-pc-windows-gnu -y + + - name: Install QEMU + shell: msys2 {0} + run: | + pacman -Syu --noconfirm + pacman -Sy mingw-w64-ucrt-x86_64-qemu --noconfirm + + - name: Test QEMU + run: | + C:\msys-custom\msys64\ucrt64\bin\qemu-system-x86_64.exe --version + + - uses: actions/checkout@v4 + + - name: Download Cloud Image + run: | + $ProgressPreference = 'SilentlyContinue' + Invoke-WebRequest -Uri ${{ env.FEDORA_CLOUDIMG_URL }} -OutFile Fedora-Cloud-Base-39-1.5.x86_64.qcow2 + ls + + - name: Build and Test Tiny + run: | + cd plugins/tiny-system + cargo build -r + cd ../.. + $process = Start-Process PowerShell.exe -NoNewWindow -RedirectStandardOutput out.txt -RedirectStandardError err.txt -PassThru -ArgumentList "-Command", "C:\msys-custom\msys64\ucrt64\bin\qemu-system-x86_64.exe -machine type=q35 -m 2G -nographic -device virtio-net-pci,netdev=net0 -netdev user,id=net0,hostfwd=tcp::2222-:22 -drive if=virtio,format=qcow2,file=Fedora-Cloud-Base-39-1.5.x86_64.qcow2 -drive if=virtio,format=raw,file=.github/rsrc/seed.img -plugin target/release/tiny_system.dll" + echo "Sleeping 180.0 seconds until booted (boot process took 118s first time)" + Start-Sleep -Seconds 180.0 + echo "Stopping process" + Stop-Process -Id $process.id + cat out.txt + cat err.txt diff --git a/.gitignore b/.gitignore index 767dae2..f296e4b 100644 --- a/.gitignore +++ b/.gitignore @@ -8,3 +8,6 @@ Cargo.lock # These are backup files generated by rustfmt **/*.rs.bk +.env +.secrets +*~ \ No newline at end of file diff --git a/Cargo.toml b/Cargo.toml index f9921b4..c89807b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,9 +15,12 @@ resolver = "2" members = [ "qemu", "qemu-plugin", - "qemu-plugin/examples/tiny", - "qemu-plugin/examples/tracer", + "qemu-plugin-sys", + "plugins/tiny", + "plugins/tiny-system", + "plugins/tracer", ] +default-members = ["qemu-plugin", "qemu-plugin-sys"] [workspace.dependencies] qemu-plugin-sys = { version = "8.1.3-v3", path = "qemu-plugin-sys" } diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..75eaa00 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,17 @@ +FROM ubuntu:22.04 + +ENV PATH="${PATH}:/root/.cargo/bin" + +RUN apt-get -y update && \ + apt-get -y install bison flex git curl libglib2.0-dev libfdt-dev \ + libpixman-1-dev zlib1g-dev ninja-build build-essential python3 python3-pip python3-venv && \ + python3 -m pip install sphinx sphinx_rtd_theme && \ + curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y --default-toolchain nightly + +COPY . /qemu-rs + +WORKDIR /qemu-rs + +RUN cargo build -r && \ + cargo run -r --bin tracer -- -a /bin/ls -- -lah + diff --git a/README.md b/README.md index 4027b58..d3570b0 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,7 @@ QEMU for Rust, and Rust for QEMU! This repository provides several QEMU-related crates: -* [qemu](https://github.com/novaafcing/qemu-rs/tree/main/qemu): QEMU binary installer +* [qemu](https://github.com/novafacing/qemu-rs/tree/main/qemu): QEMU binary installer * [qemu-plugin-sys](https://github.com/novafacing/qemu-rs/tree/main/qemu-plugin-sys): Low level bindings to the QEMU plugin API * [qemu-plugin](https://github.com/novafacing/qemu-rs/tree/main/qemu-plugin): High level bindings to the QEMU plugin API @@ -21,4 +21,20 @@ and instructions) of a program like: ```sh cargo run -r --bin tracer -- -a /bin/ls -- -lah -``` \ No newline at end of file +``` + +## Installing QEMU + +This repository also provides a crate (`qemu`) which builds QEMU from source and +installs Rust wrappers for QEMU as binaries. + +You can install QEMU with (add any additional features you need, e.g. `plugins`): + +```sh +cargo install qemu@8.1.3-v3 --features=binaries +``` + +On some systems, particularly BTRFS systems, `/tmp` may not be large enough for the +temporary build directory (QEMU is quite large to build). In this case, create a +directory on your root filesystem (e.g. `$HOME/.cargo/tmp`) and set +`CARGO_TARGET_DIR=$HOME/.cargo/tmp` when running the install command. \ No newline at end of file diff --git a/plugins/tiny-system/Cargo.toml b/plugins/tiny-system/Cargo.toml new file mode 100644 index 0000000..47d3942 --- /dev/null +++ b/plugins/tiny-system/Cargo.toml @@ -0,0 +1,13 @@ +[package] +name = "tiny-system" +version = "0.1.0" +edition = "2021" + +[lib] +crate-type = ["cdylib"] + +[dependencies] +qemu-plugin.workspace = true +anyhow = "1.0.75" +ffi = "0.1.0" +ctor = "0.2.6" diff --git a/plugins/tiny-system/src/lib.rs b/plugins/tiny-system/src/lib.rs new file mode 100644 index 0000000..22b85d1 --- /dev/null +++ b/plugins/tiny-system/src/lib.rs @@ -0,0 +1,58 @@ +use anyhow::{anyhow, Result}; +use ctor::ctor; +use qemu_plugin::{ + plugin::{HasCallbacks, Plugin, Register, PLUGIN}, + PluginId, +}; +use std::sync::Mutex; + +struct TinyTrace {} + +impl Plugin for TinyTrace {} +impl Register for TinyTrace {} + +impl HasCallbacks for TinyTrace { + fn on_vcpu_init( + &mut self, + id: PluginId, + vcpu_id: qemu_plugin::VCPUIndex, + ) -> std::prelude::v1::Result<(), anyhow::Error> { + println!("on_vcpu_init: id: {:?}, vcpu_id: {:?}", id, vcpu_id); + Ok(()) + } + + fn on_vcpu_idle( + &mut self, + id: PluginId, + vcpu_id: qemu_plugin::VCPUIndex, + ) -> std::prelude::v1::Result<(), anyhow::Error> { + println!("on_vcpu_idle: id: {:?}, vcpu_id: {:?}", id, vcpu_id); + Ok(()) + } + + fn on_vcpu_exit( + &mut self, + id: PluginId, + vcpu_id: qemu_plugin::VCPUIndex, + ) -> std::prelude::v1::Result<(), anyhow::Error> { + println!("on_vcpu_exit: id: {:?}, vcpu_id: {:?}", id, vcpu_id); + Ok(()) + } + + fn on_vcpu_resume( + &mut self, + id: PluginId, + vcpu_id: qemu_plugin::VCPUIndex, + ) -> std::prelude::v1::Result<(), anyhow::Error> { + println!("on_vcpu_resume: id: {:?}, vcpu_id: {:?}", id, vcpu_id); + Ok(()) + } +} + +#[ctor] +fn init() { + PLUGIN + .set(Mutex::new(Box::new(TinyTrace {}))) + .map_err(|_| anyhow!("Failed to set plugin")) + .expect("Failed to set plugin"); +} diff --git a/qemu-plugin/examples/tiny/Cargo.toml b/plugins/tiny/Cargo.toml similarity index 100% rename from qemu-plugin/examples/tiny/Cargo.toml rename to plugins/tiny/Cargo.toml diff --git a/qemu-plugin/examples/tiny/src/lib.rs b/plugins/tiny/src/lib.rs similarity index 100% rename from qemu-plugin/examples/tiny/src/lib.rs rename to plugins/tiny/src/lib.rs diff --git a/plugins/tracer/Cargo.toml b/plugins/tracer/Cargo.toml new file mode 100644 index 0000000..1f6e44c --- /dev/null +++ b/plugins/tracer/Cargo.toml @@ -0,0 +1,35 @@ +[package] +name = "tracer" +version = "0.1.0" +edition = "2021" + +[lib] +name = "tracer" +crate-type = ["cdylib", "lib"] + +[dependencies] +anyhow = "1.0.75" +ctor = "0.2.6" +qemu-plugin = { workspace = true, features = ["unix-weak-link"] } +serde = { version = "1.0.193", features = ["derive"] } +serde_cbor = "0.11.2" +tokio = { version = "1.35.0", features = ["full"] } +typed-builder = "0.18.0" +yaxpeax-x86 = "1.2.2" + +# Dependencies only used by this crate's `tracer` binary. We do not use dev-dependencies +# because they cannot be optional. +clap = { version = "4.4.11", features = ["derive", "string"] } +# Enable the `qemu` feature to build and install QEMU with the `qemu` crate instead +# of trying to use the system QEMU. +qemu = { workspace = true, features = [ + "plugins", + "debug-info", +], optional = true } +memfd-exec = { version = "0.2.1", optional = true } +rand = "0.8.5" +serde_json = "1.0.108" + +[features] +qemu = ["dep:memfd-exec", "dep:qemu"] +default = [] diff --git a/qemu-plugin/examples/tracer/Cargo.toml b/plugins/tracer/Cargo.toml~ similarity index 63% rename from qemu-plugin/examples/tracer/Cargo.toml rename to plugins/tracer/Cargo.toml~ index 9da2592..8551f8e 100644 --- a/qemu-plugin/examples/tracer/Cargo.toml +++ b/plugins/tracer/Cargo.toml~ @@ -8,8 +8,7 @@ name = "tracer" crate-type = ["cdylib", "lib"] [dependencies] -qemu = { workspace = true, features = ["plugins", "debug-info"] } -qemu-plugin = { workspace = true, features = ["weak"] } +qemu-plugin = { workspace = true, features = ["unix-weak-link"] } anyhow = "1.0.75" ffi = "0.1.0" ctor = "0.2.6" @@ -22,3 +21,8 @@ serde = { version = "1.0.193", features = ["derive"] } memfd-exec = "0.2.1" serde_json = "1.0.108" yaxpeax-x86 = "1.2.2" + +# Enable the `qemu` feature to build and install QEMU with the `qemu` crate instead +# of trying to use the system QEMU. +qemu = { workspace = true, features = ["plugins", "debug-info"], optional = true } + diff --git a/plugins/tracer/src/bin/tracer.rs b/plugins/tracer/src/bin/tracer.rs new file mode 100644 index 0000000..a653c4c --- /dev/null +++ b/plugins/tracer/src/bin/tracer.rs @@ -0,0 +1,319 @@ +use anyhow::{anyhow, Error, Result}; +use clap::Parser; +#[cfg(feature = "memfd-exec")] +use memfd_exec::{MemFdExecutable, Stdio}; +#[cfg(feature = "qemu")] +use qemu::QEMU_X86_64_LINUX_USER; +use rand::{distributions::Alphanumeric, thread_rng, Rng}; +use serde_cbor::Deserializer; +use serde_json::to_string; +#[cfg(not(feature = "qemu"))] +use std::process::{Command, Stdio}; +use std::{ + fs::OpenOptions, + io::{stdout, BufRead, BufReader, Write}, + os::unix::net::UnixListener, + path::{Path, PathBuf}, +}; +use tokio::{ + fs::{read, remove_file, write}, + join, main, spawn, + task::spawn_blocking, +}; +use tracer::Event; + +#[cfg(debug_assertions)] +const PLUGIN: &[u8] = include_bytes!(concat!( + env!("CARGO_MANIFEST_DIR"), + "/../../target/debug/libtracer.so" +)); + +#[cfg(not(debug_assertions))] +const PLUGIN: &[u8] = include_bytes!(concat!( + env!("CARGO_MANIFEST_DIR"), + "/../../target/release/libtracer.so" +)); + +fn tmp(prefix: &str, suffix: &str) -> PathBuf { + PathBuf::from(format!( + "{}{}{}", + prefix, + thread_rng() + .sample_iter(&Alphanumeric) + .take(8) + .map(char::from) + .collect::(), + suffix + )) +} + +#[derive(Parser, Debug, Clone)] +/// Run QEMU with a plugin that logs events. To pass arguments to QEMU, use the QEMU environment +/// variables. +struct Args { + #[clap(short = 'i', long)] + /// Whether instructions should be logged + pub log_insns: bool, + #[clap(short = 'm', long)] + /// Whether memory accesses should be logged + pub log_mem: bool, + #[clap(short = 's', long)] + /// Whether syscalls should be logged + pub log_syscalls: bool, + #[clap(short = 'a', long)] + /// Whether all events should be logged + pub log_all: bool, + #[clap(short = 'I', long)] + /// An input file to use as the program's stdin, otherwise the driver's stdin is used + pub input_file: Option, + #[clap(short = 'O', long)] + /// An output file to write the trace to, otherwise stdout is used + pub output_file: Option, + /// The program to run + #[clap()] + pub program: PathBuf, + /// The arguments to the program + #[clap(num_args = 1.., last = true)] + pub args: Vec, +} + +impl Args { + fn to_plugin_args(&self) -> String { + format!( + "log_insns={},log_mem={},log_syscalls={}", + self.log_insns | self.log_all, + self.log_mem | self.log_all, + self.log_syscalls | self.log_all + ) + } + + fn to_qemu_args(&self, socket_path: &Path, plugin_path: &Path) -> Result> { + let mut qemu_args = vec![ + "-plugin".to_string(), + format!( + "{},{},socket_path={}", + plugin_path.display(), + self.to_plugin_args(), + socket_path.display() + ), + "--".to_string(), + self.program + .to_str() + .ok_or_else(|| anyhow!("Failed to convert program path to string"))? + .to_string(), + ]; + + qemu_args.extend(self.args.clone()); + + Ok(qemu_args) + } +} + +#[cfg(feature = "qemu")] +async fn run(input: Option>, args: Vec) -> Result<()> { + let mut exe = MemFdExecutable::new("qemu", QEMU_X86_64_LINUX_USER) + .args(args) + .stdin(if input.is_some() { + Stdio::piped() + } else { + Stdio::inherit() + }) + .stdout(Stdio::piped()) + .stderr(Stdio::piped()) + .spawn()?; + + if let Some(input) = input { + let mut stdin = exe.stdin.take().ok_or_else(|| anyhow!("No stdin"))?; + spawn_blocking(move || stdin.write_all(&input)); + } + + let stdout = exe.stdout.take().ok_or_else(|| anyhow!("No stdout"))?; + + let out_reader = spawn_blocking(move || { + let mut line = String::new(); + let mut out_reader = BufReader::new(stdout); + + loop { + line.clear(); + + if let 0 = out_reader.read_line(&mut line)? { + break; + } + + let line = line.trim(); + + if !line.is_empty() { + println!("{line}"); + } + } + + Ok::<(), Error>(()) + }); + + let stderr = exe.stderr.take().ok_or_else(|| anyhow!("No stderr"))?; + + let err_reader = spawn_blocking(move || { + let mut line = String::new(); + let mut err_reader = BufReader::new(stderr); + + loop { + line.clear(); + + if let 0 = err_reader.read_line(&mut line)? { + break; + } + + let line = line.trim(); + + if !line.is_empty() { + eprintln!("{line}"); + } + } + + Ok::<(), Error>(()) + }); + + let waiter = spawn_blocking(move || exe.wait()); + + let (out_res, err_res, waiter_res) = join!(out_reader, err_reader, waiter); + + out_res??; + err_res??; + waiter_res??; + + Ok(()) +} + +#[cfg(not(feature = "qemu"))] +async fn run(input: Option>, args: Vec) -> Result<()> { + let mut exe = Command::new("qemu-x86_64") + .args(args) + .stdin(if input.is_some() { + Stdio::piped() + } else { + Stdio::inherit() + }) + .stdout(Stdio::piped()) + .stderr(Stdio::piped()) + .spawn()?; + + if let Some(input) = input { + let mut stdin = exe.stdin.take().ok_or_else(|| anyhow!("No stdin"))?; + spawn_blocking(move || stdin.write_all(&input)); + } + + let stdout = exe.stdout.take().ok_or_else(|| anyhow!("No stdout"))?; + + let out_reader = spawn_blocking(move || { + let mut line = String::new(); + let mut out_reader = BufReader::new(stdout); + + loop { + line.clear(); + + if let 0 = out_reader.read_line(&mut line)? { + break; + } + + let line = line.trim(); + + if !line.is_empty() { + println!("{line}"); + } + } + + Ok::<(), Error>(()) + }); + + let stderr = exe.stderr.take().ok_or_else(|| anyhow!("No stderr"))?; + + let err_reader = spawn_blocking(move || { + let mut line = String::new(); + let mut err_reader = BufReader::new(stderr); + + loop { + line.clear(); + + if let 0 = err_reader.read_line(&mut line)? { + break; + } + + let line = line.trim(); + + if !line.is_empty() { + eprintln!("{line}"); + } + } + + Ok::<(), Error>(()) + }); + + let waiter = spawn_blocking(move || exe.wait()); + + let (out_res, err_res, waiter_res) = join!(out_reader, err_reader, waiter); + + out_res??; + err_res??; + waiter_res??; + + Ok(()) +} + +fn listen

(listen_sock: UnixListener, outfile: Option

) -> Result<()> +where + P: AsRef, +{ + let mut outfile_stream = if let Some(outfile) = outfile.as_ref() { + Box::new(OpenOptions::new().create(true).append(true).open(outfile)?) as Box + } else { + Box::new(stdout()) as Box + }; + + let (mut stream, _) = listen_sock.accept()?; + let it = Deserializer::from_reader(&mut stream).into_iter::(); + + for event in it { + outfile_stream.write_all(to_string(&event?)?.as_bytes())?; + outfile_stream.write_all(b"\n")?; + } + + Ok(()) +} + +#[main] +async fn main() -> Result<()> { + let args = Args::parse(); + + let socket_path = tmp("/tmp/qemu-", ".sock"); + let plugin_path = tmp("/tmp/qemu-", ".so"); + + write(&plugin_path, PLUGIN).await?; + + let input = if let Some(input_file) = args.input_file.as_ref() { + let Ok(input_file) = input_file.canonicalize() else { + return Err(anyhow!("Failed to canonicalize input file")); + }; + + Some(read(input_file).await?) + } else { + None + }; + + let listen_sock = UnixListener::bind(&socket_path)?; + + let qemu_args = args.to_qemu_args(&socket_path, &plugin_path)?; + let qemu_task = spawn(async move { run(input, qemu_args).await }); + + let socket_task = spawn_blocking(move || listen(listen_sock, args.output_file.as_ref())); + + let (qemu_res, socket_res) = join!(qemu_task, socket_task); + + remove_file(&plugin_path).await?; + remove_file(&socket_path).await?; + + qemu_res??; + + socket_res??; + + Ok(()) +} diff --git a/qemu-plugin/examples/tracer/src/bin/tracer.rs b/plugins/tracer/src/bin/tracer.rs~ similarity index 99% rename from qemu-plugin/examples/tracer/src/bin/tracer.rs rename to plugins/tracer/src/bin/tracer.rs~ index a59be68..ead681e 100644 --- a/qemu-plugin/examples/tracer/src/bin/tracer.rs +++ b/plugins/tracer/src/bin/tracer.rs~ @@ -1,6 +1,7 @@ use anyhow::{anyhow, Error, Result}; use clap::Parser; use memfd_exec::{MemFdExecutable, Stdio}; +#[cfg(feature = "qemu")] use qemu::QEMU_X86_64_LINUX_USER; use rand::{distributions::Alphanumeric, thread_rng, Rng}; use serde_cbor::Deserializer; diff --git a/qemu-plugin/examples/tracer/src/lib.rs b/plugins/tracer/src/lib.rs similarity index 100% rename from qemu-plugin/examples/tracer/src/lib.rs rename to plugins/tracer/src/lib.rs diff --git a/qemu-plugin/examples/tracer/tests/.ninja_log b/plugins/tracer/tests/.ninja_log similarity index 100% rename from qemu-plugin/examples/tracer/tests/.ninja_log rename to plugins/tracer/tests/.ninja_log diff --git a/qemu-plugin/examples/tracer/tests/build.ninja b/plugins/tracer/tests/build.ninja similarity index 100% rename from qemu-plugin/examples/tracer/tests/build.ninja rename to plugins/tracer/tests/build.ninja diff --git a/qemu-plugin/examples/tracer/tests/test-aarch64 b/plugins/tracer/tests/test-aarch64 similarity index 100% rename from qemu-plugin/examples/tracer/tests/test-aarch64 rename to plugins/tracer/tests/test-aarch64 diff --git a/qemu-plugin/examples/tracer/tests/test.c b/plugins/tracer/tests/test.c similarity index 100% rename from qemu-plugin/examples/tracer/tests/test.c rename to plugins/tracer/tests/test.c diff --git a/qemu-plugin-sys/Cargo.toml b/qemu-plugin-sys/Cargo.toml index bf07893..3a2058a 100644 --- a/qemu-plugin-sys/Cargo.toml +++ b/qemu-plugin-sys/Cargo.toml @@ -10,3 +10,9 @@ publish.workspace = true readme.workspace = true repository.workspace = true version.workspace = true + +[build-dependencies] +anyhow = "1.0.75" + +[lints.rust] +non_snake_case = "allow" \ No newline at end of file diff --git a/qemu-plugin-sys/build.rs b/qemu-plugin-sys/build.rs new file mode 100644 index 0000000..36e8b6f --- /dev/null +++ b/qemu-plugin-sys/build.rs @@ -0,0 +1,40 @@ +#[cfg(windows)] +use anyhow::anyhow; +use anyhow::Result; +#[cfg(windows)] +use std::{env::var, path::PathBuf, process::Command, str::FromStr}; + +#[cfg(windows)] +fn out_dir() -> Result { + Ok(PathBuf::from( + var("OUT_DIR").map_err(|e| anyhow!("OUT_DIR not set: {e}"))?, + )) +} + +fn main() -> Result<()> { + #[cfg(windows)] + { + let out_dir = out_dir()?; + let def_file = PathBuf::from_str("src/qemu_plugin_api.def")?; + let def_file_str = def_file.to_string_lossy(); + let lib_file = out_dir.join("qemu_plugin_api.lib"); + let lib_file_str = lib_file.to_string_lossy(); + let ch = Command::new("dlltool") + .args([ + "--input-def", + &def_file_str, + "--output-delaylib", + &lib_file_str, + "--dllname", + "qemu.exe", + ]) + .spawn()? + .wait()?; + if !ch.success() { + return Err(anyhow!("dlltool failed")); + } + println!("cargo:rustc-link-search={}", out_dir.display()); + println!("cargo:rustc-link-lib=qemu_plugin_api"); + } + Ok(()) +} diff --git a/qemu-plugin-sys/generate-bindings.rs b/qemu-plugin-sys/generate-bindings.rs index bcc53a6..8be0987 100755 --- a/qemu-plugin-sys/generate-bindings.rs +++ b/qemu-plugin-sys/generate-bindings.rs @@ -1,14 +1,15 @@ -#!/usr/bin/env -S cargo +nightly -Z script - -//! ```cargo -//! [dependencies] -//! anyhow = "*" -//! bindgen = "*" -//! cargo_metadata = "*" -//! reqwest = { version = "*", features = ["blocking"] } -//! tar = "*" -//! xz2 = "*" -//! ``` +#!/usr/bin/env -S cargo +nightly-gnu -Z script +## [package] +## edition = "2021" +## [dependencies] +## anyhow = "*" +## bindgen = "*" +## cargo_metadata = "*" +## reqwest = { version = "*", features = ["blocking"] } +## tar = "*" +## xz2 = "*" +## [lints.rust] +## non_snake_case = "allow" use anyhow::{anyhow, Result}; use bindgen::{ @@ -17,8 +18,10 @@ use bindgen::{ }; use cargo_metadata::MetadataCommand; use reqwest::blocking::get; +#[cfg(windows)] +use std::fs::{read_to_string, write}; use std::{ - fs::{File, OpenOptions, create_dir_all}, + fs::{create_dir_all, File, OpenOptions}, path::Path, }; use tar::Archive; @@ -71,6 +74,16 @@ fn extract_txz(archive: &Path, destination: &Path) -> Result<()> { Ok(()) } +#[cfg(windows)] +fn generate_windows_delaylink_library(qemu_plugin_symbols: &Path, out_dir: &Path) -> Result<()> { + let def_file = out_dir.join("qemu_plugin_api.def"); + let all_commands = read_to_string(qemu_plugin_symbols)?; + let all_commands = all_commands.replace(|x| "{};".contains(x), ""); + write(&def_file, format!("EXPORTS\n{all_commands}"))?; + + Ok(()) +} + fn generate_bindings(qemu_plugin_header: &Path, destination: &Path) -> Result<()> { let rust_bindings = builder() .clang_arg("-fretain-comments-from-system-headers") @@ -92,8 +105,356 @@ fn generate_bindings(qemu_plugin_header: &Path, destination: &Path) -> Result<() .derive_partialeq(true) .generate_comments(true) .header(qemu_plugin_header.to_str().unwrap()) + // Blocklist because we will define these items .blocklist_function("qemu_plugin_install") .blocklist_item("qemu_plugin_version") + // Blocklist because these types are not necessary + .blocklist_item("_INTTYPES_H") + .blocklist_item("_FEATURES_H") + .blocklist_item("_DEFAULT_SOURCE") + .blocklist_item("__GLIBC_USE_ISOC2X") + .blocklist_item("__USE_ISOC11") + .blocklist_item("__USE_ISOC99") + .blocklist_item("__USE_ISOC95") + .blocklist_item("__USE_POSIX_IMPLICITLY") + .blocklist_item("_POSIX_SOURCE") + .blocklist_item("_POSIX_C_SOURCE") + .blocklist_item("__USE_POSIX") + .blocklist_item("__USE_POSIX2") + .blocklist_item("__USE_POSIX199309") + .blocklist_item("__USE_POSIX199506") + .blocklist_item("__USE_XOPEN2K") + .blocklist_item("__USE_XOPEN2K8") + .blocklist_item("_ATFILE_SOURCE") + .blocklist_item("__WORDSIZE") + .blocklist_item("__WORDSIZE_TIME64_COMPAT32") + .blocklist_item("__TIMESIZE") + .blocklist_item("__USE_MISC") + .blocklist_item("__USE_ATFILE") + .blocklist_item("__USE_FORTIFY_LEVEL") + .blocklist_item("__GLIBC_USE_DEPRECATED_GETS") + .blocklist_item("__GLIBC_USE_DEPRECATED_SCANF") + .blocklist_item("__GLIBC_USE_C2X_STRTOL") + .blocklist_item("_STDC_PREDEF_H") + .blocklist_item("__STDC_IEC_559__") + .blocklist_item("__STDC_IEC_60559_BFP__") + .blocklist_item("__STDC_IEC_559_COMPLEX__") + .blocklist_item("__STDC_IEC_60559_COMPLEX__") + .blocklist_item("__STDC_ISO_10646__") + .blocklist_item("__GNU_LIBRARY__") + .blocklist_item("__GLIBC__") + .blocklist_item("__GLIBC_MINOR__") + .blocklist_item("_SYS_CDEFS_H") + .blocklist_item("__glibc_c99_flexarr_available") + .blocklist_item("__LDOUBLE_REDIRECTS_TO_FLOAT128_ABI") + .blocklist_item("__HAVE_GENERIC_SELECTION") + .blocklist_item("_STDINT_H") + .blocklist_item("__GLIBC_USE_LIB_EXT2") + .blocklist_item("__GLIBC_USE_IEC_60559_BFP_EXT") + .blocklist_item("__GLIBC_USE_IEC_60559_BFP_EXT_C2X") + .blocklist_item("__GLIBC_USE_IEC_60559_EXT") + .blocklist_item("__GLIBC_USE_IEC_60559_FUNCS_EXT") + .blocklist_item("__GLIBC_USE_IEC_60559_FUNCS_EXT_C2X") + .blocklist_item("__GLIBC_USE_IEC_60559_TYPES_EXT") + .blocklist_item("_BITS_TYPES_H") + .blocklist_item("_BITS_TYPESIZES_H") + .blocklist_item("__OFF_T_MATCHES_OFF64_T") + .blocklist_item("__INO_T_MATCHES_INO64_T") + .blocklist_item("__RLIM_T_MATCHES_RLIM64_T") + .blocklist_item("__STATFS_MATCHES_STATFS64") + .blocklist_item("__FD_SETSIZE") + .blocklist_item("_BITS_TIME64_H") + .blocklist_item("_BITS_WCHAR_H") + .blocklist_item("_BITS_STDINT_INTN_H") + .blocklist_item("_BITS_STDINT_UINTN_H") + .blocklist_item("INT8_MIN") + .blocklist_item("INT16_MIN") + .blocklist_item("INT32_MIN") + .blocklist_item("INT8_MAX") + .blocklist_item("INT16_MAX") + .blocklist_item("INT32_MAX") + .blocklist_item("UINT8_MAX") + .blocklist_item("UINT16_MAX") + .blocklist_item("UINT32_MAX") + .blocklist_item("INT_LEAST8_MIN") + .blocklist_item("INT_LEAST16_MIN") + .blocklist_item("INT_LEAST32_MIN") + .blocklist_item("INT_LEAST8_MAX") + .blocklist_item("INT_LEAST16_MAX") + .blocklist_item("INT_LEAST32_MAX") + .blocklist_item("UINT_LEAST8_MAX") + .blocklist_item("UINT_LEAST16_MAX") + .blocklist_item("UINT_LEAST32_MAX") + .blocklist_item("INT_FAST8_MIN") + .blocklist_item("INT_FAST16_MIN") + .blocklist_item("INT_FAST32_MIN") + .blocklist_item("INT_FAST8_MAX") + .blocklist_item("INT_FAST16_MAX") + .blocklist_item("INT_FAST32_MAX") + .blocklist_item("UINT_FAST8_MAX") + .blocklist_item("UINT_FAST16_MAX") + .blocklist_item("UINT_FAST32_MAX") + .blocklist_item("INTPTR_MIN") + .blocklist_item("INTPTR_MAX") + .blocklist_item("UINTPTR_MAX") + .blocklist_item("PTRDIFF_MIN") + .blocklist_item("PTRDIFF_MAX") + .blocklist_item("SIG_ATOMIC_MIN") + .blocklist_item("SIG_ATOMIC_MAX") + .blocklist_item("SIZE_MAX") + .blocklist_item("WINT_MIN") + .blocklist_item("WINT_MAX") + .blocklist_item("____gwchar_t_defined") + .blocklist_item("__PRI64_PREFIX") + .blocklist_item("__PRIPTR_PREFIX") + .blocklist_item("PRId8") + .blocklist_item("PRId16") + .blocklist_item("PRId32") + .blocklist_item("PRId64") + .blocklist_item("PRIdLEAST8") + .blocklist_item("PRIdLEAST16") + .blocklist_item("PRIdLEAST32") + .blocklist_item("PRIdLEAST64") + .blocklist_item("PRIdFAST8") + .blocklist_item("PRIdFAST16") + .blocklist_item("PRIdFAST32") + .blocklist_item("PRIdFAST64") + .blocklist_item("PRIi8") + .blocklist_item("PRIi16") + .blocklist_item("PRIi32") + .blocklist_item("PRIi64") + .blocklist_item("PRIiLEAST8") + .blocklist_item("PRIiLEAST16") + .blocklist_item("PRIiLEAST32") + .blocklist_item("PRIiLEAST64") + .blocklist_item("PRIiFAST8") + .blocklist_item("PRIiFAST16") + .blocklist_item("PRIiFAST32") + .blocklist_item("PRIiFAST64") + .blocklist_item("PRIo8") + .blocklist_item("PRIo16") + .blocklist_item("PRIo32") + .blocklist_item("PRIo64") + .blocklist_item("PRIoLEAST8") + .blocklist_item("PRIoLEAST16") + .blocklist_item("PRIoLEAST32") + .blocklist_item("PRIoLEAST64") + .blocklist_item("PRIoFAST8") + .blocklist_item("PRIoFAST16") + .blocklist_item("PRIoFAST32") + .blocklist_item("PRIoFAST64") + .blocklist_item("PRIu8") + .blocklist_item("PRIu16") + .blocklist_item("PRIu32") + .blocklist_item("PRIu64") + .blocklist_item("PRIuLEAST8") + .blocklist_item("PRIuLEAST16") + .blocklist_item("PRIuLEAST32") + .blocklist_item("PRIuLEAST64") + .blocklist_item("PRIuFAST8") + .blocklist_item("PRIuFAST16") + .blocklist_item("PRIuFAST32") + .blocklist_item("PRIuFAST64") + .blocklist_item("PRIx8") + .blocklist_item("PRIx16") + .blocklist_item("PRIx32") + .blocklist_item("PRIx64") + .blocklist_item("PRIxLEAST8") + .blocklist_item("PRIxLEAST16") + .blocklist_item("PRIxLEAST32") + .blocklist_item("PRIxLEAST64") + .blocklist_item("PRIxFAST8") + .blocklist_item("PRIxFAST16") + .blocklist_item("PRIxFAST32") + .blocklist_item("PRIxFAST64") + .blocklist_item("PRIX8") + .blocklist_item("PRIX16") + .blocklist_item("PRIX32") + .blocklist_item("PRIX64") + .blocklist_item("PRIXLEAST8") + .blocklist_item("PRIXLEAST16") + .blocklist_item("PRIXLEAST32") + .blocklist_item("PRIXLEAST64") + .blocklist_item("PRIXFAST8") + .blocklist_item("PRIXFAST16") + .blocklist_item("PRIXFAST32") + .blocklist_item("PRIXFAST64") + .blocklist_item("PRIdMAX") + .blocklist_item("PRIiMAX") + .blocklist_item("PRIoMAX") + .blocklist_item("PRIuMAX") + .blocklist_item("PRIxMAX") + .blocklist_item("PRIXMAX") + .blocklist_item("PRIdPTR") + .blocklist_item("PRIiPTR") + .blocklist_item("PRIoPTR") + .blocklist_item("PRIuPTR") + .blocklist_item("PRIxPTR") + .blocklist_item("PRIXPTR") + .blocklist_item("SCNd8") + .blocklist_item("SCNd16") + .blocklist_item("SCNd32") + .blocklist_item("SCNd64") + .blocklist_item("SCNdLEAST8") + .blocklist_item("SCNdLEAST16") + .blocklist_item("SCNdLEAST32") + .blocklist_item("SCNdLEAST64") + .blocklist_item("SCNdFAST8") + .blocklist_item("SCNdFAST16") + .blocklist_item("SCNdFAST32") + .blocklist_item("SCNdFAST64") + .blocklist_item("SCNi8") + .blocklist_item("SCNi16") + .blocklist_item("SCNi32") + .blocklist_item("SCNi64") + .blocklist_item("SCNiLEAST8") + .blocklist_item("SCNiLEAST16") + .blocklist_item("SCNiLEAST32") + .blocklist_item("SCNiLEAST64") + .blocklist_item("SCNiFAST8") + .blocklist_item("SCNiFAST16") + .blocklist_item("SCNiFAST32") + .blocklist_item("SCNiFAST64") + .blocklist_item("SCNu8") + .blocklist_item("SCNu16") + .blocklist_item("SCNu32") + .blocklist_item("SCNu64") + .blocklist_item("SCNuLEAST8") + .blocklist_item("SCNuLEAST16") + .blocklist_item("SCNuLEAST32") + .blocklist_item("SCNuLEAST64") + .blocklist_item("SCNuFAST8") + .blocklist_item("SCNuFAST16") + .blocklist_item("SCNuFAST32") + .blocklist_item("SCNuFAST64") + .blocklist_item("SCNo8") + .blocklist_item("SCNo16") + .blocklist_item("SCNo32") + .blocklist_item("SCNo64") + .blocklist_item("SCNoLEAST8") + .blocklist_item("SCNoLEAST16") + .blocklist_item("SCNoLEAST32") + .blocklist_item("SCNoLEAST64") + .blocklist_item("SCNoFAST8") + .blocklist_item("SCNoFAST16") + .blocklist_item("SCNoFAST32") + .blocklist_item("SCNoFAST64") + .blocklist_item("SCNx8") + .blocklist_item("SCNx16") + .blocklist_item("SCNx32") + .blocklist_item("SCNx64") + .blocklist_item("SCNxLEAST8") + .blocklist_item("SCNxLEAST16") + .blocklist_item("SCNxLEAST32") + .blocklist_item("SCNxLEAST64") + .blocklist_item("SCNxFAST8") + .blocklist_item("SCNxFAST16") + .blocklist_item("SCNxFAST32") + .blocklist_item("SCNxFAST64") + .blocklist_item("SCNdMAX") + .blocklist_item("SCNiMAX") + .blocklist_item("SCNoMAX") + .blocklist_item("SCNuMAX") + .blocklist_item("SCNxMAX") + .blocklist_item("SCNdPTR") + .blocklist_item("SCNiPTR") + .blocklist_item("SCNoPTR") + .blocklist_item("SCNuPTR") + .blocklist_item("SCNxPTR") + .blocklist_item("__bool_true_false_are_defined") + .blocklist_item("true_") + .blocklist_item("false_") + .blocklist_item("__u_char") + .blocklist_item("__u_short") + .blocklist_item("__u_int") + .blocklist_item("__u_long") + .blocklist_item("__int8_t") + .blocklist_item("__uint8_t") + .blocklist_item("__int16_t") + .blocklist_item("__uint16_t") + .blocklist_item("__int32_t") + .blocklist_item("__uint32_t") + .blocklist_item("__int64_t") + .blocklist_item("__uint64_t") + .blocklist_item("__int_least8_t") + .blocklist_item("__uint_least8_t") + .blocklist_item("__int_least16_t") + .blocklist_item("__uint_least16_t") + .blocklist_item("__int_least32_t") + .blocklist_item("__uint_least32_t") + .blocklist_item("__int_least64_t") + .blocklist_item("__uint_least64_t") + .blocklist_item("__quad_t") + .blocklist_item("__u_quad_t") + .blocklist_item("__intmax_t") + .blocklist_item("__uintmax_t") + .blocklist_item("__dev_t") + .blocklist_item("__uid_t") + .blocklist_item("__gid_t") + .blocklist_item("__ino_t") + .blocklist_item("__ino64_t") + .blocklist_item("__mode_t") + .blocklist_item("__nlink_t") + .blocklist_item("__off_t") + .blocklist_item("__off64_t") + .blocklist_item("__pid_t") + .blocklist_item("__fsid_t") + .blocklist_item("__clock_t") + .blocklist_item("__rlim_t") + .blocklist_item("__rlim64_t") + .blocklist_item("__id_t") + .blocklist_item("__time_t") + .blocklist_item("__useconds_t") + .blocklist_item("__suseconds_t") + .blocklist_item("__suseconds64_t") + .blocklist_item("__daddr_t") + .blocklist_item("__key_t") + .blocklist_item("__clockid_t") + .blocklist_item("__timer_t") + .blocklist_item("__blksize_t") + .blocklist_item("__blkcnt_t") + .blocklist_item("__blkcnt64_t") + .blocklist_item("__fsblkcnt_t") + .blocklist_item("__fsblkcnt64_t") + .blocklist_item("__fsfilcnt_t") + .blocklist_item("__fsfilcnt64_t") + .blocklist_item("__fsword_t") + .blocklist_item("__ssize_t") + .blocklist_item("__syscall_slong_t") + .blocklist_item("__syscall_ulong_t") + .blocklist_item("__loff_t") + .blocklist_item("__caddr_t") + .blocklist_item("__intptr_t") + .blocklist_item("__socklen_t") + .blocklist_item("__sig_atomic_t") + .blocklist_item("int_least8_t") + .blocklist_item("int_least16_t") + .blocklist_item("int_least32_t") + .blocklist_item("int_least64_t") + .blocklist_item("uint_least8_t") + .blocklist_item("uint_least16_t") + .blocklist_item("uint_least32_t") + .blocklist_item("uint_least64_t") + .blocklist_item("int_fast8_t") + .blocklist_item("int_fast16_t") + .blocklist_item("int_fast32_t") + .blocklist_item("int_fast64_t") + .blocklist_item("uint_fast8_t") + .blocklist_item("uint_fast16_t") + .blocklist_item("uint_fast32_t") + .blocklist_item("uint_fast64_t") + .blocklist_item("intmax_t") + .blocklist_item("uintmax_t") + .blocklist_item("__gwchar_t") + .blocklist_item("imaxdiv_t") + .blocklist_item("imaxabs") + .blocklist_item("imaxdiv") + .blocklist_item("strtoimax") + .blocklist_item("strtoumax") + .blocklist_item("wcstoimax") + .blocklist_item("wcstoumax") + .blocklist_item("max_align_t") + .blocklist_item("wchar_t") .generate()?; rust_bindings.write_to_file(destination)?; @@ -101,15 +462,17 @@ fn generate_bindings(qemu_plugin_header: &Path, destination: &Path) -> Result<() } fn main() -> Result<()> { - let metadata = MetadataCommand::new() - .no_deps() - .exec()?; + let metadata = MetadataCommand::new().no_deps().exec()?; - let package = metadata.packages.iter() + let package = metadata + .packages + .iter() .find(|p| p.name == "qemu-plugin-sys") .ok_or_else(|| anyhow!("Failed to find package"))?; - let out_dir = package.manifest_path.parent() + let out_dir = package + .manifest_path + .parent() .ok_or_else(|| anyhow!("Failed to get manifest path"))? .join("src") .into_std_path_buf(); @@ -126,24 +489,21 @@ fn main() -> Result<()> { let src_dir = tmp_dir.join(format!("qemu-{}", QEMU_VERSION)); if !src_archive.exists() { - download( - &qemu_src_url(), - &src_archive, - )?; + download(&qemu_src_url(), &src_archive)?; } if !src_dir.exists() { - extract_txz( - &src_archive, - &src_dir, - )?; + extract_txz(&src_archive, &src_dir)?; } + #[cfg(windows)] + generate_windows_delaylink_library( + &src_dir.join("plugins").join("qemu-plugins.symbols"), + &out_dir, + )?; + generate_bindings( - &src_dir - .join("include") - .join("qemu") - .join("qemu-plugin.h"), + &src_dir.join("include").join("qemu").join("qemu-plugin.h"), &out_dir.join("bindings.rs"), )?; diff --git a/qemu-plugin-sys/src/bindings.rs b/qemu-plugin-sys/src/bindings.rs index 0266839..139b51e 100644 --- a/qemu-plugin-sys/src/bindings.rs +++ b/qemu-plugin-sys/src/bindings.rs @@ -1,511 +1,6 @@ /* automatically generated by rust-bindgen 0.69.1 */ -pub const _INTTYPES_H: u32 = 1; -pub const _FEATURES_H: u32 = 1; -pub const _DEFAULT_SOURCE: u32 = 1; -pub const __GLIBC_USE_ISOC2X: u32 = 0; -pub const __USE_ISOC11: u32 = 1; -pub const __USE_ISOC99: u32 = 1; -pub const __USE_ISOC95: u32 = 1; -pub const __USE_POSIX_IMPLICITLY: u32 = 1; -pub const _POSIX_SOURCE: u32 = 1; -pub const _POSIX_C_SOURCE: u32 = 200809; -pub const __USE_POSIX: u32 = 1; -pub const __USE_POSIX2: u32 = 1; -pub const __USE_POSIX199309: u32 = 1; -pub const __USE_POSIX199506: u32 = 1; -pub const __USE_XOPEN2K: u32 = 1; -pub const __USE_XOPEN2K8: u32 = 1; -pub const _ATFILE_SOURCE: u32 = 1; -pub const __WORDSIZE: u32 = 64; -pub const __WORDSIZE_TIME64_COMPAT32: u32 = 0; -pub const __TIMESIZE: u32 = 64; -pub const __USE_MISC: u32 = 1; -pub const __USE_ATFILE: u32 = 1; -pub const __USE_FORTIFY_LEVEL: u32 = 0; -pub const __GLIBC_USE_DEPRECATED_GETS: u32 = 0; -pub const __GLIBC_USE_DEPRECATED_SCANF: u32 = 0; -pub const __GLIBC_USE_C2X_STRTOL: u32 = 0; -pub const _STDC_PREDEF_H: u32 = 1; -pub const __STDC_IEC_559__: u32 = 1; -pub const __STDC_IEC_60559_BFP__: u32 = 201404; -pub const __STDC_IEC_559_COMPLEX__: u32 = 1; -pub const __STDC_IEC_60559_COMPLEX__: u32 = 201404; -pub const __STDC_ISO_10646__: u32 = 201706; -pub const __GNU_LIBRARY__: u32 = 6; -pub const __GLIBC__: u32 = 2; -pub const __GLIBC_MINOR__: u32 = 38; -pub const _SYS_CDEFS_H: u32 = 1; -pub const __glibc_c99_flexarr_available: u32 = 1; -pub const __LDOUBLE_REDIRECTS_TO_FLOAT128_ABI: u32 = 0; -pub const __HAVE_GENERIC_SELECTION: u32 = 1; -pub const _STDINT_H: u32 = 1; -pub const __GLIBC_USE_LIB_EXT2: u32 = 0; -pub const __GLIBC_USE_IEC_60559_BFP_EXT: u32 = 0; -pub const __GLIBC_USE_IEC_60559_BFP_EXT_C2X: u32 = 0; -pub const __GLIBC_USE_IEC_60559_EXT: u32 = 0; -pub const __GLIBC_USE_IEC_60559_FUNCS_EXT: u32 = 0; -pub const __GLIBC_USE_IEC_60559_FUNCS_EXT_C2X: u32 = 0; -pub const __GLIBC_USE_IEC_60559_TYPES_EXT: u32 = 0; -pub const _BITS_TYPES_H: u32 = 1; -pub const _BITS_TYPESIZES_H: u32 = 1; -pub const __OFF_T_MATCHES_OFF64_T: u32 = 1; -pub const __INO_T_MATCHES_INO64_T: u32 = 1; -pub const __RLIM_T_MATCHES_RLIM64_T: u32 = 1; -pub const __STATFS_MATCHES_STATFS64: u32 = 1; -pub const __FD_SETSIZE: u32 = 1024; -pub const _BITS_TIME64_H: u32 = 1; -pub const _BITS_WCHAR_H: u32 = 1; -pub const _BITS_STDINT_INTN_H: u32 = 1; -pub const _BITS_STDINT_UINTN_H: u32 = 1; -pub const INT8_MIN: i32 = -128; -pub const INT16_MIN: i32 = -32768; -pub const INT32_MIN: i32 = -2147483648; -pub const INT8_MAX: u32 = 127; -pub const INT16_MAX: u32 = 32767; -pub const INT32_MAX: u32 = 2147483647; -pub const UINT8_MAX: u32 = 255; -pub const UINT16_MAX: u32 = 65535; -pub const UINT32_MAX: u32 = 4294967295; -pub const INT_LEAST8_MIN: i32 = -128; -pub const INT_LEAST16_MIN: i32 = -32768; -pub const INT_LEAST32_MIN: i32 = -2147483648; -pub const INT_LEAST8_MAX: u32 = 127; -pub const INT_LEAST16_MAX: u32 = 32767; -pub const INT_LEAST32_MAX: u32 = 2147483647; -pub const UINT_LEAST8_MAX: u32 = 255; -pub const UINT_LEAST16_MAX: u32 = 65535; -pub const UINT_LEAST32_MAX: u32 = 4294967295; -pub const INT_FAST8_MIN: i32 = -128; -pub const INT_FAST16_MIN: i64 = -9223372036854775808; -pub const INT_FAST32_MIN: i64 = -9223372036854775808; -pub const INT_FAST8_MAX: u32 = 127; -pub const INT_FAST16_MAX: u64 = 9223372036854775807; -pub const INT_FAST32_MAX: u64 = 9223372036854775807; -pub const UINT_FAST8_MAX: u32 = 255; -pub const UINT_FAST16_MAX: i32 = -1; -pub const UINT_FAST32_MAX: i32 = -1; -pub const INTPTR_MIN: i64 = -9223372036854775808; -pub const INTPTR_MAX: u64 = 9223372036854775807; -pub const UINTPTR_MAX: i32 = -1; -pub const PTRDIFF_MIN: i64 = -9223372036854775808; -pub const PTRDIFF_MAX: u64 = 9223372036854775807; -pub const SIG_ATOMIC_MIN: i32 = -2147483648; -pub const SIG_ATOMIC_MAX: u32 = 2147483647; -pub const SIZE_MAX: i32 = -1; -pub const WINT_MIN: u32 = 0; -pub const WINT_MAX: u32 = 4294967295; -pub const ____gwchar_t_defined: u32 = 1; -pub const __PRI64_PREFIX: &[u8; 2] = b"l\0"; -pub const __PRIPTR_PREFIX: &[u8; 2] = b"l\0"; -pub const PRId8: &[u8; 2] = b"d\0"; -pub const PRId16: &[u8; 2] = b"d\0"; -pub const PRId32: &[u8; 2] = b"d\0"; -pub const PRId64: &[u8; 3] = b"ld\0"; -pub const PRIdLEAST8: &[u8; 2] = b"d\0"; -pub const PRIdLEAST16: &[u8; 2] = b"d\0"; -pub const PRIdLEAST32: &[u8; 2] = b"d\0"; -pub const PRIdLEAST64: &[u8; 3] = b"ld\0"; -pub const PRIdFAST8: &[u8; 2] = b"d\0"; -pub const PRIdFAST16: &[u8; 3] = b"ld\0"; -pub const PRIdFAST32: &[u8; 3] = b"ld\0"; -pub const PRIdFAST64: &[u8; 3] = b"ld\0"; -pub const PRIi8: &[u8; 2] = b"i\0"; -pub const PRIi16: &[u8; 2] = b"i\0"; -pub const PRIi32: &[u8; 2] = b"i\0"; -pub const PRIi64: &[u8; 3] = b"li\0"; -pub const PRIiLEAST8: &[u8; 2] = b"i\0"; -pub const PRIiLEAST16: &[u8; 2] = b"i\0"; -pub const PRIiLEAST32: &[u8; 2] = b"i\0"; -pub const PRIiLEAST64: &[u8; 3] = b"li\0"; -pub const PRIiFAST8: &[u8; 2] = b"i\0"; -pub const PRIiFAST16: &[u8; 3] = b"li\0"; -pub const PRIiFAST32: &[u8; 3] = b"li\0"; -pub const PRIiFAST64: &[u8; 3] = b"li\0"; -pub const PRIo8: &[u8; 2] = b"o\0"; -pub const PRIo16: &[u8; 2] = b"o\0"; -pub const PRIo32: &[u8; 2] = b"o\0"; -pub const PRIo64: &[u8; 3] = b"lo\0"; -pub const PRIoLEAST8: &[u8; 2] = b"o\0"; -pub const PRIoLEAST16: &[u8; 2] = b"o\0"; -pub const PRIoLEAST32: &[u8; 2] = b"o\0"; -pub const PRIoLEAST64: &[u8; 3] = b"lo\0"; -pub const PRIoFAST8: &[u8; 2] = b"o\0"; -pub const PRIoFAST16: &[u8; 3] = b"lo\0"; -pub const PRIoFAST32: &[u8; 3] = b"lo\0"; -pub const PRIoFAST64: &[u8; 3] = b"lo\0"; -pub const PRIu8: &[u8; 2] = b"u\0"; -pub const PRIu16: &[u8; 2] = b"u\0"; -pub const PRIu32: &[u8; 2] = b"u\0"; -pub const PRIu64: &[u8; 3] = b"lu\0"; -pub const PRIuLEAST8: &[u8; 2] = b"u\0"; -pub const PRIuLEAST16: &[u8; 2] = b"u\0"; -pub const PRIuLEAST32: &[u8; 2] = b"u\0"; -pub const PRIuLEAST64: &[u8; 3] = b"lu\0"; -pub const PRIuFAST8: &[u8; 2] = b"u\0"; -pub const PRIuFAST16: &[u8; 3] = b"lu\0"; -pub const PRIuFAST32: &[u8; 3] = b"lu\0"; -pub const PRIuFAST64: &[u8; 3] = b"lu\0"; -pub const PRIx8: &[u8; 2] = b"x\0"; -pub const PRIx16: &[u8; 2] = b"x\0"; -pub const PRIx32: &[u8; 2] = b"x\0"; -pub const PRIx64: &[u8; 3] = b"lx\0"; -pub const PRIxLEAST8: &[u8; 2] = b"x\0"; -pub const PRIxLEAST16: &[u8; 2] = b"x\0"; -pub const PRIxLEAST32: &[u8; 2] = b"x\0"; -pub const PRIxLEAST64: &[u8; 3] = b"lx\0"; -pub const PRIxFAST8: &[u8; 2] = b"x\0"; -pub const PRIxFAST16: &[u8; 3] = b"lx\0"; -pub const PRIxFAST32: &[u8; 3] = b"lx\0"; -pub const PRIxFAST64: &[u8; 3] = b"lx\0"; -pub const PRIX8: &[u8; 2] = b"X\0"; -pub const PRIX16: &[u8; 2] = b"X\0"; -pub const PRIX32: &[u8; 2] = b"X\0"; -pub const PRIX64: &[u8; 3] = b"lX\0"; -pub const PRIXLEAST8: &[u8; 2] = b"X\0"; -pub const PRIXLEAST16: &[u8; 2] = b"X\0"; -pub const PRIXLEAST32: &[u8; 2] = b"X\0"; -pub const PRIXLEAST64: &[u8; 3] = b"lX\0"; -pub const PRIXFAST8: &[u8; 2] = b"X\0"; -pub const PRIXFAST16: &[u8; 3] = b"lX\0"; -pub const PRIXFAST32: &[u8; 3] = b"lX\0"; -pub const PRIXFAST64: &[u8; 3] = b"lX\0"; -pub const PRIdMAX: &[u8; 3] = b"ld\0"; -pub const PRIiMAX: &[u8; 3] = b"li\0"; -pub const PRIoMAX: &[u8; 3] = b"lo\0"; -pub const PRIuMAX: &[u8; 3] = b"lu\0"; -pub const PRIxMAX: &[u8; 3] = b"lx\0"; -pub const PRIXMAX: &[u8; 3] = b"lX\0"; -pub const PRIdPTR: &[u8; 3] = b"ld\0"; -pub const PRIiPTR: &[u8; 3] = b"li\0"; -pub const PRIoPTR: &[u8; 3] = b"lo\0"; -pub const PRIuPTR: &[u8; 3] = b"lu\0"; -pub const PRIxPTR: &[u8; 3] = b"lx\0"; -pub const PRIXPTR: &[u8; 3] = b"lX\0"; -pub const SCNd8: &[u8; 4] = b"hhd\0"; -pub const SCNd16: &[u8; 3] = b"hd\0"; -pub const SCNd32: &[u8; 2] = b"d\0"; -pub const SCNd64: &[u8; 3] = b"ld\0"; -pub const SCNdLEAST8: &[u8; 4] = b"hhd\0"; -pub const SCNdLEAST16: &[u8; 3] = b"hd\0"; -pub const SCNdLEAST32: &[u8; 2] = b"d\0"; -pub const SCNdLEAST64: &[u8; 3] = b"ld\0"; -pub const SCNdFAST8: &[u8; 4] = b"hhd\0"; -pub const SCNdFAST16: &[u8; 3] = b"ld\0"; -pub const SCNdFAST32: &[u8; 3] = b"ld\0"; -pub const SCNdFAST64: &[u8; 3] = b"ld\0"; -pub const SCNi8: &[u8; 4] = b"hhi\0"; -pub const SCNi16: &[u8; 3] = b"hi\0"; -pub const SCNi32: &[u8; 2] = b"i\0"; -pub const SCNi64: &[u8; 3] = b"li\0"; -pub const SCNiLEAST8: &[u8; 4] = b"hhi\0"; -pub const SCNiLEAST16: &[u8; 3] = b"hi\0"; -pub const SCNiLEAST32: &[u8; 2] = b"i\0"; -pub const SCNiLEAST64: &[u8; 3] = b"li\0"; -pub const SCNiFAST8: &[u8; 4] = b"hhi\0"; -pub const SCNiFAST16: &[u8; 3] = b"li\0"; -pub const SCNiFAST32: &[u8; 3] = b"li\0"; -pub const SCNiFAST64: &[u8; 3] = b"li\0"; -pub const SCNu8: &[u8; 4] = b"hhu\0"; -pub const SCNu16: &[u8; 3] = b"hu\0"; -pub const SCNu32: &[u8; 2] = b"u\0"; -pub const SCNu64: &[u8; 3] = b"lu\0"; -pub const SCNuLEAST8: &[u8; 4] = b"hhu\0"; -pub const SCNuLEAST16: &[u8; 3] = b"hu\0"; -pub const SCNuLEAST32: &[u8; 2] = b"u\0"; -pub const SCNuLEAST64: &[u8; 3] = b"lu\0"; -pub const SCNuFAST8: &[u8; 4] = b"hhu\0"; -pub const SCNuFAST16: &[u8; 3] = b"lu\0"; -pub const SCNuFAST32: &[u8; 3] = b"lu\0"; -pub const SCNuFAST64: &[u8; 3] = b"lu\0"; -pub const SCNo8: &[u8; 4] = b"hho\0"; -pub const SCNo16: &[u8; 3] = b"ho\0"; -pub const SCNo32: &[u8; 2] = b"o\0"; -pub const SCNo64: &[u8; 3] = b"lo\0"; -pub const SCNoLEAST8: &[u8; 4] = b"hho\0"; -pub const SCNoLEAST16: &[u8; 3] = b"ho\0"; -pub const SCNoLEAST32: &[u8; 2] = b"o\0"; -pub const SCNoLEAST64: &[u8; 3] = b"lo\0"; -pub const SCNoFAST8: &[u8; 4] = b"hho\0"; -pub const SCNoFAST16: &[u8; 3] = b"lo\0"; -pub const SCNoFAST32: &[u8; 3] = b"lo\0"; -pub const SCNoFAST64: &[u8; 3] = b"lo\0"; -pub const SCNx8: &[u8; 4] = b"hhx\0"; -pub const SCNx16: &[u8; 3] = b"hx\0"; -pub const SCNx32: &[u8; 2] = b"x\0"; -pub const SCNx64: &[u8; 3] = b"lx\0"; -pub const SCNxLEAST8: &[u8; 4] = b"hhx\0"; -pub const SCNxLEAST16: &[u8; 3] = b"hx\0"; -pub const SCNxLEAST32: &[u8; 2] = b"x\0"; -pub const SCNxLEAST64: &[u8; 3] = b"lx\0"; -pub const SCNxFAST8: &[u8; 4] = b"hhx\0"; -pub const SCNxFAST16: &[u8; 3] = b"lx\0"; -pub const SCNxFAST32: &[u8; 3] = b"lx\0"; -pub const SCNxFAST64: &[u8; 3] = b"lx\0"; -pub const SCNdMAX: &[u8; 3] = b"ld\0"; -pub const SCNiMAX: &[u8; 3] = b"li\0"; -pub const SCNoMAX: &[u8; 3] = b"lo\0"; -pub const SCNuMAX: &[u8; 3] = b"lu\0"; -pub const SCNxMAX: &[u8; 3] = b"lx\0"; -pub const SCNdPTR: &[u8; 3] = b"ld\0"; -pub const SCNiPTR: &[u8; 3] = b"li\0"; -pub const SCNoPTR: &[u8; 3] = b"lo\0"; -pub const SCNuPTR: &[u8; 3] = b"lu\0"; -pub const SCNxPTR: &[u8; 3] = b"lx\0"; -pub const __bool_true_false_are_defined: u32 = 1; -pub const true_: u32 = 1; -pub const false_: u32 = 0; pub const QEMU_PLUGIN_VERSION: u32 = 1; -#[doc = " Convenience types."] -pub type __u_char = ::std::os::raw::c_uchar; -pub type __u_short = ::std::os::raw::c_ushort; -pub type __u_int = ::std::os::raw::c_uint; -pub type __u_long = ::std::os::raw::c_ulong; -#[doc = " Fixed-size types, underlying types depend on word size and compiler."] -pub type __int8_t = ::std::os::raw::c_schar; -pub type __uint8_t = ::std::os::raw::c_uchar; -pub type __int16_t = ::std::os::raw::c_short; -pub type __uint16_t = ::std::os::raw::c_ushort; -pub type __int32_t = ::std::os::raw::c_int; -pub type __uint32_t = ::std::os::raw::c_uint; -pub type __int64_t = ::std::os::raw::c_long; -pub type __uint64_t = ::std::os::raw::c_ulong; -#[doc = " Smallest types with at least a given width."] -pub type __int_least8_t = __int8_t; -pub type __uint_least8_t = __uint8_t; -pub type __int_least16_t = __int16_t; -pub type __uint_least16_t = __uint16_t; -pub type __int_least32_t = __int32_t; -pub type __uint_least32_t = __uint32_t; -pub type __int_least64_t = __int64_t; -pub type __uint_least64_t = __uint64_t; -pub type __quad_t = ::std::os::raw::c_long; -pub type __u_quad_t = ::std::os::raw::c_ulong; -pub type __intmax_t = ::std::os::raw::c_long; -pub type __uintmax_t = ::std::os::raw::c_ulong; -pub type __dev_t = ::std::os::raw::c_ulong; -pub type __uid_t = ::std::os::raw::c_uint; -pub type __gid_t = ::std::os::raw::c_uint; -pub type __ino_t = ::std::os::raw::c_ulong; -pub type __ino64_t = ::std::os::raw::c_ulong; -pub type __mode_t = ::std::os::raw::c_uint; -pub type __nlink_t = ::std::os::raw::c_uint; -pub type __off_t = ::std::os::raw::c_long; -pub type __off64_t = ::std::os::raw::c_long; -pub type __pid_t = ::std::os::raw::c_int; -#[repr(C)] -#[derive(Debug, Default, Copy, Clone, Hash, PartialOrd, Ord, PartialEq, Eq)] -pub struct __fsid_t { - pub __val: [::std::os::raw::c_int; 2usize], -} -#[test] -fn bindgen_test_layout___fsid_t() { - const UNINIT: ::std::mem::MaybeUninit<__fsid_t> = ::std::mem::MaybeUninit::uninit(); - let ptr = UNINIT.as_ptr(); - assert_eq!( - ::std::mem::size_of::<__fsid_t>(), - 8usize, - concat!("Size of: ", stringify!(__fsid_t)) - ); - assert_eq!( - ::std::mem::align_of::<__fsid_t>(), - 4usize, - concat!("Alignment of ", stringify!(__fsid_t)) - ); - assert_eq!( - unsafe { ::std::ptr::addr_of!((*ptr).__val) as usize - ptr as usize }, - 0usize, - concat!( - "Offset of field: ", - stringify!(__fsid_t), - "::", - stringify!(__val) - ) - ); -} -pub type __clock_t = ::std::os::raw::c_long; -pub type __rlim_t = ::std::os::raw::c_ulong; -pub type __rlim64_t = ::std::os::raw::c_ulong; -pub type __id_t = ::std::os::raw::c_uint; -pub type __time_t = ::std::os::raw::c_long; -pub type __useconds_t = ::std::os::raw::c_uint; -pub type __suseconds_t = ::std::os::raw::c_long; -pub type __suseconds64_t = ::std::os::raw::c_long; -pub type __daddr_t = ::std::os::raw::c_int; -pub type __key_t = ::std::os::raw::c_int; -pub type __clockid_t = ::std::os::raw::c_int; -pub type __timer_t = *mut ::std::os::raw::c_void; -pub type __blksize_t = ::std::os::raw::c_int; -pub type __blkcnt_t = ::std::os::raw::c_long; -pub type __blkcnt64_t = ::std::os::raw::c_long; -pub type __fsblkcnt_t = ::std::os::raw::c_ulong; -pub type __fsblkcnt64_t = ::std::os::raw::c_ulong; -pub type __fsfilcnt_t = ::std::os::raw::c_ulong; -pub type __fsfilcnt64_t = ::std::os::raw::c_ulong; -pub type __fsword_t = ::std::os::raw::c_long; -pub type __ssize_t = ::std::os::raw::c_long; -pub type __syscall_slong_t = ::std::os::raw::c_long; -pub type __syscall_ulong_t = ::std::os::raw::c_ulong; -#[doc = " These few don't really vary by system, they always correspond\nto one of the other defined types."] -pub type __loff_t = __off64_t; -pub type __caddr_t = *mut ::std::os::raw::c_char; -pub type __intptr_t = ::std::os::raw::c_long; -pub type __socklen_t = ::std::os::raw::c_uint; -#[doc = " C99: An integer type that can be accessed as an atomic entity,\neven in the presence of asynchronous interrupts.\nIt is not currently necessary for this to be machine-specific."] -pub type __sig_atomic_t = ::std::os::raw::c_int; -#[doc = " Signed."] -pub type int_least8_t = __int_least8_t; -pub type int_least16_t = __int_least16_t; -pub type int_least32_t = __int_least32_t; -pub type int_least64_t = __int_least64_t; -#[doc = " Unsigned."] -pub type uint_least8_t = __uint_least8_t; -pub type uint_least16_t = __uint_least16_t; -pub type uint_least32_t = __uint_least32_t; -pub type uint_least64_t = __uint_least64_t; -#[doc = " Signed."] -pub type int_fast8_t = ::std::os::raw::c_schar; -pub type int_fast16_t = ::std::os::raw::c_long; -pub type int_fast32_t = ::std::os::raw::c_long; -pub type int_fast64_t = ::std::os::raw::c_long; -#[doc = " Unsigned."] -pub type uint_fast8_t = ::std::os::raw::c_uchar; -pub type uint_fast16_t = ::std::os::raw::c_ulong; -pub type uint_fast32_t = ::std::os::raw::c_ulong; -pub type uint_fast64_t = ::std::os::raw::c_ulong; -#[doc = " Largest integral types."] -pub type intmax_t = __intmax_t; -pub type uintmax_t = __uintmax_t; -pub type __gwchar_t = ::std::os::raw::c_uint; -#[doc = " We have to define the `uintmax_t' type using `ldiv_t'."] -#[repr(C)] -#[derive(Debug, Default, Copy, Clone, Hash, PartialOrd, Ord, PartialEq, Eq)] -pub struct imaxdiv_t { - #[doc = " Quotient."] - pub quot: ::std::os::raw::c_long, - #[doc = " Remainder."] - pub rem: ::std::os::raw::c_long, -} -#[test] -fn bindgen_test_layout_imaxdiv_t() { - const UNINIT: ::std::mem::MaybeUninit = ::std::mem::MaybeUninit::uninit(); - let ptr = UNINIT.as_ptr(); - assert_eq!( - ::std::mem::size_of::(), - 16usize, - concat!("Size of: ", stringify!(imaxdiv_t)) - ); - assert_eq!( - ::std::mem::align_of::(), - 8usize, - concat!("Alignment of ", stringify!(imaxdiv_t)) - ); - assert_eq!( - unsafe { ::std::ptr::addr_of!((*ptr).quot) as usize - ptr as usize }, - 0usize, - concat!( - "Offset of field: ", - stringify!(imaxdiv_t), - "::", - stringify!(quot) - ) - ); - assert_eq!( - unsafe { ::std::ptr::addr_of!((*ptr).rem) as usize - ptr as usize }, - 8usize, - concat!( - "Offset of field: ", - stringify!(imaxdiv_t), - "::", - stringify!(rem) - ) - ); -} -extern "C" { - #[doc = " Compute absolute value of N."] - pub fn imaxabs(__n: intmax_t) -> intmax_t; -} -extern "C" { - #[doc = " Return the `imaxdiv_t' representation of the value of NUMER over DENOM."] - pub fn imaxdiv(__numer: intmax_t, __denom: intmax_t) -> imaxdiv_t; -} -extern "C" { - #[doc = " Like `strtol' but convert to `intmax_t'."] - pub fn strtoimax( - __nptr: *const ::std::os::raw::c_char, - __endptr: *mut *mut ::std::os::raw::c_char, - __base: ::std::os::raw::c_int, - ) -> intmax_t; -} -extern "C" { - #[doc = " Like `strtoul' but convert to `uintmax_t'."] - pub fn strtoumax( - __nptr: *const ::std::os::raw::c_char, - __endptr: *mut *mut ::std::os::raw::c_char, - __base: ::std::os::raw::c_int, - ) -> uintmax_t; -} -extern "C" { - #[doc = " Like `wcstol' but convert to `intmax_t'."] - pub fn wcstoimax( - __nptr: *const __gwchar_t, - __endptr: *mut *mut __gwchar_t, - __base: ::std::os::raw::c_int, - ) -> intmax_t; -} -extern "C" { - #[doc = " Like `wcstoul' but convert to `uintmax_t'."] - pub fn wcstoumax( - __nptr: *const __gwchar_t, - __endptr: *mut *mut __gwchar_t, - __base: ::std::os::raw::c_int, - ) -> uintmax_t; -} -pub type wchar_t = ::std::os::raw::c_uint; -#[doc = " Define 'max_align_t' to match the GCC definition."] -#[repr(C)] -#[repr(align(16))] -#[derive(Debug, Default, Copy, Clone, PartialOrd, PartialEq)] -pub struct max_align_t { - pub __clang_max_align_nonce1: ::std::os::raw::c_longlong, - pub __bindgen_padding_0: u64, - pub __clang_max_align_nonce2: u128, -} -#[test] -fn bindgen_test_layout_max_align_t() { - const UNINIT: ::std::mem::MaybeUninit = ::std::mem::MaybeUninit::uninit(); - let ptr = UNINIT.as_ptr(); - assert_eq!( - ::std::mem::size_of::(), - 32usize, - concat!("Size of: ", stringify!(max_align_t)) - ); - assert_eq!( - ::std::mem::align_of::(), - 16usize, - concat!("Alignment of ", stringify!(max_align_t)) - ); - assert_eq!( - unsafe { ::std::ptr::addr_of!((*ptr).__clang_max_align_nonce1) as usize - ptr as usize }, - 0usize, - concat!( - "Offset of field: ", - stringify!(max_align_t), - "::", - stringify!(__clang_max_align_nonce1) - ) - ); - assert_eq!( - unsafe { ::std::ptr::addr_of!((*ptr).__clang_max_align_nonce2) as usize - ptr as usize }, - 16usize, - concat!( - "Offset of field: ", - stringify!(max_align_t), - "::", - stringify!(__clang_max_align_nonce2) - ) - ); -} #[doc = " typedef qemu_plugin_id_t - Unique plugin ID"] pub type qemu_plugin_id_t = u64; #[doc = " struct qemu_info_t - system information for plugins\n\n This structure provides for some limited information about the\n system to allow the plugin to make decisions on how to proceed. For\n example it might only be suitable for running on some guest\n architectures or when under full system emulation."] diff --git a/qemu-plugin-sys/src/qemu_plugin_api.def b/qemu-plugin-sys/src/qemu_plugin_api.def new file mode 100644 index 0000000..b13f0ea --- /dev/null +++ b/qemu-plugin-sys/src/qemu_plugin_api.def @@ -0,0 +1,46 @@ +EXPORTS + + qemu_plugin_bool_parse + qemu_plugin_end_code + qemu_plugin_entry_code + qemu_plugin_get_hwaddr + qemu_plugin_hwaddr_device_name + qemu_plugin_hwaddr_is_io + qemu_plugin_hwaddr_phys_addr + qemu_plugin_insn_data + qemu_plugin_insn_disas + qemu_plugin_insn_haddr + qemu_plugin_insn_size + qemu_plugin_insn_symbol + qemu_plugin_insn_vaddr + qemu_plugin_mem_is_big_endian + qemu_plugin_mem_is_sign_extended + qemu_plugin_mem_is_store + qemu_plugin_mem_size_shift + qemu_plugin_n_max_vcpus + qemu_plugin_n_vcpus + qemu_plugin_outs + qemu_plugin_path_to_binary + qemu_plugin_register_atexit_cb + qemu_plugin_register_flush_cb + qemu_plugin_register_vcpu_exit_cb + qemu_plugin_register_vcpu_idle_cb + qemu_plugin_register_vcpu_init_cb + qemu_plugin_register_vcpu_insn_exec_cb + qemu_plugin_register_vcpu_insn_exec_inline + qemu_plugin_register_vcpu_mem_cb + qemu_plugin_register_vcpu_mem_inline + qemu_plugin_register_vcpu_resume_cb + qemu_plugin_register_vcpu_syscall_cb + qemu_plugin_register_vcpu_syscall_ret_cb + qemu_plugin_register_vcpu_tb_exec_cb + qemu_plugin_register_vcpu_tb_exec_inline + qemu_plugin_register_vcpu_tb_trans_cb + qemu_plugin_reset + qemu_plugin_start_code + qemu_plugin_tb_get_insn + qemu_plugin_tb_n_insns + qemu_plugin_tb_vaddr + qemu_plugin_uninstall + qemu_plugin_vcpu_for_each + diff --git a/qemu-plugin/Cargo.toml b/qemu-plugin/Cargo.toml index 90394ad..b0c0c52 100644 --- a/qemu-plugin/Cargo.toml +++ b/qemu-plugin/Cargo.toml @@ -17,6 +17,17 @@ once_cell = "1.19.0" qemu-plugin-sys = { version = "8.1.3-v3", workspace = true } thiserror = "1.0.51" +[target.'cfg(windows)'.dependencies.windows] +version = "0.52" +features = [ + "Win32_System_WindowsProgramming", + "Win32_System_LibraryLoader", + "Win32_Foundation", +] + +[target.'cfg(windows)'.dependencies.libc] +version = "0.2.152" + [features] -# Define external symbols with weak definitions -weak = [] +# Define external symbols with weak definition +unix-weak-link = [] diff --git a/qemu-plugin/src/lib.rs b/qemu-plugin/src/lib.rs index b8b576b..1f2b8e5 100644 --- a/qemu-plugin/src/lib.rs +++ b/qemu-plugin/src/lib.rs @@ -71,12 +71,17 @@ //! ``` #![deny(missing_docs)] -#![cfg_attr(feature = "weak", feature(linkage))] +#![cfg_attr(all(unix, feature = "unix-weak-link"), feature(linkage))] -#[cfg(feature = "weak")] -mod weak; +#[cfg(all(unix, feature = "unix-weak-link"))] +mod unix_weak_link; + +#[cfg(windows)] +mod win_link_hook; use crate::error::{Error, Result}; +#[cfg(windows)] +use libc::free; use qemu_plugin_sys::{ qemu_plugin_cb_flags, qemu_plugin_hwaddr, qemu_plugin_id_t, qemu_plugin_insn, qemu_plugin_mem_rw, qemu_plugin_meminfo_t, qemu_plugin_simple_cb_t, qemu_plugin_tb, @@ -95,11 +100,28 @@ pub mod install; pub mod plugin; pub mod sys; +#[cfg(not(windows))] extern "C" { /// glib g_free is provided by the QEMU program we are being linked into fn g_free(mem: *mut c_void); } +#[cfg(windows)] +unsafe fn g_free(mem: *mut c_void) { + //TODO: We would really like to call g_free in the qemu binary here + //but we can't, because windows doesn't export symbols unless you explicitly export them + //and g_free isn't so exported. + + // NOTE: glib 2.46 g_malloc always uses system malloc implementation: + // https://docs.gtk.org/glib/func.mem_is_system_malloc.html + // So it is safe to call libc free to free a `g_malloc`-ed object + unsafe { + if !mem.is_null() { + free(mem) + } + } +} + /// The index of a vCPU pub type VCPUIndex = c_uint; /// Flags for callbacks diff --git a/qemu-plugin/src/weak.rs b/qemu-plugin/src/unix_weak_link/mod.rs similarity index 98% rename from qemu-plugin/src/weak.rs rename to qemu-plugin/src/unix_weak_link/mod.rs index a3bdd29..a40a2ff 100644 --- a/qemu-plugin/src/weak.rs +++ b/qemu-plugin/src/unix_weak_link/mod.rs @@ -1,3 +1,6 @@ +//! Weak linkage for unix hosts allowing this library to be built as an rlib, leaving QEMU +//! symbols unresolved + use std::ptr::{null, null_mut}; use qemu_plugin_sys::*; diff --git a/qemu-plugin/src/win_link_hook/mod.rs b/qemu-plugin/src/win_link_hook/mod.rs new file mode 100644 index 0000000..65d6582 --- /dev/null +++ b/qemu-plugin/src/win_link_hook/mod.rs @@ -0,0 +1,28 @@ +//! Hook for linking against exported QEMU symbols at runtime + +use windows::core::PCSTR; +use windows::Win32::Foundation::HMODULE; +use windows::Win32::System::LibraryLoader::GetModuleHandleA; +use windows::Win32::System::WindowsProgramming::DELAYLOAD_INFO; + +type FailureHook = unsafe extern "C" fn(dli_notify: u32, pdli: DELAYLOAD_INFO) -> HMODULE; + +#[no_mangle] +/// Callback invoked by Windows loader to complete linking against QEMU symbols +pub static __pfnDliFailureHook2: FailureHook = delaylink_hook; + +extern "C" fn delaylink_hook(dli_notify: u32, pdli: DELAYLOAD_INFO) -> HMODULE { + if dli_notify == 3 { + let name = unsafe { pdli.TargetDllName.to_string() }.unwrap_or_default(); + if name == "qemu.exe" { + match unsafe { GetModuleHandleA(PCSTR::null()) } { + Ok(h) => return h, + Err(e) => { + eprintln!("Error loading top level qemu module: {e:?}"); + } + } + } + } + + HMODULE::default() +} diff --git a/qemu/build.rs b/qemu/build.rs index d6f1b8c..5caa5f7 100644 --- a/qemu/build.rs +++ b/qemu/build.rs @@ -1,9 +1,12 @@ +//! Build script for QEMU binaries. Configures QEMU by converting crate features to configure +//! arguments, then builds it into the crate OUT_DIR. + use anyhow::{anyhow, Error, Result}; use command_ext::CommandExtCheck; use reqwest::blocking::get; use std::{ env::var, - fs::{File, OpenOptions, create_dir_all}, + fs::{create_dir_all, File, OpenOptions}, path::{Path, PathBuf}, process::Command, }; @@ -898,28 +901,17 @@ fn main() -> Result<()> { let build_dir = out_dir.join("qemu-build"); let install_dir = out_dir.join("qemu"); - if !src_archive.exists() { - download( - &qemu_src_url(), - &src_archive, - )?; + download(&qemu_src_url(), &src_archive)?; } if !src_dir.exists() { - extract_txz( - &src_archive, - &src_dir, - )?; + extract_txz(&src_archive, &src_dir)?; } if !build_dir.exists() { create_dir_all(&build_dir)?; - configure( - &build_dir, - &src_dir, - &install_dir, - )?; + configure(&build_dir, &src_dir, &install_dir)?; make(&build_dir)?; } diff --git a/scripts/ci.sh b/scripts/ci.sh new file mode 100755 index 0000000..96de2b0 --- /dev/null +++ b/scripts/ci.sh @@ -0,0 +1,84 @@ +#!/bin/bash + +# Copyright (C) 2023 Intel Corporation +# SPDX-License-Identifier: Apache-2.0 + +#Run workflows locally using act + +SCRIPT_DIR=$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" &>/dev/null && pwd) +WORKFLOW_FILE="${SCRIPT_DIR}/../.github/workflows/ci.yml" +SECRETS_FILE="${SCRIPT_DIR}/../.secrets" + +if [ ! -f "${SECRETS_FILE}" ]; then + echo "No file '${SECRETS_FILE}' found. Please create one. It must have the following keys: + GITHUB_TOKEN" \ + "You can find your GitHub token with 'gh auth token'" + exit 1 +fi + +if ! command -v act &>/dev/null; then + echo "act must be installed! Install at https://github.com/nektos/act" + exit 1 +fi + +if ! command -v unbuffer &>/dev/null; then + echo "unbuffer must be installed! Install 'expect' from your package manager" + exit 1 +fi + +populate_env_file() { + ENV_FILE="${1}" + echo "Attempting automatic configuration of proxy with ENV_FILE=${ENV_FILE}" + + if [ -z "${HTTP_PROXY}" ] && [ -f ~/.docker/config.json ]; then + HTTP_PROXY=$(grep httpProxy ~/.docker/config.json | awk -F'\"[:space:]*:[:space:]*' '{split($2,a,"\""); print a[2]}') + echo "Exported docker config HTTP_PROXY=${HTTP_PROXY}" + elif [ -n "${HTTP_PROXY}" ]; then + echo "Exported docker config HTTP_PROXY=${HTTP_PROXY}" + fi + echo "HTTP_PROXY=${HTTP_PROXY}" >>"${ENV_FILE}" + echo "proxy=${HTTP_PROXY}" >>"${ENV_FILE}" + + if [ -z "${HTTPS_PROXY}" ] && [ -f ~/.docker/config.json ]; then + HTTPS_PROXY=$(grep httpsProxy ~/.docker/config.json | awk -F'\"[:space:]*:[:space:]*' '{split($2,a,"\""); print a[2]}') + echo "Exported docker config HTTPS_PROXY=${HTTPS_PROXY}" + elif [ -n "${HTTPS_PROXY}" ]; then + echo "Exported docker config HTTPS_PROXY=${HTTPS_PROXY}" + fi + echo "HTTPS_PROXY=${HTTPS_PROXY}" >>"${ENV_FILE}" + + if [ -z "${http_proxy}" ] && [ -f ~/.docker/config.json ]; then + http_proxy=$(grep httpProxy ~/.docker/config.json | awk -F'\"[:space:]*:[:space:]*' '{split($2,a,"\""); print a[2]}') + echo "Exported docker config http_proxy=${http_proxy}" + elif [ -n "${http_proxy}" ]; then + echo "Exported docker config http_proxy=${http_proxy}" + fi + echo "http_proxy=${http_proxy}" >>"${ENV_FILE}" + + if [ -z "${https_proxy}" ] && [ -f ~/.docker/config.json ]; then + https_proxy=$(grep httpsProxy ~/.docker/config.json | awk -F'\"[:space:]*:[:space:]*' '{split($2,a,"\""); print a[2]}') + echo "Exported docker config https_proxy=${https_proxy}" + elif [ -n "${https_proxy}" ]; then + echo "Exported docker config https_proxy=${https_proxy}" + fi + echo "https_proxy=${https_proxy}" >>"${ENV_FILE}" + + if [ -z "${NO_PROXY}" ] && [ -f ~/.docker/config.json ]; then + NO_PROXY=$(grep noProxy ~/.docker/config.json | awk -F'\"[:space:]*:[:space:]*' '{split($2,a,"\""); print a[2]}') + echo "Exported docker config NO_PROXY=${NO_PROXY}" + elif [ -n "${NO_PROXY}" ]; then + echo "Exported docker config NO_PROXY=${NO_PROXY}" + fi + echo "NO_PROXY=${NO_PROXY}" >>"${ENV_FILE}" + + cat "${ENV_FILE}" +} + +ENV_FILE=$(mktemp) +ARTIFACT_DIR=$(mktemp -d) +populate_env_file "${ENV_FILE}" +mkdir -p "${SCRIPT_DIR}/../.github/logs/" +unbuffer act -W "${WORKFLOW_FILE}" --env-file="${ENV_FILE}" --secret-file="${SECRETS_FILE}" \ + --artifact-server-path "${ARTIFACT_DIR}" \ + "$@" | tee "${SCRIPT_DIR}/../.github/logs/$(date '+%F-%T').log" +rm "${ENV_FILE}" diff --git a/scripts/mk-cloudinit.sh b/scripts/mk-cloudinit.sh new file mode 100755 index 0000000..48ebc65 --- /dev/null +++ b/scripts/mk-cloudinit.sh @@ -0,0 +1,29 @@ +#!/bin/bash + +set -e + +# sudo -E dnf install -y cloud-utils + +mkdir -p .github/rsrc/ +rm .github/rsrc/id_* || true +if [ ! -f .github/rsrc/id_rsa ]; then + ssh-keygen -C fedora@localhost -t rsa -q -f .github/rsrc/id_rsa -N "" +fi + +KEY="$(cat .github/rsrc/id_rsa.pub)" +# password is "password" +# mkpasswd --method=SHA-512 --rounds=4096 +PASSWORD='$6$rounds=4096$At.ZMrhUfvsFwTiG$VJ8aQCC3nr8SpUL99OHcWsR6BvlVur5qvKQHni8n5v1HxB0E3.2eLX0tbxq8nHv.JJb2cU5mXr8bAgogCd5Ke1' +cat < .github/rsrc/user-data.yml +#cloud-config +bootcmd: + - useradd -m -p ${PASSWORD} -s /bin/bash fedora + - mkdir -p /home/fedora/.ssh + - echo "${KEY}" >> /home/fedora/.ssh/authorized_keys + - chown -R fedora:fedora /home/fedora/.ssh + - chmod 700 /home/fedora/.ssh + - chmod 600 /home/fedora/.ssh/authorized_keys + - echo "fedora ALL=(ALL) NOPASSWD:ALL" >> /etc/sudoers +EOF + +cloud-localds .github/rsrc/seed.img .github/rsrc/user-data.yml \ No newline at end of file