Skip to contentSkip to conversation

cpu-exec: Fix compiler warning (-Werror=clobbered)

PATCH Superseded Stefan Weil 3 participants 9 messages Sep 17, 2013 – Oct 29, 2013

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 cpu-exec: Fix compiler warning (-Werror=clobbered)

'cpu' and 'env' are not modified after sigsetjmp. Therefore they will still have their last value after longjmp restored the stack context.

The code which should "reload" both variables causes a compiler warning:

cpu-exec.c:204:15: error:
variable ‘cpu’ might be clobbered by ‘longjmp’ or ‘vfork’ [-Werror=clobbered] cpu-exec.c:202:28: error:
argument ‘env’ might be clobbered by ‘longjmp’ or ‘vfork’ [-Werror=clobbered]

Remove this unneeded code.

---

Jan,

could you please review this patch which removes code added by you earlier? I have run tests with the old code and assertions to see whether the values were really smashed. They never were, and from the documentation of setjmp I'd not expect that they ever might be.

The patch is needed to fix a compiler warning with -Wextra.

Thanks,
Stefan

 cpu-exec.c |    5 -----
 1 file changed, 5 deletions(-)
cpu-exec.c +0 -5
index 5a43995..fbfb749 100644
@@ -676,11 +676,6 @@ int cpu_exec(CPUArchState *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'. */
-            cpu = current_cpu;
-            env = cpu->env_ptr;
         }
     } /* for(;;) */
 
Signature · 1 line
1.7.10.4
#1 lore raw
Quoted 15 lines · Stefan Weil

'cpu' and 'env' are not modified after sigsetjmp. Therefore they will still have their last value after longjmp restored the stack context.

The code which should "reload" both variables causes a compiler warning:

cpu-exec.c:204:15: error:
variable ‘cpu’ might be clobbered by ‘longjmp’ or ‘vfork’ [-Werror=clobbered] cpu-exec.c:202:28: error:
argument ‘env’ might be clobbered by ‘longjmp’ or ‘vfork’ [-Werror=clobbered]

Remove this unneeded code.

---

Quoted 8 lines

Jan,

could you please review this patch which removes code added by you earlier? I have run tests with the old code and assertions to see whether the values were really smashed. They never were, and from the documentation of setjmp I'd not expect that they ever might be.

The patch is needed to fix a compiler warning with -Wextra.

This used to fix a real, deadly crash. Therefore a reversion can't be trivial by definition. Unfortunately, I don't recall which compiler version and concrete scenario were involved back then.

Anyway - did anything change in the code structure around since then? Does anything ensure that this "optimization" is not longer performed by the compiler?

I'll try to understand the warnings meanwhile.

Jan

Quoted 24 lines

Thanks,
Stefan

 cpu-exec.c |    5 -----
 1 file changed, 5 deletions(-)
cpu-exec.c +0 -5
index 5a43995..fbfb749 100644
@@ -676,11 +676,6 @@ int cpu_exec(CPUArchState *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'. */
-            cpu = current_cpu;
-            env = cpu->env_ptr;
         }
     } /* for(;;) */
 
#2 lore raw

Am 17.09.2013 19:17, schrieb Jan Kiszka:

Quoted 8 lines · Stefan Weil
Quoted 7 lines

'cpu' and 'env' are not modified after sigsetjmp. Therefore they will still have their last value after longjmp restored the stack context.

The code which should "reload" both variables causes a compiler warning:

cpu-exec.c:204:15: error:
variable ‘cpu’ might be clobbered by ‘longjmp’ or ‘vfork’

[-Werror=clobbered]

Quoted 2 lines
Quoted 2 lines

cpu-exec.c:202:28: error:
argument ‘env’ might be clobbered by ‘longjmp’ or ‘vfork’

[-Werror=clobbered]

Quoted 9 lines
Quoted 9 lines

Remove this unneeded code.

---

Jan,

could you please review this patch which removes code added by you

earlier?

Quoted 1 line
Quoted 1 line

I have run tests with the old code and assertions to see whether the

values

Quoted 1 line
Quoted 1 line

were really smashed. They never were, and from the documentation of

setjmp

Quoted 8 lines
Quoted 3 lines

I'd not expect that they ever might be.

The patch is needed to fix a compiler warning with -Wextra.

This used to fix a real, deadly crash. Therefore a reversion can't be trivial by definition. Unfortunately, I don't recall which compiler version and concrete scenario were involved back then.

Quoted 8 lines

Anyway - did anything change in the code structure around since then? Does anything ensure that this "optimization" is not longer performed by the compiler?

I'll try to understand the warnings meanwhile.

Jan

The code changed a lot since that time, e.g. setjmp was replaced by sigsetjmp.

Maybe you had a broken compiler which could be forced to do the right thing by that code?

Stefan

#3 lore raw
In reply to Stefan Weil
Quoted 4 lines · Stefan Weil

could you please review this patch which removes code added by you earlier? I have run tests with the old code and assertions to see whether the values were really smashed. They never were, and from the documentation of setjmp I'd not expect that they ever might be.

We had a discussion about this back in 2011. Any compiler which needs these statements is definitely buggy -- the C standard mandates that they're not needed. Unfortunately Jan never said what compiler he was using; obviously the ones the rest of us were using at the time didn't have the bug.
http://patchwork.ozlabs.org/patch/102980/ has the discussion.

-- PMM

#4 lore raw
Quoted 9 lines · Peter Maydell
Quoted 4 lines · Stefan Weil

could you please review this patch which removes code added by you earlier? I have run tests with the old code and assertions to see whether the values were really smashed. They never were, and from the documentation of setjmp I'd not expect that they ever might be.

