[R-pkg-devel] How to check memory access (understanding Section 4.3 WRE)

Marius Hofert m@riu@@hofert @ending from uw@terloo@c@
Mon Jun 18 17:53:33 CEST 2018


I tried to understand how to check memory access of an R package with
C code; see https://cran.r-project.org/doc/manuals/r-release/R-exts.html#Checking-memory-access

I'm about 90% sure I understood WRE Section 4.3.2 about valgrind. In essence:
1) When configuring R before make, use the configure flags '-C
--with-valgrind-instrumentation=2 --with-system-valgrind-headers'
   to get a memory-profiling-enabled version of R. This I found on
   and it's also described at the bottom of
2) Then use 'R CMD check --use-valgrind' or directly check examples via
   'R -d "valgrind --tool=memcheck --leak-check=full" --vanilla < mypkg-Ex.R'
I also seems to work (but reports false positives for Mac as others
have reported).

I'm still struggling with 4.3.3, see
It says "The simplest way to make use of this is to build a version of
R with something like..." and
CC="gcc -std=gnu99 -fsanitize=address"
CFLAGS="-fno-omit-frame-pointer -g -O2 -Wall -pedantic -mtune=native"
I set these flags via 'configure CC="gcc -std=gnu99
-fsanitize=address" CFLAGS="-fno-omit-frame-pointer -g -O2 -Wall
-pedantic -mtune=native"' before 'make'. I used R-devel for it and the
installation was fine. Is there any option 'R CMD check' would need to
obtain (like the '--use-valgrind' above)? 'R CMD check mypkg.tar.gz'
doesn't seem to do any memory access checking. Would I also need to
set "MAIN_LDFLAGS=-fsanitize=address" for configure? (it's a bit
unclear from WRE whether this applies to both of the approaches
mentioned or just to the one based on Makevars)

I also tried the second approach based on "~/.R/Makevars" and put the
following in "~/.R/Makevars" as suggested in WRE:
CC = gcc -std=gnu99 -fsanitize=address -fno-omit-frame-pointer
CXX = g++ -fsanitize=address -fno-omit-frame-pointer
F77 = gfortran -fsanitize=address
FC = gfortran -fsanitize=address
'R CMD check mypkg.tar.gz' then fails and 00install.out shows (on
macOS 10.13.5, R-3.5.0 here):

* installing *source* package ‘mypkg’ ...
** libs
* R
** demo
** byte-compile and prepare package for lazy loading
** help
*** installing help indices
** building package indices
** testing if installed package can be loaded
sh: line 1: 14016 Abort trap: 6
'/usr/local/R/R-3.5.0_build/bin/R' --no-save --slave 2>&1 <
==14016==ERROR: Interceptors are not working. This may be because
AddressSanitizer is loaded too late (e.g. via dlopen). Please launch
the executable with:
"interceptors not installed" && 0
ERROR: loading failed
* removing ‘/Users/mhofert/pro/mypkg/mypkg.Rcheck/mypkg’
Any ideas where the 'DYLD_INSERT_LIBRARIES' should go?

Thanks & cheers,

More information about the R-package-devel mailing list