Skip to content

Commit e94d694

Browse files
authored
Merge pull request #328 from Freax13/unsafe_block_in_unsafe_fn
enable `unsafe_block_in_unsafe_fn` lint
2 parents 575de31 + 2a719f8 commit e94d694

15 files changed

+279
-156
lines changed

src/instructions/port.rs

+38-14
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,15 @@ impl PortRead for u8 {
1313
#[cfg(feature = "inline_asm")]
1414
{
1515
let value: u8;
16-
asm!("in al, dx", out("al") value, in("dx") port, options(nomem, nostack, preserves_flags));
16+
unsafe {
17+
asm!("in al, dx", out("al") value, in("dx") port, options(nomem, nostack, preserves_flags));
18+
}
1719
value
1820
}
1921
#[cfg(not(feature = "inline_asm"))]
20-
crate::asm::x86_64_asm_read_from_port_u8(port)
22+
unsafe {
23+
crate::asm::x86_64_asm_read_from_port_u8(port)
24+
}
2125
}
2226
}
2327

@@ -27,11 +31,15 @@ impl PortRead for u16 {
2731
#[cfg(feature = "inline_asm")]
2832
{
2933
let value: u16;
30-
asm!("in ax, dx", out("ax") value, in("dx") port, options(nomem, nostack, preserves_flags));
34+
unsafe {
35+
asm!("in ax, dx", out("ax") value, in("dx") port, options(nomem, nostack, preserves_flags));
36+
}
3137
value
3238
}
3339
#[cfg(not(feature = "inline_asm"))]
34-
crate::asm::x86_64_asm_read_from_port_u16(port)
40+
unsafe {
41+
crate::asm::x86_64_asm_read_from_port_u16(port)
42+
}
3543
}
3644
}
3745

@@ -41,44 +49,60 @@ impl PortRead for u32 {
4149
#[cfg(feature = "inline_asm")]
4250
{
4351
let value: u32;
44-
asm!("in eax, dx", out("eax") value, in("dx") port, options(nomem, nostack, preserves_flags));
52+
unsafe {
53+
asm!("in eax, dx", out("eax") value, in("dx") port, options(nomem, nostack, preserves_flags));
54+
}
4555
value
4656
}
4757
#[cfg(not(feature = "inline_asm"))]
48-
crate::asm::x86_64_asm_read_from_port_u32(port)
58+
unsafe {
59+
crate::asm::x86_64_asm_read_from_port_u32(port)
60+
}
4961
}
5062
}
5163

5264
impl PortWrite for u8 {
5365
#[inline]
5466
unsafe fn write_to_port(port: u16, value: u8) {
5567
#[cfg(feature = "inline_asm")]
56-
asm!("out dx, al", in("dx") port, in("al") value, options(nomem, nostack, preserves_flags));
68+
unsafe {
69+
asm!("out dx, al", in("dx") port, in("al") value, options(nomem, nostack, preserves_flags));
70+
}
5771

5872
#[cfg(not(feature = "inline_asm"))]
59-
crate::asm::x86_64_asm_write_to_port_u8(port, value);
73+
unsafe {
74+
crate::asm::x86_64_asm_write_to_port_u8(port, value);
75+
}
6076
}
6177
}
6278

6379
impl PortWrite for u16 {
6480
#[inline]
6581
unsafe fn write_to_port(port: u16, value: u16) {
6682
#[cfg(feature = "inline_asm")]
67-
asm!("out dx, ax", in("dx") port, in("ax") value, options(nomem, nostack, preserves_flags));
83+
unsafe {
84+
asm!("out dx, ax", in("dx") port, in("ax") value, options(nomem, nostack, preserves_flags));
85+
}
6886

6987
#[cfg(not(feature = "inline_asm"))]
70-
crate::asm::x86_64_asm_write_to_port_u16(port, value);
88+
unsafe {
89+
crate::asm::x86_64_asm_write_to_port_u16(port, value);
90+
}
7191
}
7292
}
7393

7494
impl PortWrite for u32 {
7595
#[inline]
7696
unsafe fn write_to_port(port: u16, value: u32) {
7797
#[cfg(feature = "inline_asm")]
78-
asm!("out dx, eax", in("dx") port, in("eax") value, options(nomem, nostack, preserves_flags));
98+
unsafe {
99+
asm!("out dx, eax", in("dx") port, in("eax") value, options(nomem, nostack, preserves_flags));
100+
}
79101

80102
#[cfg(not(feature = "inline_asm"))]
81-
crate::asm::x86_64_asm_write_to_port_u32(port, value);
103+
unsafe {
104+
crate::asm::x86_64_asm_write_to_port_u32(port, value);
105+
}
82106
}
83107
}
84108

@@ -164,7 +188,7 @@ impl<T: PortRead, A: PortReadAccess> PortGeneric<T, A> {
164188
/// safety.
165189
#[inline]
166190
pub unsafe fn read(&mut self) -> T {
167-
T::read_from_port(self.port)
191+
unsafe { T::read_from_port(self.port) }
168192
}
169193
}
170194

