From 5130d5dde035bb442a80fd45c0988639ddf3554e Mon Sep 17 00:00:00 2001 From: Joe Richey Date: Wed, 27 May 2020 22:08:00 -0700 Subject: [PATCH 1/2] cpu: Have "cpu" feature take precedence over "custom" Right now, features are always enabled across all targets. This means that if _any_ Custom RNG is used _anywhere_ in a crate's dependancy graph on _any_ target. The "custom" feature will be active on all targets. This makes it impossible to have a bare metal target that uses "cpu" on x86_64 and a Custom RNG on aarch64. This solution also makes sure that any implementation `getrandom` itself provides cannot be overridden. Signed-off-by: Joe Richey --- src/lib.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 8a03009a..b595b4bf 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -183,11 +183,11 @@ cfg_if! { #[path = "windows.rs"] mod imp; } else if #[cfg(all(target_arch = "x86_64", target_env = "sgx"))] { #[path = "rdrand.rs"] mod imp; - } else if #[cfg(feature = "custom")] { - use custom as imp; } else if #[cfg(all(feature = "cpu", any(target_arch = "x86_64", target_arch = "x86")))] { #[path = "rdrand.rs"] mod imp; + } else if #[cfg(feature = "custom")] { + use custom as imp; } else { compile_error!("\ target is not supported, for more information see: \ From 0689cb59f13018ec3ffdba6f18525c02bb4d4b5d Mon Sep 17 00:00:00 2001 From: Joe Richey Date: Thu, 28 May 2020 17:10:06 -0700 Subject: [PATCH 2/2] Rename "cpu" feature to "rdrand" Signed-off-by: Joe Richey --- .travis.yml | 4 ++-- Cargo.toml | 4 ++-- src/lib.rs | 4 ++-- src/{test_cpu.rs => test_rdrand.rs} | 2 +- 4 files changed, 7 insertions(+), 7 deletions(-) rename src/{test_cpu.rs => test_rdrand.rs} (71%) diff --git a/.travis.yml b/.travis.yml index a5f74cde..3d3e443c 100644 --- a/.travis.yml +++ b/.travis.yml @@ -145,11 +145,11 @@ jobs: script: # We test that getrandom builds for all targets - echo $STD_TARGETS | xargs -t -n1 cargo build --target - - echo $NO_STD_TARGETS | xargs -t -n1 cargo xbuild --features=cpu --target + - echo $NO_STD_TARGETS | xargs -t -n1 cargo xbuild --features=rdrand --target # also test minimum dependency versions are usable - cargo generate-lockfile -Z minimal-versions - echo $STD_TARGETS | xargs -t -n1 cargo build --target - - echo $NO_STD_TARGETS | xargs -t -n1 cargo xbuild --features=cpu --target + - echo $NO_STD_TARGETS | xargs -t -n1 cargo xbuild --features=rdrand --target # Trust cross-built/emulated targets. We must repeat all non-default values. - name: "Linux (MIPS, big-endian)" diff --git a/Cargo.toml b/Cargo.toml index 0488cc19..5ace3432 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -35,8 +35,8 @@ wasi = "0.9" [features] std = [] -# Feature to enable fallback CPU-based implementation -cpu = [] +# Feature to enable fallback RDRAND-based implementation +rdrand = [] # Feature to enable custom RNG implementations custom = [] # Unstable feature to support being a libstd dependency diff --git a/src/lib.rs b/src/lib.rs index b595b4bf..b988445c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -183,7 +183,7 @@ cfg_if! { #[path = "windows.rs"] mod imp; } else if #[cfg(all(target_arch = "x86_64", target_env = "sgx"))] { #[path = "rdrand.rs"] mod imp; - } else if #[cfg(all(feature = "cpu", + } else if #[cfg(all(feature = "rdrand", any(target_arch = "x86_64", target_arch = "x86")))] { #[path = "rdrand.rs"] mod imp; } else if #[cfg(feature = "custom")] { @@ -219,4 +219,4 @@ pub fn getrandom(dest: &mut [u8]) -> Result<(), Error> { #[cfg(test)] mod test_common; #[cfg(test)] -mod test_cpu; +mod test_rdrand; diff --git a/src/test_cpu.rs b/src/test_rdrand.rs similarity index 71% rename from src/test_cpu.rs rename to src/test_rdrand.rs index 7a9a0669..fafa6acc 100644 --- a/src/test_cpu.rs +++ b/src/test_rdrand.rs @@ -1,4 +1,4 @@ -// We only test the CPU-based RNG source on supported architectures. +// We only test the RDRAND-based RNG source on supported architectures. #![cfg(any(target_arch = "x86_64", target_arch = "x86"))] #[path = "rdrand.rs"]