Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove fixed_stack_segment #10407

Merged
merged 2 commits into from
Nov 11, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
168 changes: 25 additions & 143 deletions doc/tutorial-ffi.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,9 @@ extern {
fn snappy_max_compressed_length(source_length: size_t) -> size_t;
}

#[fixed_stack_segment]
fn main() {
let x = unsafe { snappy_max_compressed_length(100) };
println(fmt!("max compressed length of a 100 byte buffer: %?", x));
println!("max compressed length of a 100 byte buffer: {}", x);
}
~~~~

Expand All @@ -36,11 +35,6 @@ interfaces that aren't thread-safe, and almost any function that takes a pointer
valid for all possible inputs since the pointer could be dangling, and raw pointers fall outside of
Rust's safe memory model.

Finally, the `#[fixed_stack_segment]` annotation that appears on
`main()` instructs the Rust compiler that when `main()` executes, it
should request a "very large" stack segment. More details on
stack management can be found in the following sections.

When declaring the argument types to a foreign function, the Rust compiler will not check if the
declaration is correct, so specifying it correctly is part of keeping the binding correct at
runtime.
Expand Down Expand Up @@ -81,8 +75,6 @@ length is number of elements currently contained, and the capacity is the total
the allocated memory. The length is less than or equal to the capacity.

~~~~ {.xfail-test}
#[fixed_stack_segment]
#[inline(never)]
pub fn validate_compressed_buffer(src: &[u8]) -> bool {
unsafe {
snappy_validate_compressed_buffer(vec::raw::to_ptr(src), src.len() as size_t) == 0
Expand All @@ -94,36 +86,6 @@ The `validate_compressed_buffer` wrapper above makes use of an `unsafe` block, b
guarantee that calling it is safe for all inputs by leaving off `unsafe` from the function
signature.

The `validate_compressed_buffer` wrapper is also annotated with two
attributes `#[fixed_stack_segment]` and `#[inline(never)]`. The
purpose of these attributes is to guarantee that there will be
sufficient stack for the C function to execute. This is necessary
because Rust, unlike C, does not assume that the stack is allocated in
one continuous chunk. Instead, we rely on a *segmented stack* scheme,
in which the stack grows and shrinks as necessary. C code, however,
expects one large stack, and so callers of C functions must request a
large stack segment to ensure that the C routine will not run off the
end of the stack.

The compiler includes a lint mode that will report an error if you
call a C function without a `#[fixed_stack_segment]` attribute. More
details on the lint mode are given in a later section.

You may be wondering why we include a `#[inline(never)]` directive.
This directive informs the compiler never to inline this function.
While not strictly necessary, it is usually a good idea to use an
`#[inline(never)]` directive in concert with `#[fixed_stack_segment]`.
The reason is that if a fn annotated with `fixed_stack_segment` is
inlined, then its caller also inherits the `fixed_stack_segment`
annotation. This means that rather than requesting a large stack
segment only for the duration of the call into C, the large stack
segment would be used for the entire duration of the caller. This is
not necessarily *bad* -- it can for example be more efficient,
particularly if `validate_compressed_buffer()` is called multiple
times in a row -- but it does work against the purpose of the
segmented stack scheme, which is to keep stacks small and thus
conserve address space.

The `snappy_compress` and `snappy_uncompress` functions are more complex, since a buffer has to be
allocated to hold the output too.

Expand All @@ -134,8 +96,6 @@ the true length after compression for setting the length.

~~~~ {.xfail-test}
pub fn compress(src: &[u8]) -> ~[u8] {
#[fixed_stack_segment]; #[inline(never)];

unsafe {
let srclen = src.len() as size_t;
let psrc = vec::raw::to_ptr(src);
Expand All @@ -156,8 +116,6 @@ format and `snappy_uncompressed_length` will retrieve the exact buffer size requ

~~~~ {.xfail-test}
pub fn uncompress(src: &[u8]) -> Option<~[u8]> {
#[fixed_stack_segment]; #[inline(never)];

unsafe {
let srclen = src.len() as size_t;
let psrc = vec::raw::to_ptr(src);
Expand All @@ -181,98 +139,28 @@ pub fn uncompress(src: &[u8]) -> Option<~[u8]> {
For reference, the examples used here are also available as an [library on
GitHub](https://github.com/thestinger/rust-snappy).

# Automatic wrappers

Sometimes writing Rust wrappers can be quite tedious. For example, if
function does not take any pointer arguments, often there is no need
for translating types. In such cases, it is usually still a good idea
to have a Rust wrapper so as to manage the segmented stacks, but you
can take advantage of the (standard) `externfn!` macro to remove some
of the tedium.

In the initial section, we showed an extern block that added a call
to a specific snappy API:

~~~~ {.xfail-test}
use std::libc::size_t;

#[link_args = "-lsnappy"]
extern {
fn snappy_max_compressed_length(source_length: size_t) -> size_t;
}

#[fixed_stack_segment]
fn main() {
let x = unsafe { snappy_max_compressed_length(100) };
println(fmt!("max compressed length of a 100 byte buffer: %?", x));
}
~~~~

To avoid the need to create a wrapper fn for `snappy_max_compressed_length()`,
and also to avoid the need to think about `#[fixed_stack_segment]`, we
could simply use the `externfn!` macro instead, as shown here:

~~~~ {.xfail-test}
use std::libc::size_t;

externfn!(#[link_args = "-lsnappy"]
fn snappy_max_compressed_length(source_length: size_t) -> size_t)

fn main() {
let x = unsafe { snappy_max_compressed_length(100) };
println(fmt!("max compressed length of a 100 byte buffer: %?", x));
}
~~~~

As you can see from the example, `externfn!` replaces the extern block
entirely. After macro expansion, it will create something like this:

~~~~ {.xfail-test}
use std::libc::size_t;

// Automatically generated by
// externfn!(#[link_args = "-lsnappy"]
// fn snappy_max_compressed_length(source_length: size_t) -> size_t)
unsafe fn snappy_max_compressed_length(source_length: size_t) -> size_t {
#[fixed_stack_segment]; #[inline(never)];
return snappy_max_compressed_length(source_length);

#[link_args = "-lsnappy"]
extern {
fn snappy_max_compressed_length(source_length: size_t) -> size_t;
}
}

fn main() {
let x = unsafe { snappy_max_compressed_length(100) };
println(fmt!("max compressed length of a 100 byte buffer: %?", x));
}
~~~~

# Segmented stacks and the linter

By default, whenever you invoke a non-Rust fn, the `cstack` lint will
check that one of the following conditions holds:

1. The call occurs inside of a fn that has been annotated with
`#[fixed_stack_segment]`;
2. The call occurs inside of an `extern fn`;
3. The call occurs within a stack closure created by some other
safe fn.

All of these conditions ensure that you are running on a large stack
segment. However, they are sometimes too strict. If your application
will be making many calls into C, it is often beneficial to promote
the `#[fixed_stack_segment]` attribute higher up the call chain. For
example, the Rust compiler actually labels main itself as requiring a
`#[fixed_stack_segment]`. In such cases, the linter is just an
annoyance, because all C calls that occur from within the Rust
compiler are made on a large stack. Another situation where this
frequently occurs is on a 64-bit architecture, where large stacks are
the default. In cases, you can disable the linter by including a
`#[allow(cstack)]` directive somewhere, which permits violations of
the "cstack" rules given above (you can also use `#[warn(cstack)]` to
convert the errors into warnings, if you prefer).
# Stack management

Rust tasks by default run on a "large stack". This is actually implemented as a
reserving a large segment of the address space and then lazily mapping in pages
as they are needed. When calling an external C function, the code is invoked on
the same stack as the rust stack. This means that there is no extra
stack-switching mechanism in place because it is assumed that the large stack
for the rust task is plenty for the C function to have.

A planned future improvement (net yet implemented at the time of this writing)
is to have a guard page at the end of every rust stack. No rust function will
hit this guard page (due to rust's usage of LLVM's __morestack). The intention
for this unmapped page is to prevent infinite recursion in C from overflowing
onto other rust stacks. If the guard page is hit, then the process will be
terminated with a message saying that the guard page was hit.

For normal external function usage, this all means that there shouldn't be any
need for any extra effort on a user's perspective. The C stack naturally
interleaves with the rust stack, and it's "large enough" for both to
interoperate. If, however, it is determined that a larger stack is necessary,
there are appropriate functions in the task spawning API to control the size of
the stack of the task which is spawned.

# Destructors

Expand All @@ -296,9 +184,6 @@ pub struct Unique<T> {

impl<T: Send> Unique<T> {
pub fn new(value: T) -> Unique<T> {
#[fixed_stack_segment];
#[inline(never)];

unsafe {
let ptr = malloc(std::mem::size_of::<T>() as size_t) as *mut T;
assert!(!ptr::is_null(ptr));
Expand All @@ -322,9 +207,6 @@ impl<T: Send> Unique<T> {
#[unsafe_destructor]
impl<T: Send> Drop for Unique<T> {
fn drop(&mut self) {
#[fixed_stack_segment];
#[inline(never)];

unsafe {
let x = intrinsics::init(); // dummy value to swap in
// moving the object out is needed to call the destructor
Expand Down Expand Up @@ -384,8 +266,8 @@ extern {
}

fn main() {
println(fmt!("You have readline version %d installed.",
rl_readline_version as int));
println!("You have readline version {} installed.",
rl_readline_version as int);
}
~~~

Expand Down
4 changes: 0 additions & 4 deletions src/libextra/c_vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,6 @@ mod tests {
}

impl Runnable for LibcFree {
#[fixed_stack_segment]
fn run(~self) {
unsafe {
libc::free(self.mem)
Expand All @@ -171,9 +170,6 @@ mod tests {
}

fn malloc(n: size_t) -> CVec<u8> {
#[fixed_stack_segment];
#[inline(never)];

unsafe {
let mem = libc::malloc(n);

Expand Down
4 changes: 0 additions & 4 deletions src/libextra/flate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,6 @@ static TINFL_FLAG_PARSE_ZLIB_HEADER : c_int = 0x1; // parse zlib header and adle
static TDEFL_WRITE_ZLIB_HEADER : c_int = 0x01000; // write zlib header and adler32 checksum

fn deflate_bytes_internal(bytes: &[u8], flags: c_int) -> ~[u8] {
#[fixed_stack_segment]; #[inline(never)];

do bytes.as_imm_buf |b, len| {
unsafe {
let mut outsz : size_t = 0;
Expand All @@ -75,8 +73,6 @@ pub fn deflate_bytes_zlib(bytes: &[u8]) -> ~[u8] {
}

fn inflate_bytes_internal(bytes: &[u8], flags: c_int) -> ~[u8] {
#[fixed_stack_segment]; #[inline(never)];

do bytes.as_imm_buf |b, len| {
unsafe {
let mut outsz : size_t = 0;
Expand Down
2 changes: 2 additions & 0 deletions src/libextra/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ Rust extras are part of the standard Rust distribution.

#[deny(non_camel_case_types)];
#[deny(missing_doc)];
#[allow(unrecognized_lint)]; // NOTE: remove after the next snapshot
#[allow(cstack)]; // NOTE: remove after the next snapshot.

use std::str::{StrSlice, OwnedStr};

Expand Down
2 changes: 0 additions & 2 deletions src/libextra/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,8 +220,6 @@ fn optgroups() -> ~[getopts::groups::OptGroup] {
}

fn usage(binary: &str, helpstr: &str) {
#[fixed_stack_segment]; #[inline(never)];

let message = format!("Usage: {} [OPTIONS] [FILTER]", binary);
println(groups::usage(message, optgroups()));
println("");
Expand Down
13 changes: 0 additions & 13 deletions src/libextra/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,6 @@ impl Ord for Timespec {
* nanoseconds since 1970-01-01T00:00:00Z.
*/
pub fn get_time() -> Timespec {
#[fixed_stack_segment]; #[inline(never)];

unsafe {
let mut sec = 0i64;
let mut nsec = 0i32;
Expand All @@ -79,8 +77,6 @@ pub fn get_time() -> Timespec {
* in nanoseconds since an unspecified epoch.
*/
pub fn precise_time_ns() -> u64 {
#[fixed_stack_segment]; #[inline(never)];

unsafe {
let mut ns = 0u64;
rustrt::precise_time_ns(&mut ns);
Expand All @@ -98,8 +94,6 @@ pub fn precise_time_s() -> f64 {
}

pub fn tzset() {
#[fixed_stack_segment]; #[inline(never)];

unsafe {
rustrt::rust_tzset();
}
Expand Down Expand Up @@ -143,8 +137,6 @@ pub fn empty_tm() -> Tm {

/// Returns the specified time in UTC
pub fn at_utc(clock: Timespec) -> Tm {
#[fixed_stack_segment]; #[inline(never)];

unsafe {
let Timespec { sec, nsec } = clock;
let mut tm = empty_tm();
Expand All @@ -160,8 +152,6 @@ pub fn now_utc() -> Tm {

/// Returns the specified time in the local timezone
pub fn at(clock: Timespec) -> Tm {
#[fixed_stack_segment]; #[inline(never)];

unsafe {
let Timespec { sec, nsec } = clock;
let mut tm = empty_tm();
Expand All @@ -179,8 +169,6 @@ pub fn now() -> Tm {
impl Tm {
/// Convert time to the seconds from January 1, 1970
pub fn to_timespec(&self) -> Timespec {
#[fixed_stack_segment]; #[inline(never)];

unsafe {
let sec = match self.tm_gmtoff {
0_i32 => rustrt::rust_timegm(self),
Expand Down Expand Up @@ -969,7 +957,6 @@ mod tests {
use std::libc;

#[cfg(windows)]
#[fixed_stack_segment]
fn set_time_zone() {
// Windows crt doesn't see any environment variable set by
// `SetEnvironmentVariable`, which `os::setenv` internally uses.
Expand Down
11 changes: 1 addition & 10 deletions src/librustc/driver/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -278,9 +278,6 @@ pub fn phase_3_run_analysis_passes(sess: Session,
time(time_passes, "loop checking", (), |_|
middle::check_loop::check_crate(ty_cx, crate));

time(time_passes, "stack checking", (), |_|
middle::stack_check::stack_check_crate(ty_cx, crate));

let middle::moves::MoveMaps {moves_map, moved_variables_set,
capture_map} =
time(time_passes, "compute moves", (), |_|
Expand Down Expand Up @@ -428,7 +425,6 @@ pub fn stop_after_phase_5(sess: Session) -> bool {
return false;
}

#[fixed_stack_segment]
pub fn compile_input(sess: Session, cfg: ast::CrateConfig, input: &input,
outdir: &Option<Path>, output: &Option<Path>) {
// We need nested scopes here, because the intermediate results can keep
Expand Down Expand Up @@ -703,12 +699,7 @@ pub fn build_session_options(binary: @str,
}

if debugging_opts & session::debug_llvm != 0 {
set_llvm_debug();

fn set_llvm_debug() {
#[fixed_stack_segment]; #[inline(never)];
unsafe { llvm::LLVMSetDebug(1); }
}
unsafe { llvm::LLVMSetDebug(1); }
}

let output_type =
Expand Down
Loading