Skip to content

Commit 2e39004

Browse files
committed
rdrand: Add 32-bit x86 support
Also add tests for 32-bit x86
1 parent 52e7e9f commit 2e39004

File tree

4 files changed

+35
-11
lines changed

4 files changed

+35
-11
lines changed

.travis.yml

+15-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
language: rust
22
os: linux
33

4-
# Targets that we just build (rather than run and test)
54
env:
65
global:
6+
# All of the supported x86 Linux targets
7+
- LINUX_TARGETS="x86_64-unknown-linux-gnu x86_64-unknown-linux-musl i686-unknown-linux-gnu i686-unknown-linux-musl"
8+
# Targets that we just build (rather than run and test)
79
- STD_TARGETS="x86_64-sun-solaris x86_64-unknown-cloudabi x86_64-unknown-freebsd x86_64-fuchsia x86_64-unknown-netbsd x86_64-unknown-redox x86_64-fortanix-unknown-sgx"
810
- NO_STD_TARGETS="x86_64-unknown-uefi x86_64-unknown-hermit x86_64-unknown-l4re-uclibc x86_64-uwp-windows-gnu x86_64-wrs-vxworks"
911

@@ -112,6 +114,18 @@ jobs:
112114
name: "OSX, nightly, docs"
113115
os: osx
114116

117+
- name: "cross-platform tests"
118+
rust: nightly
119+
addons:
120+
apt:
121+
packages:
122+
- gcc-multilib
123+
install:
124+
- echo $LINUX_TARGETS | xargs -n1 rustup target add
125+
script:
126+
# We run tests for all supported x86 Linux targets
127+
- echo $LINUX_TARGETS | xargs -t -n1 cargo test --target
128+
115129
- name: "cross-platform build only"
116130
rust: nightly
117131
install:

src/lib.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,8 @@ cfg_if! {
185185
#[path = "rdrand.rs"] mod imp;
186186
} else if #[cfg(feature = "custom")] {
187187
use custom as imp;
188-
} else if #[cfg(all(feature = "cpu", target_arch = "x86_64"))] {
188+
} else if #[cfg(all(feature = "cpu",
189+
any(target_arch = "x86_64", target_arch = "x86")))] {
189190
#[path = "rdrand.rs"] mod imp;
190191
} else {
191192
compile_error!("\

src/rdrand.rs

+17-8
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,30 @@
77
// except according to those terms.
88

99
//! Implementation for SGX using RDRAND instruction
10-
#[cfg(not(target_feature = "rdrand"))]
11-
use crate::util::LazyBool;
1210
use crate::Error;
13-
use core::arch::x86_64::_rdrand64_step;
1411
use core::mem;
1512

13+
cfg_if! {
14+
if #[cfg(target_arch = "x86_64")] {
15+
use core::arch::x86_64 as arch;
16+
use arch::_rdrand64_step as rdrand_step;
17+
} else if #[cfg(target_arch = "x86")] {
18+
use core::arch::x86 as arch;
19+
use arch::_rdrand32_step as rdrand_step;
20+
}
21+
}
22+
1623
// Recommendation from "Intel® Digital Random Number Generator (DRNG) Software
1724
// Implementation Guide" - Section 5.2.1 and "Intel® 64 and IA-32 Architectures
1825
// Software Developer’s Manual" - Volume 1 - Section 7.3.17.1.
1926
const RETRY_LIMIT: usize = 10;
20-
const WORD_SIZE: usize = mem::size_of::<u64>();
27+
const WORD_SIZE: usize = mem::size_of::<usize>();
2128

2229
#[target_feature(enable = "rdrand")]
2330
unsafe fn rdrand() -> Result<[u8; WORD_SIZE], Error> {
2431
for _ in 0..RETRY_LIMIT {
2532
let mut el = mem::zeroed();
26-
if _rdrand64_step(&mut el) == 1 {
33+
if rdrand_step(&mut el) == 1 {
2734
// AMD CPUs from families 14h to 16h (pre Ryzen) sometimes fail to
2835
// set CF on bogus random data, so we check these values explicitly.
2936
// See https://github.com/systemd/systemd/issues/11810#issuecomment-489727505
@@ -53,11 +60,13 @@ fn is_rdrand_supported() -> bool {
5360
// https://github.com/rust-lang-nursery/stdsimd/issues/464
5461
#[cfg(not(target_feature = "rdrand"))]
5562
fn is_rdrand_supported() -> bool {
56-
use core::arch::x86_64::__cpuid;
57-
// SAFETY: All x86_64 CPUs support CPUID leaf 1
63+
use crate::util::LazyBool;
64+
65+
// SAFETY: All Rust x86 targets are new enough to have CPUID, and if CPUID
66+
// is supported, CPUID leaf 1 is always supported.
5867
const FLAG: u32 = 1 << 30;
5968
static HAS_RDRAND: LazyBool = LazyBool::new();
60-
HAS_RDRAND.unsync_init(|| unsafe { (__cpuid(1).ecx & FLAG) != 0 })
69+
HAS_RDRAND.unsync_init(|| unsafe { (arch::__cpuid(1).ecx & FLAG) != 0 })
6170
}
6271

6372
pub fn getrandom_inner(dest: &mut [u8]) -> Result<(), Error> {

src/test_cpu.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// We only test the CPU-based RNG source on supported architectures.
2-
#![cfg(target_arch = "x86_64")]
2+
#![cfg(any(target_arch = "x86_64", target_arch = "x86"))]
33

44
#[path = "rdrand.rs"]
55
mod rdrand;

0 commit comments

Comments
 (0)