Seg-fault running cargo test without using unsafe!

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 and running cargo test.

It's a stack overflow.
You are creating huge amounts of data on the stack and then move them into a Box here:

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.

1 Like

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.

1 Like

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)


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: A fix just went in and might be in tonight's nightly?

1 Like