Skip to content

enable unsafe_block_in_unsafe_fn lint #328

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 38 additions & 14 deletions src/instructions/port.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,15 @@ impl PortRead for u8 {
#[cfg(feature = "inline_asm")]
{
let value: u8;
asm!("in al, dx", out("al") value, in("dx") port, options(nomem, nostack, preserves_flags));
unsafe {
asm!("in al, dx", out("al") value, in("dx") port, options(nomem, nostack, preserves_flags));
}
value
}
#[cfg(not(feature = "inline_asm"))]
crate::asm::x86_64_asm_read_from_port_u8(port)
unsafe {
crate::asm::x86_64_asm_read_from_port_u8(port)
}
}
}

Expand All @@ -25,11 +29,15 @@ impl PortRead for u16 {
#[cfg(feature = "inline_asm")]
{
let value: u16;
asm!("in ax, dx", out("ax") value, in("dx") port, options(nomem, nostack, preserves_flags));
unsafe {
asm!("in ax, dx", out("ax") value, in("dx") port, options(nomem, nostack, preserves_flags));
}
value
}
#[cfg(not(feature = "inline_asm"))]
crate::asm::x86_64_asm_read_from_port_u16(port)
unsafe {
crate::asm::x86_64_asm_read_from_port_u16(port)
}
}
}

Expand All @@ -39,44 +47,60 @@ impl PortRead for u32 {
#[cfg(feature = "inline_asm")]
{
let value: u32;
asm!("in eax, dx", out("eax") value, in("dx") port, options(nomem, nostack, preserves_flags));
unsafe {
asm!("in eax, dx", out("eax") value, in("dx") port, options(nomem, nostack, preserves_flags));
}
value
}
#[cfg(not(feature = "inline_asm"))]
crate::asm::x86_64_asm_read_from_port_u32(port)
unsafe {
crate::asm::x86_64_asm_read_from_port_u32(port)
}
}
}

impl PortWrite for u8 {
#[inline]
unsafe fn write_to_port(port: u16, value: u8) {
#[cfg(feature = "inline_asm")]
asm!("out dx, al", in("dx") port, in("al") value, options(nomem, nostack, preserves_flags));
unsafe {
asm!("out dx, al", in("dx") port, in("al") value, options(nomem, nostack, preserves_flags));
}

#[cfg(not(feature = "inline_asm"))]
crate::asm::x86_64_asm_write_to_port_u8(port, value);
unsafe {
crate::asm::x86_64_asm_write_to_port_u8(port, value);
}
}
}

impl PortWrite for u16 {
#[inline]
unsafe fn write_to_port(port: u16, value: u16) {
#[cfg(feature = "inline_asm")]
asm!("out dx, ax", in("dx") port, in("ax") value, options(nomem, nostack, preserves_flags));
unsafe {
asm!("out dx, ax", in("dx") port, in("ax") value, options(nomem, nostack, preserves_flags));
}

#[cfg(not(feature = "inline_asm"))]
crate::asm::x86_64_asm_write_to_port_u16(port, value);
unsafe {
crate::asm::x86_64_asm_write_to_port_u16(port, value);
}
}
}

impl PortWrite for u32 {
#[inline]
unsafe fn write_to_port(port: u16, value: u32) {
#[cfg(feature = "inline_asm")]
asm!("out dx, eax", in("dx") port, in("eax") value, options(nomem, nostack, preserves_flags));
unsafe {
asm!("out dx, eax", in("dx") port, in("eax") value, options(nomem, nostack, preserves_flags));
}

#[cfg(not(feature = "inline_asm"))]
crate::asm::x86_64_asm_write_to_port_u32(port, value);
unsafe {
crate::asm::x86_64_asm_write_to_port_u32(port, value);
}
}
}

Expand Down Expand Up @@ -162,7 +186,7 @@ impl<T: PortRead, A: PortReadAccess> PortGeneric<T, A> {
/// safety.
#[inline]
pub unsafe fn read(&mut self) -> T {
T::read_from_port(self.port)
unsafe { T::read_from_port(self.port) }
}
}

Expand All @@ -175,7 +199,7 @@ impl<T: PortWrite, A: PortWriteAccess> PortGeneric<T, A> {
/// safety.
#[inline]
pub unsafe fn write(&mut self, value: T) {
T::write_to_port(self.port, value)
unsafe { T::write_to_port(self.port, value) }
}
}

Expand Down
68 changes: 42 additions & 26 deletions src/instructions/segmentation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,14 @@ macro_rules! segment_impl {

unsafe fn set_reg(sel: SegmentSelector) {
#[cfg(feature = "inline_asm")]
asm!(concat!("mov ", $name, ", {0:x}"), in(reg) sel.0, options(nostack, preserves_flags));
unsafe {
asm!(concat!("mov ", $name, ", {0:x}"), in(reg) sel.0, options(nostack, preserves_flags));
}

#[cfg(not(feature = "inline_asm"))]
crate::asm::$asm_load(sel.0);
unsafe{
crate::asm::$asm_load(sel.0);
}
}
}
};
Expand All @@ -59,10 +63,14 @@ macro_rules! segment64_impl {

unsafe fn write_base(base: VirtAddr) {
#[cfg(feature = "inline_asm")]
asm!(concat!("wr", $name, "base {}"), in(reg) base.as_u64(), options(nostack, preserves_flags));
unsafe{
asm!(concat!("wr", $name, "base {}"), in(reg) base.as_u64(), options(nostack, preserves_flags));
}

#[cfg(not(feature = "inline_asm"))]
crate::asm::$asm_wr(base.as_u64());
unsafe{
crate::asm::$asm_wr(base.as_u64());
}
}
}
};
Expand All @@ -81,19 +89,23 @@ impl Segment for CS {
/// for 64-bit far calls/jumps in long-mode, AMD does not.
unsafe fn set_reg(sel: SegmentSelector) {
#[cfg(feature = "inline_asm")]
asm!(
"push {sel}",
"lea {tmp}, [1f + rip]",
"push {tmp}",
"retfq",
"1:",
sel = in(reg) u64::from(sel.0),
tmp = lateout(reg) _,
options(preserves_flags),
);
unsafe {
asm!(
"push {sel}",
"lea {tmp}, [1f + rip]",
"push {tmp}",
"retfq",
"1:",
sel = in(reg) u64::from(sel.0),
tmp = lateout(reg) _,
options(preserves_flags),
);
}

#[cfg(not(feature = "inline_asm"))]
crate::asm::x86_64_asm_set_cs(u64::from(sel.0));
unsafe {
crate::asm::x86_64_asm_set_cs(u64::from(sel.0));
}
}
}

Expand All @@ -114,10 +126,14 @@ impl GS {
/// swap operation cannot lead to undefined behavior.
pub unsafe fn swap() {
#[cfg(feature = "inline_asm")]
asm!("swapgs", options(nostack, preserves_flags));
unsafe {
asm!("swapgs", options(nostack, preserves_flags));
}

#[cfg(not(feature = "inline_asm"))]
crate::asm::x86_64_asm_swapgs();
unsafe {
crate::asm::x86_64_asm_swapgs();
}
}
}

