public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
From: Rogerio Alves <rcardoso@linux.vnet.ibm.com>
To: Alexander Monakov <amonakov@ispras.ru>
Cc: libc-alpha@sourceware.org
Subject: Re: [RFC] powerpc: restore TOC when static longjmp to shared object
Date: Mon, 04 Jun 2018 14:34:00 -0000	[thread overview]
Message-ID: <4de7760b-04a9-855c-34ac-1565c5532fb9@linux.vnet.ibm.com> (raw)
In-Reply-To: <2b5a4221-f766-0f57-1aa5-94991c4dfa8e@linux.vnet.ibm.com>

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

This is v3 of patch.  As pointed out by Alexander Monakov we can't rely 
on  alloca/memset since gcc can optimize this out or it can save the 
registers on the stack and the test will no longer make sense.

Now I use small asm code to deliberately overwrite TOC area on caller 
frame with zero. If the TOC is not restored by longjmp the test will 
fail and them I removed alloca/memset since it's no longer necessary for 
the test itself.

Em 25-05-2018 16:39, Rogerio Alves escreveu:
> 
> 
> Em 23-05-2018 11:24, Alexander Monakov escreveu:
>> On Wed, 23 May 2018, Rogerio Alves wrote:
>>> I don't think that alloca/memset should destroy the caller stack like is
>>> happening but, also I think we have to restore TOC to the caller 
>>> frame after
>>> the longjmp in that case also. I don't know if there's any other more 
>>> direct
>>> and robust ways to checking if TOC is correctly restored. I can't 
>>> think in
>>> anything easier than always restore.
>>
>> I'm not challenging the idea that "always restoring" is an appropriate 
>> fix.
>> My question was about *verifying* that TOC register is restored as it 
>> ought
>> to, i.e. how the testcase needs to work. Right now, the test uses my code
>> from Bugzilla that worked okay for demonstration purposes, but has 
>> issues as
>> a long-term testsuite addition:
>>
>> * it demonstrates the issue in a very intransparent fashion, relying on
>>    non-obvious interaction with alloca-memset part of the test;
>>
>> * when GCC manages to optimize out the alloca-memset part, the test
>>    will cease to work for the intended purpose.
>>
>> Alexander
>>
> 
> Ok. I understand your concern. Let me see if I can change this test to 
> check if the TOC was been restored.
> 
> Rogerio

