Skip to content

_mm_movemask_epi8 unexpectedly triggers Undefined Behavior #1347

Closed

Description

@Nugine

Moved from rust-lang/miri#2617


playground

use core::arch::x86_64::*;

#[target_feature(enable = "sse2")]
pub unsafe fn is_ascii_sse2(a: &[u8; 16]) -> bool {
    let a = _mm_loadu_si128(a.as_ptr().cast());
    let m = _mm_movemask_epi8(a);
    m == 0
}

fn main() {
    assert!(cfg!(target_feature = "sse2"));
    let s = b"helloworld123456";
    assert!(unsafe { is_ascii_sse2(s) });
}
error: Undefined Behavior: each element of a SIMD mask must be all-0-bits or all-1-bits
    --> /playground/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/../../stdarch/crates/core_arch/src/x86/sse2.rs:1381:5
     |
1381 |     simd_bitmask::<_, u16>(a.as_i8x16()) as u32 as i32
     |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ each element of a SIMD mask must be all-0-bits or all-1-bits
     |
     = help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior
     = help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information
     = note: BACKTRACE:
     = note: inside `std::arch::x86_64::_mm_movemask_epi8` at /playground/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/../../stdarch/crates/core_arch/src/x86/sse2.rs:1381:5
note: inside `is_ascii_sse2` at src/main.rs:6:13

See also:


Related:


_mm[256]_movemask_epi8 has defined behavior:

Returns a mask of the most significant bit of each element in a.

(regardless of the other bits)

Changing the requirement may introduce unsoundness into existing crates.

Activity

  1. Amanieu commented on Oct 26, 2022

    @Amanieu
    Member

    I commented on the miri issue, I think that one should be reopened.

  2. yanns commented on Dec 15, 2022

    @yanns

    Sorry for the naive question, but I still have this issue on nightly:

    MIRIFLAGS="-Zmiri-disable-isolation -Zmiri-measureme=crox" cargo +nightly miri run
    Preparing a sysroot for Miri (target: x86_64-apple-darwin)... done
        Finished dev [unoptimized + debuginfo] target(s) in 0.48s
         Running `/Users/yannsimon/.rustup/toolchains/nightly-x86_64-apple-darwin/bin/cargo-miri runner target/miri/x86_64-apple-darwin/debug/proto_refexp`
    error: Undefined Behavior: each element of a SIMD mask must be all-0-bits or all-1-bits
        --> /Users/yannsimon/.rustup/toolchains/nightly-x86_64-apple-darwin/lib/rustlib/src/rust/library/core/src/../../stdarch/crates/core_arch/src/x86/avx2.rs:2004:5
         |
    2004 |     simd_bitmask::<_, u32>(a.as_i8x32()) as i32
         |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ each element of a SIMD mask must be all-0-bits or all-1-bits
    

    from:

    pub unsafe fn _mm256_movemask_epi8(a: __m256i) -> i32 {
        simd_bitmask::<_, u32>(a.as_i8x32()) as i32
    }
    

    although this has been changed by #1354

    Do we need some kind of release to use the new code?

  3. Amanieu commented on Dec 15, 2022

    @Amanieu
    Member

    The stdarch submodule in rust-lang/rust needs to be updated.

  4. yanns commented on Dec 15, 2022

    @yanns

    Is it something I can do? Locally? Or it has to be done by the rust-lang?

  5. Amanieu commented on Dec 15, 2022

    @Amanieu
    Member

    You can submit a pull request to rust-lang/rust that updates the submodule.

  6. yanns commented on Dec 16, 2022

    @yanns
  7. yanns commented on Dec 16, 2022

    @yanns

    I don't know if you can help in the rust PR.
    It seems that there is a new feature tag:

    error: the feature named `gfni` is not valid for this target
      --> library/core/src/../../stdarch/crates/core_arch/src/x86/gfni.rs:68:18
       |
    68 | #[target_feature(enable = "gfni,avx512bw,avx512f")]
       |                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `gfni` is not valid for this target
    
  8. added a commit that references this issue on Dec 29, 2022
  9. added 2 commits that reference this issue on Jan 27, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions