Skip to content

Commit

Permalink
Rollup merge of rust-lang#47334 - etaoins:only-call-res-init-on-gnu-u…
Browse files Browse the repository at this point in the history
…nix, r=alexcrichton

Only link res_init() on GNU/*nix

To workaround a bug in glibc <= 2.26 lookup_host() calls res_init() based on the glibc version detected at runtime. While this avoids calling res_init() on platforms where it's not required we will still end up linking against the symbol.

This causes an issue on macOS where res_init() is implemented in a separate library (libresolv.9.dylib) from the main libc. While this is harmless for standalone programs it becomes a problem if Rust code is statically linked against another program. If the linked program doesn't already specify -lresolv it will cause the link to fail. This is captured in issue rust-lang#46797

Fix this by hooking in to the glibc workaround in `cvt_gai` and only activating it for the "gnu" environment on Unix This should include all glibc platforms while excluding musl, windows-gnu, macOS, FreeBSD, etc.

This has the side benefit of removing the #[cfg] in sys_common; only unix.rs has code related to the workaround now.

Before this commit:
```shell
> cat main.rs
use std::net::ToSocketAddrs;

#[no_mangle]
pub extern "C" fn resolve_test() -> () {
    let addr_list = ("google.com.au", 0).to_socket_addrs().unwrap();
    println!("{:?}", addr_list);
}
> rustc --crate-type=staticlib main.rs
> clang libmain.a test.c -o combined
Undefined symbols for architecture x86_64:
  "_res_9_init", referenced from:
      std::net::lookup_host::h93c17fe9ad38464a in libmain.a(std-826c8d3b356e180c.std0.rcgu.o)
ld: symbol(s) not found for architecture x86_64
clang-5.0: error: linker command failed with exit code 1 (use -v to see invocation)
```

Afterwards:
```shell
> rustc --crate-type=staticlib main.rs
> clang libmain.a test.c -o combined
> ./combined
IntoIter([V4(172.217.25.131:0)])
```

Fixes  rust-lang#46797
  • Loading branch information
GuillaumeGomez committed Jan 21, 2018
2 parents 6bb1b0d + 090a968 commit ab54a9c
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 32 deletions.
4 changes: 0 additions & 4 deletions src/libstd/sys/unix/l4re.rs
Original file line number Diff line number Diff line change
Expand Up @@ -437,9 +437,5 @@ pub mod net {
pub fn lookup_host(_: &str) -> io::Result<LookupHost> {
unimpl!();
}

pub fn res_init_if_glibc_before_2_26() -> io::Result<()> {
unimpl!();
}
}

20 changes: 13 additions & 7 deletions src/libstd/sys/unix/net.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ pub fn cvt_gai(err: c_int) -> io::Result<()> {
if err == 0 {
return Ok(())
}

// We may need to trigger a glibc workaround. See on_resolver_failure() for details.
on_resolver_failure();

if err == EAI_SYSTEM {
return Err(io::Error::last_os_error())
}
Expand Down Expand Up @@ -377,21 +381,22 @@ impl IntoInner<c_int> for Socket {
// res_init unconditionally, we call it only when we detect we're linking
// against glibc version < 2.26. (That is, when we both know its needed and
// believe it's thread-safe).
pub fn res_init_if_glibc_before_2_26() -> io::Result<()> {
#[cfg(target_env = "gnu")]
fn on_resolver_failure() {
// If the version fails to parse, we treat it the same as "not glibc".
if let Some(Ok(version_str)) = glibc_version_cstr().map(CStr::to_str) {
if let Some(version) = parse_glibc_version(version_str) {
if version < (2, 26) {
let ret = unsafe { libc::res_init() };
if ret != 0 {
return Err(io::Error::last_os_error());
}
unsafe { libc::res_init() };
}
}
}
Ok(())
}

#[cfg(not(target_env = "gnu"))]
fn on_resolver_failure() {}

#[cfg(target_env = "gnu")]
fn glibc_version_cstr() -> Option<&'static CStr> {
weak! {
fn gnu_get_libc_version() -> *const libc::c_char
Expand All @@ -405,6 +410,7 @@ fn glibc_version_cstr() -> Option<&'static CStr> {

// Returns Some((major, minor)) if the string is a valid "x.y" version,
// ignoring any extra dot-separated parts. Otherwise return None.
#[cfg(target_env = "gnu")]
fn parse_glibc_version(version: &str) -> Option<(usize, usize)> {
let mut parsed_ints = version.split(".").map(str::parse::<usize>).fuse();
match (parsed_ints.next(), parsed_ints.next()) {
Expand All @@ -413,7 +419,7 @@ fn parse_glibc_version(version: &str) -> Option<(usize, usize)> {
}
}

#[cfg(test)]
#[cfg(all(test, taget_env = "gnu"))]
mod test {
use super::*;

Expand Down
24 changes: 3 additions & 21 deletions src/libstd/sys_common/net.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,27 +166,9 @@ pub fn lookup_host(host: &str) -> io::Result<LookupHost> {
hints.ai_socktype = c::SOCK_STREAM;
let mut res = ptr::null_mut();
unsafe {
match cvt_gai(c::getaddrinfo(c_host.as_ptr(), ptr::null(), &hints, &mut res)) {
Ok(_) => {
Ok(LookupHost { original: res, cur: res })
},
#[cfg(unix)]
Err(e) => {
// If we're running glibc prior to version 2.26, the lookup
// failure could be caused by caching a stale /etc/resolv.conf.
// We need to call libc::res_init() to clear the cache. But we
// shouldn't call it in on any other platform, because other
// res_init implementations aren't thread-safe. See
// https://github.com/rust-lang/rust/issues/41570 and
// https://github.com/rust-lang/rust/issues/43592.
use sys::net::res_init_if_glibc_before_2_26;
let _ = res_init_if_glibc_before_2_26();
Err(e)
},
// the cfg is needed here to avoid an "unreachable pattern" warning
#[cfg(not(unix))]
Err(e) => Err(e),
}
cvt_gai(c::getaddrinfo(c_host.as_ptr(), ptr::null(), &hints, &mut res)).map(|_| {
LookupHost { original: res, cur: res }
})
}
}

Expand Down

0 comments on commit ab54a9c

Please sign in to comment.