[-- Attachment #2: 0001-powerpc-Always-restore-TOC-on-longjmp.patch --]
[-- Type: text/x-patch, Size: 7054 bytes --]

From 0b59a0e92f56901a3512069441d46778fc97c719 Mon Sep 17 00:00:00 2001
From: Rogerio Alves <rcardoso@linux.vnet.ibm.com>
Date: Wed, 16 May 2018 14:20:53 -0500
Subject: [PATCH v3] powerpc: Always restore TOC on longjmp.

This patch change longjmp to always restore the TOC pointer (r2 register)
to the caller frame on powerpc. This is related to bug 21895[1] that reports
a situation where you have a static longjmp to a shared object file.

[1] https://sourceware.org/bugzilla/show_bug.cgi?id=21895

2018-05-16 Rogerio A. Cardoso <rcardoso@linux.vnet.ibm.com>

	*sysdeps/powerpc/powerpc64/__longjmp-common.S: Remove condition code for
	restore r2 on longjmp.
	*setjmp/Makefile: Added tst-setjmp-bug21895-static to test list.
	Added rules to build test tst-setjmp-bug21895-static
	Added module setjmp-bug21895 and rules to build a shared object from it.
	*setjmp/setjmp-bug21895.c: New test file.
	*setjmp/tst-setjmp-bug21895-static.c: New test file.
---
 Changes in v3: as pointed out by Alexander Monakov we can't rely on
alloca/memset since gcc can optimize this out or it can save the
registers on the stack and the test will no longer make sense. Now I use
a small asm code to deliberately overwrite TOC area on caller frame with
zero. If the TOC is not restored by longjmp the test will fail.

 Changes in v2: Per Adhemerval Zanella. Fix test fail using the suggestions
given. Fix changelog. Fix copyright and indentation for new tests. Change
tests to use libsupport instead old test-skeleton. Fix a extra space
in  __longjmp-common.

 setjmp/Makefile                              | 16 +++++-
 setjmp/setjmp-bug21895.c                     | 41 +++++++++++++++
 setjmp/tst-setjmp-bug21895-static.c          | 75 ++++++++++++++++++++++++++++
 sysdeps/powerpc/powerpc64/__longjmp-common.S |  5 +-
 4 files changed, 132 insertions(+), 5 deletions(-)
 create mode 100644 setjmp/setjmp-bug21895.c
 create mode 100644 setjmp/tst-setjmp-bug21895-static.c

diff --git a/setjmp/Makefile b/setjmp/Makefile
index dc2fcc6..d70daa2 100644
--- a/setjmp/Makefile
+++ b/setjmp/Makefile
@@ -29,9 +29,23 @@ routines	:= setjmp sigjmp bsd-setjmp bsd-_setjmp \
 
 tests		:= tst-setjmp jmpbug bug269-setjmp tst-setjmp-fp \
 		   tst-sigsetjmp tst-setjmp-static
-tests-static	:= tst-setjmp-static
 
+tests-static	:= tst-setjmp-static tst-setjmp-bug21895-static
+
+modules-names = setjmp-bug21895
 
 include ../Rules
 
 $(objpfx)tst-setjmp-fp: $(libm)
+
+test-modules = $(addprefix $(objpfx),$(addsuffix .so,$(modules-names)))
+
+ifeq ($(build-shared),yes)
+tests: $(test-modules)
+endif
+
+$(objpfx)tst-setjmp-bug21895-static: $(common-objpfx)dlfcn/libdl.a
+$(objpfx)tst-setjmp-bug21895-static.out: $(objpfx)setjmp-bug21895.so
+
+tst-setjmp-bug21895-static-ENV = \
+	LD_LIBRARY_PATH=$(objpfx):$(common-objpfx):$(common-objpfx)setjmp:$(common-objpfx)elf
diff --git a/setjmp/setjmp-bug21895.c b/setjmp/setjmp-bug21895.c
new file mode 100644
index 0000000..d7c0133
--- /dev/null
+++ b/setjmp/setjmp-bug21895.c
@@ -0,0 +1,41 @@
+/* Shared object part of test for setjmp interoperability with static
+   dlopen BZ #21895.
+   Copyright (C) 2017-2018 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/>.  */
+
+#include <alloca.h>
+#include <string.h>
+#include <setjmp.h>
+
+jmp_buf jb;
+void (*bar)(jmp_buf);
+
+void
+lbar (void)
+{
+  bar(jb);
+  for(;;);
+}
+
+void
+foo (void)
+{
+  setjmp(jb);
+  lbar();
+
+  for(;;);
+}
diff --git a/setjmp/tst-setjmp-bug21895-static.c b/setjmp/tst-setjmp-bug21895-static.c
new file mode 100644
index 0000000..6ab5340
--- /dev/null
+++ b/setjmp/tst-setjmp-bug21895-static.c
@@ -0,0 +1,75 @@
+/* Test setjmp interoperability with static dlopen BZ #21895.
+   Copyright (C) 2017-2018 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/>.  */
+
+#include <setjmp.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <dlfcn.h>
+
+static void
+bar (jmp_buf jb)
+{
+  static int i;
+  if (i++==1)
+    exit(0);	/* Success.  */
+
+  /* Destroy TOC area on caller frame.  */
+  asm volatile(
+    "li 14, 0\n\t"
+    "std 14, 24(%0)"
+    :
+    : "r" (jb->__jmpbuf[20])
+  );
+
+  longjmp(jb, i);
+}
+
+static int
+do_test (void)
+{
+  void *h = dlopen("setjmp-bug21895.so", RTLD_NOW);
+  if (!h)
+    {
+      puts(dlerror());
+      return 1;
+    }
+
+  void (*pfoo)(void) = dlsym(h, "foo");
+  if (!pfoo)
+    {
+      puts(dlerror());
+      return 1;
+    }
+
+  void (**ppbar)(jmp_buf) = dlsym(h, "bar");
+  if (!ppbar)
+    {
+      puts(dlerror());
+      return 1;
+    }
+
+  *ppbar = bar;
+  pfoo();
+
+  for(;;);
+}
+
+/* Make sure the test will not stuck if jmp fails and fall into one of
+   for(;;).  */
+#define TIMEOUT 100
+#include <support/test-driver.c>
diff --git a/sysdeps/powerpc/powerpc64/__longjmp-common.S b/sysdeps/powerpc/powerpc64/__longjmp-common.S
index 0e10b8d..99c17c5 100644
--- a/sysdeps/powerpc/powerpc64/__longjmp-common.S
+++ b/sysdeps/powerpc/powerpc64/__longjmp-common.S
@@ -130,9 +130,6 @@ L(no_vmx):
 	ld r0,(JB_LR*8)(r3)
 	ld r14,((JB_GPRS+0)*8)(r3)
 	lfd fp14,((JB_FPRS+0)*8)(r3)
-#if defined SHARED && !IS_IN (rtld)
-	std r2,FRAME_TOC_SAVE(r1)	/* Restore the callers TOC save area.  */
-#endif
 	ld r15,((JB_GPRS+1)*8)(r3)
 	lfd fp15,((JB_FPRS+1)*8)(r3)
 	ld r16,((JB_GPRS+2)*8)(r3)
@@ -152,7 +149,7 @@ L(no_vmx):
 	second argument (-4@4), and target address (8@0), respectively.  */
 	LIBC_PROBE (longjmp, 3, 8@3, -4@4, 8@0)
 	mtlr r0
-/* 	std r2,FRAME_TOC_SAVE(r1)	Restore the TOC save area.  */
+	std r2,FRAME_TOC_SAVE(r1)	/* Restore the TOC save area.  */
 	ld r21,((JB_GPRS+7)*8)(r3)
 	lfd fp21,((JB_FPRS+7)*8)(r3)
 	ld r22,((JB_GPRS+8)*8)(r3)
-- 
2.7.4


  reply	other threads:[~2018-06-04 14:34 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <f88767a0-1a38-564d-7bd8-0b5db5bc04df@linux.vnet.ibm.com>
2018-05-22 18:41 ` Rogerio Alves
2018-05-23  9:27   ` Alexander Monakov
2018-05-23 12:33     ` Rogerio Alves
2018-05-23 13:45       ` Tulio Magno Quites Machado Filho
2018-05-23 14:25       ` Alexander Monakov
2018-05-25 19:39         ` Rogerio Alves
2018-06-04 14:34           ` Rogerio Alves [this message]
2018-06-04 14:38             ` Florian Weimer
2018-06-21 19:14               ` Rogerio Alves
2018-07-16 19:11                 ` Tulio Magno Quites Machado Filho
2018-05-15 17:29 Rogerio Alves
2018-05-15 18:53 ` Florian Weimer
2018-05-15 19:15   ` Rogerio Alves
2018-05-15 20:49   ` Tulio Magno Quites Machado Filho
2018-05-16 19:36     ` Rogerio Alves
2018-05-16 20:56       ` Adhemerval Zanella

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=4de7760b-04a9-855c-34ac-1565c5532fb9@linux.vnet.ibm.com \
    --to=rcardoso@linux.vnet.ibm.com \
    --cc=amonakov@ispras.ru \
    --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).