69

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.

Szabolcs
  • 24,728
  • 9
  • 85
  • 174
  • 3
    I've tried `options(expressions=300000)` then running an infinite recursion but R is written well enough that it doesn't crash :) – Szabolcs Aug 05 '14 at 12:50
  • 10
    https://bugs.r-project.org/bugzilla/buglist.cgi?query_format=specific&order=relevance+desc&no_redirect=0&bug_status=__open__&product=&content=crash – James Aug 05 '14 at 13:02
  • 1
    This may depend on your platform. Want to add that info? – Stephan Kolassa Aug 05 '14 at 13:02
  • 1
    @StephanKolassa I'm on OS X, but I'd rather keep the question general for the sake of future readers. Answers specific to any platform are acceptable. – Szabolcs Aug 05 '14 at 13:07
  • 4
    Can downvoters explain what they consider to be wrong with the question? @DirkEddelbuettel Please be tolerant, that does not make the solution obvious to everyone. A link to the man page doesn't make it clear how to do this. – Szabolcs Aug 05 '14 at 15:27
  • 4
    Must it be a crash? Could you simply `quit` with a non-zero status instead? – Joshua Ulrich Aug 05 '14 at 17:08

6 Answers6

53

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 load dyn.load("C:/.../Rtools/bin/cygwin1.dll").

lord.garbage
  • 5,884
  • 5
  • 36
  • 55
  • 1
    When 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. – Szabolcs Aug 05 '14 at 13:12
  • Strange, I use it from the command line and I get: `Abort (core dumped)`. – lord.garbage Aug 05 '14 at 13:13
  • 2
    You've got to load libc before: `dyn.load("/lib/x86_64-linux-gnu/libc.so.6")`. The path may vary on your system, use `locate libc.so.6` to find it. – Phillip Aug 05 '14 at 13:14
  • @Phillip Thanks! On OS X that would be `/usr/lib/libc.dylib`. lord.garbage, can you update your answer with the info provided by Phillip? – Szabolcs Aug 05 '14 at 13:18
  • That is cheating as `abort()` and `assert()` and ... are all declared inadmissible by the [CRAN Policy](http://cran.r-project.org/web/packages/policies.html). – Dirk Eddelbuettel Aug 05 '14 at 13:32
  • 3
    The `crash` package calls `abort` as well. At least as far as I can see but I'm not nearly as experienced as you are! – lord.garbage Aug 05 '14 at 13:33
  • 1
    So it'll never get on CRAN :) – Dirk Eddelbuettel Aug 05 '14 at 13:34
  • 1
    Hello @Joshua, I am currently not on a Windows machine. The last time I successfully called `C` functions on a Windows machine from `R` it involved installing either a complete `Cygwin` environment or the `Rtools` package. If there is an easier way I am unfortunately not aware of it. – lord.garbage Aug 06 '14 at 01:06
  • 3
    You 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. – Dirk Eddelbuettel Aug 06 '14 at 01:09
49

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.

Community
  • 1
  • 1
Dirk Eddelbuettel
  • 360,940
  • 56
  • 644
  • 725
  • 21
    And it calls `abort`, nice. – lord.garbage Aug 05 '14 at 13:33
  • 8
    Assuming you have the required tools installed ([*nix](http://cran.r-project.org/doc/manuals/R-admin.html#Essential-and-useful-other-programs-under-a-Unix_002dalike), [Windows](http://cran.r-project.org/doc/manuals/R-admin.html#The-Windows-toolset)), one way to install this package from github is: `library(devtools); install_github('jdanielnd/crash')`. Then you can crash your R session with `library(crash); crash()` – Joshua Ulrich Aug 06 '14 at 11:24
17

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

Thomas
  • 43,637
  • 12
  • 109
  • 140
  • 1
    This 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 ... – Szabolcs Aug 05 '14 at 13:45
15

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.

Dirk Eddelbuettel
  • 360,940
  • 56
  • 644
  • 725
  • The above snippet crashes to desktop (in a low-mem env) after the first line. Here be dragons. – Deer Hunter Aug 06 '14 at 09: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. – Dirk Eddelbuettel Aug 06 '14 at 13:54
6

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?

Spacedman
  • 92,590
  • 12
  • 140
  • 224
  • 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. – Dirk Eddelbuettel Aug 05 '14 at 17:14
  • 3
    `spacedman <- inline::cfunction(body="raise(SIGSEGV);", include="#include ")` -- and no C++ was harmed^Hused in this answer. – Dirk Eddelbuettel Aug 05 '14 at 17:16
  • `spacedman <- Rcpp::cppFunction("void crashme() { ::raise(SIGSEGV); }", includes="#include ")` -- so there. – Dirk Eddelbuettel Aug 05 '14 at 17:29
0

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. – Szabolcs Mar 02 '20 at 15:23