raise() marked __leaf__ is not C-compliant?

Adhemerval Zanella adhemerval.zanella@linaro.org
Tue Oct 27 18:50:27 GMT 2020



On 27/10/2020 13:57, Tadeus Prastowo via Libc-help wrote:
> Hello,
> 
> To quote C18 on the signal() function
> (https://web.archive.org/web/20181230041359/http://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf),
> Section 7.14.1.1 Paragraph 5:
> 
> If the signal occurs other than as the result of calling the abort or
> raise function, the behavior is undefined if the signal handler refers
> to any object with static or thread storage duration that is not a
> lock-free atomic object other than by assigning a value to an object
> declared as volatile sig_atomic_t
> 
> End quote.
> 
> My understanding is that if my single-threaded program installs a
> signal handler using signal() and the handler is executed as a result
> of calling raise(), then the handler has a defined behavior when the
> handler accesses any object with static storage duration even though
> the object is not qualified using volatile and not of type
> sig_atomic_t.
> 
> If my understanding is incorrect, I would like to have some pointer to
> parts of the C standard that say so.

Unfortunately this is not fully correct, specially if you accessing the 
object outside the signal handler.  As you have noticed in your example, 
compiler can assume the variable 'terminated' won't be modified outside 
the function and apply optimization that avoid read its value from 
memory (most likely GCC will optimize out the '!terminated').

One of the best didactic explanation I have for this is from [1].

[1] https://wiki.sei.cmu.edu/confluence/display/c/SIG31-C.+Do+not+access+shared+objects+in+signal+handlers


More information about the Libc-help mailing list