mailing list of musl libc
 help / color / mirror / code / Atom feed
From: 'Rich Felker' <dalias@libc.org>
To: Andrey Bugaevskiy <bugaevskiy@yandex-team.ru>
Cc: 'Florian Weimer' <fweimer@redhat.com>, musl@lists.openwall.com
Subject: Re: [musl] errno and swapcontext in a multithreaded setup
Date: Fri, 9 Apr 2021 08:15:51 -0400	[thread overview]
Message-ID: <20210409121551.GD2546@brightrain.aerifal.cx> (raw)
In-Reply-To: <00d101d72d39$70639840$512ac8c0$@yandex-team.ru>

On Fri, Apr 09, 2021 at 03:11:14PM +0300, Andrey Bugaevskiy wrote:
> On Thu, 8 Apr 2021, Rich Felker wrote:
> > On Thu, Apr 08, 2021 at 08:46:00PM +0300, Andrey Bugaevskiy wrote:
> > > On Thu, 8 Apr 2021, Florian Weimer wrote:
> > > > * Andrey Bugaevskiy:
> > > > 
> > > > > I'm using makecontext/swapcontext to migrate contexts between
> threads
> > > > > and this sometimes leads to getting incorrect errno values.
> > > > >
> > > > > Investigating further I've noticed that __errno_location
> > > > > is marked __attribute__((const)).
> > > > > This causes optimizers to assume that errno address never changes
> > > > > in the scope of the function which is not the case in my scenario.
> > > > 
> > > > The optimizers make the same assumption for actual thread-local
> > > > variables, not just __errno_location.  Migrating contexts between
> > > > threads results in undefined behavior.
> > > 
> > > Can you please point me to some explanation why this optimization
> > > is valid for thread-local variables?
> > > 
> > > As far as I can imagine optimizer should at least prove that it
> > > won't be changed from some other place or (if the variable is local
> > > to the function) that it is not changed by a recursive call.
> > 
> > Perhaps you're confusing the value of errno with its address. No
> > function can change the address of errno, only its value.
> > Conceptually, __errno_location() is just &errno.
> 
> Well, it's a function returning a value, but I see the point.
> I'm still a bit confused about why optimizer can assume that
> TLS location never changes though.

Because that's a guarantee of the C language. All objects have
addresses that are constant for their lifetime. For example, if *you*
took &errno or &some_tls_var and saved it, the address you saved is
valid until the lifetime of the object ends. It will compare equal to
what you get if you take the address again. (The fact tht it doesn't
necessarily in your ucontext example is a consequence of you having
invoked UB by attempting to "move" an executing block from one thread
to another.)

Rich

  reply	other threads:[~2021-04-09 12:16 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-04-08 16:10 Andrey Bugaevskiy
2021-04-08 17:04 ` Florian Weimer
2021-04-08 17:17   ` Rich Felker
2021-04-08 17:46   ` Andrey Bugaevskiy
2021-04-08 18:55     ` Rich Felker
2021-04-09 12:11       ` Andrey Bugaevskiy
2021-04-09 12:15         ` 'Rich Felker' [this message]
2021-04-08 17:40 ` Ariadne Conill
2021-04-08 17:49   ` Rich Felker

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=20210409121551.GD2546@brightrain.aerifal.cx \
    --to=dalias@libc.org \
    --cc=bugaevskiy@yandex-team.ru \
    --cc=fweimer@redhat.com \
    --cc=musl@lists.openwall.com \
    /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.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/musl/

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 NNTP newsgroup(s).