This commit is contained in:
Andrea Fioraldi 2021-09-27 14:09:15 +02:00
parent 171c85fc4f
commit 21f88b58b5
8 changed files with 133 additions and 22 deletions

View File

@ -28,4 +28,5 @@ default-members = [
] ]
exclude = [ exclude = [
"fuzzers", "fuzzers",
"bindings",
] ]

View File

@ -0,0 +1,16 @@
[package]
name = "pylibafl"
version = "0.1.0"
edition = "2018"
[dependencies]
pyo3 = { version = "0.14.3", features = ["extension-module"] }
libafl_qemu = { path = "../../libafl_qemu", version = "0.6", features = ["python"] }
libafl_sugar = { path = "../../libafl_sugar", version = "0.6", features = ["python"] }
[build-dependencies]
pyo3-build-config = { version = "0.14.5" }
[lib]
name = "pylibafl"
crate-type = ["cdylib"]

View File

@ -0,0 +1,17 @@
use libafl_sugar;
use libafl_qemu;
use pyo3::prelude::*;
#[pymodule]
#[pyo3(name = "pylibafl")]
pub fn python_module(py: Python, m: &PyModule) -> PyResult<()> {
let sugar_module = PyModule::new(py, "sugar")?;
libafl_sugar::python_module(py, sugar_module)?;
m.add_submodule(sugar_module)?;
let qemu_module = PyModule::new(py, "qemu")?;
libafl_qemu::python_module(py, qemu_module)?;
m.add_submodule(qemu_module)?;
Ok(())
}

View File

@ -11,7 +11,7 @@ keywords = ["fuzzing", "qemu", "instrumentation"]
edition = "2018" edition = "2018"
[features] [features]
python = ["pyo3"] python = ["pyo3", "pyo3-build-config"]
default = [] default = []
[dependencies] [dependencies]
@ -23,11 +23,13 @@ num = "0.4"
num_enum = "0.5.1" num_enum = "0.5.1"
goblin = "0.4.2" goblin = "0.4.2"
libc = "0.2.97" libc = "0.2.97"
pyo3 = { version = "0.14.3", features = ["extension-module"], optional = true } #pyo3 = { version = "0.14.3", features = ["extension-module"], optional = true }
pyo3 = { version = "0.14.3", optional = true }
[build-dependencies] [build-dependencies]
cc = { version = "1.0" } cc = { version = "1.0" }
which = "4.1" which = "4.1"
pyo3-build-config = { version = "0.14.5", optional = true }
[lib] [lib]
name = "libafl_qemu" name = "libafl_qemu"

View File

