From eb9895f1548c8f6e1826095aee3221eaf9ce84c9 Mon Sep 17 00:00:00 2001 From: Rogerio Alves Date: Wed, 16 May 2018 14:20:53 -0500 Subject: [PATCH] [PATCH v1] 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 *sysdeps/powerpc/powerpc64/__longjmp-common.S: Remove condition code for restore r2 on longjmp. *setjmp/Makefile: Include test build directives. *setjmp/setjmp-bug21895.c: new test file. *setjmp/tst-setjmp-bug21895.c: new test file. --- setjmp/Makefile | 18 ++++++-- setjmp/setjmp-bug21895.c | 42 ++++++++++++++++++ setjmp/tst-setjmp-bug21895.c | 65 ++++++++++++++++++++++++++++ sysdeps/powerpc/powerpc64/__longjmp-common.S | 5 +-- 4 files changed, 123 insertions(+), 7 deletions(-) create mode 100644 setjmp/setjmp-bug21895.c create mode 100644 setjmp/tst-setjmp-bug21895.c diff --git a/setjmp/Makefile b/setjmp/Makefile index dc2fcc6..e715ee6 100644 --- a/setjmp/Makefile +++ b/setjmp/Makefile @@ -22,16 +22,28 @@ subdir := setjmp include ../Makeconfig -headers := setjmp.h bits/setjmp.h bits/setjmp2.h +headers := setjmp.h bits/setjmp.h bits/setjmp2.h bits/dlfcn.h dlfcn/dlfcn.h routines := setjmp sigjmp bsd-setjmp bsd-_setjmp \ longjmp __longjmp jmp-unwind tests := tst-setjmp jmpbug bug269-setjmp tst-setjmp-fp \ - tst-sigsetjmp tst-setjmp-static + tst-sigsetjmp tst-setjmp-static tst-setjmp-bug21895 + tests-static := tst-setjmp-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)setjmp-bug21895.so: $(libdl) +$(objpfx)tst-setjmp-bug21895: $(libdl) +$(objpfx)tst-setjmp-bug21895.out: $(objpfx)setjmp-bug21895.so + +$(objpfx)ts-tsetjmp-fp: $(libm) diff --git a/setjmp/setjmp-bug21895.c b/setjmp/setjmp-bug21895.c new file mode 100644 index 0000000..d6f5516 --- /dev/null +++ b/setjmp/setjmp-bug21895.c @@ -0,0 +1,42 @@ +/* Copyright (C) 2013-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 + . */ + +/* Test that setjmp/longjmp interoperability with static dlopen. + Bugzila #21895. */ +#include +#include +#include + +jmp_buf jb; +void (*bar)(jmp_buf); + +void +lbar (int i, ...) +{ + bar(jb); + for(;;); +} + +void +foo (void) +{ + int i = setjmp(jb); + char *c = alloca(256); + memset(c, 0, 256); + lbar(i); + for(;;); +} diff --git a/setjmp/tst-setjmp-bug21895.c b/setjmp/tst-setjmp-bug21895.c new file mode 100644 index 0000000..5333494 --- /dev/null +++ b/setjmp/tst-setjmp-bug21895.c @@ -0,0 +1,65 @@ +/* Copyright (C) 2013-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 + . */ + +/* Test that setjmp/longjmp interoperability with static dlopen. + Bugzila #21895. */ + +#include +#include +#include +#include + +static void +bar (jmp_buf jb) +{ + static int i; + if (i++==1) exit(0); + longjmp(jb, i); +} + +static int +do_test (void) +{ + void *h = dlopen("./setjmp-bug21895.so", RTLD_NOW); + if (!h) { + puts ("FAIL: "); + puts (dlerror()); + return 1; + } + + void (*pfoo)(void) = dlsym(h, "foo"); + if (!pfoo) { + puts ("FAIL: "); + puts (dlerror()); + return 1; + } + + void (**ppbar)(jmp_buf) = dlsym(h, "bar"); + if (!ppbar) { + puts ("FAIL: "); + puts (dlerror()); + return 1; + } + + *ppbar = bar; + pfoo(); + + for(;;); +} + +#define TEST_FUNCTION do_test () +#include "../test-skeleton.c" diff --git a/sysdeps/powerpc/powerpc64/__longjmp-common.S b/sysdeps/powerpc/powerpc64/__longjmp-common.S index 0e10b8d..a5973c9 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