From ripley at stats.ox.ac.uk  Wed Apr 12 07:45:38 2006
From: ripley at stats.ox.ac.uk (Prof Brian Ripley)
Date: Wed, 12 Apr 2006 06:45:38 +0100 (BST)
Subject: [Rd] Stack checking, core dumps, and embedding R
In-Reply-To: <443C863B.8030700@vanderbilt.edu>
References: <1abe3fa90604110234v1b4958c0w1e51e2b49f7ff9c5@mail.gmail.com>
	<E725DDAC-4044-4A6E-B9F6-007FAF7225DA@r-project.org>
	<443C863B.8030700@vanderbilt.edu>
Message-ID: <Pine.LNX.4.64.0604120643590.31187@gannet.stats.ox.ac.uk>

On Tue, 11 Apr 2006, Jeffrey Horner wrote:

> Simon Urbanek wrote:
>> On Apr 11, 2006, at 5:34 AM, A.J. Rossini wrote:
>>
>>> I've been experiencing some interesting stack warnings recently
>>> when moving from R 2.2.x to the R 2.3.0 series and the R 2.4.0
>>> series.  In particular, I'm getting warnings of "Error: C stack
>>> usage is too close to the limit" before segfaulting, and this
>>> wasn't happening under the 2.2.x series.
>>>
>>> Here's the question statement:  (ONLY) In the situation where one
>>> is embedding R, is anyone else seeing this occur with recent (past
>>> 2 weeks) versions of R (both the 2.3.x and 2.4.x series)?
>>>
>>
>> When embedding R, the stack base is set to -1 (which is almost
>> certainly wrong) if R cannot find the stack base using some system
>> method. I have just fixed the detection for OS X, so you shouldn't
>
> My question may be unrelated, but I stumbled across it while trying to
> understand the above problem. The following is from R-trunk r37731
> src/unix/system.c in Rf_initialize_R() and is run when the os is neither
> linux nor freebsd:
>
>     if(R_running_as_main_program) {
>     /* This is not the main program, but unless embedded it is
>        near the top, 5540 bytes away when checked. */
>     R_CStackStart = (uintptr_t) &i + 6000;
>     }
>
> This is run before we know which direction the stack grows:
>
>     {
>         int ii;
>     /* 1 is downwards */
>         R_CStackDir = ((uintptr_t)&i > (uintptr_t)&ii) ? 1 : -1;
>     }
>
> So, R_CStackStart is being set to the address of i (on the stack) plus
> 6000 bytes, an approximation to somewhere near the beginning of the
> stack, but this presumes the stack grows down, right? What if it grows
> up? Then shouldn't we subtract 6000 bytes?

Probably, but is 12000 bytes important (stack sizes are around 10Mb)?
Do you know of an R platform on which it grows up, BTW (I don't)?

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595



[Rd] Stack checking, core dumps, and embedding R
Prof Brian Ripley