Skip to contentSkip to conversation

target/riscv/csr.c: fix OVERFLOW_BEFORE_WIDEN in rmw_sctrdepth()

PATCH Applied Daniel Henrique Barboza 3 participants 6 messages Mar 7, 2025 – Mar 19, 2025 RISC-V

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 target/riscv/csr.c: fix OVERFLOW_BEFORE_WIDEN in rmw_sctrdepth()

Coverity found the following issue:

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

CID 1593156: Integer handling issues (OVERFLOW_BEFORE_WIDEN) Potentially overflowing expression "0x10 << depth" with type

"int" (32 bits, signed) is evaluated using 32-bit arithmetic, and then used in a context that expects an expression of type "uint64_t" (64 bits, unsigned).
4299 depth = 16 << depth;

Fix it by forcing the expression to be 64 bits wide by using '16ULL'.

Resolves: Coverity CID 1593156

Fixes c48bd18eae ("target/riscv: Add support for Control Transfer Records extension CSRs.")
---
 target/riscv/csr.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
target/riscv/csr.c +1 -1
index 0ebcca4597..e832ff3ca9 100644
@@ -4296,7 +4296,7 @@ static RISCVException rmw_sctrdepth(CPURISCVState *env, int csrno,
         }
 
         /* Update sctrstatus.WRPTR with a legal value */
-        depth = 16 << depth;
+        depth = 16ULL << depth;
         env->sctrstatus =
             env->sctrstatus & (~SCTRSTATUS_WRPTR_MASK | (depth - 1));
     }
Signature · 1 line
2.48.1
Reviewed-by on v1 1/1
#1 lore raw
Quoted 10 lines · Daniel Henrique Barboza

Coverity found the following issue:

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

CID 1593156: Integer handling issues (OVERFLOW_BEFORE_WIDEN) Potentially overflowing expression "0x10 << depth" with type

"int" (32 bits, signed) is evaluated using 32-bit arithmetic, and then used in a context that expects an expression of type "uint64_t" (64 bits, unsigned).
4299 depth = 16 << depth;

Quoted 5 lines

Fix it by forcing the expression to be 64 bits wide by using '16ULL'.

Resolves: Coverity CID 1593156

Fixes c48bd18eae ("target/riscv: Add support for Control Transfer Records extension CSRs.")

Alistair

Quoted 21 lines
---
 target/riscv/csr.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
target/riscv/csr.c +1 -2
index 0ebcca4597..e832ff3ca9 100644
@@ -4296,7 +4296,7 @@ static RISCVException rmw_sctrdepth(CPURISCVState *env, int csrno,
         }
 
         /* Update sctrstatus.WRPTR with a legal value */
-        depth = 16 << depth;
+        depth = 16ULL << depth;
         env->sctrstatus =
             env->sctrstatus & (~SCTRSTATUS_WRPTR_MASK | (depth - 1));
     }
--

2.48.1

#2 lore raw
In reply to Daniel Henrique Barboza
Quoted 10 lines · Daniel Henrique Barboza

Coverity found the following issue:

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

CID 1593156: Integer handling issues (OVERFLOW_BEFORE_WIDEN) Potentially overflowing expression "0x10 << depth" with type

"int" (32 bits, signed) is evaluated using 32-bit arithmetic, and then used in a context that expects an expression of type "uint64_t" (64 bits, unsigned).
4299 depth = 16 << depth;

Quoted 5 lines

Fix it by forcing the expression to be 64 bits wide by using '16ULL'.

Resolves: Coverity CID 1593156

Fixes c48bd18eae ("target/riscv: Add support for Control Transfer Records extension CSRs.")

Thanks!

Applied to riscv-to-apply.next

Alistair

Quoted 21 lines
---
 target/riscv/csr.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
target/riscv/csr.c +1 -2
index 0ebcca4597..e832ff3ca9 100644
@@ -4296,7 +4296,7 @@ static RISCVException rmw_sctrdepth(CPURISCVState *env, int csrno,
         }
 
         /* Update sctrstatus.WRPTR with a legal value */
-        depth = 16 << depth;
+        depth = 16ULL << depth;
         env->sctrstatus =
             env->sctrstatus & (~SCTRSTATUS_WRPTR_MASK | (depth - 1));
     }
--

2.48.1

#3 lore raw
In reply to Daniel Henrique Barboza
Quoted 24 lines · Daniel Henrique Barboza

Coverity found the following issue:

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

CID 1593156: Integer handling issues (OVERFLOW_BEFORE_WIDEN) Potentially overflowing expression "0x10 << depth" with type

"int" (32 bits, signed) is evaluated using 32-bit arithmetic, and then used in a context that expects an expression of type "uint64_t" (64 bits, unsigned).
4299 depth = 16 << depth;

Fix it by forcing the expression to be 64 bits wide by using '16ULL'.

Resolves: Coverity CID 1593156

Fixes c48bd18eae ("target/riscv: Add support for Control Transfer Records extension CSRs.")
---
 target/riscv/csr.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
target/riscv/csr.c +0 -0
index 0ebcca4597..e832ff3ca9 100644

target/riscv/csr.c static RISCVException rmw_sctrdepth(CPURISCVState *env, int csrno, · line 4302

Quoted 8 lines
target/riscv/csr.c +1 -1
@@ -4296,7 +4296,7 @@ static RISCVException rmw_sctrdepth(CPURISCVState *env, int csrno,
         }
 
         /* Update sctrstatus.WRPTR with a legal value */
-        depth = 16 << depth;
+        depth = 16ULL << depth;
         env->sctrstatus =
             env->sctrstatus & (~SCTRSTATUS_WRPTR_MASK | (depth - 1));
     }

This is a clear false-positive from Coverity, by the way: we just checked and enforced that depth is at most SCTRDEPTH_MAX, i.e. 4, and 16 << 4 cannot possibly overflow anything.

-- PMM

#4 lore raw
Quoted 28 lines · Peter Maydell
Quoted 26 lines · Daniel Henrique Barboza

Coverity found the following issue:

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

CID 1593156: Integer handling issues (OVERFLOW_BEFORE_WIDEN) Potentially overflowing expression "0x10 << depth" with type

"int" (32 bits, signed) is evaluated using 32-bit arithmetic, and then used in a context that expects an expression of type "uint64_t" (64 bits, unsigned).
4299 depth = 16 << depth;

Fix it by forcing the expression to be 64 bits wide by using '16ULL'.

Resolves: Coverity CID 1593156

Fixes c48bd18eae ("target/riscv: Add support for Control Transfer Records extension CSRs.")
---
  target/riscv/csr.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
target/riscv/csr.c +0 -0
index 0ebcca4597..e832ff3ca9 100644
@@ -4296,7 +4296,7 @@ static RISCVException rmw_sctrdepth(CPURISCVState *env, int csrno,
          }
 
Quoted 10 lines

target/riscv/csr.c static RISCVException rmw_sctrdepth(CPURISCVState *env, int csrno, · line 4302

Quoted 6 lines
target/riscv/csr.c +1 -1
          /* Update sctrstatus.WRPTR with a legal value */
-        depth = 16 << depth;
+        depth = 16ULL << depth;
          env->sctrstatus =
              env->sctrstatus & (~SCTRSTATUS_WRPTR_MASK | (depth - 1));
      }

This is a clear false-positive from Coverity, by the way: we just checked and enforced that depth is at most SCTRDEPTH_MAX, i.e. 4, and 16 << 4 cannot possibly overflow anything.

True. I wonder if we should keep this patch anyway due to the better code pattern in using ULL when left shifting into a 64 bit var, regardless of not fixing any overflows. There's a chance that we might copy/paste the existing pattern into another situation where an overflow might actually happen.

I'll leave to Alistair to decide whether to keep to drop this patch. Either way works for me. Thanks,

Daniel

Quoted 2 lines

-- PMM

#5 lore raw
Quoted 43 lines · Daniel Henrique Barboza
Quoted 28 lines · Peter Maydell
Quoted 26 lines · Daniel Henrique Barboza

Coverity found the following issue:

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

CID 1593156: Integer handling issues (OVERFLOW_BEFORE_WIDEN) Potentially overflowing expression "0x10 << depth" with type

"int" (32 bits, signed) is evaluated using 32-bit arithmetic, and then used in a context that expects an expression of type "uint64_t" (64 bits, unsigned).
4299 depth = 16 << depth;

Fix it by forcing the expression to be 64 bits wide by using '16ULL'.

Resolves: Coverity CID 1593156

Fixes c48bd18eae ("target/riscv: Add support for Control Transfer Records extension CSRs.")
---
  target/riscv/csr.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
target/riscv/csr.c +0 -0
index 0ebcca4597..e832ff3ca9 100644
@@ -4296,7 +4296,7 @@ static RISCVException rmw_sctrdepth(CPURISCVState *env, int csrno,
          }
 
Quoted 10 lines

target/riscv/csr.c static RISCVException rmw_sctrdepth(CPURISCVState *env, int csrno, · line 4302

Quoted 6 lines
target/riscv/csr.c +1 -1
          /* Update sctrstatus.WRPTR with a legal value */
-        depth = 16 << depth;
+        depth = 16ULL << depth;
          env->sctrstatus =
              env->sctrstatus & (~SCTRSTATUS_WRPTR_MASK | (depth - 1));
      }

This is a clear false-positive from Coverity, by the way: we just checked and enforced that depth is at most SCTRDEPTH_MAX, i.e. 4, and 16 << 4 cannot possibly overflow anything.

Quoted 8 lines

True. I wonder if we should keep this patch anyway due to the better code pattern in using ULL when left shifting into a 64 bit var, regardless of not fixing any overflows. There's a chance that we might copy/paste the existing pattern into another situation where an overflow might actually happen.

I'll leave to Alistair to decide whether to keep to drop this patch. Either way works for me. Thanks,

Yeah, I figured it was a false positive with SCTRDEPTH_MAX being 4. It seemed easiest to just "fix" it to keep Coverity happy though. It doesn't cost us anything to fix it here.

Alistair

Quoted 9 lines

Daniel

Quoted 2 lines

-- PMM