Skip to contentSkip to conversation

fuzz: update x86emul fuzzer

PATCH Superseded Wei Liu 4 participants 24 messages Jan 25, 2017 – Jan 30, 2017

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Wei Liu

author
#0 lore raw

cover fuzz: update x86emul fuzzer

Wei Liu (7):
x86emul/test: remove extraneous commas in debug messages
x86_emulate: lift a bunch of macros to header file
x86: extract macros to x86-defns.h x86emul/test: use x86-defns.h
fuzz/x86emul: update fuzzer
fuzz/x86emul: print out minimal input size
fuzz: update README.afl example

 tools/fuzz/README.afl                              |   5 +-
 .../afl-x86-insn-emulator-fuzzer.c                 |   8 +
 .../x86-insn-emulator-fuzzer.c                     | 658 +++++++++++++++++++--
 tools/tests/x86_emulator/test_x86_emulator.c       |  17 +-
 tools/tests/x86_emulator/x86_emulate.h             |   7 +-
 xen/arch/x86/x86_emulate/x86_emulate.c             |  85 ---
 xen/arch/x86/x86_emulate/x86_emulate.h             |  86 +++
 xen/include/asm-x86/processor.h                    | 121 +---
 xen/include/asm-x86/x86-defns.h                    | 125 ++++
 9 files changed, 829 insertions(+), 283 deletions(-)
 create mode 100644 xen/include/asm-x86/x86-defns.h
Signature · 7 lines
2.11.0


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
author
#1 lore raw

PATCH 1/7 x86emul/test: remove extraneous commas in debug messages

---
 tools/tests/x86_emulator/test_x86_emulator.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
tools/tests/x86_emulator/test_x86_emulator.c +4 -4
index 924fd36f18..e86369ffe7 100644
@@ -42,7 +42,7 @@ static int read(
     struct x86_emulate_ctxt *ctxt)
 {
     if ( verbose )
-        printf("** %s(%u, %p,, %u,)\n", __func__, seg, (void *)offset, bytes);
+        printf("** %s(%u, %p, %u)\n", __func__, seg, (void *)offset, bytes);
 
     switch ( seg )
     {
@@ -97,7 +97,7 @@ static int fetch(
     struct x86_emulate_ctxt *ctxt)
 {
     if ( verbose )
-        printf("** %s(%u, %p,, %u,)\n", __func__, seg, (void *)offset, bytes);
+        printf("** %s(%u, %p, %u)\n", __func__, seg, (void *)offset, bytes);
 
     memcpy(p_data, (void *)offset, bytes);
     return X86EMUL_OKAY;
@@ -111,7 +111,7 @@ static int write(
     struct x86_emulate_ctxt *ctxt)
 {
     if ( verbose )
-        printf("** %s(%u, %p,, %u,)\n", __func__, seg, (void *)offset, bytes);
+        printf("** %s(%u, %p, %u)\n", __func__, seg, (void *)offset, bytes);
 
     if ( !is_x86_user_segment(seg) )
         return X86EMUL_UNHANDLEABLE;
@@ -128,7 +128,7 @@ static int cmpxchg(
     struct x86_emulate_ctxt *ctxt)
 {
     if ( verbose )
-        printf("** %s(%u, %p,, %u,)\n", __func__, seg, (void *)offset, bytes);
+        printf("** %s(%u, %p, %u)\n", __func__, seg, (void *)offset, bytes);
 
     if ( !is_x86_user_segment(seg) )
         return X86EMUL_UNHANDLEABLE;
Signature · 7 lines
2.11.0


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
#2 lore raw

Re: [PATCH 1/7] x86emul/test: remove extraneous commas in debug messages

tools/tests/x86_emulator/test_x86_emulator.c static int read( · line 45

Quoted 9 lines
Quoted 1 line
Quoted 1 line · On 25.01.17 at 16:44, <[email protected]> wrote:
tools/tests/x86_emulator/test_x86_emulator.c +1 -1
@@ -42,7 +42,7 @@ static int read(
     struct x86_emulate_ctxt *ctxt)
 {
     if ( verbose )
-        printf("** %s(%u, %p,, %u,)\n", __func__, seg, (void *)offset, bytes);
+        printf("** %s(%u, %p, %u)\n", __func__, seg, (void *)offset, bytes);

Back when these got added, Andrew did explain why he put them there.

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

author
#3 lore raw

Re: [PATCH 1/7] x86emul/test: remove extraneous commas in debug messages

Quoted 13 lines · Jan Beulich

tools/tests/x86_emulator/test_x86_emulator.c static int read( · line 45

Quoted 9 lines
Quoted 1 line
Quoted 1 line · On 25.01.17 at 16:44, <[email protected]> wrote:
tools/tests/x86_emulator/test_x86_emulator.c +1 -1
@@ -42,7 +42,7 @@ static int read(
     struct x86_emulate_ctxt *ctxt)
 {
     if ( verbose )
-        printf("** %s(%u, %p,, %u,)\n", __func__, seg, (void *)offset, bytes);
+        printf("** %s(%u, %p, %u)\n", __func__, seg, (void *)offset, bytes);

Back when these got added, Andrew did explain why he put them there.

Eh, OK. If there is a reason for this, I will just drop this patch. It's not essential anyway.

It just looks rather odd to me to have so many extraneous commas. ;-)

Wei.

Quoted 2 lines

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

#4 lore raw

Re: [PATCH 1/7] x86emul/test: remove extraneous commas in debug messages

Quoted 13 lines · Wei Liu
Quoted 12 lines · Jan Beulich

tools/tests/x86_emulator/test_x86_emulator.c static int read( · line 45

Quoted 9 lines
Quoted 1 line
Quoted 1 line · On 25.01.17 at 16:44, <[email protected]> wrote:
tools/tests/x86_emulator/test_x86_emulator.c +1 -1
@@ -42,7 +42,7 @@ static int read(
     struct x86_emulate_ctxt *ctxt)
 {
     if ( verbose )
-        printf("** %s(%u, %p,, %u,)\n", __func__, seg, (void *)offset, bytes);
+        printf("** %s(%u, %p, %u)\n", __func__, seg, (void *)offset, bytes);

Back when these got added, Andrew did explain why he put them there.

Quoted 4 lines

Eh, OK. If there is a reason for this, I will just drop this patch. It's not essential anyway.

It just looks rather odd to me to have so many extraneous commas. ;-)

This is my debugging shorthand, which associates values with the positional parameters.

In this case it is quite trivial to read, but debugging elsewhere in the hypervisor is rather more complicated (some of the pagetable code particular takes large numbers of parameters).

~Andrew

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

author
#5 lore raw

PATCH 2/7 x86_emulate: lift a bunch of macros to header file

In reply to Wei Liu

Some of them can be shared between hypervisor and userspace fuzzing / test code. Instead of lifting the ones as we need, lift them all.

And then remove the ones in test program.

---
 tools/tests/x86_emulator/test_x86_emulator.c |  9 ---
 xen/arch/x86/x86_emulate/x86_emulate.c       | 85 ---------------------------
 xen/arch/x86/x86_emulate/x86_emulate.h       | 86 ++++++++++++++++++++++++++++
 3 files changed, 86 insertions(+), 94 deletions(-)
tools/tests/x86_emulator/test_x86_emulator.c +0 -9
index e86369ffe7..579b28c687 100644
@@ -23,15 +23,6 @@ static const struct {
 #endif
 };
 
-/* EFLAGS bit definitions. */
-#define EFLG_OF (1<<11)
-#define EFLG_DF (1<<10)
-#define EFLG_SF (1<<7)
-#define EFLG_ZF (1<<6)
-#define EFLG_AF (1<<4)
-#define EFLG_PF (1<<2)
-#define EFLG_CF (1<<0)
-
 static unsigned int bytes_read;
 
 static int read(
xen/arch/x86/x86_emulate/x86_emulate.c +0 -85

91 lines not on this page · raw message

xen/arch/x86/x86_emulate/x86_emulate.h +86 -0

92 lines not on this page · raw message

Signature · 7 lines
2.11.0


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
#6 lore raw

Re: [PATCH 2/7] x86_emulate: lift a bunch of macros to header file

Quoted 3 lines
Quoted 1 line
Quoted 1 line · On 25.01.17 at 16:44, <[email protected]> wrote:

Some of them can be shared between hypervisor and userspace fuzzing / test code. Instead of lifting the ones as we need, lift them all.

While I appreciate the intention, I don't think we should start having the hypervisor build deal with potential (or actual, but differently named) duplicate defines. In particular, despite its location in the tree, xen/arch/x86/x86_emulate/x86_emulate.h is a header included by various other source files, representing the interface to the emulator. The definitions you move there aren't part of that interface. I think Andrew and I had agreed (Andrew, correct me if I'm wrong) that we would want to eliminate that duplication, but then fully, i.e. also covering what processor.h and msr-index.h (and maybe a few more headers) define. I.e. we'd like to introduce a header of x86 arch definitions which can be used in both the hypervisor and the test harness builds.

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

author
#7 lore raw

Re: [PATCH 2/7] x86_emulate: lift a bunch of macros to header file

Quoted 4 lines · Jan Beulich
Quoted 3 lines
Quoted 1 line
Quoted 1 line · On 25.01.17 at 16:44, <[email protected]> wrote:

Some of them can be shared between hypervisor and userspace fuzzing / test code. Instead of lifting the ones as we need, lift them all.

Quoted 13 lines

While I appreciate the intention, I don't think we should start having the hypervisor build deal with potential (or actual, but differently named) duplicate defines. In particular, despite its location in the tree, xen/arch/x86/x86_emulate/x86_emulate.h is a header included by various other source files, representing the interface to the emulator. The definitions you move there aren't part of that interface. I think Andrew and I had agreed (Andrew, correct me if I'm wrong) that we would want to eliminate that duplication, but then fully, i.e. also covering what processor.h and msr-index.h (and maybe a few more headers) define. I.e. we'd like to introduce a header of x86 arch definitions which can be used in both the hypervisor and the test harness builds.

I see. Let me go through all those flags and change x86emul to use the ones in xen headers.

Wei.

Quoted 2 lines

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

author
#8 lore raw

PATCH 3/7 x86: extract macros to x86-defns.h

In reply to Wei Liu

... so that they can be used by userspace x86 instruction emulator test program and fuzzer as well.

No functional change.

---
 xen/include/asm-x86/processor.h | 121 +-------------------------------------
 xen/include/asm-x86/x86-defns.h | 125 ++++++++++++++++++++++++++++++++++++++++
 2 files changed, 126 insertions(+), 120 deletions(-)
 create mode 100644 xen/include/asm-x86/x86-defns.h
xen/include/asm-x86/processor.h +1 -120
index 4da9c193e0..c86044f5f8 100644
@@ -16,126 +16,7 @@
 #include <asm/desc.h>
 #endif
 
-/*
- * CPU vendor IDs
- */
-#define X86_VENDOR_INTEL 0
-#define X86_VENDOR_AMD 1
-#define X86_VENDOR_CENTAUR 2
-#define X86_VENDOR_NUM 3
-#define X86_VENDOR_UNKNOWN 0xff
-
-/*
- * EFLAGS bits
- */
-#define X86_EFLAGS_CF	0x00000001 /* Carry Flag */
-#define X86_EFLAGS_MBS	0x00000002 /* Resvd bit */
-#define X86_EFLAGS_PF	0x00000004 /* Parity Flag */
-#define X86_EFLAGS_AF	0x00000010 /* Auxillary carry Flag */
-#define X86_EFLAGS_ZF	0x00000040 /* Zero Flag */
-#define X86_EFLAGS_SF	0x00000080 /* Sign Flag */
-#define X86_EFLAGS_TF	0x00000100 /* Trap Flag */
-#define X86_EFLAGS_IF	0x00000200 /* Interrupt Flag */
-#define X86_EFLAGS_DF	0x00000400 /* Direction Flag */
-#define X86_EFLAGS_OF	0x00000800 /* Overflow Flag */
-#define X86_EFLAGS_IOPL	0x00003000 /* IOPL mask */
-#define X86_EFLAGS_NT	0x00004000 /* Nested Task */
-#define X86_EFLAGS_RF	0x00010000 /* Resume Flag */
-#define X86_EFLAGS_VM	0x00020000 /* Virtual Mode */
-#define X86_EFLAGS_AC	0x00040000 /* Alignment Check */
-#define X86_EFLAGS_VIF	0x00080000 /* Virtual Interrupt Flag */
-#define X86_EFLAGS_VIP	0x00100000 /* Virtual Interrupt Pending */
-#define X86_EFLAGS_ID	0x00200000 /* CPUID detection flag */
-
-#define X86_EFLAGS_ARITH_MASK                          \
-    (X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |   \
-     X86_EFLAGS_ZF | X86_EFLAGS_SF | X86_EFLAGS_OF)
-
-/*
- * Intel CPU flags in CR0
- */
-#define X86_CR0_PE              0x00000001 /* Enable Protected Mode    (RW) */
-#define X86_CR0_MP              0x00000002 /* Monitor Coprocessor      (RW) */
-#define X86_CR0_EM              0x00000004 /* Require FPU Emulation    (RO) */
-#define X86_CR0_TS              0x00000008 /* Task Switched            (RW) */
-#define X86_CR0_ET              0x00000010 /* Extension type           (RO) */
-#define X86_CR0_NE              0x00000020 /* Numeric Error Reporting  (RW) */
-#define X86_CR0_WP              0x00010000 /* Supervisor Write Protect (RW) */
-#define X86_CR0_AM              0x00040000 /* Alignment Checking       (RW) */
-#define X86_CR0_NW              0x20000000 /* Not Write-Through        (RW) */
-#define X86_CR0_CD              0x40000000 /* Cache Disable            (RW) */
-#define X86_CR0_PG              0x80000000 /* Paging                   (RW) */
-
-/*
- * Intel CPU features in CR4
- */
-#define X86_CR4_VME        0x00000001 /* enable vm86 extensions */
-#define X86_CR4_PVI        0x00000002 /* virtual interrupts flag enable */
-#define X86_CR4_TSD        0x00000004 /* disable time stamp at ipl 3 */
-#define X86_CR4_DE         0x00000008 /* enable debugging extensions */
-#define X86_CR4_PSE        0x00000010 /* enable page size extensions */
-#define X86_CR4_PAE        0x00000020 /* enable physical address extensions */
-#define X86_CR4_MCE        0x00000040 /* Machine check enable */
-#define X86_CR4_PGE        0x00000080 /* enable global pages */
-#define X86_CR4_PCE        0x00000100 /* enable performance counters at ipl 3 */
-#define X86_CR4_OSFXSR     0x00000200 /* enable fast FPU save and restore */
-#define X86_CR4_OSXMMEXCPT 0x00000400 /* enable unmasked SSE exceptions */
-#define X86_CR4_VMXE       0x00002000 /* enable VMX */
-#define X86_CR4_SMXE       0x00004000 /* enable SMX */
-#define X86_CR4_FSGSBASE   0x00010000 /* enable {rd,wr}{fs,gs}base */
-#define X86_CR4_PCIDE      0x00020000 /* enable PCID */
-#define X86_CR4_OSXSAVE    0x00040000 /* enable XSAVE/XRSTOR */
-#define X86_CR4_SMEP       0x00100000 /* enable SMEP */
-#define X86_CR4_SMAP       0x00200000 /* enable SMAP */
-#define X86_CR4_PKE        0x00400000 /* enable PKE */
-
-/*
- * Trap/fault mnemonics.
- */
-#define TRAP_divide_error      0
-#define TRAP_debug             1
-#define TRAP_nmi               2
-#define TRAP_int3              3
-#define TRAP_overflow          4
-#define TRAP_bounds            5
-#define TRAP_invalid_op        6
-#define TRAP_no_device         7
-#define TRAP_double_fault      8
-#define TRAP_copro_seg         9
-#define TRAP_invalid_tss      10
-#define TRAP_no_segment       11
-#define TRAP_stack_error      12
-#define TRAP_gp_fault         13
-#define TRAP_page_fault       14
-#define TRAP_spurious_int     15
-#define TRAP_copro_error      16
-#define TRAP_alignment_check  17
-#define TRAP_machine_check    18
-#define TRAP_simd_error       19
-#define TRAP_virtualisation   20
-#define TRAP_nr               32
-
-#define TRAP_HAVE_EC                                                    \
-    ((1u << TRAP_double_fault) | (1u << TRAP_invalid_tss) |             \
-     (1u << TRAP_no_segment) | (1u << TRAP_stack_error) |               \
-     (1u << TRAP_gp_fault) | (1u << TRAP_page_fault) |                  \
-     (1u << TRAP_alignment_check))
-
-/* Set for entry via SYSCALL. Informs return code to use SYSRETQ not IRETQ. */
-/* NB. Same as VGCF_in_syscall. No bits in common with any other TRAP_ defn. */
-#define TRAP_syscall         256
-
-/* Boolean return code: the reason for a fault has been fixed. */
-#define EXCRET_fault_fixed 1
-
-/* 'trap_bounce' flags values */
-#define TBF_EXCEPTION          1
-#define TBF_EXCEPTION_ERRCODE  2
-#define TBF_INTERRUPT          8
-
-/* 'arch_vcpu' flags values */
-#define _TF_kernel_mode        0
-#define TF_kernel_mode         (1<<_TF_kernel_mode)
+#include "x86-defns.h"
 
 /* #PF error code values. */
 #define PFEC_page_present   (_AC(1,U) << 0)
xen/include/asm-x86/x86-defns.h new file +125 -0

125 lines not on this page · raw message

Signature · 7 lines
2.11.0


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
#9 lore raw

Re: [PATCH 3/7] x86: extract macros to x86-defns.h

Quoted 3 lines
Quoted 1 line
Quoted 1 line · On 25.01.17 at 16:44, <[email protected]> wrote:

... so that they can be used by userspace x86 instruction emulator test program and fuzzer as well.

Ah, here we go. This should be patch 2 then imo.

Quoted 4 lines
xen/include/asm-x86/x86-defns.h new file +1 -0
@@ -0,0 +1,125 @@
+#ifndef __XEN_X86_DEFNS_H__

xen/include/asm-x86/x86-defns.h line 11

Quoted 10 lines
xen/include/asm-x86/x86-defns.h new file +10 -0
+#define __XEN_X86_DEFNS_H__
+
+/*
+ * CPU vendor IDs
+ */
+#define X86_VENDOR_INTEL 0
+#define X86_VENDOR_AMD 1
+#define X86_VENDOR_CENTAUR 2
+#define X86_VENDOR_NUM 3
+#define X86_VENDOR_UNKNOWN 0xff

Let's strictly separate things: These aren't architectural definitions, so should - if we really mean to share them - go into another header. I'm not sure though whether sharing these is all that useful.

Quoted 5 lines
xen/include/asm-x86/x86-defns.h new file +5 -0
+/* Set for entry via SYSCALL. Informs return code to use SYSRETQ not IRETQ. */
+/* NB. Same as VGCF_in_syscall. No bits in common with any other TRAP_ defn. */
+#define TRAP_syscall         256
+
+/* Boolean return code: the reason for a fault has been fixed. */

xen/include/asm-x86/x86-defns.h line 123

Quoted 10 lines
xen/include/asm-x86/x86-defns.h new file +10 -0
+#define EXCRET_fault_fixed 1
+
+/* 'trap_bounce' flags values */
+#define TBF_EXCEPTION          1
+#define TBF_EXCEPTION_ERRCODE  2
+#define TBF_INTERRUPT          8
+
+/* 'arch_vcpu' flags values */
+#define _TF_kernel_mode        0
+#define TF_kernel_mode         (1<<_TF_kernel_mode)

None of these should be needed by the emulator, nor is any of this dictated by the architecture, so they all should remain in processor.h.

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

author
#10 lore raw

Re: [PATCH 3/7] x86: extract macros to x86-defns.h

Quoted 16 lines · Jan Beulich
Quoted 3 lines
Quoted 1 line
Quoted 1 line · On 25.01.17 at 16:44, <[email protected]> wrote:

... so that they can be used by userspace x86 instruction emulator test program and fuzzer as well.

Ah, here we go. This should be patch 2 then imo.

Quoted 10 lines
xen/include/asm-x86/x86-defns.h new file +7 -0
@@ -0,0 +1,125 @@
+#ifndef __XEN_X86_DEFNS_H__
+#define __XEN_X86_DEFNS_H__
+
+/*
+ * CPU vendor IDs
+ */
+#define X86_VENDOR_INTEL 0
Quoted 8 lines

xen/include/asm-x86/x86-defns.h line 11

Quoted 4 lines
xen/include/asm-x86/x86-defns.h new file +4 -0
+#define X86_VENDOR_AMD 1
+#define X86_VENDOR_CENTAUR 2
+#define X86_VENDOR_NUM 3
+#define X86_VENDOR_UNKNOWN 0xff

Let's strictly separate things: These aren't architectural definitions, so should - if we really mean to share them - go into another header. I'm not sure though whether sharing these is all that useful.

Actually I don't think these are needed.

I moved them because there were duplicates in x86emul/test. But after having a closer look those duplicates are not used.

Quoted 11 lines
Quoted 11 lines
xen/include/asm-x86/x86-defns.h new file +10 -0
+/* Set for entry via SYSCALL. Informs return code to use SYSRETQ not IRETQ. */
+/* NB. Same as VGCF_in_syscall. No bits in common with any other TRAP_ defn. */
+#define TRAP_syscall         256
+
+/* Boolean return code: the reason for a fault has been fixed. */
+#define EXCRET_fault_fixed 1
+
+/* 'trap_bounce' flags values */
+#define TBF_EXCEPTION          1
+#define TBF_EXCEPTION_ERRCODE  2
Quoted 9 lines

xen/include/asm-x86/x86-defns.h line 123

Quoted 5 lines
xen/include/asm-x86/x86-defns.h new file +5 -0
+#define TBF_INTERRUPT          8
+
+/* 'arch_vcpu' flags values */
+#define _TF_kernel_mode        0
+#define TF_kernel_mode         (1<<_TF_kernel_mode)

None of these should be needed by the emulator, nor is any of this dictated by the architecture, so they all should remain in processor.h.

OK.

Wei.

Quoted 2 lines

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

author
#11 lore raw

Re: [PATCH 3/7] x86: extract macros to x86-defns.h

Quoted 26 lines · Wei Liu
Quoted 16 lines · Jan Beulich
Quoted 3 lines
Quoted 1 line
Quoted 1 line · On 25.01.17 at 16:44, <[email protected]> wrote:

... so that they can be used by userspace x86 instruction emulator test program and fuzzer as well.

Ah, here we go. This should be patch 2 then imo.

Quoted 10 lines
xen/include/asm-x86/x86-defns.h new file +7 -0
@@ -0,0 +1,125 @@
+#ifndef __XEN_X86_DEFNS_H__
+#define __XEN_X86_DEFNS_H__
+
+/*
+ * CPU vendor IDs
+ */
+#define X86_VENDOR_INTEL 0
Quoted 8 lines

xen/include/asm-x86/x86-defns.h line 11

Quoted 4 lines
xen/include/asm-x86/x86-defns.h new file +4 -0
+#define X86_VENDOR_AMD 1
+#define X86_VENDOR_CENTAUR 2
+#define X86_VENDOR_NUM 3
+#define X86_VENDOR_UNKNOWN 0xff

Let's strictly separate things: These aren't architectural definitions, so should - if we really mean to share them - go into another header. I'm not sure though whether sharing these is all that useful.

Quoted 4 lines

Actually I don't think these are needed.

I moved them because there were duplicates in x86emul/test. But after having a closer look those duplicates are not used.

Spoke too soon. I only grepped for X86_VENDOR_INTEL and came to that conclusion. Actually X86_VENDOR_AMD is used in x86_emualte/x86_emualte.c and X86_VENDOR_UNKNOWN in test_x86_emulate.c.

I propose we move these to x86-vendors.h. What do you think?

Wei.

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

#12 lore raw

Re: [PATCH 3/7] x86: extract macros to x86-defns.h

Quoted 30 lines
Quoted 1 line
Quoted 1 line · On 30.01.17 at 13:46, <[email protected]> wrote:
Quoted 28 lines · Wei Liu
Quoted 16 lines · Jan Beulich
Quoted 3 lines
Quoted 1 line
Quoted 1 line · On 25.01.17 at 16:44, <[email protected]> wrote:

... so that they can be used by userspace x86 instruction emulator test program and fuzzer as well.

Ah, here we go. This should be patch 2 then imo.

Quoted 10 lines
xen/include/asm-x86/x86-defns.h new file +7 -0
@@ -0,0 +1,125 @@
+#ifndef __XEN_X86_DEFNS_H__
+#define __XEN_X86_DEFNS_H__
+
+/*
+ * CPU vendor IDs
+ */
+#define X86_VENDOR_INTEL 0
Quoted 8 lines

xen/include/asm-x86/x86-defns.h line 11

Quoted 4 lines
xen/include/asm-x86/x86-defns.h new file +4 -0
+#define X86_VENDOR_AMD 1
+#define X86_VENDOR_CENTAUR 2
+#define X86_VENDOR_NUM 3
+#define X86_VENDOR_UNKNOWN 0xff

Let's strictly separate things: These aren't architectural definitions, so should - if we really mean to share them - go into another header. I'm not sure though whether sharing these is all that useful.

Actually I don't think these are needed.

Quoted 8 lines
Quoted 2 lines

I moved them because there were duplicates in x86emul/test. But after having a closer look those duplicates are not used.

Spoke too soon. I only grepped for X86_VENDOR_INTEL and came to that conclusion. Actually X86_VENDOR_AMD is used in x86_emualte/x86_emualte.c and X86_VENDOR_UNKNOWN in test_x86_emulate.c.

I propose we move these to x86-vendors.h. What do you think?

Well, if you really think moving them is useful, then the header name would be fine. But as said, I don't really see the point.

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

author
#13 lore raw

Re: [PATCH 3/7] x86: extract macros to x86-defns.h

Quoted 33 lines · Jan Beulich
Quoted 33 lines
Quoted 1 line
Quoted 1 line · On 30.01.17 at 13:46, <[email protected]> wrote:
Quoted 30 lines · Wei Liu
Quoted 16 lines · Jan Beulich
Quoted 3 lines
Quoted 1 line
Quoted 1 line · On 25.01.17 at 16:44, <[email protected]> wrote:

... so that they can be used by userspace x86 instruction emulator test program and fuzzer as well.

Ah, here we go. This should be patch 2 then imo.

Quoted 10 lines
xen/include/asm-x86/x86-defns.h new file +7 -0
@@ -0,0 +1,125 @@
+#ifndef __XEN_X86_DEFNS_H__
+#define __XEN_X86_DEFNS_H__
+
+/*
+ * CPU vendor IDs
+ */
+#define X86_VENDOR_INTEL 0
Quoted 8 lines

xen/include/asm-x86/x86-defns.h line 11

Quoted 4 lines
xen/include/asm-x86/x86-defns.h new file +4 -0
+#define X86_VENDOR_AMD 1
+#define X86_VENDOR_CENTAUR 2
+#define X86_VENDOR_NUM 3
+#define X86_VENDOR_UNKNOWN 0xff

Let's strictly separate things: These aren't architectural definitions, so should - if we really mean to share them - go into another header. I'm not sure though whether sharing these is all that useful.

Actually I don't think these are needed.

I moved them because there were duplicates in x86emul/test. But after having a closer look those duplicates are not used.

Quoted 9 lines
Quoted 5 lines

Spoke too soon. I only grepped for X86_VENDOR_INTEL and came to that conclusion. Actually X86_VENDOR_AMD is used in x86_emualte/x86_emualte.c and X86_VENDOR_UNKNOWN in test_x86_emulate.c.

I propose we move these to x86-vendors.h. What do you think?

Well, if you really think moving them is useful, then the header name would be fine. But as said, I don't really see the point.

Yes, I think that's going to be useful because that allows emulator compiles in both xen and userspace harness. The other route is to disentangle some more code to achieve the same effect. But since you don't object to the proposed approach, I would avoid the more complicated route.

Wei.

Quoted 2 lines

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

#14 lore raw

Re: [PATCH 3/7] x86: extract macros to x86-defns.h

Quoted 41 lines
Quoted 1 line
Quoted 1 line · On 30.01.17 at 15:17, <[email protected]> wrote:
Quoted 39 lines · Jan Beulich
Quoted 38 lines
Quoted 1 line
Quoted 1 line · On 30.01.17 at 13:46, <[email protected]> wrote:
Quoted 26 lines · Wei Liu
Quoted 16 lines · Jan Beulich
Quoted 3 lines
Quoted 1 line
Quoted 1 line · On 25.01.17 at 16:44, <[email protected]> wrote:

... so that they can be used by userspace x86 instruction emulator test program and fuzzer as well.

Ah, here we go. This should be patch 2 then imo.

Quoted 10 lines
xen/include/asm-x86/x86-defns.h new file +7 -0
@@ -0,0 +1,125 @@
+#ifndef __XEN_X86_DEFNS_H__
+#define __XEN_X86_DEFNS_H__
+
+/*
+ * CPU vendor IDs
+ */
+#define X86_VENDOR_INTEL 0
Quoted 8 lines

xen/include/asm-x86/x86-defns.h line 11

Quoted 4 lines
xen/include/asm-x86/x86-defns.h new file +4 -0
+#define X86_VENDOR_AMD 1
+#define X86_VENDOR_CENTAUR 2
+#define X86_VENDOR_NUM 3
+#define X86_VENDOR_UNKNOWN 0xff

Let's strictly separate things: These aren't architectural definitions, so should - if we really mean to share them - go into another header. I'm not sure though whether sharing these is all that useful.

Quoted 4 lines

Actually I don't think these are needed.

I moved them because there were duplicates in x86emul/test. But after having a closer look those duplicates are not used.

Spoke too soon. I only grepped for X86_VENDOR_INTEL and came to that conclusion. Actually X86_VENDOR_AMD is used in x86_emualte/x86_emualte.c and X86_VENDOR_UNKNOWN in test_x86_emulate.c.

I propose we move these to x86-vendors.h. What do you think?

Quoted 8 lines
Quoted 2 lines

Well, if you really think moving them is useful, then the header name would be fine. But as said, I don't really see the point.

Yes, I think that's going to be useful because that allows emulator compiles in both xen and userspace harness. The other route is to disentangle some more code to achieve the same effect. But since you don't object to the proposed approach, I would avoid the more complicated route.

Just to clarify - the main reason for me not really liking the idea is that (other than the architecture defines) the X86_VENDOR_* values have no need to be in sync (regarding their values) in hypervisor and test harness. All we need is for constants of that name (with whatever values) to be in scope. And that's being achieved by current code already.

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

author
#15 lore raw

Re: [PATCH 3/7] x86: extract macros to x86-defns.h

Quoted 50 lines · Jan Beulich
Quoted 41 lines
Quoted 1 line
Quoted 1 line · On 30.01.17 at 15:17, <[email protected]> wrote:
Quoted 39 lines · Jan Beulich
Quoted 38 lines
Quoted 1 line
Quoted 1 line · On 30.01.17 at 13:46, <[email protected]> wrote:
Quoted 26 lines · Wei Liu
Quoted 16 lines · Jan Beulich
Quoted 3 lines
Quoted 1 line
Quoted 1 line · On 25.01.17 at 16:44, <[email protected]> wrote:

... so that they can be used by userspace x86 instruction emulator test program and fuzzer as well.

Ah, here we go. This should be patch 2 then imo.

Quoted 10 lines
xen/include/asm-x86/x86-defns.h new file +7 -0
@@ -0,0 +1,125 @@
+#ifndef __XEN_X86_DEFNS_H__
+#define __XEN_X86_DEFNS_H__
+
+/*
+ * CPU vendor IDs
+ */
+#define X86_VENDOR_INTEL 0
Quoted 8 lines

xen/include/asm-x86/x86-defns.h line 11

Quoted 4 lines
xen/include/asm-x86/x86-defns.h new file +4 -0
+#define X86_VENDOR_AMD 1
+#define X86_VENDOR_CENTAUR 2
+#define X86_VENDOR_NUM 3
+#define X86_VENDOR_UNKNOWN 0xff

Let's strictly separate things: These aren't architectural definitions, so should - if we really mean to share them - go into another header. I'm not sure though whether sharing these is all that useful.

Quoted 4 lines

Actually I don't think these are needed.

I moved them because there were duplicates in x86emul/test. But after having a closer look those duplicates are not used.

Spoke too soon. I only grepped for X86_VENDOR_INTEL and came to that conclusion. Actually X86_VENDOR_AMD is used in x86_emualte/x86_emualte.c and X86_VENDOR_UNKNOWN in test_x86_emulate.c.

I propose we move these to x86-vendors.h. What do you think?

Quoted 8 lines
Quoted 2 lines

Well, if you really think moving them is useful, then the header name would be fine. But as said, I don't really see the point.

Yes, I think that's going to be useful because that allows emulator compiles in both xen and userspace harness. The other route is to disentangle some more code to achieve the same effect. But since you don't object to the proposed approach, I would avoid the more complicated route.

Quoted 7 lines

Just to clarify - the main reason for me not really liking the idea is that (other than the architecture defines) the X86_VENDOR_* values have no need to be in sync (regarding their values) in hypervisor and test harness. All we need is for constants of that name (with whatever values) to be in scope. And that's being achieved by current code already.

That's true. And I understand that.

My idea is that I want to avoid unnecessary duplication to reduce the distraction when one needs to modify userspace test harness. If there is already definitions in xen, I will just use that.

Wei.

Quoted 2 lines

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

author
#16 lore raw

PATCH 4/7 x86emul/test: use x86-defns.h

In reply to Wei Liu
---
 tools/tests/x86_emulator/x86_emulate.h | 7 ++-----
 1 file changed, 2 insertions(+), 5 deletions(-)
tools/tests/x86_emulator/x86_emulate.h +2 -5
index 3a6badee46..3a1d8ccad2 100644
@@ -6,6 +6,8 @@
 #include <string.h>
 #include <xen/xen.h>
 
+#include "../../../xen/include/asm-x86/x86-defns.h"
+
 #define BUG() abort()
 #define ASSERT assert
 #define ASSERT_UNREACHABLE() assert(!__LINE__)
@@ -38,11 +40,6 @@
 
 #define is_canonical_address(x) (((int64_t)(x) >> 47) == ((int64_t)(x) >> 63))
 
-/* There's no strict need for these to be in sync with processor.h. */
-#define X86_VENDOR_INTEL   0
-#define X86_VENDOR_AMD     2
-#define X86_VENDOR_UNKNOWN 0xff
-
 #define MMAP_SZ 16384
 bool emul_test_make_stack_executable(void);
 
Signature · 7 lines
2.11.0


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
author
#17 lore raw

PATCH 5/7 fuzz/x86emul: update fuzzer

In reply to Wei Liu

Provide the fuzzer with more ops, and more sophisticated input structure.

Based on a patch originally written by Andrew and George.

---
 .../x86-insn-emulator-fuzzer.c                     | 653 +++++++++++++++++++--
 1 file changed, 595 insertions(+), 58 deletions(-)
tools/fuzz/x86_instruction_emulator/x86-insn-emulator-fuzzer.c +595 -58
index 7d7f731677..8ca0421f60 100644
@@ -16,26 +16,75 @@
 
 #include "x86_emulate.h"
 
-static unsigned char data[4096];
+#include "../../../xen/include/asm-x86/msr-index.h"
+
+#ifndef offsetof
+#define offsetof(t,m) ((size_t)&(((t *)0)->m))
+#endif
+
+#define MSR_INDEX_MAX 16
+
+#define SEG_MAX x86_seg_none
+
+struct input_struct {
+    unsigned cr[5];
+    uint64_t msr[MSR_INDEX_MAX];
+    struct cpu_user_regs regs;
+    struct segment_register segments[SEG_MAX+1];
+    unsigned long options;
+    unsigned char data[4096];
+} input;
+#define DATA_OFFSET offsetof(struct input_struct, data)
 static unsigned int data_index;
 static unsigned int data_max;
 
+int maybe_fail(const char *why, bool exception)
+{
+    int rc;
+
+    if ( data_index + 1 > data_max )
+        return X86EMUL_EXCEPTION;
+    else
+    {
+        if ( input.data[data_index] > 0xc )
+            rc = X86EMUL_EXCEPTION;
+        else if ( input.data[data_index] > 0x8 )
+            rc = X86EMUL_UNHANDLEABLE;
+        else
+            rc = X86EMUL_OKAY;
+        data_index++;
+    }
+
+    if ( rc == X86EMUL_EXCEPTION && !exception )
+        rc = X86EMUL_OKAY;
+
+    printf("maybe_fail %s: %d\n", why, rc);
+
+    return rc;
+}
+
 static int data_read(const char *why, void *dst, unsigned int bytes)
 {
     unsigned int i;
+    int rc;
 
     if ( data_index + bytes > data_max )
         return X86EMUL_EXCEPTION;
+    else
+        rc = maybe_fail(why, true);
 
-    memcpy(dst,  data + data_index, bytes);
-    data_index += bytes;
+    if ( rc == X86EMUL_OKAY )
+    {
+        memcpy(dst,  input.data + data_index, bytes);
+        data_index += bytes;
 
-    printf("%s: ", why);
-    for ( i = 0; i < bytes; i++ )
-        printf(" %02x", *(unsigned char *)(dst + i));
-    printf("\n");
+        printf("%s: ", why);
+        for ( i = 0; i < bytes; i++ )
+            printf(" %02x", *(unsigned char *)(dst + i));
+        printf("\n");
+    }
 
-    return X86EMUL_OKAY;
+    return rc;
 }
 
 static int fuzz_read(
@@ -48,14 +97,71 @@ static int fuzz_read(
     return data_read("read", p_data, bytes);
 }
 
-static int fuzz_fetch(
+static int fuzz_read_io(
+    unsigned int port,
+    unsigned int bytes,
+    unsigned long *val,
+    struct x86_emulate_ctxt *ctxt)
+{
+    return data_read("read_io", val, bytes);
+}
+
+static int fuzz_insn_fetch(
     unsigned int seg,
     unsigned long offset,
     void *p_data,
     unsigned int bytes,
     struct x86_emulate_ctxt *ctxt)
 {
-    return data_read("fetch", p_data, bytes);
+    return data_read("insn_fetch", p_data, bytes);
+}
+
+static int fuzz_rep_ins(
+    uint16_t src_port,
+    enum x86_segment dst_seg,
+    unsigned long dst_offset,
+    unsigned int bytes_per_rep,
+    unsigned long *reps,
+    struct x86_emulate_ctxt *ctxt)
+{
+    int rc;
+    unsigned long bytes_read;
+
+    rc = data_read("rep_ins", &bytes_read, sizeof(bytes_read));
+
+    if ( rc != X86EMUL_OKAY )
+        return rc;
+
+    if ( bytes_read < *reps )
+        *reps -= bytes_read;
+    else
+        *reps = 0;
+
+    return rc;
+}
+
+static int fuzz_rep_movs(
+    enum x86_segment src_seg,
+    unsigned long src_offset,
+    enum x86_segment dst_seg,
+    unsigned long dst_offset,
+    unsigned int bytes_per_rep,
+    unsigned long *reps,
+    struct x86_emulate_ctxt *ctxt)
+{
+    int rc;
+    unsigned long bytes_read;
+    rc = data_read("rep_ins", &bytes_read, sizeof(bytes_read));
+
+    if ( rc != X86EMUL_OKAY )
+        return rc;
+
+    if ( bytes_read < *reps )
+        *reps -= bytes_read;
+    else
+        *reps = 0;
+
+    return rc;
 }
 
 static int fuzz_write(
@@ -65,7 +171,7 @@ static int fuzz_write(
     unsigned int bytes,
     struct x86_emulate_ctxt *ctxt)
 {
-    return X86EMUL_OKAY;
+    return maybe_fail("write", true);
 }
 
 static int fuzz_cmpxchg(
@@ -76,18 +182,323 @@ static int fuzz_cmpxchg(
     unsigned int bytes,
     struct x86_emulate_ctxt *ctxt)
 {
-    return X86EMUL_OKAY;
+    return maybe_fail("cmpxchg", true);
+}
+
+static int fuzz_invlpg(
+    enum x86_segment seg,
+    unsigned long offset,
+    struct x86_emulate_ctxt *ctxt)
+{
+    return maybe_fail("invlpg", false);
+}
+
+static int fuzz_wbinvd(
+    struct x86_emulate_ctxt *ctxt)
+{
+    return maybe_fail("wbinvd", true);
+}
+
+static int fuzz_write_io(
+    unsigned int port,
+    unsigned int bytes,
+    unsigned long val,
+    struct x86_emulate_ctxt *ctxt)
+{
+    return maybe_fail("write_io", true);
+}
+
+static int fuzz_rep_stos(
+    void *p_data,
+    enum x86_segment seg,
+    unsigned long offset,
+    unsigned int bytes_per_rep,
+    unsigned long *reps,
+    struct x86_emulate_ctxt *ctxt)
+{
+    return maybe_fail("rep_stos", true);
 }
 
+static int fuzz_rep_outs(
+    enum x86_segment src_seg,
+    unsigned long src_offset,
+    uint16_t dst_port,
+    unsigned int bytes_per_rep,
+    unsigned long *reps,
+    struct x86_emulate_ctxt *ctxt)
+{
+    return maybe_fail("rep_outs", true);
+}
+
+static int fuzz_cpuid(
+    uint32_t leaf,
+    uint32_t subleaf,
+    struct cpuid_leaf *res,
+    struct x86_emulate_ctxt *ctxt)
+{
+    int rc;
+
+    rc = maybe_fail("cpuid", true);
+
+    if ( rc == X86EMUL_OKAY )
+        emul_test_cpuid(leaf, subleaf, res, ctxt);
+
+    return rc;
+}
+
+static int fuzz_read_segment(
+    enum x86_segment seg,
+    struct segment_register *reg,
+    struct x86_emulate_ctxt *ctxt)
+{
+    int rc;
+
+    if ( seg > SEG_MAX )
+        return X86EMUL_UNHANDLEABLE;
+
+    rc = maybe_fail("read_segment", true);
+
+    if ( rc == X86EMUL_OKAY )
+        memcpy(reg, input.segments+seg, sizeof(struct segment_register));
+
+    return rc;
+}
+
+static int fuzz_write_segment(
+    enum x86_segment seg,
+    const struct segment_register *reg,
+    struct x86_emulate_ctxt *ctxt)
+{
+    int rc;
+
+    if ( seg > SEG_MAX )
+        return X86EMUL_UNHANDLEABLE;
+
+    rc = maybe_fail("write_segment", true);
+
+    if ( rc == X86EMUL_OKAY )
+        memcpy(input.segments+seg, reg, sizeof(struct segment_register));
+
+    return rc;
+}
+
+static int fuzz_read_cr(
+    unsigned int reg,
+    unsigned long *val,
+    struct x86_emulate_ctxt *ctxt)
+{
+    int rc;
+
+    if ( reg > 5 )
+        return X86EMUL_UNHANDLEABLE;
+
+    rc = maybe_fail("read_cr", true);
+
+    if ( rc == X86EMUL_OKAY )
+        *val = input.cr[reg];
+
+    return rc;
+}
+
+static int fuzz_write_cr(
+    unsigned int reg,
+    unsigned long val,
+    struct x86_emulate_ctxt *ctxt)
+{
+    int rc;
+
+    if ( reg > 5 )
+        return X86EMUL_UNHANDLEABLE;
+
+    rc = maybe_fail("write_cr", true);
+
+    if ( rc == X86EMUL_OKAY )
+        input.cr[reg] = val;
+
+    return rc;
+}
+
+enum {
+    MSRI_IA32_SYSENTER_CS,
+    MSRI_IA32_SYSENTER_ESP,
+    MSRI_IA32_SYSENTER_EIP,
+    MSRI_EFER,
+    MSRI_STAR,
+    MSRI_LSTAR,
+    MSRI_CSTAR,
+    MSRI_SYSCALL_MASK
+};
+
+int msr_index[MSR_INDEX_MAX] = {
+    [MSRI_IA32_SYSENTER_CS] = MSR_IA32_SYSENTER_CS,
+    [MSRI_IA32_SYSENTER_ESP] = MSR_IA32_SYSENTER_ESP,
+    [MSRI_IA32_SYSENTER_EIP] = MSR_IA32_SYSENTER_EIP,
+    [MSRI_EFER] = MSR_EFER,
+    [MSRI_STAR] = MSR_STAR,
+    [MSRI_LSTAR] = MSR_LSTAR,
+    [MSRI_CSTAR] = MSR_CSTAR,
+    [MSRI_SYSCALL_MASK] = MSR_SYSCALL_MASK
+};
+
+static int fuzz_read_msr_(
+    unsigned int reg,
+    uint64_t *val,
+    struct x86_emulate_ctxt *ctxt)
+{
+    unsigned int idx;
+
+    switch ( reg )
+    {
+    case MSR_TSC_AUX:
+    case MSR_TSC:
+        return data_read("read_msr", val, sizeof(*val));
+    case MSR_EFER:
+        *val = input.msr[MSRI_EFER];
+        *val &= ~EFER_LMA;
+        if ( (*val & EFER_LME) && (input.cr[4] & X86_CR4_PAE) &&
+             (input.cr[0] & X86_CR0_PG) )
+        {
+            printf("Setting EFER_LMA\n");
+            *val |= EFER_LMA;
+        }
+        return X86EMUL_OKAY;
+    }
+
+    for ( idx = 0; idx < MSR_INDEX_MAX; idx++ )
+    {
+        if ( msr_index[idx] == reg )
+        {
+            *val = input.msr[idx];
+            return X86EMUL_OKAY;
+        }
+    }
+
+    return X86EMUL_EXCEPTION;
+}
+
+static int fuzz_read_msr(
+    unsigned int reg,
+    uint64_t *val,
+    struct x86_emulate_ctxt *ctxt) {
+    int rc;
+
+    rc = maybe_fail("read_msr", true);
+    if ( rc != X86EMUL_OKAY )
+        return rc;
+    else
+        return fuzz_read_msr_(reg, val, ctxt);
+}
+
+static int fuzz_write_msr(
+    unsigned int reg,
+    uint64_t val,
+    struct x86_emulate_ctxt *ctxt)
+{
+    unsigned int idx;
+    int rc;
+
+    rc = maybe_fail("write_ms", true);
+    if ( rc != X86EMUL_OKAY )
+        return rc;
+
+    switch ( reg )
+    {
+    case MSR_TSC_AUX:
+    case MSR_TSC:
+        return X86EMUL_OKAY;
+    }
+
+    for ( idx = 0; idx < MSR_INDEX_MAX; idx++ )
+    {
+        if ( msr_index[idx] == reg )
+        {
+            input.msr[idx] = val;
+            return X86EMUL_OKAY;
+        }
+    }
+
+    return X86EMUL_EXCEPTION;
+}
+
+#define SET(h) .h = fuzz_##h
 static struct x86_emulate_ops fuzz_emulops = {
-    .read       = fuzz_read,
-    .insn_fetch = fuzz_fetch,
-    .write      = fuzz_write,
-    .cmpxchg    = fuzz_cmpxchg,
-    .cpuid      = emul_test_cpuid,
-    .read_cr    = emul_test_read_cr,
+    SET(read),
+    SET(insn_fetch),
+    SET(write),
+    SET(cmpxchg),
+    SET(rep_ins),
+    SET(rep_outs),
+    SET(rep_movs),
+    SET(rep_stos),
+    SET(read_segment),
+    SET(write_segment),
+    SET(read_io),
+    SET(write_io),
+    SET(read_cr),
+    SET(write_cr),
+    SET(read_msr),
+    SET(write_msr),
+    SET(wbinvd),
+    SET(cpuid),
+    SET(invlpg),
     .get_fpu    = emul_test_get_fpu,
 };
+#undef SET
+
+static void setup_fpu_exception_handler(void)
+{
+    /* FIXME - just disable exceptions for now */
+    unsigned long a;
+
+    asm volatile ( "fnclex");
+    a=0x3f;
+    asm volatile ( "fldcw %0" :: "m" (a));
+    a=0x1f80;
+    asm volatile ( "ldmxcsr %0" :: "m" (a) );
+}
+
+static void dump_state(struct x86_emulate_ctxt *ctxt)
+{
+    struct cpu_user_regs *regs = ctxt->regs;
+    uint64_t val;
+
+    printf(" -- State -- \n");
+    printf("addr / sp size: %d / %d\n", ctxt->addr_size, ctxt->sp_size);
+    printf(" cr0: %x\n", input.cr[0]);
+    printf(" cr3: %x\n", input.cr[3]);
+    printf(" cr4: %x\n", input.cr[4]);
+
+    printf(" rip: %"PRIx64"\n", regs->rip);
+
+    fuzz_read_msr_(MSR_EFER, &val, ctxt);
+    printf("EFER: %"PRIx64"\n", val);
+}
+
+static bool long_mode_active(struct x86_emulate_ctxt *ctxt)
+{
+    uint64_t val;
+
+    if ( fuzz_read_msr_(MSR_EFER, &val, ctxt) != X86EMUL_OKAY )
+        return false;
+
+    return !!(val & EFER_LMA);
+}
+
+static bool in_longmode(struct x86_emulate_ctxt *ctxt)
+{
+    return long_mode_active(ctxt) && input.segments[x86_seg_cs].attr.fields.l;
+}
+
+static void set_sizes(struct x86_emulate_ctxt *ctxt)
+{
+    if ( in_longmode(ctxt) )
+        ctxt->addr_size = ctxt->sp_size = 64;
+    else
+    {
+        ctxt->addr_size = input.segments[x86_seg_cs].attr.fields.db ? 32 : 16;
+        ctxt->sp_size   = input.segments[x86_seg_ss].attr.fields.db ? 32 : 16;
+    }
+}
 
 #define CANONICALIZE(x)                                   \
     do {                                                  \
@@ -100,10 +511,152 @@ static struct x86_emulate_ops fuzz_emulops = {
         (x) = _y;                                       \
     } while( 0 )
 
-#define ADDR_SIZE_SHIFT 60
-#define ADDR_SIZE_64 (2ULL << ADDR_SIZE_SHIFT)
-#define ADDR_SIZE_32 (1ULL << ADDR_SIZE_SHIFT)
-#define ADDR_SIZE_16 (0)
+/* Expects bitmap and regs to be defined */
+#define CANONICALIZE_MAYBE(reg)                       \
+    if ( !(bitmap & (1 << CANONICALIZE_##reg)) )      \
+        CANONICALIZE(regs->reg);                      \
+
+enum {
+    HOOK_read,
+    HOOK_insn_fetch,
+    HOOK_write,
+    HOOK_cmpxchg,
+    HOOK_rep_ins,
+    HOOK_rep_outs,
+    HOOK_rep_movs,
+    HOOK_rep_stos,
+    HOOK_read_segment,
+    HOOK_write_segment,
+    HOOK_read_io,
+    HOOK_write_io,
+    HOOK_read_cr,
+    HOOK_write_cr,
+    HOOK_read_dr,
+    HOOK_write_dr,
+    HOOK_read_msr,
+    HOOK_write_msr,
+    HOOK_wbinvd,
+    HOOK_cpuid,
+    HOOK_inject_hw_exception,
+    HOOK_inject_sw_interrupt,
+    HOOK_get_fpu,
+    HOOK_put_fpu,
+    HOOK_invlpg,
+    HOOK_vmfunc, /* 26 */
+    OPTION_swint_emulation = 27, /* Two bits */
+    CANONICALIZE_rip = 29,
+    CANONICALIZE_rsp,
+    CANONICALIZE_rbp
+};
+
+/* Expects bitmap to be defined */
+#define MAYBE_DISABLE_HOOK(h)                          \
+    if ( bitmap & (1 << HOOK_##h) )                    \
+    {                                                  \
+        fuzz_emulops.h = NULL;                         \
+        printf("Disabling hook "#h"\n");               \
+    }
+
+static void disable_hooks(void)
+{
+    unsigned long bitmap = input.options;
+
+    MAYBE_DISABLE_HOOK(read);
+    MAYBE_DISABLE_HOOK(insn_fetch);
+    MAYBE_DISABLE_HOOK(write);
+    MAYBE_DISABLE_HOOK(cmpxchg);
+    MAYBE_DISABLE_HOOK(rep_ins);
+    MAYBE_DISABLE_HOOK(rep_outs);
+    MAYBE_DISABLE_HOOK(rep_movs);
+    MAYBE_DISABLE_HOOK(rep_stos);
+    MAYBE_DISABLE_HOOK(read_segment);
+    MAYBE_DISABLE_HOOK(write_segment);
+    MAYBE_DISABLE_HOOK(read_io);
+    MAYBE_DISABLE_HOOK(write_io);
+    MAYBE_DISABLE_HOOK(read_cr);
+    MAYBE_DISABLE_HOOK(write_cr);
+    MAYBE_DISABLE_HOOK(read_msr);
+    MAYBE_DISABLE_HOOK(write_msr);
+    MAYBE_DISABLE_HOOK(wbinvd);
+    MAYBE_DISABLE_HOOK(cpuid);
+    MAYBE_DISABLE_HOOK(get_fpu);
+    MAYBE_DISABLE_HOOK(invlpg);
+}
+
+static void set_swint_support(struct x86_emulate_ctxt *ctxt)
+{
+    int swint_opt = (input.options >> OPTION_swint_emulation) & 3;
+
+    int map[4] = {
+        x86_swint_emulate_none,
+        x86_swint_emulate_none,
+        x86_swint_emulate_icebp,
+        x86_swint_emulate_all };
+
+    ctxt->swint_emulate = map[swint_opt];
+}
+
+/*
+ * Constrain input to architecturally-possible states where
+ * the emulator relies on these
+ *
+ * In general we want the emulator to be as absolutely robust as
+ * possible; which means that we want to minimize the number of things
+ * it assumes about the input state.  Tesing this means minimizing and
+ * removing as much of the input constraints as possible.
+ *
+ * So we only add constraints that (in general) have been proven to
+ * cause crashes in the emulator.
+ *
+ * For future reference: other constraints which might be necessary at
+ * some point:
+ *
+ * - EFER.LMA => !EFLAGS.NT
+ * - In VM86 mode (and real mode?), force segment...
+ *  - ...access rights to 0xf3
+ *  - ...limits to 0xffff
+ *  - ...bases to below 1Mb, 16-byte aligned
+ *  - ...selectors to (base >> 4)
+ */
+void sanitize_input(struct x86_emulate_ctxt *ctxt) {
+    struct cpu_user_regs *regs = &input.regs;
+    unsigned long bitmap = input.options;
+
+    input.options &=
+        ~((1<<HOOK_read)|
+          (1<<HOOK_write)|
+          (1<<HOOK_cmpxchg)|
+          (1<<HOOK_insn_fetch));
+
+    /* Zero 'private' entries */
+    regs->error_code = 0;
+    regs->entry_vector = 0;
+
+    CANONICALIZE_MAYBE(rip);
+    CANONICALIZE_MAYBE(rsp);
+    CANONICALIZE_MAYBE(rbp);
+
+    /*
+     * CR0.PG can't be set if CR0.PE isn't set.  Set is more interesting, so
+     * set PE if PG is set.
+     */
+    if ( input.cr[0] & X86_CR0_PG )
+        input.cr[0] |= X86_CR0_PE;
+
+    /*
+     * EFLAGS.VM not available in long mode
+     */
+    if ( long_mode_active(ctxt) )
+        regs->rflags &= ~EFLG_VM;
+
+    /*
+     * EFLAGS.VM implies 16-bit mode
+     */
+    if ( regs->rflags & EFLG_VM ) {
+        input.segments[x86_seg_cs].attr.fields.db = 0;
+        input.segments[x86_seg_ss].attr.fields.db = 0;
+    }
+}
 
 int LLVMFuzzerTestOneInput(const uint8_t *data_p, size_t size)
 {
@@ -114,10 +667,7 @@ int LLVMFuzzerTestOneInput(const uint8_t *data_p, size_t size)
         .addr_size = 8 * sizeof(void *),
         .sp_size = 8 * sizeof(void *),
     };
-    unsigned int nr = 0;
     int rc;
-    unsigned int x;
-    const uint8_t *p = data_p;
 
     stack_exec = emul_test_make_stack_executable();
     if ( !stack_exec )
@@ -127,52 +677,39 @@ int LLVMFuzzerTestOneInput(const uint8_t *data_p, size_t size)
     }
 
     /* Reset all global state variables */
-    memset(data, 0, sizeof(data));
+    memset(&input, 0, sizeof(input));
     data_index = 0;
     data_max = 0;
 
-    nr = size < sizeof(regs) ? size : sizeof(regs);
-
-    memcpy(&regs, p, nr);
-    p += sizeof(regs);
+    if ( size <= DATA_OFFSET )
+    {
+        printf("Input too small\n");
+        return 1;
+    }
 
-    if ( nr < size )
+    if ( size > sizeof(input) )
     {
-        memcpy(data, p, size - nr);
-        data_max = size - nr;
+        printf("Input too large\n");
+        return 1;
     }
 
-    ctxt.force_writeback = false;
+    memcpy(&input, data_p, size);
 
-    /* Zero 'private' fields */
-    regs.error_code = 0;
-    regs.entry_vector = 0;
+    data_max = size - DATA_OFFSET;
 
-    /* Use the upper bits of regs.eip to determine addr_size */
-    x = (regs.rip >> ADDR_SIZE_SHIFT) & 0x3;
-    if ( x == 3 )
-        x = 2;
-    ctxt.addr_size = 16 << x;
-    printf("addr_size: %d\n", ctxt.addr_size);
+    sanitize_input(&ctxt);
 
-    /* Use the upper bit of regs.rsp to determine sp_size (if appropriate) */
-    if ( ctxt.addr_size == 64 )
-        ctxt.sp_size = 64;
-    else
-    {
-        /* If addr_size isn't 64-bits, sp_size can only be 16 or 32 bits */
-        x = (regs.rsp >> ADDR_SIZE_SHIFT) & 0x1;
-        ctxt.sp_size = 16 << x;
-    }
-    printf("sp_size: %d\n", ctxt.sp_size);
-    CANONICALIZE(regs.rip);
-    CANONICALIZE(regs.rsp);
-    CANONICALIZE(regs.rbp);
+    disable_hooks();
 
-    /* Zero all segments for now */
-    regs.cs = regs.ss = regs.es = regs.ds = regs.fs = regs.gs = 0;
+    set_swint_support(&ctxt);
 
     do {
+        /* FIXME: Until we actually implement SIGFPE handling properly */
+        setup_fpu_exception_handler();
+
+        set_sizes(&ctxt);
+        dump_state(&ctxt);
+
         rc = x86_emulate(&ctxt, &fuzz_emulops);
         printf("Emulation result: %d\n", rc);
     } while ( rc == X86EMUL_OKAY );
Signature · 7 lines
2.11.0


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
#18 lore raw

Re: [PATCH 5/7] fuzz/x86emul: update fuzzer

tools/fuzz/x86_instruction_emulator/x86-insn-emulator-fuzzer.c line 23

Quoted 13 lines
Quoted 1 line
Quoted 1 line · On 25.01.17 at 16:44, <[email protected]> wrote:
tools/fuzz/x86_instruction_emulator/x86-insn-emulator-fuzzer.c +5 -1
@@ -16,26 +16,75 @@
 
 #include "x86_emulate.h"
 
-static unsigned char data[4096];
+#include "../../../xen/include/asm-x86/msr-index.h"
+
+#ifndef offsetof
+#define offsetof(t,m) ((size_t)&(((t *)0)->m))
+#endif

Why can't you include the standard header for this?

tools/fuzz/x86_instruction_emulator/x86-insn-emulator-fuzzer.c line 30

Quoted 6 lines
tools/fuzz/x86_instruction_emulator/x86-insn-emulator-fuzzer.c +6 -0
+#define MSR_INDEX_MAX 16
+
+#define SEG_MAX x86_seg_none
+
+struct input_struct {
+    unsigned cr[5];

unsigned long?

tools/fuzz/x86_instruction_emulator/x86-insn-emulator-fuzzer.c line 33

Quoted 3 lines
tools/fuzz/x86_instruction_emulator/x86-insn-emulator-fuzzer.c +3 -0
+    uint64_t msr[MSR_INDEX_MAX];
+    struct cpu_user_regs regs;
+    struct segment_register segments[SEG_MAX+1];

Either SEG_MAX needs to be x86_seg_none - 1, or it is slightly mis-named (should be SEG_NUM) and the +1 here is not needed.

tools/fuzz/x86_instruction_emulator/x86-insn-emulator-fuzzer.c line 45

Quoted 5 lines
tools/fuzz/x86_instruction_emulator/x86-insn-emulator-fuzzer.c +5 -0
+int maybe_fail(const char *why, bool exception)
+{
+    int rc;
+
+    if ( data_index + 1 > data_max )

The naming issue makes this look wrong too - I think you again mean data_num or data_amount, not data_max.

tools/fuzz/x86_instruction_emulator/x86-insn-emulator-fuzzer.c line 52

Quoted 7 lines
tools/fuzz/x86_instruction_emulator/x86-insn-emulator-fuzzer.c +7 -0
+        return X86EMUL_EXCEPTION;
+    else
+    {
+        if ( input.data[data_index] > 0xc )
+            rc = X86EMUL_EXCEPTION;
+        else if ( input.data[data_index] > 0x8 )
+            rc = X86EMUL_UNHANDLEABLE;

How were these numbers chosen?

tools/fuzz/x86_instruction_emulator/x86-insn-emulator-fuzzer.c line 59

Quoted 7 lines
tools/fuzz/x86_instruction_emulator/x86-insn-emulator-fuzzer.c +7 -0
+        else
+            rc = X86EMUL_OKAY;
+        data_index++;
+    }
+
+    if ( rc == X86EMUL_EXCEPTION && !exception )
+        rc = X86EMUL_OKAY;

This could do with a comment explaining the intention.

Quoted 4 lines
tools/fuzz/x86_instruction_emulator/x86-insn-emulator-fuzzer.c +4 -0
+    printf("maybe_fail %s: %d\n", why, rc);
+
+    return rc;
+}

tools/fuzz/x86_instruction_emulator/x86-insn-emulator-fuzzer.c line 74

Quoted 10 lines
tools/fuzz/x86_instruction_emulator/x86-insn-emulator-fuzzer.c +4 -0
+
 static int data_read(const char *why, void *dst, unsigned int bytes)
 {
     unsigned int i;
+    int rc;
 
     if ( data_index + bytes > data_max )
         return X86EMUL_EXCEPTION;
+    else
+        rc = maybe_fail(why, true);

No need for "else".

Quoted 13 lines
tools/fuzz/x86_instruction_emulator/x86-insn-emulator-fuzzer.c +8 -1
@@ -48,14 +97,71 @@ static int fuzz_read(
     return data_read("read", p_data, bytes);
 }
 
-static int fuzz_fetch(
+static int fuzz_read_io(
+    unsigned int port,
+    unsigned int bytes,
+    unsigned long *val,
+    struct x86_emulate_ctxt *ctxt)
+{
+    return data_read("read_io", val, bytes);
+}

tools/fuzz/x86_instruction_emulator/x86-insn-emulator-fuzzer.c static int fuzz_read( · line 116

Quoted 10 lines
tools/fuzz/x86_instruction_emulator/x86-insn-emulator-fuzzer.c +3 -1
+
+static int fuzz_insn_fetch(
     unsigned int seg,
     unsigned long offset,
     void *p_data,
     unsigned int bytes,
     struct x86_emulate_ctxt *ctxt)
 {
-    return data_read("fetch", p_data, bytes);
+    return data_read("insn_fetch", p_data, bytes);

Why is "fetch" not good enough? It looks like there may be a lot of output, so any compaction opportunity may be worthwhile to use.

tools/fuzz/x86_instruction_emulator/x86-insn-emulator-fuzzer.c static int fuzz_read( · line 154

Quoted 12 lines
tools/fuzz/x86_instruction_emulator/x86-insn-emulator-fuzzer.c +12 -0
+static int fuzz_rep_movs(
+    enum x86_segment src_seg,
+    unsigned long src_offset,
+    enum x86_segment dst_seg,
+    unsigned long dst_offset,
+    unsigned int bytes_per_rep,
+    unsigned long *reps,
+    struct x86_emulate_ctxt *ctxt)
+{
+    int rc;
+    unsigned long bytes_read;
+    rc = data_read("rep_ins", &bytes_read, sizeof(bytes_read));

"rep_movs"

tools/fuzz/x86_instruction_emulator/x86-insn-emulator-fuzzer.c static int fuzz_cmpxchg( · line 220

Quoted 10 lines
tools/fuzz/x86_instruction_emulator/x86-insn-emulator-fuzzer.c +9 -0
+static int fuzz_rep_stos(
+    void *p_data,
+    enum x86_segment seg,
+    unsigned long offset,
+    unsigned int bytes_per_rep,
+    unsigned long *reps,
+    struct x86_emulate_ctxt *ctxt)
+{
+    return maybe_fail("rep_stos", true);
 }

Perhaps better moved next to rep_movs().

Quoted 5 lines
tools/fuzz/x86_instruction_emulator/x86-insn-emulator-fuzzer.c +5 -0
+static int fuzz_cpuid(
+    uint32_t leaf,
+    uint32_t subleaf,
+    struct cpuid_leaf *res,
+    struct x86_emulate_ctxt *ctxt)

tools/fuzz/x86_instruction_emulator/x86-insn-emulator-fuzzer.c static int fuzz_cmpxchg( · line 247

Quoted 10 lines
tools/fuzz/x86_instruction_emulator/x86-insn-emulator-fuzzer.c +10 -0
+{
+    int rc;
+
+    rc = maybe_fail("cpuid", true);
+
+    if ( rc == X86EMUL_OKAY )
+        emul_test_cpuid(leaf, subleaf, res, ctxt);
+
+    return rc;
+}

Careful here: ->cpuid() is documented to only be allowed to fail with X86EMUL_EXCEPTION.

Quoted 7 lines
tools/fuzz/x86_instruction_emulator/x86-insn-emulator-fuzzer.c +7 -0
+static int fuzz_read_segment(
+    enum x86_segment seg,
+    struct segment_register *reg,
+    struct x86_emulate_ctxt *ctxt)
+{
+    int rc;
+

tools/fuzz/x86_instruction_emulator/x86-insn-emulator-fuzzer.c static int fuzz_cmpxchg( · line 265

Quoted 10 lines
tools/fuzz/x86_instruction_emulator/x86-insn-emulator-fuzzer.c +10 -0
+    if ( seg > SEG_MAX )
+        return X86EMUL_UNHANDLEABLE;
+
+    rc = maybe_fail("read_segment", true);
+
+    if ( rc == X86EMUL_OKAY )
+        memcpy(reg, input.segments+seg, sizeof(struct segment_register));
+
+    return rc;
+}

This continues from the earlier comment: x86_seg_none must not make it here (i.e. should by included in the early bailing).

Quoted 7 lines
tools/fuzz/x86_instruction_emulator/x86-insn-emulator-fuzzer.c +7 -0
+static int fuzz_write_segment(
+    enum x86_segment seg,
+    const struct segment_register *reg,
+    struct x86_emulate_ctxt *ctxt)
+{
+    int rc;
+

tools/fuzz/x86_instruction_emulator/x86-insn-emulator-fuzzer.c static int fuzz_cmpxchg( · line 283

Quoted 10 lines
tools/fuzz/x86_instruction_emulator/x86-insn-emulator-fuzzer.c +10 -0
+    if ( seg > SEG_MAX )
+        return X86EMUL_UNHANDLEABLE;
+
+    rc = maybe_fail("write_segment", true);
+
+    if ( rc == X86EMUL_OKAY )
+        memcpy(input.segments+seg, reg, sizeof(struct segment_register));
+
+    return rc;
+}

Even more so here then.

tools/fuzz/x86_instruction_emulator/x86-insn-emulator-fuzzer.c static int fuzz_cmpxchg( · line 292

Quoted 8 lines
tools/fuzz/x86_instruction_emulator/x86-insn-emulator-fuzzer.c +8 -0
+static int fuzz_read_cr(
+    unsigned int reg,
+    unsigned long *val,
+    struct x86_emulate_ctxt *ctxt)
+{
+    int rc;
+
+    if ( reg > 5 )

ARRAY_SIZE()

tools/fuzz/x86_instruction_emulator/x86-insn-emulator-fuzzer.c static int fuzz_cmpxchg( · line 310

Quoted 8 lines
tools/fuzz/x86_instruction_emulator/x86-insn-emulator-fuzzer.c +8 -0
+static int fuzz_write_cr(
+    unsigned int reg,
+    unsigned long val,
+    struct x86_emulate_ctxt *ctxt)
+{
+    int rc;
+
+    if ( reg > 5 )

Same here.

Quoted 1 line

+int msr_index[MSR_INDEX_MAX] = {

static unsigned int (and maybe also const, I didn't check whether it gets written to).

Quoted 1 line

+static int fuzz_read_msr_(

Please use a leading rather than a trailing underscore (unless there's a reason for you doing it that way).

tools/fuzz/x86_instruction_emulator/x86-insn-emulator-fuzzer.c static int fuzz_cmpxchg( · line 457

Quoted 10 lines
tools/fuzz/x86_instruction_emulator/x86-insn-emulator-fuzzer.c +10 -0
+static void setup_fpu_exception_handler(void)
+{
+    /* FIXME - just disable exceptions for now */
+    unsigned long a;
+
+    asm volatile ( "fnclex");
+    a=0x3f;
+    asm volatile ( "fldcw %0" :: "m" (a));
+    a=0x1f80;
+    asm volatile ( "ldmxcsr %0" :: "m" (a) );

I don't see what you need "a" for in this function. And I also wonder how the FCW value was chosen (the MXCSR one is a commonly used value).

tools/fuzz/x86_instruction_emulator/x86-insn-emulator-fuzzer.c static int fuzz_cmpxchg( · line 484

Quoted 8 lines
tools/fuzz/x86_instruction_emulator/x86-insn-emulator-fuzzer.c +8 -0
+static bool long_mode_active(struct x86_emulate_ctxt *ctxt)
+{
+    uint64_t val;
+
+    if ( fuzz_read_msr_(MSR_EFER, &val, ctxt) != X86EMUL_OKAY )
+        return false;
+
+    return !!(val & EFER_LMA);

No need for !! here.

Quoted 19 lines
tools/fuzz/x86_instruction_emulator/x86-insn-emulator-fuzzer.c +19 -0
+enum {
+    HOOK_read,
+    HOOK_insn_fetch,
+    HOOK_write,
+    HOOK_cmpxchg,
+    HOOK_rep_ins,
+    HOOK_rep_outs,
+    HOOK_rep_movs,
+    HOOK_rep_stos,
+    HOOK_read_segment,
+    HOOK_write_segment,
+    HOOK_read_io,
+    HOOK_write_io,
+    HOOK_read_cr,
+    HOOK_write_cr,
+    HOOK_read_dr,
+    HOOK_write_dr,
+    HOOK_read_msr,
+    HOOK_write_msr,

tools/fuzz/x86_instruction_emulator/x86-insn-emulator-fuzzer.c static struct x86_emulate_ops fuzz_emulops = { · line 547

Quoted 10 lines
tools/fuzz/x86_instruction_emulator/x86-insn-emulator-fuzzer.c +10 -0
+    HOOK_wbinvd,
+    HOOK_cpuid,
+    HOOK_inject_hw_exception,
+    HOOK_inject_sw_interrupt,
+    HOOK_get_fpu,
+    HOOK_put_fpu,
+    HOOK_invlpg,
+    HOOK_vmfunc, /* 26 */
+    OPTION_swint_emulation = 27, /* Two bits */
+    CANONICALIZE_rip = 29,

HOOK_vmfunc,
OPTION_swint_emulation, /* Two bits */ CANONICALIZE_rip = OPTION_swint_emulation + 2,

Quoted 4 lines
tools/fuzz/x86_instruction_emulator/x86-insn-emulator-fuzzer.c +4 -0
+/* Expects bitmap to be defined */
+#define MAYBE_DISABLE_HOOK(h)                          \
+    if ( bitmap & (1 << HOOK_##h) )                    \
+    {                                                  \

tools/fuzz/x86_instruction_emulator/x86-insn-emulator-fuzzer.c static struct x86_emulate_ops fuzz_emulops = { · line 565

Quoted 10 lines
tools/fuzz/x86_instruction_emulator/x86-insn-emulator-fuzzer.c +10 -0
+        fuzz_emulops.h = NULL;                         \
+        printf("Disabling hook "#h"\n");               \
+    }
+
+static void disable_hooks(void)
+{
+    unsigned long bitmap = input.options;
+
+    MAYBE_DISABLE_HOOK(read);
+    MAYBE_DISABLE_HOOK(insn_fetch);

-> read and ->insn_fetch must not be NULL (there are ASSERT()s to that effect).

tools/fuzz/x86_instruction_emulator/x86-insn-emulator-fuzzer.c static struct x86_emulate_ops fuzz_emulops = { · line 588

Quoted 3 lines
tools/fuzz/x86_instruction_emulator/x86-insn-emulator-fuzzer.c +3 -0
+static void set_swint_support(struct x86_emulate_ctxt *ctxt)
+{
+    int swint_opt = (input.options >> OPTION_swint_emulation) & 3;

unsigned int

Quoted 1 line

+ int map[4] = {

static const enum x86_swint_emulation

Quoted 9 lines

+/*
+ * Constrain input to architecturally-possible states where
+ * the emulator relies on these
+ *
+ * In general we want the emulator to be as absolutely robust as
+ * possible; which means that we want to minimize the number of things
+ * it assumes about the input state. Tesing this means minimizing and
+ * removing as much of the input constraints as possible.
+ *

Quoted 8 lines

+ * So we only add constraints that (in general) have been proven to
+ * cause crashes in the emulator.
+ *
+ * For future reference: other constraints which might be necessary at
+ * some point:
+ *
+ * - EFER.LMA => !EFLAGS.NT
+ * - In VM86 mode (and real mode?), force segment...

As per commit 05118b1596 real mode should not be included here.

Quoted 4 lines
+ *  - ...access rights to 0xf3
+ *  - ...limits to 0xffff
+ *  - ...bases to below 1Mb, 16-byte aligned
+ *  - ...selectors to (base >> 4)

Most of this does not match the implementation (which only clear DB for CS and SS).

tools/fuzz/x86_instruction_emulator/x86-insn-emulator-fuzzer.c static struct x86_emulate_ops fuzz_emulops = { · line 629

Quoted 10 lines

+ */

tools/fuzz/x86_instruction_emulator/x86-insn-emulator-fuzzer.c +9 -0
+void sanitize_input(struct x86_emulate_ctxt *ctxt) {
+    struct cpu_user_regs *regs = &input.regs;
+    unsigned long bitmap = input.options;
+
+    input.options &=
+        ~((1<<HOOK_read)|
+          (1<<HOOK_write)|
+          (1<<HOOK_cmpxchg)|
+          (1<<HOOK_insn_fetch));

Ah, this addresses one of the earlier remarks. However, ->write and ->cmpxchg have become optional a little while ago.

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

#19 lore raw

Re: [PATCH 5/7] fuzz/x86emul: update fuzzer

Quoted 7 lines · Jan Beulich
Quoted 7 lines
Quoted 1 line
Quoted 1 line · On 25.01.17 at 16:44, <[email protected]> wrote:
tools/fuzz/x86_instruction_emulator/x86-insn-emulator-fuzzer.c +0 -0
@@ -16,26 +16,75 @@
 
 #include "x86_emulate.h"
 
Quoted 8 lines

tools/fuzz/x86_instruction_emulator/x86-insn-emulator-fuzzer.c line 23

Quoted 6 lines
tools/fuzz/x86_instruction_emulator/x86-insn-emulator-fuzzer.c +5 -1
-static unsigned char data[4096];
+#include "../../../xen/include/asm-x86/msr-index.h"
+
+#ifndef offsetof
+#define offsetof(t,m) ((size_t)&(((t *)0)->m))
+#endif

Why can't you include the standard header for this?

[Responding because this is code I wrote]

Because I didn't know where it was. What's the best thing to include to get this?

Quoted 34 lines

tools/fuzz/x86_instruction_emulator/x86-insn-emulator-fuzzer.c line 30

Quoted 7 lines
tools/fuzz/x86_instruction_emulator/x86-insn-emulator-fuzzer.c +6 -0
+#define MSR_INDEX_MAX 16
+
+#define SEG_MAX x86_seg_none
+
+struct input_struct {
+    unsigned cr[5];

unsigned long?

tools/fuzz/x86_instruction_emulator/x86-insn-emulator-fuzzer.c line 33

Quoted 3 lines
tools/fuzz/x86_instruction_emulator/x86-insn-emulator-fuzzer.c +3 -0
+    uint64_t msr[MSR_INDEX_MAX];
+    struct cpu_user_regs regs;
+    struct segment_register segments[SEG_MAX+1];

Either SEG_MAX needs to be x86_seg_none - 1, or it is slightly mis-named (should be SEG_NUM) and the +1 here is not needed.

tools/fuzz/x86_instruction_emulator/x86-insn-emulator-fuzzer.c line 45

Quoted 5 lines
tools/fuzz/x86_instruction_emulator/x86-insn-emulator-fuzzer.c +5 -0
+int maybe_fail(const char *why, bool exception)
+{
+    int rc;
+
+    if ( data_index + 1 > data_max )

The naming issue makes this look wrong too - I think you again mean data_num or data_amount, not data_max.

tools/fuzz/x86_instruction_emulator/x86-insn-emulator-fuzzer.c line 52

Quoted 7 lines
tools/fuzz/x86_instruction_emulator/x86-insn-emulator-fuzzer.c +7 -0
+        return X86EMUL_EXCEPTION;
+    else
+    {
+        if ( input.data[data_index] > 0xc )
+            rc = X86EMUL_EXCEPTION;
+        else if ( input.data[data_index] > 0x8 )
+            rc = X86EMUL_UNHANDLEABLE;
Quoted 1 line

How were these numbers chosen?

The idea here is to give the fuzzer a way of (blindly) changing whether the operation succeeds or how it fails in a "modular" way, by just "consuming" one byte from the input. The numbers are meant to make random values roughly 50% succeed, 25% generate an exception, and 25% return unhandleable.

In theory these could be any numbers; it could be 0 -> EXCEPTION, 1 -> UNHANDLEABLE, anything else -> OKAY.

Quoted 10 lines

tools/fuzz/x86_instruction_emulator/x86-insn-emulator-fuzzer.c line 59

Quoted 8 lines
tools/fuzz/x86_instruction_emulator/x86-insn-emulator-fuzzer.c +7 -0
+        else
+            rc = X86EMUL_OKAY;
+        data_index++;
+    }
+
+    if ( rc == X86EMUL_EXCEPTION && !exception )
+        rc = X86EMUL_OKAY;

This could do with a comment explaining the intention.

This allows the caller to control whether the operation is allowed to return an EXCEPTION. If it's not, change any EXCEPTION values into OKAY.

Quoted 36 lines
Quoted 5 lines
tools/fuzz/x86_instruction_emulator/x86-insn-emulator-fuzzer.c +4 -0
+    printf("maybe_fail %s: %d\n", why, rc);
+
+    return rc;
+}

tools/fuzz/x86_instruction_emulator/x86-insn-emulator-fuzzer.c line 74

Quoted 10 lines
tools/fuzz/x86_instruction_emulator/x86-insn-emulator-fuzzer.c +4 -0
+
 static int data_read(const char *why, void *dst, unsigned int bytes)
 {
     unsigned int i;
+    int rc;
 
     if ( data_index + bytes > data_max )
         return X86EMUL_EXCEPTION;
+    else
+        rc = maybe_fail(why, true);

No need for "else".

Quoted 18 lines
tools/fuzz/x86_instruction_emulator/x86-insn-emulator-fuzzer.c +10 -1
@@ -48,14 +97,71 @@ static int fuzz_read(
     return data_read("read", p_data, bytes);
 }
 
-static int fuzz_fetch(
+static int fuzz_read_io(
+    unsigned int port,
+    unsigned int bytes,
+    unsigned long *val,
+    struct x86_emulate_ctxt *ctxt)
+{
+    return data_read("read_io", val, bytes);
+}
+
+static int fuzz_insn_fetch(
     unsigned int seg,
     unsigned long offset,
     void *p_data,
Quoted 8 lines

tools/fuzz/x86_instruction_emulator/x86-insn-emulator-fuzzer.c static int fuzz_read( · line 116

Quoted 5 lines
tools/fuzz/x86_instruction_emulator/x86-insn-emulator-fuzzer.c +1 -1
     unsigned int bytes,
     struct x86_emulate_ctxt *ctxt)
 {
-    return data_read("fetch", p_data, bytes);
+    return data_read("insn_fetch", p_data, bytes);

Why is "fetch" not good enough? It looks like there may be a lot of output, so any compaction opportunity may be worthwhile to use.

Well the function name has been changed so that I can use the macro below to enable / disable the hook, rather than having to open-code each one; and the printf is there to match the function name.

Most of the tests only had a few hundred lines of output, so although it's a bike shed, I'd prefer "make the printout match the actual name of the hook".

Quoted 10 lines
Quoted 10 lines
tools/fuzz/x86_instruction_emulator/x86-insn-emulator-fuzzer.c +10 -0
+static int fuzz_cpuid(
+    uint32_t leaf,
+    uint32_t subleaf,
+    struct cpuid_leaf *res,
+    struct x86_emulate_ctxt *ctxt)
+{
+    int rc;
+
+    rc = maybe_fail("cpuid", true);
+
Quoted 8 lines

tools/fuzz/x86_instruction_emulator/x86-insn-emulator-fuzzer.c static int fuzz_cmpxchg( · line 247

Quoted 5 lines
tools/fuzz/x86_instruction_emulator/x86-insn-emulator-fuzzer.c +5 -0
+    if ( rc == X86EMUL_OKAY )
+        emul_test_cpuid(leaf, subleaf, res, ctxt);
+
+    return rc;
+}

Careful here: ->cpuid() is documented to only be allowed to fail with X86EMUL_EXCEPTION.

But at the moment, the emulator seems to function properly even if you return UNHANDLEABLE. This is probably more robust than otherwise.

Quoted 4 lines

tools/fuzz/x86_instruction_emulator/x86-insn-emulator-fuzzer.c static int fuzz_cmpxchg( · line 332

Quoted 1 line
tools/fuzz/x86_instruction_emulator/x86-insn-emulator-fuzzer.c +1 -0
+int msr_index[MSR_INDEX_MAX] = {

static unsigned int (and maybe also const, I didn't check whether it gets written to).

No, I think this is a static "reverse mapping" of MSR number to storage index.

Quoted 5 lines

tools/fuzz/x86_instruction_emulator/x86-insn-emulator-fuzzer.c static int fuzz_cmpxchg( · line 343

Quoted 2 lines
tools/fuzz/x86_instruction_emulator/x86-insn-emulator-fuzzer.c +1 -0
+static int fuzz_read_msr_(

Please use a leading rather than a trailing underscore (unless there's a reason for you doing it that way).

I think there was a reason but I can't remember what it was. I guess
we can try it and see what happens. :-)

Quoted 6 lines
Quoted 6 lines
tools/fuzz/x86_instruction_emulator/x86-insn-emulator-fuzzer.c +6 -0
+static void setup_fpu_exception_handler(void)
+{
+    /* FIXME - just disable exceptions for now */
+    unsigned long a;
+
+    asm volatile ( "fnclex");
Quoted 8 lines

tools/fuzz/x86_instruction_emulator/x86-insn-emulator-fuzzer.c static int fuzz_cmpxchg( · line 457

Quoted 4 lines
tools/fuzz/x86_instruction_emulator/x86-insn-emulator-fuzzer.c +4 -0
+    a=0x3f;
+    asm volatile ( "fldcw %0" :: "m" (a));
+    a=0x1f80;
+    asm volatile ( "ldmxcsr %0" :: "m" (a) );

I don't see what you need "a" for in this function. And I also wonder how the FCW value was chosen (the MXCSR one is a commonly used value).

I just copied it from somewhere. Could you suggest something better?

Quoted 9 lines
Quoted 9 lines
tools/fuzz/x86_instruction_emulator/x86-insn-emulator-fuzzer.c +9 -0
+/* Expects bitmap to be defined */
+#define MAYBE_DISABLE_HOOK(h)                          \
+    if ( bitmap & (1 << HOOK_##h) )                    \
+    {                                                  \
+        fuzz_emulops.h = NULL;                         \
+        printf("Disabling hook "#h"\n");               \
+    }
+
+static void disable_hooks(void)
Quoted 8 lines

tools/fuzz/x86_instruction_emulator/x86-insn-emulator-fuzzer.c static struct x86_emulate_ops fuzz_emulops = { · line 565

Quoted 5 lines
tools/fuzz/x86_instruction_emulator/x86-insn-emulator-fuzzer.c +5 -0
+{
+    unsigned long bitmap = input.options;
+
+    MAYBE_DISABLE_HOOK(read);
+    MAYBE_DISABLE_HOOK(insn_fetch);

-> read and ->insn_fetch must not be NULL (there are ASSERT()s to that effect).

I wanted to have all the information about what "quirks" or requirements the emulator had empirically in one place. So have the code to disable it, but filter out input that would do so in the "sanitize" section.

Quoted 20 lines
Quoted 7 lines
tools/fuzz/x86_instruction_emulator/x86-insn-emulator-fuzzer.c +7 -0
+/*
+ * Constrain input to architecturally-possible states where
+ * the emulator relies on these
+ *
+ * In general we want the emulator to be as absolutely robust as
+ * possible; which means that we want to minimize the number of things
+ * it assumes about the input state.  Tesing this means minimizing and

tools/fuzz/x86_instruction_emulator/x86-insn-emulator-fuzzer.c static struct x86_emulate_ops fuzz_emulops = { · line 615

Quoted 10 lines
tools/fuzz/x86_instruction_emulator/x86-insn-emulator-fuzzer.c +10 -0
+ * removing as much of the input constraints as possible.
+ *
+ * So we only add constraints that (in general) have been proven to
+ * cause crashes in the emulator.
+ *
+ * For future reference: other constraints which might be necessary at
+ * some point:
+ *
+ * - EFER.LMA => !EFLAGS.NT
+ * - In VM86 mode (and real mode?), force segment...

As per commit 05118b1596 real mode should not be included here.

Quoted 7 lines
Quoted 4 lines
+ *  - ...access rights to 0xf3
+ *  - ...limits to 0xffff
+ *  - ...bases to below 1Mb, 16-byte aligned
+ *  - ...selectors to (base >> 4)

Most of this does not match the implementation (which only clear DB for CS and SS).

You mean that the code here doesn't actually do this? Yes, that's what the comment says -- "For future reference, other constraints which may be necessary at some point" -- that is, restrictions which exist in the architecture but do not (yet) exist in this code.

Quoted 14 lines

tools/fuzz/x86_instruction_emulator/x86-insn-emulator-fuzzer.c static struct x86_emulate_ops fuzz_emulops = { · line 629

Quoted 11 lines

+ */

tools/fuzz/x86_instruction_emulator/x86-insn-emulator-fuzzer.c +9 -0
+void sanitize_input(struct x86_emulate_ctxt *ctxt) {
+    struct cpu_user_regs *regs = &input.regs;
+    unsigned long bitmap = input.options;
+
+    input.options &=
+        ~((1<<HOOK_read)|
+          (1<<HOOK_write)|
+          (1<<HOOK_cmpxchg)|
+          (1<<HOOK_insn_fetch));

Ah, this addresses one of the earlier remarks. However, ->write and ->cmpxchg have become optional a little while ago.

And yet, empirically, the emulator crashes if you don't have them. If this isn't expected, we should submit some patches.

-George

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

#20 lore raw

Re: [PATCH 5/7] fuzz/x86emul: update fuzzer

Quoted 18 lines
Quoted 1 line
Quoted 1 line · On 26.01.17 at 13:02, <[email protected]> wrote:
Quoted 7 lines · Jan Beulich
Quoted 7 lines
Quoted 1 line
Quoted 1 line · On 25.01.17 at 16:44, <[email protected]> wrote:
tools/fuzz/x86_instruction_emulator/x86-insn-emulator-fuzzer.c +0 -0
@@ -16,26 +16,75 @@
 
 #include "x86_emulate.h"
 
Quoted 8 lines

tools/fuzz/x86_instruction_emulator/x86-insn-emulator-fuzzer.c line 23

Quoted 6 lines
tools/fuzz/x86_instruction_emulator/x86-insn-emulator-fuzzer.c +5 -1
-static unsigned char data[4096];
+#include "../../../xen/include/asm-x86/msr-index.h"
+
+#ifndef offsetof
+#define offsetof(t,m) ((size_t)&(((t *)0)->m))
+#endif

Why can't you include the standard header for this?

Quoted 4 lines

[Responding because this is code I wrote]

Because I didn't know where it was. What's the best thing to include to get this?

The standard says stddef.h.

Quoted 8 lines
Quoted 8 lines
Quoted 7 lines
tools/fuzz/x86_instruction_emulator/x86-insn-emulator-fuzzer.c +7 -0
+        return X86EMUL_EXCEPTION;
+    else
+    {
+        if ( input.data[data_index] > 0xc )
+            rc = X86EMUL_EXCEPTION;
+        else if ( input.data[data_index] > 0x8 )
+            rc = X86EMUL_UNHANDLEABLE;
Quoted 7 lines
Quoted 1 line

How were these numbers chosen?

The idea here is to give the fuzzer a way of (blindly) changing whether the operation succeeds or how it fails in a "modular" way, by just "consuming" one byte from the input. The numbers are meant to make random values roughly 50% succeed, 25% generate an exception, and 25% return unhandleable.

But the vast majority of cases would fall in the exception group. Did you perhaps mean 0xc0 and 0x80 then? In any event a comment would be nice to clarify the intention.

Quoted 19 lines
Quoted 10 lines
Quoted 10 lines
tools/fuzz/x86_instruction_emulator/x86-insn-emulator-fuzzer.c +10 -0
+static int fuzz_cpuid(
+    uint32_t leaf,
+    uint32_t subleaf,
+    struct cpuid_leaf *res,
+    struct x86_emulate_ctxt *ctxt)
+{
+    int rc;
+
+    rc = maybe_fail("cpuid", true);
+
Quoted 8 lines

tools/fuzz/x86_instruction_emulator/x86-insn-emulator-fuzzer.c static int fuzz_cmpxchg( · line 247

Quoted 5 lines
tools/fuzz/x86_instruction_emulator/x86-insn-emulator-fuzzer.c +5 -0
+    if ( rc == X86EMUL_OKAY )
+        emul_test_cpuid(leaf, subleaf, res, ctxt);
+
+    return rc;
+}

Careful here: ->cpuid() is documented to only be allowed to fail with X86EMUL_EXCEPTION.

Quoted 2 lines

But at the moment, the emulator seems to function properly even if you return UNHANDLEABLE. This is probably more robust than otherwise.

Hmm, yes and no. Considering it being documented that way, someone adding an ASSERT() to that effect would be a legitimate thing to do, yet would result in aborts for suitable input here.

Quoted 15 lines
Quoted 6 lines
Quoted 6 lines
tools/fuzz/x86_instruction_emulator/x86-insn-emulator-fuzzer.c +6 -0
+static void setup_fpu_exception_handler(void)
+{
+    /* FIXME - just disable exceptions for now */
+    unsigned long a;
+
+    asm volatile ( "fnclex");
Quoted 8 lines

tools/fuzz/x86_instruction_emulator/x86-insn-emulator-fuzzer.c static int fuzz_cmpxchg( · line 457

Quoted 4 lines
tools/fuzz/x86_instruction_emulator/x86-insn-emulator-fuzzer.c +4 -0
+    a=0x3f;
+    asm volatile ( "fldcw %0" :: "m" (a));
+    a=0x1f80;
+    asm volatile ( "ldmxcsr %0" :: "m" (a) );

I don't see what you need "a" for in this function. And I also wonder how the FCW value was chosen (the MXCSR one is a commonly used value).

Quoted 1 line

I just copied it from somewhere. Could you suggest something better?

The FCW_DEFAULT (0x037f) value from the hypervisor.

Quoted 25 lines
Quoted 25 lines
Quoted 7 lines
tools/fuzz/x86_instruction_emulator/x86-insn-emulator-fuzzer.c +7 -0
+/*
+ * Constrain input to architecturally-possible states where
+ * the emulator relies on these
+ *
+ * In general we want the emulator to be as absolutely robust as
+ * possible; which means that we want to minimize the number of things
+ * it assumes about the input state.  Tesing this means minimizing and

tools/fuzz/x86_instruction_emulator/x86-insn-emulator-fuzzer.c static struct x86_emulate_ops fuzz_emulops = { · line 615

Quoted 10 lines
tools/fuzz/x86_instruction_emulator/x86-insn-emulator-fuzzer.c +10 -0
+ * removing as much of the input constraints as possible.
+ *
+ * So we only add constraints that (in general) have been proven to
+ * cause crashes in the emulator.
+ *
+ * For future reference: other constraints which might be necessary at
+ * some point:
+ *
+ * - EFER.LMA => !EFLAGS.NT
+ * - In VM86 mode (and real mode?), force segment...

As per commit 05118b1596 real mode should not be included here.

Quoted 4 lines
tools/fuzz/x86_instruction_emulator/x86-insn-emulator-fuzzer.c +4 -0
+ *  - ...access rights to 0xf3
+ *  - ...limits to 0xffff
+ *  - ...bases to below 1Mb, 16-byte aligned
+ *  - ...selectors to (base >> 4)
Quoted 7 lines
Quoted 2 lines

Most of this does not match the implementation (which only clear DB for CS and SS).

You mean that the code here doesn't actually do this? Yes, that's what the comment says -- "For future reference, other constraints which may be necessary at some point" -- that is, restrictions which exist in the architecture but do not (yet) exist in this code.

Oh, yes, I'm sorry. When coming back up here from below (where the respective code is) I didn't go back far enough in the comment to note the "For future reference:".

Quoted 14 lines
Quoted 5 lines
Quoted 5 lines
tools/fuzz/x86_instruction_emulator/x86-insn-emulator-fuzzer.c +5 -0
+ */
+void sanitize_input(struct x86_emulate_ctxt *ctxt) {
+    struct cpu_user_regs *regs = &input.regs;
+    unsigned long bitmap = input.options;
+
Quoted 8 lines

tools/fuzz/x86_instruction_emulator/x86-insn-emulator-fuzzer.c static struct x86_emulate_ops fuzz_emulops = { · line 629

Quoted 5 lines
tools/fuzz/x86_instruction_emulator/x86-insn-emulator-fuzzer.c +5 -0
+    input.options &=
+        ~((1<<HOOK_read)|
+          (1<<HOOK_write)|
+          (1<<HOOK_cmpxchg)|
+          (1<<HOOK_insn_fetch));

Ah, this addresses one of the earlier remarks. However, ->write and ->cmpxchg have become optional a little while ago.

Quoted 2 lines

And yet, empirically, the emulator crashes if you don't have them. If this isn't expected, we should submit some patches.

Is that the case now, or was that when you wrote this code (before 4.8 went out)? As said, the change isn't all that old.

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

#21 lore raw

Re: [PATCH 5/7] fuzz/x86emul: update fuzzer

Quoted 16 lines · Jan Beulich
Quoted 8 lines
Quoted 8 lines
Quoted 7 lines
tools/fuzz/x86_instruction_emulator/x86-insn-emulator-fuzzer.c +7 -0
+        return X86EMUL_EXCEPTION;
+    else
+    {
+        if ( input.data[data_index] > 0xc )
+            rc = X86EMUL_EXCEPTION;
+        else if ( input.data[data_index] > 0x8 )
+            rc = X86EMUL_UNHANDLEABLE;
Quoted 7 lines
Quoted 1 line

How were these numbers chosen?

The idea here is to give the fuzzer a way of (blindly) changing whether the operation succeeds or how it fails in a "modular" way, by just "consuming" one byte from the input. The numbers are meant to make random values roughly 50% succeed, 25% generate an exception, and 25% return unhandleable.

Quoted 3 lines

But the vast majority of cases would fall in the exception group. Did you perhaps mean 0xc0 and 0x80 then? In any event a comment would be nice to clarify the intention.

Oh, yes -- that should definitely be 0xc0 and 0x80. :-)

Quoted 19 lines
Quoted 19 lines
Quoted 18 lines
Quoted 5 lines
tools/fuzz/x86_instruction_emulator/x86-insn-emulator-fuzzer.c +5 -0
+static int fuzz_cpuid(
+    uint32_t leaf,
+    uint32_t subleaf,
+    struct cpuid_leaf *res,
+    struct x86_emulate_ctxt *ctxt)

tools/fuzz/x86_instruction_emulator/x86-insn-emulator-fuzzer.c static int fuzz_cmpxchg( · line 247

Quoted 10 lines
tools/fuzz/x86_instruction_emulator/x86-insn-emulator-fuzzer.c +10 -0
+{
+    int rc;
+
+    rc = maybe_fail("cpuid", true);
+
+    if ( rc == X86EMUL_OKAY )
+        emul_test_cpuid(leaf, subleaf, res, ctxt);
+
+    return rc;
+}

Careful here: ->cpuid() is documented to only be allowed to fail with X86EMUL_EXCEPTION.

Quoted 6 lines
Quoted 2 lines

But at the moment, the emulator seems to function properly even if you return UNHANDLEABLE. This is probably more robust than otherwise.

Hmm, yes and no. Considering it being documented that way, someone adding an ASSERT() to that effect would be a legitimate thing to do, yet would result in aborts for suitable input here.

Yes. And I think that makes sense for how I was running it initially, where the main person running it knows the delta between what is "officially" required and what is actually required. But that probably needs revisiting.

I still think that knowing empirically what the emulation code will accept (as opposed to what it's supposed to accept) is a useful thing, but false positives aren't free. If we had a list somewhere of the places we deviate from the spec, it would be easier to determine if a particular crash was a false positive or not.

I'll leave it for Wei now to decide whether he wants to document deviations or just follow the spec in this instance.

Quoted 14 lines
Quoted 14 lines
Quoted 13 lines

tools/fuzz/x86_instruction_emulator/x86-insn-emulator-fuzzer.c static struct x86_emulate_ops fuzz_emulops = { · line 629

Quoted 10 lines

+ */

tools/fuzz/x86_instruction_emulator/x86-insn-emulator-fuzzer.c +9 -0
+void sanitize_input(struct x86_emulate_ctxt *ctxt) {
+    struct cpu_user_regs *regs = &input.regs;
+    unsigned long bitmap = input.options;
+
+    input.options &=
+        ~((1<<HOOK_read)|
+          (1<<HOOK_write)|
+          (1<<HOOK_cmpxchg)|
+          (1<<HOOK_insn_fetch));

Ah, this addresses one of the earlier remarks. However, ->write and ->cmpxchg have become optional a little while ago.

Quoted 5 lines
Quoted 2 lines

And yet, empirically, the emulator crashes if you don't have them. If this isn't expected, we should submit some patches.

Is that the case now, or was that when you wrote this code (before 4.8 went out)? As said, the change isn't all that old.

Oh, right -- I interpreted "a little while ago" as >6 months. :-) Yes, if it's supposed to handle not having write and cmpxchg, then we should test not having them.

-George

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

author
#22 lore raw

PATCH 6/7 fuzz/x86emul: print out minimal input size

In reply to Wei Liu

... so that users can know how big the initial input should be.

---
 .../fuzz/x86_instruction_emulator/afl-x86-insn-emulator-fuzzer.c  | 8 ++++++++
 tools/fuzz/x86_instruction_emulator/x86-insn-emulator-fuzzer.c    | 5 +++++
 2 files changed, 13 insertions(+)
tools/fuzz/x86_instruction_emulator/afl-x86-insn-emulator-fuzzer.c +8 -0
index b5668c11e7..655315f9c1 100644
@@ -2,8 +2,10 @@
 #include <stdint.h>
 #include <stdio.h>
 #include <stdlib.h>
+#include <string.h>
 
 extern int LLVMFuzzerTestOneInput(const uint8_t *data_p, size_t size);
+extern unsigned int fuzz_minimal_input_size(void);
 
 #define INPUT_SIZE  4096
 static uint8_t input[INPUT_SIZE];
@@ -19,6 +21,12 @@ int main(int argc, char **argv)
         exit(-1);
     }
 
+    if ( !strcmp(argv[1], "--min-input-size") )
+    {
+        printf("%u\n", fuzz_minimal_input_size());
+        exit(0);
+    }
+
     fp = fopen(argv[1], "rb");
     if ( fp == NULL )
     {
tools/fuzz/x86_instruction_emulator/x86-insn-emulator-fuzzer.c +5 -0
index 8ca0421f60..3a731b28f2 100644
@@ -717,6 +717,11 @@ int LLVMFuzzerTestOneInput(const uint8_t *data_p, size_t size)
     return 0;
 }
 
+unsigned int fuzz_minimal_input_size(void)
+{
+    return DATA_OFFSET + 1;
+}
+
 /*
  * Local variables:
  * mode: C
Signature · 7 lines
2.11.0


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
author
#23 lore raw

PATCH 7/7 fuzz: update README.afl example

In reply to Wei Liu
---
 tools/fuzz/README.afl | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
tools/fuzz/README.afl +3 -2
index 431b4a8cbf..68e0fa396f 100644
@@ -20,9 +20,10 @@ Use the x86 instruction emulator fuzzer as an example.
    $ make distclean
    $ make CC=$AFLPATH/afl-gcc afl # produces afl-x86-insn-emulator-fuzzer
 
-3. provide initial test case:
+3. provide initial test case (fuzzer dependent, see afl-*.c):
    $ mkdir testcase_dir
-   $ echo -n -e '\xc3' > testcase_dir/ret.bin
+   $ dd if=/dev/urandom of=testcase_dir/rand.bin \
+       bs=`./afl-x86-insn-emulator-fuzzer --min-input-size` count=1
 
 4. run the fuzzer with AFL:
    $ $AFLPATH/afl-fuzz -m none -t 1000 -i testcase_dir -o findings_dir -- \
Signature · 7 lines
2.11.0


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel