Skip to contentSkip to conversation

Fuzzing targets for oss-fuzz

RFC PATCH Superseded Wei Liu 2 participants 17 messages Dec 9, 2016

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Wei Liu

author
#0 lore raw

v2 cover Fuzzing targets for oss-fuzz

Hi all

This series adds two fuzzing targets to run in Google's oss-fuzz infrastructure.

There will be some other patches on the oss-fuzz side. Their recommendation is to have all the fuzzing targets committed in our tree so that they can be kept up to date.

The fuzzing targets aren't very sophiscated at the moment. The purpose of this series is to gather feedback at this early stage.

We can always improve the fuzzing code in the future.

Wei.

v2:
1. Share more code between existing test code and fuzzing target.
2. More details in README.

Wei Liu (7):
tools/fuzz: introduce libelf target x86emul/test: factor out emul_test_make_stack_executable x86emul/test: factor out emul_test_{read_cr,cpuid} x86emul/test: remove unused macros
tools/fuzz: introduce x86 instruction emulator target
tools: hook up fuzz directory
tools/fuzz: add README

 .gitignore                                         |   1 +
 tools/Makefile                                     |   1 +
 tools/fuzz/Makefile                                |  11 +
 tools/fuzz/README                                  |  39 +++
 tools/fuzz/libelf/Makefile                         |  31 +++
 tools/fuzz/libelf/libelf-fuzzer.c                  |  32 +++
 tools/fuzz/x86_instruction_emulator/Makefile       |  31 +++
 .../x86-insn-emulator-fuzzer.c                     | 262 +++++++++++++++++++++
 tools/tests/x86_emulator/test_x86_emulator.c       |  93 +-------
 tools/tests/x86_emulator/x86_emulate.c             |  61 +++++
 tools/tests/x86_emulator/x86_emulate.h             |  22 ++
 xen/common/libelf/libelf-private.h                 |   2 +
 12 files changed, 502 insertions(+), 84 deletions(-)
 create mode 100644 tools/fuzz/Makefile
 create mode 100644 tools/fuzz/README
 create mode 100644 tools/fuzz/libelf/Makefile
 create mode 100644 tools/fuzz/libelf/libelf-fuzzer.c
 create mode 100644 tools/fuzz/x86_instruction_emulator/Makefile
 create mode 100644 tools/fuzz/x86_instruction_emulator/x86-insn-emulator-fuzzer.c
Signature · 7 lines
2.1.4


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

RFC PATCH v2 1/7 tools/fuzz: introduce libelf target

Source code and Makefile to fuzz libelf in Google's oss-fuzz infrastructure.

Introduce FUZZ_NO_LIBXC in libelf-private.h. That macro will be set when compiling libelf fuzzer target because libxc is not required in libelf fuzzing.

---
Cc: Andrew Cooper <[email protected]>
Cc: George Dunlap <[email protected]>
Cc: Ian Jackson <[email protected]>
Cc: Jan Beulich <[email protected]>
Cc: Konrad Rzeszutek Wilk <[email protected]>
Cc: Stefano Stabellini <[email protected]>
Cc: Tim Deegan <[email protected]>
Cc: Wei Liu <[email protected]>

---
 tools/fuzz/libelf/Makefile         | 31 +++++++++++++++++++++++++++++++
 tools/fuzz/libelf/libelf-fuzzer.c  | 32 ++++++++++++++++++++++++++++++++
 xen/common/libelf/libelf-private.h |  2 ++
 3 files changed, 65 insertions(+)
 create mode 100644 tools/fuzz/libelf/Makefile
 create mode 100644 tools/fuzz/libelf/libelf-fuzzer.c
tools/fuzz/libelf/Makefile new file +31 -0
new file mode 100644
index 0000000..0e9d40a
@@ -0,0 +1,31 @@
+XEN_ROOT = $(CURDIR)/../../..
+include $(XEN_ROOT)/tools/Rules.mk
+
+# libelf fuzz target
+vpath %.c ../../../xen/common/libelf
+CFLAGS += -I../../../xen/common/libelf
+ELF_SRCS-y += libelf-tools.c libelf-loader.c libelf-dominfo.c
+ELF_LIB_OBJS := $(patsubst %.c,%.o,$(ELF_SRCS-y))
+
+$(patsubst %.c,%.o,$(ELF_SRCS-y)): CFLAGS += -Wno-pointer-sign
+
+$(ELF_LIB_OBJS): CFLAGS += -DFUZZ_NO_LIBXC $(CFLAGS_xeninclude)
+
+libelf-fuzzer.o: CFLAGS += $(CFLAGS_xeninclude)
+
+libelf.a: $(ELF_LIB_OBJS)
+	$(AR) rc $@ $^
+
+.PHONY: libelf-fuzzer-all
+libelf-fuzzer-all: libelf.a libelf-fuzzer.o
+
+# Common targets
+.PHONY: all
+all: libelf-fuzzer-all
+
+.PHONY: distclean
+distclean: clean
+
+.PHONY: clean
+clean:
+	rm -f *.o *.a
tools/fuzz/libelf/libelf-fuzzer.c new file +32 -0
new file mode 100644
index 0000000..71561d3
@@ -0,0 +1,32 @@
+#include <inttypes.h>
+#include <stddef.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include <xen/libelf/libelf.h>
+
+int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
+{
+    struct elf_binary elf_buf, *elf;
+    struct elf_dom_parms parms;
+
+    elf = &elf_buf;
+
+    memset(elf, 0, sizeof(*elf));
+    elf_init(elf, (const char *)data, size);
+    elf_parse_binary(elf);
+    elf_xen_parse(elf, &parms);
+
+    return 0;
+}
+
+
+/*
+ * Local variables:
+ * mode: C
+ * c-file-style: "BSD"
+ * c-basic-offset: 4
+ * tab-width: 4
+ * indent-tabs-mode: nil
+ * End:
+ */
xen/common/libelf/libelf-private.h +2 -0
index 388c3da..47db679 100644
@@ -72,8 +72,10 @@
 #include <xen/elfnote.h>
 #include <xen/libelf/libelf.h>
 
+#ifndef FUZZ_NO_LIBXC
 #include "xenctrl.h"
 #include "xc_private.h"
+#endif
 
 #define elf_msg(elf, fmt, args ... )                    \
     elf_call_log_callback(elf, 0, fmt , ## args );
Signature · 7 lines
2.1.4


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

RFC PATCH v2 2/7 x86emul/test: factor out emul_test_make_stack_executable

In reply to Wei Liu

It will be used by emulator fuzzing target.

---
Cc: Jan Beulich <[email protected]>
Cc: Andrew Cooper <[email protected]>
Cc: Ian Jackson <[email protected]>
Cc: Wei Liu <[email protected]>

---
 tools/tests/x86_emulator/test_x86_emulator.c | 12 ++----------
 tools/tests/x86_emulator/x86_emulate.c       | 22 ++++++++++++++++++++++
 tools/tests/x86_emulator/x86_emulate.h       |  3 +++
 3 files changed, 27 insertions(+), 10 deletions(-)
tools/tests/x86_emulator/test_x86_emulator.c +2 -10
index eed8a0d..0d80bff 100644
@@ -23,8 +23,6 @@ static const struct {
 #endif
 };
 
-#define MMAP_SZ 16384
-
 /* EFLAGS bit definitions. */
 #define EFLG_OF (1<<11)
 #define EFLG_DF (1<<10)
@@ -234,7 +232,6 @@ int main(int argc, char **argv)
     struct cpu_user_regs regs;
     char *instr;
     unsigned int *res, i, j;
-    unsigned long sp;
     bool stack_exec;
     int rc;
 #ifndef __x86_64__
@@ -258,13 +255,8 @@ int main(int argc, char **argv)
     }
     instr = (char *)res + 0x100;
 
-#ifdef __x86_64__
-    asm ("movq %%rsp, %0" : "=g" (sp));
-#else
-    asm ("movl %%esp, %0" : "=g" (sp));
-#endif
-    stack_exec = mprotect((void *)(sp & -0x1000L) - (MMAP_SZ - 0x1000),
-                          MMAP_SZ, PROT_READ|PROT_WRITE|PROT_EXEC) == 0;
+    stack_exec = emul_test_make_stack_executable();
+
     if ( !stack_exec )
         printf("Warning: Stack could not be made executable (%d).\n", errno);
 
tools/tests/x86_emulator/x86_emulate.c +22 -0
index 66c2464..78e6380 100644
@@ -1,5 +1,7 @@
 #include "x86_emulate.h"
 
+#include <sys/mman.h>
+
 #define EFER_SCE       (1 << 0)
 #define EFER_LMA       (1 << 10)
 
@@ -18,4 +20,24 @@
 #define get_stub(stb) ((void *)((stb).addr = (uintptr_t)(stb).buf))
 #define put_stub(stb)
 
+bool emul_test_make_stack_executable(void)
+{
+    unsigned long sp;
+    bool stack_exec;
+
+    /*
+     * Mark the entire stack executable so that the stub executions
+     * don't fault
+     */
+#ifdef __x86_64__
+    asm ("movq %%rsp, %0" : "=g" (sp));
+#else
+    asm ("movl %%esp, %0" : "=g" (sp));
+#endif
+
+    stack_exec = mprotect((void *)(sp & -0x1000L) - (MMAP_SZ - 0x1000),
+                          MMAP_SZ, PROT_READ|PROT_WRITE|PROT_EXEC) == 0;
+    return stack_exec;
+}
+
 #include "x86_emulate/x86_emulate.c"
tools/tests/x86_emulator/x86_emulate.h +3 -0
index 1981326..a9b874c 100644
@@ -33,4 +33,7 @@
 
 #define is_canonical_address(x) (((int64_t)(x) >> 47) == ((int64_t)(x) >> 63))
 
+#define MMAP_SZ 16384
+bool emul_test_make_stack_executable(void);
+
 #include "x86_emulate/x86_emulate.h"
Signature · 7 lines
2.1.4


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

Re: [PATCH RFC v2 2/7] x86emul/test: factor out emul_test_make_stack_executable

tools/tests/x86_emulator/x86_emulate.c line 26

Quoted 9 lines
Quoted 1 line
Quoted 1 line · On 09.12.16 at 13:23, <[email protected]> wrote:
tools/tests/x86_emulator/x86_emulate.c +4 -0
@@ -18,4 +20,24 @@
 #define get_stub(stb) ((void *)((stb).addr = (uintptr_t)(stb).buf))
 #define put_stub(stb)
 
+bool emul_test_make_stack_executable(void)
+{
+    unsigned long sp;
+    bool stack_exec;

I'd prefer for this local variable to be removed as unnecessary, but in the end the patch can have my ack either way.

Jan

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

author
#4 lore raw

RFC PATCH v2 3/7 x86emul/test: factor out emul_test_{read_cr, cpuid}

In reply to Wei Liu

---
Cc: Jan Beulich <[email protected]>
Cc: Andrew Cooper <[email protected]>
Cc: Ian Jackson <[email protected]>
Cc: Wei Liu <[email protected]>

---
 tools/tests/x86_emulator/test_x86_emulator.c | 59 +++++-----------------------
 tools/tests/x86_emulator/x86_emulate.c       | 39 ++++++++++++++++++
 tools/tests/x86_emulator/x86_emulate.h       | 19 +++++++++
 3 files changed, 68 insertions(+), 49 deletions(-)
tools/tests/x86_emulator/test_x86_emulator.c +10 -49
index 0d80bff..6099401 100644
@@ -92,51 +92,33 @@ static int cmpxchg(
     return X86EMUL_OKAY;
 }
 
-static int cpuid(
-    unsigned int *eax,
-    unsigned int *ebx,
-    unsigned int *ecx,
-    unsigned int *edx,
-    struct x86_emulate_ctxt *ctxt)
-{
-    unsigned int leaf = *eax;
-
-    asm ("cpuid" : "+a" (*eax), "+c" (*ecx), "=d" (*edx), "=b" (*ebx));
-
-    /* The emulator doesn't itself use MOVBE, so we can always run the test. */
-    if ( leaf == 1 )
-        *ecx |= 1U << 22;
-
-    return X86EMUL_OKAY;
-}
-
 #define cache_line_size() ({ \
     unsigned int eax = 1, ebx, ecx = 0, edx; \
-    cpuid(&eax, &ebx, &ecx, &edx, NULL); \
+    emul_test_cpuid(&eax, &ebx, &ecx, &edx, NULL); \
     edx & (1U << 19) ? (ebx >> 5) & 0x7f8 : 0; \
 })
 
 #define cpu_has_mmx ({ \
     unsigned int eax = 1, ecx = 0, edx; \
-    cpuid(&eax, &ecx, &ecx, &edx, NULL); \
+    emul_test_cpuid(&eax, &ecx, &ecx, &edx, NULL); \
     (edx & (1U << 23)) != 0; \
 })
 
 #define cpu_has_sse ({ \
     unsigned int eax = 1, ecx = 0, edx; \
-    cpuid(&eax, &ecx, &ecx, &edx, NULL); \
+    emul_test_cpuid(&eax, &ecx, &ecx, &edx, NULL); \
     (edx & (1U << 25)) != 0; \
 })
 
 #define cpu_has_sse2 ({ \
     unsigned int eax = 1, ecx = 0, edx; \
-    cpuid(&eax, &ecx, &ecx, &edx, NULL); \
+    emul_test_cpuid(&eax, &ecx, &ecx, &edx, NULL); \
     (edx & (1U << 26)) != 0; \
 })
 
 #define cpu_has_xsave ({ \
     unsigned int eax = 1, ecx = 0; \
-    cpuid(&eax, &eax, &ecx, &eax, NULL); \
+    emul_test_cpuid(&eax, &eax, &ecx, &eax, NULL); \
     /* Intentionally checking OSXSAVE here. */ \
     (ecx & (1U << 27)) != 0; \
 })
@@ -152,7 +134,7 @@ static inline uint64_t xgetbv(uint32_t xcr)
 
 #define cpu_has_avx ({ \
     unsigned int eax = 1, ecx = 0; \
-    cpuid(&eax, &eax, &ecx, &eax, NULL); \
+    emul_test_cpuid(&eax, &eax, &ecx, &eax, NULL); \
     if ( !(ecx & (1U << 27)) || ((xgetbv(0) & 6) != 6) ) \
         ecx = 0; \
     (ecx & (1U << 28)) != 0; \
@@ -160,37 +142,16 @@ static inline uint64_t xgetbv(uint32_t xcr)
 
 #define cpu_has_avx2 ({ \
     unsigned int eax = 1, ebx, ecx = 0; \
-    cpuid(&eax, &ebx, &ecx, &eax, NULL); \
+    emul_test_cpuid(&eax, &ebx, &ecx, &eax, NULL); \
     if ( !(ecx & (1U << 27)) || ((xgetbv(0) & 6) != 6) ) \
         ebx = 0; \
     else { \
         eax = 7, ecx = 0; \
-        cpuid(&eax, &ebx, &ecx, &eax, NULL); \
+        emul_test_cpuid(&eax, &ebx, &ecx, &eax, NULL); \
     } \
     (ebx & (1U << 5)) != 0; \
 })
 
-static int read_cr(
-    unsigned int reg,
-    unsigned long *val,
-    struct x86_emulate_ctxt *ctxt)
-{
-    /* Fake just enough state for the emulator's _get_fpu() to be happy. */
-    switch ( reg )
-    {
-    case 0:
-        *val = 0x00000001; /* PE */
-        return X86EMUL_OKAY;
-
-    case 4:
-        /* OSFXSR, OSXMMEXCPT, and maybe OSXSAVE */
-        *val = 0x00000600 | (cpu_has_xsave ? 0x00040000 : 0);
-        return X86EMUL_OKAY;
-    }
-
-    return X86EMUL_UNHANDLEABLE;
-}
-
 int get_fpu(
     void (*exception_callback)(void *, struct cpu_user_regs *),
     void *exception_callback_arg,
@@ -221,8 +182,8 @@ static struct x86_emulate_ops emulops = {
     .insn_fetch = fetch,
     .write      = write,
     .cmpxchg    = cmpxchg,
-    .cpuid      = cpuid,
-    .read_cr    = read_cr,
+    .cpuid      = emul_test_cpuid,
+    .read_cr    = emul_test_read_cr,
     .get_fpu    = get_fpu,
 };
 
tools/tests/x86_emulator/x86_emulate.c +39 -0
index 78e6380..b4bd61c 100644
@@ -40,4 +40,43 @@ bool emul_test_make_stack_executable(void)
     return stack_exec;
 }
 
+int emul_test_cpuid(
+    unsigned int *eax,
+    unsigned int *ebx,
+    unsigned int *ecx,
+    unsigned int *edx,
+    struct x86_emulate_ctxt *ctxt)
+{
+    unsigned int leaf = *eax;
+
+    asm ("cpuid" : "+a" (*eax), "+c" (*ecx), "=d" (*edx), "=b" (*ebx));
+
+    /* The emulator doesn't itself use MOVBE, so we can always run the test. */
+    if ( leaf == 1 )
+        *ecx |= 1U << 22;
+
+    return X86EMUL_OKAY;
+}
+
+int emul_test_read_cr(
+    unsigned int reg,
+    unsigned long *val,
+    struct x86_emulate_ctxt *ctxt)
+{
+    /* Fake just enough state for the emulator's _get_fpu() to be happy. */
+    switch ( reg )
+    {
+    case 0:
+        *val = 0x00000001; /* PE */
+        return X86EMUL_OKAY;
+
+    case 4:
+        /* OSFXSR, OSXMMEXCPT, and maybe OSXSAVE */
+        *val = 0x00000600 | (cpu_has_xsave ? 0x00040000 : 0);
+        return X86EMUL_OKAY;
+    }
+
+    return X86EMUL_UNHANDLEABLE;
+}
+
 #include "x86_emulate/x86_emulate.c"
tools/tests/x86_emulator/x86_emulate.h +19 -0
index a9b874c..b213f2d 100644
@@ -37,3 +37,22 @@
 bool emul_test_make_stack_executable(void);
 
 #include "x86_emulate/x86_emulate.h"
+
+#define cpu_has_xsave ({	   \
+    unsigned int eax = 1, ecx = 0; \
+    emul_test_cpuid(&eax, &eax, &ecx, &eax, NULL); \
+    /* Intentionally checking OSXSAVE here. */ \
+    (ecx & (1U << 27)) != 0; \
+})
+
+int emul_test_cpuid(
+    unsigned int *eax,
+    unsigned int *ebx,
+    unsigned int *ecx,
+    unsigned int *edx,
+    struct x86_emulate_ctxt *ctxt);
+
+int emul_test_read_cr(
+    unsigned int reg,
+    unsigned long *val,
+    struct x86_emulate_ctxt *ctxt);
Signature · 7 lines
2.1.4


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

Re: [PATCH RFC v2 3/7] x86emul/test: factor out emul_test_{read_cr, cpuid}

Quoted 50 lines
Quoted 1 line
Quoted 1 line · On 09.12.16 at 13:23, <[email protected]> wrote:
tools/tests/x86_emulator/test_x86_emulator.c +4 -22
@@ -92,51 +92,33 @@ static int cmpxchg(
     return X86EMUL_OKAY;
 }
 
-static int cpuid(
-    unsigned int *eax,
-    unsigned int *ebx,
-    unsigned int *ecx,
-    unsigned int *edx,
-    struct x86_emulate_ctxt *ctxt)
-{
-    unsigned int leaf = *eax;
-
-    asm ("cpuid" : "+a" (*eax), "+c" (*ecx), "=d" (*edx), "=b" (*ebx));
-
-    /* The emulator doesn't itself use MOVBE, so we can always run the test. */
-    if ( leaf == 1 )
-        *ecx |= 1U << 22;
-
-    return X86EMUL_OKAY;
-}
-
 #define cache_line_size() ({ \
     unsigned int eax = 1, ebx, ecx = 0, edx; \
-    cpuid(&eax, &ebx, &ecx, &edx, NULL); \
+    emul_test_cpuid(&eax, &ebx, &ecx, &edx, NULL); \
     edx & (1U << 19) ? (ebx >> 5) & 0x7f8 : 0; \
 })
 
 #define cpu_has_mmx ({ \
     unsigned int eax = 1, ecx = 0, edx; \
-    cpuid(&eax, &ecx, &ecx, &edx, NULL); \
+    emul_test_cpuid(&eax, &ecx, &ecx, &edx, NULL); \
     (edx & (1U << 23)) != 0; \
 })
 
 #define cpu_has_sse ({ \
     unsigned int eax = 1, ecx = 0, edx; \
-    cpuid(&eax, &ecx, &ecx, &edx, NULL); \
+    emul_test_cpuid(&eax, &ecx, &ecx, &edx, NULL); \
     (edx & (1U << 25)) != 0; \
 })
 
 #define cpu_has_sse2 ({ \
     unsigned int eax = 1, ecx = 0, edx; \
-    cpuid(&eax, &ecx, &ecx, &edx, NULL); \
+    emul_test_cpuid(&eax, &ecx, &ecx, &edx, NULL); \

tools/tests/x86_emulator/test_x86_emulator.c static int cmpxchg( · line 124

Quoted 10 lines
tools/tests/x86_emulator/test_x86_emulator.c +1 -1
     (edx & (1U << 26)) != 0; \
 })
 
 #define cpu_has_xsave ({ \
     unsigned int eax = 1, ecx = 0; \
-    cpuid(&eax, &eax, &ecx, &eax, NULL); \
+    emul_test_cpuid(&eax, &eax, &ecx, &eax, NULL); \
     /* Intentionally checking OSXSAVE here. */ \
     (ecx & (1U << 27)) != 0; \
 })

Hmm, you cpu_has_xsave in place here, but you also ...

tools/tests/x86_emulator/x86_emulate.h line 46

