public inbox for libc-help@sourceware.org
 help / color / mirror / Atom feed
From: Tadeus Prastowo <0x66726565@gmail.com>
To: Adhemerval Zanella <adhemerval.zanella@linaro.org>
Cc: "libc-help@sourceware.org" <libc-help@sourceware.org>
Subject: Re: raise() marked __leaf__ is not C-compliant?
Date: Wed, 28 Oct 2020 06:47:47 +0100	[thread overview]
Message-ID: <CAA1YtmsFLhaH5Bc5Uru+RoL2DjAZgdwecY3paM7x=q_vAAAerA@mail.gmail.com> (raw)
In-Reply-To: <25b5791b-5368-7a78-f80d-5ceb2b618d72@linaro.org>

[-- Attachment #1: Type: text/plain, Size: 2988 bytes --]

On Tue, Oct 27, 2020 at 7:50 PM Adhemerval Zanella
<adhemerval.zanella@linaro.org> wrote:
>
> On 27/10/2020 13:57, Tadeus Prastowo via Libc-help wrote:
> >
> > 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').

However, the compiler cannot assume that the variable `terminated'
won't be modified outside the loop if the loop calls a function whose
definition the compiler does not see (e.g., the raise() function
defined by glibc).  Otherwise, the compiler is wrong if the loop calls
a function defined in another translation unit because the function
may modify `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

Thank you for the reference.

I have studied it but found no pointer to parts of the C standard that
says that it is an undefined behavior to call a signal handler
synchronously using raise() on a normal execution path (i.e., not from
within another signal handler, which certainly is undefined behavior
as noted in "undefined behavior 131").

I know that the safest thing to do is always to qualify such a
variable with `volatile', but I think it is valid to not do so for the
cases that are not stated as
undefined/unspecified/implementation-defined behavior by the C
standard.  That is why I ask for a pointer to parts of the C standard
if indeed calling a signal handler synchronously using raise() on a
normal execution path is an undefined behavior.

After further thought, I found out that the problem is not glibc-2.30
marking raise() with __leaf__ but the file-scope limitation of
`terminated'.  As I already mentioned, since `terminated' has a
file-scope, the compiler can assume that `terminated' will not be
modified by a function defined in another translation unit.  And
hence, the compiler is correct in optimizing out the `!terminated'.
Once `terminated' has a global scope, the compiler correctly does not
optimize it out as exemplified by the attached program.  So, marking
raise() with __leaf__ does not make raise() non-compliant with the C
standard.

Thank you for answering my question.

-- 
Best regards,
Tadeus

[-- Attachment #2: raise.c --]
[-- Type: text/x-csrc, Size: 945 bytes --]

/* Compile this as follows:
 *   gcc -O0 -o raise raise.c
 * The C standard on signal() guarantees that the execution of raise will
 * terminate.
 *
 * Compile this as follows:
 *   gcc -Os -o raise raise.c
 * The C standard on signal() guarantees that the execution of raise will
 * terminate.
 *
 * Compile this as follows:
 *   gcc -O2 -o raise raise.c
 * The C standard on signal() guarantees that the execution of raise will
 * terminate.
 */

#include <signal.h>
#include <stdlib.h>
#include <time.h>

int terminated;

static void handler(int signo)
{
  terminated = 1;
}

int main(int argc, char **argv) {
  unsigned total = 0, delta = drand48() >= argc ? 1 : 0;
  /* At runtime, delta will be 0 as argc is one */

  signal(SIGUSR1, handler);

  /* This is an infinite loop unless terminated is 1 as delta is surely 0 */
  for (int i = 0; !terminated && i < 100; i += delta) {
    total += i;
    raise(SIGUSR1);
  }
  
  return total;
}

  reply	other threads:[~2020-10-28  5:47 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-10-27 16:57 Tadeus Prastowo
2020-10-27 18:50 ` Adhemerval Zanella
2020-10-28  5:47   ` Tadeus Prastowo [this message]
2020-10-28  6:13     ` Tadeus Prastowo
2020-10-28  7:33       ` Tadeus Prastowo
2020-10-28 11:53         ` Adhemerval Zanella
2020-10-28 13:19           ` Tadeus Prastowo
2020-10-28 17:34             ` Adhemerval Zanella
2020-10-28 19:23               ` Tadeus Prastowo
2020-10-28 20:17                 ` Adhemerval Zanella
2020-10-29  7:50                   ` Tadeus Prastowo
2020-10-28  8:21 ` Florian Weimer
2020-10-28 12:58   ` Tadeus Prastowo
2020-10-28 17:16     ` Tadeus Prastowo

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CAA1YtmsFLhaH5Bc5Uru+RoL2DjAZgdwecY3paM7x=q_vAAAerA@mail.gmail.com' \
    --to=0x66726565@gmail.com \
    --cc=adhemerval.zanella@linaro.org \
    --cc=libc-help@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).