Patchwork tcg: Reload local variables after return from longjmp

login
register
mail settings
Submitter Blue Swirl
Date July 2, 2011, 9:08 a.m.
Message ID <CAAu8pHuwnWf8njsyZEyTdK9zBoOYaHd7XJhn_6McVUPvDu7Hog@mail.gmail.com>
Download mbox | patch
Permalink /patch/102986/
State New
Headers show

Comments

Blue Swirl - July 2, 2011, 9:08 a.m.
On Sat, Jul 2, 2011 at 10:50 AM, Jan Kiszka <jan.kiszka@web.de> wrote:
> From: Jan Kiszka <jan.kiszka@siemens.com>
>
> Recent compilers look deep into cpu_exec, find longjmp as a noreturn
> function and decide to smash some stack variables as they won't be used
> again. This may lead to env becoming invalid after return from setjmp,
> causing crashes. Fix it by reloading env from cpu_single_env in that
> case.

Nice. Could you try if gcc flag -Wclobbered catches something using
your compiler without your patch:

commit f826f0d0f5cf5dd18a0d34159c1a3bc8f2e6ddf4
Author: Blue Swirl <blauwirbel@gmail.com>
Date:   Sun Sep 26 11:58:38 2010 +0000

    Add gcc warning -Wclobbered

    Signed-off-by: Blue Swirl <blauwirbel@gmail.com>



> Signed-off-by: Jan Kiszka <jan.kiszka@siemens.com>
> ---
>  cpu-exec.c |    4 ++++
>  1 files changed, 4 insertions(+), 0 deletions(-)
>
> diff --git a/cpu-exec.c b/cpu-exec.c
> index 20e3ec4..de0d716 100644
> --- a/cpu-exec.c
> +++ b/cpu-exec.c
> @@ -587,6 +587,10 @@ int cpu_exec(CPUState *env)
>                 /* reset soft MMU for next block (it can currently
>                    only be set by a memory fault) */
>             } /* for(;;) */
> +        } else {
> +            /* Reload env after longjmp - the compiler may have smashed all
> +             * local variables as longjmp is marked 'noreturn'. */
> +            env = cpu_single_env;
>         }
>     } /* for(;;) */
>
>

Patch

diff --git a/configure b/configure
index 88159ac..2417205 100755
--- a/configure
+++ b/configure
@@ -1038,7 +1038,7 @@  fi
 gcc_flags="-Wold-style-declaration -Wold-style-definition -Wtype-limits"
 gcc_flags="-Wformat-security -Wformat-y2k -Winit-self
-Wignored-qualifiers $gcc_flags"
 gcc_flags="-Wmissing-include-dirs -Wempty-body -Wnested-externs $gcc_flags"
-gcc_flags="-fstack-protector-all -Wendif-labels $gcc_flags"
+gcc_flags="-fstack-protector-all -Wendif-labels -Wclobbered $gcc_flags"
 cat > $TMPC << EOF
 int main(void) { return 0; }
 EOF