[PATCH] target/i386: fix cross-page access bounds check

Vladimir Chebotarev posted 1 patch 20 hours ago
target/i386/tcg/access.c               |  4 +-
tests/tcg/i386/system/meson.build      |  3 +-
tests/tcg/i386/system/x87-page-cross.S | 82 ++++++++++++++++++++++++++
3 files changed, 86 insertions(+), 3 deletions(-)
create mode 100644 tests/tcg/i386/system/x87-page-cross.S
[PATCH] target/i386: fix cross-page access bounds check
Posted by Vladimir Chebotarev 20 hours ago
Fix an unsigned bounds-check underflow that corrupts x87 loads
crossing page boundaries. Add a regression test for the split-page
access.

Tests: i386-softmmu-x87-page-cross
Signed-off-by: Vladimir Chebotarev <vladimir.chebotarev@gmail.com>
---
Regarding #4682, I have a reproducer: on Windows NT 3.1 with Visual
C++ 1.5, an array of 17 structs, each containing six consecutive
double values, produces a garbled value in the 17th struct; adding an
L suffix changes the arena layout and masks the failure.

 target/i386/tcg/access.c               |  4 +-
 tests/tcg/i386/system/meson.build      |  3 +-
 tests/tcg/i386/system/x87-page-cross.S | 82 ++++++++++++++++++++++++++
 3 files changed, 86 insertions(+), 3 deletions(-)
 create mode 100644 tests/tcg/i386/system/x87-page-cross.S

diff --git a/target/i386/tcg/access.c b/target/i386/tcg/access.c
index 97e3f0e756..832c96ac4a 100644
--- a/target/i386/tcg/access.c
+++ b/target/i386/tcg/access.c
@@ -65,10 +65,10 @@ static void *access_ptr(X86Access *ac, vaddr addr,
unsigned len)
     }

 #ifdef CONFIG_USER_ONLY
-    assert(offset <= ac->size1 - len);
+    assert(offset <= ac->size1 && len <= ac->size1 - offset);
     return ac->haddr1 + offset;
 #else
-    if (likely(offset <= ac->size1 - len)) {
+    if (likely(offset <= ac->size1 && len <= ac->size1 - offset)) {
         return ac->haddr1 + offset;
     }
     assert(offset <= ac->size - len);
diff --git a/tests/tcg/i386/system/meson.build
b/tests/tcg/i386/system/meson.build
index 44e86a12cd..de8a1e1ad2 100644
--- a/tests/tcg/i386/system/meson.build
+++ b/tests/tcg/i386/system/meson.build
@@ -35,7 +35,8 @@ foreach t: tcg_tests['multiarch-softmmu']['tests']
 endforeach

 tests += {
-  'wraparound.S': { 'cflags': cflags, 'qemu_args': ['-m', '4G'] +
qemu_def_args }
+  'wraparound.S': { 'cflags': cflags, 'qemu_args': ['-m', '4G'] +
qemu_def_args },
+  'x87-page-cross.S': { 'cflags': cflags, 'qemu_args': ['-m', '16M']
+ qemu_def_args }
 }

 if 'qemu-system-i386' in emulators
diff --git a/tests/tcg/i386/system/x87-page-cross.S
b/tests/tcg/i386/system/x87-page-cross.S
new file mode 100644
index 0000000000..fe21e73628
--- /dev/null
+++ b/tests/tcg/i386/system/x87-page-cross.S
@@ -0,0 +1,82 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+/*
+ * Regression test for an x87 80-bit load that crosses a guest page.
+ *
+ * The two virtual pages are deliberately mapped to non-adjacent physical
+ * pages.  This prevents X86Access from treating the host pointers as one
+ * contiguous range and exercises the split-access fallback in access_ptr().
+ */
+
+        .code32
+        .section .text
+
+.globl  main
+main:
+        /*
+         * Build an identity map for the first 4 MiB and a separate mapping
+         * for 0x400000..0x401fff:
+         *
+         *   0x400000 -> 0x00300000
+         *   0x401000 -> 0x00500000
+         *
+         * The test value starts at virtual address 0x400ffc.
+         */
+        xorl    %eax, %eax
+        movl    $0x200000, %edi
+        movl    $3072, %ecx
+        rep
+        stosl
+
+        movl    $0x201000, %edi
+        movl    $0x00000003, %eax
+        movl    $1024, %ecx
+.Lfill_identity:
+        stosl
+        addl    $0x1000, %eax
+        loop    .Lfill_identity
+
+        movl    $0x00201003, 0x200000
+        movl    $0x00202003, 0x200004
+        movl    $0x00300003, 0x202000
+        movl    $0x00500003, 0x202004
+
+        /* Expected x87 value: significand 0xc000000012345678, exp 0x3fff. */
+        movl    $0x12345678, 0x00300ffc
+        movl    $0xc0000000, 0x00500000
+        movw    $0x3fff, 0x00500004
+
+        /* Bytes read past physical page 0x00300000 must not be used. */
+        movl    $0x80000000, 0x00301000
+
+        movl    $0x00200000, %eax
+        movl    %eax, %cr3
+
+        movl    %cr0, %eax
+        orl     $0x80000000, %eax
+        movl    %eax, %cr0
+
+        fninit
+        fldt    0x400ffc
+        fstpt   result
+
+        movl    result, %eax
+        cmpl    $0x12345678, %eax
+        jne     .Lfail
+        movl    result + 4, %eax
+        cmpl    $0xc0000000, %eax
+        jne     .Lfail
+        movzwl  result + 8, %eax
+        cmpl    $0x3fff, %eax
+        jne     .Lfail
+
+        xorl    %eax, %eax
+        ret
+
+.Lfail:
+        movl    $1, %eax
+        ret
+
+        .section .bss
+        .align  4
+result:
+        .space  10
--
2.47.1