Skip to contentSkip to conversation

migration: fix coverity nits

PATCH In review Peter Maydell 3 participants 11 messages Jul 21, 2022 – Aug 2, 2022

lore
Share & tools
lore
#0 lore raw

cover migration: fix coverity nits

This patchset fixes four Coverity nits in the migration code. The first patch is just adding an assert() to clue coverity in that an array index must be in-bounds. The second adds an ULL suffix to force a multiplication to be done at 64 bits.

thanks
-- PMM

Peter Maydell (2):
migration: Assert that migrate_multifd_compression() returns an
in-range value
migration: Define BLK_MIG_BLOCK_SIZE as unsigned long long

 migration/block.c     | 2 +-
 migration/migration.c | 1 +
 2 files changed, 2 insertions(+), 1 deletion(-)
Signature · 1 line
2.25.1
#1 lore raw

PATCH 1/2 migration: Assert that migrate_multifd_compression() returns an in-range value

Coverity complains that when we use the return value from migrate_multifd_compression() as an array index:
multifd_recv_state->ops = multifd_ops[migrate_multifd_compression()];

that this might overrun the array (which is declared to have size MULTIFD_COMPRESSION__MAX). This is because the function return type is MultiFDCompression, which is an autogenerated enum. The code generator includes the "one greater than the maximum possible value" MULTIFD_COMPRESSION__MAX in the enum, even though this is not actually a valid value for the enum, and this makes Coverity think that migrate_multifd_compression() could return that __MAX value and index off the end of the array.

Suppress the Coverity error by asserting that the value we're going to return is within range.

Resolves: Coverity CID 1487239, 1487254

---
 migration/migration.c | 1 +
 1 file changed, 1 insertion(+)
migration/migration.c +1 -0
index e03f698a3ca..befd4c58a69 100644
@@ -2617,6 +2617,7 @@ MultiFDCompression migrate_multifd_compression(void)
 
     s = migrate_get_current();
 
+    assert(s->parameters.multifd_compression < MULTIFD_COMPRESSION__MAX);
     return s->parameters.multifd_compression;
 }
 
Signature · 1 line
2.25.1
Reviewed-by on v1 1/2
#2 lore raw

Re: [PATCH 1/2] migration: Assert that migrate_multifd_compression() returns an in-range value

Quoted 13 lines · * Peter Maydell ([email protected]) wrote:

Coverity complains that when we use the return value from migrate_multifd_compression() as an array index:
multifd_recv_state->ops = multifd_ops[migrate_multifd_compression()];

that this might overrun the array (which is declared to have size MULTIFD_COMPRESSION__MAX). This is because the function return type is MultiFDCompression, which is an autogenerated enum. The code generator includes the "one greater than the maximum possible value" MULTIFD_COMPRESSION__MAX in the enum, even though this is not actually a valid value for the enum, and this makes Coverity think that migrate_multifd_compression() could return that __MAX value and index off the end of the array.

Quoted 5 lines

Suppress the Coverity error by asserting that the value we're going to return is within range.

Resolves: Coverity CID 1487239, 1487254

Quoted 19 lines
---
 migration/migration.c | 1 +
 1 file changed, 1 insertion(+)
migration/migration.c +1 -0
index e03f698a3ca..befd4c58a69 100644
@@ -2617,6 +2617,7 @@ MultiFDCompression migrate_multifd_compression(void)
 
     s = migrate_get_current();
 
+    assert(s->parameters.multifd_compression < MULTIFD_COMPRESSION__MAX);
     return s->parameters.multifd_compression;
 }
 
Signature · 1 line
2.25.1
Signature · 1 line
Dr. David Alan Gilbert / [email protected] / Manchester, UK
Reviewed-by on v1 1/2
#3 lore raw

Re: [PATCH 1/2] migration: Assert that migrate_multifd_compression() returns an in-range value

In reply to Peter Maydell
Quoted 13 lines · Peter Maydell

Coverity complains that when we use the return value from migrate_multifd_compression() as an array index:
multifd_recv_state->ops = multifd_ops[migrate_multifd_compression()];

that this might overrun the array (which is declared to have size MULTIFD_COMPRESSION__MAX). This is because the function return type is MultiFDCompression, which is an autogenerated enum. The code generator includes the "one greater than the maximum possible value" MULTIFD_COMPRESSION__MAX in the enum, even though this is not actually a valid value for the enum, and this makes Coverity think that migrate_multifd_compression() could return that __MAX value and index off the end of the array.

Quoted 5 lines

Suppress the Coverity error by asserting that the value we're going to return is within range.

Resolves: Coverity CID 1487239, 1487254

#4 lore raw

PATCH 2/2 migration: Define BLK_MIG_BLOCK_SIZE as unsigned long long

In reply to Peter Maydell

When we use BLK_MIG_BLOCK_SIZE in expressions like block_mig_state.submitted * BLK_MIG_BLOCK_SIZE, this multiplication is done as 32 bits, because both operands are 32 bits. Coverity complains about possible overflows because we then accumulate that into a 64 bit variable.

Define BLK_MIG_BLOCK_SIZE as unsigned long long using the ULL suffix. The only two current uses of it with this problem are both in block_save_pending(), so we could just cast to uint64_t there, but using the ULL suffix is simpler and ensures that we don't accidentally introduce new variants of the same issue in future.

Resolves: Coverity CID 1487136, 1487175

---
I haven't tried to analyse the code to see if the multiplications could ever actually end up overflowing, but I would assume probably not.

 migration/block.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
migration/block.c +1 -1
index 9e5aae58982..3577c815a94 100644
@@ -28,7 +28,7 @@
 #include "sysemu/block-backend.h"
 #include "trace.h"
 
-#define BLK_MIG_BLOCK_SIZE           (1 << 20)
+#define BLK_MIG_BLOCK_SIZE           (1ULL << 20)
 #define BDRV_SECTORS_PER_DIRTY_CHUNK (BLK_MIG_BLOCK_SIZE >> BDRV_SECTOR_BITS)
 
 #define BLK_MIG_FLAG_DEVICE_BLOCK       0x01
Signature · 1 line
2.25.1
#5 lore raw

Re: [PATCH 2/2] migration: Define BLK_MIG_BLOCK_SIZE as unsigned long long

Quoted 27 lines · * Peter Maydell ([email protected]) wrote:

When we use BLK_MIG_BLOCK_SIZE in expressions like block_mig_state.submitted * BLK_MIG_BLOCK_SIZE, this multiplication is done as 32 bits, because both operands are 32 bits. Coverity complains about possible overflows because we then accumulate that into a 64 bit variable.

Define BLK_MIG_BLOCK_SIZE as unsigned long long using the ULL suffix. The only two current uses of it with this problem are both in block_save_pending(), so we could just cast to uint64_t there, but using the ULL suffix is simpler and ensures that we don't accidentally introduce new variants of the same issue in future.

Resolves: Coverity CID 1487136, 1487175

---
I haven't tried to analyse the code to see if the multiplications could ever actually end up overflowing, but I would assume probably not.

 migration/block.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
migration/block.c +0 -0
index 9e5aae58982..3577c815a94 100644

migration/block.c line 31

Quoted 5 lines
migration/block.c +1 -1
@@ -28,7 +28,7 @@
 #include "sysemu/block-backend.h"
 #include "trace.h"
 
-#define BLK_MIG_BLOCK_SIZE           (1 << 20)
+#define BLK_MIG_BLOCK_SIZE           (1ULL << 20)

Is it a problem that this is passed to bdrv_create_dirty_bitmap that takes a uint32_t ?

Dave

Quoted 6 lines
migration/block.c +0 -0
 #define BDRV_SECTORS_PER_DIRTY_CHUNK (BLK_MIG_BLOCK_SIZE >> BDRV_SECTOR_BITS)
 
 #define BLK_MIG_FLAG_DEVICE_BLOCK       0x01
Signature · 1 line
2.25.1
Signature · 1 line
Dr. David Alan Gilbert / [email protected] / Manchester, UK
#6 lore raw

Re: [PATCH 2/2] migration: Define BLK_MIG_BLOCK_SIZE as unsigned long long

Quoted 32 lines · Dr. David Alan Gilbert
Quoted 30 lines · * Peter Maydell ([email protected]) wrote:

When we use BLK_MIG_BLOCK_SIZE in expressions like block_mig_state.submitted * BLK_MIG_BLOCK_SIZE, this multiplication is done as 32 bits, because both operands are 32 bits. Coverity complains about possible overflows because we then accumulate that into a 64 bit variable.

Define BLK_MIG_BLOCK_SIZE as unsigned long long using the ULL suffix. The only two current uses of it with this problem are both in block_save_pending(), so we could just cast to uint64_t there, but using the ULL suffix is simpler and ensures that we don't accidentally introduce new variants of the same issue in future.

Resolves: Coverity CID 1487136, 1487175

---
I haven't tried to analyse the code to see if the multiplications could ever actually end up overflowing, but I would assume probably not.

 migration/block.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
migration/block.c +0 -0
index 9e5aae58982..3577c815a94 100644
@@ -28,7 +28,7 @@
 #include "sysemu/block-backend.h"
 #include "trace.h"
 
Quoted 5 lines

migration/block.c line 31

Quoted 2 lines
migration/block.c +1 -1
-#define BLK_MIG_BLOCK_SIZE           (1 << 20)
+#define BLK_MIG_BLOCK_SIZE           (1ULL << 20)

Is it a problem that this is passed to bdrv_create_dirty_bitmap that takes a uint32_t ?

Shouldn't be -- the constant value still fits within 32 bits.

-- PMM

#7 lore raw

Re: [PATCH 2/2] migration: Define BLK_MIG_BLOCK_SIZE as unsigned long long

Quoted 34 lines · * Peter Maydell ([email protected]) wrote:
Quoted 32 lines · Dr. David Alan Gilbert
Quoted 30 lines · * Peter Maydell ([email protected]) wrote:

When we use BLK_MIG_BLOCK_SIZE in expressions like block_mig_state.submitted * BLK_MIG_BLOCK_SIZE, this multiplication is done as 32 bits, because both operands are 32 bits. Coverity complains about possible overflows because we then accumulate that into a 64 bit variable.

Define BLK_MIG_BLOCK_SIZE as unsigned long long using the ULL suffix. The only two current uses of it with this problem are both in block_save_pending(), so we could just cast to uint64_t there, but using the ULL suffix is simpler and ensures that we don't accidentally introduce new variants of the same issue in future.

Resolves: Coverity CID 1487136, 1487175

---
I haven't tried to analyse the code to see if the multiplications could ever actually end up overflowing, but I would assume probably not.

 migration/block.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
migration/block.c +0 -0
index 9e5aae58982..3577c815a94 100644
@@ -28,7 +28,7 @@
 #include "sysemu/block-backend.h"
 #include "trace.h"
 
Quoted 7 lines
Quoted 5 lines

migration/block.c line 31

Quoted 2 lines
migration/block.c +1 -1
-#define BLK_MIG_BLOCK_SIZE           (1 << 20)
+#define BLK_MIG_BLOCK_SIZE           (1ULL << 20)

Is it a problem that this is passed to bdrv_create_dirty_bitmap that takes a uint32_t ?

Shouldn't be -- the constant value still fits within 32 bits.

Hmm OK, lets keep an eye out for build problems on any odd combos

Quoted 2 lines

-- PMM

Signature · 1 line
Dr. David Alan Gilbert / [email protected] / Manchester, UK
Reviewed-by on v1 2/2
#8 lore raw

Re: [PATCH 2/2] migration: Define BLK_MIG_BLOCK_SIZE as unsigned long long

In reply to Peter Maydell
Quoted 6 lines · Peter Maydell

When we use BLK_MIG_BLOCK_SIZE in expressions like block_mig_state.submitted * BLK_MIG_BLOCK_SIZE, this multiplication is done as 32 bits, because both operands are 32 bits. Coverity complains about possible overflows because we then accumulate that into a 64 bit variable.

Quoted 8 lines

Define BLK_MIG_BLOCK_SIZE as unsigned long long using the ULL suffix. The only two current uses of it with this problem are both in block_save_pending(), so we could just cast to uint64_t there, but using the ULL suffix is simpler and ensures that we don't accidentally introduce new variants of the same issue in future.

Resolves: Coverity CID 1487136, 1487175

#9 lore raw
In reply to Peter Maydell
Quoted 6 lines · Peter Maydell

This patchset fixes four Coverity nits in the migration code. The first patch is just adding an assert() to clue coverity in that an array index must be in-bounds. The second adds an ULL suffix to force a multiplication to be done at 64 bits.

Quoted 7 lines

thanks
-- PMM

Peter Maydell (2):
migration: Assert that migrate_multifd_compression() returns an
in-range value
migration: Define BLK_MIG_BLOCK_SIZE as unsigned long long

Ping? This series got reviewed but doesn't seem to have made it into a migration pullreq yet.

thanks
-- PMM

#10 lore raw
Quoted 10 lines · * Peter Maydell ([email protected]) wrote:
Quoted 9 lines · Peter Maydell

This patchset fixes four Coverity nits in the migration code. The first patch is just adding an assert() to clue coverity in that an array index must be in-bounds. The second adds an ULL suffix to force a multiplication to be done at 64 bits.

thanks
-- PMM

Quoted 7 lines
Quoted 4 lines

Peter Maydell (2):
migration: Assert that migrate_multifd_compression() returns an
in-range value
migration: Define BLK_MIG_BLOCK_SIZE as unsigned long long

Ping? This series got reviewed but doesn't seem to have made it into a migration pullreq yet.

Queued

Quoted 3 lines

thanks
-- PMM

Signature · 1 line
Dr. David Alan Gilbert / [email protected] / Manchester, UK