Specify valgrind options to R package on Travis CI

534 views Asked by At

I am trying to build an R package with c++ code on Travis CI and checking it with valgrind by running R CMD check with the --use-valgrind option. In the R extensions manual it says to either provide a ~/.valgrindrc file with the required options or set them in the environment variable VALGRIND_OPTS but I haven't been able to figure out either of these.

I have created a demo repo here that includes a Rcpp function which valgrind ought to respond to.

My .travis.yml setup looks like this:

language: r
sudo: false
cache: packages

r_check_args: '--use-valgrind'

addons:
  apt:
    packages:
      - valgrind

I have a .valgrindrc file with the following,

--leak-check=full
--track-origins=yes

, and have tried to put it in ./inst/ and the root directory but none of that has worked. Does anybody know how to set these options properly?

Edit: here is a link to the latest travis build.

3

There are 3 answers

1
joepd On BEST ANSWER

According to the docs, the file ./.valgrindrc (so in the current working directory, is only read if the file ~/.valgrindrc does not exist, and the environment variable $VALGRIND_OPTS is not set.

Not sure what is the case, but you might want to do the following:

before_install:
  - "head ~/.valgrindrc || echo '~/.valgrindrc does not exist'"
  - echo VALGRIND_OPTS: $VALGRIND_OPTS
  - cat ./valgrindrc >> ~/.valgrindrc

The first two are there for diagnostic purposes. The last one might fix the issue (it appends your configuration to the global configuration). If it would not work, you will likely have some more insight in what is at play.

0
Johan Larsson On

The solution given by @joepd works but I discovered a (perhaps) leaner solution in simply setting environment variables.

env:
  - VALGRIND_OPTS='--leak-check=full --track-origins=yes'

Note that valgrind still advises us to set --leak-check=full for some unknown reason, which is what brought me to look fro another solution in the first place.

0
mpadge On

You can also use the following script to customise valgrind checks, and to flag what kind of output should cause a travis failure. Just make some kind of valgrind-script.R to load and test package functionality, then put both that and the following in the tests folder. (valgrind also needs to be installed with .travis.yml.)

vg_check <- function ()
{
    vg <- system2 (command = 'R',
                   args = c ('-d "valgrind --tool=memcheck --leak-check=full"',
                             '-f valgrind-script.R'),
                   stdout = TRUE, stderr = TRUE)

    lost <- NULL
    types <- c ("definitely lost", "indirectly lost", "possibly lost")
    for (ty in types)
    {
        lost_type <- which (grepl (ty, vg))
        n <- regmatches(vg [lost_type], gregexpr("[[:digit:]]+", vg [lost_type]))
        lost <- c (lost, as.numeric (n [[1]] [2:3]))
    }
    if (any (lost > 0))
        stop ("valgrind memory leaks detected!")

    if (attr (vg, "status") != 0)
        stop ("valgrind error")
}

if (identical (Sys.getenv ("TRAVIS"), "true"))
{
    vg_check ()
}

This uses memcheck, but you can customise it to you use valgrind tool you like, and to translate any kind of output into a travis failure.