@ -1,5 +1,8 @@
use std::{env, fs::copy, path::Path, process::Command}; #[cfg(not(feature = "python"))]
//use std::fs::read_dir; use std::fs::copy;
#[cfg(feature = "python")]
use std::fs::read_dir;
use std::{env, path::Path, process::Command};
use which::which; use which::which;
const QEMU_URL: &str = "https://github.com/AFLplusplus/qemu-libafl-bridge"; const QEMU_URL: &str = "https://github.com/AFLplusplus/qemu-libafl-bridge";
@ -150,6 +153,77 @@ fn main() {
//let _ = remove_file(build_dir.join(&format!("libqemu-{}.so", cpu_target))); //let _ = remove_file(build_dir.join(&format!("libqemu-{}.so", cpu_target)));
} }
#[cfg(feature = "python")]
{
let mut objects = vec![];
for dir in &[
build_dir.join("libcommon.fa.p"),
build_dir.join(&format!("libqemu-{}-linux-user.fa.p", cpu_target)),
//build_dir.join("libqemuutil.a.p"),
//build_dir.join("libqom.fa.p"),
//build_dir.join("libhwcore.fa.p"),
//build_dir.join("libcapstone.a.p"),
] {
for path in read_dir(dir).unwrap() {
let path = path.unwrap().path();
if path.is_file() {
if let Some(name) = path.file_name() {
if name.to_string_lossy().starts_with("stubs") {
continue;
} else if let Some(ext) = path.extension() {
if ext == "o" {
objects.push(path);
}
}
}
}
}
}
for obj in &objects {
println!("cargo:rustc-cdylib-link-arg={}", obj.display());
}
println!("cargo:rustc-cdylib-link-arg=-Wl,--start-group");
println!("cargo:rustc-cdylib-link-arg=-Wl,--whole-archive");
println!(
"cargo:rustc-cdylib-link-arg={}/libhwcore.fa",
build_dir.display()
);
println!(
"cargo:rustc-cdylib-link-arg={}/libqom.fa",
build_dir.display()
);
println!("cargo:rustc-cdylib-link-arg=-Wl,--no-whole-archive");
println!(
"cargo:rustc-cdylib-link-arg={}/libcapstone.a",
build_dir.display()
);
println!(
"cargo:rustc-cdylib-link-arg={}/libqemuutil.a",
build_dir.display()
);
println!(
"cargo:rustc-cdylib-link-arg={}/libhwcore.fa",
build_dir.display()
);
println!(
"cargo:rustc-cdylib-link-arg={}/libqom.fa",
build_dir.display()
);
println!("cargo:rustc-cdylib-link-arg=-lrt");
println!("cargo:rustc-cdylib-link-arg=-lutil");
println!("cargo:rustc-cdylib-link-arg=-lgthread-2.0");
println!("cargo:rustc-cdylib-link-arg=-lglib-2.0");
println!("cargo:rustc-cdylib-link-arg=-lstdc++");
println!("cargo:rustc-cdylib-link-arg=-Wl,--end-group");
}
#[cfg(not(feature = "python"))]
{
copy( copy(
build_dir.join(&format!("libqemu-{}.so", cpu_target)), build_dir.join(&format!("libqemu-{}.so", cpu_target)),
target_dir.join(&format!("libqemu-{}.so", cpu_target)), target_dir.join(&format!("libqemu-{}.so", cpu_target)),
@ -164,6 +238,7 @@ fn main() {
println!("cargo:rustc-env=LD_LIBRARY_PATH={}", target_dir.display()); println!("cargo:rustc-env=LD_LIBRARY_PATH={}", target_dir.display());
} }
}
/* /*
// Build a static library // Build a static library

View File

@ -33,14 +33,14 @@ pub fn filter_qemu_args() -> Vec<String> {
args args
} }
#[cfg(all(target_od = "linux", feature = "python"))] #[cfg(all(target_os = "linux", feature = "python"))]
use pyo3::prelude::*; use pyo3::prelude::*;
#[cfg(all(target_od = "linux", feature = "python"))] #[cfg(all(target_os = "linux", feature = "python"))]
#[pymodule] #[pymodule]
#[pyo3(name = "libafl_qemu")] #[pyo3(name = "libafl_qemu")]
#[allow(clippy::items_after_statements)] #[allow(clippy::items_after_statements)]
fn python_module(_py: Python, m: &PyModule) -> PyResult<()> { pub fn python_module(_py: Python, m: &PyModule) -> PyResult<()> {
use core::mem::transmute; use core::mem::transmute;
use pyo3::exceptions::PyValueError; use pyo3::exceptions::PyValueError;

View File

@ -16,15 +16,15 @@ python = ["pyo3", "libafl_qemu/python", "pyo3-build-config"]
default = [] default = []
[build-dependencies] [build-dependencies]
pyo3-build-config = { version = "0.14.2", optional = true } pyo3-build-config = { version = "0.14.5", optional = true }
[dependencies] [dependencies]
libafl = { path = "../libafl", version = "0.6.1" } libafl = { path = "../libafl", version = "0.6.1" }
libafl_targets = { path = "../libafl_targets", version = "0.6.1" } libafl_targets = { path = "../libafl_targets", version = "0.6.1" }
libafl_qemu = { path = "../libafl_qemu", version = "0.6.1" } libafl_qemu = { path = "../libafl_qemu", version = "0.6.1" }
typed-builder = "0.9.0" # Implement the builder pattern at compiletime typed-builder = "0.9.0" # Implement the builder pattern at compiletime
pyo3 = { version = "0.14.5", features = ["extension-module"], optional = true } #pyo3 = { version = "0.14.3", features = ["extension-module"], optional = true }
pyo3 = { version = "0.14.3", optional = true }
[lib] [lib]
name = "libafl_sugar" name = "libafl_sugar"

View File

@ -17,7 +17,7 @@ use pyo3::prelude::*;
#[cfg(feature = "python")] #[cfg(feature = "python")]
#[pymodule] #[pymodule]
#[pyo3(name = "libafl_sugar")] #[pyo3(name = "libafl_sugar")]
fn python_module(py: Python, m: &PyModule) -> PyResult<()> { pub fn python_module(py: Python, m: &PyModule) -> PyResult<()> {
inmemory::pybind::register(py, m)?; inmemory::pybind::register(py, m)?;
#[cfg(target_os = "linux")] #[cfg(target_os = "linux")]
{ {