Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit b6bdfaa

Browse files
Merge pull request #1585 from rust-osdev/bishop-unsafe-op
uefi: Enable unsafe_op_in_unsafe_fn lint
2 parents 322fa3e + cbff720 commit b6bdfaa

File tree

17 files changed

+151
-115
lines changed

17 files changed

+151
-115
lines changed

‎uefi/src/allocator.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,11 @@ unsafe impl GlobalAlloc for Allocator {
9090
// write is appropriately aligned for a `*mut u8` pointer because
9191
// `align_ptr` is aligned, and alignments are always powers of two
9292
// (as enforced by the `Layout` type).
93-
let aligned_ptr = full_alloc_ptr.add(offset);
94-
(aligned_ptr.cast::<*mut u8>()).sub(1).write(full_alloc_ptr);
95-
aligned_ptr
93+
unsafe {
94+
let aligned_ptr = full_alloc_ptr.add(offset);
95+
(aligned_ptr.cast::<*mut u8>()).sub(1).write(full_alloc_ptr);
96+
aligned_ptr
97+
}
9698
} else {
9799
// The requested alignment is less than or equal to eight, and
98100
// `allocate_pool` always provides eight-byte alignment, so we can
@@ -108,13 +110,13 @@ unsafe impl GlobalAlloc for Allocator {
108110
if layout.align() > 8 {
109111
// Retrieve the pointer to the full allocation that was packed right
110112
// before the aligned allocation in `alloc`.
111-
ptr = (ptr as *const *mut u8).sub(1).read();
113+
ptr = unsafe{(ptr as *const *mut u8).sub(1).read()};
112114
}
113115

114116
// OK to unwrap: `ptr` is required to be a valid allocation by the trait API.
115117
let ptr = NonNull::new(ptr).unwrap();
116118

117119
// Warning: this will panic after exiting boot services.
118-
boot::free_pool(ptr).unwrap();
120+
unsafe{boot::free_pool(ptr)}.unwrap();
119121
}
120122
}

‎uefi/src/boot.rs

Lines changed: 57 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ pub unsafe fn raise_tpl(tpl: Tpl) -> TplGuard {
116116
let bt = unsafe { bt.as_ref() };
117117

118118
TplGuard {
119-
old_tpl: (bt.raise_tpl)(tpl),
119+
old_tpl: unsafe{(bt.raise_tpl)(tpl)},
120120
}
121121
}
122122

@@ -381,15 +381,17 @@ pub unsafe fn create_event(
381381

382382
// Safety: the argument types of the function pointers are defined
383383
// differently, but are compatible and can be safely transmuted.
384-
let notify_fn: Option<uefi_raw::table::boot::EventNotifyFn> = mem::transmute(notify_fn);
384+
let notify_fn: Option<uefi_raw::table::boot::EventNotifyFn> =
385+
unsafe { mem::transmute(notify_fn) };
385386

386387
let notify_ctx = opt_nonnull_to_ptr(notify_ctx);
387388

388389
// Now we're ready to call UEFI
389-
(bt.create_event)(event_ty, notify_tpl, notify_fn, notify_ctx, &mut event).to_result_with_val(
390-
// OK to unwrap: event is non-null for Status::SUCCESS.
391-
|| Event::from_ptr(event).unwrap(),
392-
)
390+
unsafe { (bt.create_event)(event_ty, notify_tpl, notify_fn, notify_ctx, &mut event) }
391+
.to_result_with_val(
392+
// OK to unwrap: event is non-null for Status::SUCCESS.
393+
|| unsafe { Event::from_ptr(event) }.unwrap(),
394+
)
393395
}
394396

