Skip to content

Commit

Permalink
Merge branch 'master' into msrv
Browse files Browse the repository at this point in the history
  • Loading branch information
tkaitchuck committed Nov 12, 2023
2 parents f9dd31a + 53c08e4 commit d92be76
Show file tree
Hide file tree
Showing 11 changed files with 140 additions and 48 deletions.
13 changes: 13 additions & 0 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -181,3 +181,16 @@ jobs:
uses: actions-rs/cargo@v1
with:
command: check
no_std:
name: no-std build
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions-rs/toolchain@v1
with:
toolchain: nightly
override: true
- uses: actions-rs/cargo@v1
with:
command: build
args: --manifest-path=no_std_test/Cargo.toml
5 changes: 4 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ no-rng = []
# in case this is being used on an architecture lacking core::sync::atomic::AtomicUsize and friends
atomic-polyfill = [ "dep:atomic-polyfill", "once_cell/atomic-polyfill"]

# Nightly-only support for AES intrinsics on 32-bit ARM
nightly-arm-aes = []

[[bench]]
name = "ahash"
path = "tests/bench.rs"
Expand Down Expand Up @@ -84,7 +87,7 @@ getrandom = { version = "0.2.7", optional = true }
zerocopy = { version = "0.7.20", default-features = false, features = ["simd"] }

[target.'cfg(not(all(target_arch = "arm", target_os = "none")))'.dependencies]
once_cell = { version = "1.13.1", default-features = false, features = ["unstable", "alloc"] }
once_cell = { version = "1.18.0", default-features = false, features = ["alloc"] }

