fuzz: update x86emul fuzzer
v3 cover fuzz: update x86emul fuzzer
The first few patches refactor x86emul code so that more code can be shared between xen and userspace tools.
I have run XTF suite (tests subject to availability on the testbox I use, and xsa-195 was skipped because qemu segfault -- a known issue) against this series, no issue is found.
Please see individual patch for changelog.
Wei.
---
Cc: Ian Jackson <[email protected]>
Cc: Jan Beulich <[email protected]>
Cc: Andrew Cooper <[email protected]>
Cc: George Dunlap <[email protected]>
Wei Liu (11):
x86emul/test: add missing dependency for x86_emulate.o
x86: extract macros to x86-defns.h
x86: extract vendor numeric id to x86-vendors.h x86emul/test: use x86-vendors.h
x86emul: use eflags definitions in x86-defns.h
x86emul: use msr definitions in msr-index.h
x86: add UMIP CR4 bit
x86emul: use CR definitions in x86-defns.h fuzz/x86emul: update fuzzer
fuzz/x86emul: print out minimal input size
fuzz: update README.afl example
tools/fuzz/README.afl | 5 +- tools/fuzz/x86_instruction_emulator/Makefile | 9 +- .../afl-x86-insn-emulator-fuzzer.c | 8 + .../x86-insn-emulator-fuzzer.c | 666 +++++++++++++++++++-- tools/tests/x86_emulator/Makefile | 11 +- tools/tests/x86_emulator/test_x86_emulator.c | 9 - tools/tests/x86_emulator/x86_emulate.c | 3 - tools/tests/x86_emulator/x86_emulate.h | 9 +- xen/arch/x86/x86_emulate/x86_emulate.c | 403 ++++++------- xen/include/asm-x86/processor.h | 73 +-- xen/include/asm-x86/x86-defns.h | 69 +++ xen/include/asm-x86/x86-vendors.h | 13 + 12 files changed, 900 insertions(+), 378 deletions(-) create mode 100644 xen/include/asm-x86/x86-defns.h create mode 100644 xen/include/asm-x86/x86-vendors.h
Signature · 7 lines
2.11.0 _______________________________________________ Xen-devel mailing list [email protected] https://lists.xen.org/xen-devel
PATCH v3 1/11 x86emul/test: add missing dependency for x86_emulate.o
f4497d6b74 added x86_emulate.h private header but didn't add dependency for it.
---
Cc: Jan Beulich <[email protected]>
Cc: Andrew Cooper <[email protected]>
--- tools/tests/x86_emulator/Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
tools/tests/x86_emulator/Makefile +1 -1
@@ -45,7 +45,7 @@ x86_emulate/x86_emulate.c x86_emulate/x86_emulate.h: HOSTCFLAGS += $(CFLAGS_xeninclude) -x86_emulate.o: x86_emulate.c x86_emulate/x86_emulate.c x86_emulate/x86_emulate.h +x86_emulate.o: x86_emulate.c x86_emulate.h x86_emulate/x86_emulate.c x86_emulate/x86_emulate.h $(HOSTCC) $(HOSTCFLAGS) -D__XEN_TOOLS__ -c -g -o $@ $< test_x86_emulator.o: test_x86_emulator.c blowfish.h x86_emulate/x86_emulate.h
Signature · 7 lines
2.11.0 _______________________________________________ Xen-devel mailing list [email protected] https://lists.xen.org/xen-devel
Re: [PATCH v3 01/11] x86emul/test: add missing dependency for x86_emulate.o
tools/tests/x86_emulator/Makefile x86_emulate/x86_emulate.c x86_emulate/x86_emulate.h: · line 51
Quoted 12 lines
Quoted 1 line
Quoted 1 line · On 01.02.17 at 13:02, <[email protected]> wrote:
tools/tests/x86_emulator/Makefile +1
-1@@ -45,7 +45,7 @@ x86_emulate/x86_emulate.c x86_emulate/x86_emulate.h: HOSTCFLAGS += $(CFLAGS_xeninclude) -x86_emulate.o: x86_emulate.c x86_emulate/x86_emulate.c x86_emulate/x86_emulate.h +x86_emulate.o: x86_emulate.c x86_emulate.h x86_emulate/x86_emulate.c x86_emulate/x86_emulate.h $(HOSTCC) $(HOSTCFLAGS) -D__XEN_TOOLS__ -c -g -o $@ $< test_x86_emulator.o: test_x86_emulator.c blowfish.h x86_emulate/x86_emulate.h
I think this latter one should be updated too. Which then calls for macroizing:
x86_emulate.h := x86_emulate.h x86_emulate/x86_emulate.h
Jan
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
Re: [PATCH v3 01/11] x86emul/test: add missing dependency for x86_emulate.o
Quoted 11 lines · Jan Beulich
Quoted 11 lines
Quoted 1 line
Quoted 1 line · On 01.02.17 at 13:02, <[email protected]> wrote:
tools/tests/x86_emulator/Makefile +1
-1@@ -45,7 +45,7 @@ x86_emulate/x86_emulate.c x86_emulate/x86_emulate.h: HOSTCFLAGS += $(CFLAGS_xeninclude) -x86_emulate.o: x86_emulate.c x86_emulate/x86_emulate.c x86_emulate/x86_emulate.h +x86_emulate.o: x86_emulate.c x86_emulate.h x86_emulate/x86_emulate.c x86_emulate/x86_emulate.h $(HOSTCC) $(HOSTCFLAGS) -D__XEN_TOOLS__ -c -g -o $@ $<
Quoted 9 lines
tools/tests/x86_emulator/Makefile x86_emulate/x86_emulate.c x86_emulate/x86_emulate.h: · line 51
Quoted 1 line
tools/tests/x86_emulator/Makefile +0
-0test_x86_emulator.o: test_x86_emulator.c blowfish.h x86_emulate/x86_emulate.hI think this latter one should be updated too. Which then calls for macroizing:
x86_emulate.h:=x86_emulate.hx86_emulate/x86_emulate.hJan
Not sure I understand what you want, but using x86_emulate.h as variable name looks a bit weird. Anyway, this is what I come up with:
From 4bd9c4cbe3ca6197d38355eac24023ccf7b24335 Mon Sep 17 00:00:00 2001
From: Wei Liu <[email protected]>
Date: Wed, 1 Feb 2017 11:49:41 +0000
Subject: [PATCH] x86emul/test: add missing dependency for x86_emulate.o
f4497d6b74 added x86_emulate.h private header but didn't add dependency for it.
Use macro to reduce repetition.
--- tools/tests/x86_emulator/Makefile | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)
tools/tests/x86_emulator/Makefile +4 -2
@@ -45,8 +45,10 @@ x86_emulate/x86_emulate.c x86_emulate/x86_emulate.h: HOSTCFLAGS += $(CFLAGS_xeninclude) -x86_emulate.o: x86_emulate.c x86_emulate/x86_emulate.c x86_emulate/x86_emulate.h +x86_emulate.h := x86_emulate.h x86_emulate/x86_emulate.h + +x86_emulate.o: x86_emulate.c x86_emulate/x86_emulate.c $(x86_emulate.h) $(HOSTCC) $(HOSTCFLAGS) -D__XEN_TOOLS__ -c -g -o $@ $< -test_x86_emulator.o: test_x86_emulator.c blowfish.h x86_emulate/x86_emulate.h +test_x86_emulator.o: test_x86_emulator.c blowfish.h $(x86_emulate.h) $(HOSTCC) $(HOSTCFLAGS) -c -g -o $@ $<
Signature · 7 lines
2.11.0 _______________________________________________ Xen-devel mailing list [email protected] https://lists.xen.org/xen-devel
Re: [PATCH v3 01/11] x86emul/test: add missing dependency for x86_emulate.o
Quoted 18 lines
Quoted 1 line
Quoted 1 line · On 02.02.17 at 18:43, <[email protected]> wrote:
Quoted 16 lines · Jan Beulich
tools/tests/x86_emulator/Makefile x86_emulate/x86_emulate.c x86_emulate/x86_emulate.h: · line 51
Quoted 12 lines
Quoted 1 line
Quoted 1 line · On 01.02.17 at 13:02, <[email protected]> wrote:
tools/tests/x86_emulator/Makefile +1
-1@@ -45,7 +45,7 @@ x86_emulate/x86_emulate.c x86_emulate/x86_emulate.h: HOSTCFLAGS += $(CFLAGS_xeninclude) -x86_emulate.o: x86_emulate.c x86_emulate/x86_emulate.c x86_emulate/x86_emulate.h +x86_emulate.o: x86_emulate.c x86_emulate.h x86_emulate/x86_emulate.c x86_emulate/x86_emulate.h $(HOSTCC) $(HOSTCFLAGS) -D__XEN_TOOLS__ -c -g -o $@ $< test_x86_emulator.o: test_x86_emulator.c blowfish.h x86_emulate/x86_emulate.hI think this latter one should be updated too. Which then calls for macroizing:
Quoted 4 lines
Quoted 1 line
x86_emulate.h:=x86_emulate.hx86_emulate/x86_emulate.hNot sure I understand what you want, but using
x86_emulate.has variable name looks a bit weird.
Well, that name very clearly identifies what the variable is to represent. If the dot in the name is really disturbing, I could live with x86_emulate-h as the name. Your proposed adjustment looks exactly like I was hoping for.
Jan
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
PATCH v3 2/11 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.
---
Cc: Jan Beulich <[email protected]>
Cc: Andrew Cooper <[email protected]>
v3: Don't move TRAP_*
--- xen/include/asm-x86/processor.h | 65 ++------------------------------------- xen/include/asm-x86/x86-defns.h | 68 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 70 insertions(+), 63 deletions(-) create mode 100644 xen/include/asm-x86/x86-defns.h
xen/include/asm-x86/processor.h +2 -63
@@ -16,6 +16,8 @@#include <asm/desc.h> #endif +#include "x86-defns.h" +/* * CPU vendor IDs */
@@ -25,69 +27,6 @@#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.
xen/include/asm-x86/x86-defns.h new file +68 -0
68 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
Re: [PATCH v3 02/11] x86: extract macros to x86-defns.h
xen/include/asm-x86/processor.h line 19
Quoted 8 lines
Quoted 1 line
Quoted 1 line · On 01.02.17 at 13:02, <[email protected]> wrote:
xen/include/asm-x86/processor.h +1
-0@@ -16,6 +16,8 @@#include <asm/desc.h> #endif +#include "x86-defns.h"
This should be <asm/x86-defns.h>.
With that adjusted,
Jan
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
Re: [PATCH v3 02/11] x86: extract macros to x86-defns.h
Quoted 10 lines · Jan Beulich
xen/include/asm-x86/processor.h line 19
Quoted 8 lines
Quoted 1 line
Quoted 1 line · On 01.02.17 at 13:02, <[email protected]> wrote:
xen/include/asm-x86/processor.h +1
-0@@ -16,6 +16,8 @@#include <asm/desc.h> #endif +#include "x86-defns.h" This should be <
asm/x86-defns.h>.
Right. I will fix the inclusion of x86-vendors.h as well.
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
PATCH v3 3/11 x86: extract vendor numeric id to x86-vendors.h
In reply to Wei LiuThey will be shared between xen and userspace programs.
This is not strictly necessary, but it helps reduce overall code size.
No functional change.
--- xen/include/asm-x86/processor.h | 10 +--------- xen/include/asm-x86/x86-vendors.h | 13 +++++++++++++ 2 files changed, 14 insertions(+), 9 deletions(-) create mode 100644 xen/include/asm-x86/x86-vendors.h
xen/include/asm-x86/processor.h +1 -9
@@ -17,15 +17,7 @@#endif #include "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 +#include "x86-vendors.h" /*
xen/include/asm-x86/x86-vendors.h new file +13 -0
@@ -0,0 +1,13 @@ +#ifndef __XEN_X86_VENDORS_H__ +#define __XEN_X86_VENDORS_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 + +#endif /* __XEN_X86_VENDORS_H__ */
Signature · 7 lines
2.11.0 _______________________________________________ Xen-devel mailing list [email protected] https://lists.xen.org/xen-devel
PATCH v3 4/11 x86emul/test: use x86-vendors.h
In reply to Wei LiuNo functional change.
---
Cc: Jan Beulich <[email protected]>
Cc: Andrew Cooper <[email protected]>
V3: link asm-x86 to working directory
--- tools/fuzz/x86_instruction_emulator/Makefile | 9 +++++++-- tools/tests/x86_emulator/Makefile | 9 +++++++-- tools/tests/x86_emulator/x86_emulate.h | 7 ++----- 3 files changed, 16 insertions(+), 9 deletions(-)
tools/fuzz/x86_instruction_emulator/Makefile +7 -2
@@ -11,10 +11,15 @@ endif x86_emulate/x86_emulate.c x86_emulate/x86_emulate.h: [ -L x86_emulate ] || ln -sf $(XEN_ROOT)/xen/arch/x86/x86_emulate . +asm/x86-vendors.h: + [ -L asm ] || ln -sf $(XEN_ROOT)/xen/include/asm-x86 asm + x86_emulate.c x86_emulate.h: %: [ -L $* ] || ln -sf $(XEN_ROOT)/tools/tests/x86_emulator/$* -CFLAGS += $(CFLAGS_xeninclude) -D__XEN_TOOLS__ +CFLAGS += $(CFLAGS_xeninclude) -D__XEN_TOOLS__ -I. + +x86_emulate.h: asm/x86-vendors.h x86_emulate.o: x86_emulate.c x86_emulate.h x86_emulate/x86_emulate.c x86_emulate/x86_emulate.h
@@ -31,7 +36,7 @@ all: x86-instruction-emulator-fuzzer-all .PHONY: distclean distclean: clean - rm -f x86_emulate x86_emulate.c x86_emulate.h + rm -f x86_emulate x86_emulate.c x86_emulate.h asm .PHONY: clean clean:
tools/tests/x86_emulator/Makefile +7 -2
@@ -32,7 +32,7 @@ $(TARGET): x86_emulate.o test_x86_emulator.o .PHONY: clean clean: - rm -rf $(TARGET) *.o *~ core blowfish.h blowfish.bin x86_emulate + rm -rf $(TARGET) *.o *~ core blowfish.h blowfish.bin x86_emulate asm .PHONY: distclean distclean: clean
@@ -43,7 +43,12 @@ install: x86_emulate/x86_emulate.c x86_emulate/x86_emulate.h: [ -L x86_emulate ] || ln -sf $(XEN_ROOT)/xen/arch/x86/x86_emulate . -HOSTCFLAGS += $(CFLAGS_xeninclude) +asm/x86-vendors.h: + [ -L asm ] || ln -sf $(XEN_ROOT)/xen/include/asm-x86 asm + +x86_emulate.h: asm/x86-vendors.h + +HOSTCFLAGS += $(CFLAGS_xeninclude) -I. x86_emulate.o: x86_emulate.c x86_emulate.h x86_emulate/x86_emulate.c x86_emulate/x86_emulate.h $(HOSTCC) $(HOSTCFLAGS) -D__XEN_TOOLS__ -c -g -o $@ $<
tools/tests/x86_emulator/x86_emulate.h +2 -5
@@ -6,6 +6,8 @@#include <string.h> #include <xen/xen.h> +#include <asm/x86-vendors.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 16384bool emul_test_make_stack_executable (void );
Signature · 7 lines
2.11.0 _______________________________________________ Xen-devel mailing list [email protected] https://lists.xen.org/xen-devel
Re: [PATCH v3 04/11] x86emul/test: use x86-vendors.h
tools/fuzz/x86_instruction_emulator/Makefile endif · line 15
Quoted 9 lines
Quoted 1 line
Quoted 1 line · On 01.02.17 at 13:02, <[email protected]> wrote:
tools/fuzz/x86_instruction_emulator/Makefile +2
-0@@ -11,10 +11,15 @@ endif x86_emulate/x86_emulate.c x86_emulate/x86_emulate.h: [ -L x86_emulate ] || ln -sf $(XEN_ROOT)/xen/arch/x86/x86_emulate . +asm/x86-vendors.h: + [ -L asm ] || ln -sf $(XEN_ROOT)/xen/include/asm-x86 asm
Hmm, that's making all headers available. I don't really like it to be that broad. Andrew, what do you think
tools/fuzz/x86_instruction_emulator/Makefile endif · line 22
Quoted 4 lines
tools/fuzz/x86_instruction_emulator/Makefile +3
-1-CFLAGS += $(CFLAGS_xeninclude) -D__XEN_TOOLS__ +CFLAGS += $(CFLAGS_xeninclude) -D__XEN_TOOLS__ -I. + +x86_emulate.h: asm/x86-vendors.h
That's not a true dependency; I'd rather see this build on the suggested macroization (i.e. introduce it here too, and do the same for the non-fuzzing harness).
Jan
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
Re: [PATCH v3 04/11] x86emul/test: use x86-vendors.h
Quoted 11 lines · Jan Beulich
tools/fuzz/x86_instruction_emulator/Makefile endif · line 15
Quoted 9 lines
Quoted 1 line
Quoted 1 line · On 01.02.17 at 13:02, <[email protected]> wrote:
tools/fuzz/x86_instruction_emulator/Makefile +2
-0@@ -11,10 +11,15 @@ endif x86_emulate/x86_emulate.c x86_emulate/x86_emulate.h: [ -L x86_emulate ] || ln -sf $(XEN_ROOT)/xen/arch/x86/x86_emulate . +asm/x86-vendors.h: + [ -L asm ] || ln -sf $(XEN_ROOT)/xen/include/asm-x86 asmHmm, that's making all headers available. I don't really like it to be that broad. Andrew, what do you think
This is just a test utility. I think we can trust ourselves to only include appropriate headers.
This is simpler and easier than keeping a whitelist of permitted headers, which doesn't require any modification when we add a new header.
~Andrew
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
Re: [PATCH v3 04/11] x86emul/test: use x86-vendors.h
Quoted 14 lines
Quoted 1 line
Quoted 1 line · On 01.02.17 at 14:13, <[email protected]> wrote:
Quoted 11 lines · Jan Beulich
tools/fuzz/x86_instruction_emulator/Makefile endif · line 15
Quoted 9 lines
Quoted 1 line
Quoted 1 line · On 01.02.17 at 13:02, <[email protected]> wrote:
tools/fuzz/x86_instruction_emulator/Makefile +2
-0@@ -11,10 +11,15 @@ endif x86_emulate/x86_emulate.c x86_emulate/x86_emulate.h: [ -L x86_emulate ] || ln -sf $(XEN_ROOT)/xen/arch/x86/x86_emulate . +asm/x86-vendors.h: + [ -L asm ] || ln -sf $(XEN_ROOT)/xen/include/asm-x86 asmHmm, that's making all headers available. I don't really like it to be that broad. Andrew, what do you think
Quoted 5 lines
This is just a test utility. I think we can trust ourselves to only include appropriate headers.
This is simpler and easier than keeping a whitelist of permitted headers, which doesn't require any modification when we add a new header.
Well, okay then (albeit this "doesn't require any modification" doesn't hold with the dependency the patch also adds; perhaps midterm we should arrange for the compiler to generate dependencies for us).
Jan
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
Re: [PATCH v3 04/11] x86emul/test: use x86-vendors.h
In reply to Andrew CooperQuoted 13 lines · Andrew Cooper
Quoted 11 lines · Jan Beulich
tools/fuzz/x86_instruction_emulator/Makefile endif · line 15
Quoted 9 lines
Quoted 1 line
Quoted 1 line · On 01.02.17 at 13:02, <[email protected]> wrote:
tools/fuzz/x86_instruction_emulator/Makefile +2
-0@@ -11,10 +11,15 @@ endif x86_emulate/x86_emulate.c x86_emulate/x86_emulate.h: [ -L x86_emulate ] || ln -sf $(XEN_ROOT)/xen/arch/x86/x86_emulate . +asm/x86-vendors.h: + [ -L asm ] || ln -sf $(XEN_ROOT)/xen/include/asm-x86 asmHmm, that's making all headers available. I don't really like it to be that broad. Andrew, what do you think
Quoted 6 lines
This is just a test utility. I think we can trust ourselves to only include appropriate headers.
This is simpler and easier than keeping a whitelist of permitted headers, which doesn't require any modification when we add a new header.
Yes, this is exactly the reason why I did it like this.
Wei.
Quoted 1 line
~Andrew
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
PATCH v3 5/11 x86emul: use eflags definitions in x86-defns.h
In reply to Wei LiuBasically this patch does 's/EFLG_/X86_EFLAGS_/g' and with indentation fixed up. And remove the duplicates in x86_emualte.c. This in turn requires userspace test harness to include x86-defns.h. Also remove a few duplicates in userspace harness program.
No functional change.
---
v3:
1. Adapt build system to previous patch
2. Add spaces around "|"
--- tools/fuzz/x86_instruction_emulator/Makefile | 4 +- tools/tests/x86_emulator/Makefile | 4 +- tools/tests/x86_emulator/test_x86_emulator.c | 9 - tools/tests/x86_emulator/x86_emulate.h | 1 + xen/arch/x86/x86_emulate/x86_emulate.c | 321 +++++++++++++-------------- 5 files changed, 160 insertions(+), 179 deletions(-)
tools/fuzz/x86_instruction_emulator/Makefile +2 -2
@@ -11,7 +11,7 @@ endif x86_emulate/x86_emulate.c x86_emulate/x86_emulate.h: [ -L x86_emulate ] || ln -sf $(XEN_ROOT)/xen/arch/x86/x86_emulate . -asm/x86-vendors.h: +asm/x86-vendors.h asm/x86-defns.h: [ -L asm ] || ln -sf $(XEN_ROOT)/xen/include/asm-x86 asm x86_emulate.c x86_emulate.h: %:
@@ -19,7 +19,7 @@ x86_emulate.c x86_emulate.h: %: CFLAGS += $(CFLAGS_xeninclude) -D__XEN_TOOLS__ -I. -x86_emulate.h: asm/x86-vendors.h +x86_emulate.h: asm/x86-vendors.h asm/x86-defns.h x86_emulate.o: x86_emulate.c x86_emulate.h x86_emulate/x86_emulate.c x86_emulate/x86_emulate.h
tools/tests/x86_emulator/Makefile +2 -2
@@ -43,10 +43,10 @@ install: x86_emulate/x86_emulate.c x86_emulate/x86_emulate.h: [ -L x86_emulate ] || ln -sf $(XEN_ROOT)/xen/arch/x86/x86_emulate . -asm/x86-vendors.h: +asm/x86-vendors.h asm/x86-defns.h: [ -L asm ] || ln -sf $(XEN_ROOT)/xen/include/asm-x86 asm -x86_emulate.h: asm/x86-vendors.h +x86_emulate.h: asm/x86-vendors.h asm/x86-defns.h HOSTCFLAGS += $(CFLAGS_xeninclude) -I.
tools/tests/x86_emulator/test_x86_emulator.c +0 -9
@@ -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(
tools/tests/x86_emulator/x86_emulate.h +1 -0
@@ -6,6 +6,7 @@#include <string.h> #include <xen/xen.h> +#include <asm/x86-defns.h> #include <asm/x86-vendors.h> #define BUG () abort()
xen/arch/x86/x86_emulate/x86_emulate.c +155 -166
895 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
PATCH v3 6/11 x86emul: use msr definitions in msr-index.h
In reply to Wei LiuChange the names used in code according to numeric values. Remove the now unused macros in x86_emualte.c and fix indentation. This in turns requires including msr-index.h and removing duplicates in userspace x86_emulate.c in userspace harness program.
No functional change.
---
v3:
1. Adapt build system to previous patch
--- tools/fuzz/x86_instruction_emulator/Makefile | 4 +-- tools/tests/x86_emulator/Makefile | 4 +-- tools/tests/x86_emulator/x86_emulate.c | 3 -- tools/tests/x86_emulator/x86_emulate.h | 1 + xen/arch/x86/x86_emulate/x86_emulate.c | 43 ++++++++++------------------ 5 files changed, 20 insertions(+), 35 deletions(-)
tools/fuzz/x86_instruction_emulator/Makefile +2 -2
@@ -11,7 +11,7 @@ endif x86_emulate/x86_emulate.c x86_emulate/x86_emulate.h: [ -L x86_emulate ] || ln -sf $(XEN_ROOT)/xen/arch/x86/x86_emulate . -asm/x86-vendors.h asm/x86-defns.h: +asm/x86-vendors.h asm/x86-defns.h asm/msr-index.h: [ -L asm ] || ln -sf $(XEN_ROOT)/xen/include/asm-x86 asm x86_emulate.c x86_emulate.h: %:
@@ -19,7 +19,7 @@ x86_emulate.c x86_emulate.h: %: CFLAGS += $(CFLAGS_xeninclude) -D__XEN_TOOLS__ -I. -x86_emulate.h: asm/x86-vendors.h asm/x86-defns.h +x86_emulate.h: asm/x86-vendors.h asm/x86-defns.h asm/msr-index.h x86_emulate.o: x86_emulate.c x86_emulate.h x86_emulate/x86_emulate.c x86_emulate/x86_emulate.h
tools/tests/x86_emulator/Makefile +2 -2
@@ -43,10 +43,10 @@ install: x86_emulate/x86_emulate.c x86_emulate/x86_emulate.h: [ -L x86_emulate ] || ln -sf $(XEN_ROOT)/xen/arch/x86/x86_emulate . -asm/x86-vendors.h asm/x86-defns.h: +asm/x86-vendors.h asm/x86-defns.h asm/msr-index.h: [ -L asm ] || ln -sf $(XEN_ROOT)/xen/include/asm-x86 asm -x86_emulate.h: asm/x86-vendors.h asm/x86-defns.h +x86_emulate.h: asm/x86-vendors.h asm/x86-defns.h asm/msr-index.h HOSTCFLAGS += $(CFLAGS_xeninclude) -I.
tools/tests/x86_emulator/x86_emulate.c +0 -3
@@ -2,9 +2,6 @@#include <sys/mman.h> -#define EFER_SCE (1 << 0) -#define EFER_LMA (1 << 10) -#define cpu_has_amd_erratum (nr) 0#define mark_regs_dirty (r) ((void)(r))#define cpu_has_mpx false
tools/tests/x86_emulator/x86_emulate.h +1 -0
@@ -6,6 +6,7 @@#include <string.h> #include <xen/xen.h> +#include <asm/msr-index.h> #include <asm/x86-defns.h> #include <asm/x86-vendors.h>
xen/arch/x86/x86_emulate/x86_emulate.c +15 -28
101 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
PATCH v3 7/11 x86: add UMIP CR4 bit
In reply to Wei LiuIt will be used later to remove duplicates in x86emul.
--- xen/include/asm-x86/x86-defns.h | 1 + 1 file changed, 1 insertion(+)
xen/include/asm-x86/x86-defns.h +1 -0
@@ -56,6 +56,7 @@#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_UMIP 0x00000800/* enable UMIP */ #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 */
Signature · 7 lines
2.11.0 _______________________________________________ Xen-devel mailing list [email protected] https://lists.xen.org/xen-devel
PATCH v3 8/11 x86emul: use CR definitions in x86-defns.h
In reply to Wei LiuAnd remove the duplicates.
No functional change.
--- xen/arch/x86/x86_emulate/x86_emulate.c | 45 ++++++++++++---------------------- 1 file changed, 15 insertions(+), 30 deletions(-)
xen/arch/x86/x86_emulate/x86_emulate.c +15 -30
@@ -415,21 +415,6 @@ typedef union {# define ASM_FLAG_OUT (yes, no) no#endif -/* Control register flags. */ -#define CR0_PE (1<<0) -#define CR0_MP (1<<1) -#define CR0_EM (1<<2) -#define CR0_TS (1<<3) - -#define CR4_VME (1<<0) -#define CR4_PVI (1<<1) -#define CR4_TSD (1<<2) -#define CR4_OSFXSR (1<<9) -#define CR4_OSXMMEXCPT (1<<10) -#define CR4_UMIP (1<<11) -#define CR4_FSGSBASE (1<<16) -#define CR4_OSXSAVE (1<<18) -/* Floating point status word definitions. */ #define FSW_ES (1U << 7)
@@ -810,7 +795,7 @@ static int _get_fpu(if ( rc !=X86EMUL_OKAY )return rc;generate_exception_if (!(cr4 & ((type == X86EMUL_FPU_xmm) - ?CR4_OSFXSR :CR4_OSXSAVE )), + ?X86_CR4_OSFXSR :X86_CR4_OSXSAVE )),EXC_UD ); }
@@ -820,16 +805,16 @@ static int _get_fpu(if ( type >= X86EMUL_FPU_ymm ) {/* Should be unreachable if VEX decoding is working correctly. */ -ASSERT ((cr0 &CR0_PE ) && !(ctxt->regs->_eflags &X86_EFLAGS_VM )); +ASSERT ((cr0 &X86_CR0_PE ) && !(ctxt->regs->_eflags &X86_EFLAGS_VM )); } -if ( cr0 &CR0_EM ) +if ( cr0 &X86_CR0_EM ) {generate_exception_if (type == X86EMUL_FPU_fpu,EXC_NM );generate_exception_if (type == X86EMUL_FPU_mmx,EXC_UD );generate_exception_if (type == X86EMUL_FPU_xmm,EXC_UD ); } -generate_exception_if ((cr0 &CR0_TS ) && - (type != X86EMUL_FPU_wait || (cr0 &CR0_MP )), +generate_exception_if ((cr0 &X86_CR0_TS ) && + (type != X86EMUL_FPU_wait || (cr0 &X86_CR0_MP )),EXC_NM ); }
@@ -852,7 +837,7 @@ do { \_put_fpu (); \if ( (_fic)->exn_raised ==EXC_XM && ops->read_cr && \ ops->read_cr (4 , &cr4, ctxt) ==X86EMUL_OKAY && \ - !(cr4 &CR4_OSXMMEXCPT ) ) \ + !(cr4 &X86_CR4_OSXMMEXCPT ) ) \ (_fic)->exn_raised =EXC_UD ; \generate_exception_if ((_fic)->exn_raised >=0 , \ (_fic)->exn_raised); \
@@ -1184,7 +1169,7 @@ _mode_iopl( rc = ops->read_cr (4 , &cr4, ctxt); \if ( rc !=X86EMUL_OKAY ) goto done; \ } \ - !!(cr4 & (_regs._eflags &X86_EFLAGS_VM ?CR4_VME :CR4_PVI )); \ + !!(cr4 & (_regs._eflags &X86_EFLAGS_VM ?X86_CR4_VME :X86_CR4_PVI )); \ }) static int ioport_access_check(
@@ -1258,7 +1243,7 @@ in_realmode(return 0 ; rc = ops->read_cr (0 , &cr0, ctxt); -return (!rc && !(cr0 &CR0_PE )); +return (!rc && !(cr0 &X86_CR0_PE )); } static bool
@@ -1726,7 +1711,7 @@ static bool umip_active(struct x86_emulate_ctxt *ctxt,/* Intentionally not using mode_ring0() here to avoid its fail_if(). */ return get_cpl (ctxt, ops) >0 && ops->read_cr && ops->read_cr (4 , &cr4, ctxt) ==X86EMUL_OKAY && - (cr4 &CR4_UMIP ); + (cr4 &X86_CR4_UMIP ); }/* Inject a software interrupt/exception, emulating if needed. */
@@ -3345,7 +3330,7 @@ x86_emulate(if ( rc !=X86EMUL_OKAY ) goto done; } -generate_exception_if (!(cr4 &CR4_VME ),EXC_GP ,0 ); +generate_exception_if (!(cr4 &X86_CR4_VME ),EXC_GP ,0 ); src.val = (_regs.flags & ~X86_EFLAGS_IF ) |X86_EFLAGS_IOPL ;if ( _regs._eflags &X86_EFLAGS_VIF ) src.val |=X86_EFLAGS_IF ;
@@ -3368,7 +3353,7 @@ x86_emulate(if ( rc !=X86EMUL_OKAY ) goto done; } -generate_exception_if (!(cr4 &CR4_VME ) && +generate_exception_if (!(cr4 &X86_CR4_VME ) &&MASK_EXTR (_regs._eflags,X86_EFLAGS_IOPL ) !=3 ,EXC_GP ,0 ); }
@@ -3385,7 +3370,7 @@ x86_emulate(if ( op_bytes ==2 ) { dst.val = (uint16_t )dst.val | (_regs._eflags &0xffff0000u ); -if ( cr4 &CR4_VME ) +if ( cr4 &X86_CR4_VME ) {if ( dst.val &X86_EFLAGS_IF ) {
@@ -5009,7 +4994,7 @@ x86_emulate(generate_exception_if (!mode_ring0 (),EXC_GP ,0 );fail_if ((ops->read_cr ==NULL ) || (ops->write_cr ==NULL ));if ( (rc = ops->read_cr (0 , &dst.val, ctxt)) !=X86EMUL_OKAY || - (rc = ops->write_cr (0 , dst.val & ~CR0_TS , ctxt)) !=X86EMUL_OKAY ) + (rc = ops->write_cr (0 , dst.val & ~X86_CR0_TS , ctxt)) !=X86EMUL_OKAY ) goto done;break ;
@@ -5187,7 +5172,7 @@ x86_emulate(fail_if (ops->read_cr ==NULL );if ( (rc = ops->read_cr (4 , &cr4, ctxt)) ) goto done; -generate_exception_if (cr4 &CR4_TSD ,EXC_GP ,0 ); +generate_exception_if (cr4 &X86_CR4_TSD ,EXC_GP ,0 ); }fail_if (ops->read_msr ==NULL );if ( (rc = ops->read_msr (MSR_IA32_TSC , &val, ctxt)) !=0 )
@@ -5560,7 +5545,7 @@ x86_emulate(fail_if (!ops->read_cr);if ( (rc = ops->read_cr (4 , &cr4, ctxt)) !=X86EMUL_OKAY ) goto done; -generate_exception_if (!(cr4 &CR4_FSGSBASE ),EXC_UD ); +generate_exception_if (!(cr4 &X86_CR4_FSGSBASE ),EXC_UD ); seg = modrm_reg &1 ? x86_seg_gs : x86_seg_fs;fail_if (!ops->read_segment);if ( (rc = ops->read_segment (seg, &sreg, ctxt)) !=X86EMUL_OKAY )
Signature · 7 lines
2.11.0 _______________________________________________ Xen-devel mailing list [email protected] https://lists.xen.org/xen-devel
PATCH v3 9/11 fuzz/x86emul: update fuzzer
In reply to Wei LiuProvide the fuzzer with more ops, and more sophisticated input structure.
Based on a patch originally written by Andrew and George.
---
Cc: Ian Jackson <[email protected]>
Cc: Jan Beulich <[email protected]>
Cc: Andrew Cooper <[email protected]>
Cc: George Dunlap <[email protected]>
v3:
1. Address comments from Jan
v2:
1. Address comments from Jan
2. Provide architecturally correct behaviour for rep stub
--- .../x86-insn-emulator-fuzzer.c | 661 +++++++++++++++++++-- 1 file changed, 599 insertions(+), 62 deletions(-)
tools/fuzz/x86_instruction_emulator/x86-insn-emulator-fuzzer.c +599 -62
@@ -4,6 +4,7 @@#include <inttypes.h> #include <limits.h> #include <stdbool.h> +#include <stddef.h> #include <stdint.h> #include <stdio.h> #include <stdlib.h>
@@ -16,26 +17,78 @@#include "x86_emulate.h" -static unsigned char data[4096 ]; +#define MSR_INDEX_MAX 16 + +#define SEG_NUM x86_seg_none + +struct input_struct { +unsigned long cr[5 ]; +uint64_t msr[MSR_INDEX_MAX ]; +struct cpu_user_regs regs; +struct segment_register segments[SEG_NUM ]; +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; +static unsigned int data_num; + +/* +* Randomly return success or failure when processing data. If +* `exception` is false, this function turns _EXCEPTION to _OKAY. +*/ +int maybe_fail (const char *why,bool exception) +{ +int rc; + +if ( data_index >= data_num ) + rc =X86EMUL_EXCEPTION ; +else + { +/* Randomly returns value: +* 50% okay +* 25% unhandlable +* 25% exception +*/ +if ( input.data[data_index] >0xc0 ) + rc =X86EMUL_EXCEPTION ; +else if ( input.data[data_index] >0x80 ) + 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 ; +if ( data_index + bytes > data_num ) + rc =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 +101,112 @@ 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_read (const char *why,unsigned long *reps) +{ +int rc; +unsigned long bytes_read =0 ; + + rc =data_read (why, &bytes_read,sizeof (bytes_read)); + +if ( bytes_read <= *reps ) + *reps = bytes_read; + +switch ( rc ) + { +case X86EMUL_UNHANDLEABLE : +/* No work is done in this case */ + *reps =0 ; +break ; +case X86EMUL_EXCEPTION : +case X86EMUL_RETRY : +/* Halve the amount in this case */ + *reps /=2 ; + } + +return rc; +} + +static int _fuzz_rep_write (const char *why,unsigned long *reps) +{ +int rc =maybe_fail (why, true); + +switch ( rc ) + { +case X86EMUL_UNHANDLEABLE : +/* No work is done in this case */ + *reps =0 ; +break ; +case X86EMUL_EXCEPTION : +case X86EMUL_RETRY : +/* Halve the amount in this case */ + *reps /=2 ; + } + +return rc; +} + +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) +{ +return _fuzz_rep_read ("rep_ins" , reps); +} + +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) +{ +return _fuzz_rep_read ("rep_movs" , reps); +} + +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 _fuzz_rep_write ("rep_outs" , reps); +} + +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 _fuzz_rep_write ("rep_stos" , reps); } static int fuzz_write(
@@ -65,7 +216,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 +227,285 @@ 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_read_segment ( +enum x86_segment seg, +struct segment_register *reg, +struct x86_emulate_ctxt *ctxt) +{ +int rc; + +if ( seg >=SEG_NUM ) +return X86EMUL_UNHANDLEABLE ; + + rc =maybe_fail ("read_segment" , true); + +if ( rc ==X86EMUL_OKAY ) + *reg = input.segments[seg]; + +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_NUM ) +return X86EMUL_UNHANDLEABLE ; + + rc =maybe_fail ("write_segment" , true); + +if ( rc ==X86EMUL_OKAY ) + input.segments[seg] = *reg; + +return rc; +} + +static int fuzz_read_cr ( +unsigned int reg, +unsigned long *val, +struct x86_emulate_ctxt *ctxt) +{ +int rc; + +if ( reg >=ARRAY_SIZE (input.cr) ) +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 >=ARRAY_SIZE (input.cr) ) +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 +}; + +static const unsigned 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_IA32_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; + +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_IA32_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_##hstatic 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 (invlpg), .get_fpu = emul_test_get_fpu, + .cpuid = emul_test_cpuid, }; +#undef SET + +static void setup_fpu_exception_handler (void ) +{ +/* FIXME - just disable exceptions for now */ +unsigned long a; + + asmvolatile ("fnclex" ); + a =0x37f ;/* FCW_DEFAULT in Xen */ + asmvolatile ("fldcw %0" ::"m" (a)); + a =0x1f80 ;/* MXCSR_DEFAULT in Xen */ + asmvolatile ("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: %lx\n" , input.cr[0 ]); +printf (" cr3: %lx\n" , input.cr[3 ]); +printf (" cr4: %lx\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 +518,146 @@ 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, + OPTION_swint_emulation,/* Two bits */ + CANONICALIZE_rip = OPTION_swint_emulation +2 , + 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; + +/* See also sanitize_input, some hooks can't be disabled. */ +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) +{ +unsigned int swint_opt = (input.options >> OPTION_swint_emulation) &3 ; + +static const enum x86_swint_emulation 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, 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; + +/* Some hooks can't be disabled. */ + input.options &= ~((1 <<HOOK_read)|(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 &= ~X86_EFLAGS_VM ; + +/* EFLAGS.VM implies 16-bit mode */ +if ( regs->rflags &X86_EFLAGS_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 +668,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 +678,38 @@ 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); + data_num =0 ; -memcpy (®s, 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_num = 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 { +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
Re: [PATCH v3 09/11] fuzz/x86emul: update fuzzer
This patch will be squashed in.
From 4990d760f900223e32cb800e7374ee45d357081b Mon Sep 17 00:00:00 2001
From: Wei Liu <[email protected]>
Date: Wed, 1 Feb 2017 16:54:58 +0000
Subject: [PATCH] fixup! fuzz/x86emul: update fuzzer
--- .../x86-insn-emulator-fuzzer.c | 18 ++++-------------- 1 file changed, 4 insertions(+), 14 deletions(-)
tools/fuzz/x86_instruction_emulator/x86-insn-emulator-fuzzer.c +4 -14
@@ -294,17 +294,12 @@ static int fuzz_read_cr( unsigned long *val, struct x86_emulate_ctxt *ctxt) { - int rc; -if ( reg >=ARRAY_SIZE (input.cr) )return X86EMUL_UNHANDLEABLE ; - rc =maybe_fail ("read_cr" , true); + *val = input.cr[reg]; -if ( rc ==X86EMUL_OKAY ) - *val = input.cr[reg]; - -return rc; +return X86EMUL_OKAY ; } static int fuzz_write_cr(
@@ -312,17 +307,12 @@ static int fuzz_write_cr( unsigned long val, struct x86_emulate_ctxt *ctxt) { - int rc; -if ( reg >=ARRAY_SIZE (input.cr) )return X86EMUL_UNHANDLEABLE ; - rc =maybe_fail ("write_cr" , true); + input.cr[reg] = val; -if ( rc ==X86EMUL_OKAY ) - input.cr[reg] = val; - -return rc; +return X86EMUL_OKAY ; }enum {
Signature · 7 lines
2.11.0 _______________________________________________ Xen-devel mailing list [email protected] https://lists.xen.org/xen-devel
Re: [PATCH v3 09/11] fuzz/x86emul: update fuzzer
In reply to Wei LiuQuoted 18 lines
Quoted 1 line
Quoted 1 line · On 01.02.17 at 13:02, <[email protected]> wrote:
tools/fuzz/x86_instruction_emulator/x86-insn-emulator-fuzzer.c +12
-1@@ -16,26 +17,78 @@#include "x86_emulate.h" -static unsigned char data[4096 ]; +#define MSR_INDEX_MAX 16 + +#define SEG_NUM x86_seg_none + +struct input_struct { +unsigned long cr[5 ]; +uint64_t msr[MSR_INDEX_MAX ]; +struct cpu_user_regs regs; +struct segment_register segments[SEG_NUM ]; +unsigned long options; +unsigned char data[4096 ]; +} input;
tools/fuzz/x86_instruction_emulator/x86-insn-emulator-fuzzer.c line 40
Quoted 10 lines
tools/fuzz/x86_instruction_emulator/x86-insn-emulator-fuzzer.c +8
-1+#define DATA_OFFSET offsetof(struct input_struct, data)static unsigned int data_index; -static unsigned int data_max; +static unsigned int data_num; + +/* +* Randomly return success or failure when processing data. If +* `exception` is false, this function turns _EXCEPTION to _OKAY. +*/ +int maybe_fail (const char *why,bool exception)
static?
Quoted 11 lines
tools/fuzz/x86_instruction_emulator/x86-insn-emulator-fuzzer.c +11
-0+static int _fuzz_rep_read (const char *why,unsigned long *reps) +{ +int rc; +unsigned long bytes_read =0 ; + + rc =data_read (why, &bytes_read,sizeof (bytes_read)); + +if ( bytes_read <= *reps ) + *reps = bytes_read; + + switch ( rc )
tools/fuzz/x86_instruction_emulator/x86-insn-emulator-fuzzer.c static int fuzz_read( · line 143
Quoted 10 lines
tools/fuzz/x86_instruction_emulator/x86-insn-emulator-fuzzer.c +10
-0+ { +case X86EMUL_UNHANDLEABLE : +/* No work is done in this case */ + *reps =0 ; +break ; +case X86EMUL_EXCEPTION : +case X86EMUL_RETRY : +/* Halve the amount in this case */ + *reps /=2 ; + }
Even if not strictly needed at this point in time, adding a break statement here would be nice.
tools/fuzz/x86_instruction_emulator/x86-insn-emulator-fuzzer.c static int fuzz_cmpxchg( · line 245
Quoted 13 lines
tools/fuzz/x86_instruction_emulator/x86-insn-emulator-fuzzer.c +13
-0+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); +}
Can these two reasonably fail? I wonder if we shouldn't perhaps make the hooks return void.
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 272
Quoted 10 lines
tools/fuzz/x86_instruction_emulator/x86-insn-emulator-fuzzer.c +10
-0+if ( seg >=SEG_NUM ) +return X86EMUL_UNHANDLEABLE ; + + rc =maybe_fail ("read_segment" , true); + +if ( rc ==X86EMUL_OKAY ) + *reg = input.segments[seg]; + +return rc; +}
Just like with ->read_cr(), this must not vary in returned state between multiple invocations.
tools/fuzz/x86_instruction_emulator/x86-insn-emulator-fuzzer.c static int fuzz_cmpxchg( · line 361
Quoted 12 lines
tools/fuzz/x86_instruction_emulator/x86-insn-emulator-fuzzer.c +12
-0+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_IA32_TSC : +return data_read ("read_msr" , val,sizeof (*val));
Strictly speaking the above applies to TSC_AUX too. And TSC should return monotonically increasing values. I don't think though that producing random output here matters right now. A comment may be worthwhile.
tools/fuzz/x86_instruction_emulator/x86-insn-emulator-fuzzer.c static int fuzz_cmpxchg( · line 394
Quoted 9 lines
tools/fuzz/x86_instruction_emulator/x86-insn-emulator-fuzzer.c +9
-0+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;
This, otoh, again needs to strictly follow the revised read_cr() model.
tools/fuzz/x86_instruction_emulator/x86-insn-emulator-fuzzer.c static struct x86_emulate_ops fuzz_emulops = { · line 598
Quoted 5 lines
tools/fuzz/x86_instruction_emulator/x86-insn-emulator-fuzzer.c +5
-0+static void set_swint_support (struct x86_emulate_ctxt *ctxt) +{ +unsigned int swint_opt = (input.options >> OPTION_swint_emulation) &3 ; + +static const enum x86_swint_emulation map[4 ] = {
As (I think) said before - please don't put blank lines between declarations; they're supposed to separate declarations from statements.
Quoted 13 lines
tools/fuzz/x86_instruction_emulator/x86-insn-emulator-fuzzer.c +13
-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 +* 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
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 +10
-0+* some point: +* +* - EFER.LMA => !EFLAGS.NT +* - In VM86 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) {
static? And { on its own line.
Quoted 2 lines
do {+
setup_fpu_exception_handler();
I'm sorry for having mislead you regarding the comment which was
here: As said elsewhere, I was - based on the function's name - assuming this to be a one time action, while in fact this is being done before every emulated insn. Once this becomes a one time thing, it indeed needs moving out of the loop, so leaving a comment here is warranted.
Jan
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
Re: [PATCH v3 09/11] fuzz/x86emul: update fuzzer
Quoted 22 lines · Jan Beulich
Quoted 22 lines
Quoted 1 line
Quoted 1 line · On 01.02.17 at 13:02, <[email protected]> wrote:
tools/fuzz/x86_instruction_emulator/x86-insn-emulator-fuzzer.c +14
-2@@ -16,26 +17,78 @@#include "x86_emulate.h" -static unsigned char data[4096 ]; +#define MSR_INDEX_MAX 16 + +#define SEG_NUM x86_seg_none + +struct input_struct { +unsigned long cr[5 ]; +uint64_t msr[MSR_INDEX_MAX ]; +struct cpu_user_regs regs; +struct segment_register segments[SEG_NUM ]; +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; +static unsigned int data_num;
Quoted 8 lines
tools/fuzz/x86_instruction_emulator/x86-insn-emulator-fuzzer.c line 40
Quoted 6 lines
tools/fuzz/x86_instruction_emulator/x86-insn-emulator-fuzzer.c +6
-0+ +/* +* Randomly return success or failure when processing data. If +* `exception` is false, this function turns _EXCEPTION to _OKAY. +*/ +int maybe_fail (const char *why,bool exception)static?
Done.
Quoted 17 lines
Quoted 17 lines
tools/fuzz/x86_instruction_emulator/x86-insn-emulator-fuzzer.c +16
-0+static int _fuzz_rep_read (const char *why,unsigned long *reps) +{ +int rc; +unsigned long bytes_read =0 ; + + rc =data_read (why, &bytes_read,sizeof (bytes_read)); + +if ( bytes_read <= *reps ) + *reps = bytes_read; + +switch ( rc ) + { +case X86EMUL_UNHANDLEABLE : +/* No work is done in this case */ + *reps =0 ; +break ;
Quoted 9 lines
tools/fuzz/x86_instruction_emulator/x86-insn-emulator-fuzzer.c static int fuzz_read( · line 143
Quoted 5 lines
tools/fuzz/x86_instruction_emulator/x86-insn-emulator-fuzzer.c +5
-0+case X86EMUL_EXCEPTION : +case X86EMUL_RETRY : +/* Halve the amount in this case */ + *reps /=2 ; + }Even if not strictly needed at this point in time, adding a break statement here would be nice.
Done.
Quoted 8 lines
Quoted 8 lines
tools/fuzz/x86_instruction_emulator/x86-insn-emulator-fuzzer.c +8
-0+static int fuzz_invlpg ( +enum x86_segment seg, +unsigned long offset, +struct x86_emulate_ctxt *ctxt) +{ +return maybe_fail ("invlpg" , false); +} +
Quoted 9 lines
tools/fuzz/x86_instruction_emulator/x86-insn-emulator-fuzzer.c static int fuzz_cmpxchg( · line 245
Quoted 5 lines
tools/fuzz/x86_instruction_emulator/x86-insn-emulator-fuzzer.c +5
-0+static int fuzz_wbinvd ( +struct x86_emulate_ctxt *ctxt) +{ +return maybe_fail ("wbinvd" , true); +}Can these two reasonably fail? I wonder if we shouldn't perhaps make the hooks return void.
Both can generate exceptions in some cases according to manual, so I think it makes sense to leave them as-is, provided they fit in the model you want to use (like in_realmode).
Of course if you end up changing the hooks to return void, I'm fine with just removing these two hooks from fuzzer altogether.
Quoted 12 lines
Quoted 12 lines
tools/fuzz/x86_instruction_emulator/x86-insn-emulator-fuzzer.c +12
-0+static int fuzz_read_segment ( +enum x86_segment seg, +struct segment_register *reg, +struct x86_emulate_ctxt *ctxt) +{ +int rc; + +if ( seg >=SEG_NUM ) +return X86EMUL_UNHANDLEABLE ; + + rc =maybe_fail ("read_segment" , true); +
Quoted 9 lines
tools/fuzz/x86_instruction_emulator/x86-insn-emulator-fuzzer.c static int fuzz_cmpxchg( · line 272
Quoted 5 lines
tools/fuzz/x86_instruction_emulator/x86-insn-emulator-fuzzer.c +5
-0+if ( rc ==X86EMUL_OKAY ) + *reg = input.segments[seg]; + +return rc; +}Just like with ->read_cr(), this must not vary in returned state between multiple invocations.
Fixed for both read_segment and write_segment.
Quoted 9 lines
Quoted 9 lines
tools/fuzz/x86_instruction_emulator/x86-insn-emulator-fuzzer.c +9
-0+static int _fuzz_read_msr ( +unsigned int reg, +uint64_t *val, +struct x86_emulate_ctxt *ctxt) +{ +unsigned int idx; + +switch ( reg ) + {
Quoted 9 lines
tools/fuzz/x86_instruction_emulator/x86-insn-emulator-fuzzer.c static int fuzz_cmpxchg( · line 361
Quoted 3 lines
tools/fuzz/x86_instruction_emulator/x86-insn-emulator-fuzzer.c +3
-0+case MSR_TSC_AUX : +case MSR_IA32_TSC : +return data_read ("read_msr" , val,sizeof (*val));Strictly speaking the above applies to
TSC_AUXtoo. And TSC should return monotonically increasing values. I don't think though that producing random output here matters right now. A comment may be worthwhile.
Right, I will add the following:
/*
* TSC should return monotonically increasing values, but
* returning random values is fine in fuzzer. */
Quoted 11 lines
tools/fuzz/x86_instruction_emulator/x86-insn-emulator-fuzzer.c static int fuzz_cmpxchg( · line 394
Quoted 9 lines
tools/fuzz/x86_instruction_emulator/x86-insn-emulator-fuzzer.c +9
-0+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;This, otoh, again needs to strictly follow the revised
read_cr()model.
Fixed. And then merge _fuzz_read_msr and fuzz_read_msr.
Quoted 11 lines
tools/fuzz/x86_instruction_emulator/x86-insn-emulator-fuzzer.c static struct x86_emulate_ops fuzz_emulops = { · line 598
Quoted 6 lines
tools/fuzz/x86_instruction_emulator/x86-insn-emulator-fuzzer.c +5
-0+static void set_swint_support (struct x86_emulate_ctxt *ctxt) +{ +unsigned int swint_opt = (input.options >> OPTION_swint_emulation) &3 ; + +static const enum x86_swint_emulation map[4 ] = {As (I think) said before - please don't put blank lines between declarations; they're supposed to separate declarations from statements.
Fixed.
Quoted 17 lines
Quoted 17 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.
+ *
+ * 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, force segment...
Quoted 9 lines
Quoted 6 lines
+ * - ...access rights to 0xf3 + * - ...limits to 0xffff + * - ...bases to below 1Mb, 16-byte aligned + * - ...selectors to (base >> 4)+ */
+voidsanitize_input(struct x86_emulate_ctxt *ctxt){static? And { on its own line.
Done.
Quoted 10 lines
Quoted 2 lines
do {+
setup_fpu_exception_handler();I'm sorry for having mislead you regarding the comment which was
here: As said elsewhere, I was - based on the function's name - assuming this to be a one time action, while in fact this is being done before every emulated insn. Once this becomes a one time thing, it indeed needs moving out of the loop, so leaving a comment here is warranted.
No problem, I add back the original comment.
Wei.
Quoted 1 line
Jan
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
Re: [PATCH v3 09/11] fuzz/x86emul: update fuzzer
Quoted 24 lines
Quoted 1 line
Quoted 1 line · On 02.02.17 at 17:50, <[email protected]> wrote:
Quoted 13 lines · Jan Beulich
Quoted 13 lines
Quoted 1 line
Quoted 1 line · On 01.02.17 at 13:02, <[email protected]> wrote:
tools/fuzz/x86_instruction_emulator/x86-insn-emulator-fuzzer.c +12
-0+static int fuzz_read_segment ( +enum x86_segment seg, +struct segment_register *reg, +struct x86_emulate_ctxt *ctxt) +{ +int rc; + +if ( seg >=SEG_NUM ) +return X86EMUL_UNHANDLEABLE ; + + rc =maybe_fail ("read_segment" , true); +Quoted 8 lines
tools/fuzz/x86_instruction_emulator/x86-insn-emulator-fuzzer.c static int fuzz_cmpxchg( · line 272
Quoted 5 lines
tools/fuzz/x86_instruction_emulator/x86-insn-emulator-fuzzer.c +5
-0+if ( rc ==X86EMUL_OKAY ) + *reg = input.segments[seg]; + +return rc; +}Just like with ->read_cr(), this must not vary in returned state between multiple invocations.
Quoted 1 line
Fixed for both
read_segmentandwrite_segment.
Why for write_segment? That one may fail at any time (and wouldn't normally be invoked more than once for a given segment anyway).
Quoted 19 lines
Quoted 9 lines
Quoted 9 lines
tools/fuzz/x86_instruction_emulator/x86-insn-emulator-fuzzer.c +9
-0+static int _fuzz_read_msr ( +unsigned int reg, +uint64_t *val, +struct x86_emulate_ctxt *ctxt) +{ +unsigned int idx; + +switch ( reg ) + {Quoted 9 lines
tools/fuzz/x86_instruction_emulator/x86-insn-emulator-fuzzer.c static int fuzz_cmpxchg( · line 361
Quoted 3 lines
tools/fuzz/x86_instruction_emulator/x86-insn-emulator-fuzzer.c +3
-0+case MSR_TSC_AUX : +case MSR_IA32_TSC : +return data_read ("read_msr" , val,sizeof (*val));Strictly speaking the above applies to
TSC_AUXtoo. And TSC should return monotonically increasing values. I don't think though that producing random output here matters right now. A comment may be worthwhile.
Quoted 6 lines
Right, I will add the following:
/*
* TSC should return monotonically increasing values, but
* returning random values is fine in fuzzer. */
What about TSC_AUX then?
Jan
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
Re: [PATCH v3 09/11] fuzz/x86emul: update fuzzer
Quoted 24 lines · Jan Beulich
Quoted 24 lines
Quoted 1 line
Quoted 1 line · On 02.02.17 at 17:50, <[email protected]> wrote:
Quoted 21 lines · Jan Beulich
Quoted 8 lines
Quoted 1 line
Quoted 1 line · On 01.02.17 at 13:02, <[email protected]> wrote:
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 272
Quoted 10 lines
tools/fuzz/x86_instruction_emulator/x86-insn-emulator-fuzzer.c +10
-0+if ( seg >=SEG_NUM ) +return X86EMUL_UNHANDLEABLE ; + + rc =maybe_fail ("read_segment" , true); + +if ( rc ==X86EMUL_OKAY ) + *reg = input.segments[seg]; + +return rc; +}Just like with ->read_cr(), this must not vary in returned state between multiple invocations.
Quoted 4 lines
Quoted 1 line
Fixed for both
read_segmentandwrite_segment.Why for
write_segment? That one may fail at any time (and wouldn't normally be invoked more than once for a given segment anyway).
OK, that makes sense. I will revert it back.
Quoted 20 lines
Quoted 20 lines
Quoted 19 lines
tools/fuzz/x86_instruction_emulator/x86-insn-emulator-fuzzer.c static int fuzz_cmpxchg( · line 361
Quoted 13 lines
tools/fuzz/x86_instruction_emulator/x86-insn-emulator-fuzzer.c +12
-0+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_IA32_TSC : +return data_read ("read_msr" , val,sizeof (*val));Strictly speaking the above applies to
TSC_AUXtoo. And TSC should return monotonically increasing values. I don't think though that producing random output here matters right now. A comment may be worthwhile.
Quoted 9 lines
Quoted 6 lines
Right, I will add the following:
/*
* TSC should return monotonically increasing values, but
* returning random values is fine in fuzzer. */What about
TSC_AUXthen?
What model would you like it to follow? I suppose returning random value is also fine? I.e. I should just add TSC_AUX to the comment as well.
Wei.
Quoted 2 lines
Jan
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
Re: [PATCH v3 09/11] fuzz/x86emul: update fuzzer
Quoted 31 lines
Quoted 1 line
Quoted 1 line · On 02.02.17 at 18:12, <[email protected]> wrote:
Quoted 29 lines · Jan Beulich
Quoted 28 lines
Quoted 1 line
Quoted 1 line · On 02.02.17 at 17:50, <[email protected]> wrote:
Quoted 10 lines · Jan Beulich
Quoted 10 lines
Quoted 1 line
Quoted 1 line · On 01.02.17 at 13:02, <[email protected]> wrote:
tools/fuzz/x86_instruction_emulator/x86-insn-emulator-fuzzer.c +9
-0+static int _fuzz_read_msr ( +unsigned int reg, +uint64_t *val, +struct x86_emulate_ctxt *ctxt) +{ +unsigned int idx; + +switch ( reg ) + {Quoted 9 lines
tools/fuzz/x86_instruction_emulator/x86-insn-emulator-fuzzer.c static int fuzz_cmpxchg( · line 361
Quoted 3 lines
tools/fuzz/x86_instruction_emulator/x86-insn-emulator-fuzzer.c +3
-0+case MSR_TSC_AUX : +case MSR_IA32_TSC : +return data_read ("read_msr" , val,sizeof (*val));Strictly speaking the above applies to
TSC_AUXtoo. And TSC should return monotonically increasing values. I don't think though that producing random output here matters right now. A comment may be worthwhile.Right, I will add the following:
/*
* TSC should return monotonically increasing values, but
* returning random values is fine in fuzzer. */
Quoted 5 lines
Quoted 2 lines
What about
TSC_AUXthen?What model would you like it to follow? I suppose returning random value is also fine? I.e. I should just add
TSC_AUXto the comment as well.
That's one option. Even better would be to consistently return the same (possibly random) value. But the value itself isn't being looked at by the emulator, and iirc the MSR also isn't ever being read multiple times, so it really doesn't matter as long as the caller(s) of x86_emulate() do(es)n't care.
Jan
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
Re: [PATCH v3 09/11] fuzz/x86emul: update fuzzer
Quoted 34 lines · Jan Beulich
Quoted 34 lines
Quoted 1 line
Quoted 1 line · On 02.02.17 at 18:12, <[email protected]> wrote:
Quoted 31 lines · Jan Beulich
Quoted 22 lines
Quoted 1 line
Quoted 1 line · On 02.02.17 at 17:50, <[email protected]> wrote:
Quoted 10 lines · Jan Beulich
Quoted 10 lines
Quoted 1 line
Quoted 1 line · On 01.02.17 at 13:02, <[email protected]> wrote:
tools/fuzz/x86_instruction_emulator/x86-insn-emulator-fuzzer.c +9
-0+static int _fuzz_read_msr ( +unsigned int reg, +uint64_t *val, +struct x86_emulate_ctxt *ctxt) +{ +unsigned int idx; + +switch ( reg ) + {Quoted 9 lines
tools/fuzz/x86_instruction_emulator/x86-insn-emulator-fuzzer.c static int fuzz_cmpxchg( · line 361
Quoted 3 lines
tools/fuzz/x86_instruction_emulator/x86-insn-emulator-fuzzer.c +3
-0+case MSR_TSC_AUX : +case MSR_IA32_TSC : +return data_read ("read_msr" , val,sizeof (*val));Strictly speaking the above applies to
TSC_AUXtoo. And TSC should return monotonically increasing values. I don't think though that producing random output here matters right now. A comment may be worthwhile.Quoted 6 lines
Right, I will add the following:
/*
* TSC should return monotonically increasing values, but
* returning random values is fine in fuzzer. */What about
TSC_AUXthen?
Quoted 8 lines
Quoted 2 lines
What model would you like it to follow? I suppose returning random value is also fine? I.e. I should just add
TSC_AUXto the comment as well.That's one option. Even better would be to consistently return the same (possibly random) value. But the value itself isn't being looked at by the emulator, and iirc the MSR also isn't ever being read multiple times, so it really doesn't matter as long as the caller(s) of
x86_emulate()do(es)n't care.
Returning consistent doable but that would involve rewriting a significant portion of the fuzzer. Let's leave it for another day.
I will modify the comment as follows:
/*
* TSC should return monotonically increasing values, TSC_AUX should
* preferably return consistent value, but returning random values
* is fine in fuzzer.
*/
Quoted 3 lines
Jan
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
PATCH v3 10/11 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
@@ -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 4096static uint8_t input[INPUT_SIZE ];
@@ -21,6 +23,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
@@ -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
PATCH v3 11/11 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
@@ -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