Skip to contentSkip to conversation

linux-user: Fix siginfo_t contents when jumping to non-readable pages

PATCH Superseded Ilya Leoshkevich 2 participants 13 messages Aug 8, 2022 – Aug 12, 2022

v3 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#1 lore raw

PATCH v3 1/4 accel/tcg: Invalidate translations when clearing PAGE_EXEC

In reply to Ilya Leoshkevich

After mprotect(addr, PROT_NONE), addr can still be executed if there are cached translations. Drop them.

---
 accel/tcg/translate-all.c | 17 ++++++++++++-----
 1 file changed, 12 insertions(+), 5 deletions(-)
accel/tcg/translate-all.c +12 -5
index ef62a199c7..32ea5f0adf 100644
@@ -2295,12 +2295,19 @@ void page_set_flags(target_ulong start, target_ulong end, int flags)
          len != 0;
          len -= TARGET_PAGE_SIZE, addr += TARGET_PAGE_SIZE) {
         PageDesc *p = page_find_alloc(addr >> TARGET_PAGE_BITS, 1);
+        bool write_set, exec_cleared;
 
-        /* If the write protection bit is set, then we invalidate
-           the code inside.  */
-        if (!(p->flags & PAGE_WRITE) &&
-            (flags & PAGE_WRITE) &&
-            p->first_tb) {
+        /*
+         * If the write protection bit is set, then we invalidate the code
+         * inside.
+         */
+        write_set = !(p->flags & PAGE_WRITE) && (flags & PAGE_WRITE);
+        /*
+         * If PAGE_EXEC is cleared, we also need to invalidate the code in
+         * order to force a fault when trying to run it.
+         */
+        exec_cleared = (p->flags & PAGE_EXEC) && !(flags & PAGE_EXEC);
+        if ((write_set || exec_cleared) && p->first_tb) {
             tb_invalidate_phys_page(addr, 0);
         }
         if (reset_target_data) {
Signature · 1 line
2.37.1
#0 lore raw

v3 cover linux-user: Fix siginfo_t contents when jumping to non-readable pages

Hi,

I noticed that when we get a SEGV due to jumping to non-readable memory, sometimes si_addr and program counter in siginfo_t are slightly off. I tracked this down to the assumption that translators stop before the end of a page, while in reality they may stop right after it.

Patch 1 fixes a minor invalidation issue, which may prevent SEGV from happening altogether.
Patches 2-3 fix the main issue on x86_64 and s390x. Many other architectures have fixed-size instructions and are not affected. Patch 4 adds tests.

Best regards,
Ilya

v1: https://lists.gnu.org/archive/html/qemu-devel/2022-08/msg00822.html
v1 -> v2: Fix individual translators instead of translator_loop

          (Peter).

v2: https://lists.gnu.org/archive/html/qemu-devel/2022-08/msg01079.html
v2 -> v3: Peek at the next instruction on s390x (Richard).
Undo more on i386 (Richard).
Check PAGE_EXEC, not PAGE_READ (Peter, Richard).

Ilya Leoshkevich (4):
accel/tcg: Invalidate translations when clearing PAGE_EXEC target/s390x: Make translator stop before the end of a page target/i386: Make translator stop before the end of a page tests/tcg: Test siginfo_t contents when jumping to non-readable pages

 accel/tcg/translate-all.c        |  17 ++--
 accel/tcg/translator.c           |   8 ++
 include/exec/translator.h        |  13 +++
 target/i386/tcg/translate.c      |  21 ++++-
 target/s390x/tcg/translate.c     |  15 +++-
 tests/tcg/multiarch/noexec.h     | 114 ++++++++++++++++++++++++
 tests/tcg/s390x/Makefile.target  |   1 +
 tests/tcg/s390x/noexec.c         | 145 +++++++++++++++++++++++++++++++
 tests/tcg/x86_64/Makefile.target |   3 +-
 tests/tcg/x86_64/noexec.c        | 116 +++++++++++++++++++++++++
 10 files changed, 442 insertions(+), 11 deletions(-)
 create mode 100644 tests/tcg/multiarch/noexec.h
 create mode 100644 tests/tcg/s390x/noexec.c
 create mode 100644 tests/tcg/x86_64/noexec.c
Signature · 1 line
2.37.1
#12 lore raw

PATCH v3 4/4 tests/tcg: Test siginfo_t contents when jumping to non-readable pages

Add x86_64 and s390x tests to prevent regressions.

---
 tests/tcg/multiarch/noexec.h     | 114 ++++++++++++++++++++++++
 tests/tcg/s390x/Makefile.target  |   1 +
 tests/tcg/s390x/noexec.c         | 145 +++++++++++++++++++++++++++++++
 tests/tcg/x86_64/Makefile.target |   3 +-
 tests/tcg/x86_64/noexec.c        | 116 +++++++++++++++++++++++++
 5 files changed, 378 insertions(+), 1 deletion(-)
 create mode 100644 tests/tcg/multiarch/noexec.h
 create mode 100644 tests/tcg/s390x/noexec.c
 create mode 100644 tests/tcg/x86_64/noexec.c
tests/tcg/multiarch/noexec.h new file +114 -0
new file mode 100644
index 0000000000..a76e0aa9ea
@@ -0,0 +1,114 @@
+/*
+ * Common code for arch-specific MMU_INST_FETCH fault testing.
+ *
+ * Declare struct arch_noexec_test before including this file and define
+ * arch_check_mcontext() after that.
+ */
+
+#include <assert.h>
+#include <signal.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/mman.h>
+#include <sys/ucontext.h>
+#include <unistd.h>
+
+/* Forward declarations. */
+
+static void arch_check_mcontext(const struct arch_noexec_test *test,
+                                const mcontext_t *ctx);
+
+/* Utility functions. */
+
+static void safe_print(const char *s)
+{
+    write(0, s, strlen(s));
+}
+
+static void safe_puts(const char *s)
+{
+    safe_print(s);
+    safe_print("\n");
+}
+
+#define PAGE_ALIGN(p) (void *)((unsigned long)(p) & ~0xfffUL)
+
+/* Testing infrastructure. */
+
+struct noexec_test {
+    const char *name;
+    void (*func)(int);
+    void *page;
+    void *expected_si_addr;
+    struct arch_noexec_test arch;
+};
+
+static const struct noexec_test *current_noexec_test;
+
+static void handle_segv(int sig, siginfo_t *info, void *ucontext)
+{
+    int err;
+
+    if (current_noexec_test == NULL) {
+        safe_puts("[  FAILED  ] unexpected SEGV");
+        _exit(1);
+    }
+
+    if (info->si_addr != current_noexec_test->expected_si_addr) {
+        safe_puts("[  FAILED  ] wrong si_addr");
+        _exit(1);
+    }
+
+    arch_check_mcontext(&current_noexec_test->arch,
+                        &((ucontext_t *)ucontext)->uc_mcontext);
+
+    err = mprotect(current_noexec_test->page, 0x1000, PROT_READ | PROT_EXEC);
+    if (err != 0) {
+        safe_puts("[  FAILED  ] mprotect() failed");
+        _exit(1);
+    }
+
+    current_noexec_test = NULL;
+}
+
+static void test_noexec_1(const struct noexec_test *test)
+{
+    int ret;
+
+    /* Trigger TB creation in order to test invalidation. */
+    test->func(0);
+
+    ret = mprotect(test->page, 0x1000, PROT_NONE);
+    assert(ret == 0);
+
+    /* Trigger SEGV and check that handle_segv() ran. */
+    current_noexec_test = test;
+    test->func(0);
+    assert(current_noexec_test == NULL);
+}
+
+static int test_noexec(struct noexec_test *tests, size_t n_tests)
+{
+    struct sigaction act;
+    size_t i;
+    int err;
+
+    memset(&act, 0, sizeof(act));
+    act.sa_sigaction = handle_segv;
+    act.sa_flags = SA_SIGINFO;
+    err = sigaction(SIGSEGV, &act, NULL);
+    assert(err == 0);
+
+    for (i = 0; i < n_tests; i++) {
+        struct noexec_test *test = &tests[i];
+
+        safe_print("[ RUN      ] ");
+        safe_puts(test->name);
+        test_noexec_1(test);
+        safe_puts("[       OK ]");
+    }
+
+    safe_puts("[  PASSED  ]");
+
+    return EXIT_SUCCESS;
+}
tests/tcg/s390x/Makefile.target +1 -0
index 1a7a4a2f59..5e13a41c3f 100644
@@ -16,6 +16,7 @@ TESTS+=shift
 TESTS+=trap
 TESTS+=signals-s390x
 TESTS+=branch-relative-long
+TESTS+=noexec
 
 Z14_TESTS=vfminmax
 vfminmax: LDFLAGS+=-lm
tests/tcg/s390x/noexec.c new file +145 -0

145 lines not on this page · raw message

tests/tcg/x86_64/Makefile.target +2 -1
index b71a6bcd5e..c0e7e5b005 100644
@@ -10,6 +10,7 @@ include $(SRC_PATH)/tests/tcg/i386/Makefile.target
 
 ifeq ($(filter %-linux-user, $(TARGET)),$(TARGET))
 X86_64_TESTS += vsyscall
+X86_64_TESTS += noexec
 TESTS=$(MULTIARCH_TESTS) $(X86_64_TESTS) test-x86_64
 else
 TESTS=$(MULTIARCH_TESTS)
@@ -20,5 +21,5 @@ test-x86_64: LDFLAGS+=-lm -lc
 test-x86_64: test-i386.c test-i386.h test-i386-shift.h test-i386-muldiv.h
 	$(CC) $(CFLAGS) $< -o $@ $(LDFLAGS)
 
-vsyscall: $(SRC_PATH)/tests/tcg/x86_64/vsyscall.c
+%: $(SRC_PATH)/tests/tcg/x86_64/%.c
 	$(CC) $(CFLAGS) $< -o $@ $(LDFLAGS)
tests/tcg/x86_64/noexec.c new file +116 -0

116 lines not on this page · raw message

Signature · 1 line
2.37.1
#7 lore raw

PATCH v3 2/4 target/s390x: Make translator stop before the end of a page

In reply to Ilya Leoshkevich

Right now translator stops right *after* the end of a page, which breaks reporting of fault locations when the last instruction of a multi-insn translation block crosses a page boundary.

---
 include/exec/translator.h    | 10 ++++++++++
 target/s390x/tcg/translate.c | 15 +++++++++++----
 2 files changed, 21 insertions(+), 4 deletions(-)
include/exec/translator.h +10 -0
index 7db6845535..d27f8c33b6 100644
@@ -187,4 +187,14 @@ FOR_EACH_TRANSLATOR_LD(GEN_TRANSLATOR_LD)
 
 #undef GEN_TRANSLATOR_LD
 
+/*
+ * Return whether addr is on the same page as where disassembly started.
+ * Translators can use this to enforce the rule that only single-insn
+ * translation blocks are allowed to cross page boundaries.
+ */
+static inline bool is_same_page(DisasContextBase *db, target_ulong addr)
+{
+    return ((addr ^ db->pc_first) & TARGET_PAGE_MASK) == 0;
+}
+
 #endif /* EXEC__TRANSLATOR_H */
target/s390x/tcg/translate.c +11 -4
index e2ee005671..8e45a0e0d3 100644
@@ -6609,6 +6609,14 @@ static void s390x_tr_insn_start(DisasContextBase *dcbase, CPUState *cs)
     dc->insn_start = tcg_last_op();
 }
 
+static target_ulong get_next_pc(CPUS390XState *env, DisasContext *s,
+                                uint64_t pc)
+{
+    uint64_t insn = ld_code2(env, s, pc);
+
+    return pc + get_ilen((insn >> 8) & 0xff);
+}
+
 static void s390x_tr_translate_insn(DisasContextBase *dcbase, CPUState *cs)
 {
     CPUS390XState *env = cs->env_ptr;
@@ -6616,10 +6624,9 @@ static void s390x_tr_translate_insn(DisasContextBase *dcbase, CPUState *cs)
 
     dc->base.is_jmp = translate_one(env, dc);
     if (dc->base.is_jmp == DISAS_NEXT) {
-        uint64_t page_start;
-
-        page_start = dc->base.pc_first & TARGET_PAGE_MASK;
-        if (dc->base.pc_next - page_start >= TARGET_PAGE_SIZE || dc->ex_value) {
+        if (!is_same_page(dcbase, dc->base.pc_next) ||
+            !is_same_page(dcbase, get_next_pc(env, dc, dc->base.pc_next)) ||
+            dc->ex_value) {
             dc->base.is_jmp = DISAS_TOO_MANY;
         }
     }
Signature · 1 line
2.37.1
#10 lore raw

PATCH v3 3/4 target/i386: Make translator stop before the end of a page

In reply to Ilya Leoshkevich

Right now translator stops right *after* the end of a page, which breaks reporting of fault locations when the last instruction of a multi-insn translation block crosses a page boundary.

An implementation, like the one arm and s390x have, would require an i386 length disassembler, which is burdensome to maintain. Another alternative would be to single-step at the end of a guest page, but this may come with a performance impact.

Fix by snapshotting disassembly state and restoring it after we figure out we crossed a page boundary. This includes rolling back cc_op updates and emitted ops. Even though i386 is the only architecture that does rollback, split it into common and architecture-dependent parts to improve readability.

---
 accel/tcg/translator.c      |  8 ++++++++
 include/exec/translator.h   |  3 +++
 target/i386/tcg/translate.c | 21 ++++++++++++++++++++-
 3 files changed, 31 insertions(+), 1 deletion(-)
accel/tcg/translator.c +8 -0
index fe7af9b943..2c4dd09df8 100644
@@ -56,6 +56,7 @@ void translator_loop(const TranslatorOps *ops, DisasContextBase *db,
 {
     uint32_t cflags = tb_cflags(tb);
     bool plugin_enabled;
+    TCGOp *last_op;
 
     /* Initialize DisasContext */
     db->tb = tb;
@@ -82,6 +83,7 @@ void translator_loop(const TranslatorOps *ops, DisasContextBase *db,
 
     while (true) {
         db->num_insns++;
+        last_op = tcg_last_op();
         ops->insn_start(db, cpu);
         tcg_debug_assert(db->is_jmp == DISAS_NEXT);  /* no early exit */
 
@@ -103,6 +105,12 @@ void translator_loop(const TranslatorOps *ops, DisasContextBase *db,
             ops->translate_insn(db, cpu);
         }
 
+        if (db->is_jmp == DISAS_TOO_MANY_UNDO) {
+            db->num_insns--;
+            tcg_remove_ops_after(last_op);
+            db->is_jmp = DISAS_TOO_MANY;
+        }
+
         /* Stop translation if translate_insn so indicated.  */
         if (db->is_jmp != DISAS_NEXT) {
             break;
include/exec/translator.h +3 -0
index d27f8c33b6..e1533aee87 100644
@@ -31,6 +31,8 @@
  * DisasJumpType:
  * @DISAS_NEXT: Next instruction in program order.
  * @DISAS_TOO_MANY: Too many instructions translated.
+ * @DISAS_TOO_MANY_UNDO: Too many instructions translated. Everything that was
+ *                       done for the current instruction must be undone.
  * @DISAS_NORETURN: Following code is dead.
  * @DISAS_TARGET_*: Start of target-specific conditions.
  *
@@ -39,6 +41,7 @@
 typedef enum DisasJumpType {
     DISAS_NEXT,
     DISAS_TOO_MANY,
+    DISAS_TOO_MANY_UNDO,
     DISAS_NORETURN,
     DISAS_TARGET_0,
     DISAS_TARGET_1,
target/i386/tcg/translate.c +20 -1
index b7972f0ff5..14d4ed1412 100644
@@ -2008,6 +2008,12 @@ static uint64_t advance_pc(CPUX86State *env, DisasContext *s, int num_bytes)
 {
     uint64_t pc = s->pc;
 
+    /* This is a subsequent insn that crosses a page boundary.  */
+    if (s->base.num_insns > 1 &&
+        !is_same_page(&s->base, s->pc + num_bytes - 1)) {
+        siglongjmp(s->jmpbuf, 2);
+    }
+
     s->pc += num_bytes;
     if (unlikely(s->pc - s->pc_start > X86_MAX_INSN_LENGTH)) {
         /* If the instruction's 16th byte is on a different page than the 1st, a
@@ -4556,6 +4562,8 @@ static target_ulong disas_insn(DisasContext *s, CPUState *cpu)
     int modrm, reg, rm, mod, op, opreg, val;
     target_ulong next_eip, tval;
     target_ulong pc_start = s->base.pc_next;
+    bool orig_cc_op_dirty = s->cc_op_dirty;
+    CCOp orig_cc_op = s->cc_op;
 
     s->pc_start = s->pc = pc_start;
     s->override = -1;
@@ -4568,9 +4576,20 @@ static target_ulong disas_insn(DisasContext *s, CPUState *cpu)
     s->rip_offset = 0; /* for relative ip address */
     s->vex_l = 0;
     s->vex_v = 0;
-    if (sigsetjmp(s->jmpbuf, 0) != 0) {
+    switch (sigsetjmp(s->jmpbuf, 0)) {
+    case 0:
+        break;
+    case 1:
         gen_exception_gpf(s);
         return s->pc;
+    case 2:
+        /* Restore state that may affect the next instruction. */
+        s->cc_op_dirty = orig_cc_op_dirty;
+        s->cc_op = orig_cc_op;
+        s->base.is_jmp = DISAS_TOO_MANY_UNDO;
+        return pc_start;
+    default:
+        g_assert_not_reached();
     }
 
     prefixes = 0;
Signature · 1 line
2.37.1
Reviewed-by on v3 2/4
#8 lore raw

Re: [PATCH v3 2/4] target/s390x: Make translator stop before the end of a page

In reply to Ilya Leoshkevich
Quoted 9 lines · Ilya Leoshkevich

Right now translator stops right*after* the end of a page, which breaks reporting of fault locations when the last instruction of a multi-insn translation block crosses a page boundary.

---
  include/exec/translator.h    | 10 ++++++++++
  target/s390x/tcg/translate.c | 15 +++++++++++----
  2 files changed, 21 insertions(+), 4 deletions(-)

r~

#2 lore raw

Re: [PATCH v3 1/4] accel/tcg: Invalidate translations when clearing PAGE_EXEC

In reply to Ilya Leoshkevich
Quoted 24 lines · Ilya Leoshkevich

After mprotect(addr, PROT_NONE), addr can still be executed if there are cached translations. Drop them.

---
  accel/tcg/translate-all.c | 17 ++++++++++++-----
  1 file changed, 12 insertions(+), 5 deletions(-)
accel/tcg/translate-all.c +2 -5
index ef62a199c7..32ea5f0adf 100644
@@ -2295,12 +2295,19 @@ void page_set_flags(target_ulong start, target_ulong end, int flags)
           len != 0;
           len -= TARGET_PAGE_SIZE, addr += TARGET_PAGE_SIZE) {
          PageDesc *p = page_find_alloc(addr >> TARGET_PAGE_BITS, 1);
+        bool write_set, exec_cleared;
 
-        /* If the write protection bit is set, then we invalidate
-           the code inside.  */
-        if (!(p->flags & PAGE_WRITE) &&
-            (flags & PAGE_WRITE) &&
-            p->first_tb) {
+        /*

accel/tcg/translate-all.c void page_set_flags(target_ulong start, target_ulong end, int flags) · line 2310

Quoted 10 lines
accel/tcg/translate-all.c +10 -0
+         * If the write protection bit is set, then we invalidate the code
+         * inside.
+         */
+        write_set = !(p->flags & PAGE_WRITE) && (flags & PAGE_WRITE);
+        /*
+         * If PAGE_EXEC is cleared, we also need to invalidate the code in
+         * order to force a fault when trying to run it.
+         */
+        exec_cleared = (p->flags & PAGE_EXEC) && !(flags & PAGE_EXEC);
+        if ((write_set || exec_cleared) && p->first_tb) {

I believe the bug you're trying to fix is in get_page_addr_code, which for USER_ONLY is currently a no-op. It ought to be checking the page permissions there, as we do for softmmu.

I have a patch for get_page_addr_code in the works, because I was working on pther stuff in the area.

r~

#11 lore raw

Re: [PATCH v3 3/4] target/i386: Make translator stop before the end of a page

In reply to Ilya Leoshkevich
Quoted 69 lines · Ilya Leoshkevich

Right now translator stops right *after* the end of a page, which breaks reporting of fault locations when the last instruction of a multi-insn translation block crosses a page boundary.

An implementation, like the one arm and s390x have, would require an i386 length disassembler, which is burdensome to maintain. Another alternative would be to single-step at the end of a guest page, but this may come with a performance impact.

Fix by snapshotting disassembly state and restoring it after we figure out we crossed a page boundary. This includes rolling back cc_op updates and emitted ops. Even though i386 is the only architecture that does rollback, split it into common and architecture-dependent parts to improve readability.

---
  accel/tcg/translator.c      |  8 ++++++++
  include/exec/translator.h   |  3 +++
  target/i386/tcg/translate.c | 21 ++++++++++++++++++++-
  3 files changed, 31 insertions(+), 1 deletion(-)
accel/tcg/translator.c +8 -0
index fe7af9b943..2c4dd09df8 100644
@@ -56,6 +56,7 @@ void translator_loop(const TranslatorOps *ops, DisasContextBase *db,
  {
      uint32_t cflags = tb_cflags(tb);
      bool plugin_enabled;
+    TCGOp *last_op;
 
      /* Initialize DisasContext */
      db->tb = tb;
@@ -82,6 +83,7 @@ void translator_loop(const TranslatorOps *ops, DisasContextBase *db,
 
      while (true) {
          db->num_insns++;
+        last_op = tcg_last_op();
          ops->insn_start(db, cpu);
          tcg_debug_assert(db->is_jmp == DISAS_NEXT);  /* no early exit */
 
@@ -103,6 +105,12 @@ void translator_loop(const TranslatorOps *ops, DisasContextBase *db,
              ops->translate_insn(db, cpu);
          }
 
+        if (db->is_jmp == DISAS_TOO_MANY_UNDO) {
+            db->num_insns--;
+            tcg_remove_ops_after(last_op);
+            db->is_jmp = DISAS_TOO_MANY;
+        }
+
          /* Stop translation if translate_insn so indicated.  */
          if (db->is_jmp != DISAS_NEXT) {
              break;
include/exec/translator.h +2 -0
index d27f8c33b6..e1533aee87 100644
@@ -31,6 +31,8 @@
   * DisasJumpType:
   * @DISAS_NEXT: Next instruction in program order.
   * @DISAS_TOO_MANY: Too many instructions translated.
+ * @DISAS_TOO_MANY_UNDO: Too many instructions translated. Everything that was
+ *                       done for the current instruction must be undone.
   * @DISAS_NORETURN: Following code is dead.
   * @DISAS_TARGET_*: Start of target-specific conditions.
   *

include/exec/translator.h line 44

Quoted 4 lines
include/exec/translator.h +1 -0
@@ -39,6 +41,7 @@
  typedef enum DisasJumpType {
      DISAS_NEXT,
      DISAS_TOO_MANY,
+    DISAS_TOO_MANY_UNDO,

Hmm, maybe. I'm not overly keen on the generic change, because I think it would be easy to use incorrectly.

include/exec/translator.h

Quoted 5 lines
include/exec/translator.h +5 -0
+    case 2:
+        /* Restore state that may affect the next instruction. */
+        s->cc_op_dirty = orig_cc_op_dirty;
+        s->cc_op = orig_cc_op;
+        s->base.is_jmp = DISAS_TOO_MANY_UNDO;

I think you can simply set s->prev_insn_end in i386_tr_insn_start, for discarding opcodes.

r~

#9 lore raw

Re: [PATCH v3 2/4] target/s390x: Make translator stop before the end of a page

In reply to Ilya Leoshkevich
Quoted 17 lines · Ilya Leoshkevich

Right now translator stops right *after* the end of a page, which breaks reporting of fault locations when the last instruction of a multi-insn translation block crosses a page boundary.

---
  include/exec/translator.h    | 10 ++++++++++
  target/s390x/tcg/translate.c | 15 +++++++++++----
  2 files changed, 21 insertions(+), 4 deletions(-)
include/exec/translator.h +0 -0
index 7db6845535..d27f8c33b6 100644
@@ -187,4 +187,14 @@ FOR_EACH_TRANSLATOR_LD(GEN_TRANSLATOR_LD)
 
  #undef GEN_TRANSLATOR_LD

include/exec/translator.h FOR_EACH_TRANSLATOR_LD(GEN_TRANSLATOR_LD) · line 198

Quoted 10 lines
include/exec/translator.h +9 -0
 
+/*
+ * Return whether addr is on the same page as where disassembly started.
+ * Translators can use this to enforce the rule that only single-insn
+ * translation blocks are allowed to cross page boundaries.
+ */
+static inline bool is_same_page(DisasContextBase *db, target_ulong addr)
+{
+    return ((addr ^ db->pc_first) & TARGET_PAGE_MASK) == 0;
+}

FYI, I've had occasion to pull this out to a separate patch locally.

r~

#3 lore raw

Re: [PATCH v3 1/4] accel/tcg: Invalidate translations when clearing PAGE_EXEC

In reply to Richard Henderson
Quoted 48 lines · Richard Henderson
Quoted 33 lines · Ilya Leoshkevich

After mprotect(addr, PROT_NONE), addr can still be executed if there
are cached translations. Drop them.

---
  accel/tcg/translate-all.c | 17 ++++++++++++-----

  1 file changed, 12 insertions(+), 5 deletions(-)
accel/tcg/translate-all.c +1 -0
index ef62a199c7..32ea5f0adf 100644
@@ -2295,12 +2295,19 @@ void page_set_flags(target_ulong start,
 target_ulong end, int flags)
            len != 0;
            len -= TARGET_PAGE_SIZE, addr += TARGET_PAGE_SIZE) {
           PageDesc *p = page_find_alloc(addr >> TARGET_PAGE_BITS,
 1);
+        bool write_set, exec_cleared;


-        /* If the write protection bit is set, then we invalidate -           the code inside.  */
-        if (!(p->flags & PAGE_WRITE) &&
-            (flags & PAGE_WRITE) &&
-            p->first_tb) {
+        /*
+         * If the write protection bit is set, then we invalidate the code
+         * inside.
+         */
+        write_set = !(p->flags & PAGE_WRITE) && (flags & PAGE_WRITE);

Quoted 8 lines

+        /*
+         * If PAGE_EXEC is cleared, we also need to invalidate the code in
+         * order to force a fault when trying to run it. +         */
+        exec_cleared = (p->flags & PAGE_EXEC) && !(flags & PAGE_EXEC);
+        if ((write_set || exec_cleared) && p->first_tb) {

I believe the bug you're trying to fix is in get_page_addr_code, which for USER_ONLY is
currently a no-op.  It ought to be checking the page permissions there, as we do for softmmu.

Quoted 3 lines

I have a patch for get_page_addr_code in the works, because I was working on pther stuff
in the area.

How is qemu-user's get_page_addr_code() involved here?

I tried to experiment with it, and while I agree that it looks buggy, it's called only from translation code paths. If we already have a translation block, these code paths are not used.

#4 lore raw

Re: [PATCH v3 1/4] accel/tcg: Invalidate translations when clearing PAGE_EXEC

Quoted 5 lines · Ilya Leoshkevich

How is qemu-user's get_page_addr_code() involved here?

I tried to experiment with it, and while I agree that it looks buggy, it's called only from translation code paths. If we already have a translation block, these code paths are not used.

It's called from tb_lookup too, when we're trying to find an existing TB.

r~

#5 lore raw

Re: [PATCH v3 1/4] accel/tcg: Invalidate translations when clearing PAGE_EXEC

Quoted 8 lines · Richard Henderson
Quoted 6 lines · Ilya Leoshkevich

How is qemu-user's get_page_addr_code() involved here?

I tried to experiment with it, and while I agree that it looks buggy,
it's called only from translation code paths. If we already have a translation block, these code paths are not used.

Quoted 6 lines

It's called from tb_lookup too, when we're trying to find an existing TB.

r~

Oh, I see. I was first worried about direct block chaining with goto_tb, but it turned out that translator_use_goto_tb() prevented it.

tb_lookup() skips get_page_addr_code() if tb is found in tb_jmp_cache. I assume it's a bug?

#6 lore raw

Re: [PATCH v3 1/4] accel/tcg: Invalidate translations when clearing PAGE_EXEC

Quoted 2 lines · Ilya Leoshkevich

tb_lookup() skips get_page_addr_code() if tb is found in tb_jmp_cache. I assume it's a bug?

Yes, I think so. I've rearranged that for other reasons, and so may have inadvertently fix this. I'll post the in-progress work in a moment.

r~