Skip to content

Commit a9a8344

Browse files
committed
enable unsafe_block_in_unsafe_fn lint
1 parent 55d5fad commit a9a8344

File tree

15 files changed

+279
-150
lines changed

15 files changed

+279
-150
lines changed

src/instructions/port.rs

Lines changed: 38 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,15 @@ impl PortRead for u8 {
1111
#[cfg(feature = "inline_asm")]
1212
{
1313
let value: u8;
14-
asm!("in al, dx", out("al") value, in("dx") port, options(nomem, nostack, preserves_flags));
14+
unsafe {
15+
asm!("in al, dx", out("al") value, in("dx") port, options(nomem, nostack, preserves_flags));
16+
}
1517
value
1618
}
1719
#[cfg(not(feature = "inline_asm"))]
18-
crate::asm::x86_64_asm_read_from_port_u8(port)
20+
unsafe {
21+
crate::asm::x86_64_asm_read_from_port_u8(port)
22+
}
1923
}
2024
}
2125

@@ -25,11 +29,15 @@ impl PortRead for u16 {
2529
#[cfg(feature = "inline_asm")]
2630
{
2731
let value: u16;
28-
asm!("in ax, dx", out("ax") value, in("dx") port, options(nomem, nostack, preserves_flags));
32+
unsafe {
33+
asm!("in ax, dx", out("ax") value, in("dx") port, options(nomem, nostack, preserves_flags));
34+
}
2935
value
3036
}
3137
#[cfg(not(feature = "inline_asm"))]
32-
crate::asm::x86_64_asm_read_from_port_u16(port)
38+
unsafe {
39+
crate::asm::x86_64_asm_read_from_port_u16(port)
40+
}
3341
}
3442
}
3543

@@ -39,44 +47,60 @@ impl PortRead for u32 {
3947
#[cfg(feature = "inline_asm")]
4048
{
4149
let value: u32;
42-
asm!("in eax, dx", out("eax") value, in("dx") port, options(nomem, nostack, preserves_flags));
50+
unsafe {
51+
asm!("in eax, dx", out("eax") value, in("dx") port, options(nomem, nostack, preserves_flags));
52+
}
4353
value
4454
}
4555
#[cfg(not(feature = "inline_asm"))]
46-
crate::asm::x86_64_asm_read_from_port_u32(port)
56+
unsafe {
57+
crate::asm::x86_64_asm_read_from_port_u32(port)
58+
}
4759
}
4860
}
4961

5062
impl PortWrite for u8 {
5163
#[inline]
5264
unsafe fn write_to_port(port: u16, value: u8) {
5365
#[cfg(feature = "inline_asm")]
54-
asm!("out dx, al", in("dx") port, in("al") value, options(nomem, nostack, preserves_flags));
66+
unsafe {
67+
asm!("out dx, al", in("dx") port, in("al") value, options(nomem, nostack, preserves_flags));
68+
}
5569

5670
#[cfg(not(feature = "inline_asm"))]
57-
crate::asm::x86_64_asm_write_to_port_u8(port, value);
71+
unsafe {
72+
crate::asm::x86_64_asm_write_to_port_u8(port, value);
73+
}
5874
}
5975
}
6076

6177
impl PortWrite for u16 {
6278
#[inline]
6379
unsafe fn write_to_port(port: u16, value: u16) {
6480
#[cfg(feature = "inline_asm")]
65-
asm!("out dx, ax", in("dx") port, in("ax") value, options(nomem, nostack, preserves_flags));
81+
unsafe {
82+
asm!("out dx, ax", in("dx") port, in("ax") value, options(nomem, nostack, preserves_flags));
83+
}
6684

6785
#[cfg(not(feature = "inline_asm"))]
68-
crate::asm::x86_64_asm_write_to_port_u16(port, value);
86+
unsafe {
87+
crate::asm::x86_64_asm_write_to_port_u16(port, value);
88+
}
6989
}
7090
}
7191

7292
impl PortWrite for u32 {
7393
#[inline]
7494
unsafe fn write_to_port(port: u16, value: u32) {
7595
#[cfg(feature = "inline_asm")]
76-
asm!("out dx, eax", in("dx") port, in("eax") value, options(nomem, nostack, preserves_flags));
96+
unsafe {
97+
asm!("out dx, eax", in("dx") port, in("eax") value, options(nomem, nostack, preserves_flags));
98+
}
7799

78100
#[cfg(not(feature = "inline_asm"))]
79-
crate::asm::x86_64_asm_write_to_port_u32(port, value);
101+
unsafe {
102+
crate::asm::x86_64_asm_write_to_port_u32(port, value);
103+
}
80104
}
81105
}
82106

@@ -162,7 +186,7 @@ impl<T: PortRead, A: PortReadAccess> PortGeneric<T, A> {
162186
/// safety.
163187
#[inline]
164188
pub unsafe fn read(&mut self) -> T {
165-
T::read_from_port(self.port)
189+
unsafe { T::read_from_port(self.port) }
166190
}
167191
}
168192

@@ -175,7 +199,7 @@ impl<T: PortWrite, A: PortWriteAccess> PortGeneric<T, A> {
175199
/// safety.
176200
#[inline]
177201
pub unsafe fn write(&mut self, value: T) {
178-
T::write_to_port(self.port, value)
202+
unsafe { T::write_to_port(self.port, value) }
179203
}
180204
}
181205

src/instructions/segmentation.rs

Lines changed: 42 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,14 @@ macro_rules! segment_impl {
3131

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

3638
#[cfg(not(feature = "inline_asm"))]
37-
crate::asm::$asm_load(sel.0);
39+
unsafe{
40+
crate::asm::$asm_load(sel.0);
41+
}
3842
}
3943
}
4044
};
@@ -59,10 +63,14 @@ macro_rules! segment64_impl {
5963

6064
unsafe fn write_base(base: VirtAddr) {
6165
#[cfg(feature = "inline_asm")]
62-
asm!(concat!("wr", $name, "base {}"), in(reg) base.as_u64(), options(nostack, preserves_flags));
66+
unsafe{
67+
asm!(concat!("wr", $name, "base {}"), in(reg) base.as_u64(), options(nostack, preserves_flags));
68+
}
6369

6470
#[cfg(not(feature = "inline_asm"))]
65-
crate::asm::$asm_wr(base.as_u64());
71+
unsafe{
72+
crate::asm::$asm_wr(base.as_u64());
73+
}
6674
}
6775
}
6876
};
@@ -81,19 +89,23 @@ impl Segment for CS {
8189
/// for 64-bit far calls/jumps in long-mode, AMD does not.
8290
unsafe fn set_reg(sel: SegmentSelector) {
8391
#[cfg(feature = "inline_asm")]
84-
asm!(
85-
"push {sel}",
86-
"lea {tmp}, [1f + rip]",
87-
"push {tmp}",
88-
"retfq",
89-
"1:",
90-
sel = in(reg) u64::from(sel.0),
91-
tmp = lateout(reg) _,
92-
options(preserves_flags),
93-
);
92+
unsafe {
93+
asm!(
94+
"push {sel}",
95+
"lea {tmp}, [1f + rip]",
96+
"push {tmp}",
97+
"retfq",
98+
"1:",
99+
sel = in(reg) u64::from(sel.0),
100+
tmp = lateout(reg) _,
101+
options(preserves_flags),
102+
);
103+
}
94104

95105
#[cfg(not(feature = "inline_asm"))]
96-
crate::asm::x86_64_asm_set_cs(u64::from(sel.0));
106+
unsafe {
107+
crate::asm::x86_64_asm_set_cs(u64::from(sel.0));
108+
}
97109
}
98110
}
99111

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

119133
#[cfg(not(feature = "inline_asm"))]
120-
crate::asm::x86_64_asm_swapgs();
134+
unsafe {
135+
crate::asm::x86_64_asm_swapgs();
136+
}
121137
}
122138
}
123139

@@ -126,49 +142,49 @@ impl GS {
126142
#[allow(clippy::missing_safety_doc)]
127143
#[inline]
128144
pub unsafe fn set_cs(sel: SegmentSelector) {
129-
CS::set_reg(sel)
145+
unsafe { CS::set_reg(sel) }
130146
}
131147
/// Alias for [`SS::set_reg()`]
132148
#[deprecated(since = "0.14.4", note = "use `SS::set_reg()` instead")]
133149
#[allow(clippy::missing_safety_doc)]
134150
#[inline]
135151
pub unsafe fn load_ss(sel: SegmentSelector) {
136-
SS::set_reg(sel)
152+
unsafe { SS::set_reg(sel) }
137153
}
138154
/// Alias for [`DS::set_reg()`]
139155
#[deprecated(since = "0.14.4", note = "use `DS::set_reg()` instead")]
140156
#[allow(clippy::missing_safety_doc)]
141157
#[inline]
142158
pub unsafe fn load_ds(sel: SegmentSelector) {
143-
DS::set_reg(sel)
159+
unsafe { DS::set_reg(sel) }
144160
}
145161
/// Alias for [`ES::set_reg()`]
146162
#[deprecated(since = "0.14.4", note = "use `ES::set_reg()` instead")]
147163
#[allow(clippy::missing_safety_doc)]
148164
#[inline]
149165
pub unsafe fn load_es(sel: SegmentSelector) {
150-
ES::set_reg(sel)
166+
unsafe { ES::set_reg(sel) }
151167
}
152168
/// Alias for [`FS::set_reg()`]
153169
#[deprecated(since = "0.14.4", note = "use `FS::set_reg()` instead")]
154170
#[allow(clippy::missing_safety_doc)]
155171
#[inline]
156172
pub unsafe fn load_fs(sel: SegmentSelector) {
157-
FS::set_reg(sel)
173+
unsafe { FS::set_reg(sel) }
158174
}
159175
/// Alias for [`GS::set_reg()`]
160176
#[deprecated(since = "0.14.4", note = "use `GS::set_reg()` instead")]
161177
#[allow(clippy::missing_safety_doc)]
162178
#[inline]
163179
pub unsafe fn load_gs(sel: SegmentSelector) {
164-
GS::set_reg(sel)
180+
unsafe { GS::set_reg(sel) }
165181
}
166182
/// Alias for [`GS::swap()`]
167183
#[deprecated(since = "0.14.4", note = "use `GS::swap()` instead")]
168184
#[allow(clippy::missing_safety_doc)]
169185
#[inline]
170186
pub unsafe fn swap_gs() {
171-
GS::swap()
187+
unsafe { GS::swap() }
172188
}
173189
/// Alias for [`CS::get_reg()`]
174190
#[deprecated(since = "0.14.4", note = "use `CS::get_reg()` instead")]
@@ -184,7 +200,7 @@ pub fn cs() -> SegmentSelector {
184200
#[allow(clippy::missing_safety_doc)]
185201
#[inline]
186202
pub unsafe fn wrfsbase(val: u64) {
187-
FS::write_base(VirtAddr::new(val))
203+
unsafe { FS::write_base(VirtAddr::new(val)) }
188204
}
189205
/// Alias for [`FS::read_base()`]
190206
#[deprecated(since = "0.14.4", note = "use `FS::read_base()` instead")]
@@ -200,7 +216,7 @@ pub unsafe fn rdfsbase() -> u64 {
200216
#[allow(clippy::missing_safety_doc)]
201217
#[inline]
202218
pub unsafe fn wrgsbase(val: u64) {
203-
GS::write_base(VirtAddr::new(val))
219+
unsafe { GS::write_base(VirtAddr::new(val)) }
204220
}
205221
/// Alias for [`GS::read_base()`]
206222
#[deprecated(since = "0.14.4", note = "use `GS::read_base()` instead")]

src/instructions/tables.rs

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,14 @@ pub use crate::structures::DescriptorTablePointer;
1919
#[inline]
2020
pub unsafe fn lgdt(gdt: &DescriptorTablePointer) {
2121
#[cfg(feature = "inline_asm")]
22-
asm!("lgdt [{}]", in(reg) gdt, options(readonly, nostack, preserves_flags));
22+
unsafe {
23+
asm!("lgdt [{}]", in(reg) gdt, options(readonly, nostack, preserves_flags));
24+
}
2325

2426
#[cfg(not(feature = "inline_asm"))]
25-
crate::asm::x86_64_asm_lgdt(gdt as *const _);
27+
unsafe {
28+
crate::asm::x86_64_asm_lgdt(gdt as *const _);
29+
}
2630
}
2731

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

4450
#[cfg(not(feature = "inline_asm"))]
45-
crate::asm::x86_64_asm_lidt(idt as *const _);
51+
unsafe {
52+
crate::asm::x86_64_asm_lidt(idt as *const _);
53+
}
4654
}
4755

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

94104
#[cfg(not(feature = "inline_asm"))]
95-
crate::asm::x86_64_asm_ltr(sel.0);
105+
unsafe {
106+
crate::asm::x86_64_asm_ltr(sel.0);
107+
}
96108
}

src/instructions/tlb.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,12 @@ pub unsafe fn flush_pcid(command: InvPicdCommand) {
9797
}
9898

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

102104
#[cfg(not(feature = "inline_asm"))]
103-
crate::asm::x86_64_asm_invpcid(kind, &desc as *const _ as u64);
105+
unsafe {
106+
crate::asm::x86_64_asm_invpcid(kind, &desc as *const _ as u64);
107+
}
104108
}

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#![cfg_attr(feature = "doc_cfg", feature(doc_cfg))]
1111
#![warn(missing_docs)]
1212
#![deny(missing_debug_implementations)]
13+
#![deny(unsafe_op_in_unsafe_fn)]
1314

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

0 commit comments

Comments
 (0)