@@ -177,7 +201,7 @@ impl<T: PortWrite, A: PortWriteAccess> PortGeneric<T, A> {
177201
/// safety.
178202
#[inline]
179203
pub unsafe fn write(&mut self, value: T) {
180-
T::write_to_port(self.port, value)
204+
unsafe { T::write_to_port(self.port, value) }
181205
}
182206
}
183207

src/instructions/segmentation.rs

+42-26
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,14 @@ macro_rules! segment_impl {
3333

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

3840
#[cfg(not(feature = "inline_asm"))]
39-
crate::asm::$asm_load(sel.0);
41+
unsafe{
42+
crate::asm::$asm_load(sel.0);
43+
}
4044
}
4145
}
4246
};
@@ -61,10 +65,14 @@ macro_rules! segment64_impl {
6165

6266
unsafe fn write_base(base: VirtAddr) {
6367
#[cfg(feature = "inline_asm")]
64-
asm!(concat!("wr", $name, "base {}"), in(reg) base.as_u64(), options(nostack, preserves_flags));
68+
unsafe{
69+
asm!(concat!("wr", $name, "base {}"), in(reg) base.as_u64(), options(nostack, preserves_flags));
70+
}
6571

6672
#[cfg(not(feature = "inline_asm"))]
67-
crate::asm::$asm_wr(base.as_u64());
73+
unsafe{
74+
crate::asm::$asm_wr(base.as_u64());
75+
}
6876
}
6977
}
7078
};
@@ -83,19 +91,23 @@ impl Segment for CS {
8391
/// for 64-bit far calls/jumps in long-mode, AMD does not.
8492
unsafe fn set_reg(sel: SegmentSelector) {
8593
#[cfg(feature = "inline_asm")]
86-
asm!(
87-
"push {sel}",
88-
"lea {tmp}, [1f + rip]",
89-
"push {tmp}",
90-
"retfq",
91-
"1:",
92-
sel = in(reg) u64::from(sel.0),
93-
tmp = lateout(reg) _,
94-
options(preserves_flags),
95-
);
94+
unsafe {
95+
asm!(
96+
"push {sel}",
97+
"lea {tmp}, [1f + rip]",
98+
"push {tmp}",
99+
"retfq",
100+
"1:",
101+
sel = in(reg) u64::from(sel.0),
102+
tmp = lateout(reg) _,
103+
options(preserves_flags),
104+
);
105+
}
96106

97107
#[cfg(not(feature = "inline_asm"))]
98-
crate::asm::x86_64_asm_set_cs(u64::from(sel.0));
108+
unsafe {
109+
crate::asm::x86_64_asm_set_cs(u64::from(sel.0));
110+
}
99111
}
100112
}
101113

@@ -116,10 +128,14 @@ impl GS {
116128
/// swap operation cannot lead to undefined behavior.
117129
pub unsafe fn swap() {
118130
#[cfg(feature = "inline_asm")]
119-
asm!("swapgs", options(nostack, preserves_flags));
131+
unsafe {
132+
asm!("swapgs", options(nostack, preserves_flags));
133+
}
120134

121135
#[cfg(not(feature = "inline_asm"))]
122-
crate::asm::x86_64_asm_swapgs();
136+
unsafe {
137+
crate::asm::x86_64_asm_swapgs();
138+
}
123139
}
124140
}
125141

