Is there a simple way to trigger a crash in R? This is for testing purposes only, to see how a certain program that uses R in the background reacts to a crash and help determine if some rare problems are due to crashes or not.
6 Answers
The easiest way is to call C
-code. C
provides a standard function abort()
[1] that does what you want. You need to call: .Call("abort")
.
As @Phillip pointed out you may need to load libc
via:
on Linux,
dyn.load("/lib/x86_64-linux-gnu/libc.so.6")
before issuing.Call("abort")
. The path may of course vary depending on your system.on OS X,
dyn.load("/usr/lib/libc.dylib")
on Windows (I just tested it on XP as I could not get hold of a newer version.) you will need to install
Rtools
[2]. After that you should loaddyn.load("C:/.../Rtools/bin/cygwin1.dll")
.
-
1When running R from the command line or using the official GUI, I get
Error in .Call("abort") : C symbol name "abort" not in load table
. When using RStudio it crashes.– SzabolcsAug 5, 2014 at 13:12 -
2You've got to load libc before:
dyn.load("/lib/x86_64-linux-gnu/libc.so.6")
. The path may vary on your system, uselocate libc.so.6
to find it.– PhillipAug 5, 2014 at 13:14 -
3The
crash
package callsabort
as well. At least as far as I can see but I'm not nearly as experienced as you are! Aug 5, 2014 at 13:33 -
1
-
3You probably meant the Rtools package as R was never supported on Cygwin. What Joshua was too polite to mention directly is that your answer isn't fully portable this way. But yes,
abort()
is the key. Aug 6, 2014 at 1:09
There is an entire package on GitHub dedicated to this:
crash
R package that purposely crash an R session. WARNING: intended for test.
How to install a package from github is covered in other questions.
I'm going to steal an idea from @Spacedman, but I'm giving him full conceptual credit by copying from his Twitter feed:
Segfault #rstats in one easy step:
options(device=function(){});plot(1)
reported Danger, will crash your R session. — Barry Rowlingson (@geospacedman) July 16, 2014
-
1This is useful because it doesn't exit right away, instead it presents a prompt and asks what to do next. It's a different sort of behaviour which may also be what's going wrong in my own project ...– SzabolcsAug 5, 2014 at 13:45
As mentioned in a comment to your question, the minimal approach is a simple call to the system function abort()
. One way to do this in one line is to
R> Rcpp::cppFunction('int crashMe(int ignored) { ::abort(); }');
R> crashMe(123)
Aborted (core dumped)
$
or you can use the inline package:
R> library(inline)
R> crashMe <- cfunction(body="::abort();")
R> crashMe()
Aborted (core dumped)
$
You can of course also do this outside of Rcpp or inline, but then you need to deal with the system-dependent ways of compiling, linking and loading.
-
The above snippet crashes to desktop (in a low-mem env) after the first line. Here be dragons. Aug 6, 2014 at 9:24
-
1@DeerHunter. I also noticed that on maybe one out five attempts. There must be a race somewhere. Then again, R isn't exactly designed to be
abort()
ed. Aug 6, 2014 at 13:54
I'll do this in plain C because my C++-foo isn't Dirkian:
Create a C file, segv.c
:
#include <signal.h>
void crashme(){raise(SIGSEGV);}
Compile it at the command line (windows users will have to work this out for themselves):
R CMD SHLIB segv.c
In R, load and run:
dyn.load("segv.so") # or possibly .dll for Windows users
.C("crashme")
Producing a segfault:
> .C("crashme")
*** caught segfault ***
address 0x1d9e, cause 'unknown'
Traceback:
1: .C("crashme")
Possible actions:
1: abort (with core dump, if enabled)
2: normal R exit
3: exit R without saving workspace
4: exit R saving workspace
Selection: 1
aborting ...
Segmentation fault
This is the same behaviour as the one Thomas references in the graphics system bug report which I have filed and might get fixed one day. However this two-liner will always raise a segfault...
Maybe Dirk can one-line-Rcpp-ise it?
-
Re-read my post, the inline use is entirely C -- I use
cfunction()
i in C mode. You could do the same here to make your answer easier/more concise/less OS-dependent. The Rcpp use is 'merely' to deploy the even easier build mechanism, there is no C++ there per se. Aug 5, 2014 at 17:14 -
3
spacedman <- inline::cfunction(body="raise(SIGSEGV);", include="#include <signal.h>")
-- and no C++ was harmed^Hused in this answer. Aug 5, 2014 at 17:16 -
spacedman <- Rcpp::cppFunction("void crashme() { ::raise(SIGSEGV); }", includes="#include <signal.h>")
-- so there. Aug 5, 2014 at 17:29
If you want to crash your R, try this
lapply("", function(x) eval(sys.call(1)))
(Save everything before running because this immediately results in "R Session Aborted")
Edit: This works for me on Windows 10.
-
At least on macOS there seems to be some protection in place against stack overflow. This results in "Error: C stack usage 7971744 is too close to the limit", but no crash.– SzabolcsMar 2, 2020 at 15:23
options(expressions=300000)
then running an infinite recursion but R is written well enough that it doesn't crash :)quit
with a non-zero status instead?