GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — add template specializations to avoid DATAPTR use by kevinushey · Pull Request #1310 · RcppCore/Rcpp

Conversation

@kevinushey

@kevinushey kevinushey commented Jun 11, 2024 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

Addresses #1308, but note that this PR would turn these out-of-bounds accesses into R errors, and so we might expect to see some CRAN failures. If that feels too aggressive, we could make these warnings instead, or control the behavior with an environment variable (read on package load), or something else.

This PR seeks to avoid some of the crashes seen in packages using Rcpp, where out-of-bounds accessed could trigger an ASAN misaligned read (due to special behavior in DATAPTR attempting to catch such usages).

This PR also now adds bounds checking to offset accessing via operator[] for the various Vector classes provided by Rcpp; controlled via the same RCPP_NO_BOUNDS_CHECK macro used by the subsetting code.

For example:

#include <Rcpp.h>
using namespace Rcpp;

// [[Rcpp::export]]
double get(NumericVector x, R_xlen_t index) {
    return x[index];
}

/*** R
get(42, 2)
*/

Would now give:

> get(42, 2)
Error in eval(ei, envir) : 
  subscript out of bounds (index 2 >= vector size 1)

Checklist

  • Code compiles correctly
  • R CMD check still passes all tests
  • Preferably, new tests were added which fail without the change
  • Document the changes by file in ChangeLog

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
kevinushey added 4 commits June 11, 2024 15:41
@kevinushey
add template specializations to avoid DATAPTR use
936c19b
@kevinushey
also implement bounds checking in vector access
d48a6ed
@kevinushey
tweak error message
84c885a
@kevinushey
add tests; implement bounds check for proxy
3addbc9
@kevinushey
kevinushey requested a review from eddelbuettel June 11, 2024 23:13
kevinushey added 2 commits June 11, 2024 16:14
@kevinushey
finish NEWS
394e220
@kevinushey
don't check index when constructing iterators
c8c3a5c
kevinushey
kevinushey commented Jun 11, 2024
View reviewed changes
Comment thread tests/tinytest.R Resolved

Uh oh!

There was an error while loading. Please reload this page.

kevinushey added 3 commits June 11, 2024 16:32
@kevinushey
avoid invoking wrong 'get'
1d7ce1b
@kevinushey
don't pass 'ncpus'
4c8df0c
@kevinushey
finish NEWS
2520e51
@eddelbuettel

eddelbuettel commented Jun 11, 2024

Copy link
Copy Markdown
Member

Nice stuff. Would you mind if I spend the usual day or two and a half on rev.deps, "just in case" ?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@kevinushey
test tweaks
528f453
@Enchufa2

Enchufa2 commented Jun 12, 2024

Copy link
Copy Markdown
Member

Nice implementation, LGTM! Given that these now are going to be ASAN errors on CRAN anyways, I'd say it's fine if they trigger errors from our side.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@eddelbuettel

eddelbuettel commented Jun 12, 2024

Copy link
Copy Markdown
Member

Given that these now are going to be ASAN errors on CRAN anyways, I'd say it's fine if they trigger errors from our side.

Very true but it may make updating at CRAN harder for us if we trigger N new things in M packages.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@eddelbuettel
Small robustification for package version check during dev cycle
69cb4dd
@eddelbuettel
eddelbuettel force-pushed the bugfix/r-vector-start-specializations branch from 86cf6ba to 69cb4dd Compare June 12, 2024 13:27
@kevinushey

kevinushey commented Jun 12, 2024

Copy link
Copy Markdown
Contributor Author

Nice stuff. Would you mind if I spend the usual day or two and a half on rev.deps, "just in case" ?

For sure -- we could also consider whether we should allow users (or CRAN) to configure whether out-of-bounds checks should be errors, warnings, or something else.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@eddelbuettel

eddelbuettel commented Jun 12, 2024 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member

Three hours in (had some follow-up to do on RcppArmadillo run first)

Test of Rcpp 1.0.12.3.1.1 had 142 successes, 3 failures, and 1 skipped packages.

2 of the 3 are out of bounds (aum, BART) the third is small numerical diff on test so maybe not us (bayesianVars).