@@ -128,49 +144,49 @@ impl GS {
128144
#[allow(clippy::missing_safety_doc)]
129145
#[inline]
130146
pub unsafe fn set_cs(sel: SegmentSelector) {
131-
CS::set_reg(sel)
147+
unsafe { CS::set_reg(sel) }
132148
}
133149
/// Alias for [`SS::set_reg()`]
134150
#[deprecated(since = "0.14.4", note = "use `SS::set_reg()` instead")]
135151
#[allow(clippy::missing_safety_doc)]
136152
#[inline]
137153
pub unsafe fn load_ss(sel: SegmentSelector) {
138-
SS::set_reg(sel)
154+
unsafe { SS::set_reg(sel) }
139155
}
140156
/// Alias for [`DS::set_reg()`]
141157
#[deprecated(since = "0.14.4", note = "use `DS::set_reg()` instead")]
142158
#[allow(clippy::missing_safety_doc)]
143159
#[inline]
144160
pub unsafe fn load_ds(sel: SegmentSelector) {
145-
DS::set_reg(sel)
161+
unsafe { DS::set_reg(sel) }
146162
}
147163
/// Alias for [`ES::set_reg()`]
148164
#[deprecated(since = "0.14.4", note = "use `ES::set_reg()` instead")]
149165
#[allow(clippy::missing_safety_doc)]
150166
#[inline]
151167
pub unsafe fn load_es(sel: SegmentSelector) {
152-
ES::set_reg(sel)
168+
unsafe { ES::set_reg(sel) }
153169
}
154170
/// Alias for [`FS::set_reg()`]
155171
#[deprecated(since = "0.14.4", note = "use `FS::set_reg()` instead")]
156172
#[allow(clippy::missing_safety_doc)]
157173
#[inline]
158174
pub unsafe fn load_fs(sel: SegmentSelector) {
159-
FS::set_reg(sel)
175+
unsafe { FS::set_reg(sel) }
160176
}
161177
/// Alias for [`GS::set_reg()`]
162178
#[deprecated(since = "0.14.4", note = "use `GS::set_reg()` instead")]
163179
#[allow(clippy::missing_safety_doc)]
164180
#[inline]
165181
pub unsafe fn load_gs(sel: SegmentSelector) {
166-
GS::set_reg(sel)
182+
unsafe { GS::set_reg(sel) }
167183
}
168184
/// Alias for [`GS::swap()`]
169185
#[deprecated(since = "0.14.4", note = "use `GS::swap()` instead")]
170186
#[allow(clippy::missing_safety_doc)]
171187
#[inline]
172188
pub unsafe fn swap_gs() {
173-
GS::swap()
189+
unsafe { GS::swap() }
174190
}
175191
/// Alias for [`CS::get_reg()`]
176192
#[deprecated(since = "0.14.4", note = "use `CS::get_reg()` instead")]
@@ -186,7 +202,7 @@ pub fn cs() -> SegmentSelector {
186202
#[allow(clippy::missing_safety_doc)]
187203
#[inline]
188204
pub unsafe fn wrfsbase(val: u64) {
189-
FS::write_base(VirtAddr::new(val))
205+
unsafe { FS::write_base(VirtAddr::new(val)) }
190206
}
191207
/// Alias for [`FS::read_base()`]
192208
#[deprecated(since = "0.14.4", note = "use `FS::read_base()` instead")]
@@ -202,7 +218,7 @@ pub unsafe fn rdfsbase() -> u64 {
202218
#[allow(clippy::missing_safety_doc)]
203219
#[inline]
204220
pub unsafe fn wrgsbase(val: u64) {
205-
GS::write_base(VirtAddr::new(val))
221+
unsafe { GS::write_base(VirtAddr::new(val)) }
206222
}
207223
/// Alias for [`GS::read_base()`]
208224
#[deprecated(since = "0.14.4", note = "use `GS::read_base()` instead")]

src/instructions/tables.rs

+18-6
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,14 @@ pub use crate::structures::DescriptorTablePointer;
2121
#[inline]
2222
pub unsafe fn lgdt(gdt: &DescriptorTablePointer) {
2323
#[cfg(feature = "inline_asm")]
24-
asm!("lgdt [{}]", in(reg) gdt, options(readonly, nostack, preserves_flags));
24+
unsafe {
25+
asm!("lgdt [{}]", in(reg) gdt, options(readonly, nostack, preserves_flags));
26+
}
2527

2628
#[cfg(not(feature = "inline_asm"))]
27-
crate::asm::x86_64_asm_lgdt(gdt as *const _);
29+
unsafe {
30+
crate::asm::x86_64_asm_lgdt(gdt as *const _);
31+
}
2832
}
2933

3034
/// Load an IDT.
@@ -41,10 +45,14 @@ pub unsafe fn lgdt(gdt: &DescriptorTablePointer) {
4145
#[inline]
4246
pub unsafe fn lidt(idt: &DescriptorTablePointer) {
4347
#[cfg(feature = "inline_asm")]
44-
asm!("lidt [{}]", in(reg) idt, options(readonly, nostack, preserves_flags));
48+
unsafe {
49+
asm!("lidt [{}]", in(reg) idt, options(readonly, nostack, preserves_flags));
50+
}
4551

4652
#[cfg(not(feature = "inline_asm"))]
47-
crate::asm::x86_64_asm_lidt(idt as *const _);
53+
unsafe {
54+
crate::asm::x86_64_asm_lidt(idt as *const _);
55+
}
4856
}
4957

5058
/// Get the address of the current GDT.
@@ -91,8 +99,12 @@ pub fn sidt() -> DescriptorTablePointer {
9199
#[inline]
92100
pub unsafe fn load_tss(sel: SegmentSelector) {
93101
#[cfg(feature = "inline_asm")]
94-
asm!("ltr {0:x}", in(reg) sel.0, options(nomem, nostack, preserves_flags));
102+
unsafe {
103+
asm!("ltr {0:x}", in(reg) sel.0, options(nomem, nostack, preserves_flags));
104+
}
95105

96106
#[cfg(not(feature = "inline_asm"))]
97-
crate::asm::x86_64_asm_ltr(sel.0);
107+
unsafe {
108+
crate::asm::x86_64_asm_ltr(sel.0);
109+
}
98110
}

src/instructions/tlb.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,12 @@ pub unsafe fn flush_pcid(command: InvPicdCommand) {
9999
}
100100

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

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

src/lib.rs

+1
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)