395397
/// Creates an event in an event group.
@@ -451,19 +453,22 @@ pub unsafe fn create_event_ex(
451453

452454
// Safety: the argument types of the function pointers are defined
453455
// differently, but are compatible and can be safely transmuted.
454-
let notify_fn: Option<uefi_raw::table::boot::EventNotifyFn> = mem::transmute(notify_fn);
455-
456-
(bt.create_event_ex)(
457-
event_type,
458-
notify_tpl,
459-
notify_fn,
460-
opt_nonnull_to_ptr(notify_ctx),
461-
opt_nonnull_to_ptr(event_group),
462-
&mut event,
463-
)
456+
let notify_fn: Option<uefi_raw::table::boot::EventNotifyFn> =
457+
unsafe { mem::transmute(notify_fn) };
458+
459+
unsafe {
460+
(bt.create_event_ex)(
461+
event_type,
462+
notify_tpl,
463+
notify_fn,
464+
opt_nonnull_to_ptr(notify_ctx),
465+
opt_nonnull_to_ptr(event_group),
466+
&mut event,
467+
)
468+
}
464469
.to_result_with_val(
465470
// OK to unwrap: event is non-null for Status::SUCCESS.
466-
|| Event::from_ptr(event).unwrap(),
471+
|| unsafe{Event::from_ptr(event)}.unwrap(),
467472
)
468473
}
469474

@@ -696,13 +701,15 @@ pub unsafe fn install_protocol_interface(
696701
let bt = unsafe { bt.as_ref() };
697702

698703
let mut handle = Handle::opt_to_ptr(handle);
699-
((bt.install_protocol_interface)(
700-
&mut handle,
701-
protocol,
702-
InterfaceType::NATIVE_INTERFACE,
703-
interface,
704-
))
705-
.to_result_with_val(|| Handle::from_ptr(handle).unwrap())
704+
unsafe {
705+
(bt.install_protocol_interface)(
706+
&mut handle,
707+
protocol,
708+
InterfaceType::NATIVE_INTERFACE,
709+
interface,
710+
)
711+
}
712+
.to_result_with_val(|| unsafe { Handle::from_ptr(handle) }.unwrap())
706713
}
707714

708715
/// Reinstalls a protocol interface on a device handle. `old_interface` is replaced with `new_interface`.
@@ -730,8 +737,10 @@ pub unsafe fn reinstall_protocol_interface(
730737
let bt = boot_services_raw_panicking();
731738
let bt = unsafe { bt.as_ref() };
732739

733-
(bt.reinstall_protocol_interface)(handle.as_ptr(), protocol, old_interface, new_interface)
734-
.to_result()
740+
unsafe {
741+
(bt.reinstall_protocol_interface)(handle.as_ptr(), protocol, old_interface, new_interface)
742+
}
743+
.to_result()
735744
}
736745

737746
/// Removes a protocol interface from a device handle.
@@ -757,7 +766,7 @@ pub unsafe fn uninstall_protocol_interface(
757766
let bt = boot_services_raw_panicking();
758767
let bt = unsafe { bt.as_ref() };
759768

760-
(bt.uninstall_protocol_interface)(handle.as_ptr(), protocol, interface).to_result()
769+
unsafe{(bt.uninstall_protocol_interface)(handle.as_ptr(), protocol, interface).to_result()}
761770
}
762771

