public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
From: "H.J. Lu" <hjl.tools@gmail.com>
To: Florian Weimer <fweimer@redhat.com>
Cc: libc-alpha@sourceware.org
Subject: Re: [PATCH] x86-64: Stack alignment in _dl_tlsdesc_dynamic and red zone usage (bug 31501)
Date: Sat, 16 Mar 2024 09:32:41 -0700	[thread overview]
Message-ID: <CAMe9rOq94uz7ysG=CKaFsbzOm6+cWOwMemAQjgVH5_rxNGM6vQ@mail.gmail.com> (raw)
In-Reply-To: <CAMe9rOpLsXDucdC2zXa07W8s93F6yby-XPJdhc8WS7SJdq-A3Q@mail.gmail.com>

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

On Sat, Mar 16, 2024 at 8:18 AM H.J. Lu <hjl.tools@gmail.com> wrote:
>
> On Sat, Mar 16, 2024 at 8:04 AM Florian Weimer <fweimer@redhat.com> wrote:
> >
> > * H. J. Lu:
> >
> > > On Sat, Mar 16, 2024 at 7:43 AM H.J. Lu <hjl.tools@gmail.com> wrote:
> > >>
> > >> On Sat, Mar 16, 2024 at 7:37 AM H.J. Lu <hjl.tools@gmail.com> wrote:
> > >> >
> > >> > On Sat, Mar 16, 2024 at 7:33 AM Florian Weimer <fweimer@redhat.com> wrote:
> > >> > >
> > >> > > In sysdeps/x86_64/dl-tlsdesc-dynamic.h, the stack pointer is
> > >> > > realigned for some variants (notably _dl_tlsdesc_dynamic_xsavec).
> > >> > > This realignment does not take into account that the function has
> > >> > > already used part of the red zone at this point, thus clobbering
> > >> > > the initally saved register values located there if the stack
> > >> > > alignment inherited from the caller is unfortunate.
> > >> > >
> > >> > > (Note: I do not know to write a good test case for this in the existing
> > >> > > framework.  We saw this as a random LTO plugin crash when building GCC
> > >> > > with -mtls-dialect=gnu2.  The existing tests on pass on x86_64-linux-gnu
> > >> > > with this change here.)
> > >
> > > We should try to find a testcase.  Can you provide a backtrace when it
> > > happens?  It should be possible to write a testcase with the backtrace.
> >
> > In my reproducer, when %rbx is about to be clobbered, I see
> > (%rsp % 64) == 8 at the start of _dl_tlsdesc_dynamic_xsavec.
> >
> > The %rbx register does not get clobbered if (%rsp % 64) == 56.
> >
> > Does this help?
> >
>
> Yes.  I am working on a testcase.

Hi Florian,

Please verify if this is the right testcase.

Thanks.

-- 
H.J.