PS1: Twelve hours in:

Test of Rcpp 1.0.12.3.1.1 had 723 successes, 6 failures, and 6 skipped packages

PS2: Twentyfour hours in

Test of Rcpp 1.0.12.3.1.1 had 1514 successes, 16 failures, and 14 skipped packages.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@bbolker

bbolker commented Jun 12, 2024

Copy link
Copy Markdown
Contributor

Will look forward to seeing whether this resolves lme4/lme4#794 (still hoping that what we're seeing is not the revealing of previously cryptic out-of-bounds accesses, but rather things that are legal but trigger an ASAN warning anyway ...)

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@bbolker bbolker mentioned this pull request Jun 13, 2024
lme4/RcppEigen gcc-UBSAN issues lme4/lme4#794
Closed
@eddelbuettel

eddelbuettel commented Jun 16, 2024

Copy link
Copy Markdown
Member

The reverse depends run finished just as I was leaving for a two-day bike trip. The results are not terrible but also not something which makes me think we can commit this as is:

Test of Rcpp 1.0.12.3.1.1 had 2787 successes, 40 failures, and 25 skipped packages.
Ran from 2024-06-12 08:35:10.73 to 2024-06-14 06:50:59.41 for 1.928 days
Average of 58.397 secs relative to 311.843 secs using 7 runners

Failed packages:  aum, BART, bayesianVARs, birdie, chouca, cna, fixest, FLOPART, FLSSS, HistDAWass, kdevine, LOPART, magi, maxnodf, mined, mixvlmc, nanotime, netdiffuseR, nlmixr2est, o2geosocial, ondisc, onemap\
, openxlsx, pedmod, PKPDsim, Rborist, reservr, revdbayes, rsparse, rstpm2, rxode2, secr, supc, thunder, TreeDist, TSrepr, VAJointSurv, vapour, xrnet, ZIPBayes

Almost all the 'fails' are due to run-time and not install-time so it could be the 'harsher' out of bounds access. I will try to summarize later.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@eddelbuettel

eddelbuettel commented Jun 16, 2024 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member

@kevinushey With the extra delay of me disappearing for 1 1/2 or 2 days 😀 I was now able to summarize the reverse dependency run. As logged, it is essentially 'out of bounds'. So we probably want to preemptively define the 'death by bounds check' away, or maybe tone it down to a message.

PS I played a little with a simple define to avoid the current stop() but it gets me into different trouble and segfaults.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@Enchufa2

Enchufa2 commented Jun 17, 2024

Copy link
Copy Markdown
Member

Most of them already have ASAN/UBSAN errors on CRAN, and the others will likely have them when the CRAN machinery reaches them.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@eddelbuettel

eddelbuettel commented Jun 17, 2024 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member

I am much less sanguine here as we are (by the standard release pattern) looking into a release in two week (early July) to four weeks (mid July). I think we need to address this, or possibly roll it to the next release with sufficient time to communicate to affected maintainers. (Also, package nanotime was in that set and as I prepared a release for it yesterday it was not very clear what/how to change for it. It used zero-length arrays as a feature yet some of its unit tests balked and while there may be a out of index write hiding there are also other issues present.)

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@Enchufa2

Enchufa2 commented Jun 17, 2024

Copy link
Copy Markdown
Member

Then, let's make it a warning for now and an error in the next release?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@eddelbuettel

eddelbuettel commented Jun 17, 2024

Copy link
Copy Markdown
Member

I ran into errors when I mod'ed it to 'continue' rather than stop().

@kevinushey Any insights from your end?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@eddelbuettel eddelbuettel mentioned this pull request Jun 18, 2024
New CRAN UBSAN issue(s) eddelbuettel/nanotime#123
Closed
kevinushey added 2 commits June 18, 2024 09:37
@kevinushey
use warning instead of stop
bc529e0
@kevinushey
update NEWS
d412e22
@kevinushey
NEWS once more
3f4d5df
@eddelbuettel

eddelbuettel commented Jun 18, 2024 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member

Thanks for the update, @kevinushey -- I will give this a spin in another reverse-dependency run.

Another thing I noticed was that if one where to define the 'off-switch' the code doesn't actually compile cleanly. I tried this diff

diff --git a/inst/include/Rcpp/vector/Subsetter.h b/inst/include/Rcpp/vector/Subsetter.h
index 0d8b32a6..0905e1a3 100644
--- a/inst/include/Rcpp/vector/Subsetter.h
+++ b/inst/include/Rcpp/vector/Subsetter.h
@@ -147,7 +147,7 @@ private:
     }
     #else
     template <typename IDX>
-    void check_indices(IDX* x, IDX n, IDX size) {}
+    void check_indices(IDX* x, R_xlen_t n, R_xlen_t size) {}
     #endif

     void get_indices( traits::identity< traits::int2type<INTSXP> > t ) {

Can you take a peek?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@kevinushey
fixup for check_indices in subsetter
c1ca0bb
@kevinushey

kevinushey commented Jun 18, 2024

Copy link
Copy Markdown
Contributor Author

Should be good now. We now give a warning rather than a hard error; I indicated in NEWS that this might change in the future.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@eddelbuettel

eddelbuettel commented Jun 18, 2024

Copy link
Copy Markdown
Member

We now give a warning rather than a hard error

In one spot. You left a stop() in the other. On purpose?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@kevinushey

kevinushey commented Jun 18, 2024

Copy link
Copy Markdown
Contributor Author

I left https://github.com/RcppCore/Rcpp/pull/1310/files#diff-4d30eca18cf4d872269fc011605a047e2212875dcb25688bd2460986c632064bL144 alone because that was pre-existing code (we already gave a hard error in those cases) but I can switch that to a warning if we want to be uniform?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@eddelbuettel

eddelbuettel commented Jun 18, 2024

Copy link
Copy Markdown
Member

Oh, I completely missed that we already stopped there / that that bit was not new code. My bad!

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@eddelbuettel

eddelbuettel commented Jun 21, 2024

Copy link
Copy Markdown
Member

So I ran another reverse dependency check, and the result summary is here. It has seven failures of which one is due to missing build dependencies and at least one is recurrent leaving five. One is possibly local / spurious (connection issue), leaving four. CRAN may let me get away with arguing that the DATAPTR improvement is worth it.

Enchufa2 reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@eddelbuettel

eddelbuettel commented Jun 22, 2024

Copy link
Copy Markdown
Member

We are down to five now. One is possibly a test setup issue 1. One is a package for which CRAN requested other changes. I will open an issue here with links and try to get in touch with the maintainers -- but first things first: I will merge this, and then make a new incremental release 1.0.12.4 with this merged and make it available via the drat (and r-universe) so that the affected maintainers can test with it.

Footnotes

  1. Sometimes tests fail on my end and not at CRAN, here we have a package not getting on with RcppArmadillo on the spot compilation. ↩

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

eddelbuettel
eddelbuettel approved these changes Jun 22, 2024
View reviewed changes

@eddelbuettel eddelbuettel left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is ready now. Big thank you for preparing it!

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@eddelbuettel
eddelbuettel merged commit 5614a8b into master Jun 22, 2024
@eddelbuettel
eddelbuettel deleted the bugfix/r-vector-start-specializations branch June 22, 2024 16:06
@eddelbuettel eddelbuettel mentioned this pull request Jun 22, 2024
Preparing for Rcpp 1.0.13 and asking four packages for an adjustment #1311
Closed
3 of 4 tasks
@Enchufa2 Enchufa2 mentioned this pull request Mar 27, 2026
Return dataptr() again to avoid getting an invalid address for empty vectors #1462
Merged
4 tasks done
This was referenced Sep 30, 2026
per-element bounds check and uncached lengths slow down vector access and sugar #1510
Closed
speed up bounds-checked element access and cache vector lengths #1511
Merged
sugar expressions give wrong results under aliasing, length mismatch, and captured temporaries #1512
Closed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@eddelbuettel eddelbuettel eddelbuettel approved these changes

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

4 participants

@kevinushey @eddelbuettel @Enchufa2 @bbolker
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.