target/mips/mxu: Fix overruns
cover target/mips/mxu: Fix overruns
Fix overruns mxu_gpr[XRb - 1] reported by Coverity.
Philippe Mathieu-Daudé (3):
target/mips/mxu: Replace magic array size by its definition target/mips/mxu: Avoid overrun in gen_mxu_S32SLT() target/mips/mxu: Avoid overrun in gen_mxu_q8adde()
target/mips/tcg/mxu_translate.c | 36 +++++++++++++++++++++++---------- 1 file changed, 25 insertions(+), 11 deletions(-)
Signature · 1 line
2.38.1
PATCH 1/3 target/mips/mxu: Replace magic array size by its definition
--- target/mips/tcg/mxu_translate.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
target/mips/tcg/mxu_translate.c +1 -1
@@ -609,7 +609,7 @@ enum {static TCGv mxu_gpr[NUMBER_OF_MXU_REGISTERS -1 ];static TCGv mxu_CR; -static const char mxuregnames[][4 ] = { +static const char mxuregnames[NUMBER_OF_MXU_REGISTERS ][4 ] = {"XR1" ,"XR2" ,"XR3" ,"XR4" ,"XR5" ,"XR6" ,"XR7" ,"XR8" ,"XR9" ,"XR10" ,"XR11" ,"XR12" ,"XR13" ,"XR14" ,"XR15" ,"XCR" , };
Signature · 1 line
2.38.1
PATCH 2/3 target/mips/mxu: Avoid overrun in gen_mxu_S32SLT()
In reply to Philippe Mathieu-DaudéCoverity reports a potential overrun (CID 1517769):
Overrunning array "mxu_gpr" of 15 8-byte elements at element index 4294967295 (byte offset 34359738367) using index "XRb - 1U" (which evaluates to 4294967295).
Use gen_load_mxu_gpr() to safely load MXU registers.
--- target/mips/tcg/mxu_translate.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-)
target/mips/tcg/mxu_translate.c +6 -2
@@ -2434,8 +2434,12 @@ static void gen_mxu_S32SLT(DisasContext *ctx)tcg_gen_movi_tl (mxu_gpr[XRa -1 ],0 ); }else {/* the most general case */ -tcg_gen_setcond_tl (TCG_COND_LT , mxu_gpr[XRa -1 ], - mxu_gpr[XRb -1 ], mxu_gpr[XRc -1 ]); +TCGv t0 =tcg_temp_new (); +TCGv t1 =tcg_temp_new (); + +gen_load_mxu_gpr (t0, XRb); +gen_load_mxu_gpr (t1, XRc); +tcg_gen_setcond_tl (TCG_COND_LT , mxu_gpr[XRa -1 ], t0, t1); } }
Signature · 1 line
2.38.1
PATCH 3/3 target/mips/mxu: Avoid overrun in gen_mxu_q8adde()
In reply to Philippe Mathieu-DaudéCoverity reports a potential overruns (CID 1517770):
Overrunning array "mxu_gpr" of 15 8-byte elements at element index 4294967295 (byte offset 34359738367) using index "XRb - 1U" (which evaluates to 4294967295).
Add a gen_extract_mxu_gpr() helper similar to gen_load_mxu_gpr() to safely extract MXU registers.
--- target/mips/tcg/mxu_translate.c | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-)
target/mips/tcg/mxu_translate.c +18 -8
@@ -644,6 +644,16 @@ static inline void gen_store_mxu_gpr(TCGv t, unsigned int reg) } } +static inline void gen_extract_mxu_gpr (TCGv t,unsigned int reg, +unsigned int ofs,unsigned int len) +{ +if (reg ==0 ) { +tcg_gen_movi_tl (t,0 ); + }else if (reg <=15 ) { +tcg_gen_extract_tl (t, mxu_gpr[reg -1 ], ofs, len); + } +} +/* MXU control register moves. */ static inline void gen_load_mxu_cr (TCGv t) {
@@ -3004,10 +3014,10 @@ static void gen_mxu_q8adde(DisasContext *ctx, bool accumulate)TCGv t5 =tcg_temp_new ();if (XRa !=0 ) { -tcg_gen_extract_tl (t0, mxu_gpr[XRb -1 ],16 ,8 ); -tcg_gen_extract_tl (t1, mxu_gpr[XRc -1 ],16 ,8 ); -tcg_gen_extract_tl (t2, mxu_gpr[XRb -1 ],24 ,8 ); -tcg_gen_extract_tl (t3, mxu_gpr[XRc -1 ],24 ,8 ); +gen_extract_mxu_gpr (t0, XRb,16 ,8 ); +gen_extract_mxu_gpr (t1, XRc,16 ,8 ); +gen_extract_mxu_gpr (t2, XRb,24 ,8 ); +gen_extract_mxu_gpr (t3, XRc,24 ,8 );if (aptn2 &2 ) {tcg_gen_sub_tl (t0, t0, t1);tcg_gen_sub_tl (t2, t2, t3);
@@ -3027,10 +3037,10 @@ static void gen_mxu_q8adde(DisasContext *ctx, bool accumulate)tcg_gen_or_tl (t4, t2, t0); }if (XRd !=0 ) { -tcg_gen_extract_tl (t0, mxu_gpr[XRb -1 ],0 ,8 ); -tcg_gen_extract_tl (t1, mxu_gpr[XRc -1 ],0 ,8 ); -tcg_gen_extract_tl (t2, mxu_gpr[XRb -1 ],8 ,8 ); -tcg_gen_extract_tl (t3, mxu_gpr[XRc -1 ],8 ,8 ); +gen_extract_mxu_gpr (t0, XRb,0 ,8 ); +gen_extract_mxu_gpr (t1, XRc,0 ,8 ); +gen_extract_mxu_gpr (t2, XRb,8 ,8 ); +gen_extract_mxu_gpr (t3, XRc,8 ,8 );if (aptn2 &1 ) {tcg_gen_sub_tl (t0, t0, t1);tcg_gen_sub_tl (t2, t2, t3);
Signature · 1 line
2.38.1
Quoted 6 lines · Philippe Mathieu-Daudé
Fix overruns mxu_gpr[XRb - 1] reported by Coverity.
Philippe Mathieu-Daudé (3):
target/mips/mxu: Replace magic array size by its definition target/mips/mxu: Avoid overrun ingen_mxu_S32SLT()target/mips/mxu: Avoid overrun ingen_mxu_q8adde()
r~