GuillaumeGomez / rust-GSL

A GSL (the GNU Scientific Library) binding for Rust

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Seg Fault using Minimization and Root

catompkins opened this issue · comments

Hi,

I was working with the library and ran into a Segfault when trying the root and minimization functions. Perhaps I'm missing how you intended them to be applied.

Running Rust 1.54 on MacOS 11.5.2 with GSL 2.7.

anyway here's an MWE for the minimizer. However, the application for roots is the same but with a different convergence check:

#[cfg(test)]
mod test {
    use super::*;
    use minimizer::test_interval;
    extern crate assert_approx_eq;

    fn quadratic_test_fn(x: f64) -> f64 {
        x.powf(2.0) - 5.0
    }

    #[test]
    fn test_min() {
        let mut min = Minimizer::new(MinimizerType::brent()).unwrap();
        min.set(&quadratic_test_fn, 1.0, -5.0, 5.0);

        let max_iter = 5_usize;
        let eps_abs = 0.0001;
        let eps_rel = 0.0000001;

        let mut status = ::Value::Continue;
        let mut iter = 0_usize;

        while matches!(status, ::Value::Continue) && iter < max_iter {
            // iterate for next value
            status = min.iterate();  // fails here w/ segfault

            // test for convergence
            let r = min.minimum();
            let x_lo = min.x_lower();
            let x_hi = min.x_upper();

            status = test_interval(x_lo, x_hi, eps_abs, eps_rel);

            // check if iteration succeeded
            if matches!(status, ::Value::Success) {
                println!("Converged");
            }

            // print current iteration
            println!("{} [{}, {}] {} {}",iter, x_lo, x_hi, r, x_hi - x_lo);

            iter += 1;
        }

        assert_approx_eq::assert_approx_eq!(min.minimum(), 0.0_f64.sqrt())
    }
}

The failure occurs when calling min.iterate().

Here's the output:

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

Caused by:
  process didn't exit successfully: `/rust-GSL/target/debug/deps/rgsl-13d5c769f322bfbb 'types::minimizer::test' --show-output --nocapture` (signal: 11, SIGSEGV: invalid memory reference)

I was able to get some more information with lldb --file:

* thread #1, queue = 'com.apple.main-thread', stop reason = EXC_BAD_ACCESS (code=2, address=0x100504608)
  * frame #0: 0x0000000100504608
    frame #1: 0x00000001002000a9 libgsl.25.dylib`brent_iterate + 677
    frame #2: 0x00000001001ffbd6 libgsl.25.dylib`gsl_min_fminimizer_iterate + 51
    frame #3: 0x0000000100001c4e other`rgsl::types::minimizer::Minimizer::iterate::h9067c94a769cfa90(self=0x00007ffeefbff180) at minimizer.rs:251:32
    frame #4: 0x0000000100000f89 other`other::main::hdb959a6f26461a41 at other.rs:24:18

Not sure what the problem is at this point...

The same problem also exists on linux, it'll make the debugging easier hopefully...

Found the issue, the problem was on the bindings.