Generate stub bindings less aggressively. (#2164)

Emit warning when using runtime files for an incompatible architecture.
This commit is contained in:
Romain Malmain 2024-05-13 11:58:19 +02:00 committed by GitHub
parent 1cf3df665a
commit edb6b509c2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 39 additions and 42 deletions

View File

@ -4,6 +4,8 @@ use std::{
process::Command, process::Command,
}; };
use libafl_qemu_build::maybe_generate_stub_bindings;
#[allow(clippy::too_many_lines)] #[allow(clippy::too_many_lines)]
pub fn build() { pub fn build() {
// Note: Unique features are checked in libafl_qemu_sys // Note: Unique features are checked in libafl_qemu_sys
@ -114,11 +116,11 @@ pub fn build() {
.write_to_file(&runtime_bindings_file) .write_to_file(&runtime_bindings_file)
.expect("Could not write bindings."); .expect("Could not write bindings.");
libafl_qemu_build::store_generated_content_if_different( maybe_generate_stub_bindings(
&cpu_target,
&emulation_mode,
&stub_runtime_bindings_file, &stub_runtime_bindings_file,
fs::read(&runtime_bindings_file) &runtime_bindings_file
.expect("Could not read generated bindings file")
.as_slice(),
); );
if (emulation_mode == "usermode") && (qemu_asan || qemu_asan_guest) { if (emulation_mode == "usermode") && (qemu_asan || qemu_asan_guest) {

View File

@ -36,3 +36,4 @@ shell-words = "1.1"
pkg-config = "0.3.26" pkg-config = "0.3.26"
cc = "1.0" cc = "1.0"
regex = "1" regex = "1"
rustversion = "1.0"

View File

@ -292,3 +292,29 @@ pub fn store_generated_content_if_different(file_to_update: &PathBuf, fresh_cont
.unwrap_or_else(|_| panic!("Unable to write in {}", file_to_update.display())); .unwrap_or_else(|_| panic!("Unable to write in {}", file_to_update.display()));
} }
} }
#[rustversion::nightly]
pub fn maybe_generate_stub_bindings(
cpu_target: &str,
emulation_mode: &str,
stub_bindings_file: &PathBuf,
bindings_file: &PathBuf,
) {
if cpu_target == "x86_64" && emulation_mode == "usermode" {
store_generated_content_if_different(
stub_bindings_file,
fs::read(bindings_file)
.expect("Could not read generated bindings file")
.as_slice(),
);
}
}
#[rustversion::not(nightly)]
pub fn maybe_generate_stub_bindings(
_cpu_target: &str,
_emulation_mode: &str,
_stub_bindings_file: &PathBuf,
_bindings_file: &PathBuf,
) {
// Do nothing
}

View File

@ -1,10 +1,6 @@
#[rustversion::nightly]
use std::fs;
use std::{env, fs::copy, path::PathBuf}; use std::{env, fs::copy, path::PathBuf};
use libafl_qemu_build::build_with_bindings; use libafl_qemu_build::{build_with_bindings, maybe_generate_stub_bindings};
#[rustversion::nightly]
use libafl_qemu_build::store_generated_content_if_different;
#[macro_export] #[macro_export]
macro_rules! assert_unique_feature { macro_rules! assert_unique_feature {
@ -18,33 +14,6 @@ macro_rules! assert_unique_feature {
} }
} }
#[rustversion::nightly]
fn maybe_generate_stub_bindings(
cpu_target: &str,
emulation_mode: &str,
stub_bindings_file: &PathBuf,
bindings_file: &PathBuf,
) {
if cpu_target == "x86_64" && emulation_mode == "usermode" {
store_generated_content_if_different(
stub_bindings_file,
fs::read(bindings_file)
.expect("Could not read generated bindings file")
.as_slice(),
);
}
}
#[rustversion::not(nightly)]
fn maybe_generate_stub_bindings(
_cpu_target: &str,
_emulation_mode: &str,
_stub_bindings_file: &PathBuf,
_bindings_file: &PathBuf,
) {
// Do nothing
}
pub fn build() { pub fn build() {
println!(r#"cargo::rustc-check-cfg=cfg(emulation_mode, values("usermode", "systemmode"))"#); println!(r#"cargo::rustc-check-cfg=cfg(emulation_mode, values("usermode", "systemmode"))"#);
println!( println!(

View File

@ -2340,7 +2340,6 @@ pub type DeviceReset = ::std::option::Option<unsafe extern "C" fn(dev: *mut Devi
#[derive(Debug, Copy, Clone)] #[derive(Debug, Copy, Clone)]
pub struct DeviceClass { pub struct DeviceClass {
pub parent_class: ObjectClass, pub parent_class: ObjectClass,
#[doc = " @categories: device categories device belongs to"]
pub categories: [::std::os::raw::c_ulong; 1usize], pub categories: [::std::os::raw::c_ulong; 1usize],
#[doc = " @fw_name: name used to identify device to firmware interfaces"] #[doc = " @fw_name: name used to identify device to firmware interfaces"]
pub fw_name: *const ::std::os::raw::c_char, pub fw_name: *const ::std::os::raw::c_char,

View File

@ -80,7 +80,7 @@ typedef enum LibaflQemuEndStatus {
#endif #endif
#else #else
#ifdef __x86_64__ #if defined(__x86_64__)
#define LIBAFL_DEFINE_FUNCTIONS(name, opcode) \ #define LIBAFL_DEFINE_FUNCTIONS(name, opcode) \
libafl_word LIBAFL_CALLING_CONVENTION _libafl_##name##_call0( \ libafl_word LIBAFL_CALLING_CONVENTION _libafl_##name##_call0( \
libafl_word action) { \ libafl_word action) { \
@ -126,9 +126,8 @@ typedef enum LibaflQemuEndStatus {
); \ ); \
return ret; \ return ret; \
} }
#endif
#ifdef __arm__ #elif defined(__arm__)
#define LIBAFL_DEFINE_FUNCTIONS(name, opcode) \ #define LIBAFL_DEFINE_FUNCTIONS(name, opcode) \
libafl_word LIBAFL_CALLING_CONVENTION _libafl_##name##_call0( \ libafl_word LIBAFL_CALLING_CONVENTION _libafl_##name##_call0( \
libafl_word action) { \ libafl_word action) { \
@ -174,9 +173,8 @@ typedef enum LibaflQemuEndStatus {
); \ ); \
return ret; \ return ret; \
} }
#endif
#ifdef __aarch64__ #elif defined(__aarch64__)
#define LIBAFL_DEFINE_FUNCTIONS(name, opcode) \ #define LIBAFL_DEFINE_FUNCTIONS(name, opcode) \
libafl_word LIBAFL_CALLING_CONVENTION _libafl_##name##_call0( \ libafl_word LIBAFL_CALLING_CONVENTION _libafl_##name##_call0( \
libafl_word action) { \ libafl_word action) { \
@ -222,6 +220,8 @@ typedef enum LibaflQemuEndStatus {
); \ ); \
return ret; \ return ret; \
} }
#else
#warning "LibAFL QEMU Runtime does not support your architecture yet, please leave an issue."
#endif #endif
#endif #endif