Skip to contentSkip to conversation

target/ppc: fix unreachable code in do_ldst_quad()

PATCH Superseded Daniel Henrique Barboza 3 participants 3 messages Jul 20, 2022 – Jul 24, 2022

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 target/ppc: fix unreachable code in do_ldst_quad()

Coverity reports that commit fc34e81acd51 ("target/ppc: add macros to check privilege level") turned the following code unreachable:

if (!prefixed && !(ctx->insns_flags2 & PPC2_LSQ_ISA207)) {
    /* lq and stq were privileged prior to V. 2.07 */
    REQUIRE_SV(ctx);
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

CID 1490757: Control flow issues (UNREACHABLE) This code cannot be reached: "if (ctx->le_mode) {

    if (ctx->le_mode) {
        gen_align_no_le(ctx);
        return true;
    }
}

This happens because the macro REQUIRE_SV(), in CONFIG_USER_MODE, will always result in a 'return true' statement.

Fix it by using "#if !defined(CONFIG_USER_ONLY)" to fold the code that shouldn't be there if we're running in a non-privileged state. This is also how the REQUIRE_SV() macro is being used in storage-ctrl-impl.c.inc.

Fixes Coverity CID 1490757
Fixes fc34e81acd51 ("target/ppc: add macros to check privilege level")
Cc Matheus Ferst <[email protected]>
---
 target/ppc/translate/fixedpoint-impl.c.inc | 4 ++++
 1 file changed, 4 insertions(+)
target/ppc/translate/fixedpoint-impl.c.inc +4 -0
index db14d3bebc..4a32fac4f3 100644
@@ -82,10 +82,14 @@ static bool do_ldst_quad(DisasContext *ctx, arg_D *a, bool store, bool prefixed)
         /* lq and stq were privileged prior to V. 2.07 */
         REQUIRE_SV(ctx);
 
+#if !defined(CONFIG_USER_ONLY)
         if (ctx->le_mode) {
             gen_align_no_le(ctx);
             return true;
         }
+#else
+    qemu_build_not_reached();
+#endif
     }
 
     if (!store && unlikely(a->ra == a->rt)) {
Signature · 1 line
2.36.1
Reviewed-by on v1 1/1
#1 lore raw
Quoted 36 lines · Daniel Henrique Barboza

Coverity reports that commit fc34e81acd51 ("target/ppc: add macros to check privilege level") turned the following code unreachable:

if (!prefixed && !(ctx->insns_flags2 & PPC2_LSQ_ISA207)) {
     /* lq and stq were privileged prior to V. 2.07 */
     REQUIRE_SV(ctx);
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

CID 1490757: Control flow issues (UNREACHABLE) This code cannot be reached: "if (ctx->le_mode) {

     if (ctx->le_mode) {
         gen_align_no_le(ctx);
         return true;
     }
}

This happens because the macro REQUIRE_SV(), in CONFIG_USER_MODE, will always result in a 'return true' statement.

Fix it by using "#if !defined(CONFIG_USER_ONLY)" to fold the code that shouldn't be there if we're running in a non-privileged state. This is also how the REQUIRE_SV() macro is being used in storage-ctrl-impl.c.inc.

Fixes Coverity CID 1490757
Fixes fc34e81acd51 ("target/ppc: add macros to check privilege level")
Cc Matheus Ferst <[email protected]>
---
  target/ppc/translate/fixedpoint-impl.c.inc | 4 ++++
  1 file changed, 4 insertions(+)
target/ppc/translate/fixedpoint-impl.c.inc +0 -0
index db14d3bebc..4a32fac4f3 100644

target/ppc/translate/fixedpoint-impl.c.inc static bool do_ldst_quad(DisasContext *ctx, arg_D *a, bool store, bool prefixed) · line 91

Quoted 10 lines
target/ppc/translate/fixedpoint-impl.c.inc +3 -0
@@ -82,10 +82,14 @@ static bool do_ldst_quad(DisasContext *ctx, arg_D *a, bool store, bool prefixed)
          /* lq and stq were privileged prior to V. 2.07 */
          REQUIRE_SV(ctx);
 
+#if !defined(CONFIG_USER_ONLY)
          if (ctx->le_mode) {
              gen_align_no_le(ctx);
              return true;
          }
+#else
+    qemu_build_not_reached();

nit: I think the indentation here is off by 1 level (missing 4 spaces)?

Quoted 8 lines
target/ppc/translate/fixedpoint-impl.c.inc +1 -1
+#endif
      }
 
      if (!store && unlikely(a->ra == a->rt)) {
--

2.36.1

Signature · 3 lines
Víctor Cora Colombo
Instituto de Pesquisas ELDORADO
Aviso Legal - Disclaimer <https://www.eldorado.org.br/disclaimer.html>
#2 lore raw
In reply to Daniel Henrique Barboza
Quoted 9 lines · Daniel Henrique Barboza

Coverity reports that commit fc34e81acd51 ("target/ppc: add macros to check privilege level") turned the following code unreachable:

if (!prefixed && !(ctx->insns_flags2 & PPC2_LSQ_ISA207)) {
     /* lq and stq were privileged prior to V. 2.07 */
     REQUIRE_SV(ctx);
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

CID 1490757: Control flow issues (UNREACHABLE) This code cannot be reached: "if (ctx->le_mode) {

Quoted 8 lines
     if (ctx->le_mode) {
         gen_align_no_le(ctx);
         return true;
     }
}

This happens because the macro REQUIRE_SV(), in CONFIG_USER_MODE, will always result in a 'return true' statement.

I think adding ifdefs isn't fantastic. This isn't actually fix a bug, so we *could* just mark this as ignore in Coverity.

If you wanted to clean this up, remove the implicit control flow from REQUIRE_* and turn the macros into pure predicates, so that you get

    if (REQUIRE_SV(ctx)) {
        return true;
    }
    if (ctx->le_mode) {
        ...
    }

r~