Skip to content
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

Weird UB when using sprs in nightly release mode #370

Closed
Specy opened this issue Oct 29, 2024 · 8 comments
Closed

Weird UB when using sprs in nightly release mode #370

Specy opened this issue Oct 29, 2024 · 8 comments

Comments

@Specy
Copy link

Specy commented Oct 29, 2024

Is there anything I need to know about using the safe Api to prevent unsoundness?

I had to fork an unmaintained library whose only dependency is sprs, it does not use any unsafe but I'm experiencing really weird bugs that don't make any sense, and that seem to be UB. They happen only in opt-mode >= 1 and miri doesn't seem to catch anything

@mulimoen
Copy link
Collaborator

mulimoen commented Oct 30, 2024

If it is just sprs and not the bindings then unsafe is only used for upholding the guarantee of sorted indices. This is just a correctness and performance thing, and not unsoundness

@Specy
Copy link
Author

Specy commented Oct 30, 2024

Unsoundness is the first thing that came to mind because of this:

20241029-2125-25.0816714.mp4

Where adding a print statement makes the code not panic on a guaranteed unwrap

Where the function returns None even though the branch of the None isn't hit (you can't see that on this video, it shows something else), and only returns None if compiled with opt-mode >= 1 (there is no race conditions, multi thread or IO that could cause this as simply a performance issue)

I'll investigate more today, just wanted to make sure that the bug wasn't coming from incorrect usage of sprs

@mulimoen
Copy link
Collaborator

That sure looks like undef behaviour. If the source is available then I'd be happy to take a look at it this afternoon

@Specy
Copy link
Author

Specy commented Oct 30, 2024

I made a minimal example here: https://github.com/Specy/microlp/tree/panic-bug, i've narrowed down the bug a bit, it only happens in release mode in nightly, running it on stable seems to work.

To reproduce the bug just run cargo run it should run with no errors, then run cargo run --release (in nightly!) and there should be a panic

I've used a debugger and found an illegal memory read in the unwrap

I've used nightly 1.83.0 and 1.84.0, bug happens in both, on stable and nightly 1.82.0 it doesn't happen

I had forked an unmaintained lib that uses this crate, i updated all deps to latest but this bug was happening also for sprs 0.9.2

@Specy Specy changed the title Any gotchas using the safe API? Weird UB when using sprs in nightly release mode Oct 30, 2024
@mulimoen
Copy link
Collaborator

It is not clear it is a bug in sprs, try the following:

order_simple(4, |c| {
        match c {
            0 => &[0, 1, 2, 3],
            1 => &[2],
            2 => &[0, 1],
            3 => &[1, 2, 3],
            _ => unreachable!(),
        }
    });

@Specy
Copy link
Author

Specy commented Oct 30, 2024

It is not clear it is a bug in sprs, try the following:

order_simple(4, |c| {
        match c {
            0 => &[0, 1, 2, 3],
            1 => &[2],
            2 => &[0, 1],
            3 => &[1, 2, 3],
            _ => unreachable!(),
        }
    });

Just tried that, the error still happens, i've filed a bug on the rust repo, just removed the sprs dependency, should i close this issue?

@mulimoen
Copy link
Collaborator

Yes, lets close this

@Specy
Copy link
Author

Specy commented Oct 30, 2024

Thanks for helping narrow down the bug!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants