Skip to contentSkip to conversation

accel/tcg: Fix undefined shift in store_whole_le16

PATCH In review Richard Henderson 2 participants 2 messages Jun 6, 2023

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 accel/tcg: Fix undefined shift in store_whole_le16

The computation is documented as unused in this case, but triggers an ubsan error:

../accel/tcg/ldst_atomicity.c.inc:837:33: runtime error: shift exponent -32 is negative
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior ../accel/tcg/ldst_atomicity.c.inc:837:33 in

---

Hi Peter. Found this while merge testing FEAT_LSE. Clearly my bug, which I ought to have found earlier.

r~

---
 accel/tcg/ldst_atomicity.c.inc | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
accel/tcg/ldst_atomicity.c.inc +3 -1
index 2514899408..de70531a7a 100644
@@ -833,7 +833,9 @@ static uint64_t store_whole_le16(void *pv, int size, Int128 val_le)
     }
     store_atom_insert_al16(pv - o, v, m);
 
-    /* Unused if sz <= 64. */
+    if (sz <= 64) {
+        return 0;
+    }
     return int128_gethi(val_le) >> (sz - 64);
 }
 
Signature · 1 line
2.34.1
Reviewed-by on v1 1/1
#1 lore raw
Quoted 25 lines · Richard Henderson

The computation is documented as unused in this case, but triggers an ubsan error:

../accel/tcg/ldst_atomicity.c.inc:837:33: runtime error: shift exponent -32 is negative
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior ../accel/tcg/ldst_atomicity.c.inc:837:33 in

---

Hi Peter. Found this while merge testing FEAT_LSE. Clearly my bug, which I ought to have found earlier.

r~

---
 accel/tcg/ldst_atomicity.c.inc | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
accel/tcg/ldst_atomicity.c.inc +0 -0
index 2514899408..de70531a7a 100644

accel/tcg/ldst_atomicity.c.inc static uint64_t store_whole_le16(void *pv, int size, Int128 val_le) · line 840

Quoted 9 lines
accel/tcg/ldst_atomicity.c.inc +3 -1
@@ -833,7 +833,9 @@ static uint64_t store_whole_le16(void *pv, int size, Int128 val_le)
     }
     store_atom_insert_al16(pv - o, v, m);
 
-    /* Unused if sz <= 64. */
+    if (sz <= 64) {
+        return 0;
+    }
     return int128_gethi(val_le) >> (sz - 64);
 }

thanks
-- PMM