Skip to content

Segfault in je_rallocx after pushing new value to Vec<u8> causing it to resize #27878

@ashleysommer

Description

@ashleysommer

Im getting this very interesting segfault when using with CStrings in a project Im working on.

Program received signal SIGSEGV, Segmentation fault.
0x0000555555574551 in je_rallocx ()
Missing separate debuginfos, use: dnf debuginfo-install fcgi-2.4.0-28.fc23.x86_64
(gdb) bt
#0  0x0000555555574551 in je_rallocx ()
#1  0x000055555555dfc3 in fcgi::heap::reallocate (ptr=0x5555557af9fc "REQUEST_METHOD", old_size=14, size=28, align=1) at ../src/liballoc/heap.rs:71
#2  0x000055555555de5f in fcgi::raw_vec::RawVec<T>::double (self=0x7fffffffd188) at ../src/liballoc/raw_vec.rs:202
#3  0x000055555555dbf1 in fcgi::vec::Vec<T>::push (self=0x7fffffffd188, value=0 '\000') at ../src/libcollections/vec.rs:577
#4  0x000055555555d8dc in fcgi::DefaultRequest.Request::get_param (self=0x7fffffffd868, name=...) at /home/flubba86/workspace/rust-fcgi/src/lib.rs:153
#5  0x0000555555559c60 in testcase:main () at src/main.rs:19
#6  0x000055555556abc5 in rt::unwind::try::try_fn::h5614954417008942540 ()
#7  0x0000555555568829 in __rust_try ()
#8  0x000055555556a8b2 in rt::lang_start::ha172a3ce74bb453aK5w ()
#9  0x000055555555d0f7 in main ()

I am passing a static &str with the contents "REQUEST_METHOD" to get_param() in the fcgi lib. The get_param() method converts that to a CString using CString::new(). To do that, the rust CString new() constructor method first converts the &str into a Vec<u8> using into(). It then does a .push(0) to append a null char, then finally returns a new CString using into_boxed_slice on the Vec.

The error occurs on the .push(0) line here. The Vec<u8> is created with an initial size being the length of the string, and to add the new char it must call double() on the Vec to make it bigger. When it does that, the allocator segfaults during the jemalloc reallocation call.

I isolated the issue further by first converting the &str to a Vec<u8> myself then doing .push(0) on it before passing it to the CString constructor. The push() still crashes when reallocating, as seen in the pasted trace above.

I cannot however, create a minimal reproduction of the error. It only does it in this one library. I have tried multiple basic test cases and they all seem to work fine.

I was using a Nightly from Saturday the 15th of August and I just updated to the Current nightly (18th of August) and it is still the same.

Any suggestions on how I can further debug this? Temporarily, is there any way of creating a CString from a &str without using an intermediate Vec?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions