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

Make rust_stack_exhausted a lang item #11922

Closed
brson opened this issue Jan 30, 2014 · 11 comments
Closed

Make rust_stack_exhausted a lang item #11922

brson opened this issue Jan 30, 2014 · 11 comments
Labels
A-runtime Area: std's runtime and "pre-main" init for handling backtraces, unwinds, stack overflows

Comments

@brson
Copy link
Contributor

brson commented Jan 30, 2014

cc #9839. Right now if you don't link to the standard library you get a link error because __morestack expects a symbol called rust_stack_exhausted to exist.

Add a "stack_exhausted" lang item. For every compilation unit, if the lang item doesn't exist, emit the code

extern "rust-intrinsic" { fn abort() -> !; }

#[no_mangle] pub extern "C" fn __rust_stack_exhausted() {
    unsafe { abort() }
}

If it does exist, emit the same function, but call the lang item.

This would work in our current scheme where std defines what happens when the stack runs out, but not if we refactored std like #11828, so that the lowest levels of the crate hierarchy don't know the failure policy.

@thestinger
Copy link
Contributor

The morestack-based stack overflow checking doesn't work at all in a kernel. I think this would just hide the issue. Rust can also only generate position-independent code and is unable to easily define memcpy, memset and memmove which will be called by LLVM so it's only one of many blockers.

I've been tagging these as A-freestanding so you can look under that to find the other issues.

@brson
Copy link
Contributor Author

brson commented Jan 30, 2014

Why doesn't morestack work in a kernel?

@alexcrichton
Copy link
Member

The morestack-based stack overflow checking doesn't work at all in a kernel

That is not true. I have written a kernel that makes use of morestack checks. It is just a little more difficult to set up the state of the world such that the stack checking works.

@thestinger
Copy link
Contributor

@alexcrichton: That's exactly what I mean though. It's broken out-of-the-box and will be a silent hazard in a freestanding project like a Linux kernel module.

@brson
Copy link
Contributor Author

brson commented Jan 30, 2014

The problem is that, without doing some setup, the morestack checks will trip incorrectly?

@brson
Copy link
Contributor Author

brson commented Jan 30, 2014

Is it true that this incorrect trip only happens in kernel space? Does userpace always initialize our morestack TCB slot to zero for us? (ISTR it doesn't in some cases)

@thestinger
Copy link
Contributor

@brson: That issue along with the possibility of setting it up not being an option if you're integrating with an existing kernel like Linux. I don't know if it's ever a problem in userland, as I haven't seen it go wrong yet.

@brson
Copy link
Contributor Author

brson commented Jan 30, 2014

We could probably provide portable routines for initializing morestack. If this is only a problem for kernelspace it doesn't seem too burdensome to ask them to call an init function. Of course, having an option to disable morestack generation would also be useful.

As long as we are using morestack, I'd like to make it as convenient as we can.

@huonw
Copy link
Member

huonw commented Jan 30, 2014

Could we just have this have normal lang-item detection?

If there is any function that will require the split-stack header, emit a "requires stack_exhausted" error, which at least means we don't get linking errors.

@thestinger
Copy link
Contributor

As long as it's still possible to build with without marking every function as #[no_split_stack] :P. It is now by going through either llc or clang and relying on a guard page for safety. Of course, it's not a full solution until LLVM has a chkstk equivalent on more than Windows, and morestack will still be required without a MMU.

alexcrichton added a commit to alexcrichton/rust that referenced this issue May 19, 2014
This commit is part of the ongoing libstd facade efforts (cc rust-lang#13851). The
compiler now recognizes some language items as "extern { fn foo(...); }" and
will automatically perform the following actions:

1. The foreign function has a pre-defined name.
2. The crate and downstream crates can only be built as rlibs until a crate
   defines the lang item itself.
3. The actual lang item has a pre-defined name.

This is essentially nicer compiler support for the hokey
core-depends-on-std-failure scheme today, but it is implemented the same way.
The details are a little more hidden under the covers.

In addition to failure, this commit promotes the eh_personality and
rust_stack_exhausted functions to official lang items. The compiler can generate
calls to these functions, causing linkage errors if they are left undefined. The
checking for these items is not as precise as it could be. Crates compiling with
`-Z no-landing-pads` will not need the eh_personality lang item, and crates
compiling with no split stacks won't need the stack exhausted lang item. For
ease, however, these items are checked for presence in all final outputs of the
compiler.

It is quite easy to define dummy versions of the functions necessary:

    #[lang = "stack_exhausted"]
    extern fn stack_exhausted() { /* ... */ }

    #[lang = "eh_personality"]
    extern fn eh_personality() { /* ... */ }

cc rust-lang#11922, rust_stack_exhausted is now a lang item
cc rust-lang#13851, libcollections is blocked on eh_personality becoming weak
bors added a commit that referenced this issue May 21, 2014
This commit is part of the ongoing libstd facade efforts (cc #13851). The
compiler now recognizes some language items as "extern { fn foo(...); }" and
will automatically perform the following actions:

1. The foreign function has a pre-defined name.
2. The crate and downstream crates can only be built as rlibs until a crate
   defines the lang item itself.
3. The actual lang item has a pre-defined name.

This is essentially nicer compiler support for the hokey
core-depends-on-std-failure scheme today, but it is implemented the same way.
The details are a little more hidden under the covers.

In addition to failure, this commit promotes the eh_personality and
rust_stack_exhausted functions to official lang items. The compiler can generate
calls to these functions, causing linkage errors if they are left undefined. The
checking for these items is not as precise as it could be. Crates compiling with
`-Z no-landing-pads` will not need the eh_personality lang item, and crates
compiling with no split stacks won't need the stack exhausted lang item. For
ease, however, these items are checked for presence in all final outputs of the
compiler.

It is quite easy to define dummy versions of the functions necessary:

    #[lang = "stack_exhausted"]
    extern fn stack_exhausted() { /* ... */ }

    #[lang = "eh_personality"]
    extern fn eh_personality() { /* ... */ }

cc #11922, rust_stack_exhausted is now a lang item
cc #13851, libcollections is blocked on eh_personality becoming weak
@alexcrichton
Copy link
Member

Closing, this was done in #14293

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-runtime Area: std's runtime and "pre-main" init for handling backtraces, unwinds, stack overflows
Projects
None yet
Development

No branches or pull requests

4 participants