Skip to contentSkip to conversation

LLVMLinux: Remove warning about returning an uninitialized variable

PATCH Superseded [email protected] 6 participants 19 messages Mar 22, 2014 – Mar 31, 2014 Generic include/asm header files

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 LLVMLinux: Remove warning about returning an uninitialized variable

From: Behan Webster <[email protected]>

Fix uninitialized return code in default case in cmpxchg-local.h

This patch fixes the code to prevent an uninitialized return value that is detected when compiling with clang. The bug produces numerous warnings when compiling the Linux kernel with clang.

---
 include/asm-generic/cmpxchg-local.h | 1 +
 1 file changed, 1 insertion(+)
include/asm-generic/cmpxchg-local.h +1 -0
index d8d4c89..4c41bb8 100644
@@ -41,6 +41,7 @@ static inline unsigned long __cmpxchg_local_generic(volatile void *ptr,
 		break;
 	default:
 		wrong_size_cmpxchg(ptr);
+		prev = 0;
 	}
 	raw_local_irq_restore(flags);
 	return prev;
Signature · 1 line
1.8.3.2
maintainer
#1 lore raw

include/asm-generic/cmpxchg-local.h static inline unsigned long __cmpxchg_local_generic(volatile void *ptr, · line 47

Quoted 12 lines · On Saturday 22 March 2014, [email protected] wrote:
include/asm-generic/cmpxchg-local.h +1 -0
index d8d4c89..4c41bb8 100644
@@ -41,6 +41,7 @@ static inline unsigned long __cmpxchg_local_generic(volatile void *ptr,
                break;
        default:
                wrong_size_cmpxchg(ptr);
+               prev = 0;
        }
        raw_local_irq_restore(flags);
        return prev;

How about using __unreachable() instead to annotate the fact that you won't get here?

Arnd

#2 lore raw
Quoted 16 lines · Arnd Bergmann

include/asm-generic/cmpxchg-local.h static inline unsigned long __cmpxchg_local_generic(volatile void *ptr, · line 47

Quoted 12 lines · On Saturday 22 March 2014, [email protected] wrote:
include/asm-generic/cmpxchg-local.h +1 -0
index d8d4c89..4c41bb8 100644
@@ -41,6 +41,7 @@ static inline unsigned long __cmpxchg_local_generic(volatile void *ptr,
                 break;
         default:
                 wrong_size_cmpxchg(ptr);
+               prev = 0;
         }
         raw_local_irq_restore(flags);
         return prev;

How about using __unreachable() instead to annotate the fact that you won't get here?

Yours is a _much_ better idea. Will fix.

Thanks,

Behan

Signature · 2 lines
Behan Webster
[email protected]
#3 lore raw

PATCH v2 1/1 LLVMLinux: Remove warning about returning an uninitialized variable

In reply to Arnd Bergmann

From: Behan Webster <[email protected]>

Fix uninitialized return code in default case in cmpxchg-local.h

This patch fixes the code to prevent an uninitialized return value that is detected when compiling with clang. The bug produces numerous warnings when compiling the Linux kernel with clang.

---
 include/asm-generic/cmpxchg-local.h | 1 +
 1 file changed, 1 insertion(+)
include/asm-generic/cmpxchg-local.h +1 -0
index d8d4c89..9112111 100644
@@ -41,6 +41,7 @@ static inline unsigned long __cmpxchg_local_generic(volatile void *ptr,
 		break;
 	default:
 		wrong_size_cmpxchg(ptr);
+		__builtin_unreachable();
 	}
 	raw_local_irq_restore(flags);
 	return prev;
Signature · 1 line
1.8.3.2
#4 lore raw
Quoted 19 lines · On Sat, Mar 22, 2014 at 08:48:19AM -0700, [email protected] wrote:

From: Behan Webster <[email protected]>

Fix uninitialized return code in default case in cmpxchg-local.h

This patch fixes the code to prevent an uninitialized return value that is detected when compiling with clang. The bug produces numerous warnings when compiling the Linux kernel with clang.

---
 include/asm-generic/cmpxchg-local.h | 1 +
 1 file changed, 1 insertion(+)
include/asm-generic/cmpxchg-local.h +0 -0
index d8d4c89..9112111 100644

include/asm-generic/cmpxchg-local.h static inline unsigned long __cmpxchg_local_generic(volatile void *ptr, · line 44

Quoted 4 lines
include/asm-generic/cmpxchg-local.h +1 -0
@@ -41,6 +41,7 @@ static inline unsigned long __cmpxchg_local_generic(volatile void *ptr,
 		break;
 	default:
 		wrong_size_cmpxchg(ptr);
+		__builtin_unreachable();

It is unreachable() - see compiler.h

Sam

#7 lore raw
In reply to [email protected]
Quoted 19 lines · On Sat, 2014-03-22 at 08:48 -0700, [email protected] wrote:

From: Behan Webster <[email protected]>

Fix uninitialized return code in default case in cmpxchg-local.h

This patch fixes the code to prevent an uninitialized return value that is detected when compiling with clang. The bug produces numerous warnings when compiling the Linux kernel with clang.

---
 include/asm-generic/cmpxchg-local.h | 1 +
 1 file changed, 1 insertion(+)
include/asm-generic/cmpxchg-local.h +0 -0
index d8d4c89..9112111 100644

include/asm-generic/cmpxchg-local.h static inline unsigned long __cmpxchg_local_generic(volatile void *ptr, · line 44

Quoted 4 lines
include/asm-generic/cmpxchg-local.h +1 -0
@@ -41,6 +41,7 @@ static inline unsigned long __cmpxchg_local_generic(volatile void *ptr,
 		break;
 	default:
 		wrong_size_cmpxchg(ptr);
+		__builtin_unreachable();

No, that's got to be unreachable() so that it works in all compilers, (__builtin_unreachable is a gcc-4 ism).

Got to say this still looks wrong. If wrong_size_cmpxchg() cannot return, the function should be annotated as such with __noreturn (like panic()) so the unreachable() should be superfluous.

James

#5 lore raw
In reply to Sam Ravnborg
Quoted 15 lines · Sam Ravnborg
Quoted 14 lines · On Sat, Mar 22, 2014 at 08:48:19AM -0700, [email protected] wrote:

From: Behan Webster <[email protected]>

Fix uninitialized return code in default case in cmpxchg-local.h

This patch fixes the code to prevent an uninitialized return value that is detected when compiling with clang. The bug produces numerous warnings when compiling the Linux kernel with clang.

---
  include/asm-generic/cmpxchg-local.h | 1 +
  1 file changed, 1 insertion(+)
Quoted 10 lines

include/asm-generic/cmpxchg-local.h static inline unsigned long __cmpxchg_local_generic(volatile void *ptr, · line 44

Quoted 9 lines
include/asm-generic/cmpxchg-local.h +1 -0
index d8d4c89..9112111 100644
@@ -41,6 +41,7 @@ static inline unsigned long __cmpxchg_local_generic(volatile void *ptr,
  		break;
  	default:
  		wrong_size_cmpxchg(ptr);
+		__builtin_unreachable();

It is unreachable() - see compiler.h

Oh I see.

Behan

Signature · 2 lines
Behan Webster
[email protected]
#6 lore raw

PATCH v3 1/1 LLVMLinux: Remove warning about returning an uninitialized variable

In reply to Sam Ravnborg

From: Behan Webster <[email protected]>

Fix uninitialized return code in default case in cmpxchg-local.h

This patch fixes the code to prevent an uninitialized return value that is detected when compiling with clang. The bug produces numerous warnings when compiling the Linux kernel with clang.

---
 include/asm-generic/cmpxchg-local.h | 1 +
 1 file changed, 1 insertion(+)
include/asm-generic/cmpxchg-local.h +1 -0
index d8d4c89..a4f5882 100644
@@ -41,6 +41,7 @@ static inline unsigned long __cmpxchg_local_generic(volatile void *ptr,
 		break;
 	default:
 		wrong_size_cmpxchg(ptr);
+		unreachable();
 	}
 	raw_local_irq_restore(flags);
 	return prev;
Signature · 1 line
1.8.3.2
#8 lore raw
In reply to James Bottomley
Quoted 15 lines · James Bottomley
Quoted 14 lines · On Sat, 2014-03-22 at 08:48 -0700, [email protected] wrote:

From: Behan Webster <[email protected]>

Fix uninitialized return code in default case in cmpxchg-local.h

This patch fixes the code to prevent an uninitialized return value that is detected when compiling with clang. The bug produces numerous warnings when compiling the Linux kernel with clang.

---
  include/asm-generic/cmpxchg-local.h | 1 +
  1 file changed, 1 insertion(+)
Quoted 11 lines

include/asm-generic/cmpxchg-local.h static inline unsigned long __cmpxchg_local_generic(volatile void *ptr, · line 44

Quoted 9 lines
include/asm-generic/cmpxchg-local.h +1 -0
index d8d4c89..9112111 100644
@@ -41,6 +41,7 @@ static inline unsigned long __cmpxchg_local_generic(volatile void *ptr,
  		break;
  	default:
  		wrong_size_cmpxchg(ptr);
+		__builtin_unreachable();

No, that's got to be unreachable() so that it works in all compilers, (__builtin_unreachable is a gcc-4 ism).

It is also supported by clang.

Quoted 3 lines

Got to say this still looks wrong. If wrong_size_cmpxchg() cannot return, the function should be annotated as such with __noreturn (like panic()) so the unreachable() should be superfluous.

Okay. I can try that instead.

Thanks,

Behan

Signature · 2 lines
Behan Webster
[email protected]
#9 lore raw
Quoted 27 lines · Behan Webster
Quoted 15 lines · James Bottomley
Quoted 14 lines · On Sat, 2014-03-22 at 08:48 -0700, [email protected] wrote:

From: Behan Webster <[email protected]>

Fix uninitialized return code in default case in cmpxchg-local.h

This patch fixes the code to prevent an uninitialized return value that is detected when compiling with clang. The bug produces numerous warnings when compiling the Linux kernel with clang.

---
  include/asm-generic/cmpxchg-local.h | 1 +
  1 file changed, 1 insertion(+)
Quoted 11 lines

include/asm-generic/cmpxchg-local.h static inline unsigned long __cmpxchg_local_generic(volatile void *ptr, · line 44

Quoted 9 lines
include/asm-generic/cmpxchg-local.h +1 -0
index d8d4c89..9112111 100644
@@ -41,6 +41,7 @@ static inline unsigned long __cmpxchg_local_generic(volatile void *ptr,
  		break;
  	default:
  		wrong_size_cmpxchg(ptr);
+		__builtin_unreachable();

No, that's got to be unreachable() so that it works in all compilers, (__builtin_unreachable is a gcc-4 ism).

Quoted 1 line

It is also supported by clang.

OK, but it's not supported by gcc-3. So gcc-3 would crap out seeing the statement, which is why we have to wrapper it.

Quoted 4 lines
Quoted 3 lines

Got to say this still looks wrong. If wrong_size_cmpxchg() cannot return, the function should be annotated as such with __noreturn (like panic()) so the unreachable() should be superfluous.

Okay. I can try that instead.

Great; This seems to work for me (but then my compiler doesn't see the unreachable problem).

James

---

include/asm-generic/cmpxchg-local.h +2 -1
index d8d4c89..70bef78 100644
@@ -4,7 +4,8 @@
 #include <linux/types.h>
 #include <linux/irqflags.h>
 
-extern unsigned long wrong_size_cmpxchg(volatile void *ptr);
+extern unsigned long wrong_size_cmpxchg(volatile void *ptr)
+	__noreturn;
 
 /*
  * Generic version of __cmpxchg_local (disables interrupts). Takes an unsigned
#10 lore raw
Quoted 28 lines · James Bottomley
Quoted 27 lines · Behan Webster
Quoted 26 lines · James Bottomley
Quoted 19 lines · On Sat, 2014-03-22 at 08:48 -0700, [email protected] wrote:

From: Behan Webster <[email protected]>

Fix uninitialized return code in default case in cmpxchg-local.h

This patch fixes the code to prevent an uninitialized return value that is detected when compiling with clang. The bug produces numerous warnings when compiling the Linux kernel with clang.

---
   include/asm-generic/cmpxchg-local.h | 1 +
   1 file changed, 1 insertion(+)
include/asm-generic/cmpxchg-local.h +0 -0
index d8d4c89..9112111 100644

include/asm-generic/cmpxchg-local.h static inline unsigned long __cmpxchg_local_generic(volatile void *ptr, · line 44

Quoted 4 lines
include/asm-generic/cmpxchg-local.h +1 -0
@@ -41,6 +41,7 @@ static inline unsigned long __cmpxchg_local_generic(volatile void *ptr,
   		break;
   	default:
   		wrong_size_cmpxchg(ptr);
+		__builtin_unreachable();

No, that's got to be unreachable() so that it works in all compilers, (__builtin_unreachable is a gcc-4 ism).

Quoted 3 lines
Quoted 1 line

It is also supported by clang.

OK, but it's not supported by gcc-3. So gcc-3 would crap out seeing the statement, which is why we have to wrapper it.

Sorry. I wasn't clear. It is not merely a gcc-4 ism; it is also supported by clang.

I'm not arguing it should be used. I understand (and agree with) your objection to its use since it isn't supported before gcc-4.

Quoted 15 lines
Quoted 4 lines
Quoted 3 lines

Got to say this still looks wrong. If wrong_size_cmpxchg() cannot return, the function should be annotated as such with __noreturn (like panic()) so the unreachable() should be superfluous.

Okay. I can try that instead.

Great; This seems to work for me (but then my compiler doesn't see the unreachable problem).

James

---

include/asm-generic/cmpxchg-local.h +0 -0
index d8d4c89..70bef78 100644

include/asm-generic/cmpxchg-local.h line 11

Quoted 9 lines
include/asm-generic/cmpxchg-local.h +2 -1
@@ -4,7 +4,8 @@
  #include <linux/types.h>
  #include <linux/irqflags.h>
 
-extern unsigned long wrong_size_cmpxchg(volatile void *ptr);
+extern unsigned long wrong_size_cmpxchg(volatile void *ptr)
+	__noreturn;
 
  /*
   * Generic version of __cmpxchg_local (disables interrupts). Takes an unsigned

Nice. I'll give it a try. It looks like a much better solution.

Behan

Signature · 2 lines
Behan Webster
[email protected]
#11 lore raw

PATCH v4 1/1 LLVMLinux: Remove warning about returning an uninitialized variable

In reply to James Bottomley

From: Behan Webster <[email protected]>

Fix uninitialized return code in default case in cmpxchg-local.h

This patch fixes the code to prevent an uninitialized return value that is detected when compiling with clang. The bug produces numerous warnings when compiling the Linux kernel with clang.

---
 include/asm-generic/cmpxchg-local.h | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
include/asm-generic/cmpxchg-local.h +2 -1
index d8d4c89..70bef78 100644
@@ -4,7 +4,8 @@
 #include <linux/types.h>
 #include <linux/irqflags.h>
 
-extern unsigned long wrong_size_cmpxchg(volatile void *ptr);
+extern unsigned long wrong_size_cmpxchg(volatile void *ptr)
+	__noreturn;
 
 /*
  * Generic version of __cmpxchg_local (disables interrupts). Takes an unsigned
Signature · 1 line
1.8.3.2
Acked-by on v4 1/1
#12 lore raw
Quoted 10 lines · [email protected] wrote:

From: Behan Webster <[email protected]>

Fix uninitialized return code in default case in cmpxchg-local.h

This patch fixes the code to prevent an uninitialized return value that is detected when compiling with clang. The bug produces numerous warnings when compiling the Linux kernel with clang.

maintainer Acked-by on v4 1/1
#13 lore raw
In reply to [email protected]
Quoted 11 lines · On Monday 24 March 2014, [email protected] wrote:

From: Behan Webster <[email protected]>

Fix uninitialized return code in default case in cmpxchg-local.h

This patch fixes the code to prevent an uninitialized return value that is detected when compiling with clang. The bug produces numerous warnings when compiling the Linux kernel with clang.

#14 lore raw
In reply to [email protected]
Quoted 20 lines · On 03/21/2014 11:38 PM, [email protected] wrote:

From: Behan Webster <[email protected]>

Fix uninitialized return code in default case in cmpxchg-local.h

This patch fixes the code to prevent an uninitialized return value that is detected when compiling with clang. The bug produces numerous warnings when compiling the Linux kernel with clang.

---
 include/asm-generic/cmpxchg-local.h | 1 +
 1 file changed, 1 insertion(+)
include/asm-generic/cmpxchg-local.h +0 -0
index d8d4c89..4c41bb8 100644

include/asm-generic/cmpxchg-local.h static inline unsigned long __cmpxchg_local_generic(volatile void *ptr, · line 47

Quoted 7 lines
include/asm-generic/cmpxchg-local.h +1 -0
@@ -41,6 +41,7 @@ static inline unsigned long __cmpxchg_local_generic(volatile void *ptr,
 		break;
 	default:
 		wrong_size_cmpxchg(ptr);
+		prev = 0;
 	}
 	raw_local_irq_restore(flags);
 	return prev;

Shouldn't this be a build time assert (__compiletime_error())?

-hpa

#15 lore raw
Quoted 15 lines · H. Peter Anvin
Quoted 14 lines · On 03/21/2014 11:38 PM, [email protected] wrote:

From: Behan Webster <[email protected]>

Fix uninitialized return code in default case in cmpxchg-local.h

This patch fixes the code to prevent an uninitialized return value that is detected when compiling with clang. The bug produces numerous warnings when compiling the Linux kernel with clang.

---
  include/asm-generic/cmpxchg-local.h | 1 +
  1 file changed, 1 insertion(+)
Quoted 14 lines

include/asm-generic/cmpxchg-local.h static inline unsigned long __cmpxchg_local_generic(volatile void *ptr, · line 47

Quoted 13 lines
include/asm-generic/cmpxchg-local.h +1 -0
index d8d4c89..4c41bb8 100644
@@ -41,6 +41,7 @@ static inline unsigned long __cmpxchg_local_generic(volatile void *ptr,
  		break;
  	default:
  		wrong_size_cmpxchg(ptr);
+		prev = 0;
  	}
  	raw_local_irq_restore(flags);
  	return prev;

Shouldn't this be a build time assert (__compiletime_error())?

I changed it to a __noreturn on wrong_size_cmpxchg thanks to James Bottomley.

Which would be better?

Behan

Signature · 2 lines
Behan Webster
[email protected]
#16 lore raw
Quoted 17 lines · Behan Webster
Quoted 17 lines
Quoted 14 lines
include/asm-generic/cmpxchg-local.h +1 -0
index d8d4c89..4c41bb8 100644
@@ -41,6 +41,7 @@ static inline unsigned long
 __cmpxchg_local_generic(volatile void *ptr,
          break;
      default:
          wrong_size_cmpxchg(ptr);
+        prev = 0;
      }
      raw_local_irq_restore(flags);
Quoted 2 lines
      return prev;

Shouldn't this be a build time assert (__compiletime_error())?

Quoted 5 lines

I changed it to a __noreturn on wrong_size_cmpxchg thanks to James Bottomley.

Which would be better?

__compiletime_error traps at compile time rather than link time, which is what you want.

-hpa

#17 lore raw
Quoted 17 lines · H. Peter Anvin
Quoted 16 lines · Behan Webster
Quoted 16 lines
Quoted 13 lines
include/asm-generic/cmpxchg-local.h +1 -0
index d8d4c89..4c41bb8 100644
@@ -41,6 +41,7 @@ static inline unsigned long
 __cmpxchg_local_generic(volatile void *ptr,
           break;
       default:
           wrong_size_cmpxchg(ptr);
+        prev = 0;
       }
       raw_local_irq_restore(flags);
Quoted 2 lines
       return prev;

Shouldn't this be a build time assert (__compiletime_error())?

Quoted 7 lines
Quoted 5 lines

I changed it to a __noreturn on wrong_size_cmpxchg thanks to James Bottomley.

Which would be better?

__compiletime_error traps at compile time rather than link time, which is what you want.

The idea is to remove the compile time warning that the return code "prev" isn't initialized in the default case. Indicating that wrong_size_cmpxchg doesn't return fixes that false positive.

Behan

Signature · 2 lines
Behan Webster
[email protected]
#18 lore raw
Quoted 7 lines · Behan Webster
Quoted 3 lines
Quoted 1 line

__compiletime_error traps at compile time rather than link time, which is what you want.

The idea is to remove the compile time warning that the return code "prev" isn't initialized in the default case. Indicating that wrong_size_cmpxchg doesn't return fixes that false positive.

Perhaps __compiletime_error() should have __noreturn built in?

-hpa