-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
detecting problems with zero-length vectors? #4
Comments
a relevant part of writing R extensions is https://cran.r-project.org/doc/manuals/r-release/R-exts.html#Transient-storage-allocation |
zero length malloc https://wiki.sei.cmu.edu/confluence/display/c/MEM04-C.+Beware+of+zero-length+allocations |
Writing R extensions https://cran.r-project.org/doc/manuals/r-release/R-exts.html#Using-valgrind says |
this program which uses new/delete
saved in one_past_end.c, compiled via
causes an invalid read when run through valgrind
and when I use the same function via Rcpp, I do get an invalid read from valgrind. there are lots of false positive messages but the important one is
|
same thing for malloc.
R gives me an invalid read at
|
this function uses IntegerVector(0) instead of malloc(0)
for read_memory(1) there is no invalid read from valgrind.
|
Simon Urbanek says that valgrind can NOT detect these issues because of the way R treats zero length vectors. "Rcpp simply calls allocVector() so regular R rules apply. R's SEXP can hold vectors up to length 1 inside without additional allocations*, therefore from memory management perspective writes to the first element of a 0-length vector are not invalid. The valgrind instrumentation of R doesn't guard against that case, i.e., it doesn't mark those 8 bytes as NOACCESS, it only marks additional allocated memory accordingly (not relevant in this case).
Dirk says that he would accept a PR which adds range checking so that would probably
There is a very cute and less-than-a-dozen-lines class Vec in Stroustrup's "A |
hi can you please keep this open until the issue is fixed? (it's not fixed yet, right?) |
The issue can be resolved only by creating a PR for range checking as Dirk suggested. @tdhock Should we keep this open till then? |
yes please keep it open as a reminder that it has not yet been fixed. |
in binsegRcpp there is an argument
IntegerVector max_segments
which could have zero length and cause problems. how can we detect that using RcppDeepState and tell the user to change that?here is the related rcpp-devel thread http://lists.r-forge.r-project.org/pipermail/rcpp-devel/2020-June/010465.html
from R internals
1.13.1 Internals of RallocThe memory used byR_allocis allocated as R vectors, of typeRAWSXP. Thus the allocationis in units of 8 bytes, and is rounded up. A request for zero bytes currently returnsNULL(butthis should not be relied on). For historical reasons, in all other cases 1 byte is added beforerounding up so the allocation is always 1–8 bytes more than was asked for: again this shouldnot be relied on.The vectors allocated are protected via the setting ofR_VStack, as the garbage collectormarks everything that can be reached from that location. When a vector isR_allocated, itsATTRIBpointer is set to the currentR_VStack, andR_VStackis set to the latest allocation.ThusR_VStackis a single-linked chain of the vectors currently allocated viaR_alloc. Functionvmaxsetresets the locationR_VStack, and should be to a value that has previously be obtainedviavmaxget: allocations after the value was obtained will no longer be protected and henceavailable for garbage collection.
The text was updated successfully, but these errors were encountered: