From 63d18658c1d19bdcc4cad91ce808a9590b53f3d2 Mon Sep 17 00:00:00 2001 From: Brian Anderson Date: Tue, 12 Mar 2013 12:23:24 -0700 Subject: [PATCH] rt: Add RUST_DEBUG_MEM to rust_env to avoid races --- src/libcore/cleanup.rs | 6 +----- src/libcore/rt/env.rs | 47 +++++++++++++++++++++++++++++++++++++++++ src/libcore/rt/mod.rs | 1 + src/rt/rust_builtin.cpp | 5 +++++ src/rt/rust_env.cpp | 2 ++ src/rt/rust_env.h | 8 +++++-- src/rt/rustrt.def.in | 1 + 7 files changed, 63 insertions(+), 7 deletions(-) create mode 100644 src/libcore/rt/env.rs diff --git a/src/libcore/cleanup.rs b/src/libcore/cleanup.rs index faa6db45df2..a46e9154703 100644 --- a/src/libcore/cleanup.rs +++ b/src/libcore/cleanup.rs @@ -145,11 +145,7 @@ unsafe fn each_live_alloc(f: &fn(box: *mut BoxRepr, uniq: bool) -> bool) { #[cfg(unix)] fn debug_mem() -> bool { - use os; - use libc; - do os::as_c_charp("RUST_DEBUG_MEM") |p| { - unsafe { libc::getenv(p) != null() } - } + ::rt::env::get().debug_mem } #[cfg(windows)] diff --git a/src/libcore/rt/env.rs b/src/libcore/rt/env.rs new file mode 100644 index 00000000000..008e31777b0 --- /dev/null +++ b/src/libcore/rt/env.rs @@ -0,0 +1,47 @@ +// Copyright 2013 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +//! Runtime environment settings + +use libc::{size_t, c_char, c_int}; + +pub struct Environment { + /// The number of threads to use by default + num_sched_threads: size_t, + /// The minimum size of a stack segment + min_stack_size: size_t, + /// The maximum amount of total stack per task before aborting + max_stack_size: size_t, + /// The default logging configuration + logspec: *c_char, + /// Record and report detailed information about memory leaks + detailed_leaks: bool, + /// Seed the random number generator + rust_seed: *c_char, + /// Poison allocations on free + poison_on_free: bool, + /// The argc value passed to main + argc: c_int, + /// The argv value passed to main + argv: **c_char, + /// Print GC debugging info + debug_mem: bool +} + +/// Get the global environment settings +/// # Safety Note +/// This will abort the process if run outside of task context +pub fn get() -> &Environment { + unsafe { rust_get_rt_env() } +} + +extern { + fn rust_get_rt_env() -> &Environment; +} \ No newline at end of file diff --git a/src/libcore/rt/mod.rs b/src/libcore/rt/mod.rs index ea3878adbf0..a1a9884aeca 100644 --- a/src/libcore/rt/mod.rs +++ b/src/libcore/rt/mod.rs @@ -45,3 +45,4 @@ mod work_queue; mod stack; mod context; mod thread; +pub mod env; diff --git a/src/rt/rust_builtin.cpp b/src/rt/rust_builtin.cpp index ccb2c0c5c1c..a2053c115bb 100644 --- a/src/rt/rust_builtin.cpp +++ b/src/rt/rust_builtin.cpp @@ -876,6 +876,11 @@ rust_dbg_extern_identity_u8(char u) { return u; } +extern "C" rust_env* +rust_get_rt_env() { + rust_task *task = rust_get_current_task(); + return task->kernel->env; +} // // Local Variables: diff --git a/src/rt/rust_env.cpp b/src/rt/rust_env.cpp index b2df5f03b23..cade5f1ed2c 100644 --- a/src/rt/rust_env.cpp +++ b/src/rt/rust_env.cpp @@ -23,6 +23,7 @@ #define DETAILED_LEAKS "DETAILED_LEAKS" #define RUST_SEED "RUST_SEED" #define RUST_POISON_ON_FREE "RUST_POISON_ON_FREE" +#define RUST_DEBUG_MEM "RUST_DEBUG_MEM" #if defined(__WIN32__) static int @@ -128,6 +129,7 @@ load_env(int argc, char **argv) { env->poison_on_free = getenv(RUST_POISON_ON_FREE) != NULL; env->argc = argc; env->argv = argv; + env->debug_mem = getenv(RUST_DEBUG_MEM) != NULL; return env; } diff --git a/src/rt/rust_env.h b/src/rt/rust_env.h index c2aba575c44..df27f7674f2 100644 --- a/src/rt/rust_env.h +++ b/src/rt/rust_env.h @@ -14,16 +14,20 @@ #include "rust_globals.h" +// Avoiding 'bool' type here since I'm not sure it has a standard size +typedef uint8_t rust_bool; + struct rust_env { size_t num_sched_threads; size_t min_stack_size; size_t max_stack_size; char* logspec; - bool detailed_leaks; + rust_bool detailed_leaks; char* rust_seed; - bool poison_on_free; + rust_bool poison_on_free; int argc; char **argv; + rust_bool debug_mem; }; rust_env* load_env(int argc, char **argv); diff --git a/src/rt/rustrt.def.in b/src/rt/rustrt.def.in index 7fb6334ca75..5a8868f33f9 100644 --- a/src/rt/rustrt.def.in +++ b/src/rt/rustrt.def.in @@ -201,3 +201,4 @@ rust_dbg_extern_identity_u64 rust_dbg_extern_identity_TwoU64s rust_dbg_extern_identity_double rust_dbg_extern_identity_u8 +rust_get_rt_env