763772
/// Registers `event` to be signaled whenever a protocol interface is registered for
@@ -1035,19 +1044,21 @@ pub unsafe fn open_protocol<P: ProtocolPointer + ?Sized>(
10351044
let bt = unsafe { bt.as_ref() };
10361045

10371046
let mut interface = ptr::null_mut();
1038-
(bt.open_protocol)(
1039-
params.handle.as_ptr(),
1040-
&P::GUID,
1041-
&mut interface,
1042-
params.agent.as_ptr(),
1043-
Handle::opt_to_ptr(params.controller),
1044-
attributes as u32,
1045-
)
1047+
unsafe {
1048+
(bt.open_protocol)(
1049+
params.handle.as_ptr(),
1050+
&P::GUID,
1051+
&mut interface,
1052+
params.agent.as_ptr(),
1053+
Handle::opt_to_ptr(params.controller),
1054+
attributes as u32,
1055+
)
1056+
}
10461057
.to_result_with_val(|| {
10471058
let interface = if interface.is_null() {
10481059
None
10491060
} else {
1050-
NonNull::new(P::mut_ptr_from_ffi(interface))
1061+
NonNull::new(unsafe{P::mut_ptr_from_ffi(interface)})
10511062
};
10521063
ScopedProtocol {
10531064
interface,
@@ -1220,12 +1231,14 @@ pub unsafe fn exit(
12201231
let bt = boot_services_raw_panicking();
12211232
let bt = unsafe { bt.as_ref() };
12221233

1223-
(bt.exit)(
1224-
image_handle.as_ptr(),
1225-
exit_status,
1226-
exit_data_size,
1227-
exit_data.cast(),
1228-
)
1234+
unsafe {
1235+
(bt.exit)(
1236+
image_handle.as_ptr(),
1237+
exit_status,
1238+
exit_data_size,
1239+
exit_data.cast(),
1240+
)
1241+
}
12291242
}
12301243

12311244
/// Get the current memory map and exit boot services.
@@ -1241,7 +1254,7 @@ unsafe fn get_memory_map_and_exit_boot_services(buf: &mut [u8]) -> Result<Memory
12411254
// what boot services functions can be called. In UEFI 2.8 and earlier,
12421255
// only `get_memory_map` and `exit_boot_services` are allowed. Starting
12431256
// in UEFI 2.9 other memory allocation functions may also be called.
1244-
(bt.exit_boot_services)(image_handle().as_ptr(), memory_map.map_key.0)
1257+
unsafe{(bt.exit_boot_services)(image_handle().as_ptr(), memory_map.map_key.0)}
12451258
.to_result_with_val(|| memory_map)
12461259
}
12471260

@@ -1344,7 +1357,7 @@ pub unsafe fn install_configuration_table(
13441357
let bt = boot_services_raw_panicking();
13451358
let bt = unsafe { bt.as_ref() };
13461359

1347-
(bt.install_configuration_table)(guid_entry, table_ptr).to_result()
1360+
unsafe{(bt.install_configuration_table)(guid_entry, table_ptr)}.to_result()
13481361
}
13491362

13501363
/// Sets the watchdog timer.

‎uefi/src/data_types/strs.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -143,11 +143,11 @@ impl CStr8 {
143143
#[must_use]
144144
pub unsafe fn from_ptr<'ptr>(ptr: *const Char8) -> &'ptr Self {
145145
let mut len = 0;
146-
while *ptr.add(len) != NUL_8 {
146+
while unsafe{*ptr.add(len)} != NUL_8 {
147147
len += 1
148148
}
149149
let ptr = ptr.cast::<u8>();
150-
Self::from_bytes_with_nul_unchecked(slice::from_raw_parts(ptr, len + 1))
150+
unsafe{Self::from_bytes_with_nul_unchecked(slice::from_raw_parts(ptr, len + 1))}
151151
}
152152

153153
/// Creates a CStr8 reference from bytes.
@@ -171,7 +171,7 @@ impl CStr8 {
171171
/// null-terminated string, with no interior null bytes.
172172
#[must_use]
173173
pub const unsafe fn from_bytes_with_nul_unchecked(chars: &[u8]) -> &Self {
174-
&*(ptr::from_ref(chars) as *const Self)
174+
unsafe{&*(ptr::from_ref(chars) as *const Self)}
175175
}
176176

177177
/// Returns the inner pointer to this CStr8.
@@ -352,11 +352,11 @@ impl CStr16 {
352352
#[must_use]
353353
pub unsafe fn from_ptr<'ptr>(ptr: *const Char16) -> &'ptr Self {
354354
let mut len = 0;
355-
while *ptr.add(len) != NUL_16 {
355+
while unsafe{*ptr.add(len)} != NUL_16 {
356356
len += 1
357357
}
358358
let ptr = ptr.cast::<u16>();
359-
Self::from_u16_with_nul_unchecked(slice::from_raw_parts(ptr, len + 1))
359+
unsafe{Self::from_u16_with_nul_unchecked(slice::from_raw_parts(ptr, len + 1))}
360360
}
361361

362362
/// Creates a `&CStr16` from a u16 slice, stopping at the first nul character.
@@ -405,7 +405,7 @@ impl CStr16 {
405405
/// null-terminated string, with no interior null characters.
406406
#[must_use]
407407
pub const unsafe fn from_u16_with_nul_unchecked(codes: &[u16]) -> &Self {
408-
&*(ptr::from_ref(codes) as *const Self)
408+
unsafe{&*(ptr::from_ref(codes) as *const Self)}
409409
}
410410

411411
/// Creates a `&CStr16` from a [`Char16`] slice, stopping at the first nul character.
@@ -455,7 +455,7 @@ impl CStr16 {
455455
#[must_use]
456456
pub const unsafe fn from_char16_with_nul_unchecked(chars: &[Char16]) -> &Self {
457457
let ptr: *const [Char16] = chars;
458-
&*(ptr as *const Self)
458+
unsafe{&*(ptr as *const Self)}
459459
}
460460

461461
/// Convert a [`&str`] to a `&CStr16`, backed by a buffer.

‎uefi/src/helpers/logger.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ static LOGGER: Logger = Logger::new();
2929
/// disable() on exit from UEFI boot services.
3030
pub unsafe fn init() {
3131
// Connect the logger to stdout.
32-
system::with_stdout(|stdout| {
32+
system::with_stdout(|stdout| unsafe{
3333
LOGGER.set_output(stdout);
3434
});
3535

‎uefi/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,7 @@
224224
clippy::use_self,
225225
missing_debug_implementations,
226226
missing_docs,
227+
unsafe_op_in_unsafe_fn,
227228
unused
228229
)]
229230

‎uefi/src/proto/console/gop.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -589,7 +589,7 @@ impl FrameBuffer<'_> {
589589
#[inline]
590590
pub unsafe fn write_byte(&mut self, index: usize, value: u8) {
591591
debug_assert!(index < self.size, "Frame buffer accessed out of bounds");
592-
self.base.add(index).write_volatile(value)
592+
unsafe{self.base.add(index).write_volatile(value)}
593593
}
594594

595595
/// Read the i-th byte of the frame buffer
@@ -603,7 +603,7 @@ impl FrameBuffer<'_> {
603603
#[must_use]
604604
pub unsafe fn read_byte(&self, index: usize) -> u8 {
605605
debug_assert!(index < self.size, "Frame buffer accessed out of bounds");
606-
self.base.add(index).read_volatile()
606+
unsafe{self.base.add(index).read_volatile()}
607607
}
608608

609609
/// Write a value in the frame buffer, starting at the i-th byte
@@ -624,8 +624,10 @@ impl FrameBuffer<'_> {
624624
index.saturating_add(size_of::<T>()) <= self.size,
625625
"Frame buffer accessed out of bounds"
626626
);
627-
let ptr = self.base.add(index).cast::<T>();
628-
ptr.write_volatile(value)
627+
unsafe {
628+
let ptr = self.base.add(index).cast::<T>();
629+
ptr.write_volatile(value)
630+
}
629631
}
630632

631633
/// Read a value from the frame buffer, starting at the i-th byte
@@ -647,6 +649,6 @@ impl FrameBuffer<'_> {
647649
index.saturating_add(size_of::<T>()) <= self.size,
648650
"Frame buffer accessed out of bounds"
649651
);
650-
(self.base.add(index) as *const T).read_volatile()
652+
unsafe{(self.base.add(index) as *const T).read_volatile()}
651653
}
652654
}

‎uefi/src/proto/debug/mod.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ impl DebugSupport {
9898
}
9999

100100
// Safety: As we've validated the `processor_index`, this should always be safe
101-
(self.register_periodic_callback)(self, processor_index, callback).to_result()
101+
unsafe{(self.register_periodic_callback)(self, processor_index, callback)}.to_result()
102102
}
103103

104104
/// Registers a function to be called when a given processor exception occurs.
@@ -122,8 +122,10 @@ impl DebugSupport {
122122
}
123123

124124
// Safety: As we've validated the `processor_index`, this should always be safe
125-
(self.register_exception_callback)(self, processor_index, callback, exception_type)
126-
.to_result()
125+
unsafe {
126+
(self.register_exception_callback)(self, processor_index, callback, exception_type)
127+
}
128+
.to_result()
127129
}
128130

129131
/// Invalidates processor instruction cache for a memory range for a given `processor_index`.
@@ -144,7 +146,8 @@ impl DebugSupport {
144146

145147
// per the UEFI spec, this call should only return EFI_SUCCESS
146148
// Safety: As we've validated the `processor_index`, this should always be safe
147-
(self.invalidate_instruction_cache)(self, processor_index, start, length).to_result()
149+
unsafe { (self.invalidate_instruction_cache)(self, processor_index, start, length) }
150+
.to_result()
148151
}
149152
}
150153

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /