Skip to content

Commit 5576b05

Browse files
committed
Auto merge of #24448 - alexcrichton:issue-24445, r=huonw
One of the parameters to the magical "register a thread-local destructor" function is called `__dso_handle` and largely just passed along (this seems to be what other implementations do). Currently we pass the *value* of this symbol, but apparently the correct piece of information to pass is the *address* of the symbol. In a PIE binary the symbol actually contains an address to itself which is why we've gotten away with what we're doing as long as we have. In a non-PIE binary the symbol contains the address `NULL`, causing a segfault in the runtime library if it keeps going. Closes #24445
2 parents ac2b6f6 + 3e57c6c commit 5576b05

File tree

4 files changed

+54
-1
lines changed

4 files changed

+54
-1
lines changed

src/libstd/thread/local.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,7 @@ mod imp {
373373
arg: *mut u8,
374374
dso_handle: *mut u8) -> libc::c_int;
375375
mem::transmute::<*const (), F>(__cxa_thread_atexit_impl)
376-
(dtor, t, __dso_handle);
376+
(dtor, t, &__dso_handle as *const _ as *mut _);
377377
return
378378
}
379379

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
-include ../tools.mk
2+
3+
ifeq ($(UNAME),Linux)
4+
all:
5+
$(RUSTC) foo.rs
6+
$(CC) foo.c -lfoo -L $(TMPDIR) -Wl,--gc-sections -lpthread -o $(TMPDIR)/foo
7+
$(call RUN,foo)
8+
$(CC) foo.c -lfoo -L $(TMPDIR) -Wl,--gc-sections -lpthread -pie -fPIC -o $(TMPDIR)/foo
9+
$(call RUN,foo)
10+
else
11+
all:
12+
endif

src/test/run-make/issue-24445/foo.c

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
void foo();
12+
13+
int main() {
14+
foo();
15+
return 0;
16+
}

src/test/run-make/issue-24445/foo.rs

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
#![crate_type = "staticlib"]
12+
13+
struct Destroy;
14+
impl Drop for Destroy {
15+
fn drop(&mut self) { println!("drop"); }
16+
}
17+
18+
thread_local! {
19+
static X: Destroy = Destroy
20+
}
21+
22+
#[no_mangle]
23+
pub extern "C" fn foo() {
24+
X.with(|_| ());
25+
}

0 commit comments

Comments
 (0)