Skip to content

Segfault with rWishart in R 2.15.1

4 messages · Michael Braun, Peter Dalgaard, Brian Ripley

#
I just upgraded to R 2.15.1, and I am getting a segmentation fault when using the rWishart function (from the stats package) to sample moderately-size matrices.

Here is the output when I run R within gdb.  720 appears to be the dimensionality cut-off.  Anything smaller works fine.  Anything larger crashes.
Error: C stack usage is too close to the limit
Program received signal EXC_BAD_ACCESS, Could not access memory.
Reason: KERN_PROTECTION_FAILURE at address: 0x00007fff5f3fff88
0x00007fff88b9c68b in __vfprintf ()
(gdb) bt
#0  0x00007fff88b9c68b in __vfprintf ()
#1  0x00007fff88b9c18e in vfprintf_l ()
#2  0x00007fff88ba52d0 in printf ()
#3  0x0000000100198619 in Rstd_WriteConsoleEx (
    buf=<value temporarily unavailable, due to optimizations>, 
    len=<value temporarily unavailable, due to optimizations>, 
    otype=<value temporarily unavailable, due to optimizations>)
    at ../../../../R-2.15.1/src/unix/sys-std.c:963
#4  0x00000001001497b7 in REvprintf (format=0x100253820 "%s", 
    arg=0x7fff5f4029f0) at ../../../../R-2.15.1/src/main/printutils.c:813
#5  0x0000000100149a02 in REprintf (
    format=<value temporarily unavailable, due to optimizations>)
    at ../../../../R-2.15.1/src/main/printutils.c:698
#6  0x0000000100099e45 in verrorcall_dflt (call=0x10080db78, 
    format=0x10025b9d8 "C stack usage is too close to the limit", 
    ap=0x7fff5f408cc0) at ../../../../R-2.15.1/src/main/errors.c:656
#7  0x000000010009a86e in Rf_errorcall (call=0x10080db78, 
    format=0x10025b9d8 "C stack usage is too close to the limit")
    at ../../../../R-2.15.1/src/main/errors.c:698
#8  0x000000010009c95c in R_CheckStack ()
    at ../../../../R-2.15.1/src/main/errors.c:93
#9  0x0000000102704e80 in R_rWishart ()
#10 0x000000010007af0c in do_dotcall (call=0x102f63710, 
---Type <return> to continue, or q <return> to quit---
    op=<value temporarily unavailable, due to optimizations>, 
    args=<value temporarily unavailable, due to optimizations>, 
    env=<value temporarily unavailable, due to optimizations>)
    at ../../../../R-2.15.1/src/main/dotcode.c:543
#11 0x00000001000a128f in bcEval (
    body=<value temporarily unavailable, due to optimizations>, 
    rho=0x103df1140, 
    useCache=<value temporarily unavailable, due to optimizations>)
    at ../../../../R-2.15.1/src/main/eval.c:4442
#12 0x00000001000aa1f5 in Rf_eval (e=0x102f63748, rho=0x103df1140)
    at ../../../../R-2.15.1/src/main/eval.c:397
#13 0x00000001000af5d1 in Rf_applyClosure (call=0x103df0b58, op=0x102f62470, 
    arglist=0x103df0df8, rho=0x10085b6a8, suppliedenv=0x10085b6e0)
    at ../../../../R-2.15.1/src/main/eval.c:859
#14 0x00000001000aa402 in Rf_eval (e=0x103df0b58, rho=0x10085b6a8)
    at ../../../../R-2.15.1/src/main/eval.c:510
#15 0x00000001000ad705 in do_set (call=0x103df0c38, op=0x100832030, 
    args=0x103df0c00, rho=0x10085b6a8)
    at ../../../../R-2.15.1/src/main/eval.c:1715
#16 0x00000001000aa4ec in Rf_eval (e=0x103df0c38, rho=0x10085b6a8)
    at ../../../../R-2.15.1/src/main/eval.c:466
#17 0x00000001000e3822 in Rf_ReplIteration (rho=0x10085b6a8, savestack=0, 
    browselevel=<value temporarily unavailable, due to optimizations>, 
---Type <return> to continue, or q <return> to quit---
    state=0x7fff5fbfe7d0) at ../../../../R-2.15.1/src/main/main.c:256
#18 0x00000001000e3b11 in R_ReplConsole (rho=0x10085b6a8, savestack=0, 
    browselevel=0) at ../../../../R-2.15.1/src/main/main.c:305
#19 0x00000001000e4020 in run_Rmainloop ()
    at ../../../../R-2.15.1/src/main/main.c:987
#20 0x0000000100000eeb in main ()

Here is my sessionInfo() output.  I am running R on a brand new (as in, yesterday) Macbook Pro.  This is the CRAN binary version.  I did not link to an optimized BLAS on this computer (yet).  However, I did try this on my office computer (Mac Pro) that I compiled from source, linking to Intel MKL, and that also crashes around k=722.
R version 2.15.1 (2012-06-22)
Platform: x86_64-apple-darwin9.8.0/x86_64 (64-bit)

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base
Anyway, since R is segfaulting on a brand new computer, I thought you would like to know.

Michael
-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/pkcs7-signature
Size: 1579 bytes
Desc: not available
URL: <https://stat.ethz.ch/pipermail/r-sig-mac/attachments/20120704/88a80332/attachment.bin>
#
On Jul 4, 2012, at 03:12 , Michael Braun wrote:

            
That'll be due to allocating memory off the C stack with alloca(). Please file a bug report on this (nothing to do with Mac, much less with the age of your computer).

In a tight spot, you can run R from the terminal after raising the C stack limit with, say, "ulimit -s 32768", but of course the trouble returns at twice the matrix size. 

Notice that there are really two issues here. One is that the R_CheckStack() safeguards are failing. The other is why we're using stack allocation in the first place.
#
It's not a Mac issue (except perhaps the crash).

The author of rWishart used alloca() without checking if there is space 
available on the stack.  For large enough arrays that blows the stack 
and although R can tell you that has happened, the damage has been done.

We'll fix that shortly, so take a look at a nightly build of R-patched 
in a couple of days.
On 04/07/2012 02:12, Michael Braun wrote:

  
    
#
On 04/07/2012 08:35, peter dalgaard wrote:
They are related: the R_CheckStack() safeguards were being used 
incorrectly.  alloca() is important for frequent small allocations (not 
the case here): the safeguards are only designed to work for repeated 
small allocations (< 100kB, well under 5% of the stack).

I have experimented with a pre-alloca check in the past, and it seems 
this would be a better protection against misuse, and I'll trial that in 
R-devel.