Skip to contentSkip to conversation

Hexagon: Fix undefined behavior bugs

PATCH Superseded Brian Cain 2 participants 6 messages Oct 19

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Brian Cain

author
#0 lore raw

v2 cover Hexagon: Fix undefined behavior bugs

Coverity reported an undefined behavior issue and UBSan showed another issue with UB.

In v2:
- updated shift assertion to use shift >= 41 instead of 3.

v1: https://lore.kernel.org/qemu-devel/[email protected]/

Brian Cain (2):
target/hexagon: assert qfloat shift
target/hexagon: reset implicit USR write state

 target/hexagon/mmvec/hvx_qfloat.c | 5 +++++
 target/hexagon/translate.c        | 1 +
 2 files changed, 6 insertions(+)
Signature · 1 line
2.34.1
author
#1 lore raw

PATCH v2 1/2 target/hexagon: assert qfloat shift

The midpoint rounding thresholds shift by "shift - 3", which Coverity flagged as potential undefined behavior.

The legacy add and multiply paths select exp such that the normalized result is at most one bit above it. With the largest legacy fraction width of 23 bits, shift is at least 41. We can safely assert this here.

Resolves: Coverity CID 1688240

---
 target/hexagon/mmvec/hvx_qfloat.c | 5 +++++
 1 file changed, 5 insertions(+)
target/hexagon/mmvec/hvx_qfloat.c +5 -0
index 41cc827aa87..345bddcd364 100644
@@ -721,6 +721,11 @@ static int64_t legacy_round_n(FloatParts64 parts, int exp, int frac, int adj,
     }
 
     if (midpoint) {
+        /*
+         * The legacy add/multiply result is at most one bit above exp,
+         * making shift at least 41.
+         */
+        g_assert(shift >= 41);
         threshold = UINT64_C(3) << (shift - 3);
         if (low <= threshold) {
             return base + 1;
Signature · 1 line
2.34.1
Reviewed-by on v2 1/2
#2 lore raw

Re: [PATCH v2 1/2] target/hexagon: assert qfloat shift

Quoted 7 lines · Brian Cain

The midpoint rounding thresholds shift by "shift - 3", which Coverity flagged as potential undefined behavior.

The legacy add and multiply paths select exp such that the normalized result is at most one bit above it. With the largest legacy fraction width of 23 bits, shift is at least 41. We can safely assert this here.

Quoted 8 lines

Resolves: Coverity CID 1688240

---
 target/hexagon/mmvec/hvx_qfloat.c | 5 +++++
 1 file changed, 5 insertions(+)
Reviewed-by on v2 1/2
#3 lore raw

Re: [PATCH v2 1/2] target/hexagon: assert qfloat shift

In reply to Brian Cain
Quoted 7 lines · Brian Cain

The midpoint rounding thresholds shift by "shift - 3", which Coverity flagged as potential undefined behavior.

The legacy add and multiply paths select exp such that the normalized result is at most one bit above it. With the largest legacy fraction width of 23 bits, shift is at least 41. We can safely assert this here.

Quoted 8 lines

Resolves: Coverity CID 1688240

---
 target/hexagon/mmvec/hvx_qfloat.c | 5 +++++
 1 file changed, 5 insertions(+)
author
#4 lore raw

PATCH v2 2/2 target/hexagon: reset implicit USR write state

In reply to Brian Cain

implicit_usr_write is per-packet DC state. It was not cleared before analyzing the next packet. Reset it together with the other per-packet state.

When running qfloat_v81 ubsan reported:

target/hexagon/translate.c:725:32: runtime error: load of value 192, which is not a valid value for type '_Bool' target/hexagon/translate.c:874:12: runtime error: load of value 192, which is not a valid value for type '_Bool'

Fixes 3e98496691ed ("Hexagon (target/hexagon) Implicit writes to USR don't force packet commit")
---
 target/hexagon/translate.c | 1 +
 1 file changed, 1 insertion(+)
target/hexagon/translate.c +1 -0
index 2c18f5eebff..a4c5e043c72 100644
@@ -569,6 +569,7 @@ static void clear_pkt_ctx(DisasContext *ctx)
     ctx->reg_log_idx = 0;
     bitmap_zero(ctx->regs_written, TOTAL_PER_THREAD_REGS);
     bitmap_zero(ctx->predicated_regs, TOTAL_PER_THREAD_REGS);
+    ctx->implicit_usr_write = false;
 #ifndef CONFIG_USER_ONLY
     ctx->greg_log_idx = 0;
     bitmap_zero(ctx->gregs_written, NUM_GREGS);
Signature · 1 line
2.34.1
Reviewed-by on v2 2/2
#5 lore raw

Re: [PATCH v2 2/2] target/hexagon: reset implicit USR write state

Quoted 11 lines · Brian Cain

implicit_usr_write is per-packet DC state. It was not cleared before analyzing the next packet. Reset it together with the other per-packet state.

When running qfloat_v81 ubsan reported:

target/hexagon/translate.c:725:32: runtime error: load of value 192, which is not a valid value for type '_Bool' target/hexagon/translate.c:874:12: runtime error: load of value 192, which is not a valid value for type '_Bool'

Quoted 6 lines
Fixes 3e98496691ed ("Hexagon (target/hexagon) Implicit writes to USR don't force packet commit")
---
 target/hexagon/translate.c | 1 +
 1 file changed, 1 insertion(+)