Skip to content

Commit 961e63e

Browse files
alexandruagalxiord
authored andcommitted
remove integer-atomics as distinct feature
The "integer-atomics" feature can be replaced by using intrinsic `cfg` annotations (such as detecting different platforms). Also removed the implementation of `AtomicValued` for `AtomicBool` because we cannot safely reinterpret locations in guest memory as `bool` (values other than 0 and 1 may lead to undefined behaviour). Renamed `AtomicValued` to `AtomicInteger`, which seems like a more accurate description. Signed-off-by: Alexandru Agache <[email protected]>
1 parent f40c12f commit 961e63e

File tree

3 files changed

+25
-26
lines changed

3 files changed

+25
-26
lines changed

Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ autobenches = false
1212

1313
[features]
1414
default = []
15-
integer-atomics = []
1615
backend-mmap = []
1716
backend-atomic = ["arc-swap"]
1817

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,6 @@ pub use atomic::{GuestMemoryAtomic, GuestMemoryLoadGuard};
5353

5454
pub mod volatile_memory;
5555
pub use volatile_memory::{
56-
AtomicValued, Error as VolatileMemoryError, Result as VolatileMemoryResult, VolatileArrayRef,
56+
AtomicInteger, Error as VolatileMemoryError, Result as VolatileMemoryResult, VolatileArrayRef,
5757
VolatileMemory, VolatileRef, VolatileSlice,
5858
};

src/volatile_memory.rs

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -124,29 +124,29 @@ pub fn compute_offset(base: usize, offset: usize) -> Result<usize> {
124124
/// Objects that implement this trait must consist exclusively of atomic types
125125
/// from [`std::sync::atomic`](https://doc.rust-lang.org/std/sync/atomic/), except for
126126
/// [`AtomicPtr<T>`](https://doc.rust-lang.org/std/sync/atomic/struct.AtomicPtr.html).
127-
pub unsafe trait AtomicValued: Sync + Send {}
128-
129-
// also conditionalize on #[cfg(target_has_atomic) when it is stabilized
130-
#[cfg(feature = "integer-atomics")]
131-
unsafe impl AtomicValued for std::sync::atomic::AtomicBool {}
132-
#[cfg(feature = "integer-atomics")]
133-
unsafe impl AtomicValued for std::sync::atomic::AtomicI8 {}
134-
#[cfg(feature = "integer-atomics")]
135-
unsafe impl AtomicValued for std::sync::atomic::AtomicI16 {}
136-
#[cfg(feature = "integer-atomics")]
137-
unsafe impl AtomicValued for std::sync::atomic::AtomicI32 {}
138-
#[cfg(feature = "integer-atomics")]
139-
unsafe impl AtomicValued for std::sync::atomic::AtomicI64 {}
140-
unsafe impl AtomicValued for std::sync::atomic::AtomicIsize {}
141-
#[cfg(feature = "integer-atomics")]
142-
unsafe impl AtomicValued for std::sync::atomic::AtomicU8 {}
143-
#[cfg(feature = "integer-atomics")]
144-
unsafe impl AtomicValued for std::sync::atomic::AtomicU16 {}
145-
#[cfg(feature = "integer-atomics")]
146-
unsafe impl AtomicValued for std::sync::atomic::AtomicU32 {}
147-
#[cfg(feature = "integer-atomics")]
148-
unsafe impl AtomicValued for std::sync::atomic::AtomicU64 {}
149-
unsafe impl AtomicValued for std::sync::atomic::AtomicUsize {}
127+
pub unsafe trait AtomicInteger: Sync + Send {}
128+
129+
// TODO: Detect availability using #[cfg(target_has_atomic) when it is stabilized.
130+
// Right now we essentially assume we're running on either x86 or Arm (32 or 64 bit). AFAIK,
131+
// Rust starts using additional synchronization primitives to implement atomics when they're
132+
// not natively available, and that doesn't interact safely with how we cast pointers to
133+
// atomic value references. We should be wary of this when looking at a broader range of
134+
// platforms.
135+
136+
unsafe impl AtomicInteger for std::sync::atomic::AtomicI8 {}
137+
unsafe impl AtomicInteger for std::sync::atomic::AtomicI16 {}
138+
unsafe impl AtomicInteger for std::sync::atomic::AtomicI32 {}
139+
#[cfg(any(target_arch = "x86_64", target_arch = "aarch64"))]
140+
unsafe impl AtomicInteger for std::sync::atomic::AtomicI64 {}
141+
142+
unsafe impl AtomicInteger for std::sync::atomic::AtomicU8 {}
143+
unsafe impl AtomicInteger for std::sync::atomic::AtomicU16 {}
144+
unsafe impl AtomicInteger for std::sync::atomic::AtomicU32 {}
145+
#[cfg(any(target_arch = "x86_64", target_arch = "aarch64"))]
146+
unsafe impl AtomicInteger for std::sync::atomic::AtomicU64 {}
147+
148+
unsafe impl AtomicInteger for std::sync::atomic::AtomicIsize {}
149+
unsafe impl AtomicInteger for std::sync::atomic::AtomicUsize {}
150150

151151
/// Types that support raw volatile access to their data.
152152
pub trait VolatileMemory {
@@ -235,7 +235,7 @@ pub trait VolatileMemory {
235235
///
236236
/// If the resulting pointer is not aligned, this method will return an
237237
/// [`Error`](enum.Error.html).
238-
fn get_atomic_ref<T: AtomicValued>(&self, offset: usize) -> Result<&T> {
238+
fn get_atomic_ref<T: AtomicInteger>(&self, offset: usize) -> Result<&T> {
239239
let slice = self.get_slice(offset, size_of::<T>())?;
240240
slice.check_alignment(align_of::<T>())?;
241241

0 commit comments

Comments
 (0)