Skip to content

Instantly share code, notes, and snippets.

@jonlachmann
Last active May 29, 2023 13:32
Show Gist options
  • Save jonlachmann/890a51471e7b43b1b9eea58b9cf65eda to your computer and use it in GitHub Desktop.
Save jonlachmann/890a51471e7b43b1b9eea58b9cf65eda to your computer and use it in GitHub Desktop.
Running address sanitizing in R/Rcpp/RcppEigen
To be able to run address sanitizing in your own Rcpp/RcppEigen code, you need a version of R that is compiled with support for it. You also need to compile all Rcpp libraries that you will use using address sanitizing. Finally, your own code must also be compiled with it. This is the steps that I have found that works.
1. Download Docker for your platform
2. Install the rocker docker image and give it 8GB of memory (installation of Rcpp will fail on 2GB)
docker pull rocker/r-devel-san
docker container run --memory=8g -it --entrypoint bash rocker/r-devel-san
3. Set the Makevars to be used for ALL compiling of C++ code in R by creating /root/.R/Makevars with
PKG_CXXFLAGS = -fsanitize=address -I../inst/include
4. Install devtools dependencies
apt-get install libxml2-dev libssl-dev
5. Run R while preloading the libasan shared lib
LD_PRELOAD=/usr/lib/gcc/x86_64-linux-gnu/12/libasan.so RD
6. Install devtools, Rcpp and RcppEigen, making sure that you see "-fsanitize=address" when it compiles stuff
install.packages("devtools")
install.packages("Rcpp")
install.packages("RcppEigen")
7a. Running a single cpp file: Source your file through sourceCpp("file.cpp").
7b. Running a package containing cpp code: Add "-fsanitize=address" to the src/Makevars file of your package, build it and install it.
8. Run the R code causing the problems. Address sanitizer should now pick up any writes outside allocated regions etc.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment