Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid using feature flags and env variable to set the same parameter pt.2 CPU_TARGET #2703

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions fuzzers/binary_only/qemu_cmin/Makefile.toml
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ args = [
"build",
"--profile",
"${PROFILE}",
"--no-default-features",
"--features",
"${FEATURE}",
"--target-dir",
Expand Down
2 changes: 1 addition & 1 deletion libafl_concolic/symcc_runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ cmake = { workspace = true }
bindgen = { workspace = true }
regex = { workspace = true }
which = { workspace = true }
symcc_libafl = { workspace = true, default-features = true, version = "0.14.0" }
symcc_libafl = { workspace = true, default-features = true }

[lints]
workspace = true
7 changes: 4 additions & 3 deletions libafl_qemu/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ rustdoc-args = ["--cfg", "docsrs"]
[features]
default = [
"usermode",
"x86_64",
"fork",
"build_libgasan",
"build_libqasan",
Expand All @@ -49,7 +50,7 @@ build_libqasan = []

#! ## The following architecture features are mutually exclusive.

## build qemu for x86_64 (default)
## build qemu for x86_64
x86_64 = ["libafl_qemu_sys/x86_64"]
i386 = ["libafl_qemu_sys/i386"] # build qemu for i386
arm = ["libafl_qemu_sys/arm"] # build qemu for arm
Expand Down Expand Up @@ -90,7 +91,7 @@ clippy = ["libafl_qemu_sys/clippy"]
[dependencies]
libafl = { workspace = true, features = ["std", "derive", "regex"] }
libafl_bolts = { workspace = true, features = ["std", "derive"] }
libafl_targets = { workspace = true, default-features = true, version = "0.14.0" }
libafl_targets = { workspace = true, default-features = true }
libafl_qemu_sys = { workspace = true }
libafl_derive = { workspace = true, default-features = true }

Expand Down Expand Up @@ -131,7 +132,7 @@ getset = "0.1.3"
document-features = { workspace = true, optional = true }

[build-dependencies]
libafl_qemu_build = { workspace = true, default-features = true, version = "0.14.0" }
libafl_qemu_build = { workspace = true, default-features = true }
pyo3-build-config = { version = "0.22.3", optional = true }
rustversion = { workspace = true }
bindgen = { workspace = true }
Expand Down
44 changes: 15 additions & 29 deletions libafl_qemu/build_linux.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,6 @@ void __libafl_qemu_testfile() {}
#[allow(clippy::too_many_lines)]
pub fn build() {
// Note: Unique features are checked in libafl_qemu_sys
println!(
r#"cargo::rustc-check-cfg=cfg(cpu_target, values("arm", "aarch64", "hexagon", "i386", "mips", "ppc", "riscv32", "riscv64", "x86_64"))"#
);

let emulation_mode = if cfg!(feature = "usermode") {
"usermode"
} else if cfg!(feature = "systemmode") {
"systemmode"
} else {
unreachable!(
"The macros `assert_unique_feature` and `assert_at_least_one_feature` in \
`libafl_qemu_sys/build_linux.rs` should panic before this code is reached."
);
};

let src_dir = env::var("CARGO_MANIFEST_DIR").unwrap();
let src_dir = PathBuf::from(src_dir);
Expand Down Expand Up @@ -81,29 +67,29 @@ pub fn build() {
println!("cargo:rerun-if-changed={}", libafl_runtime_dir.display());

let cpu_target = if cfg!(feature = "x86_64") {
"x86_64".to_string()
"x86_64"
} else if cfg!(feature = "arm") {
"arm".to_string()
"arm"
} else if cfg!(feature = "aarch64") {
"aarch64".to_string()
"aarch64"
} else if cfg!(feature = "i386") {
"i386".to_string()
"i386"
} else if cfg!(feature = "mips") {
"mips".to_string()
"mips"
} else if cfg!(feature = "ppc") {
"ppc".to_string()
"ppc"
} else if cfg!(feature = "riscv32") {
"riscv32".to_string()
"riscv32"
} else if cfg!(feature = "riscv64") {
"riscv64".to_string()
"riscv64"
} else if cfg!(feature = "hexagon") {
"hexagon".to_string()
"hexagon"
} else {
env::var("CPU_TARGET").unwrap_or_else(|_| "x86_64".to_string())
unreachable!(
"The macros `assert_unique_feature` and `assert_at_least_one_feature` in \
`libafl_qemu_sys/build_linux.rs` should panic before this code is reached."
);
};
println!("cargo:rerun-if-env-changed=CPU_TARGET");
println!("cargo:rustc-cfg=cpu_target=\"{cpu_target}\"");
println!("cargo::rustc-check-cfg=cfg(cpu_target, values(\"x86_64\", \"arm\", \"aarch64\", \"i386\", \"mips\", \"ppc\", \"hexagon\", \"riscv32\", \"riscv64\"))");

let cross_cc = if cfg!(feature = "usermode") && (qemu_asan || qemu_asan_guest) {
// TODO try to autodetect a cross compiler with the arch name (e.g. aarch64-linux-gnu-gcc)
Expand Down Expand Up @@ -167,8 +153,8 @@ pub fn build() {
.expect("Could not write bindings.");

maybe_generate_stub_bindings(
&cpu_target,
emulation_mode,
cpu_target,
cfg!(feature = "usermode"),
stub_runtime_bindings_file.as_path(),
runtime_bindings_file.as_path(),
);
Expand Down
7 changes: 2 additions & 5 deletions libafl_qemu/libafl_qemu_build/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -389,14 +389,11 @@ pub fn store_generated_content_if_different(
#[allow(unused)]
pub fn maybe_generate_stub_bindings(
cpu_target: &str,
emulation_mode: &str,
is_usermode: bool,
stub_bindings_file: &Path,
bindings_file: &Path,
) {
if env::var("LIBAFL_QEMU_GEN_STUBS").is_ok()
&& cpu_target == "x86_64"
&& emulation_mode == "usermode"
{
if env::var("LIBAFL_QEMU_GEN_STUBS").is_ok() && cpu_target == "x86_64" && is_usermode {
let current_rustc_version =
rustc_version::version().expect("Could not get current rustc version");

Expand Down
4 changes: 0 additions & 4 deletions libafl_qemu/libafl_qemu_sys/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,6 @@ fn nightly() {}

fn main() {
println!("cargo:rustc-check-cfg=cfg(nightly)");
println!(r#"cargo::rustc-check-cfg=cfg(emulation_mode, values("usermode", "systemmode"))"#);
println!(
r#"cargo::rustc-check-cfg=cfg(cpu_target, values("arm", "aarch64", "hexagon", "i386", "mips", "ppc", "x86_64"))"#
);
nightly();
host_specific::build();
}
56 changes: 22 additions & 34 deletions libafl_qemu/libafl_qemu_sys/build_linux.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,58 +28,46 @@ pub fn build() {
// Make sure that at least one qemu mode is set
assert_at_least_one_feature!("usermode", "systemmode");

let emulation_mode = if cfg!(feature = "usermode") {
"usermode"
} else if cfg!(feature = "systemmode") {
"systemmode"
} else {
unreachable!(
"The above macros, `assert_unique_feature` and `assert_at_least_one_feature`, should \
panic before this code is reached."
);
};

// Make sure we have at most one architecutre feature set
// Else, we default to `x86_64` - having a default makes CI easier :)
assert_unique_feature!(
"arm", "aarch64", "i386", "x86_64", "mips", "ppc", "hexagon", "riscv32", "riscv64"
);
// Make sure we have at least one architecutre feature set
assert_at_least_one_feature!(
"arm", "aarch64", "i386", "x86_64", "mips", "ppc", "hexagon", "riscv32", "riscv64"
);

// Make sure that we don't have BE set for any architecture other than arm and mips
// Sure aarch64 may support BE, but its not in common usage and we don't
// need it yet and so haven't tested it
assert_unique_feature!("be", "aarch64", "i386", "x86_64", "hexagon", "riscv32", "riscv64");

let cpu_target = if cfg!(feature = "x86_64") {
"x86_64".to_string()
"x86_64"
} else if cfg!(feature = "arm") {
"arm".to_string()
"arm"
} else if cfg!(feature = "aarch64") {
"aarch64".to_string()
"aarch64"
} else if cfg!(feature = "i386") {
"i386".to_string()
"i386"
} else if cfg!(feature = "mips") {
"mips".to_string()
"mips"
} else if cfg!(feature = "ppc") {
"ppc".to_string()
"ppc"
} else if cfg!(feature = "riscv32") {
"riscv32".to_string()
"riscv32"
} else if cfg!(feature = "riscv64") {
"riscv64".to_string()
"riscv64"
} else if cfg!(feature = "hexagon") {
"hexagon".to_string()
"hexagon"
} else {
env::var("CPU_TARGET").unwrap_or_else(|_| {
println!(
"cargo:warning=No architecture feature enabled or CPU_TARGET env specified for libafl_qemu, supported: arm, aarch64, hexagon, i386, mips, ppc, riscv32, riscv64, x86_64 - defaulting to x86_64"
);
"x86_64".to_string()
})
unreachable!(
"The above macros, `assert_unique_feature` and `assert_at_least_one_feature`, should \
panic before this code is reached."
);
};
println!("cargo:rerun-if-env-changed=CPU_TARGET");

println!("cargo:rerun-if-env-changed=LIBAFL_QEMU_GEN_STUBS");
println!("cargo:rustc-cfg=cpu_target=\"{cpu_target}\"");
println!("cargo::rustc-check-cfg=cfg(cpu_target, values(\"x86_64\", \"arm\", \"aarch64\", \"i386\", \"mips\", \"ppc\", \"hexagon\", \"riscv32\", \"riscv64\"))");

let jobs = env::var("NUM_JOBS")
.ok()
Expand All @@ -100,9 +88,9 @@ pub fn build() {
}

build_with_bindings(
&cpu_target,
cpu_target,
cfg!(feature = "be"),
emulation_mode == "usermode",
cfg!(feature = "usermode"),
jobs,
&bindings_file,
);
Expand All @@ -111,8 +99,8 @@ pub fn build() {

// If the bindings are built and differ from the current stub, replace it with the freshly generated bindings
maybe_generate_stub_bindings(
&cpu_target,
emulation_mode,
cpu_target,
cfg!(feature = "usermode"),
stub_bindings_file.as_path(),
bindings_file.as_path(),
);
Expand Down
32 changes: 16 additions & 16 deletions libafl_qemu/src/arch/mod.rs
Original file line number Diff line number Diff line change
@@ -1,39 +1,39 @@
#[cfg(cpu_target = "aarch64")]
#[cfg(feature = "aarch64")]
pub mod aarch64;
#[cfg(all(cpu_target = "aarch64", not(feature = "clippy")))]
#[cfg(all(feature = "aarch64", not(feature = "clippy")))]
pub use aarch64::*;

#[cfg(cpu_target = "arm")]
#[cfg(feature = "arm")]
pub mod arm;
#[cfg(all(cpu_target = "arm", not(feature = "clippy")))]
#[cfg(all(feature = "arm", not(feature = "clippy")))]
pub use arm::*;

#[cfg(cpu_target = "i386")]
#[cfg(feature = "i386")]
pub mod i386;
#[cfg(all(cpu_target = "i386", not(feature = "clippy")))]
#[cfg(all(feature = "i386", not(feature = "clippy")))]
pub use i386::*;

#[cfg(cpu_target = "x86_64")]
#[cfg(feature = "x86_64")]
pub mod x86_64;
#[cfg(cpu_target = "x86_64")]
#[cfg(feature = "x86_64")]
pub use x86_64::*;

#[cfg(cpu_target = "mips")]
#[cfg(feature = "mips")]
pub mod mips;
#[cfg(cpu_target = "mips")]
#[cfg(feature = "mips")]
pub use mips::*;

#[cfg(cpu_target = "ppc")]
#[cfg(feature = "ppc")]
pub mod ppc;
#[cfg(cpu_target = "ppc")]
#[cfg(feature = "ppc")]
pub use ppc::*;

#[cfg(cpu_target = "hexagon")]
#[cfg(feature = "hexagon")]
pub mod hexagon;
#[cfg(cpu_target = "hexagon")]
#[cfg(feature = "hexagon")]
pub use hexagon::*;

#[cfg(any(cpu_target = "riscv32", cpu_target = "riscv64"))]
#[cfg(any(feature = "riscv32", feature = "riscv64"))]
pub mod riscv;
#[cfg(any(cpu_target = "riscv32", cpu_target = "riscv64"))]
#[cfg(any(feature = "riscv32", feature = "riscv64"))]
pub use riscv::*;
8 changes: 4 additions & 4 deletions libafl_qemu/src/elf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,17 +54,17 @@ impl<'a> EasyElf<'a> {
return if sym.st_value == 0 {
None
} else if self.is_pic() {
#[cfg(cpu_target = "arm")]
#[cfg(feature = "arm")]
// Required because of arm interworking addresses aka bit(0) for thumb mode
let addr = (sym.st_value as GuestAddr + load_addr) & !(0x1 as GuestAddr);
#[cfg(not(cpu_target = "arm"))]
#[cfg(not(feature = "arm"))]
let addr = sym.st_value as GuestAddr + load_addr;
Some(addr)
} else {
#[cfg(cpu_target = "arm")]
#[cfg(feature = "arm")]
// Required because of arm interworking addresses aka bit(0) for thumb mode
let addr = (sym.st_value as GuestAddr) & !(0x1 as GuestAddr);
#[cfg(not(cpu_target = "arm"))]
#[cfg(not(feature = "arm"))]
let addr = sym.st_value as GuestAddr;
Some(addr)
};
Expand Down
2 changes: 1 addition & 1 deletion libafl_qemu/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
// This lint triggers too often on the current GuestAddr type when emulating 64-bit targets because
// u64::from(GuestAddr) is a no-op, but the .into() call is needed when GuestAddr is u32.
#![cfg_attr(
any(cpu_target = "x86_64", cpu_target = "aarch64"),
any(feature = "x86_64", feature = "aarch64"),
allow(clippy::useless_conversion)
)]
// libafl_qemu_sys export types with empty struct markers (e.g. struct {} start_init_save)
Expand Down
2 changes: 1 addition & 1 deletion libafl_qemu/src/modules/calls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ where
return None;
}

#[cfg(cpu_target = "arm")]
#[cfg(feature = "arm")]
h.cs.set_mode(if pc & 1 == 1 {
arch::arm::ArchMode::Thumb.into()
} else {
Expand Down
2 changes: 1 addition & 1 deletion libafl_qemu/src/modules/cmplog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ impl CmpLogRoutinesModule {
return None;
}

#[cfg(cpu_target = "arm")]
#[cfg(feature = "arm")]
h.cs.set_mode(if pc & 1 == 1 {
capstone::arch::arm::ArchMode::Thumb.into()
} else {
Expand Down
12 changes: 6 additions & 6 deletions libafl_qemu/src/modules/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,19 @@ pub use edges::{
StdEdgeCoverageFullModuleBuilder, StdEdgeCoverageModule, StdEdgeCoverageModuleBuilder,
};

#[cfg(not(cpu_target = "hexagon"))]
#[cfg(not(feature = "hexagon"))]
pub mod calls;
#[cfg(not(cpu_target = "hexagon"))]
#[cfg(not(feature = "hexagon"))]
pub use calls::CallTracerModule;

#[cfg(not(any(cpu_target = "mips", cpu_target = "hexagon")))]
#[cfg(not(any(feature = "mips", feature = "hexagon")))]
pub mod cmplog;
#[cfg(not(any(cpu_target = "mips", cpu_target = "hexagon")))]
#[cfg(not(any(feature = "mips", feature = "hexagon")))]
pub use cmplog::CmpLogModule;

#[cfg(not(cpu_target = "hexagon"))]
#[cfg(not(feature = "hexagon"))]
pub mod drcov;
#[cfg(not(cpu_target = "hexagon"))]
#[cfg(not(feature = "hexagon"))]
pub use drcov::{DrCovMetadata, DrCovModule, DrCovModuleBuilder};

use crate::{emu::EmulatorModules, Qemu};
Expand Down
Loading
Loading