Seg-fault running cargo test without using unsafe!


#1

I think I should report this as a bug, but I’m not sure how to narrow it down, or identify where the bug is. I’m getting a segfault when running tests.

$ cargo test
   Compiling david-set v0.1.1 (file:///home/droundy/src/david-set)
    Finished dev [unoptimized + debuginfo] target(s) in 2.8 secs
     Running target/debug/deps/david_set-ac5684906c0623f5

running 15 tests
test castset::tests::it_works ... ok
test castset::tests::random_inserts_and_removals_u16 ... ok
test castset::tests::size_unwasted ... ok
test castset::tests::test_matches_u8 ... ok
test copyset::tests::it_works ... ok
test copyset::tests::size_unwasted ... ok
error: process didn't exit successfully: `/home/droundy/src/david-set/target/debug/deps/david_set-ac5684906c0623f5` (signal: 11, SIGSEGV: invalid memory reference)

To learn more, run the command again with --verbose.

I could start by pruning down my number of tests until I’ve just got the one. But right now anyone interested can reproduce this by git pulling from https://github.com/droundy/david-set and running cargo test.


#2

It’s a stack overflow.
You are creating huge amounts of data on the stack and then move them into a Box here: https://github.com/droundy/david-set/blob/e9def0a4e49c5a00d70ef996331ff70f8e539745/src/optcastset.rs#L165-L186

Rust is (currently) not smart enough to know that the arrays don’t need to be on the stack at all.
This could be solved by the box keyword/placement-new, which is both not stable yet.


#3

Thanks for the quick analysis!

Wow, that’s crazy! I would have assumed that Box::new would be a zero-copy operation. Is there no stable way to allocate a large array, then?!

It seems like even for small things creating data in-place would be a good optimization to have.


#4

You’d have to use a Vec. You can turn it into a boxed slice to avoid later reallocation.

The optimization of creating data in-place is exactly what placement-new would enable. It is just not stable yet (as it is a whole lot more complicated to get right)


#5

Are you using nightly rust newer than 2017-06-19 by any chance? There’s a bug currently that looks kind of like what you’re seeing: https://github.com/rust-lang/rust/issues/42903 A fix just went in and might be in tonight’s nightly?