1 // Copyright 2013 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.
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.
11 // ignore-lexer-test FIXME #15677
18 use libc::{self, uintptr_t};
22 use sync::atomic::{self, Ordering};
24 /// Dynamically inquire about whether we're running under V.
25 /// You should usually not use this unless your test definitely
26 /// can't run correctly un-altered. Valgrind is there to help
27 /// you notice weirdness in normal, un-doctored code paths!
28 pub fn running_on_valgrind() -> bool {
30 fn rust_running_on_valgrind() -> uintptr_t;
32 unsafe { rust_running_on_valgrind() != 0 }
35 /// Valgrind has a fixed-sized array (size around 2000) of segment descriptors
36 /// wired into it; this is a hard limit and requires rebuilding valgrind if you
37 /// want to go beyond it. Normally this is not a problem, but in some tests, we
38 /// produce a lot of threads casually. Making lots of threads alone might not
39 /// be a problem _either_, except on OSX, the segments produced for new threads
40 /// _take a while_ to get reclaimed by the OS. Combined with the fact that libuv
41 /// schedulers fork off a separate thread for polling fsevents on OSX, we get a
42 /// perfect storm of creating "too many mappings" for valgrind to handle when
43 /// running certain stress tests in the runtime.
44 pub fn limit_thread_creation_due_to_osx_and_valgrind() -> bool {
45 (cfg!(target_os="macos")) && running_on_valgrind()
48 pub fn min_stack() -> uint {
49 static MIN: atomic::AtomicUsize = atomic::ATOMIC_USIZE_INIT;
50 match MIN.load(Ordering::SeqCst) {
54 let amt = os::getenv("RUST_MIN_STACK").and_then(|s| s.parse());
55 let amt = amt.unwrap_or(2 * 1024 * 1024);
56 // 0 is our sentinel value, so ensure that we'll never see 0 after
57 // initialization has run
58 MIN.store(amt + 1, Ordering::SeqCst);
62 /// Get's the number of scheduler threads requested by the environment
63 /// either `RUST_THREADS` or `num_cpus`.
64 pub fn default_sched_threads() -> uint {
65 match os::getenv("RUST_THREADS") {
67 let opt_n: Option<uint> = nstr.parse();
69 Some(n) if n > 0 => n,
70 _ => panic!("`RUST_THREADS` is `{}`, should be a positive integer", nstr)
74 if limit_thread_creation_due_to_osx_and_valgrind() {
83 // Indicates whether we should perform expensive sanity checks, including rtassert!
85 // FIXME: Once the runtime matures remove the `true` below to turn off rtassert,
87 pub const ENFORCE_SANITY: bool = true || !cfg!(rtopt) || cfg!(rtdebug) ||
90 #[allow(missing_copy_implementations)]
91 pub struct Stdio(libc::c_int);
93 #[allow(non_upper_case_globals)]
94 pub const Stdout: Stdio = Stdio(libc::STDOUT_FILENO);
95 #[allow(non_upper_case_globals)]
96 pub const Stderr: Stdio = Stdio(libc::STDERR_FILENO);
99 pub fn write_bytes(&mut self, data: &[u8]) {
101 type WriteLen = libc::size_t;
103 type WriteLen = libc::c_uint;
105 let Stdio(fd) = *self;
107 data.as_ptr() as *const libc::c_void,
108 data.len() as WriteLen);
113 impl fmt::Writer for Stdio {
114 fn write_str(&mut self, data: &str) -> fmt::Result {
115 self.write_bytes(data.as_bytes());
116 Ok(()) // yes, we're lying
120 pub fn dumb_print(args: fmt::Arguments) {
121 let _ = Stderr.write_fmt(args);
124 pub fn abort(args: fmt::Arguments) -> ! {
127 struct BufWriter<'a> {
131 impl<'a> fmt::Writer for BufWriter<'a> {
132 fn write_str(&mut self, bytes: &str) -> fmt::Result {
133 let left = self.buf.slice_from_mut(self.pos);
134 let to_write = &bytes.as_bytes()[..cmp::min(bytes.len(), left.len())];
135 slice::bytes::copy_memory(left, to_write);
136 self.pos += to_write.len();
141 // Convert the arguments into a stack-allocated string
142 let mut msg = [0u8; 512];
143 let mut w = BufWriter { buf: &mut msg, pos: 0 };
144 let _ = write!(&mut w, "{}", args);
145 let msg = str::from_utf8(&w.buf[..w.pos]).unwrap_or("aborted");
146 let msg = if msg.is_empty() {"aborted"} else {msg};
147 rterrln!("fatal runtime error: {}", msg);
148 unsafe { intrinsics::abort(); }
151 pub unsafe fn report_overflow() {
154 // See the message below for why this is not emitted to the
155 // ^ Where did the message below go?
156 // task's logger. This has the additional conundrum of the
157 // logger may not be initialized just yet, meaning that an FFI
158 // call would happen to initialized it (calling out to libuv),
159 // and the FFI call needs 2MB of stack when we just ran out.
161 rterrln!("\nthread '{}' has overflowed its stack",
162 Thread::current().name().unwrap_or("<unknown>"));