Skip to contentSkip to conversation

Hexagon: Fix undefined behavior bugs

PATCH In review Brian Cain 3 participants 6 messages Oct 20 – Oct 21

lore
Share & tools
lore

Brian Cain

author
#0 lore raw

v3 cover Hexagon: Fix undefined behavior bugs

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

In v3:
- testing with UBSan didn't show issues at v2 but I was using gcc. clang exposes another UB bug, this one in decode_set_slot_number(). Added a patch "avoid negative slot shift" to fix that bug. Pointed out by Peter.

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

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

Brian Cain (3):
target/hexagon: assert qfloat shift
target/hexagon: reset implicit USR write state
target/hexagon: avoid negative slot shift

 target/hexagon/decode.c           | 5 ++++-
 target/hexagon/mmvec/hvx_qfloat.c | 5 +++++
 target/hexagon/translate.c        | 1 +
 3 files changed, 10 insertions(+), 1 deletion(-)
Signature · 1 line
2.34.1
author
#1 lore raw

PATCH v3 1/3 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
author
#2 lore raw

PATCH v3 2/3 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
author
#3 lore raw

PATCH v3 3/3 target/hexagon: avoid negative slot shift

In reply to Brian Cain

Invalid packets can exhaust the available instruction slots while decode_set_slot_number() searches for a valid one. We would then try to evaluate a negative shift before returning the packet as invalid.

Detected by clang's UBSan.

---
 target/hexagon/decode.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
target/hexagon/decode.c +4 -1
index 89e961331ff..8391b51d629 100644
@@ -600,9 +600,12 @@ decode_set_slot_number(Packet *pkt)
     for (i = 0, slot = 3; i < pkt->num_insns; i++) {
         SlotMask valid_slots = get_valid_slots(pkt, i);
 
-        while (!(valid_slots & (1 << slot))) {
+        while (slot >= 0 && !(valid_slots & (1 << slot))) {
             slot--;
         }
+        if (slot < 0) {
+            return false;
+        }
         pkt->insn[i].slot = slot;
         if (slot) {
             /* I've assigned the slot, now decrement it for the next insn */
Signature · 1 line
2.34.1
Reviewed-by on v3 3/3
#4 lore raw

Re: [PATCH v3 3/3] target/hexagon: avoid negative slot shift

Quoted 4 lines · Brian Cain

Invalid packets can exhaust the available instruction slots while decode_set_slot_number() searches for a valid one. We would then try to evaluate a negative shift before returning the packet as invalid.

Quoted 8 lines

Detected by clang's UBSan.

---
 target/hexagon/decode.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
Reviewed-by on v3 3/3
#5 lore raw

Re: [PATCH v3 3/3] target/hexagon: avoid negative slot shift

In reply to Brian Cain
Quoted 11 lines · Brian Cain

Invalid packets can exhaust the available instruction slots while decode_set_slot_number() searches for a valid one. We would then try to evaluate a negative shift before returning the packet as invalid.

Detected by clang's UBSan.

---
  target/hexagon/decode.c | 5 ++++-
  1 file changed, 4 insertions(+), 1 deletion(-)