Skip to contentSkip to conversation

accel/tcg: Make sure that tb->size != 0 after translation

PATCH In review Ilya Leoshkevich 3 participants 10 messages Apr 16, 2021 – Apr 26, 2021

lore
Share & tools
lore
#0 lore raw

v5 cover accel/tcg: Make sure that tb->size != 0 after translation

If arch-specific code generates a translation block of size 0, tb_gen_code() may generate a spurious exception.

Fix s390x (patch 1), ARM (patch 2) and xtensa (patch 3) and add an assertion in order to catch such situations earlier (patch 4).

v1: https://lists.nongnu.org/archive/html/qemu-devel/2021-04/msg02037.html
v1 -> v2: Fix target/s390x instead of trying to tolerate tb->size == 0

          in tb_gen_code().

v2: https://lists.nongnu.org/archive/html/qemu-devel/2021-04/msg02101.html
v2 -> v3: Split the common code change into a separate patch, add the
ARM patch in order to fix
https://gitlab.com/cohuck/qemu/-/jobs/1178409450

v3: https://lists.nongnu.org/archive/html/qemu-devel/2021-04/msg02332.html
v3 -> v4: Add the xtensa patch in order to fix

          https://gitlab.com/cohuck/qemu/-/jobs/1178409540

v4: https://lists.nongnu.org/archive/html/qemu-devel/2021-04/msg02592.html
v4 -> v5: Handle thumb: the following C code triggers the assertion:

          typedef void (*funcptr)(void);
          int main() { funcptr f = (funcptr)0xffff0001; f(); }

Ilya Leoshkevich (4):
target/s390x: Fix translation exception on illegal instruction target/arm: Make sure that commpage's tb->size != 0 target/xtensa: Make sure that tb->size != 0 accel/tcg: Assert that tb->size != 0 after translation

 accel/tcg/translate-all.c |  1 +
 target/arm/translate.c    |  2 ++
 target/s390x/translate.c  | 16 +++++++++++-----
 target/xtensa/translate.c |  3 +++
 4 files changed, 17 insertions(+), 5 deletions(-)
Signature · 1 line
2.29.2
#1 lore raw

PATCH v5 1/4 target/s390x: Fix translation exception on illegal instruction

Hitting an uretprobe in a s390x TCG guest causes a SIGSEGV. What happens is:

* uretprobe maps a userspace page containing an invalid instruction.
* uretprobe replaces the target function's return address with the address of that page.
* When tb_gen_code() is called on that page, tb->size ends up being 0 (because the page starts with the invalid instruction), which causes virt_page2 to point to the previous page.
* The previous page is not mapped, so this causes a spurious translation exception.

tb->size must never be 0: even if there is an illegal instruction, the instruction bytes that have been looked at must count towards tb->size. So adjust s390x's translate_one() to act this way for both illegal instructions and instructions that are known to generate exceptions.

---
 target/s390x/translate.c | 16 +++++++++++-----
 1 file changed, 11 insertions(+), 5 deletions(-)
target/s390x/translate.c +11 -5
index 4f953ddfba..e243624d2a 100644
@@ -6412,7 +6412,8 @@ static DisasJumpType translate_one(CPUS390XState *env, DisasContext *s)
         qemu_log_mask(LOG_UNIMP, "unimplemented opcode 0x%02x%02x\n",
                       s->fields.op, s->fields.op2);
         gen_illegal_opcode(s);
-        return DISAS_NORETURN;
+        ret = DISAS_NORETURN;
+        goto out;
     }
 
 #ifndef CONFIG_USER_ONLY
@@ -6428,7 +6429,8 @@ static DisasJumpType translate_one(CPUS390XState *env, DisasContext *s)
         /* privileged instruction */
         if ((s->base.tb->flags & FLAG_MASK_PSTATE) && (insn->flags & IF_PRIV)) {
             gen_program_exception(s, PGM_PRIVILEGED);
-            return DISAS_NORETURN;
+            ret = DISAS_NORETURN;
+            goto out;
         }
 
         /* if AFP is not enabled, instructions and registers are forbidden */
@@ -6455,7 +6457,8 @@ static DisasJumpType translate_one(CPUS390XState *env, DisasContext *s)
             }
             if (dxc) {
                 gen_data_exception(dxc);
-                return DISAS_NORETURN;
+                ret = DISAS_NORETURN;
+                goto out;
             }
         }
 
@@ -6463,7 +6466,8 @@ static DisasJumpType translate_one(CPUS390XState *env, DisasContext *s)
         if (insn->flags & IF_VEC) {
             if (!((s->base.tb->flags & FLAG_MASK_VECTOR))) {
                 gen_data_exception(0xfe);
-                return DISAS_NORETURN;
+                ret = DISAS_NORETURN;
+                goto out;
             }
         }
 