Expand All @@ -126,49 +142,49 @@ impl GS {
#[allow(clippy::missing_safety_doc)]
#[inline]
pub unsafe fn set_cs(sel: SegmentSelector) {
CS::set_reg(sel)
unsafe { CS::set_reg(sel) }
}
/// Alias for [`SS::set_reg()`]
#[deprecated(since = "0.14.4", note = "use `SS::set_reg()` instead")]
#[allow(clippy::missing_safety_doc)]
#[inline]
pub unsafe fn load_ss(sel: SegmentSelector) {
SS::set_reg(sel)
unsafe { SS::set_reg(sel) }
}
/// Alias for [`DS::set_reg()`]
#[deprecated(since = "0.14.4", note = "use `DS::set_reg()` instead")]
#[allow(clippy::missing_safety_doc)]
#[inline]
pub unsafe fn load_ds(sel: SegmentSelector) {
DS::set_reg(sel)
unsafe { DS::set_reg(sel) }
}
/// Alias for [`ES::set_reg()`]
#[deprecated(since = "0.14.4", note = "use `ES::set_reg()` instead")]
#[allow(clippy::missing_safety_doc)]
#[inline]
pub unsafe fn load_es(sel: SegmentSelector) {
ES::set_reg(sel)
unsafe { ES::set_reg(sel) }
}
/// Alias for [`FS::set_reg()`]
#[deprecated(since = "0.14.4", note = "use `FS::set_reg()` instead")]
#[allow(clippy::missing_safety_doc)]
#[inline]
pub unsafe fn load_fs(sel: SegmentSelector) {
FS::set_reg(sel)
unsafe { FS::set_reg(sel) }
}
/// Alias for [`GS::set_reg()`]
#[deprecated(since = "0.14.4", note = "use `GS::set_reg()` instead")]
#[allow(clippy::missing_safety_doc)]
#[inline]
pub unsafe fn load_gs(sel: SegmentSelector) {
GS::set_reg(sel)
unsafe { GS::set_reg(sel) }
}
/// Alias for [`GS::swap()`]
#[deprecated(since = "0.14.4", note = "use `GS::swap()` instead")]
#[allow(clippy::missing_safety_doc)]
#[inline]
pub unsafe fn swap_gs() {
GS::swap()
unsafe { GS::swap() }
}
/// Alias for [`CS::get_reg()`]
#[deprecated(since = "0.14.4", note = "use `CS::get_reg()` instead")]
Expand All @@ -184,7 +200,7 @@ pub fn cs() -> SegmentSelector {
#[allow(clippy::missing_safety_doc)]
#[inline]
pub unsafe fn wrfsbase(val: u64) {
FS::write_base(VirtAddr::new(val))
unsafe { FS::write_base(VirtAddr::new(val)) }
}
/// Alias for [`FS::read_base()`]
#[deprecated(since = "0.14.4", note = "use `FS::read_base()` instead")]
Expand All @@ -200,7 +216,7 @@ pub unsafe fn rdfsbase() -> u64 {
#[allow(clippy::missing_safety_doc)]
#[inline]
pub unsafe fn wrgsbase(val: u64) {
GS::write_base(VirtAddr::new(val))
unsafe { GS::write_base(VirtAddr::new(val)) }
}
/// Alias for [`GS::read_base()`]
#[deprecated(since = "0.14.4", note = "use `GS::read_base()` instead")]
Expand Down
24 changes: 18 additions & 6 deletions src/instructions/tables.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,14 @@ pub use crate::structures::DescriptorTablePointer;
#[inline]
pub unsafe fn lgdt(gdt: &DescriptorTablePointer) {
#[cfg(feature = "inline_asm")]
asm!("lgdt [{}]", in(reg) gdt, options(readonly, nostack, preserves_flags));
unsafe {
asm!("lgdt [{}]", in(reg) gdt, options(readonly, nostack, preserves_flags));
}

#[cfg(not(feature = "inline_asm"))]
crate::asm::x86_64_asm_lgdt(gdt as *const _);
unsafe {
crate::asm::x86_64_asm_lgdt(gdt as *const _);
}
}

/// Load an IDT.
Expand All @@ -39,10 +43,14 @@ pub unsafe fn lgdt(gdt: &DescriptorTablePointer) {
#[inline]
pub unsafe fn lidt(idt: &DescriptorTablePointer) {
#[cfg(feature = "inline_asm")]
asm!("lidt [{}]", in(reg) idt, options(readonly, nostack, preserves_flags));
unsafe {
asm!("lidt [{}]", in(reg) idt, options(readonly, nostack, preserves_flags));
}

#[cfg(not(feature = "inline_asm"))]
crate::asm::x86_64_asm_lidt(idt as *const _);
unsafe {
crate::asm::x86_64_asm_lidt(idt as *const _);
}
}

/// Get the address of the current GDT.
Expand Down Expand Up @@ -89,8 +97,12 @@ pub fn sidt() -> DescriptorTablePointer {
#[inline]
pub unsafe fn load_tss(sel: SegmentSelector) {
#[cfg(feature = "inline_asm")]
asm!("ltr {0:x}", in(reg) sel.0, options(nomem, nostack, preserves_flags));
unsafe {
asm!("ltr {0:x}", in(reg) sel.0, options(nomem, nostack, preserves_flags));
}

#[cfg(not(feature = "inline_asm"))]
crate::asm::x86_64_asm_ltr(sel.0);
unsafe {
crate::asm::x86_64_asm_ltr(sel.0);
}
}
8 changes: 6 additions & 2 deletions src/instructions/tlb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,12 @@ pub unsafe fn flush_pcid(command: InvPicdCommand) {
}

#[cfg(feature = "inline_asm")]
asm!("invpcid {0}, [{1}]", in(reg) kind, in(reg) &desc, options(nostack, preserves_flags));
unsafe {
asm!("invpcid {0}, [{1}]", in(reg) kind, in(reg) &desc, options(nostack, preserves_flags));
}

#[cfg(not(feature = "inline_asm"))]
crate::asm::x86_64_asm_invpcid(kind, &desc as *const _ as u64);
unsafe {
crate::asm::x86_64_asm_invpcid(kind, &desc as *const _ as u64);
}
}
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#![cfg_attr(feature = "doc_cfg", feature(doc_cfg))]
#![warn(missing_docs)]
#![deny(missing_debug_implementations)]
#![deny(unsafe_op_in_unsafe_fn)]

pub use crate::addr::{align_down, align_up, PhysAddr, VirtAddr};

Expand Down
Loading