nalgebra icon indicating copy to clipboard operation
nalgebra copied to clipboard

Undefined Behavior in some decomposition methods on `SMatrix`

Open JarredAllen opened this issue 1 year ago • 1 comments

I have some code that uses this library and was hitting undefined behavior as reported by Miri, and I was able to simplify it to this simple example that produces UB without any use of unsafe:

fn main() {
    let mut mat = nalgebra::SMatrix::<f32, 2, 2>::zeros_generic(nalgebra::Const, nalgebra::Const);
    mat[(0, 0)] = 1.0;
    mat[(1, 1)] = 1.0;
    println!("{:?}", mat.cholesky());
}

When run under cargo +nightly miri run, it reports this output:

error: Undefined Behavior: trying to retag from <2370> for Unique permission at alloc642[0xc], but that tag does not exist in the borrow stack for this location
   --> /home/jarred/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/slice/raw.rs:192:9
    |
192 |         &mut *ptr::slice_from_raw_parts_mut(data, len)
    |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    |         |
    |         trying to retag from <2370> for Unique permission at alloc642[0xc], but that tag does not exist in the borrow stack for this location
    |         this error occurs as part of retag at alloc642[0xc..0x10]
    |
    = help: this indicates a potential bug in the program: it performed an invalid operation, but the Stacked Borrows rules it violated are still experimental
    = help: see https://github.com/rust-lang/unsafe-code-guidelines/blob/master/wip/stacked-borrows.md for further information
help: <2370> was created by a SharedReadWrite retag at offsets [0x0..0x10]
   --> src/main.rs:5:22
    |
5   |     println!("{:?}", mat.cholesky());
    |                      ^^^^^^^^^^^^^^
help: <2370> was later invalidated at offsets [0x0..0x10] by a Unique retag
   --> src/main.rs:5:22
    |
5   |     println!("{:?}", mat.cholesky());
    |                      ^^^^^^^^^^^^^^
    = note: BACKTRACE (of the first span):
    = note: inside `std::slice::from_raw_parts_mut::<'_, f32>` at /home/jarred/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/slice/raw.rs:192:9: 192:55
    = note: inside `<nalgebra::ViewStorageMut<'_, f32, nalgebra::Dyn, nalgebra::Const<1>, nalgebra::Const<1>, nalgebra::Const<2>> as nalgebra::RawStorageMut<f32, nalgebra::Dyn>>::as_mut_slice_unchecked` at /home/jarred/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/nalgebra-0.33.2/src/base/matrix_view.rs:256:13: 256:56
    = note: inside `nalgebra::base::blas_uninit::axcpy_uninit::<nalgebra::uninit::Init, f32, nalgebra::Dyn, nalgebra::Dyn, nalgebra::ViewStorageMut<'_, f32, nalgebra::Dyn, nalgebra::Const<1>, nalgebra::Const<1>, nalgebra::Const<2>>, nalgebra::ViewStorage<'_, f32, nalgebra::Dyn, nalgebra::Const<1>, nalgebra::Const<1>, nalgebra::Const<2>>>` at /home/jarred/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/nalgebra-0.33.2/src/base/blas_uninit.rs:105:13: 105:44
    = note: inside `nalgebra::base::blas::<impl nalgebra::Matrix<f32, nalgebra::Dyn, nalgebra::Const<1>, nalgebra::ViewStorageMut<'_, f32, nalgebra::Dyn, nalgebra::Const<1>, nalgebra::Const<1>, nalgebra::Const<2>>>>::axcpy::<nalgebra::Dyn, nalgebra::ViewStorage<'_, f32, nalgebra::Dyn, nalgebra::Const<1>, nalgebra::Const<1>, nalgebra::Const<2>>>` at /home/jarred/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/nalgebra-0.33.2/src/base/blas.rs:300:18: 300:54
    = note: inside `nalgebra::base::blas::<impl nalgebra::Matrix<f32, nalgebra::Dyn, nalgebra::Const<1>, nalgebra::ViewStorageMut<'_, f32, nalgebra::Dyn, nalgebra::Const<1>, nalgebra::Const<1>, nalgebra::Const<2>>>>::axpy::<nalgebra::Dyn, nalgebra::ViewStorage<'_, f32, nalgebra::Dyn, nalgebra::Const<1>, nalgebra::Const<1>, nalgebra::Const<2>>>` at /home/jarred/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/nalgebra-0.33.2/src/base/blas.rs:323:9: 323:38
    = note: inside `nalgebra::Cholesky::<f32, nalgebra::Const<2>>::new_internal` at /home/jarred/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/nalgebra-0.33.2/src/linalg/cholesky.rs:233:17: 233:65
    = note: inside `nalgebra::Cholesky::<f32, nalgebra::Const<2>>::new` at /home/jarred/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/nalgebra-0.33.2/src/linalg/cholesky.rs:196:9: 196:41
    = note: inside `nalgebra::linalg::decomposition::<impl nalgebra::Matrix<f32, nalgebra::Const<2>, nalgebra::Const<2>, nalgebra::ArrayStorage<f32, 2, 2>>>::cholesky` at /home/jarred/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/nalgebra-0.33.2/src/linalg/decomposition.rs:260:9: 260:41
note: inside `main`
   --> src/main.rs:5:22
    |
5   |     println!("{:?}", mat.cholesky());
    |                      ^^^^^^^^^^^^^^

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

error: aborting due to 1 previous error

This seems to happen on the latest release to crates.io (0.33.2) as well as if I use the main branch of this repo. And cholesky() isn't the only method, hessenburg() also encounters UB according to Miri.

JarredAllen avatar May 29 '25 21:05 JarredAllen

I dug into this and traced the UB to axcpy_uninit in src/base/blas_uninit.rs (lines 108-109). Here's what's happening:

When decompositions split a matrix with columns_range_pair_mut and then call axpy on the resulting views, both views hold pointers into the same backing ArrayStorage. The problem is that axcpy_uninit converts both to slices:

let y = y.data.as_mut_slice_unchecked();  // from_raw_parts_mut → Unique retag
let x = x.data.as_slice_unchecked();       // reads from now-invalidated tag

The &mut [T] creation performs a Stacked Borrows Unique retag that kills the shared borrow used by the &[T] slice. This affects basically every decomposition that goes through the columns_range_pair_mut → BLAS path: Cholesky, LU, QR, Hessenberg, Schur, SVD, Bidiagonal, SymmetricTridiagonal, SymmetricEigen.

Proposed fix: rewrite the low-level helpers (array_axcpy, array_axc) to work with raw pointers instead of slices, and have axcpy_uninit pass ptr_mut()/ptr() instead of creating slices. This avoids the retag entirely. The change is contained to blas_uninit.rs — decomposition code and the view system stay untouched.

A couple things I want to be upfront about:

  • I also considered fixing this at the view level (as_mut_slice_unchecked itself, or columns_range_pair_mut), but that's a much more invasive change and the slice-based API is used broadly. The pointer approach in blas_uninit.rs is more surgical.
  • gemv_uninit and gemm_uninit need the same audit. gemm_uninit's matrixmultiply path already uses raw pointers so it's likely fine, but the fallback path needs checking.
  • Moving from slice iteration to pointer arithmetic could theoretically affect auto-vectorization. I'll benchmark to check, but for the small matrices where this code path runs (large matrices go through matrixmultiply), I don't expect it to matter.

Working on a PR now.

pandashark avatar Feb 18 '26 16:02 pandashark