Skip to content

Commit

Permalink
Fix deny(unused) of an unused import with SGX + Miri (#581)
Browse files Browse the repository at this point in the history
The `deny(unused)` use that used to be here is incompatible with building
this crate with Miri. The import in question is only used in a module
that is only declared when we are not `cfg(miri)`.

I tried to fix this by grouping items into modules so that I could apply
`cfg(not(miri))` to all the SGX code. One could also make the build work
by deleting the `#[deny(unused)]`, but that felt hacky.
  • Loading branch information
saethlin authored Mar 8, 2024
1 parent 45bdb72 commit da34864
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 28 deletions.
2 changes: 1 addition & 1 deletion src/backtrace/libunwind.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ impl Frame {
// the address here, which could be later mapped to correct function.
#[cfg(all(target_env = "sgx", target_vendor = "fortanix"))]
{
let image_base = super::get_image_base();
let image_base = super::sgx_image_base::get_image_base();
ip = usize::wrapping_sub(ip as usize, image_base as _) as _;
}
ip
Expand Down
64 changes: 37 additions & 27 deletions src/backtrace/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,39 +125,49 @@ impl fmt::Debug for Frame {
}
}

#[cfg(all(target_env = "sgx", target_vendor = "fortanix", not(feature = "std")))]
mod sgx_no_std_image_base {
use core::ffi::c_void;
use core::sync::atomic::{AtomicUsize, Ordering::SeqCst};

static IMAGE_BASE: AtomicUsize = AtomicUsize::new(0);

/// Set the image base address. This is only available for Fortanix SGX
/// target when the `std` feature is not enabled. This can be used in the
/// standard library to set the correct base address.
#[doc(hidden)]
pub fn set_image_base(base_addr: *mut c_void) {
IMAGE_BASE.store(base_addr as _, SeqCst);
#[cfg(all(target_env = "sgx", target_vendor = "fortanix", not(miri)))]
mod sgx_image_base {

#[cfg(not(feature = "std"))]
pub(crate) mod imp {
use core::ffi::c_void;
use core::sync::atomic::{AtomicUsize, Ordering::SeqCst};

static IMAGE_BASE: AtomicUsize = AtomicUsize::new(0);

/// Set the image base address. This is only available for Fortanix SGX
/// target when the `std` feature is not enabled. This can be used in the
/// standard library to set the correct base address.
#[doc(hidden)]
pub fn set_image_base(base_addr: *mut c_void) {
IMAGE_BASE.store(base_addr as _, SeqCst);
}

pub(crate) fn get_image_base() -> *mut c_void {
IMAGE_BASE.load(SeqCst) as _
}
}

pub(crate) fn get_image_base() -> *mut c_void {
IMAGE_BASE.load(SeqCst) as _
}
}

#[cfg(all(target_env = "sgx", target_vendor = "fortanix", not(feature = "std")))]
pub use self::sgx_no_std_image_base::set_image_base;
#[cfg(feature = "std")]
mod imp {
use core::ffi::c_void;

#[cfg(all(target_env = "sgx", target_vendor = "fortanix", not(feature = "std")))]
#[deny(unused)]
pub(crate) use self::sgx_no_std_image_base::get_image_base;
pub(crate) fn get_image_base() -> *mut c_void {
std::os::fortanix_sgx::mem::image_base() as _
}
}

#[cfg(all(target_env = "sgx", target_vendor = "fortanix", feature = "std"))]
#[deny(unused)]
pub(crate) fn get_image_base() -> *mut c_void {
std::os::fortanix_sgx::mem::image_base() as _
pub(crate) use imp::get_image_base;
}

#[cfg(all(
target_env = "sgx",
target_vendor = "fortanix",
not(feature = "std"),
not(miri)
))]
pub use sgx_image_base::imp::set_image_base;

cfg_if::cfg_if! {
// This needs to come first, to ensure that
// Miri takes priority over the host platform
Expand Down

0 comments on commit da34864

Please sign in to comment.