-
Notifications
You must be signed in to change notification settings - Fork 225
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
Crashing BEAM with SIGBUS without use of unsafe code #294
Comments
After studying this for a little bit. I think I've found the crux of the problem. Inside the decode function of if let Ok(Args::One(arg)) = term.decode() {
return Ok(Args::One(arg));
} Which is self-referential; It uses a decoded Changing it to: if let Ok((arg,)) = term.decode() {
return Ok(Args::One(arg));
} Does not cause a SIGBUS. |
The question now is: How do we keep others from having this issue? |
@elbow-jason Thanks for finding this! Some additional info when running this with a debug build of ERTS and valgrind (rustc 1.39.0 (4560ea788 2019-11-04), Elixir 1.9.4, Erlang/OTP 22 [erts-10.6]) :
|
GDB for the coredump indeed indicates an endless loop:
|
For simple cases, Rust will warn on endless recursion. Apparently, it cannot detect the recursion in this case here. See this playground example for a simple case which is detectable. EDIT: When running the playground example, the overflow is detected properly:
I assume that we do not see such a message as the NIF is not actually handling setting up the stack itself. See here for a discussion regarding this. The calling environment is responsible to set up the running thread and its stack. |
With some digging, I found the place where ERTS sets a guard page for spawned threads (ethread.c): #ifdef ETHR_STACK_GUARD_SIZE
(void) pthread_attr_setguardsize(&attr, ETHR_STACK_GUARD_SIZE);
#endif In your example, we should run into this guard. I believe that we cannot do anything further, as we cannot know at compile time if a recursive call is finite. But it would probably be helpful to at least have some indication of this in the README. Maybe a "Pitfalls" section would be good? |
This could fit well as an example of safety caveats at https://rustler-web.onrender.com/docs. |
I implemented
Decoder
on a type that crashes the VM with a SIGBUS.I made a minimal reproducible example in this repo: https://github.com/elbow-jason/why_sig_bus
The text was updated successfully, but these errors were encountered: