Skip to contentSkip to conversation

x86/bitmap: Compile with -Wsign-conversion

PATCH In review Andrew Cooper 3 participants 4 messages Feb 5, 2024 – Feb 6, 2024

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 x86/bitmap: Compile with -Wsign-conversion

Use pragmas to able the warning in this file only. All supported versions of Clang understand this, while older GCCs simply ignore it.

bitmap_find_free_region() is the only function which isn't sign-convert clean. This highlights a latent bug in that it can't return successfully for a bitmap larger than 2G.

Add an extra check, and explicit cast to silence the warning.

---
CC: George Dunlap <[email protected]>
CC: Jan Beulich <[email protected]>
CC: Stefano Stabellini <[email protected]>
CC: Wei Liu <[email protected]>
CC: Julien Grall <[email protected]>

Slightly RFC. This is our first use of pragmas like this.

---
 xen/common/bitmap.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
xen/common/bitmap.c +5 -2
index 18e23e2f0e18..b14f8a3b3030 100644
@@ -14,6 +14,9 @@
 #include <xen/lib.h>
 #include <asm/byteorder.h>
 
+#pragma GCC diagnostic warning "-Wsign-conversion"
+#pragma clang diagnostic warning "-Wsign-conversion"
+
 /*
  * bitmaps provide an array of bits, implemented using an an
  * array of unsigned longs.  The number of valid bits in a
@@ -263,7 +266,7 @@ int bitmap_find_free_region(unsigned long *bitmap, unsigned int bits, unsigned i
 	unsigned int pages = 1 << order;
 	unsigned int i;
 
-	if(pages > BITS_PER_LONG)
+	if (pages > BITS_PER_LONG || bits >= INT_MAX)
 		return -EINVAL;
 
 	/* make a mask of the order */
@@ -278,7 +281,7 @@ int bitmap_find_free_region(unsigned long *bitmap, unsigned int bits, unsigned i
 		if((bitmap[index] & (mask << offset)) == 0) {
 			/* set region in bimap */
 			bitmap[index] |= (mask << offset);
-			return i;
+			return (int)i;
 		}
 	}
 	return -ENOMEM;
Signature · 1 line
2.30.2
#1 lore raw

Hi Andrew,

Title: You seem to change common code. So s/x86//

Quoted 10 lines · Andrew Cooper

Use pragmas to able the warning in this file only. All supported versions of Clang understand this, while older GCCs simply ignore it.

bitmap_find_free_region() is the only function which isn't sign-convert clean. This highlights a latent bug in that it can't return successfully for a bitmap larger than 2G.

Add an extra check, and explicit cast to silence the warning.

Quoted 8 lines

---
CC: George Dunlap <[email protected]>
CC: Jan Beulich <[email protected]>
CC: Stefano Stabellini <[email protected]>
CC: Wei Liu <[email protected]>
CC: Julien Grall <[email protected]>

Slightly RFC. This is our first use of pragmas like this.

The only other approach I can think of is specifying the CFLAGS per file like Linux did. I don't know if our build system supports that though.

AFAICT, the only advantage would be to avoid duplicating the pragmas. So this is not a strong preference.

Quoted 9 lines
---
  xen/common/bitmap.c | 7 +++++--
  1 file changed, 5 insertions(+), 2 deletions(-)
xen/common/bitmap.c +0 -0
index 18e23e2f0e18..b14f8a3b3030 100644

xen/common/bitmap.c line 19

Quoted 6 lines
xen/common/bitmap.c +3 -0
@@ -14,6 +14,9 @@
  #include <xen/lib.h>
  #include <asm/byteorder.h>
 
+#pragma GCC diagnostic warning "-Wsign-conversion"
+#pragma clang diagnostic warning "-Wsign-conversion"
+

OOI, any reason why wasn't added at the right at the top of the file?

Quoted 13 lines
xen/common/bitmap.c +1 -1
  /*
   * bitmaps provide an array of bits, implemented using an an
   * array of unsigned longs.  The number of valid bits in a
@@ -263,7 +266,7 @@ int bitmap_find_free_region(unsigned long *bitmap, unsigned int bits, unsigned i
  	unsigned int pages = 1 << order;
  	unsigned int i;
 
-	if(pages > BITS_PER_LONG)
+	if (pages > BITS_PER_LONG || bits >= INT_MAX)
  		return -EINVAL;
 
  	/* make a mask of the order */

xen/common/bitmap.c int bitmap_find_free_region(unsigned long *bitmap, unsigned int bits, unsigned i · line 284

Quoted 5 lines
xen/common/bitmap.c +1 -1
@@ -278,7 +281,7 @@ int bitmap_find_free_region(unsigned long *bitmap, unsigned int bits, unsigned i
  		if((bitmap[index] & (mask << offset)) == 0) {
  			/* set region in bimap */
  			bitmap[index] |= (mask << offset);
-			return i;
+			return (int)i;

It took me a while to realize that this patch should be reviewed after "x86/bitmap: Even more signed-ness fixes".

Before hand, 'i' is a signed int and we would return -ENOMEM if 'bits' is negative. So I wonder whether the change in common/bitmap.c should belong to the other patch?

Cheers,

Signature · 1 line
Julien Grall
#2 lore raw
Quoted 18 lines · Julien Grall
Quoted 17 lines · Andrew Cooper

Use pragmas to able the warning in this file only. All supported versions of Clang understand this, while older GCCs simply ignore it.

bitmap_find_free_region() is the only function which isn't sign-convert clean. This highlights a latent bug in that it can't return successfully for a bitmap larger than 2G.

Add an extra check, and explicit cast to silence the warning.

---
CC: George Dunlap <[email protected]>
CC: Jan Beulich <[email protected]>
CC: Stefano Stabellini <[email protected]>
CC: Wei Liu <[email protected]>
CC: Julien Grall <[email protected]>

Quoted 4 lines
Quoted 1 line

Slightly RFC. This is our first use of pragmas like this.

The only other approach I can think of is specifying the CFLAGS per file like Linux did. I don't know if our build system supports that though.

It does, see e.g.

# Allows usercopy.c to include itself
$(obj)/usercopy.o: CFLAGS-y += -iquote .

in arch/x86/Makefile.

Quoted 2 lines

AFAICT, the only advantage would be to avoid duplicating the pragmas. So this is not a strong preference.

My other concern there are old gcc versions we still support. I haven't checked (yet) when support for these pragma-s was introduced; I only know they haven't been there forever. However, ...

Quoted 11 lines

xen/common/bitmap.c line 19

Quoted 9 lines
xen/common/bitmap.c +3 -0
@@ -14,6 +14,9 @@
  #include <xen/lib.h>
  #include <asm/byteorder.h>
 
+#pragma GCC diagnostic warning "-Wsign-conversion"
+#pragma clang diagnostic warning "-Wsign-conversion"
+

OOI, any reason why wasn't added at the right at the top of the file?

... this may be relevant: Inline functions may have an issue with being processed with the warning enabled. Otoh it may also be a problem if the warning isn't enabled for them.

Jan

#3 lore raw
In reply to Andrew Cooper

xen/common/bitmap.c int bitmap_find_free_region(unsigned long *bitmap, unsigned int bits, unsigned i · line 266

Quoted 2 lines · Andrew Cooper
xen/common/bitmap.c +0 -0
@@ -263,7 +266,7 @@ int bitmap_find_free_region(unsigned long *bitmap, unsigned int bits, unsigned i
 	unsigned int pages = 1 << order;

If we mean to keep this function (see my reply to the other patch), shouldn't we also check "order" along with

xen/common/bitmap.c int bitmap_find_free_region(unsigned long *bitmap, unsigned int bits, unsigned i · line 270

Quoted 5 lines
xen/common/bitmap.c +1 -1
 	unsigned int i;
 
-	if(pages > BITS_PER_LONG)
+	if (pages > BITS_PER_LONG || bits >= INT_MAX)
 		return -EINVAL;

... these checks?

xen/common/bitmap.c int bitmap_find_free_region(unsigned long *bitmap, unsigned int bits, unsigned i · line 284

Quoted 6 lines
xen/common/bitmap.c +1 -1
@@ -278,7 +281,7 @@ int bitmap_find_free_region(unsigned long *bitmap, unsigned int bits, unsigned i
 		if((bitmap[index] & (mask << offset)) == 0) {
 			/* set region in bimap */
 			bitmap[index] |= (mask << offset);
-			return i;
+			return (int)i;

I agree with Julien that it looks like this change actually belongs in the other patch.

Jan