Skip to content

Invalid loads in core::simd::splat for vectors with >= 4 elements. #605

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

Open
FractalFir opened this issue Jan 22, 2025 · 1 comment
Open
Labels
bug Something isn't working

Comments

@FractalFir
Copy link
Contributor

Calling core::simd::u8x4::splat(or any splat with 4 or more elements) with -fsantize=undefined results in invalid loads:

   // OK
   println!("u8x1");
    let v = simd::u8x1::splat(8);
    black_box(v);
    println!("u8x2");
    let v = simd::u8x2::splat(8);
    black_box(v);
    // Memory error
    println!("u8x4");
    let v = simd::u8x4::splat(8);
    black_box(v);
u8x1
u8x2
u8x4
/home/michal/rustc_codegen_gcc/build/build_sysroot/sysroot_src/library/core/src/../../portable-simd/crates/core_simd/src/swizzle.rs:90:13: runtime error: load of address 0x7fff8d6fd230 with insufficient space for an object of type 'unsigned char'
0x7fff8d6fd230: note: pointer points here
 ff 7f 08 08  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  7c d2 6f 8d
              ^ 
/home/michal/rustc_codegen_gcc/build/build_sysroot/sysroot_src/library/core/src/../../portable-simd/crates/core_simd/src/swizzle.rs:90:13: runtime error: load of address 0x7fff8d6fd231 with insufficient space for an object of type 'unsigned char'
0x7fff8d6fd231: note: pointer points here
 7f 08 08  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  7c d2 6f 8d ff
              ^ 
/home/michal/rustc_codegen_gcc/build/build_sysroot/sysroot_src/library/core/src/../../portable-simd/crates/core_simd/src/swizzle.rs:90:13: runtime error: load of address 0x7fff8d6fd230 with insufficient space for an object of type 'unsigned char'
0x7fff8d6fd230: note: pointer points here
 ff 7f 08 08  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  08 08 00 00 00 00 00 00  7c d2 6f 8d
              ^ 
/home/michal/rustc_codegen_gcc/build/build_sysroot/sysroot_src/library/core/src/../../portable-simd/crates/core_simd/src/swizzle.rs:90:13: runtime error: load of address 0x7fff8d6fd231 with insufficient space for an object of type 'unsigned char'
0x7fff8d6fd231: note: pointer points here
 7f 08 08  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  08 08 00 00 00 00 00 00  7c d2 6f 8d ff
              ^ 
/home/michal/rustc_codegen_gcc/build/build_sysroot/sysroot_src/library/core/src/../../portable-simd/crates/core_simd/src/swizzle.rs:90:13: runtime error: load of address 0x7fff8d6fd230 with insufficient space for an object of type 'unsigned char'
0x7fff8d6fd230: note: pointer points here
 ff 7f 08 08  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  08 08 00 00 00 00 00 00  08 08 00 00
              ^ 
/home/michal/rustc_codegen_gcc/build/build_sysroot/sysroot_src/library/core/src/../../portable-simd/crates/core_simd/src/swizzle.rs:90:13: runtime error: load of address 0x7fff8d6fd231 with insufficient space for an object of type 'unsigned char'
0x7fff8d6fd231: note: pointer points here
 7f 08 08  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  08 08 00 00 00 00 00 00  08 08 00 00 00
              ^ 
/home/michal/rustc_codegen_gcc/build/build_sysroot/sysroot_src/library/core/src/../../portable-simd/crates/core_simd/src/swizzle.rs:90:13: runtime error: load of address 0x7fff8d6fd230 with insufficient space for an object of type 'unsigned char'
0x7fff8d6fd230: note: pointer points here
 ff 7f 08 08  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  08 08 00 00 00 00 00 00  08 08 00 00
              ^ 
/home/michal/rustc_codegen_gcc/build/build_sysroot/sysroot_src/library/core/src/../../portable-simd/crates/core_simd/src/swizzle.rs:90:13: runtime error: load of address 0x7fff8d6fd231 with insufficient space for an object of type 'unsigned char'
0x7fff8d6fd231: note: pointer points here
 7f 08 08  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  08 08 00 00 00 00 00 00  08 08 00 00 00
              ^ 

This issue only occurs for vectors with more than 4 elements - the type of the element does not matter:

u64x4
/home/michal/rustc_codegen_gcc/build/build_sysroot/sysroot_src/library/core/src/../../portable-simd/crates/core_simd/src/swizzle.rs:90:13: runtime error: load of address 0x7ffce42ec4f0 with insufficient space for an object of type 'size_t'
0x7ffce42ec4f0: note: pointer points here
 00 00 00 00  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  40 81 f4 cb 73 55 00 00  d8 c4 2e e4
              ^ 
f32x4
/home/michal/rustc_codegen_gcc/build/build_sysroot/sysroot_src/library/core/src/../../portable-simd/crates/core_simd/src/swizzle.rs:90:13: runtime error: load of address 0x7ffce42ec620 with insufficient space for an object of type '<unknown>'
0x7ffce42ec620: note: pointer points here
 00 00 00 41  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  a0 80 f4 cb 73 55 00 00  00 00 00 41
              ^ 
i8x4
/home/michal/rustc_codegen_gcc/build/build_sysroot/sysroot_src/library/core/src/../../portable-simd/crates/core_simd/src/swizzle.rs:90:13: runtime error: load of address 0x7ffce42ec5f0 with insufficient space for an object of type 'signed char'
0x7ffce42ec5f0: note: pointer points here
 fc 7f 08 08  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  3c c6 2e e4

This issue(discovered when running the core test suite) does not seem to lead to crashes / incorrect behavaiour.

@antoyo antoyo added the bug Something isn't working label Jan 22, 2025
@FractalFir
Copy link
Contributor Author

FractalFir commented Jan 23, 2025

Reduced example:

#![feature(portable_simd,core_intrinsics,repr_simd)]
const CONST:std::simd::u8x1 = {
    unsafe{std::mem::transmute(0_u8)} // Any byte works here. This does not *have* to be const, but the issue still persists if it is const.
};
#[no_mangle]
fn problem_simd()->std::simd::u8x4{
    unsafe{
        core::intrinsics::simd::simd_shuffle(
                CONST,
                CONST,
                const {
                    #[repr(simd)]
                    struct SimdShuffleIdx([u32; 4]);
                    SimdShuffleIdx([0; 4])
                },
            )
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants