Skip to content

Commit eb86913

Browse files
committed
auto merge of #12505 : alexcrichton/rust/fix-stack-overflow, r=brson
The printing of the error message on stack overflow had two sometimes false assumptions previously. The first is that a local task was always available (it called Local::take) and the second is that it used `println!` instead of manually writing. The first assumption isn't necessarily true because while stack overflow will likely only be detected in situations that a local task is available, it's not guaranteed to always be in TLS. For example, during a `println!` call a task may be blocking, causing it to be unavailable. By using Local::try_take(), we can be resilient against these occurrences. The second assumption could lead to odd behavior because the stdout logger can be overwritten to run arbitrary code. Currently this should be possible, but the utility is much diminished because a stack overflow translates to an abort() instead of a failure.
2 parents 2e1cfae + 4f4d43b commit eb86913

File tree

2 files changed

+60
-5
lines changed

2 files changed

+60
-5
lines changed

src/libstd/rt/stack.rs

+10-5
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ pub static RED_ZONE: uint = 20 * 1024;
3636
// irrelevant for documentation purposes.
3737
#[cfg(not(test))] // in testing, use the original libstd's version
3838
pub extern "C" fn rust_stack_exhausted() {
39-
use option::None;
39+
use option::{Option, None, Some};
4040
use rt::local::Local;
4141
use rt::task::Task;
4242
use str::Str;
@@ -85,16 +85,21 @@ pub extern "C" fn rust_stack_exhausted() {
8585
// #9854 - unwinding on windows through __morestack has never worked
8686
// #2361 - possible implementation of not using landing pads
8787

88-
let mut task = Local::borrow(None::<Task>);
89-
let n = task.get().name.as_ref()
90-
.map(|n| n.as_slice()).unwrap_or("<unnamed>");
88+
let task: Option<~Task> = Local::try_take();
89+
let name = match task {
90+
Some(ref task) => {
91+
task.name.as_ref().map(|n| n.as_slice())
92+
}
93+
None => None
94+
};
95+
let name = name.unwrap_or("<unknown>");
9196

9297
// See the message below for why this is not emitted to the
9398
// task's logger. This has the additional conundrum of the
9499
// logger may not be initialized just yet, meaning that an FFI
95100
// call would happen to initialized it (calling out to libuv),
96101
// and the FFI call needs 2MB of stack when we just ran out.
97-
println!("task '{}' has overflowed its stack", n);
102+
rterrln!("task '{}' has overflowed its stack", name);
98103

99104
intrinsics::abort();
100105
}

src/test/run-pass/out-of-stack.rs

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// Copyright 2012-2014 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+
// ignore-fast
12+
13+
#[feature(asm)];
14+
15+
use std::io::Process;
16+
use std::os;
17+
use std::str;
18+
19+
// lifted from the test module
20+
pub fn black_box<T>(dummy: T) { unsafe { asm!("" : : "r"(&dummy)) } }
21+
22+
fn silent_recurse() {
23+
let buf = [0, ..1000];
24+
black_box(buf);
25+
silent_recurse();
26+
}
27+
28+
fn loud_recurse() {
29+
println!("hello!");
30+
loud_recurse();
31+
}
32+
33+
fn main() {
34+
let args = os::args();
35+
if args.len() > 1 && args[1].as_slice() == "silent" {
36+
silent_recurse();
37+
} else if args.len() > 1 && args[1].as_slice() == "loud" {
38+
loud_recurse();
39+
} else {
40+
let silent = Process::output(args[0], [~"silent"]).unwrap();
41+
assert!(!silent.status.success());
42+
let error = str::from_utf8_lossy(silent.error);
43+
assert!(error.as_slice().contains("has overflowed its stack"));
44+
45+
let loud = Process::output(args[0], [~"loud"]).unwrap();
46+
assert!(!loud.status.success());
47+
let error = str::from_utf8_lossy(silent.error);
48+
assert!(error.as_slice().contains("has overflowed its stack"));
49+
}
50+
}

0 commit comments

Comments
 (0)