Skip to content

Commit 6f06d38

Browse files
committed
cpuid is safe since the stdarch sync, so remove unsafe from usages
1 parent 6adebf5 commit 6f06d38

7 files changed

Lines changed: 32 additions & 33 deletions

File tree

library/compiler-builtins/libm/src/math/arch/x86/detect.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ fn load_x86_features() -> Flags {
5757
// (in that order)
5858
let mut vendor_id = [0u8; 12];
5959
let max_basic_leaf;
60-
unsafe {
60+
{
6161
let CpuidResult { eax, ebx, ecx, edx } = __cpuid(0);
6262
max_basic_leaf = eax;
6363
vendor_id[0..4].copy_from_slice(&ebx.to_ne_bytes());
@@ -72,7 +72,7 @@ fn load_x86_features() -> Flags {
7272

7373
// EAX = 1, ECX = 0: Queries "Processor Info and Feature Bits";
7474
// Contains information about most x86 features.
75-
let CpuidResult { ecx, edx, .. } = unsafe { __cpuid(0x0000_0001_u32) };
75+
let CpuidResult { ecx, edx, .. } = __cpuid(0x0000_0001_u32);
7676
let proc_info_ecx = Flags::from_bits(ecx);
7777
let proc_info_edx = Flags::from_bits(edx);
7878

@@ -82,23 +82,23 @@ fn load_x86_features() -> Flags {
8282
let mut extended_features_edx = Flags::empty();
8383
let mut extended_features_eax_leaf_1 = Flags::empty();
8484
if max_basic_leaf >= 7 {
85-
let CpuidResult { ebx, edx, .. } = unsafe { __cpuid(0x0000_0007_u32) };
85+
let CpuidResult { ebx, edx, .. } = __cpuid(0x0000_0007_u32);
8686
extended_features_ebx = Flags::from_bits(ebx);
8787
extended_features_edx = Flags::from_bits(edx);
8888

89-
let CpuidResult { eax, .. } = unsafe { __cpuid_count(0x0000_0007_u32, 0x0000_0001_u32) };
89+
let CpuidResult { eax, .. } = __cpuid_count(0x0000_0007_u32, 0x0000_0001_u32);
9090
extended_features_eax_leaf_1 = Flags::from_bits(eax)
9191
}
9292

9393
// EAX = 0x8000_0000, ECX = 0: Get Highest Extended Function Supported
9494
// - EAX returns the max leaf value for extended information, that is,
9595
// `cpuid` calls in range [0x8000_0000; u32::MAX]:
96-
let extended_max_basic_leaf = unsafe { __cpuid(0x8000_0000_u32) }.eax;
96+
let extended_max_basic_leaf = __cpuid(0x8000_0000_u32).eax;
9797

9898
// EAX = 0x8000_0001, ECX=0: Queries "Extended Processor Info and Feature Bits"
9999
let mut extended_proc_info_ecx = Flags::empty();
100100
if extended_max_basic_leaf >= 1 {
101-
let CpuidResult { ecx, .. } = unsafe { __cpuid(0x8000_0001_u32) };
101+
let CpuidResult { ecx, .. } = __cpuid(0x8000_0001_u32);
102102
extended_proc_info_ecx = Flags::from_bits(ecx);
103103
}
104104

library/std/src/sys/pal/uefi/time.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ pub(crate) mod instant_internal {
297297
// Inspired by [`edk2/UefiCpuPkg/Library/CpuTimerLib/CpuTimerLib.c`](https://github.com/tianocore/edk2/blob/master/UefiCpuPkg/Library/CpuTimerLib/CpuTimerLib.c)
298298
let freq = FREQUENCY
299299
.get_or_try_init(|| {
300-
let cpuid = unsafe { crate::arch::x86_64::__cpuid(0x15) };
300+
let cpuid = crate::arch::x86_64::__cpuid(0x15);
301301
if cpuid.eax == 0 || cpuid.ebx == 0 || cpuid.ecx == 0 {
302302
return Err(());
303303
}
@@ -316,7 +316,7 @@ pub(crate) mod instant_internal {
316316

317317
let freq = FREQUENCY
318318
.get_or_try_init(|| {
319-
let cpuid = unsafe { crate::arch::x86::__cpuid(0x15) };
319+
let cpuid = crate::arch::x86::__cpuid(0x15);
320320
if cpuid.eax == 0 || cpuid.ebx == 0 || cpuid.ecx == 0 {
321321
return Err(());
322322
}

library/std/src/sys/random/uefi.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,11 +107,11 @@ mod rdrand {
107107
{
108108
// SAFETY: All Rust x86 targets are new enough to have CPUID, and we
109109
// check that leaf 1 is supported before using it.
110-
let cpuid0 = unsafe { arch::__cpuid(0) };
110+
let cpuid0 = arch::__cpuid(0);
111111
if cpuid0.eax < 1 {
112112
return false;
113113
}
114-
let cpuid1 = unsafe { arch::__cpuid(1) };
114+
let cpuid1 = arch::__cpuid(1);
115115

116116
let vendor_id =
117117
[cpuid0.ebx.to_le_bytes(), cpuid0.edx.to_le_bytes(), cpuid0.ecx.to_le_bytes()];

library/std_detect/src/detect/os/x86.rs

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,10 @@ pub(crate) fn detect_features() -> cache::Initializer {
4040
// leaf value for subsequent calls of `cpuinfo` in range [0,
4141
// 0x8000_0000]. - The vendor ID is stored in 12 u8 ascii chars,
4242
// returned in EBX, EDX, and ECX (in that order):
43-
let (max_basic_leaf, vendor_id) = unsafe {
43+
let (max_basic_leaf, vendor_id) = {
4444
let CpuidResult { eax: max_basic_leaf, ebx, ecx, edx } = __cpuid(0);
4545
let vendor_id: [[u8; 4]; 3] = [ebx.to_ne_bytes(), edx.to_ne_bytes(), ecx.to_ne_bytes()];
46-
let vendor_id: [u8; 12] = mem::transmute(vendor_id);
46+
let vendor_id: [u8; 12] = unsafe { mem::transmute(vendor_id) };
4747
(max_basic_leaf, vendor_id)
4848
};
4949

@@ -54,8 +54,7 @@ pub(crate) fn detect_features() -> cache::Initializer {
5454

5555
// EAX = 1, ECX = 0: Queries "Processor Info and Feature Bits";
5656
// Contains information about most x86 features.
57-
let CpuidResult { ecx: proc_info_ecx, edx: proc_info_edx, .. } =
58-
unsafe { __cpuid(0x0000_0001_u32) };
57+
let CpuidResult { ecx: proc_info_ecx, edx: proc_info_edx, .. } = __cpuid(0x0000_0001_u32);
5958

6059
// EAX = 7: Queries "Extended Features";
6160
// Contains information about bmi,bmi2, and avx2 support.
@@ -66,9 +65,9 @@ pub(crate) fn detect_features() -> cache::Initializer {
6665
extended_features_eax_leaf_1,
6766
extended_features_edx_leaf_1,
6867
) = if max_basic_leaf >= 7 {
69-
let CpuidResult { ebx, ecx, edx, .. } = unsafe { __cpuid(0x0000_0007_u32) };
68+
let CpuidResult { ebx, ecx, edx, .. } = __cpuid(0x0000_0007_u32);
7069
let CpuidResult { eax: eax_1, edx: edx_1, .. } =
71-
unsafe { __cpuid_count(0x0000_0007_u32, 0x0000_0001_u32) };
70+
__cpuid_count(0x0000_0007_u32, 0x0000_0001_u32);
7271
(ebx, ecx, edx, eax_1, edx_1)
7372
} else {
7473
(0, 0, 0, 0, 0) // CPUID does not support "Extended Features"
@@ -77,12 +76,12 @@ pub(crate) fn detect_features() -> cache::Initializer {
7776
// EAX = 0x8000_0000, ECX = 0: Get Highest Extended Function Supported
7877
// - EAX returns the max leaf value for extended information, that is,
7978
// `cpuid` calls in range [0x8000_0000; u32::MAX]:
80-
let CpuidResult { eax: extended_max_basic_leaf, .. } = unsafe { __cpuid(0x8000_0000_u32) };
79+
let CpuidResult { eax: extended_max_basic_leaf, .. } = __cpuid(0x8000_0000_u32);
8180

8281
// EAX = 0x8000_0001, ECX=0: Queries "Extended Processor Info and Feature
8382
// Bits"
8483
let extended_proc_info_ecx = if extended_max_basic_leaf >= 1 {
85-
let CpuidResult { ecx, .. } = unsafe { __cpuid(0x8000_0001_u32) };
84+
let CpuidResult { ecx, .. } = __cpuid(0x8000_0001_u32);
8685
ecx
8786
} else {
8887
0
@@ -132,7 +131,7 @@ pub(crate) fn detect_features() -> cache::Initializer {
132131

133132
// Detect if CPUID.19h available
134133
if bit::test(extended_features_ecx as usize, 23) {
135-
let CpuidResult { ebx, .. } = unsafe { __cpuid(0x19) };
134+
let CpuidResult { ebx, .. } = __cpuid(0x19);
136135
enable(ebx, 0, Feature::kl);
137136
enable(ebx, 2, Feature::widekl);
138137
}
@@ -223,7 +222,7 @@ pub(crate) fn detect_features() -> cache::Initializer {
223222
// ECX = 1):
224223
if max_basic_leaf >= 0xd {
225224
let CpuidResult { eax: proc_extended_state1_eax, .. } =
226-
unsafe { __cpuid_count(0xd_u32, 1) };
225+
__cpuid_count(0xd_u32, 1);
227226
enable(proc_extended_state1_eax, 0, Feature::xsaveopt);
228227
enable(proc_extended_state1_eax, 1, Feature::xsavec);
229228
enable(proc_extended_state1_eax, 3, Feature::xsaves);
@@ -282,7 +281,7 @@ pub(crate) fn detect_features() -> cache::Initializer {
282281

283282
if max_basic_leaf >= 0x1e {
284283
let CpuidResult { eax: amx_feature_flags_eax, .. } =
285-
unsafe { __cpuid_count(0x1e_u32, 1) };
284+
__cpuid_count(0x1e_u32, 1);
286285

287286
enable(amx_feature_flags_eax, 4, Feature::amx_fp8);
288287
enable(amx_feature_flags_eax, 6, Feature::amx_tf32);
@@ -297,7 +296,7 @@ pub(crate) fn detect_features() -> cache::Initializer {
297296

298297
let avx10_1 = enable(extended_features_edx_leaf_1, 19, Feature::avx10_1);
299298
if avx10_1 {
300-
let CpuidResult { ebx, .. } = unsafe { __cpuid(0x24) };
299+
let CpuidResult { ebx, .. } = __cpuid(0x24);
301300
let avx10_version = ebx & 0xff;
302301
if avx10_version >= 2 {
303302
value.set(Feature::avx10_2 as u32);

src/tools/miri/src/bin/log/tracing_chrome_instant.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ mod tsc {
151151

152152
// implemented like https://docs.rs/raw-cpuid/latest/src/raw_cpuid/extended.rs.html#965-967
153153
const LEAF: u32 = 0x80000007; // this is the leaf for "advanced power management info"
154-
let cpuid = unsafe { __cpuid(LEAF) };
154+
let cpuid = __cpuid(LEAF);
155155
(cpuid.edx & (1 << 8)) != 0 // EDX bit 8 indicates invariant TSC
156156
}
157157

src/tools/rustfmt/tests/source/cfg_if/detect/os/x86.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,13 +66,13 @@ fn detect_features() -> cache::Initializer {
6666
ecx: proc_info_ecx,
6767
edx: proc_info_edx,
6868
..
69-
} = unsafe { __cpuid(0x0000_0001_u32) };
69+
} = __cpuid(0x0000_0001_u32);
7070

7171
// EAX = 7, ECX = 0: Queries "Extended Features";
7272
// Contains information about bmi,bmi2, and avx2 support.
7373
let (extended_features_ebx, extended_features_ecx) = if max_basic_leaf >= 7
7474
{
75-
let CpuidResult { ebx, ecx, .. } = unsafe { __cpuid(0x0000_0007_u32) };
75+
let CpuidResult { ebx, ecx, .. } = __cpuid(0x0000_0007_u32);
7676
(ebx, ecx)
7777
} else {
7878
(0, 0) // CPUID does not support "Extended Features"
@@ -84,12 +84,12 @@ fn detect_features() -> cache::Initializer {
8484
let CpuidResult {
8585
eax: extended_max_basic_leaf,
8686
..
87-
} = unsafe { __cpuid(0x8000_0000_u32) };
87+
} = __cpuid(0x8000_0000_u32);
8888

8989
// EAX = 0x8000_0001, ECX=0: Queries "Extended Processor Info and Feature
9090
// Bits"
9191
let extended_proc_info_ecx = if extended_max_basic_leaf >= 1 {
92-
let CpuidResult { ecx, .. } = unsafe { __cpuid(0x8000_0001_u32) };
92+
let CpuidResult { ecx, .. } = __cpuid(0x8000_0001_u32);
9393
ecx
9494
} else {
9595
0
@@ -182,7 +182,7 @@ fn detect_features() -> cache::Initializer {
182182
let CpuidResult {
183183
eax: proc_extended_state1_eax,
184184
..
185-
} = unsafe { __cpuid_count(0xd_u32, 1) };
185+
} = __cpuid_count(0xd_u32, 1);
186186
enable(proc_extended_state1_eax, 0, Feature::xsaveopt);
187187
enable(proc_extended_state1_eax, 1, Feature::xsavec);
188188
enable(proc_extended_state1_eax, 3, Feature::xsaves);

src/tools/rustfmt/tests/target/cfg_if/detect/os/x86.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,12 +66,12 @@ fn detect_features() -> cache::Initializer {
6666
ecx: proc_info_ecx,
6767
edx: proc_info_edx,
6868
..
69-
} = unsafe { __cpuid(0x0000_0001_u32) };
69+
} = __cpuid(0x0000_0001_u32);
7070

7171
// EAX = 7, ECX = 0: Queries "Extended Features";
7272
// Contains information about bmi,bmi2, and avx2 support.
7373
let (extended_features_ebx, extended_features_ecx) = if max_basic_leaf >= 7 {
74-
let CpuidResult { ebx, ecx, .. } = unsafe { __cpuid(0x0000_0007_u32) };
74+
let CpuidResult { ebx, ecx, .. } = __cpuid(0x0000_0007_u32);
7575
(ebx, ecx)
7676
} else {
7777
(0, 0) // CPUID does not support "Extended Features"
@@ -83,12 +83,12 @@ fn detect_features() -> cache::Initializer {
8383
let CpuidResult {
8484
eax: extended_max_basic_leaf,
8585
..
86-
} = unsafe { __cpuid(0x8000_0000_u32) };
86+
} = __cpuid(0x8000_0000_u32);
8787

8888
// EAX = 0x8000_0001, ECX=0: Queries "Extended Processor Info and Feature
8989
// Bits"
9090
let extended_proc_info_ecx = if extended_max_basic_leaf >= 1 {
91-
let CpuidResult { ecx, .. } = unsafe { __cpuid(0x8000_0001_u32) };
91+
let CpuidResult { ecx, .. } = __cpuid(0x8000_0001_u32);
9292
ecx
9393
} else {
9494
0
@@ -181,7 +181,7 @@ fn detect_features() -> cache::Initializer {
181181
let CpuidResult {
182182
eax: proc_extended_state1_eax,
183183
..
184-
} = unsafe { __cpuid_count(0xd_u32, 1) };
184+
} = __cpuid_count(0xd_u32, 1);
185185
enable(proc_extended_state1_eax, 0, Feature::xsaveopt);
186186
enable(proc_extended_state1_eax, 1, Feature::xsavec);
187187
enable(proc_extended_state1_eax, 3, Feature::xsaves);

0 commit comments

Comments
 (0)