RustCrypto / hashes

Collection of cryptographic hash functions written in pure Rust
1.9k stars 256 forks source link

sha2: Illegal instruction in windows server 2019 #442

Closed karfield closed 1 year ago

karfield commented 1 year ago

"Illegal instruction" in windows-server 2019 prompted when I simply use sha2:

use sha2::{Digest, Sha256};

fn main() {
    let mut h = Sha256::new();
    h.update(b"e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855");
    let x = format!("{:x}", h.finalize());
}

sha2 version: 0.10.6 rust version:1.68.0 cross target: x86_64-pc-windows-gnu host env: linux-mingw64

expect: should be tested okay in win* targets.

newpavlov commented 1 year ago

Can you find the exact instruction which causes the exception using debugger? Do you manually enable any target features (e.g. by using target-cpu=native)?

I assume your CPU on the windows server does not support the SHA extension, correct? Can you check whether cpufeatures detects it or not?

karfield commented 1 year ago

The cpu tested is Xeon Platinum 8269CY @ 2.5GHz.

I built by cargo +nightly build --release --target x86_64-pc-windows-gnu, not specialized target-cpu. By checking with cpufeautres, it seems the cpu has SHA feature

fn main() {
    cpufeatures::new!(cpuid_sha, "sha");
    let token: cpuid_sha::InitToken = cpuid_sha::init();
    if !token.get() {
        println!("sha not supported");
    } else {
        println!("the cpu has sha feature");
    }

    let mut h = Sha256::new();
    h.update(b"e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855");
    let x = format!("{:x}", h.finalize());
}

the results(no backtrace shows):

the cpu has sha feature
Illegal instruction

I have No idea to use debugger on Windows yet, any suggestions to find out exact inst?

newpavlov commented 1 year ago

What about is_x86_feature_detected!("sha")? According to a cursory search you have a Cascade Lake CPU and it does not support the SHA extension. So either the cpufeatures code is somehow incorrect or your system configuration is wrong and returns incorrect CPUID data. As a temporary workaround you can enable the force-soft feature.

karfield commented 1 year ago

What about is_x86_feature_detected!("sha")? According to a cursory search you have a Cascade Lake CPU and it does not support the SHA extension. So either the cpufeatures code is somehow incorrect or your system configuration is wrong and returns incorrect CPUID data. As a temporary workaround you can enable the force-soft feature.

force-soft can work temporarily. So why not detect ISA extension before using hw instruction (e.g. SHA) rather than throws 'Illegal instruction' ?

tarcieri commented 1 year ago

@karfield that’s exactly what cpufeatures is supposed to do, but it seems there’s a bug where it’s getting a false positive.

Can you check is_x86_feature_detected!("sha") to see if it works or has the same problem?

karfield commented 1 year ago

@tarcieri is_x86_feature_detected!("sha") returns true, but cpu-z shows no SHA extension Screenshot from 2022-12-17 01-55-04

tarcieri commented 1 year ago

No idea what's happening then. Is there some sort of virtualization involved?

karfield commented 1 year ago

I use cloud hosted Windows which sold as 'bare-metal' service, should not on virtualization. I closed this as not a bug for sha2 anyway.

tarcieri commented 1 year ago

Not sure what's happening then, but it seems like there is a discrepancy between what CPUID is reporting and what instructions are actually available