We had a discussion about this back in 2011. Any compiler which needs these statements is definitely buggy -- the C standard mandates that they're not needed.

I'm not that sure about this. We have a no-return function involved between setjmp and the actual longjmp. Why should the compiler have to preserve local variables when entering cpu_loop_exit?

Quoted 1 line

Unfortunately Jan never said what compiler he was using;

If you look down the thread: gcc 4.5.0. However, gcc 4.5.1 does not seem to reproduce the issue anymore.

On the other hand, reloading that variable outside the setjmp/longjmp section should not make the compiler worry about clobbering. The warning seems false positive: if you pull the assignment before the setjmp (see below), it's fine, if you move it at the end of the very same loop, my compiler starts to grumble.

cpu-exec.c +4 -5
index 5a43995..3882d74 100644
@@ -266,6 +266,10 @@ int cpu_exec(CPUArchState *env)
 
     /* prepare setjmp context for exception handling */
     for(;;) {
+        /* Reload env after longjmp - the compiler may have smashed all
+         * local variables as cpu_loop_exit is marked 'noreturn'. */
+        cpu = current_cpu;
+        env = cpu->env_ptr;
         if (sigsetjmp(env->jmp_env, 0) == 0) {
             /* if an exception is pending, we execute it here */
             if (env->exception_index >= 0) {
@@ -676,11 +680,6 @@ int cpu_exec(CPUArchState *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'. */
-            cpu = current_cpu;
-            env = cpu->env_ptr;
         }
     } /* for(;;) */
 

Jan

#5 lore raw
Quoted 7 lines · Jan Kiszka
Quoted 6 lines · Peter Maydell
Quoted 4 lines · Stefan Weil

could you please review this patch which removes code added by you earlier? I have run tests with the old code and assertions to see whether the values were really smashed. They never were, and from the documentation of setjmp I'd not expect that they ever might be.

Quoted 7 lines
Quoted 3 lines

We had a discussion about this back in 2011. Any compiler which needs these statements is definitely buggy -- the C standard mandates that they're not needed.

I'm not that sure about this. We have a no-return function involved between setjmp and the actual longjmp. Why should the compiler have to preserve local variables when entering cpu_loop_exit?

Because the C standard specification for setjmp and longjmp says it must (if they are not changed between the setjmp and the longjmp call; locals which are changed need not be preserved). I quoted the relevant section from C99 in the discussion I linked. And gcc's documentation of the 'noreturn' attribute specifically says it does not affect the exceptional path where the function returns via longjmp.

-- PMM

#6 lore raw
Quoted 16 lines · Peter Maydell
Quoted 7 lines · Jan Kiszka
Quoted 6 lines · Peter Maydell
Quoted 4 lines · Stefan Weil

could you please review this patch which removes code added by you earlier? I have run tests with the old code and assertions to see whether the values were really smashed. They never were, and from the documentation of setjmp I'd not expect that they ever might be.

Quoted 7 lines
Quoted 3 lines

We had a discussion about this back in 2011. Any compiler which needs these statements is definitely buggy -- the C standard mandates that they're not needed.

I'm not that sure about this. We have a no-return function involved between setjmp and the actual longjmp. Why should the compiler have to preserve local variables when entering cpu_loop_exit?

Quoted 8 lines

Because the C standard specification for setjmp and longjmp says it must (if they are not changed between the setjmp and the longjmp call; locals which are changed need not be preserved). I quoted the relevant section from C99 in the discussion I linked.

And gcc's documentation of the 'noreturn' attribute specifically says it does not affect the exceptional path where the function returns via longjmp.

OK, that is the clarifying bit of information.

Now the question is if want to drop support for faulty compilers again, work around the false-positive warning, or avoid the issue differently than via reloading.

Jan

#7 lore raw

Am 18.09.2013 09:48, schrieb Jan Kiszka:

Quoted 1 line · Peter Maydell

[...]

Quoted 10 lines
Quoted 3 lines

And gcc's documentation of the 'noreturn' attribute specifically says it does not affect the exceptional path where the function returns via longjmp.

OK, that is the clarifying bit of information.

Now the question is if want to drop support for faulty compilers again, work around the false-positive warning, or avoid the issue differently than via reloading.

Jan

Recently commit 6c78f29a2424622bfc9c30dfbbc13404481eacb6 added a third variable which is reloaded now. Obviously the clang compiler needs this workaround.

Jan, can you remember whether the initial problems were also caused by clang? If yes, we might restrict the code to that compiler. This would avoid the -Wclobbered warnings with newer gcc while still fixing the code generated by clang.

Stefan

#8 lore raw
Quoted 14 lines · Stefan Weil

Am 18.09.2013 09:48, schrieb Jan Kiszka:

Quoted 1 line · Peter Maydell

[...]

Quoted 10 lines
Quoted 3 lines

And gcc's documentation of the 'noreturn' attribute specifically says it does not affect the exceptional path where the function returns via longjmp.

OK, that is the clarifying bit of information.

Now the question is if want to drop support for faulty compilers again, work around the false-positive warning, or avoid the issue differently than via reloading.

Jan

Quoted 8 lines

Recently commit 6c78f29a2424622bfc9c30dfbbc13404481eacb6 added a third variable which is reloaded now. Obviously the clang compiler needs this workaround.

Jan, can you remember whether the initial problems were also caused by clang? If yes, we might restrict the code to that compiler. This would avoid the -Wclobbered warnings with newer gcc while still fixing the code generated by clang.

Look up this thread: gcc 4.5.0

Jan