Quoted 13 lines
tools/tests/x86_emulator/x86_emulate.h +7 -0
@@ -37,3 +37,22 @@
 bool emul_test_make_stack_executable(void);
 
 #include "x86_emulate/x86_emulate.h"
+
+#define cpu_has_xsave ({	   \
+    unsigned int eax = 1, ecx = 0; \
+    emul_test_cpuid(&eax, &eax, &ecx, &eax, NULL); \
+    /* Intentionally checking OSXSAVE here. */ \
+    (ecx & (1U << 27)) != 0; \
+})

... add it here - why? If you mean to put it here, perhaps all of the cpu_has_* should be moved?

Jan

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

author
#6 lore raw

Re: [PATCH RFC v2 3/7] x86emul/test: factor out emul_test_{read_cr, cpuid}

On Fri, Dec 09, 2016 at 06:13:24AM -0700, Jan Beulich wrote:
[...]

Quoted 5 lines
Quoted 5 lines
tools/tests/x86_emulator/x86_emulate.h +0 -0
@@ -37,3 +37,22 @@
 bool emul_test_make_stack_executable(void);
 
Quoted 12 lines

tools/tests/x86_emulator/x86_emulate.h line 46

Quoted 8 lines
tools/tests/x86_emulator/x86_emulate.h +7 -0
 #include "x86_emulate/x86_emulate.h"
+
+#define cpu_has_xsave ({	   \
+    unsigned int eax = 1, ecx = 0; \
+    emul_test_cpuid(&eax, &eax, &ecx, &eax, NULL); \
+    /* Intentionally checking OSXSAVE here. */ \
+    (ecx & (1U << 27)) != 0; \
+})

... add it here - why? If you mean to put it here, perhaps all of the cpu_has_* should be moved?

Yes, I think moving all of them would be better.

Wei.

Quoted 2 lines

Jan

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

author
#7 lore raw

RFC PATCH v2 4/7 x86emul/test: remove unused macros

In reply to Wei Liu

---
Cc: Jan Beulich <[email protected]>
Cc: Andrew Cooper <[email protected]>
Cc: Ian Jackson <[email protected]>
Cc: Wei Liu <[email protected]>

---
 tools/tests/x86_emulator/test_x86_emulator.c | 28 ----------------------------
 1 file changed, 28 deletions(-)
tools/tests/x86_emulator/test_x86_emulator.c +0 -28
index 6099401..bcdbdff 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(
@@ -116,13 +107,6 @@ static int cmpxchg(
     (edx & (1U << 26)) != 0; \
 })
 
-#define cpu_has_xsave ({ \
-    unsigned int eax = 1, ecx = 0; \
-    emul_test_cpuid(&eax, &eax, &ecx, &eax, NULL); \
-    /* Intentionally checking OSXSAVE here. */ \
-    (ecx & (1U << 27)) != 0; \
-})
-
 static inline uint64_t xgetbv(uint32_t xcr)
 {
     uint32_t lo, hi;
@@ -140,18 +124,6 @@ static inline uint64_t xgetbv(uint32_t xcr)
     (ecx & (1U << 28)) != 0; \
 })
 
