Hexagon: Fix undefined behavior bugs
cover Hexagon: Fix undefined behavior bugs
Coverity reported an undefined behavior issue and UBSan showed another issue with UB.
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
PATCH 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
@@ -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 >=3 ); threshold =UINT64_C (3 ) << (shift -3 );if (low <= threshold) {return base +1 ;
Signature · 1 line
2.34.1
Re: [PATCH 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.
I'm not sure how to interpret this part. If the absolute minimum of shift it 41, then assert could be >= 41 directly?
Quoted 24 lines
Link https://lore.kernel.org/qemu-devel/CAFEAcA_WJ-PYCfiiu44uTQiDoc58_9Ky3uEd-9g14nPgDPT__g@mail.gmail.com/ open hereFixes 2633da1e127f ("target/hexagon: add the QFloat logic")Resolves: Coverity CID 1688240
Signed-off-by Brian Cain <[email protected]>--- target/hexagon/mmvec/hvx_qfloat.c | 5 +++++ 1 file changed, 5 insertions(+)target/hexagon/mmvec/hvx_qfloat.c +5
-0@@ -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 >=3 ); threshold =UINT64_C (3 ) << (shift -3 );if (low <= threshold) {return base +1 ;
Re: [PATCH 1/2] target/hexagon: assert qfloat shift
Quoted 10 lines · Pierrick Bouvier
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.
I'm not sure how to interpret this part. If the absolute minimum of shift it 41, then assert could be >= 41 directly?
Yeah: I was focused more on the coverity finding than applying the right fix. I'll change it to 41.
Quoted 25 lines
Quoted 25 lines
Link https://lore.kernel.org/qemu-devel/CAFEAcA_WJ-PYCfiiu44uTQiDoc58_9Ky3uEd-9g14nPgDPT__g@mail.gmail.com/ open hereFixes 2633da1e127f ("target/hexagon: add the QFloat logic")Resolves: Coverity CID 1688240
Signed-off-by Brian Cain <[email protected]>--- target/hexagon/mmvec/hvx_qfloat.c | 5 +++++ 1 file changed, 5 insertions(+)target/hexagon/mmvec/hvx_qfloat.c +5
-0@@ -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 >=3 ); threshold =UINT64_C (3 ) << (shift -3 );if (low <= threshold) {return base +1 ;
PATCH 2/2 target/hexagon: reset implicit USR write state
In reply to Brian Cainimplicit_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'
--- target/hexagon/translate.c | 1 + 1 file changed, 1 insertion(+)
target/hexagon/translate.c +1 -0
@@ -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
Re: [PATCH 2/2] target/hexagon: reset implicit USR write state
Quoted 11 lines · Brian Cain
implicit_usr_writeis 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_v81ubsan 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")Signed-off-by Brian Cain <[email protected]>--- target/hexagon/translate.c | 1 + 1 file changed, 1 insertion(+)