From p.dalgaard at biostat.ku.dk  Wed Dec 13 00:23:40 2006
From: p.dalgaard at biostat.ku.dk (Peter Dalgaard)
Date: Wed, 13 Dec 2006 00:23:40 +0100
Subject: [Rd] [R] Segfault in pure R code
In-Reply-To: <148ed8180612121433n1287d12al6fdc622722d0b3ef@mail.gmail.com>
References: <148ed8180612121218pea45e0agcdb39a8b9fe9ff43@mail.gmail.com>	<457F18AB.4020109@biostat.ku.dk>	<148ed8180612121414x5763a5f1k4c802976fe9c85fe@mail.gmail.com>
	<148ed8180612121433n1287d12al6fdc622722d0b3ef@mail.gmail.com>
Message-ID: <457F39FC.7050406@biostat.ku.dk>

G?ran Brostr?m wrote:
> I tried once more under the debugger, and
>
> ++++++++++++++++++++++++++++++++++++++++++
> gb at kasparov:~/R/BEMANNING/Doc$ R -d gdb
> GNU gdb 6.5-debian
> Copyright (C) 2006 Free Software Foundation, Inc.
> GDB is free software, covered by the GNU General Public License, and you are
> welcome to change it and/or distribute copies of it under certain conditions.
> Type "show copying" to see the conditions.
> There is absolutely no warranty for GDB.  Type "show warranty" for details.
> This GDB was configured as "i486-linux-gnu"...Using host libthread_db
> library "/lib/tls/i686/cmov/libthread_db.so.1".
>
> (gdb) run
> Starting program: /usr/local/lib/R/bin/exec/R
> Failed to read a valid object file image from memory.
>
> R version 2.4.0 Patched (2006-10-29 r39744)
> Copyright (C) 2006 The R Foundation for Statistical Computing
> ISBN 3-900051-07-0
>
> R is free software and comes with ABSOLUTELY NO WARRANTY.
> You are welcome to redistribute it under certain conditions.
> Type 'license()' or 'licence()' for distribution details.
>
>   Natural language support but running in an English locale
>
> R is a collaborative project with many contributors.
> Type 'contributors()' for more information and
> 'citation()' on how to cite R or R packages in publications.
>
> Type 'demo()' for some demos, 'help()' for on-line help, or
> 'help.start()' for an HTML browser interface to help.
> Type 'q()' to quit R.
>
> [Previously saved workspace restored]
>
>   
>> library(xtable)
>> ?xtable)
>>     
> Error: syntax error in "?xtable)"
>   
>> ?xtable
>> help.start()
>>     
> Making links in per-session dir ...
> If '/usr/bin/firefox' is already running, it is *not* restarted, and
>     you must switch to its window.
> Otherwise, be patient ...
>   
>> library(bemanning)
>> load("bemanning07-32.rda")
>> courses("Ingrid")
>>     
>
> Program received signal SIGSEGV, Segmentation fault.
> 0x080f5026 in R_gc_internal (size_needed=17588127) at memory.c:1313
> 1313        PROCESS_NODES();
> (gdb)
> ++++++++++++++++++++++++++++++++++++++++++++
>
> How do I continue?
>   

Ouch.  Call the marines...

The error comes from the garbage collector, which means that something 
got corrupted in internal data structures some time previously.

The most important thing is to preserve the bug. Rebuild anything and 
the symptom disappears, but the bug will still be there. So keep the R 
binary, the script, and the .RData file around.

Some ideas:

Is it reproducible on other machines?

Do you have valgrind installed? Notes for using it are on 
http://developer.r-project.org/valgrind-internal.html

Turning on gctorture() may trigger the bug at an earlier state.

The last resort is to painstakingly backtrace to the point of the 
damage  using  watchpoints and whatever, but I'd rather not go there 
right away.

     -p
> G?ran
>
> On 12/12/06, G?ran Brostr?m <goran.brostrom at gmail.com> wrote:
>   
>> On 12/12/06, Peter Dalgaard <p.dalgaard at biostat.ku.dk> wrote:
>>     
>>> G?ran Brostr?m wrote:
>>>       
>>>> I just caught a segfault:
>>>>
>>>>
>>>>         
>>>>> courses("Ingrid")
>>>>>
>>>>>           
>>>>  *** caught segfault ***
>>>> address 0x99b279c, cause 'memory not mapped'
>>>>
>>>> Traceback:
>>>>  1: structure(y, class = oldClass(x), row.names = attr(x, "row.names"))
>>>>  2: `[.data.frame`(gudata, -(1:5))
>>>>  3: gudata[-(1:5)]
>>>>  4: names(gudata[-(1:5)])
>>>>  5: inherits(x, "factor")
>>>>  6: is.factor(table)
>>>>  7: match(x, table, nomatch = 0)
>>>>  8: who %in% names(gudata[-(1:5)])
>>>>  9: courses("Ingrid")
>>>>
>>>> when running a function 'courses' in an R package without compiled
>>>> code. Is this "possible"? I have got many segfaults when testing my
>>>> own packages, but it has always been caused by stupidities in C or
>>>> Fortran code, never with pure R code.
>>>>
>>>> So, before I start debugging, I'd like to know if a segfault in pure R
>>>> code indicates  a bug in R itself, or if it can be in my function?
>>>>
>>>>
>>>>         
>>> By definition, reproducible segfaults in R code are bugs in R, unless
>>> caused by abuse of .C calls or similar. (Irreproducible ones are often
>>> hardware faults.)
>>>
>>> However, at least presently, you are the only one with a handle on the
>>> bug. So either you get to do the debugging or you have to provide
>>> something that others can reproduce.
>>>
>>> Astarting point could be to run R under the debugger (R -d gdb)  and
>>> generate a C backtrace, then look at the variables involved.
>>>       
>> Thanks, Peter,
>>
>> the error is reproducible. The full session is
>>
>> +++++++++++++++++++++++++++++++++++++++++
>> gb at kasparov:~/R/BEMANNING/Doc$ R
>>
>> R version 2.4.0 Patched (2006-10-29 r39744)
>> Copyright (C) 2006 The R Foundation for Statistical Computing
>> ISBN 3-900051-07-0
>>
>> R is free software and comes with ABSOLUTELY NO WARRANTY.
>> You are welcome to redistribute it under certain conditions.
>> Type 'license()' or 'licence()' for distribution details.
>>
>>   Natural language support but running in an English locale
>>
>> R is a collaborative project with many contributors.
>> Type 'contributors()' for more information and
>> 'citation()' on how to cite R or R packages in publications.
>>
>> Type 'demo()' for some demos, 'help()' for on-line help, or
>> 'help.start()' for an HTML browser interface to help.
>> Type 'q()' to quit R.
>>
>> [Previously saved workspace restored]
>>
>>     
>>> library(xtable)
>>> ?xtable)
>>>       
>> Error: syntax error in "?xtable)"
>>     
>>> ?xtable
>>> help.start()
>>>       
>> Making links in per-session dir ...
>> If '/usr/bin/firefox' is already running, it is *not* restarted, and
>>     you must switch to its window.
>> Otherwise, be patient ...
>>     
>>> library(bemanning)
>>> load("bemanning07-32.rda")
>>> courses("Ingrid")
>>>       
>>  *** caught segfault ***
>> address 0x99b279c, cause 'memory not mapped'
>>
>> Traceback:
>>  1: structure(y, class = oldClass(x), row.names = attr(x, "row.names"))
>>  2: `[.data.frame`(gudata, -(1:5))
>>  3: gudata[-(1:5)]
>>  4: names(gudata[-(1:5)])
>>  5: inherits(x, "factor")
>>  6: is.factor(table)
>>  7: match(x, table, nomatch = 0)
>>  8: who %in% names(gudata[-(1:5)])
>>  9: courses("Ingrid")
>>
>> Possible actions:
>> 1: abort (with core dump)
>> 2: normal R exit
>> 3: exit R without saving workspace
>> 4: exit R saving workspace
>> Selection:
>> +++++++++++++++++++++++++++++++++++++++++++++
>>
>> Notes:
>>
>> 1. Tried to remove one line at a time, but then no error.
>> 2. R -d gdb: No errors
>> 3. Starting with 'R --vanilla': No errors.
>>
>> i.e., it seems as if my previously saved workspace '.RData' is part of
>> the error.
>> Could it be corrupt and cause a segfault in a bug free R?
>>
>> I'll try to build a newer version of R and check (but not tonight).
>>
>> G?ran
>>
>>     
>>> (Further discussion should probably go on r-devel instead of r-help).
>>>
>>> -p
>>>
>>>       
>>>> I have used 'courses' many times before without problems.
>>>>
>>>> Thanks,
>>>>
>>>> G?ran
>>>> ++++++++++++++++++++++++++++++++++++++
>>>>
>>>>         
>>>>> sessionInfo()
>>>>>
>>>>>           
>>>> R version 2.4.0 Patched (2006-10-29 r39744)
>>>> i686-pc-linux-gnu
>>>>
>>>>
>>>>
>>>>         
>>>
>>>       
>> --
>> G?ran Brostr?m
>>
>>     
>
>
>



[Rd] [R] Segfault in pure R code
Peter Dalgaard