-#define cpu_has_avx2 ({ \
-    unsigned int eax = 1, ebx, ecx = 0; \
-    emul_test_cpuid(&eax, &ebx, &ecx, &eax, NULL); \
-    if ( !(ecx & (1U << 27)) || ((xgetbv(0) & 6) != 6) ) \
-        ebx = 0; \
-    else { \
-        eax = 7, ecx = 0; \
-        emul_test_cpuid(&eax, &ebx, &ecx, &eax, NULL); \
-    } \
-    (ebx & (1U << 5)) != 0; \
-})
-
 int get_fpu(
     void (*exception_callback)(void *, struct cpu_user_regs *),
     void *exception_callback_arg,
Signature · 7 lines
2.1.4


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

Re: [PATCH RFC v2 4/7] x86emul/test: remove unused macros

Quoted 5 lines
Quoted 1 line
Quoted 1 line · On 09.12.16 at 13:23, <[email protected]> wrote:
tools/tests/x86_emulator/test_x86_emulator.c +0 -0
@@ -23,15 +23,6 @@ static const struct {
 #endif

tools/tests/x86_emulator/test_x86_emulator.c static const struct { · line 33

Quoted 10 lines
tools/tests/x86_emulator/test_x86_emulator.c +0 -8
 };
 
-/* 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)

Please don't - we rather should start making use of them.

Quoted 5 lines
tools/tests/x86_emulator/test_x86_emulator.c +0 -1
@@ -140,18 +124,6 @@ static inline uint64_t xgetbv(uint32_t xcr)
     (ecx & (1U << 28)) != 0; \
 })
 
-#define cpu_has_avx2 ({ \

tools/tests/x86_emulator/test_x86_emulator.c static inline uint64_t xgetbv(uint32_t xcr) · line 153

Quoted 10 lines
tools/tests/x86_emulator/test_x86_emulator.c +0 -10
-    unsigned int eax = 1, ebx, ecx = 0; \
-    emul_test_cpuid(&eax, &ebx, &ecx, &eax, NULL); \
-    if ( !(ecx & (1U << 27)) || ((xgetbv(0) & 6) != 6) ) \
-        ebx = 0; \
-    else { \
-        eax = 7, ecx = 0; \
-        emul_test_cpuid(&eax, &ebx, &ecx, &eax, NULL); \
-    } \
-    (ebx & (1U << 5)) != 0; \
-})

Please don't - I already have a patch queued needing this.

Jan

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

author
#9 lore raw

Re: [PATCH RFC v2 4/7] x86emul/test: remove unused macros

Quoted 22 lines · Jan Beulich
Quoted 5 lines
Quoted 1 line
Quoted 1 line · On 09.12.16 at 13:23, <[email protected]> wrote:
tools/tests/x86_emulator/test_x86_emulator.c +0 -0
@@ -23,15 +23,6 @@ static const struct {
 #endif

tools/tests/x86_emulator/test_x86_emulator.c static const struct { · line 33

Quoted 10 lines
tools/tests/x86_emulator/test_x86_emulator.c +0 -8
 };
 
-/* 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)

Please don't - we rather should start making use of them.

Quoted 4 lines
tools/tests/x86_emulator/test_x86_emulator.c +0 -0
@@ -140,18 +124,6 @@ static inline uint64_t xgetbv(uint32_t xcr)
     (ecx & (1U << 28)) != 0; \
 })
 
Quoted 14 lines

tools/tests/x86_emulator/test_x86_emulator.c static inline uint64_t xgetbv(uint32_t xcr) · line 153

Quoted 11 lines
tools/tests/x86_emulator/test_x86_emulator.c +0 -11
-#define cpu_has_avx2 ({ \
-    unsigned int eax = 1, ebx, ecx = 0; \
-    emul_test_cpuid(&eax, &ebx, &ecx, &eax, NULL); \
-    if ( !(ecx & (1U << 27)) || ((xgetbv(0) & 6) != 6) ) \
-        ebx = 0; \
-    else { \
-        eax = 7, ecx = 0; \
-        emul_test_cpuid(&eax, &ebx, &ecx, &eax, NULL); \
-    } \
-    (ebx & (1U << 5)) != 0; \
-})

Please don't - I already have a patch queued needing this.

No problem. I will see if this patch is still needed, given your comment on previous patch.

Wei.

Quoted 2 lines

Jan

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

author
#10 lore raw

RFC PATCH v2 5/7 tools/fuzz: introduce x86 instruction emulator target

In reply to Wei Liu

Instruction emulator fuzzing code is from code previous written by Andrew and George. Adapted to llvm fuzzer and hook up the build system.

---
Cc: Andrew Cooper <[email protected]>
Cc: George Dunlap <[email protected]>
Cc: Ian Jackson <[email protected]>
Cc: Jan Beulich <[email protected]>
Cc: Konrad Rzeszutek Wilk <[email protected]>
Cc: Stefano Stabellini <[email protected]>
Cc: Tim Deegan <[email protected]>
Cc: Wei Liu <[email protected]>

---
 .gitignore                                         |   1 +
 tools/fuzz/x86_instruction_emulator/Makefile       |  31 +++
 .../x86-insn-emulator-fuzzer.c                     | 262 +++++++++++++++++++++
 3 files changed, 294 insertions(+)
 create mode 100644 tools/fuzz/x86_instruction_emulator/Makefile
 create mode 100644 tools/fuzz/x86_instruction_emulator/x86-insn-emulator-fuzzer.c
.gitignore +1 -0
index a2f34a1..d507243 100644
@@ -145,6 +145,7 @@ tools/flask/utils/flask-loadpolicy
 tools/flask/utils/flask-setenforce
 tools/flask/utils/flask-set-bool
 tools/flask/utils/flask-label-pci
+tools/fuzz/x86_instruction_emulator/x86_emulate*
 tools/helpers/_paths.h
 tools/helpers/init-xenstore-domain
 tools/helpers/xen-init-dom0
tools/fuzz/x86_instruction_emulator/Makefile new file +31 -0
new file mode 100644
index 0000000..2b147ac
@@ -0,0 +1,31 @@
+XEN_ROOT=$(CURDIR)/../../..
+include $(XEN_ROOT)/tools/Rules.mk
+
+x86-instruction-emulator-fuzzer-all: x86-insn-emulator.a x86-insn-emulator-fuzzer.o
+
+x86_emulate/x86_emulate.c x86_emulate/x86_emulate.h:
+	[ -L x86_emulate ] || ln -sf $(XEN_ROOT)/xen/arch/x86/x86_emulate .
+
+x86_emulate.c x86_emulate.h: %:
+	[ -L $* ] || ln -sf $(XEN_ROOT)/tools/tests/x86_emulator/$*
+
+CFLAGS += $(CFLAGS_xeninclude)
+
+x86_emulate.o: x86_emulate.c x86_emulate.h x86_emulate/x86_emulate.c x86_emulate/x86_emulate.h
+
+x86-insn-emulator.a: x86_emulate.o
+	$(AR) rc $@ $^
+
+x86-insn-emulator-fuzzer.o: x86-insn-emulator-fuzzer.c
+
+# Common targets
+.PHONY: all
+all: x86-instruction-emulator-fuzzer-all
+
+.PHONY: distclean
+distclean: clean
+	rm -f x86_emulate x86_emulate.c x86_emulate.h
+
+.PHONY: clean
+clean:
+	rm -f *.a *.o
tools/fuzz/x86_instruction_emulator/x86-insn-emulator-fuzzer.c new file +262 -0

262 lines not on this page · raw message

Signature · 7 lines
2.1.4


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

Re: [PATCH RFC v2 5/7] tools/fuzz: introduce x86 instruction emulator target

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

Quoted 10 lines
Quoted 1 line
Quoted 1 line · On 09.12.16 at 13:23, <[email protected]> wrote:
tools/fuzz/x86_instruction_emulator/x86-insn-emulator-fuzzer.c new file +9 -0
+static int fuzz_cpuid(
+    unsigned int *eax,
+    unsigned int *ebx,
+    unsigned int *ecx,
+    unsigned int *edx,
+    struct x86_emulate_ctxt *ctxt)
+{
+    return emul_test_cpuid(eax, ebx, ecx, edx, ctxt);
+}

Please use emul_test_cpuid directly for the hook (same for fuzz_read_cr() then).

Quoted 18 lines
tools/fuzz/x86_instruction_emulator/x86-insn-emulator-fuzzer.c new file +18 -0
+#define cpu_has_mmx ({ \
+    unsigned int eax = 1, ecx = 0, edx; \
+    fuzz_cpuid(&eax, &ecx, &ecx, &edx, NULL); \
+    (edx & (1U << 23)) != 0; \
+})
+
+#define cpu_has_sse ({ \
+    unsigned int eax = 1, ecx = 0, edx; \
+    fuzz_cpuid(&eax, &ecx, &ecx, &edx, NULL); \
+    (edx & (1U << 25)) != 0; \
+})
+
+static inline uint64_t xgetbv(uint32_t xcr)
+{
+    uint32_t lo, hi;
+
+    asm ( ".byte 0x0f, 0x01, 0xd0" : "=a" (lo), "=d" (hi) : "c" (xcr) );
+

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

Quoted 10 lines
tools/fuzz/x86_instruction_emulator/x86-insn-emulator-fuzzer.c new file +10 -0
+    return ((uint64_t)hi << 32) | lo;
+}
+
+#define cpu_has_avx ({ \
+    unsigned int eax = 1, ecx = 0; \
+    fuzz_cpuid(&eax, &eax, &ecx, &eax, NULL); \
+    if ( !(ecx & (1U << 27)) || ((xgetbv(0) & 6) != 6) ) \
+        ecx = 0; \
+    (ecx & (1U << 28)) != 0; \
+})

See my comment on the earlier patch regarding sharing more of the cpu_has_*.

Quoted 14 lines
tools/fuzz/x86_instruction_emulator/x86-insn-emulator-fuzzer.c new file +14 -0
+static int fuzz_get_fpu(
+    void (*exception_callback)(void *, struct cpu_user_regs *),
+    void *exception_callback_arg,
+    enum x86_emulate_fpu_type type,
+    struct x86_emulate_ctxt *ctxt)
+{
+    switch ( type )
+    {
+    case X86EMUL_FPU_fpu:
+        break;
+    case X86EMUL_FPU_mmx:
+        if ( cpu_has_mmx )
+            break;
+    case X86EMUL_FPU_xmm:

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

Quoted 10 lines
tools/fuzz/x86_instruction_emulator/x86-insn-emulator-fuzzer.c new file +10 -0
+        if ( cpu_has_sse )
+            break;
+    case X86EMUL_FPU_ymm:
+        if ( cpu_has_avx )
+            break;
+    default:
+        return X86EMUL_UNHANDLEABLE;
+    }
+    return X86EMUL_OKAY;
+}

This looks to be identical to the test harness'es variant too.

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

Quoted 12 lines
tools/fuzz/x86_instruction_emulator/x86-insn-emulator-fuzzer.c new file +12 -0
+int LLVMFuzzerTestOneInput(const uint8_t *data_p, size_t size)
+{
+    bool stack_exec;
+    struct cpu_user_regs regs = {};
+    struct x86_emulate_ctxt ctxt =
+        {
+            .regs = &regs,
+            .addr_size = 8 * sizeof(void *),
+            .sp_size = 8 * sizeof(void *),
+        };
+
+    int nr = 0;

unsigned?

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

Quoted 7 lines
tools/fuzz/x86_instruction_emulator/x86-insn-emulator-fuzzer.c new file +7 -0
+    int rc;
+    unsigned x;
+    const uint8_t *p = data_p;
+
+    stack_exec = emul_test_make_stack_executable();
+    if (!stack_exec)
+        printf("Warning: Stack could not be made executable (%d).\n", errno);

I don't think it's worth continuing in case of failure here (as fuzzed input is too likely to hit one of the cases needing an on-stack stub).

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

Quoted 12 lines
tools/fuzz/x86_instruction_emulator/x86-insn-emulator-fuzzer.c new file +12 -0
+    /* Reset all global states */
+    memset(data, 0, sizeof(data));
+    data_index = 0;
+    data_max = 0;
+
+    nr = size < sizeof(regs) ? size : sizeof(regs);
+
+    memcpy(&regs, p, nr);
+    p += sizeof(regs);
+    nr += sizeof(regs);
+
+    if (nr <= size) {

Please use hypervisor coding style in this directory, just like test_x86_emulator.c at least attempts to do.

Jan

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

author
#12 lore raw

RFC PATCH v2 6/7 tools: hook up fuzz directory

In reply to Wei Liu

This will make all fuzzing targets get build every time tools directory is built. This serves as basic regression test.

---
Cc: Andrew Cooper <[email protected]>
Cc: George Dunlap <[email protected]>
Cc: Ian Jackson <[email protected]>
Cc: Jan Beulich <[email protected]>
Cc: Konrad Rzeszutek Wilk <[email protected]>
Cc: Stefano Stabellini <[email protected]>
Cc: Tim Deegan <[email protected]>
Cc: Wei Liu <[email protected]>

---
 tools/Makefile      |  1 +
 tools/fuzz/Makefile | 11 +++++++++++
 2 files changed, 12 insertions(+)
 create mode 100644 tools/fuzz/Makefile
tools/Makefile +1 -0
index 71515b4..77e0723 100644
@@ -6,6 +6,7 @@ SUBDIRS-y += include
 SUBDIRS-y += libs
 SUBDIRS-y += libxc
 SUBDIRS-y += flask
+SUBDIRS-y += fuzz
 SUBDIRS-y += xenstore
 SUBDIRS-y += misc
 SUBDIRS-y += examples
tools/fuzz/Makefile new file +11 -0
new file mode 100644
index 0000000..ce00b82
@@ -0,0 +1,11 @@
+XEN_ROOT = $(CURDIR)/../..
+include $(XEN_ROOT)/tools/Rules.mk
+
+SUBDIRS-y :=
+SUBDIRS-y += libelf
+SUBDIRS-y += x86_instruction_emulator
+
+.PHONY: all clean distclean
+all clean distclean: %: subdirs-%
+
+install:
Signature · 7 lines
2.1.4


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

RFC PATCH v2 7/7 tools/fuzz: add README

In reply to Wei Liu

---
Cc: Andrew Cooper <[email protected]>
Cc: George Dunlap <[email protected]>
Cc: Ian Jackson <[email protected]>
Cc: Jan Beulich <[email protected]>
Cc: Konrad Rzeszutek Wilk <[email protected]>
Cc: Stefano Stabellini <[email protected]>
Cc: Tim Deegan <[email protected]>
Cc: Wei Liu <[email protected]>

---
 tools/fuzz/README | 39 +++++++++++++++++++++++++++++++++++++++
 1 file changed, 39 insertions(+)
 create mode 100644 tools/fuzz/README
tools/fuzz/README new file +39 -0
new file mode 100644
index 0000000..cf47bf6
@@ -0,0 +1,39 @@
+# OVERVIEW
+
+This directory provides fuzzing targets to be run inside Google
+oss-fuzz infrastructure.
+
+See also https://github.com/google/oss-fuzz.
+
+# HOW IT WORKS
+
+We need to provide the source code and the rune to produce objects or
+archives (artefacts) from source code. These items ideally should live
+inside xen.git so that they can be kept up to date.
+
+The artefacts contain all the code we wish to fuzz and a function
+called LLVMFuzzerTestOneInput. LLVMFuzzerTestOneInput is the entry
+point to the code we wish to fuzz. Note that we don't produce
+executable programs because we don't have libFuzzEngine
+locally. libFuzzEngine is maintained by oss-fuzz.
+
+We also provide build script to oss-fuzz. The build script will
+inherit the correct compiler settings and be run in a pre-setup
+environment, which has libFuzzEngine installed. The build script is
+responsible for calling the correct Xen build rune to produce the
+artefacts, then link them against libFuzzEngine to produce
+executables, which will run in oss-fuzz infrastructure.
+
+Please refer to official oss-fuzz documents for the most up-to-date
+descriptions for all moving parts.
+
+# HOW TO IMPROVE THE FUZZING TARGETS
+
+Feel free to modify each fuzzing targets at will. Make sure they build
+by invoking make as you would build tools.
+
+To actually test the new code, you would need to run the target in
+standalone mode, please refer to oss-fuzz documents on how to do that.
+
+It is highly recommended that you run the new target for a while to
+weed out error in plumbing code to avoid false positives.
Signature · 7 lines
2.1.4


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
#14 lore raw
In reply to Wei Liu
Quoted 10 lines
Quoted 1 line
Quoted 1 line · On 09.12.16 at 13:23, <[email protected]> wrote:

This series adds two fuzzing targets to run in Google's oss-fuzz infrastructure.

There will be some other patches on the oss-fuzz side. Their recommendation is to have all the fuzzing targets committed in our tree so that they can be kept up to date.

The fuzzing targets aren't very sophiscated at the moment. The purpose of this series is to gather feedback at this early stage.

Hmm, didn't you mean to explain here or in the patches introducing those targets how they're actually intended to be used? I still don't understand how producing object files and archives can do any good (on its own).

Jan

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

author
#15 lore raw
Quoted 8 lines · Jan Beulich
Quoted 8 lines
Quoted 1 line
Quoted 1 line · On 09.12.16 at 13:23, <[email protected]> wrote:

This series adds two fuzzing targets to run in Google's oss-fuzz infrastructure.

There will be some other patches on the oss-fuzz side. Their recommendation is to have all the fuzzing targets committed in our tree so that they can be kept up to date.

Quoted 8 lines
Quoted 2 lines

The fuzzing targets aren't very sophiscated at the moment. The purpose of this series is to gather feedback at this early stage.

Hmm, didn't you mean to explain here or in the patches introducing those targets how they're actually intended to be used? I still don't understand how producing object files and archives can do any good (on its own).

It's documented in README. Maybe you can point out what you would like to know there?

Wei.

Quoted 2 lines

Jan

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

#16 lore raw
Quoted 13 lines
Quoted 1 line
Quoted 1 line · On 09.12.16 at 14:09, <[email protected]> wrote:
Quoted 11 lines · Jan Beulich
Quoted 10 lines
Quoted 1 line
Quoted 1 line · On 09.12.16 at 13:23, <[email protected]> wrote:

This series adds two fuzzing targets to run in Google's oss-fuzz infrastructure.

There will be some other patches on the oss-fuzz side. Their recommendation is to have all the fuzzing targets committed in our tree so that they can be kept up to date.

The fuzzing targets aren't very sophiscated at the moment. The purpose of this series is to gather feedback at this early stage.

Quoted 7 lines
Quoted 4 lines

Hmm, didn't you mean to explain here or in the patches introducing those targets how they're actually intended to be used? I still don't understand how producing object files and archives can do any good (on its own).

It's documented in README. Maybe you can point out what you would like to know there?

Oh, I see - that's good enough, just not really ideal to sit at the end of the series.

Jan

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