Skip to contentSkip to conversation

msf2: Remove dead code reported by Coverity

PATCH Superseded Subbaraya Sundeep 2 participants 5 messages Oct 18, 2017 – Oct 22, 2017

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 msf2: Remove dead code reported by Coverity

Fixed incorrect frame size mask, validated maximum frame size in spi_write and removed dead code.

---
v2:

	else if -> else in set_fifodepth
	log guest error when frame size is more than 32
 hw/ssi/mss-spi.c | 12 ++++++++----
 1 file changed, 8 insertions(+), 4 deletions(-)
hw/ssi/mss-spi.c +8 -4
index 5a8e308..7fef2c3 100644
@@ -76,9 +76,10 @@
 #define C_BIGFIFO            (1 << 29)
 #define C_RESET              (1 << 31)
 
-#define FRAMESZ_MASK         0x1F
+#define FRAMESZ_MASK         0x3F
 #define FMCOUNT_MASK         0x00FFFF00
 #define FMCOUNT_SHIFT        8
+#define FRAMESZ_MAX          32
 
 static void txfifo_reset(MSSSpiState *s)
 {
@@ -104,10 +105,8 @@ static void set_fifodepth(MSSSpiState *s)
         s->fifo_depth = 32;
     } else if (size <= 16) {
         s->fifo_depth = 16;
-    } else if (size <= 32) {
-        s->fifo_depth = 8;
     } else {
-        s->fifo_depth = 4;
+        s->fifo_depth = 8;
     }
 }
 
@@ -301,6 +300,11 @@ static void spi_write(void *opaque, hwaddr addr,
         if (s->enabled) {
             break;
         }
+        if ((value & FRAMESZ_MASK) > FRAMESZ_MAX) {
+            qemu_log_mask(LOG_GUEST_ERROR, "%s: Maximum frame size is %d\n",
+                         __func__, FRAMESZ_MAX);
+            break;
+        }
         s->regs[R_SPI_DFSIZE] = value;
         break;
 
Signature · 1 line
2.5.0
#1 lore raw
Quoted 41 lines · Subbaraya Sundeep

Fixed incorrect frame size mask, validated maximum frame size in spi_write and removed dead code.

---
v2:

	else if -> else in set_fifodepth
	log guest error when frame size is more than 32

hw/ssi/mss-spi.c | 12 ++++++++----

1 file changed, 8 insertions(+), 4 deletions(-)
hw/ssi/mss-spi.c +3 -4
index 5a8e308..7fef2c3 100644
@@ -76,9 +76,10 @@
 #define C_BIGFIFO            (1 << 29)
 #define C_RESET              (1 << 31)
 
-#define FRAMESZ_MASK         0x1F
+#define FRAMESZ_MASK         0x3F
 #define FMCOUNT_MASK         0x00FFFF00
 #define FMCOUNT_SHIFT        8
+#define FRAMESZ_MAX          32
 
 static void txfifo_reset(MSSSpiState *s)
 {
@@ -104,10 +105,8 @@ static void set_fifodepth(MSSSpiState *s)
        s->fifo_depth = 32;
    } else if (size <= 16) {
        s->fifo_depth = 16;
-    } else if (size <= 32) {
-        s->fifo_depth = 8;
    } else {
-        s->fifo_depth = 4;
+        s->fifo_depth = 8;
    }
 }
 

hw/ssi/mss-spi.c static void spi_write(void *opaque, hwaddr addr, · line 309

Quoted 10 lines
hw/ssi/mss-spi.c +5 -0
@@ -301,6 +300,11 @@ static void spi_write(void *opaque, hwaddr addr,
        if (s->enabled) {
            break;
        }
+        if ((value & FRAMESZ_MASK) > FRAMESZ_MAX) {
+            qemu_log_mask(LOG_GUEST_ERROR, "%s: Maximum frame size is %d\n",
+                         __func__, FRAMESZ_MAX);
+            break;
+        }
        s->regs[R_SPI_DFSIZE] = value;
        break;

This test, and subsequent use of value appear to be out of sorts - in that while it is testing for the value by ANDing it with FRAMESZ_MASK, it is subsequently using the value without that mask applied to it, which still has the potential to be larger than FRAMESZ_MASK if it contains a value larger than 0x3F.

Is that the expected behaviour? If so, maybe include a comment on it?

Also, it might be useful to include the incorrect value in the logged output too, not just what the maximum is.

Thanks,

Darren.

Quoted 5 lines
Signature · 2 lines
-- 
2.5.0
#2 lore raw

Hi Darren,

Quoted 62 lines · Darren Kenny
Quoted 43 lines · Subbaraya Sundeep

Fixed incorrect frame size mask, validated maximum frame size in spi_write and removed dead code.

---
v2:
else if -> else in set_fifodepth
log guest error when frame size is more than 32

hw/ssi/mss-spi.c | 12 ++++++++----

1 file changed, 8 insertions(+), 4 deletions(-)
hw/ssi/mss-spi.c +3 -4
index 5a8e308..7fef2c3 100644
@@ -76,9 +76,10 @@
 #define C_BIGFIFO            (1 << 29)
 #define C_RESET              (1 << 31)
 
-#define FRAMESZ_MASK         0x1F
+#define FRAMESZ_MASK         0x3F
 #define FMCOUNT_MASK         0x00FFFF00
 #define FMCOUNT_SHIFT        8
+#define FRAMESZ_MAX          32
 
 static void txfifo_reset(MSSSpiState *s)
 {
@@ -104,10 +105,8 @@ static void set_fifodepth(MSSSpiState *s)
        s->fifo_depth = 32;
    } else if (size <= 16) {
        s->fifo_depth = 16;
-    } else if (size <= 32) {
-        s->fifo_depth = 8;
    } else {
-        s->fifo_depth = 4;
+        s->fifo_depth = 8;
    }
 }
 
@@ -301,6 +300,11 @@ static void spi_write(void *opaque, hwaddr addr,
        if (s->enabled) {

hw/ssi/mss-spi.c static void spi_write(void *opaque, hwaddr addr, · line 310

Quoted 10 lines
hw/ssi/mss-spi.c +5 -0
            break;
        }
+        if ((value & FRAMESZ_MASK) > FRAMESZ_MAX) {
+            qemu_log_mask(LOG_GUEST_ERROR, "%s: Maximum frame size is
 %d\n",
+                         __func__, FRAMESZ_MAX);
+            break;
+        }
        s->regs[R_SPI_DFSIZE] = value;
        break;

This test, and subsequent use of value appear to be out of sorts - in that while it is testing for the value by ANDing it with FRAMESZ_MASK, it is subsequently using the value without that mask applied to it, which still has the potential to be larger than FRAMESZ_MASK if it contains a value larger than 0x3F.

Quoted 3 lines

Is that the expected behaviour? If so, maybe include a comment on it?

As per docs regarding [31:6]:
Software should not rely on the value of a reserved bit. To provide compatibility with future products, the value of a reserved bit should be preserved across a read-modify-write operation.

Hence we do not care about [31:6] and validate only [5:0] for size during write.
When reading size we AND with FRAMESZ_MASK. In other words we let [31:6] bits like scratch bits where guest can read and write. I am really not sure how hardware
behaves if [5:0] is greater than 32 hence guest error and write wont happen. If this is not
right we can discuss :)

Quoted 5 lines

Also, it might be useful to include the incorrect value in the logged output too, not just what the maximum is.

Ok I will change.

Thanks,
Sundeep

Quoted 10 lines

Thanks,

Darren.

Quoted 5 lines

--
2.5.0

#3 lore raw

Hi Sundeep,

Quoted 76 lines · sundeep subbaraya

Hi Darren,

Quoted 62 lines · Darren Kenny
Quoted 43 lines · Subbaraya Sundeep

Fixed incorrect frame size mask, validated maximum frame size in spi_write and removed dead code.

---
v2:
else if -> else in set_fifodepth
log guest error when frame size is more than 32

hw/ssi/mss-spi.c | 12 ++++++++----

1 file changed, 8 insertions(+), 4 deletions(-)
hw/ssi/mss-spi.c +3 -4
index 5a8e308..7fef2c3 100644
@@ -76,9 +76,10 @@
 #define C_BIGFIFO            (1 << 29)
 #define C_RESET              (1 << 31)
 
-#define FRAMESZ_MASK         0x1F
+#define FRAMESZ_MASK         0x3F
 #define FMCOUNT_MASK         0x00FFFF00
 #define FMCOUNT_SHIFT        8
+#define FRAMESZ_MAX          32
 
 static void txfifo_reset(MSSSpiState *s)
 {
@@ -104,10 +105,8 @@ static void set_fifodepth(MSSSpiState *s)
        s->fifo_depth = 32;
    } else if (size <= 16) {
        s->fifo_depth = 16;
-    } else if (size <= 32) {
-        s->fifo_depth = 8;
    } else {
-        s->fifo_depth = 4;
+        s->fifo_depth = 8;
    }
 }
 
@@ -301,6 +300,11 @@ static void spi_write(void *opaque, hwaddr addr,
        if (s->enabled) {

hw/ssi/mss-spi.c static void spi_write(void *opaque, hwaddr addr, · line 310

Quoted 10 lines
hw/ssi/mss-spi.c +5 -0
            break;
        }
+        if ((value & FRAMESZ_MASK) > FRAMESZ_MAX) {
+            qemu_log_mask(LOG_GUEST_ERROR, "%s: Maximum frame size is
 %d\n",
+                         __func__, FRAMESZ_MAX);
+            break;
+        }
        s->regs[R_SPI_DFSIZE] = value;
        break;

This test, and subsequent use of value appear to be out of sorts - in that while it is testing for the value by ANDing it with FRAMESZ_MASK, it is subsequently using the value without that mask applied to it, which still has the potential to be larger than FRAMESZ_MASK if it contains a value larger than 0x3F.

Quoted 3 lines

Is that the expected behaviour? If so, maybe include a comment on it?

As per docs regarding [31:6]:
Software should not rely on the value of a reserved bit. To provide compatibility with future products, the value of a reserved bit should be preserved across a read-modify-write operation.

Quoted 6 lines

Hence we do not care about [31:6] and validate only [5:0] for size during write. When reading size we AND with FRAMESZ_MASK. In other words we let [31:6] bits like scratch bits where guest can read and write. I am really not sure how hardware behaves if [5:0] is greater than 32 hence guest error and write wont happen. If this is not right we can discuss :)

That sounds fine then - definitely would suggest some sort of comment w.r.t. the fact that we are intentionally preserving these extra bits - in case anyone looks at this again in the future.

Quoted 7 lines
Quoted 7 lines

Also, it might be useful to include the incorrect value in the logged output too, not just what the maximum is.

Ok I will change.

OK

Thanks,

Darren.

#4 lore raw

Hi Darren,

Quoted 89 lines · Darren Kenny

Hi Sundeep,

Quoted 76 lines · sundeep subbaraya

Hi Darren,

On Wed, Oct 18, 2017 at 2:24 PM, Darren Kenny <[email protected]> wrote:

Quoted 61 lines · Subbaraya Sundeep

Fixed incorrect frame size mask, validated maximum frame

Quoted 43 lines

size in spi_write and removed dead code.

---
v2:
else if -> else in set_fifodepth
log guest error when frame size is more than 32

hw/ssi/mss-spi.c | 12 ++++++++----

1 file changed, 8 insertions(+), 4 deletions(-)
hw/ssi/mss-spi.c +3 -4
index 5a8e308..7fef2c3 100644
@@ -76,9 +76,10 @@
 #define C_BIGFIFO            (1 << 29)
 #define C_RESET              (1 << 31)
 
-#define FRAMESZ_MASK         0x1F
+#define FRAMESZ_MASK         0x3F
 #define FMCOUNT_MASK         0x00FFFF00
 #define FMCOUNT_SHIFT        8
+#define FRAMESZ_MAX          32
 
 static void txfifo_reset(MSSSpiState *s)
 {
@@ -104,10 +105,8 @@ static void set_fifodepth(MSSSpiState *s)
        s->fifo_depth = 32;
    } else if (size <= 16) {
        s->fifo_depth = 16;
-    } else if (size <= 32) {
-        s->fifo_depth = 8;
    } else {
-        s->fifo_depth = 4;
+        s->fifo_depth = 8;
    }
 }
 
@@ -301,6 +300,11 @@ static void spi_write(void *opaque, hwaddr addr,
        if (s->enabled) {

hw/ssi/mss-spi.c static void spi_write(void *opaque, hwaddr addr, · line 310

Quoted 10 lines
hw/ssi/mss-spi.c +5 -0
            break;
        }
+        if ((value & FRAMESZ_MASK) > FRAMESZ_MAX) {
+            qemu_log_mask(LOG_GUEST_ERROR, "%s: Maximum frame size is
 %d\n",
+                         __func__, FRAMESZ_MAX);
+            break;
+        }
        s->regs[R_SPI_DFSIZE] = value;
        break;

This test, and subsequent use of value appear to be out of sorts - in that while it is testing for the value by ANDing it with FRAMESZ_MASK, it is subsequently using the value without that mask applied to it, which still has the potential to be larger than FRAMESZ_MASK if it contains a value larger than 0x3F.

Quoted 4 lines

Is that the expected behaviour? If so, maybe include a comment on it?

As per docs regarding [31:6]:
Software should not rely on the value of a reserved bit. To provide compatibility with future products, the value of a reserved bit should be preserved across a read-modify-write operation.

Quoted 7 lines

Hence we do not care about [31:6] and validate only [5:0] for size during write. When reading size we AND with FRAMESZ_MASK. In other words we let [31:6] bits like scratch bits where guest can read and write. I am really not sure how hardware behaves if [5:0] is greater than 32 hence guest error and write wont happen. If this is not right we can discuss :)

Quoted 4 lines

That sounds fine then - definitely would suggest some sort of comment w.r.t. the fact that we are intentionally preserving these extra bits - in case anyone looks at this again in the future.

Sure.

Thank you,
Sundeep

Quoted 15 lines
Quoted 9 lines
Quoted 8 lines

Also, it might be useful to include the incorrect value in the logged output too, not just what the maximum is.

Ok I will change.

OK

Thanks,

Darren.