[-- Attachment #2: 0001-Add-a-test-for-BZ-31501.patch --]
[-- Type: text/x-patch, Size: 8698 bytes --]

From 8c167bd4d9faddf9580f55182542e8a05a25fd61 Mon Sep 17 00:00:00 2001
From: "H.J. Lu" <hjl.tools@gmail.com>
Date: Sat, 16 Mar 2024 08:42:27 -0700
Subject: [PATCH] Add a test for BZ #31501

---
 sysdeps/x86_64/Makefile                    | 16 +++++
 sysdeps/x86_64/tst-gnu2-tls2-x86-64-mod0.S | 57 +++++++++++++++++
 sysdeps/x86_64/tst-gnu2-tls2-x86-64-mod1.S | 74 ++++++++++++++++++++++
 sysdeps/x86_64/tst-gnu2-tls2-x86-64-mod2.S | 57 +++++++++++++++++
 sysdeps/x86_64/tst-gnu2-tls2-x86-64.c      | 21 ++++++
 5 files changed, 225 insertions(+)
 create mode 100644 sysdeps/x86_64/tst-gnu2-tls2-x86-64-mod0.S
 create mode 100644 sysdeps/x86_64/tst-gnu2-tls2-x86-64-mod1.S
 create mode 100644 sysdeps/x86_64/tst-gnu2-tls2-x86-64-mod2.S
 create mode 100644 sysdeps/x86_64/tst-gnu2-tls2-x86-64.c

diff --git a/sysdeps/x86_64/Makefile b/sysdeps/x86_64/Makefile
index 66b21954f3..f020773e56 100644
--- a/sysdeps/x86_64/Makefile
+++ b/sysdeps/x86_64/Makefile
@@ -217,6 +217,22 @@ valgrind-suppressions-tst-valgrind-smoke = \
   --suppressions=$(..)sysdeps/x86_64/tst-valgrind-smoke.supp
 endif
 
+tests += \
+  tst-gnu2-tls2-x86-64 \
+# tests
+
+modules-names += \
+  tst-gnu2-tls2-x86-64-mod0 \
+  tst-gnu2-tls2-x86-64-mod1 \
+  tst-gnu2-tls2-x86-64-mod2 \
+# modules-names
+
+$(objpfx)tst-gnu2-tls2-x86-64: $(shared-thread-library)
+$(objpfx)tst-gnu2-tls2-x86-64.out: \
+  $(objpfx)tst-gnu2-tls2-x86-64-mod0.so \
+  $(objpfx)tst-gnu2-tls2-x86-64-mod1.so \
+  $(objpfx)tst-gnu2-tls2-x86-64-mod2.so
+
 endif # $(subdir) == elf
 
 ifeq ($(subdir),csu)
diff --git a/sysdeps/x86_64/tst-gnu2-tls2-x86-64-mod0.S b/sysdeps/x86_64/tst-gnu2-tls2-x86-64-mod0.S
new file mode 100644
index 0000000000..8129b28061
--- /dev/null
+++ b/sysdeps/x86_64/tst-gnu2-tls2-x86-64-mod0.S
@@ -0,0 +1,57 @@
+/* Test TLSDESC relocation with x86-64.
+   Copyright (C) 2024 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, see
+   <http://www.gnu.org/licenses/>.  */
+
+	.text
+	.p2align 4
+	.globl	apply_tls
+	.type	apply_tls, @function
+apply_tls:
+	.cfi_startproc
+	subq	$24, %rsp
+	.cfi_def_cfa_offset 32
+	movdqu	(%rdi), %xmm0
+	movq	%fs:40, %rax
+	movq	%rax, 8(%rsp)
+	xorl	%eax, %eax
+	leaq	tls_var0@TLSDESC(%rip), %rax
+	call	*tls_var0@TLSCALL(%rax)
+	addq	%fs:0, %rax
+	movups	%xmm0, (%rax)
+	movdqu	16(%rdi), %xmm1
+	movups	%xmm1, 16(%rax)
+	movq	8(%rsp), %rdx
+	subq	%fs:40, %rdx
+	jne	.L5
+	addq	$24, %rsp
+	.cfi_remember_state
+	.cfi_def_cfa_offset 8
+	ret
+.L5:
+	.cfi_restore_state
+	call	__stack_chk_fail@PLT
+	.cfi_endproc
+	.size	apply_tls, .-apply_tls
+	.hidden	tls_var0
+	.globl	tls_var0
+	.section	.tbss,"awT",@nobits
+	.align 8
+	.type	tls_var0, @object
+	.size	tls_var0, 32
+tls_var0:
+	.zero	32
+	.section	.note.GNU-stack,"",@progbits
diff --git a/sysdeps/x86_64/tst-gnu2-tls2-x86-64-mod1.S b/sysdeps/x86_64/tst-gnu2-tls2-x86-64-mod1.S
new file mode 100644
index 0000000000..af4b7ca761
--- /dev/null
+++ b/sysdeps/x86_64/tst-gnu2-tls2-x86-64-mod1.S
@@ -0,0 +1,74 @@
+/* Test TLSDESC relocation with x86-64.
+   Copyright (C) 2024 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, see
+   <http://www.gnu.org/licenses/>.  */
+
+/* Select an offset which will cause _dl_tlsdesc_dynamic_xsavec to
+   clobber %rbx.  */
+#define OFFSET (56 + 16 + 16 + 16)
+
+	.text
+	.p2align 4
+	.globl	apply_tls
+	.type	apply_tls, @function
+apply_tls:
+	.cfi_startproc
+	pushq	%rbp
+	.cfi_def_cfa_offset 16
+	.cfi_offset 6, -16
+	movq	%rsp, %rbp
+	.cfi_def_cfa_register 6
+	/* Align stack to 64 bytes.  */
+	andq	$-64, %rsp
+	pushq	%rbx
+	subq	$OFFSET, %rsp
+	movdqu	(%rdi), %xmm0
+	movq	%fs:40, %rax
+	movq	%rax, 56(%rsp)
+	xorl	%eax, %eax
+	leaq	tls_var1@TLSDESC(%rip), %rax
+	call	*tls_var1@TLSCALL(%rax)
+	addq	%fs:0, %rax
+	movups	%xmm0, 32(%rax)
+	movdqu	16(%rdi), %xmm1
+	addq	$32, %rax
+	movups	%xmm1, 16(%rax)
+	movq	56(%rsp), %rdx
+	subq	%fs:40, %rdx
+	jne	.L5
+	/* All versions of _dl_tlsdesc_dynamic should preserve %rbx.  */
+	cmpq	OFFSET(%rsp), %rbx
+	jne	.Lhlt
+	leave
+	.cfi_remember_state
+	.cfi_def_cfa 7, 8
+	ret
+.Lhlt:
+	hlt
+.L5:
+	.cfi_restore_state
+	call	__stack_chk_fail@PLT
+	.cfi_endproc
+	.size	apply_tls, .-apply_tls
+	.hidden	tls_var1
+	.globl	tls_var1
+	.section	.tbss,"awT",@nobits
+	.align 16
+	.type	tls_var1, @object
+	.size	tls_var1, 3200
+tls_var1:
+	.zero	3200
+	.section	.note.GNU-stack,"",@progbits
diff --git a/sysdeps/x86_64/tst-gnu2-tls2-x86-64-mod2.S b/sysdeps/x86_64/tst-gnu2-tls2-x86-64-mod2.S
new file mode 100644
index 0000000000..d82f5f029b
--- /dev/null
+++ b/sysdeps/x86_64/tst-gnu2-tls2-x86-64-mod2.S
@@ -0,0 +1,57 @@
+/* Test TLSDESC relocation with x86-64.
+   Copyright (C) 2024 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, see
+   <http://www.gnu.org/licenses/>.  */
+
+	.text
+	.p2align 4
+	.globl	apply_tls
+	.type	apply_tls, @function
+apply_tls:
+	.cfi_startproc
+	subq	$24, %rsp
+	.cfi_def_cfa_offset 32
+	movdqu	(%rdi), %xmm0
+	movq	%fs:40, %rax
+	movq	%rax, 8(%rsp)
+	xorl	%eax, %eax
+	leaq	tls_var2@TLSDESC(%rip), %rax
+	call	*tls_var2@TLSCALL(%rax)
+	addq	%fs:0, %rax
+	movups	%xmm0, (%rax)
+	movdqu	16(%rdi), %xmm1
+	movups	%xmm1, 16(%rax)
+	movq	8(%rsp), %rdx
+	subq	%fs:40, %rdx
+	jne	.L5
+	addq	$24, %rsp
+	.cfi_remember_state
+	.cfi_def_cfa_offset 8
+	ret
+.L5:
+	.cfi_restore_state
+	call	__stack_chk_fail@PLT
+	.cfi_endproc
+	.size	apply_tls, .-apply_tls
+	.hidden	tls_var2
+	.globl	tls_var2
+	.section	.tbss,"awT",@nobits
+	.align 8
+	.type	tls_var2, @object
+	.size	tls_var2, 32
+tls_var2:
+	.zero	32
+	.section	.note.GNU-stack,"",@progbits
diff --git a/sysdeps/x86_64/tst-gnu2-tls2-x86-64.c b/sysdeps/x86_64/tst-gnu2-tls2-x86-64.c
new file mode 100644
index 0000000000..787181545b
--- /dev/null
+++ b/sysdeps/x86_64/tst-gnu2-tls2-x86-64.c
@@ -0,0 +1,21 @@
+/* Test TLSDESC relocation with x86-64.
+   Copyright (C) 2024 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, see
+   <http://www.gnu.org/licenses/>.  */
+
+#define MOD(i) "tst-gnu2-tls2-x86-64-mod" #i ".so"
+
+#include <elf/tst-gnu2-tls2.c>
-- 
2.44.0


  reply	other threads:[~2024-03-16 16:33 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-03-16 14:32 Florian Weimer
2024-03-16 14:37 ` H.J. Lu
2024-03-16 14:43   ` H.J. Lu
2024-03-16 14:47     ` H.J. Lu
2024-03-16 15:04       ` Florian Weimer
2024-03-16 15:18         ` H.J. Lu
2024-03-16 16:32           ` H.J. Lu [this message]
2024-03-16 16:37             ` H.J. Lu
2024-03-16 17:42             ` Florian Weimer
2024-03-16 17:51               ` H.J. Lu
2024-03-16 22:05                 ` H.J. Lu
2024-03-17  1:19                   ` H.J. Lu
2024-03-17  3:14                     ` H.J. Lu
2024-03-16 14:57     ` Florian Weimer
2024-03-16 14:59       ` H.J. Lu

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='CAMe9rOq94uz7ysG=CKaFsbzOm6+cWOwMemAQjgVH5_rxNGM6vQ@mail.gmail.com' \
    --to=hjl.tools@gmail.com \
    --cc=fweimer@redhat.com \
    --cc=libc-alpha@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).