@@ -6484,7 +6488,8 @@ static DisasJumpType translate_one(CPUS390XState *env, DisasContext *s)
             (insn->spec & SPEC_r1_f128 && !is_fp_pair(get_field(s, r1))) ||
             (insn->spec & SPEC_r2_f128 && !is_fp_pair(get_field(s, r2)))) {
             gen_program_exception(s, PGM_SPECIFICATION);
-            return DISAS_NORETURN;
+            ret = DISAS_NORETURN;
+            goto out;
         }
     }
 
@@ -6544,6 +6549,7 @@ static DisasJumpType translate_one(CPUS390XState *env, DisasContext *s)
     }
 #endif
 
+out:
     /* Advance to the next instruction.  */
     s->base.pc_next = s->pc_tmp;
     return ret;
Signature · 1 line
2.29.2
#2 lore raw

PATCH v5 2/4 target/arm: Make sure that commpage's tb->size != 0

In reply to Ilya Leoshkevich

tb_gen_code() assumes that tb->size must never be zero, otherwise it may produce spurious exceptions. For ARM this may happen when creating a translation block for the commpage.

Fix by pretending that commpage translation blocks have at least one instruction.

---
 target/arm/translate.c | 2 ++
 1 file changed, 2 insertions(+)
target/arm/translate.c +2 -0
index 62b1c2081b..cb9e30c341 100644
@@ -9060,6 +9060,7 @@ static void arm_tr_translate_insn(DisasContextBase *dcbase, CPUState *cpu)
     unsigned int insn;
 
     if (arm_pre_translate_insn(dc)) {
+        dc->base.pc_next += 4;
         return;
     }
 
@@ -9129,6 +9130,7 @@ static void thumb_tr_translate_insn(DisasContextBase *dcbase, CPUState *cpu)
     bool is_16bit;
 
     if (arm_pre_translate_insn(dc)) {
+        dc->base.pc_next += 2;
         return;
     }
 
Signature · 1 line
2.29.2
Reviewed-by on v5 2/4
#3 lore raw

Re: [PATCH v5 2/4] target/arm: Make sure that commpage's tb->size != 0

Quoted 11 lines · Ilya Leoshkevich

tb_gen_code() assumes that tb->size must never be zero, otherwise it may produce spurious exceptions. For ARM this may happen when creating a translation block for the commpage.

Fix by pretending that commpage translation blocks have at least one instruction.

---
  target/arm/translate.c | 2 ++
  1 file changed, 2 insertions(+)

r~

#4 lore raw

PATCH v5 3/4 target/xtensa: Make sure that tb->size != 0

In reply to Ilya Leoshkevich

tb_gen_code() assumes that tb->size must never be zero, otherwise it may produce spurious exceptions. For xtensa this may happen when decoding an unknown instruction, when handling a write into the CCOUNT or CCOMPARE special register and when single-stepping the first instruction of an exception handler.

Fix by pretending that the size of the respective translation block is 1 in all these cases.

---
 target/xtensa/translate.c | 3 +++
 1 file changed, 3 insertions(+)
target/xtensa/translate.c +3 -0
index 0ae4efc48a..73584d9d60 100644
@@ -917,6 +917,7 @@ static void disas_xtensa_insn(CPUXtensaState *env, DisasContext *dc)
                       "unknown instruction length (pc = %08x)\n",
                       dc->pc);
         gen_exception_cause(dc, ILLEGAL_INSTRUCTION_CAUSE);
+        dc->base.pc_next = dc->pc + 1;
         return;
     }
 
@@ -1274,11 +1275,13 @@ static void xtensa_tr_translate_insn(DisasContextBase *dcbase, CPUState *cpu)
     if ((tb_cflags(dc->base.tb) & CF_USE_ICOUNT)
         && (dc->base.tb->flags & XTENSA_TBFLAG_YIELD)) {
         gen_exception(dc, EXCP_YIELD);
+        dc->base.pc_next = dc->pc + 1;
         dc->base.is_jmp = DISAS_NORETURN;
         return;
     }
     if (dc->base.tb->flags & XTENSA_TBFLAG_EXCEPTION) {
         gen_exception(dc, EXCP_DEBUG);
+        dc->base.pc_next = dc->pc + 1;
         dc->base.is_jmp = DISAS_NORETURN;
         return;
     }
Signature · 1 line
2.29.2
#5 lore raw

PATCH v5 4/4 accel/tcg: Assert that tb->size != 0 after translation

In reply to Ilya Leoshkevich

If arch-specific code generates a translation block of size 0, tb_gen_code() may generate a spurious exception. Add an assertion in order to catch such situations early.

---
 accel/tcg/translate-all.c | 1 +
 1 file changed, 1 insertion(+)
accel/tcg/translate-all.c +1 -0
index ba6ab09790..93b2dae112 100644
@@ -1913,6 +1913,7 @@ TranslationBlock *tb_gen_code(CPUState *cpu,
 
     tcg_ctx->cpu = env_cpu(env);
     gen_intermediate_code(cpu, tb, max_insns);
+    assert(tb->size != 0);
     tcg_ctx->cpu = NULL;
     max_insns = tb->icount;
 
Signature · 1 line
2.29.2
#6 lore raw
In reply to Ilya Leoshkevich
Quoted 30 lines · On Fri, 16 Apr 2021 17:49:35 +0200 Ilya Leoshkevich <[email protected]> wrote:

If arch-specific code generates a translation block of size 0, tb_gen_code() may generate a spurious exception.

Fix s390x (patch 1), ARM (patch 2) and xtensa (patch 3) and add an assertion in order to catch such situations earlier (patch 4).

v1: https://lists.nongnu.org/archive/html/qemu-devel/2021-04/msg02037.html
v1 -> v2: Fix target/s390x instead of trying to tolerate tb->size == 0

          in tb_gen_code().

v2: https://lists.nongnu.org/archive/html/qemu-devel/2021-04/msg02101.html
v2 -> v3: Split the common code change into a separate patch, add the
ARM patch in order to fix
https://gitlab.com/cohuck/qemu/-/jobs/1178409450

v3: https://lists.nongnu.org/archive/html/qemu-devel/2021-04/msg02332.html
v3 -> v4: Add the xtensa patch in order to fix

          https://gitlab.com/cohuck/qemu/-/jobs/1178409540

v4: https://lists.nongnu.org/archive/html/qemu-devel/2021-04/msg02592.html
v4 -> v5: Handle thumb: the following C code triggers the assertion:

          typedef void (*funcptr)(void);
          int main() { funcptr f = (funcptr)0xffff0001; f(); }

Ilya Leoshkevich (4):
target/s390x: Fix translation exception on illegal instruction target/arm: Make sure that commpage's tb->size != 0 target/xtensa: Make sure that tb->size != 0 accel/tcg: Assert that tb->size != 0 after translation

Quoted 6 lines
 accel/tcg/translate-all.c |  1 +
 target/arm/translate.c    |  2 ++
 target/s390x/translate.c  | 16 +++++++++++-----
 target/xtensa/translate.c |  3 +++
 4 files changed, 17 insertions(+), 5 deletions(-)

So, what's the way forward here? I can pick this if I get an ack for the arm patch. If someone else wants to take this, I'll just ack the s390x patch.

#7 lore raw
Quoted 3 lines · Cornelia Huck

So, what's the way forward here? I can pick this if I get an ack for the arm patch. If someone else wants to take this, I'll just ack the s390x patch.

You've volunteered, so that means you get it, I think. ;-)

r~

#8 lore raw
Quoted 10 lines · On Fri, 23 Apr 2021 10:50:59 -0700 Richard Henderson…
Quoted 3 lines · Cornelia Huck

So, what's the way forward here? I can pick this if I get an ack for the arm patch. If someone else wants to take this, I'll just ack the s390x patch.

You've volunteered, so that means you get it, I think. ;-)

r~

I guessed as much :) Thanks for your review!

#9 lore raw
In reply to Ilya Leoshkevich
Quoted 30 lines · On Fri, 16 Apr 2021 17:49:35 +0200 Ilya Leoshkevich <[email protected]> wrote:

If arch-specific code generates a translation block of size 0, tb_gen_code() may generate a spurious exception.

Fix s390x (patch 1), ARM (patch 2) and xtensa (patch 3) and add an assertion in order to catch such situations earlier (patch 4).

v1: https://lists.nongnu.org/archive/html/qemu-devel/2021-04/msg02037.html
v1 -> v2: Fix target/s390x instead of trying to tolerate tb->size == 0

          in tb_gen_code().

v2: https://lists.nongnu.org/archive/html/qemu-devel/2021-04/msg02101.html
v2 -> v3: Split the common code change into a separate patch, add the
ARM patch in order to fix
https://gitlab.com/cohuck/qemu/-/jobs/1178409450

v3: https://lists.nongnu.org/archive/html/qemu-devel/2021-04/msg02332.html
v3 -> v4: Add the xtensa patch in order to fix

          https://gitlab.com/cohuck/qemu/-/jobs/1178409540

v4: https://lists.nongnu.org/archive/html/qemu-devel/2021-04/msg02592.html
v4 -> v5: Handle thumb: the following C code triggers the assertion:

          typedef void (*funcptr)(void);
          int main() { funcptr f = (funcptr)0xffff0001; f(); }

Ilya Leoshkevich (4):
target/s390x: Fix translation exception on illegal instruction target/arm: Make sure that commpage's tb->size != 0 target/xtensa: Make sure that tb->size != 0 accel/tcg: Assert that tb->size != 0 after translation

Quoted 6 lines
 accel/tcg/translate-all.c |  1 +
 target/arm/translate.c    |  2 ++
 target/s390x/translate.c  | 16 +++++++++++-----
 target/xtensa/translate.c |  3 +++
 4 files changed, 17 insertions(+), 5 deletions(-)

Thanks, queued to s390-next.