[dev-dependencies]
no-panic = "0.1.10"
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ The aHash package has the following flags:
This is done using the [getrandom](https://github.com/rust-random/getrandom) crate.
* `compile-time-rng`: For OS targets without access to a random number generator, `compile-time-rng` provides an alternative.
If `getrandom` is unavailable and `compile-time-rng` is enabled, aHash will generate random numbers at compile time and embed them in the binary.
* `nightly-arm-aes`: To use AES instructions on 32-bit ARM, which requires nightly. This is not needed on AArch64.
This allows for DOS resistance even if there is no random number generator available at runtime (assuming the compiled binary is not public).
This makes the binary non-deterministic. (If non-determinism is a problem see [constrandom's documentation](https://github.com/tkaitchuck/constrandom#deterministic-builds))

Expand Down
3 changes: 0 additions & 3 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@ fn main() {
if let Some(true) = version_check::supports_feature("specialize") {
println!("cargo:rustc-cfg=feature=\"specialize\"");
}
if let Some(true) = version_check::supports_feature("stdsimd") {
println!("cargo:rustc-cfg=feature=\"stdsimd\"");
}
let arch = env::var("CARGO_CFG_TARGET_ARCH").expect("CARGO_CFG_TARGET_ARCH was not set");
if arch.eq_ignore_ascii_case("x86_64")
|| arch.eq_ignore_ascii_case("aarch64")
Expand Down
34 changes: 34 additions & 0 deletions no_std_test/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
[workspace]

[package]
name = "no_std_test"
version = "0.1.0"
edition = "2018"
authors = ["Stephen Chung"]
description = "no-std test application"

[dependencies]
ahash = { path = "../", default_features = false }
wee_alloc = { version = "0.4.5", default_features = false }

[profile.dev]
panic = "abort"

[profile.release]
opt-level = "z" # optimize for size
debug = false
rpath = false
debug-assertions = false
codegen-units = 1
panic = "abort"

[profile.unix]
inherits = "release"
lto = true

[profile.windows]
inherits = "release"

[profile.macos]
inherits = "release"
lto = "fat"
50 changes: 50 additions & 0 deletions no_std_test/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
//! This is a bare-bones `no-std` application that hashes a value and
//! uses the hash value as the return value.

#![no_std]
#![feature(alloc_error_handler, start, core_intrinsics, lang_items, link_cfg)]

#[global_allocator]
static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT;

use core::hash::{Hash, Hasher};

// NB: Rust needs a CRT runtime on Windows MSVC.
#[cfg(all(windows, target_env = "msvc"))]
#[link(name = "msvcrt")]
#[link(name = "libcmt")]
extern "C" {}

#[start]
fn main(_argc: isize, _argv: *const *const u8) -> isize {
let mut h: ahash::AHasher = Default::default();
42_i32.hash(&mut h);
return h.finish() as isize;
}


#[alloc_error_handler]
fn foo(_: core::alloc::Layout) -> ! {
core::intrinsics::abort();
}

#[panic_handler]
#[lang = "panic_impl"]
fn rust_begin_panic(_: &core::panic::PanicInfo) -> ! {
core::intrinsics::abort();
}

#[no_mangle]
extern "C" fn _rust_eh_personality() {}

#[no_mangle]
extern "C" fn rust_eh_personality() {}

#[no_mangle]
extern "C" fn rust_eh_register_frames() {}

#[no_mangle]
extern "C" fn rust_eh_unregister_frames() {}

#[no_mangle]
extern "C" fn _Unwind_Resume() {}
20 changes: 9 additions & 11 deletions src/hash_quality_test.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use core::hash::{Hash, Hasher};
use std::collections::{HashMap};
use std::collections::HashMap;

fn assert_sufficiently_different(a: u64, b: u64, tolerance: i32) {
let (same_byte_count, same_nibble_count) = count_same_bytes_and_nibbles(a, b);
Expand Down Expand Up @@ -64,8 +64,7 @@ fn gen_combinations(options: &[u32; 11], depth: u32, so_far: Vec<u32>, combinati

fn test_no_full_collisions<T: Hasher>(gen_hash: impl Fn() -> T) {
let options: [u32; 11] = [
0x00000000, 0x10000000, 0x20000000, 0x40000000, 0x80000000, 0xF0000000,
1, 2, 4, 8, 15
0x00000000, 0x10000000, 0x20000000, 0x40000000, 0x80000000, 0xF0000000, 1, 2, 4, 8, 15,
];
let mut combinations = Vec::new();
gen_combinations(&options, 7, Vec::new(), &mut combinations);
Expand Down Expand Up @@ -342,9 +341,12 @@ fn test_sparse<T: Hasher>(hasher: impl Fn() -> T) {
let mut buf = [0u8; 256];
let mut hashes = HashMap::new();
for idx_1 in 0..256 {
for idx_2 in idx_1+1..256 {
for idx_2 in idx_1 + 1..256 {
for value_1 in [1, 2, 4, 8, 16, 32, 64, 128] {
for value_2 in [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 15, 16, 17, 18, 20, 24, 31, 32, 33, 48, 64, 96, 127, 128, 129, 192, 254, 255] {
for value_2 in [
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 15, 16, 17, 18, 20, 24, 31, 32, 33, 48, 64, 96, 127, 128, 129,
192, 254, 255,
] {
buf[idx_1] = value_1;
buf[idx_2] = value_2;
let hash_value = hash_with(&buf, &mut hasher());
Expand Down Expand Up @@ -437,12 +439,8 @@ mod fallback_tests {
///Basic sanity tests of the cypto properties of aHash.
#[cfg(any(
all(any(target_arch = "x86", target_arch = "x86_64"), target_feature = "aes", not(miri)),
all(
any(target_arch = "arm", target_arch = "aarch64"),
any(target_feature = "aes", target_feature = "crypto"),
not(miri),
feature = "stdsimd"
)
all(target_arch = "aarch64", target_feature = "aes", not(miri)),
all(feature = "nightly-arm-aes", target_arch = "arm", target_feature = "aes", not(miri)),
))]
#[cfg(test)]
mod aes_tests {
Expand Down
16 changes: 8 additions & 8 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@
//! But this also means that different computers or computers using different versions of ahash may observe different
//! hash values for the same input.
#![cfg_attr(
all(feature = "std", any(feature = "compile-time-rng", feature = "runtime-rng", feature = "no-rng")),
all(
feature = "std",
any(feature = "compile-time-rng", feature = "runtime-rng", feature = "no-rng")
),
doc = r##"
# Basic Usage
AHash provides an implementation of the [Hasher] trait.
Expand Down Expand Up @@ -95,8 +98,7 @@ Note the import of [HashMapExt]. This is needed for the constructor.
#![allow(clippy::pedantic, clippy::cast_lossless, clippy::unreadable_literal)]
#![cfg_attr(all(not(test), not(feature = "std")), no_std)]
#![cfg_attr(feature = "specialize", feature(min_specialization))]
#![cfg_attr(feature = "specialize", feature(build_hasher_simple_hash_one))]
#![cfg_attr(feature = "stdsimd", feature(stdsimd))]
#![cfg_attr(feature = "nightly-arm-aes", feature(stdarch_arm_neon_intrinsics))]

#[macro_use]
mod convert;
Expand All @@ -106,11 +108,9 @@ mod fallback_hash;
cfg_if::cfg_if! {
if #[cfg(any(
all(any(target_arch = "x86", target_arch = "x86_64"), target_feature = "aes", not(miri)),
all(any(target_arch = "arm", target_arch = "aarch64"),
any(target_feature = "aes", target_feature = "crypto"),
not(miri),
feature = "stdsimd")
))] {
all(target_arch = "aarch64", target_feature = "aes", not(miri)),
all(feature = "nightly-arm-aes", target_arch = "arm", target_feature = "aes", not(miri)),
))] {
mod aes_hash;
pub use crate::aes_hash::AHasher;
} else {
Expand Down
16 changes: 6 additions & 10 deletions src/operations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,11 +110,9 @@ pub(crate) fn aesenc(value: u128, xor: u128) -> u128 {
}
}

#[cfg(all(
any(target_arch = "arm", target_arch = "aarch64"),
any(target_feature = "aes", target_feature = "crypto"),
not(miri),
feature = "stdsimd"
#[cfg(any(
all(target_arch = "aarch64", target_feature = "aes", not(miri)),
all(feature = "nightly-arm-aes", target_arch = "arm", target_feature = "aes", not(miri)),
))]
#[allow(unused)]
#[inline(always)]
Expand Down Expand Up @@ -142,11 +140,9 @@ pub(crate) fn aesdec(value: u128, xor: u128) -> u128 {
}
}

#[cfg(all(
any(target_arch = "arm", target_arch = "aarch64"),
any(target_feature = "aes", target_feature = "crypto"),
not(miri),
feature = "stdsimd"
#[cfg(any(
all(target_arch = "aarch64", target_feature = "aes", not(miri)),
all(feature = "nightly-arm-aes", target_arch = "arm", target_feature = "aes", not(miri)),
))]
#[allow(unused)]
#[inline(always)]
Expand Down
21 changes: 10 additions & 11 deletions src/random_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ use core::hash::Hash;
cfg_if::cfg_if! {
if #[cfg(any(
all(any(target_arch = "x86", target_arch = "x86_64"), target_feature = "aes", not(miri)),
all(any(target_arch = "arm", target_arch = "aarch64"), any(target_feature = "aes", target_feature = "crypto"), not(miri), feature = "stdsimd")
all(target_arch = "aarch64", target_feature = "aes", not(miri)),
all(feature = "nightly-arm-aes", target_arch = "arm", target_feature = "aes", not(miri)),
))] {
use crate::aes_hash::*;
} else {
Expand Down Expand Up @@ -230,7 +231,6 @@ impl fmt::Debug for RandomState {
}

impl RandomState {

/// Create a new `RandomState` `BuildHasher` using random keys.
///
/// Each instance will have a unique set of keys derived from [RandomSource].
Expand Down Expand Up @@ -317,8 +317,8 @@ impl RandomState {
/// Calculates the hash of a single value. This provides a more convenient (and faster) way to obtain a hash:
/// For example:
#[cfg_attr(
feature = "std",
doc = r##" # Examples
feature = "std",
doc = r##" # Examples
```
use std::hash::BuildHasher;
use ahash::RandomState;
Expand All @@ -330,8 +330,8 @@ impl RandomState {
)]
/// This is similar to:
#[cfg_attr(
feature = "std",
doc = r##" # Examples
feature = "std",
doc = r##" # Examples
```
use std::hash::{BuildHasher, Hash, Hasher};
use ahash::RandomState;
Expand Down Expand Up @@ -419,12 +419,11 @@ impl BuildHasher for RandomState {
AHasher::from_random_state(self)
}


/// Calculates the hash of a single value. This provides a more convenient (and faster) way to obtain a hash:
/// For example:
#[cfg_attr(
feature = "std",
doc = r##" # Examples
feature = "std",
doc = r##" # Examples
```
use std::hash::BuildHasher;
use ahash::RandomState;
Expand All @@ -436,8 +435,8 @@ impl BuildHasher for RandomState {
)]
/// This is similar to:
#[cfg_attr(
feature = "std",
doc = r##" # Examples
feature = "std",
doc = r##" # Examples
```
use std::hash::{BuildHasher, Hash, Hasher};
use ahash::RandomState;
Expand Down
9 changes: 5 additions & 4 deletions tests/bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,12 @@ const AHASH_IMPL: &str = if cfg!(any(
target_feature = "aes",
not(miri),
),
all(target_arch = "aarch64", target_feature = "aes", not(miri)),
all(
any(target_arch = "arm", target_arch = "aarch64"),
any(target_feature = "aes", target_feature = "crypto"),
not(miri),
feature = "stdsimd",
feature = "nightly-arm-aes",
target_arch = "arm",
target_feature = "aes",
not(miri)
),
)) {
"aeshash"
Expand Down

0 comments on commit d92be76

Please sign in to comment.