From mtmorgan at fhcrc.org  Sat Sep 24 01:23:14 2011
From: mtmorgan at fhcrc.org (Martin Morgan)
Date: Fri, 23 Sep 2011 16:23:14 -0700
Subject: [Rd] cbind() crashes on raw vectors
In-Reply-To: <4E7CF03F.7090100@fhcrc.org>
References: <4E7CF03F.7090100@fhcrc.org>
Message-ID: <4E7D14E2.1090204@fhcrc.org>

On 09/23/2011 01:46 PM, Herv? Pag?s wrote:
> Hi,
>
> cbind() doesn't seem to like raw vectors:
>
>  > df <- cbind(a=integer(4000), b=raw(4000))
>  > df
> *** glibc detected *** /home/hpages/R-2.13.1/bin/exec/R: malloc():
> memory corruption: 0x0000000002d73ca0 ***

For what it's worth, under valgrind

R -d valgrind -e "df <- cbind(a=integer(4000), b=raw(4000)) "
==19446== Memcheck, a memory error detector
==19446== Copyright (C) 2002-2009, and GNU GPL'd, by Julian Seward et al.
==19446== Using Valgrind-3.5.0 and LibVEX; rerun with -h for copyright info
==19446== Command: /home/mtmorgan/bin/R-devel/bin/exec/R --no-save 
--quiet -e df~+~\<-~+~cbind(a=integer(4000),~+~b=raw(4000))~+~
==19446==
 > df <- cbind(a=integer(4000), b=raw(4000))
==19446== Invalid write of size 8
==19446==    at 0x4E8C488: cbind (bind.c:1318)
==19446==    by 0x4E8B910: do_bind (bind.c:1117)
==19446==    by 0x4F77460: do_internal (names.c:1229)
==19446==    by 0x4F14543: Rf_eval (eval.c:469)
==19446==    by 0x4F154EC: Rf_applyClosure (eval.c:838)
==19446==    by 0x4F14856: Rf_eval (eval.c:513)
==19446==    by 0x4F17F19: do_set (eval.c:1724)
==19446==    by 0x4F14543: Rf_eval (eval.c:469)
==19446==    by 0x4F5D0A8: Rf_ReplIteration (main.c:255)
==19446==    by 0x4F5D295: R_ReplConsole (main.c:304)
==19446==    by 0x4F5E8F4: run_Rmainloop (main.c:985)
==19446==    by 0x4F5E909: Rf_mainloop (main.c:992)
==19446==  Address 0x8253298 is 0 bytes after a block of size 32,040 alloc'd
==19446==    at 0x4C26C3A: malloc (in 
/usr/lib64/valgrind/vgpreload_memcheck-amd64-linux.so)

and at bind.c:1306:1319

		if (TYPEOF(u) <= INTSXP) {
		    if (mode <= INTSXP) {
			for (i = 0; i < idx; i++)
			    INTEGER(result)[n++] = INTEGER(u)[i % k];
		    }
		    else {
			for (i = 0; i < idx; i++)
			    REAL(result)[n++] = (INTEGER(u)[i % k]) == NA_INTEGER ? NA_REAL : 
INTEGER(u)[i % k];
		    }
		}
		else {
		    for (i = 0; i < idx; i++)
			REAL(result)[n++] = REAL(u)[i % k];
		}

we end up writing REAL(result)[n++] = REAL(u)[i % k] even though

  (gdb) p TYPEOF(u)
$15 = 24

i.e., RAWSXP

(gdb) p TYPEOF(result)
$16 = 13

i.e., INTSXP

it's because the test TYPEOF(u) <= INTSXP fails bind.c:1306.

Martin

  sessionInfo()
R Under development (unstable) (2011-09-19 r57030)
Platform: x86_64-unknown-linux-gnu (64-bit)

locale:
  [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C
  [3] LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8
  [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8
  [7] LC_PAPER=C                 LC_NAME=C
  [9] LC_ADDRESS=C               LC_TELEPHONE=C
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base


> ======= Backtrace: =========
> /lib/x86_64-linux-gnu/libc.so.6(+0x79d7a)[0x7f3592b91d7a]
> /lib/x86_64-linux-gnu/libc.so.6(__libc_malloc+0x6e)[0x7f3592b9431e]
> /lib/libreadline.so.5(xmalloc+0x6)[0x7f3592620ab6]
> /lib/libreadline.so.5(add_history+0xbe)[0x7f359262136e]
> /home/hpages/R-2.13.1/lib/libR.so(+0x1b6538)[0x7f35936bd538]
> /lib/libreadline.so.5(rl_callback_read_char+0x8c)[0x7f359261cb1c]
> /home/hpages/R-2.13.1/lib/libR.so(+0x1b6f74)[0x7f35936bdf74]
> /home/hpages/R-2.13.1/lib/libR.so(Rf_ReplIteration+0xe7)[0x7f3593610eb7]
> /home/hpages/R-2.13.1/lib/libR.so(+0x10a260)[0x7f3593611260]
> /home/hpages/R-2.13.1/lib/libR.so(run_Rmainloop+0x50)[0x7f3593611780]
> /home/hpages/R-2.13.1/bin/exec/R(main+0x1b)[0x40085b]
> /lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xff)[0x7f3592b36eff]
> /home/hpages/R-2.13.1/bin/exec/R[0x400779]
> ======= Memory map: ========
> 00400000-00401000 r-xp 00000000 08:01 3811201
> /home/hpages/R-2.13.1/bin/exec/R
> 00600000-00601000 r--p 00000000 08:01 3811201
> /home/hpages/R-2.13.1/bin/exec/R
> 00601000-00602000 rw-p 00001000 08:01 3811201
> /home/hpages/R-2.13.1/bin/exec/R
> 02583000-03b16000 rw-p 00000000 00:00 0 [heap]
> 7f358c000000-7f358c021000 rw-p 00000000 00:00 0
> 7f358c021000-7f3590000000 ---p 00000000 00:00 0
> 7f3590342000-7f3590357000 r-xp 00000000 08:06 8155
> /lib/x86_64-linux-gnu/libgcc_s.so.1
> 7f3590357000-7f3590556000 ---p 00015000 08:06 8155
> /lib/x86_64-linux-gnu/libgcc_s.so.1
> 7f3590556000-7f3590557000 r--p 00014000 08:06 8155
> /lib/x86_64-linux-gnu/libgcc_s.so.1
> 7f3590557000-7f3590558000 rw-p 00015000 08:06 8155
> /lib/x86_64-linux-gnu/libgcc_s.so.1
> 7f3590558000-7f35905c4000 r-xp 00000000 08:01 3818237
> /home/hpages/R-2.13.1/library/stats/libs/stats.so
> 7f35905c4000-7f35907c3000 ---p 0006c000 08:01 3818237
> /home/hpages/R-2.13.1/library/stats/libs/stats.so
> 7f35907c3000-7f35907c5000 r--p 0006b000 08:01 3818237
> /home/hpages/R-2.13.1/library/stats/libs/stats.so
> 7f35907c5000-7f35907c7000 rw-p 0006d000 08:01 3818237
> /home/hpages/R-2.13.1/library/stats/libs/stats.so
> 7f35907c7000-7f359083a000 rw-p 00000000 00:00 0
> 7f359083a000-7f359085c000 r-xp 00000000 08:01 3817784
> /home/hpages/R-2.13.1/library/grDevices/libs/grDevices.so
> 7f359085c000-7f3590a5b000 ---p 00022000 08:01 3817784
> /home/hpages/R-2.13.1/library/grDevices/libs/grDevices.so
> 7f3590a5b000-7f3590a5c000 r--p 00021000 08:01 3817784
> /home/hpages/R-2.13.1/library/grDevices/libs/grDevices.so
> 7f3590a5c000-7f3590a5d000 rw-p 00022000 08:01 3817784
> /home/hpages/R-2.13.1/library/grDevices/libs/grDevices.so
> 7f3590b61000-7f3590b63000 r-xp 00000000 08:07 671027
> /usr/lib/x86_64-linux-gnu/gconv/ISO8859-1.so
> 7f3590b63000-7f3590d62000 ---p 00002000 08:07 671027
> /usr/lib/x86_64-linux-gnu/gconv/ISO8859-1.so
> 7f3590d62000-7f3590d63000 r--p 00001000 08:07 671027
> /usr/lib/x86_64-linux-gnu/gconv/ISO8859-1.so
> 7f3590d63000-7f3590d64000 rw-p 00002000 08:07 671027
> /usr/lib/x86_64-linux-gnu/gconv/ISO8859-1.so
> 7f3590d64000-7f3590de6000 rw-p 00000000 00:00 0
> 7f3590de6000-7f3590ded000 r-xp 00000000 08:01 3818288
> /home/hpages/R-2.13.1/library/methods/libs/methods.so
> 7f3590ded000-7f3590fec000 ---p 00007000 08:01 3818288
> /home/hpages/R-2.13.1/library/methods/libs/methods.so
> 7f3590fec000-7f3590fed000 r--p 00006000 08:01 3818288
> /home/hpages/R-2.13.1/library/methods/libs/methods.so
> 7f3590fed000-7f3590fee000 rw-p 00007000 08:01 3818288
> /home/hpages/R-2.13.1/library/methods/libs/methods.so
> 7f3590fee000-7f3590ffa000 r-xp 00000000 08:06 8319
> /lib/x86_64-linux-gnu/libnss_files-2.13.so
> 7f3590ffa000-7f35911f9000 ---p 0000c000 08:06 8319
> /lib/x86_64-linux-gnu/libnss_files-2.13.so
> 7f35911f9000-7f35911fa000 r--p 0000b000 08:06 8319
> /lib/x86_64-linux-gnu/libnss_files-2.13.so
> 7f35911fa000-7f35911fb000 rw-p 0000c000 08:06 8319
> /lib/x86_64-linux-gnu/libnss_files-2.13.so
> 7f35911fb000-7f3591206000 r-xp 00000000 08:06 8321
> /lib/x86_64-linux-gnu/libnss_nis-2.13.so
> 7f3591206000-7f3591405000 ---p 0000b000 08:06 8321
> /lib/x86_64-linux-gnu/libnss_nis-2.13.so
> 7f3591405000-7f3591406000 r--p 0000a000 08:06 8321
> /lib/x86_64-linux-gnu/libnss_nis-2.13.so
> 7f3591406000-7f3591407000 rw-p 0000b000 08:06 8321
> /lib/x86_64-linux-gnu/libnss_nis-2.13.so
> 7f3591407000-7f359141e000 r-xp 00000000 08:06 8316
> /lib/x86_64-linux-gnu/libnsl-2.13.so
> 7f359141e000-7f359161d000 ---p 00017000 08:06 8316
> /lib/x86_64-linux-gnu/libnsl-2.13.so
> 7f359161d000-7f359161e000 r--p 00016000 08:06 8316
> /lib/x86_64-linux-gnu/libnsl-2.13.so
> 7f359161e000-7f359161f000 rw-p 00017000 08:06 8316
> /lib/x86_64-linux-gnu/libnsl-2.13.so
> 7f359161f000-7f3591621000 rw-p 00000000 00:00 0
> 7f3591621000-7f3591629000 r-xp 00000000 08:06 8317
> /lib/x86_64-linux-gnu/libnss_compat-2.13.so
> 7f3591629000-7f3591828000 ---p 00008000 08:06 8317
> /lib/x86_64-linux-gnu/libnss_compat-2.13.so
> 7f3591828000-7f3591829000 r--p 00007000 08:06 8317
> /lib/x86_64-linux-gnu/libnss_compat-2.13.so
> 7f3591829000-7f359182a000 rw-p 00008000 08:06 8317
> /lib/x86_64-linux-gnu/libnss_compat-2.13.so
> 7f359182a000-7f3591d21000 r--p 00000000 08:07 590549
> /usr/lib/locale/locale-archive
> 7f3591d21000-7f3591d61000 r-xp 00000000 08:06 8139 /lib/libncurses.so.5.7
> 7f3591d61000-7f3591f60000 ---p 00040000 08:06 8139 /lib/libncurses.so.5.7
> 7f3591f60000-7f3591f64000 r--p 0003f000 08:06 8139 /lib/libncurses.so.5.7
> 7f3591f64000-7f3591f65000 rw-p 00043000 08:06 8139 /lib/libncurses.so.5.7
> 7f3591f65000-7f3591f6c000 r-xp 00000000 08:06 8326
> /lib/x86_64-linux-gnu/librt-2.13.so
> 7f3591f6c000-7f359216b000 ---p 00007000 08:06 8326
> /lib/x86_64-linux-gnu/librt-2.13.so
> 7f359216b000-7f359216c000 r--p 00006000 08:06 8326
> /lib/x86_64-linux-gnu/librt-2.13.soAborted
>
> This is with R 2.13.1 but it also crashes recent R devel.
>
> Thanks,
> H.
>
>  > sessionInfo()
> R version 2.13.1 (2011-07-08)
> Platform: x86_64-unknown-linux-gnu (64-bit)
>
> locale:
> [1] LC_CTYPE=en_CA.UTF-8 LC_NUMERIC=C
> [3] LC_TIME=en_CA.UTF-8 LC_COLLATE=en_CA.UTF-8
> [5] LC_MONETARY=C LC_MESSAGES=en_CA.UTF-8
> [7] LC_PAPER=en_CA.UTF-8 LC_NAME=C
> [9] LC_ADDRESS=C LC_TELEPHONE=C
> [11] LC_MEASUREMENT=en_CA.UTF-8 LC_IDENTIFICATION=C
>
> attached base packages:
> [1] stats graphics grDevices utils datasets methods base
>


-- 
Computational Biology
Fred Hutchinson Cancer Research Center
1100 Fairview Ave. N. PO Box 19024 Seattle, WA 98109

Location: M1-B861
Telephone: 206 667-2793



[Rd] cbind() crashes on raw vectors
Martin Morgan