Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

signal: 11, SIGSEGV: invalid memory reference and unable to return value from unsafe #9777

Closed
ghost opened this issue Aug 9, 2021 · 2 comments
Labels
C-bug Category: bug

Comments

@ghost
Copy link

ghost commented Aug 9, 2021

one of my current project would return an array at the end.
The code I wrote involves simd so it need to be included inside an unsafe parenthesis.
It just behaves very OK when running as waterfall from beginning to the end.

But, now I have made those simd code as a feature to be optional, and added #[cfg(feature="simd")] sort of tags before each unsafe {...}, the cargo run/test/... would always leave error to me:

# cargo test hash_abc --features simd -- --nocapture
   Compiling cryptaq v0.1.0 (/home/user/cryptaq)
warning: unused import: `super::hmac`
   --> src/sm3/sm3.rs:966:9
    |
966 |     use super::hmac;
    |         ^^^^^^^^^^^
    |
    = note: `#[warn(unused_imports)]` on by default

warning: `cryptaq` (lib test) generated 1 warning
warning: function is never used: `hmac`
   --> src/sm3/sm3.rs:932:8
    |
932 | pub fn hmac(m: String, key: String) -> [u8; HALF_BLOCK_SIZE] {
    |        ^^^^
    |
    = note: `#[warn(dead_code)]` on by default

warning: `cryptaq` (bin "bench" test) generated 2 warnings (1 duplicate)
warning: `cryptaq` (bin "cryptaq" test) generated 1 warning (1 duplicate)
    Finished test [unoptimized + debuginfo] target(s) in 1.47s
     Running unittests (target/debug/deps/libaq-5ebfab2dfe6fe8da)

running 1 test
error: test failed, to rerun pass '--lib'

Caused by:
  process didn't exit successfully: `/home/user/cryptaq/target/debug/deps/libaq-5ebfab2dfe6fe8da hash_abc --nocapture` (signal: 11, SIGSEGV: invalid memory reference)
# cargo run --release --example cwo --features simd
   Compiling cryptaq v0.1.0 (/home/user/cryptaq)
    Finished release [optimized] target(s) in 5.61s
     Running `target/release/examples/cwo`
testing a length of  6061-Byte message...
fish: Job 1, 'cargo run --release --example c…' terminated by signal SIGSEGV (Address boundary error)

So I noticed #6489, I added codegen-units = 1 and it does'nt work. And I have the following hints.

# cargo fix --allow-dirty
    Checking cryptaq v0.1.0 (/home/user/cryptaq)
error[E0308]: mismatched types
  --> src/sm3/sm3.rs:71:26
   |
71 | pub fn hash(m: &[u8]) -> [u8; HALF_BLOCK_SIZE] {
   |        ----              ^^^^^^^^^^^^^^^^^^^^^ expected array `[u8; 32]`, found `()`
   |        |
   |        implicitly returns `()` as its body has no tail or `return` expression

For more information about this error, try `rustc --explain E0308`.
error: could not compile `cryptaq` due to previous error
warning: build failed, waiting for other jobs to finish...
warning: unused import: `super::hmac`
   --> src/sm3/sm3.rs:966:9
    |
966 |     use super::hmac;
    |         ^^^^^^^^^^^
    |
    = note: `#[warn(unused_imports)]` on by default

warning: `cryptaq` (lib test) generated 1 warning
error: build failed

This is rediculous because here are the very end of this function:

pub fn hash(m: &[u8]) -> [u8; HALF_BLOCK_SIZE] {
    ...
    #[cfg(feature = "simd_gen_hash")]
    unsafe {
        simd_gen_hash(digest.get_unchecked(0))
    }
    #[cfg(feature = "gen_hash")]
    {
        ...
    }
}
//...and
#[inline]
#[target_feature(enable = "avx")]
#[target_feature(enable = "avx2")]
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
#[allow(unsafe_code)]
unsafe fn simd_gen_hash(final_digest: *const u32) -> [u8; 32] {
    // Safe here because such a [u8; 32] array has a fixed size of 256-bit
    let pmt = _mm256_load_si256(PMT_ARR.get_unchecked(0) as *const _ as *const __m256i);
    // Safe here because we want to shuffle the u8x32 array
    let reg = _mm256_shuffle_epi8(_mm256_load_si256(final_digest as *const __m256i), pmt);
    // Safe here because a __m256i has same length as a [u8; 32] array
    std::mem::transmute::<__m256i, [u8; HALF_BLOCK_SIZE]>(reg)
}

Just what can we do to make this right?

@ghost ghost added the C-bug Category: bug label Aug 9, 2021
@ghost
Copy link
Author

ghost commented Aug 9, 2021

And I altered the code like

pub fn hash(m: &[u8]) -> [u8; HALF_BLOCK_SIZE] {
    ...
    let mut value: [u8; HALF_BLOCK_SIZE] = Default::default();
    #[cfg(feature = "simd_gen_hash")]
    unsafe {
        simd_gen_hash(digest.get_unchecked(0))
    }
    ...
    value
}

Then the https://github.com/rust-analyzer/rust-analyzer hints me that:

[rustc unused_mut] [W] variable does not need to be mutable
`#[warn(unused_mut)]` on by default 

And cargo test has:

# cargo test hash_abc --features simd -- --nocapture
   Compiling cryptaq v0.1.0 (/home/user/cryptaq)
warning: value assigned to `value` is never read
   --> src/sm3/sm3.rs:652:9
    |
652 |     let value: [u8; HALF_BLOCK_SIZE] = Default::default();
    |         ^^^^^
    |
    = note: `#[warn(unused_assignments)]` on by default
    = help: maybe it is overwritten before being read?

error[E0384]: cannot assign twice to immutable variable `value`
   --> src/sm3/sm3.rs:667:9
    |
652 |     let value: [u8; HALF_BLOCK_SIZE] = Default::default();
    |         -----
    |         |
    |         first assignment to `value`
    |         help: consider making this binding mutable: `mut value`
...
667 |         value = simd_gen_hash(digest.get_unchecked(0));
    |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ cannot assign twice to immutable variable

For more information about this error, try `rustc --explain E0384`.
warning: `cryptaq` (lib test) generated 1 warning (1 duplicate)
error: could not compile `cryptaq` due to previous error; 1 warning emitted
warning: build failed, waiting for other jobs to finish...
warning: `cryptaq` (lib) generated 1 warning
error: build failed

Such paradox just make me unbelievable.

@ehuss
Copy link
Contributor

ehuss commented Aug 9, 2021

This issue tracker is for tracking issues with Cargo. It's pretty hard to say what the problem is without seeing the full code, and it seems like you're describing multiple errors, but there isn't enough information here to help. I recommend trying one of the user forums (such as urlo) for help with writing Rust code. When posting for help, I recommend trying to include a fully reproducible example, and to keep your question focused on one problem.

@ehuss ehuss closed this as completed Aug 9, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: bug
Projects
None yet
Development

No branches or pull requests

1 participant