From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on inbox.vuxu.org X-Spam-Level: X-Spam-Status: No, score=-3.1 required=5.0 tests=DKIM_INVALID,DKIM_SIGNED, MAILING_LIST_MULTI,RCVD_IN_DNSWL_MED,RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL autolearn=ham autolearn_force=no version=3.4.4 Received: (qmail 12789 invoked from network); 8 Apr 2021 16:15:29 -0000 Received: from mother.openwall.net (195.42.179.200) by inbox.vuxu.org with ESMTPUTF8; 8 Apr 2021 16:15:29 -0000 Received: (qmail 30706 invoked by uid 550); 8 Apr 2021 16:15:27 -0000 Mailing-List: contact musl-help@lists.openwall.com; run by ezmlm Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-ID: Reply-To: musl@lists.openwall.com Received: (qmail 27665 invoked from network); 8 Apr 2021 16:11:01 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yandex-team.ru; s=default; t=1617898250; bh=dvuTzxM04cfofWW3mZbIFctyOAqWKiCq4oteQhdppkU=; h=Message-ID:Date:Subject:To:From; b=ZTvyMDxbMLFzF4d9Jha2BhDGmDEhL5GElRmFZRR1nFLBmT57Fa2WK1SB+3CY9jrej 7XEY1j/ln1sl9l+zKMNrhjImKx6z82E0bZeBM39y6/dNCFSb+PLzTx8tbOsXr5OmN6 j2Qojyocq87Zp4q4fSF08ByKYGaur2+DMoSfubhc= Authentication-Results: sas1-ec30c78b6c5b.qloud-c.yandex.net; dkim=pass header.i=@yandex-team.ru From: "Andrey Bugaevskiy" To: Date: Thu, 8 Apr 2021 19:10:49 +0300 Message-ID: <00bc01d72c91$bdedb030$39c91090$@yandex-team.ru> MIME-Version: 1.0 Content-Type: text/plain; charset="koi8-r" Content-Transfer-Encoding: quoted-printable X-Mailer: Microsoft Outlook 16.0 Thread-Index: AdcskXLQIRwZxL12Q5GPHeQWBb3RKQ== Content-Language: ru Subject: [musl] errno and swapcontext in a multithreaded setup Hi, 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. Namely, this code: int test(ucontext_t* old_ctx, const ucontext_t* new_ctx) { int err_before =3D errno; swapcontext(old_ctx, new_ctx); int err_after =3D errno; return err_before | err_after; // do not optimize out } translates with -O1 to something like this: 0000000000001109 : 1109: endbr64 110d: push %r13 110f: push %r12 1111: push %rbp 1112: push %rbx 1113: sub $0x8,%rsp 1117: mov %rdi,%r12 111a: mov %rsi,%r13 111d: callq 1030 <__errno_location@plt> 1122: mov %rax,%rbx 1125: mov (%rax),%ebp 1127: mov %r13,%rsi 112a: mov %r12,%rdi 112d: callq 1020 1132: mov %ebp,%eax 1134: or (%rbx),%eax 1136: add $0x8,%rsp 113a: pop %rbx 113b: pop %rbp 113c: pop %r12 113e: pop %r13 1140: retq errno location is being stored to a register and then reused. However a call to __errno_location after swapcontext is expected to return a different address if the context have been swapped back into another thread. There are a couple of similarly affected functions (pthread_self, __h_errno_location). Removing __attribute__((const)) or changing it to __attribute__((pure)) resolves the problem in newly compiled code. Can this change be considered for the future versions of musl? --=9A Andrey Bugaevskiy