Skip to content
Prev 62981 / 63424 Next

Check for protection

On 4/11/25 17:39, Duncan Murdoch wrote:
I've experimented with some things like that in the past and concluded 
they were not that useful.

Learning that a value is not protected at certain point in the program 
doesn't necessarily mean this is a bug - it depends whether that value 
will be exposed to a possible garbage collection. It is perfectly fine 
that an unprotected value is returned from a C function (and this is how 
it should be). It is fine when an unprotected value exists before it is 
passed to say SET_VECTOR_ELT().

So, right, one might ask if a specific value would be later exposed to a 
garbage collection unprotected (leaving to the tool when such collection 
would happen). But then, it may be ok, because when such a garbage 
collection happens, it would be clear the value cannot be used anymore. 
It only matters if such a value is then being used.

And then: a value may be protected by coincidence, by something that is 
not safe to rely on. Such as the example of the caching of a value in a 
global variable: when we ask whether it is protected, it may be that it 
happens to be protected by some inconsequential call on the stack, but 
we should not rely on that.

We have gc torture with the strict barrier checking, which allows to 
detect use of a value that has been in fact garbage collected. Also, one 
can use the strict barrier checking and manually place calls to gc at 
certain points of interest (though, the danger is one places it where it 
actually cannot happen). These runtime solutions can't find all possible 
problems nor would they tell one what should actually be protected where.

And we have rchk, a static analysis tool, which can direct one close to 
where the problems occur, and works based on the rules how protection 
should be done. It is faster, but, it will have false alarms.

The rules for how to protect objects in Writing R Extensions should be 
quite clear and easy to follow, and certainly it is fine and appropriate 
to ask for help on this list given a small C example. I think the bigger 
problem is when one knows the rules, tries to follow them, but simply 
forgets/makes a mistake at some point. And for that, we have the 
checking tools mentioned. UBSAN also sometimes can spot some of these 
problems.

Best
Tomas