Skip to contentSkip to conversation

target/i386: Make sure that vsyscall's tb->size != 0

PATCH Superseded Ilya Leoshkevich 3 participants 7 messages May 12, 2021 – May 17, 2021

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover target/i386: Make sure that vsyscall's tb->size != 0

This series for the s390-next tree fixes [1]. Patch 1 is the fix, patch 2 is the new test to avoid similar regressions in the future.

[1] https://lists.nongnu.org/archive/html/qemu-devel/2021-05/msg02962.html

Ilya Leoshkevich (2):
target/i386: Make sure that vsyscall's tb->size != 0 tests/tcg/x86_64: add vsyscall smoke test

 target/i386/tcg/translate.c      |  1 +
 tests/tcg/x86_64/Makefile.target |  6 +++++-
 tests/tcg/x86_64/vsyscall.c      | 11 +++++++++++
 3 files changed, 17 insertions(+), 1 deletion(-)
 create mode 100644 tests/tcg/x86_64/vsyscall.c
Signature · 1 line
2.29.2
#1 lore raw

PATCH 1/2 target/i386: Make sure that vsyscall's tb->size != 0

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

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

---
 target/i386/tcg/translate.c | 1 +
 1 file changed, 1 insertion(+)
target/i386/tcg/translate.c +1 -0
index 880bc45561..b5a2b981e2 100644
@@ -8572,6 +8572,7 @@ static void i386_tr_translate_insn(DisasContextBase *dcbase, CPUState *cpu)
      */
     if ((dc->base.pc_next & TARGET_PAGE_MASK) == TARGET_VSYSCALL_PAGE) {
         gen_exception(dc, EXCP_VSYSCALL, dc->base.pc_next);
+        dc->base.pc_next = dc->pc + 1;
         return;
     }
 #endif
Signature · 1 line
2.29.2
Reviewed-by on v1 1/2
#2 lore raw
Quoted 11 lines · Ilya Leoshkevich

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

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

---
  target/i386/tcg/translate.c | 1 +
  1 file changed, 1 insertion(+)

r~

#3 lore raw

PATCH 2/2 tests/tcg/x86_64: add vsyscall smoke test

In reply to Ilya Leoshkevich

Having a small test will prevent trivial regressions in the future.

---
 tests/tcg/x86_64/Makefile.target |  6 +++++-
 tests/tcg/x86_64/vsyscall.c      | 11 +++++++++++
 2 files changed, 16 insertions(+), 1 deletion(-)
 create mode 100644 tests/tcg/x86_64/vsyscall.c
tests/tcg/x86_64/Makefile.target +5 -1
index 20bf96202a..2151ea6302 100644
@@ -3,14 +3,18 @@
 # x86_64 tests - included from tests/tcg/Makefile.target
 #
 # Currently we only build test-x86_64 and test-i386-ssse3 from
-# $(SRC)/tests/tcg/i386/
+# $(SRC_PATH)/tests/tcg/i386/
 #
 
 include $(SRC_PATH)/tests/tcg/i386/Makefile.target
 
+X86_64_TESTS += vsyscall
 TESTS=$(MULTIARCH_TESTS) $(X86_64_TESTS) test-x86_64
 QEMU_OPTS += -cpu max
 
 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
+	$(CC) $(CFLAGS) $< -o $@ $(LDFLAGS)
tests/tcg/x86_64/vsyscall.c new file +11 -0
new file mode 100644
index 0000000000..b9b7f87459
@@ -0,0 +1,11 @@
+#include <stdio.h>
+#include <time.h>
+
+#define VSYSCALL_PAGE 0xffffffffff600000
+#define TIME_OFFSET 0x400
+typedef time_t (*time_func)(time_t *);
+
+int main(void)
+{
+    printf("%ld\n", ((time_func)(VSYSCALL_PAGE + TIME_OFFSET))(NULL));
+}
Signature · 1 line
2.29.2
Reviewed-by on v1 2/2
#4 lore raw

Re: [PATCH 2/2] tests/tcg/x86_64: add vsyscall smoke test

Quoted 8 lines · Ilya Leoshkevich

Having a small test will prevent trivial regressions in the future.

---
  tests/tcg/x86_64/Makefile.target |  6 +++++-
  tests/tcg/x86_64/vsyscall.c      | 11 +++++++++++
  2 files changed, 16 insertions(+), 1 deletion(-)

create mode 100644 tests/tcg/x86_64/vsyscall.c

r~

#5 lore raw

Re: [PATCH 2/2] tests/tcg/x86_64: add vsyscall smoke test

In reply to Ilya Leoshkevich
Quoted 16 lines · On Wed, 12 May 2021 06:02:50 +0200 Ilya Leoshkevich <[email protected]> wrote:

Having a small test will prevent trivial regressions in the future.

---
 tests/tcg/x86_64/Makefile.target |  6 +++++-
 tests/tcg/x86_64/vsyscall.c      | 11 +++++++++++
 2 files changed, 16 insertions(+), 1 deletion(-)
 create mode 100644 tests/tcg/x86_64/vsyscall.c
tests/tcg/x86_64/vsyscall.c new file +1 -0
new file mode 100644
index 0000000000..b9b7f87459
@@ -0,0 +1,11 @@
+#include <stdio.h>

tests/tcg/x86_64/vsyscall.c line 11

Quoted 10 lines
tests/tcg/x86_64/vsyscall.c new file +10 -0
+#include <time.h>
+
+#define VSYSCALL_PAGE 0xffffffffff600000
+#define TIME_OFFSET 0x400
+typedef time_t (*time_func)(time_t *);
+
+int main(void)
+{
+    printf("%ld\n", ((time_func)(VSYSCALL_PAGE + TIME_OFFSET))(NULL));
+}

Fails in https://gitlab.com/cohuck/qemu/-/jobs/1267727559 ("error:
control reaches end of non-void function").

#6 lore raw

Re: [PATCH 2/2] tests/tcg/x86_64: add vsyscall smoke test

Quoted 26 lines · Cornelia Huck
Quoted 23 lines · On Wed, 12 May 2021 06:02:50 +0200 Ilya Leoshkevich <[email protected]> wrote:

Having a small test will prevent trivial regressions in the future.

---
 tests/tcg/x86_64/Makefile.target |  6 +++++- tests/tcg/x86_64/vsyscall.c      | 11 +++++++++++

 2 files changed, 16 insertions(+), 1 deletion(-)

 create mode 100644 tests/tcg/x86_64/vsyscall.c

tests/tcg/x86_64/vsyscall.c new file +7 -0
new file mode 100644
index 0000000000..b9b7f87459
@@ -0,0 +1,11 @@
+#include <stdio.h>
+#include <time.h>
+
+#define VSYSCALL_PAGE 0xffffffffff600000
+#define TIME_OFFSET 0x400
+typedef time_t (*time_func)(time_t *);
+
Quoted 8 lines
Quoted 5 lines
tests/tcg/x86_64/vsyscall.c new file +3 -0
+int main(void)
+{
+    printf("%ld\n", ((time_func)(VSYSCALL_PAGE +

TIME_OFFSET))(NULL));
+}

Fails in https://gitlab.com/cohuck/qemu/-/jobs/1267727559 ("error:
control reaches end of non-void function").

I always thought that omitting return in main() is allowed, but turns out this is a C99-only thing. I should have probably used make docker-all-tests instead of just make check to catch this.

I will send a v2.