building R packages to use ASAN

68 Views Asked by At

Background: I am developing a package that uses Rcpp and contains some C++ files. I would like to analyze my code with ASAN. I am struggling to build my package.

What I have done so far

  1. I am using https://hub.docker.com/r/rocker/r-devel-san docker container that comes with pre-built R (or RD) for working with ASAN.
  2. Within this container, I could run a C++ script with -fsanitize=address flag to get a meaningful ASAN output.
  3. My src/Makevars files look like the following (not all flags are relevant to this question):
CC = gcc -fsanitize=address -fno-omit-frame-pointer
CXX = g++
LDFLAGS = -fsanitize=address
PKG_CXXFLAGS = -fsanitize=address -fno-omit-frame-pointer -fno-sanitize-address-use-after-scope -D_GCL_RHACK_ -DGC_NO_MUTEX -m64 -ldl -lpthread -Wall -pipe -pedantic -Wno-unused -DNDEBUG -g -std=c++17
PKG_CFLAGS = -fsanitize=address -D_GCL_RHACK_ -fno-sanitize-address-use-after-scope
  1. To install, I do (from the directory containing my package directory foo):
RD CMD build foo
RD CMD install -l temp_lib/ foo_1.0.tar.gz

The install command throws the following error (along with the last line of teh build)

-g -O2  -c loadpathutil.c -o loadpathutil.o
g++ -fsanitize=undefined,bounds-strict -fno-omit-frame-pointer -std=gnu++17 -shared -L/usr/local/lib/R/lib -L/usr/local/lib -o foo.so RcppExports.o Read.o Utilities.o Write.o gdxcc.o loadpathutil.o -L/usr/local/lib/R/lib -lR
** R
** byte-compile and prepare package for lazy loading
** help
*** installing help indices
** building package indices
** testing if installed package can be loaded from temporary location
Error: package or namespace load failed for ‘foo’ in dyn.load(file, DLLpath = DLLpath, ...):
installing to /work/temp_lib/00LOCK-foo/00new/foo/libs

unable to load shared object '/work/temp_lib/00LOCK-foo/00new/foo/libs/foo.so':
  /work/temp_lib/00LOCK-foo/00new/foo/libs/foo.so: undefined symbol: __asan_option_detect_stack_use_after_return

I understand that the final package shared object has to be somehow linked to ASAN but the suggested solutions LDFLAGS, PKG_LDFLAGS don't seem to help.

1

There are 1 best solutions below

0
JRR On

This the document I wrote for myself and that I used myself.

  1. Download and decrompress R source code
  2. Install the compiler clang. apt-get install clang should do the job
  3. Open config.site and add the following at the end
## clang-ASAN, clang-UBSAN:
CC="clang -fsanitize=address,undefined -fno-sanitize=float-divide-by-zero -fno-sanitize=alignment -fno-omit-frame-pointer"
CXX="clang++ -fsanitize=address,undefined -fno-sanitize=float-divide-by-zero -fno-sanitize=alignment -fno-omit-frame-pointer -frtti"
CFLAGS="-g -O3 -Wall -pedantic"
FFLAGS="-g -O2 -mtune=native"
CXXFLAGS="-g -O3 -Wall -pedantic"
MAIN_LD="clang++ -fsanitize=undefined,address"

export ASAN_OPTIONS='detect_leaks=0:detect_odr_violation=0'
export UBSAN_OPTIONS='print_stacktrace=1'
export RGL_USE_NULL="true"
export R_DONT_USE_TK="true"
  1. run
./configure \
    --prefix=/opt/R/${R_VERSION} \
    --enable-memory-profiling \
    --enable-R-shlib \
    --with-blas \
    --with-lapack
  1. run make

Then we can check if it actually works by running R in bin/R and triggering errors using the package sanitizers.

install.packages("sanitizers")
sanitizers::stackAddressSanitize(42)
sanitizers::heapAddressSanitize(10)

Then use R as usual and install your package as usual