pathfinder icon indicating copy to clipboard operation
pathfinder copied to clipboard

pathfinder_simd: some SSE4.1 intrinsics used without checking if SSE4.1 is present

Open Shnatsel opened this issue 7 months ago • 2 comments

Running cargo +nightly miri test --release on these tests reports UB. These intrinsics require SSE 4.1 but the code didn't check that SSE 4.1 is available.

use crate::default::{F32x4, I32x4, U32x4};
use crate::scalar::F32x4 as F32x4S;

#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
mod test_a_ub_x86_sse41 {
    use crate::x86::{F32x2, F32x4, I32x2, I32x4};

    #[test]
    fn f32x2_floor_requires_sse41() {
        // Memory safety issue: this safe API routes through x86::_mm_floor_ps, which requires
        // SSE4.1. The x86 backend is selected without enabling or checking SSE4.1, so this is UB
        // on targets where that feature is unavailable; Miri reports the target-feature violation.
        let _ = F32x2::new(1.25, -2.75).floor();
    }

    #[test]
    fn f32x2_ceil_requires_sse41() {
        // Memory safety issue: this safe API routes through x86::_mm_ceil_ps, which requires
        // SSE4.1. The x86 backend is selected without enabling or checking SSE4.1, so this is UB
        // on targets where that feature is unavailable; Miri reports the target-feature violation.
        let _ = F32x2::new(1.25, -2.75).ceil();
    }

    #[test]
    fn f32x4_floor_requires_sse41() {
        // Memory safety issue: this safe API calls x86::_mm_floor_ps, which requires SSE4.1.
        // The x86 backend is selected without enabling or checking SSE4.1, so this is UB on
        // targets where that feature is unavailable; Miri reports the target-feature violation.
        let _ = F32x4::new(1.25, -2.75, 3.5, -4.5).floor();
    }

    #[test]
    fn f32x4_ceil_requires_sse41() {
        // Memory safety issue: this safe API calls x86::_mm_ceil_ps, which requires SSE4.1.
        // The x86 backend is selected without enabling or checking SSE4.1, so this is UB on
        // targets where that feature is unavailable; Miri reports the target-feature violation.
        let _ = F32x4::new(1.25, -2.75, 3.5, -4.5).ceil();
    }

    #[test]
    fn i32x2_min_requires_sse41() {
        // Memory safety issue: this safe API routes through x86::_mm_min_epi32, which requires
        // SSE4.1. The x86 backend is selected without enabling or checking SSE4.1, so this is UB
        // on targets where that feature is unavailable; Miri reports the target-feature violation.
        let _ = I32x2::new(6, -40).min(I32x2::new(10, 46));
    }

    #[test]
    fn i32x2_max_requires_sse41() {
        // Memory safety issue: this safe API routes through x86::_mm_max_epi32, which requires
        // SSE4.1. The x86 backend is selected without enabling or checking SSE4.1, so this is UB
        // on targets where that feature is unavailable; Miri reports the target-feature violation.
        let _ = I32x2::new(6, -40).max(I32x2::new(10, 46));
    }

    #[test]
    fn i32x2_mul_requires_sse41() {
        // Memory safety issue: this safe operator routes through x86::_mm_mullo_epi32, which
        // requires SSE4.1. The x86 backend is selected without enabling or checking SSE4.1, so
        // this is UB on targets where that feature is unavailable; Miri reports the violation.
        let _ = I32x2::new(6, -40) * I32x2::new(10, 46);
    }

    #[test]
    fn i32x4_min_requires_sse41() {
        // Memory safety issue: this safe API calls x86::_mm_min_epi32, which requires SSE4.1.
        // The x86 backend is selected without enabling or checking SSE4.1, so this is UB on
        // targets where that feature is unavailable; Miri reports the target-feature violation.
        let _ = I32x4::new(6, 29, -40, 2).min(I32x4::new(10, -5, 10, 46));
    }

    #[test]
    fn i32x4_max_requires_sse41() {
        // Memory safety issue: this safe API calls x86::_mm_max_epi32, which requires SSE4.1.
        // The x86 backend is selected without enabling or checking SSE4.1, so this is UB on
        // targets where that feature is unavailable; Miri reports the target-feature violation.
        let _ = I32x4::new(6, 29, -40, 2).max(I32x4::new(10, -5, 10, 46));
    }

    #[test]
    fn i32x4_mul_requires_sse41() {
        // Memory safety issue: this safe operator calls x86::_mm_mullo_epi32, which requires
        // SSE4.1. The x86 backend is selected without enabling or checking SSE4.1, so this is UB
        // on targets where that feature is unavailable; Miri reports the target-feature violation.
        let _ = I32x4::new(6, 29, -40, 2) * I32x4::new(10, -5, 10, 46);
    }
}

Shnatsel avatar May 02 '26 16:05 Shnatsel

I generate the same miri report when testing the following rust code:

#![feature(allocator_api)]
extern crate alloc;
use pathfinder_geometry::*;
fn main() {
    let v1 = 98i32;
    let v2 = 76i32;
    let v3 = util::alignup_i32(v1, v2);
    let v4 = -117i32;
    let v5 = vector::vec2i(v3, v4);
    let v6 = <vector::Vector2I as std::ops::Mul<vector::Vector2I>>::mul(v5, v5);
}

The command I used is: MIRIFLAGS="-Zmiri-ignore-leaks -Zmiri-tree-borrows" RUSTFLAGS=-Awarnings RUST_BACKTRACE=1 cargo miri run.

The miri report is:

error: Undefined Behavior: calling a function that requires unavailable target features: sse4.1
   --> /home/rose/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/pathfinder_simd-0.5.6/src/x86/mod.rs:780:24
    |
780 |         unsafe { I32x4(x86::_mm_mullo_epi32(self.0, other.0)) }
    |                        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Undefined Behavior occurred here
    |
    = 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 `<pathfinder_simd::x86::I32x4 as std::ops::Mul>::mul` at /home/rose/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/pathfinder_simd-0.5.6/src/x86/mod.rs:780:24: 780:61
    = note: inside `<pathfinder_simd::x86::I32x2 as std::ops::Mul>::mul` at /home/rose/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/pathfinder_simd-0.5.6/src/x86/mod.rs:594:9: 594:45
    = note: inside `<pathfinder_geometry::vector::Vector2I as std::ops::Mul>::mul` at /home/rose/projects/lifesonar-tests/pathfinder_geometry-0.5.1/src/vector.rs:409:18: 409:34
note: inside `main`
   --> src/main.rs:10:14
    |
 10 |     let v6 = <vector::Vector2I as std::ops::Mul<vector::Vector2I>>::mul(v5, v5);
    |              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace

error: aborting due to 1 previous error; 1 warning emitted

But I don't know whether this is a real bug, because running this code with command cargo run it didn't panic or output UB.

yilin0518 avatar May 10 '26 04:05 yilin0518

This is a real bug, and I have a "fix" for it in a fork: https://github.com/warpdotdev/pathfinder/commit/34128a129ca6aee168fbc5060a4f21b4f7e486a9

That patch fixed the actual issues encountered by our users on machines without SSE 4.1 support, but the fact that it does the runtime feature detection at every invocation is almost certainly a major performance concern that a real patch should better address.

vorporeal avatar Jun 02 '26 20:06 vorporeal