How to crash R?

RCrash

R Problem Overview


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.

R Solutions


Solution 1 - R

The easiest way is to call C-code. C provides a standard function abort()http://www.cplusplus.com/reference/cstdlib/abort/" title="retrieved 2014-08-05T15:18+02:00">[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 Rtoolshttp://cran.r-project.org/bin/windows/Rtools/" title="retrieved 2014-08-06T12:03+02:00">[2]. After that you should load dyn.load("C:/.../Rtools/bin/cygwin1.dll").

Solution 2 - R

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.

Solution 3 - R

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) https://twitter.com/geospacedman/statuses/489344772780617728">July 16, 2014

Solution 4 - R

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.

Solution 5 - R

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?

Solution 6 - R

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSzabolcsView Question on Stackoverflow
Solution 1 - Rlord.garbageView Answer on Stackoverflow
Solution 2 - RDirk EddelbuettelView Answer on Stackoverflow
Solution 3 - RThomasView Answer on Stackoverflow
Solution 4 - RDirk EddelbuettelView Answer on Stackoverflow
Solution 5 - RSpacedmanView Answer on Stackoverflow
Solution 6 - Ruser11538509View Answer on Stackoverflow