Skip to content

Commit 073aeda

Browse files
authored
Enable the unsafe-op-in-unsafe-fn lint (#10559)
* Enable the `unsafe-op-in-unsafe-fn` lint This commit enables the `unsafe-op-in-unsafe-fn` lint in rustc for the entire workspace. This lint will be warn-by-default in the 2024 edition so this is intended to smooth the future migration to the new edition. Many `unsafe` blocks were added in places the lint warned about, with two major exceptions. The `wasmtime` and `wasmtime-c-api` crates simply expect this lint to fire and effectively disable the lint. They're too big at this time to do through this PR. My hope is that one day in the future they'll be migrated, but more realistically that probably won't happen so these crates just won't benefit from this lint. * Fix nostd fiber build prtest:full * Fix build on Windows * Fix asan build
1 parent 56148ad commit 073aeda

File tree

79 files changed

+584
-378
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

79 files changed

+584
-378
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,7 @@ rust-2024-prelude-collisions = 'warn'
196196
rust-2024-incompatible-pat = 'warn'
197197
missing-unsafe-on-extern = 'warn'
198198
impl-trait-overcaptures = 'warn'
199+
unsafe-op-in-unsafe-fn = 'warn'
199200

200201
# Don't warn about unknown cfgs for pulley
201202
[workspace.lints.rust.unexpected_cfgs]

cranelift/codegen/src/data_value.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -231,13 +231,13 @@ impl DataValue {
231231
/// Write a [DataValue] to a memory location in native-endian byte order.
232232
pub unsafe fn write_value_to(&self, p: *mut u128) {
233233
let size = self.ty().bytes() as usize;
234-
self.write_to_slice_ne(std::slice::from_raw_parts_mut(p as *mut u8, size));
234+
self.write_to_slice_ne(unsafe { std::slice::from_raw_parts_mut(p as *mut u8, size) });
235235
}
236236

237237
/// Read a [DataValue] from a memory location using a given [Type] in native-endian byte order.
238238
pub unsafe fn read_value_from(p: *const u128, ty: Type) -> Self {
239239
DataValue::read_from_slice_ne(
240-
std::slice::from_raw_parts(p as *const u8, ty.bytes() as usize),
240+
unsafe { std::slice::from_raw_parts(p as *const u8, ty.bytes() as usize) },
241241
ty,
242242
)
243243
}

cranelift/entity/src/boxed_slice.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ where
3333
/// This relies on `raw` pointing to a valid slice of `V`s.
3434
pub unsafe fn from_raw(raw: *mut [V]) -> Self {
3535
Self {
36-
elems: Box::from_raw(raw),
36+
elems: unsafe { Box::from_raw(raw) },
3737
unused: PhantomData,
3838
}
3939
}

cranelift/filetests/src/function_runner.rs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -394,14 +394,16 @@ impl<'a> Trampoline<'a> {
394394
| Architecture::Pulley32be
395395
| Architecture::Pulley64be => {
396396
let mut state = pulley::Vm::new();
397-
state.call(
398-
NonNull::new(trampoline_ptr.cast_mut()).unwrap(),
399-
&[
400-
pulley::XRegVal::new_ptr(function_ptr.cast_mut()).into(),
401-
pulley::XRegVal::new_ptr(arguments_address).into(),
402-
],
403-
[],
404-
);
397+
unsafe {
398+
state.call(
399+
NonNull::new(trampoline_ptr.cast_mut()).unwrap(),
400+
&[
401+
pulley::XRegVal::new_ptr(function_ptr.cast_mut()).into(),
402+
pulley::XRegVal::new_ptr(arguments_address).into(),
403+
],
404+
[],
405+
);
406+
}
405407
}
406408

407409
// Other targets natively execute this machine code.

cranelift/jit/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
//! which shows how to use some of the features of `cranelift_jit`.
55
66
#![deny(missing_docs, unreachable_pub)]
7+
#![expect(unsafe_op_in_unsafe_fn, reason = "crate isn't migrated yet")]
78

89
mod backend;
910
mod compiled_blob;

crates/c-api/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
//! specific to Wasmtime and has fewer gymnastics to implement.
1414
1515
#![expect(non_camel_case_types, reason = "matching C style, not Rust")]
16+
#![expect(unsafe_op_in_unsafe_fn, reason = "crate isn't migrated yet")]
1617

1718
pub use wasmtime;
1819

crates/environ/src/stack_map.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ impl<'a> StackMap<'a> {
103103
/// map is associated with.
104104
pub unsafe fn sp(&self, fp: *mut usize) -> *mut usize {
105105
let frame_size = usize::try_from(self.frame_size).unwrap();
106-
fp.byte_sub(frame_size)
106+
unsafe { fp.byte_sub(frame_size) }
107107
}
108108

109109
/// Given the stack pointer, get a reference to each live GC reference in
@@ -117,7 +117,7 @@ impl<'a> StackMap<'a> {
117117
self.offsets().map(move |i| {
118118
log::trace!("Live GC ref in frame at frame offset {:#x}", i);
119119
let i = usize::try_from(i).unwrap();
120-
let ptr_to_gc_ref = sp.byte_add(i);
120+
let ptr_to_gc_ref = unsafe { sp.byte_add(i) };
121121

122122
// Assert that the pointer is inside this stack map's frame.
123123
assert!({

crates/fiber/src/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,9 @@ impl FiberStack {
7070
/// The caller must properly allocate the stack space with a guard page and
7171
/// make the pages accessible for correct behavior.
7272
pub unsafe fn from_raw_parts(bottom: *mut u8, guard_size: usize, len: usize) -> Result<Self> {
73-
Ok(Self(imp::FiberStack::from_raw_parts(
74-
bottom, guard_size, len,
75-
)?))
73+
Ok(Self(unsafe {
74+
imp::FiberStack::from_raw_parts(bottom, guard_size, len)?
75+
}))
7676
}
7777

7878
/// Gets the top of the stack.

crates/fiber/src/nostd.rs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ impl FiberStack {
8181
pub unsafe fn from_raw_parts(base: *mut u8, guard_size: usize, len: usize) -> Result<Self> {
8282
Ok(FiberStack {
8383
storage: vec![],
84-
base: BasePtr(base.offset(isize::try_from(guard_size).unwrap())),
84+
base: BasePtr(unsafe { base.offset(isize::try_from(guard_size).unwrap()) }),
8585
len,
8686
})
8787
}
@@ -174,15 +174,19 @@ impl Suspend {
174174
}
175175

176176
unsafe fn take_resume<A, B, C>(&self) -> A {
177-
match (*self.result_location::<A, B, C>()).replace(RunResult::Executing) {
178-
RunResult::Resuming(val) => val,
179-
_ => panic!("not in resuming state"),
177+
unsafe {
178+
match (*self.result_location::<A, B, C>()).replace(RunResult::Executing) {
179+
RunResult::Resuming(val) => val,
180+
_ => panic!("not in resuming state"),
181+
}
180182
}
181183
}
182184

183185
unsafe fn result_location<A, B, C>(&self) -> *const Cell<RunResult<A, B, C>> {
184-
let ret = self.top_of_stack.cast::<*const u8>().offset(-1).read();
185-
assert!(!ret.is_null());
186-
ret.cast()
186+
unsafe {
187+
let ret = self.top_of_stack.cast::<*const u8>().offset(-1).read();
188+
assert!(!ret.is_null());
189+
ret.cast()
190+
}
187191
}
188192
}

crates/fiber/src/unix.rs

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ impl FiberStack {
9090
return Self::from_custom(asan::new_fiber_stack(len)?);
9191
}
9292
Ok(FiberStack {
93-
base: BasePtr(base.add(guard_size)),
93+
base: BasePtr(unsafe { base.add(guard_size) }),
9494
len,
9595
storage: FiberStackStorage::Unmanaged(guard_size),
9696
})
@@ -277,16 +277,20 @@ impl Suspend {
277277
}
278278

279279
unsafe fn take_resume<A, B, C>(&self) -> A {
280-
match (*self.result_location::<A, B, C>()).replace(RunResult::Executing) {
281-
RunResult::Resuming(val) => val,
282-
_ => panic!("not in resuming state"),
280+
unsafe {
281+
match (*self.result_location::<A, B, C>()).replace(RunResult::Executing) {
282+
RunResult::Resuming(val) => val,
283+
_ => panic!("not in resuming state"),
284+
}
283285
}
284286
}
285287

286288
unsafe fn result_location<A, B, C>(&self) -> *const Cell<RunResult<A, B, C>> {
287-
let ret = self.top_of_stack.cast::<*const u8>().offset(-1).read();
288-
assert!(!ret.is_null());
289-
ret.cast()
289+
unsafe {
290+
let ret = self.top_of_stack.cast::<*const u8>().offset(-1).read();
291+
assert!(!ret.is_null());
292+
ret.cast()
293+
}
290294
}
291295
}
292296

@@ -370,15 +374,19 @@ mod asan {
370374
// trigger false positives in ASAN. That leads to the design of this
371375
// module as-is where this function exists to have these three
372376
// functions very close to one another.
373-
__sanitizer_start_switch_fiber(private_asan_pointer_ref, prev.bottom, prev.size);
374-
super::wasmtime_fiber_switch(top_of_stack);
375-
__sanitizer_finish_switch_fiber(private_asan_pointer, &mut prev.bottom, &mut prev.size);
377+
unsafe {
378+
__sanitizer_start_switch_fiber(private_asan_pointer_ref, prev.bottom, prev.size);
379+
super::wasmtime_fiber_switch(top_of_stack);
380+
__sanitizer_finish_switch_fiber(private_asan_pointer, &mut prev.bottom, &mut prev.size);
381+
}
376382
}
377383

378384
/// Hook for when a fiber first starts, used to configure ASAN.
379385
pub unsafe fn fiber_start_complete() -> PreviousStack {
380386
let mut ret = PreviousStack::default();
381-
__sanitizer_finish_switch_fiber(std::ptr::null_mut(), &mut ret.bottom, &mut ret.size);
387+
unsafe {
388+
__sanitizer_finish_switch_fiber(std::ptr::null_mut(), &mut ret.bottom, &mut ret.size);
389+
}
382390
ret
383391
}
384392

@@ -485,7 +493,9 @@ mod asan_disabled {
485493
_prev: &mut PreviousStack,
486494
) {
487495
assert!(super::SUPPORTED_ARCH);
488-
super::wasmtime_fiber_switch(top_of_stack);
496+
unsafe {
497+
super::wasmtime_fiber_switch(top_of_stack);
498+
}
489499
}
490500

491501
#[inline]

0 commit comments

Comments
 (0)