jwood000 / RcppAlgos

Tool for Solving Problems in Combinatorics and Computational Mathematics
GNU General Public License v2.0
45 stars 5 forks source link

USBAN clang : nan is outside the range of representable values of type 'int' #1

Closed jwood000 closed 6 years ago

jwood000 commented 6 years ago

Here is 00check.log

* using log directory ‘/data/gannet/ripley/R/packages/tests-clang-SAN/RcppAlgos.Rcheck’
* using R Under development (unstable) (2018-06-14 r74898)
* using platform: x86_64-pc-linux-gnu (64-bit)
* using session charset: UTF-8
* using option ‘--no-stop-on-test-error’
* checking for file ‘RcppAlgos/DESCRIPTION’ ... OK
* this is package ‘RcppAlgos’ version ‘2.0.2’
* package encoding: UTF-8
* checking package dependencies ... OK
* checking if this is a source package ... OK
* checking if there is a namespace ... OK
* checking for hidden files and directories ... OK
* checking for portable file names ... OK
* checking whether package ‘RcppAlgos’ can be installed ... [225s/127s] OK
* checking package directory ... OK
* checking whether the package can be loaded ... OK
* checking whether the package can be loaded with stated dependencies ... OK
* checking whether the package can be unloaded cleanly ... OK
* checking whether the namespace can be loaded with stated dependencies ... OK
* checking whether the namespace can be unloaded cleanly ... OK
* checking loading without being on the library search path ... OK
* checking compiled code ... OK
* checking examples ... [16s/16s] OK
* checking tests ... [41s/42s] OK
Running ‘testthat.R’ [40s/40s]
* DONE
Status: OK

And here is the testthat.Rout

R Under development (unstable) (2018-06-14 r74898) -- "Unsuffered Consequences"
Copyright (C) 2018 The R Foundation for Statistical Computing
Platform: x86_64-pc-linux-gnu (64-bit)

R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.

R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.

Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.

> library(testthat)
> library(RcppAlgos)
> 
    > test_check("RcppAlgos")
/usr/local/bin/../include/c++/v1/memory:1805:35: runtime error: nan is outside the range of representable values of type 'int'
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior /usr/local/bin/../include/c++/v1/memory:1805:35 in 
══ testthat results  ═══════════════════════════════════════════════════════════
OK: 395 SKIPPED: 0 FAILED: 0
> 
    > proc.time()
user  system elapsed 
38.829   1.306  40.290
jwood000 commented 6 years ago

The problem was arising from the following lines of code:

if (IsInteger)
        vInt.assign(vNum.begin(), vNum.end());

Before this, vNum (a vector of doubles) could possibly contain a nan value. As a result, the code above would trigger undefined behavior as there is no defined conversion from nan to int. We rectify this with the following setup:

if (IsInteger) {
    for (int i = 0; i < n && IsInteger; ++i)
        if (Rcpp::NumericVector::is_na(vNum[i]))
            IsInteger = false;

    if (IsInteger)
        vInt.assign(vNum.begin(), vNum.end());
}

The fix has been confirmed with a clean run from docker with rocker/r-devel-ubsan-clang.

Many thanks to Brian J. Knaus and Dirk Eddelbuettel for posting excellent articles on this topic:

https://knausb.github.io/2017/06/reproducing-a-clang-ubsan-issue/ https://knausb.github.io/2017/06/running-r-devel-ubsan-clang-in-docker/

https://stackoverflow.com/a/33179049/4408538 https://github.com/rocker-org/r-devel-san-clang http://dirk.eddelbuettel.com/blog/2015/01/18/

franzbischoff commented 3 years ago

OffTopic, how can I test using this docker: rocker/r-devel-ubsan-clang

just drop my built .tar.gz file there and run R CMD check?

thanks

jwood000 commented 3 years ago

@franzbischoff,

It's a little more involved than that. Those links I posted are great starts.

Personally, it took me a while (about 1.5 weeks) to figure out how to get it running for my specific case. I read quite a bit of documentation and articles before I was able to get it working properly. It still wasn't over. I had to read a dozen more articles/post/documentation on what the output meant.

I have another issue (https://github.com/jwood000/RcppAlgos/issues/8) that goes into this in more detail.

I hope this helps.

Cheers, Joseph