astamm / nloptr

nloptr provides an R interface to NLopt, a free/open-source library for nonlinear optimization providing a common interface to a number of different optimization routines which can handle nonlinear constraints and lower and upper bounds for the controls.
https://astamm.github.io/nloptr/
Other
106 stars 34 forks source link

ASAN problems #158

Closed bbolker closed 12 hours ago

bbolker commented 5 months ago

I'm not sure why these aren't being flagged on CRAN yet, but when run with r-devel with address-sanitization flags on I get lots of errors ... haven't yet tried to see if this is new in r-devel ...

library(nloptr)
 fr <- function(x) {   ## Rosenbrock Banana function
         100 * (x[2] - x[1]^2)^2 + (1 - x[1])^2
     }
     (S <- bobyqa(c(0, 0, 0), fr, lower = c(0, 0, 0), upper = c(0.5, 0.5, 0.5)))
/tmp/Rtmp0I31rp/R.INSTALL1d4f354cd902/nloptr/src/nlopt-src/src/algs/bobyqa/bobyqa.c:2118:11: runtime error: load of address 0x5ce1ba042f08 with insufficient space for an object of type 'double'
0x5ce1ba042f08: note: pointer points here
 00 00 c8 bf  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  00 00 00 00 00 e0 37 40  00 00 00 00
              ^ 
/tmp/Rtmp0I31rp/R.INSTALL1d4f354cd902/nloptr/src/nlopt-src/src/algs/bobyqa/bobyqa.c:2118:20: runtime error: load of address 0x5ce1ba042f20 with insufficient space for an object of type 'double'
0x5ce1ba042f20: note: pointer points here
 00 e0 37 40  00 00 00 00 00 00 00 00  00 00 00 00 00 00 69 40  00 00 00 00 00 00 00 00  00 00 00 00
              ^ 
/tmp/Rtmp0I31rp/R.INSTALL1d4f354cd902/nloptr/src/nlopt-src/src/algs/bobyqa/bobyqa.c:2118:31: runtime error: load of address 0x5ce1ba042ee8 with insufficient space for an object of type 'double'
0x5ce1ba042ee8: note: pointer points here
 00 00 f0 3f  00 00 00 00 00 00 c0 3f  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  00 00 00 00
              ^ 
/tmp/Rtmp0I31rp/R.INSTALL1d4f354cd902/nloptr/src/nlopt-src/src/algs/bobyqa/bobyqa.c:2118:15: runtime error: store to address 0x5ce1ba042f08 with insufficient space for an object of type 'double'
0x5ce1ba042f08: note: pointer points here
 00 00 c8 bf  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  00 00 00 00 00 e0 37 40  00 00 00 00
              ^ 
/tmp/Rtmp0I31rp/R.INSTALL1d4f354cd902/nloptr/src/nlopt-src/src/algs/bobyqa/bobyqa.c:2611:17: runtime error: load of address 0x5ce1ba042f20 with insufficient space for an object of type 'double'
0x5ce1ba042f20: note: pointer points here
 00 e0 37 40  00 00 00 00 00 00 00 00  00 00 00 00 00 00 69 40  00 00 00 00 00 00 00 00  00 00 00 00
              ^ 
/tmp/Rtmp0I31rp/R.INSTALL1d4f354cd902/nloptr/src/nlopt-src/src/algs/bobyqa/bobyqa.c:2797:11: runtime error: load of address 0x5ce1ba042f08 with insufficient space for an object of type 'double'
0x5ce1ba042f08: note: pointer points here
 00 00 b2 3c  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  00 00 00 00 00 e0 37 40  00 00 00 00
              ^ 
/tmp/Rtmp0I31rp/R.INSTALL1d4f354cd902/nloptr/src/nlopt-src/src/algs/bobyqa/bobyqa.c:2797:20: runtime error: load of address 0x5ce1ba042f20 with insufficient space for an object of type 'double'
0x5ce1ba042f20: note: pointer points here
 00 e0 37 40  00 00 00 00 00 00 00 00  00 00 00 00 00 00 69 40  00 00 00 00 00 00 00 00  00 00 00 00
              ^ 
/tmp/Rtmp0I31rp/R.INSTALL1d4f354cd902/nloptr/src/nlopt-src/src/algs/bobyqa/bobyqa.c:2797:30: runtime error: load of address 0x5ce1ba043178 with insufficient space for an object of type 'double'
0x5ce1ba043178: note: pointer points here
 e1 5c 00 00  70 50 3c ed 71 15 80 3f  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  5c 4e 62 c7
              ^ 
/tmp/Rtmp0I31rp/R.INSTALL1d4f354cd902/nloptr/src/nlopt-src/src/algs/bobyqa/bobyqa.c:2797:15: runtime error: store to address 0x5ce1ba042f08 with insufficient space for an object of type 'double'
0x5ce1ba042f08: note: pointer points here
 00 00 b2 3c  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  00 00 00 00 00 e0 37 40  00 00 00 00
              ^ 
/tmp/Rtmp0I31rp/R.INSTALL1d4f354cd902/nloptr/src/nlopt-src/src/algs/bobyqa/bobyqa.c:2874:21: runtime error: load of address 0x5ce1ba042f08 with insufficient space for an object of type 'double'
0x5ce1ba042f08: note: pointer points here
 c5 a8 79 3f  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  00 00 00 00 00 e0 37 40  00 00 00 00
              ^ 
/tmp/Rtmp0I31rp/R.INSTALL1d4f354cd902/nloptr/src/nlopt-src/src/algs/bobyqa/bobyqa.c:2883:21: runtime error: load of address 0x5ce1ba042f00 with insufficient space for an object of type 'double'
0x5ce1ba042f00: note: pointer points here
 00 00 e0 3f  00 30 fb 43 1e 1c c1 bf  00 b4 66 ca 5b 45 cc 3f  00 3c 53 78 c8 0f e9 bf  80 71 26 fc
              ^ 
$par
aadler commented 5 months ago

Hi, @bbolker. I'm not enough of a C guru to try to dig into this, but reading through your pain with lme4, I think this is an outgrowth of whatever change was made to R's internal checking. BTW, you find these errors with the most recent, unpushed version of nloptr, right?

astamm commented 5 months ago

I will run nloptr on r-devel before releasing next week and go back at you if I see these issues as well.

astamm commented 5 months ago

I just ran the master branch on rhub platform clang-asan R-devel (2024-06-14 r86747) Ubuntu 22.04.4 LTS and there has been no issue.

astamm commented 5 months ago

However, it is not gcc-asan.

aadler commented 5 months ago

Based on the discussion in lme4, it looks like an NLOPT issue in its sources and not with nloptr. I opened an issue at NLOPT.

bbolker commented 5 months ago

To reproduce this:

docker pull rocker/r-devel-san
docker run --rm -ti rocker/r-devel-san bash
apt update && apt upgrade -y && apt install -y cmake
RD  ## run development version of R

From inside R:

install.packages("remotes")
remotes::install_github("astamm/nloptr", force = TRUE)
library(nloptr)
 fr <- function(x) {   ## Rosenbrock Banana function
         100 * (x[2] - x[1]^2)^2 + (1 - x[1])^2
     }
     (S <- bobyqa(c(0, 0, 0), fr, lower = c(0, 0, 0), upper = c(0.5, 0.5, 0.5)))
eddelbuettel commented 5 months ago

(Maintainer of that container here: It is imperative to use RD as that is the SAN-instrumented version on the container and by standard debugging convention that is always the upcoming R version, ie r-devel, and therefore RD. Standard 'r-release' aka R is present to easily satisfy build and run-time dependencies. So in short: Do not expect effects to materialize on an uninstrumented version such as R and use RD to debug for SAN effects.)

bbolker commented 5 months ago

It would be interesting to rebuild r-devel in this container with one or both of these lines commented out, to confirm that the enabling of these checks is indeed what drives the recent (??) appearance of these warnings.

PS I see the same errors with cobyla, newuoa, etc. (not lbfgs or sbplx), so it's fairly general.

aadler commented 5 months ago

Interesting, @bbolker. If I understand correctly, all three are algorithms by the late Dr. Michael Powell and all three work similarly. COBYLA constructs linear approximations and BOBYQA and its predecessor NEWUOA construct quadratic approximations. All three were originally Fortran code which was ported. So it stands to reason that the internals are similar. I haven't followed the discussion on R-devel too well (you and @eddelbuettel understand this much better), but there seems to be an issue passing a vector based on an empty? pointer, depending if it goes through the function REAL or the macro REAL, one of which uses DATAPTR and one does not. Lines 2082 and 2118, for example, are assignations to a vector. Are there possible cases where something isn't initialized properly and is empty? I am way out of my league 😞 .

eddelbuettel commented 5 months ago

@aadler we all are but Luke is working on that: https://mastodon.social/deck/@eddelbuettel/112633661372552025

aadler commented 1 day ago

Well, nlopt closed the issue as unreproducilble. @bbolker, are you still having this issue?

bbolker commented 12 hours ago

Probably too much trouble to try to reproduce it at this point -- go ahead and close.