alexcrichton / curl-rust

Rust bindings to libcurl
MIT License
1.01k stars 234 forks source link

curl::init::init_inner segfault #402

Closed 0x6273 closed 2 years ago

0x6273 commented 2 years ago
cargo test
warning: use of deprecated type alias `libc::time_t`: This type is changed to 64-bit in musl 1.2.0, we'll follow that change in the future release. See #1848 for more info.
  --> curl-sys/lib.rs:15:78
   |
15 | use libc::{c_char, c_double, c_int, c_long, c_short, c_uint, c_void, size_t, time_t};
   |                                                                              ^^^^^^
   |
   = note: `#[warn(deprecated)]` on by default

warning: use of deprecated type alias `libc::time_t`: This type is changed to 64-bit in musl 1.2.0, we'll follow that change in the future release. See #1848 for more info.
   --> curl-sys/lib.rs:112:15
    |
112 |     pub time: time_t,
    |               ^^^^^^

warning: use of deprecated type alias `libc::time_t`: This type is changed to 64-bit in musl 1.2.0, we'll follow that change in the future release. See #1848 for more info.
    --> curl-sys/lib.rs:1028:53
     |
1028 |     pub fn curl_getdate(p: *const c_char, _: *const time_t) -> time_t;
     |                                                     ^^^^^^

warning: use of deprecated type alias `libc::time_t`: This type is changed to 64-bit in musl 1.2.0, we'll follow that change in the future release. See #1848 for more info.
    --> curl-sys/lib.rs:1028:64
     |
1028 |     pub fn curl_getdate(p: *const c_char, _: *const time_t) -> time_t;
     |                                                                ^^^^^^

warning: `curl-sys` (lib) generated 4 warnings
    Finished test [unoptimized + debuginfo] target(s) in 0.03s
     Running unittests (target/debug/deps/curl-8a103e1eaa781301)
error: test failed, to rerun pass '--lib'

Caused by:
  process didn't exit successfully: `/home/user/Projects/curl-rust/target/debug/deps/curl-8a103e1eaa781301` (signal: 11, SIGSEGV: invalid memory reference)

gdb target/debug/deps/curl-8a103e1eaa781301
GNU gdb (Gentoo 10.2 vanilla) 10.2
Copyright (C) 2021 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Type "show copying" and "show warranty" for details.
This GDB was configured as "x86_64-gentoo-linux-musl".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<https://bugs.gentoo.org/>.
Find the GDB manual and other documentation resources online at:
    <http://www.gnu.org/software/gdb/documentation/>.

For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from target/debug/deps/curl-8a103e1eaa781301...
warning: Missing auto-load script at offset 0 in section .debug_gdb_scripts
of file /home/user/Projects/curl-rust/target/debug/deps/curl-8a103e1eaa781301.
Use `info auto-load python-scripts [REGEXP]' to list them.
(gdb) run
Starting program: /home/user/Projects/curl-rust/target/debug/deps/curl-8a103e1eaa781301

Program received signal SIGSEGV, Segmentation fault.
0x0000000000000000 in ?? ()
(gdb) bt
#0  0x0000000000000000 in ?? ()
#1  0x00007ffff7f3513f in curl::init::init_inner::{closure#0} () at src/lib.rs:114
#2  0x00007ffff7f35705 in std::sync::once::{impl#2}::call_once::{closure#0}<curl::init::init_inner::{closure#0}> ()
    at /rustc/a0035916e01d8e644ccd44554c57f0874cef8c8c/library/std/src/sync/once.rs:261
#3  0x00007ffff7f33a5c in std::sync::once::Once::call_inner ()
    at library/std/src/sync/once.rs:418
#4  0x00007ffff7f35696 in std::sync::once::Once::call_once<curl::init::init_inner::{closure#0}> (self=0x7ffff7ffd228 <curl::init::INIT>, f=...)
    at /rustc/a0035916e01d8e644ccd44554c57f0874cef8c8c/library/std/src/sync/once.rs:261
#5  0x00007ffff7f34acd in curl::init::init_inner () at src/lib.rs:109
#6  0x00007ffff7fb2e2c in libc_start_init ()
#7  0x00007ffff7f35110 in ?? ()
#8  0x00007ffff7fb2e51 in libc_start_main_stage2 ()
#9  0x00007ffff7fb2e34 in libc_start_init ()
#10 0x0000000000000000 in ?? ()
(gdb)

This happens on both master and latest release. I have tried both with and without static-curl feature. OS is Gentoo Linux with musl libc.

0x6273 commented 2 years ago

With static-ssl I get different results:

Running tests/protocols.rs (target/debug/deps/protocols-591b3709b88a6b4a)

running 1 test
error: test failed, to rerun pass '--test protocols'

Caused by:
  process didn't exit successfully: `/home/user/Projects/curl-rust/target/debug/deps/protocols-591b3709b88a6b4a` (signal: 11, SIGSEGV: invalid memory reference)

The examples seem to run now, which they did not when I ran them without static-ssl.

sagebind commented 2 years ago

Here's the issue described in the "deprecation" notice: https://github.com/rust-lang/libc/issues/1848. It appears the size of time_t under musl has/is changing and probably caused an incorrect compilation with the wrong size for time_t being used, hence the SIGSEGV. Reading the libc thread I'm not sure yet what we're supposed to change to make this work.

sagebind commented 2 years ago

Hmm, this upstream issue in the openssl crate seems to identify the issue: https://github.com/sfackler/rust-openssl/issues/1462

In particular:

The system-installed copy of OpenSSL dynamically links to the system MUSL, but Rust will by default statically link to a bundled MUSL, and that causes things to break when calling into OpenSSL.

You should either build an OpenSSL that also statically links to MUSL (e.g. with the vendored cargo feature), or tell Rust to also dynamically link to the system musl by setting RUSTFLAGS=-Ctarget-feature=-crt-static.

I was able to confirm this behavior on Alpine Linux as well which uses musl; as soon as OpenSSL tries to initialize you get a segfault of some kind, which happens when curl initializes. Enabling the static-ssl feature seems to be the correct fix when targeting such musl systems. This happens just with the openssl crate by itself without curl or curl-sys even involved.

The warnings about time_t seem to be a red herring.

sagebind commented 2 years ago

Closing as this is a platform-specific issue with OpenSSL and isn't specific to curl. Not much we can do about this.