Add virtual device fuzzing support
Subsystems (2)
v9 cover Add virtual device fuzzing support
Hello,
This series adds a framework for coverage-guided fuzzing of virtual-devices. Fuzzing targets are based on qtest and can make use of the libqos abstractions.
V9:
* Fix bug in the virtio-scsi fuzzer. Virtqueues were being kicked only if free_head != 0 (which it never was).
* Move vl.c and main.c into a new directory: softmmu/
* virtio-net-fuzz: refactor the looop over used descriptor.
* Improve comments for i440fx and virtio-scsi fuzzers.
V8:
* Small fixes to the virtio-net.
* Keep rcu_atfork when not using qtest.
V7:
* virtio-net: add virtio-net-check-used which waits for inputs on the tx/ctrl vq by watching the used vring.
* virtio-net: add virtio-net-socket which uses the socket backend and can exercise the rx components of virtio-net.
* virtio-net: add virtio-net-slirp which uses the user backend and exercises slirp. This may lead to real traffic emitted by qemu so it is best to run in an isolated network environment.
* build should succeed after each commit
V5/V6:
* added virtio-scsi fuzzer
* add support for using fork-based fuzzers with multiple libfuzzer workers
* misc fixes addressing V4 comments
* cleanup in-process handlers/globals in libqtest.c
* small fixes to fork-based fuzzing and support for multiple workers
* changes to the virtio-net fuzzer to kick after each vq add
V4:
* add/transfer license headers to new files
* restructure the added QTestClientTransportOps struct
* restructure the FuzzTarget struct and fuzzer skeleton
* fork-based fuzzer now directly mmaps shm over the coverage bitmaps
* fixes to i440 and virtio-net fuzz targets
* undo the changes to qtest_memwrite
* possible to build /fuzz and /all in the same build-dir
* misc fixes to address V3 comments
V3:
* rebased onto v4.1.0+
* add the fuzzer as a new build-target type in the build-system
* add indirection to qtest client/server communication functions
* remove ramfile and snapshot-based fuzzing support
* add i440fx fuzz-target as a reference for developers.
* add linker-script to assist with fork-based fuzzer
V2:
* split off changes to qos virtio-net and qtest server to other patches
* move vl:main initialization into new func: qemu_init
* moved useful functions from qos-test.c to a separate object
* use struct of function pointers for add_fuzz_target(), instead of arguments
* move ramfile to migration/qemu-file
* rewrite fork-based fuzzer pending patch to libfuzzer
* pass check-patch
Alexander Bulekov (23):
checkpatch: replace vl.c in the top of repo check
softmmu: move vl.c to softmmu/
softmmu: split off vl.c:main() into main.c
module: check module wasn't already initialized
fuzz: add FUZZ_TARGET module type
qtest: add qtest_server_send abstraction
libqtest: add a layer of abstraction to send/recv
libqtest: make bufwrite rely on the TransportOps
qtest: add in-process incoming command handler
libqos: rename i2c_send and i2c_recv
libqos: split qos-test and libqos makefile vars
libqos: move useful qos-test funcs to qos_external
fuzz: add fuzzer skeleton
exec: keep ram block across fork when using qtest
main: keep rcu_atfork callback enabled for qtest
fuzz: support for fork-based fuzzing.
fuzz: add support for qos-assisted fuzz targets
fuzz: add target/fuzz makefile rules
fuzz: add configure flag --enable-fuzzing
fuzz: add i440fx fuzz targets
fuzz: add virtio-net fuzz target
fuzz: add virtio-scsi fuzz target
fuzz: add documentation to docs/devel/
Makefile | 15 +- Makefile.objs | 2 - Makefile.target | 19 ++- configure | 39 +++++ docs/devel/fuzzing.txt | 116 ++++++++++++++ exec.c | 12 +- include/qemu/module.h | 4 +- include/sysemu/qtest.h | 4 + include/sysemu/sysemu.h | 4 + qtest.c | 31 +++- scripts/checkpatch.pl | 2 +- softmmu/Makefile.objs | 3 + softmmu/main.c | 53 +++++++ vl.c => softmmu/vl.c | 48 +++--- tests/qtest/Makefile.include | 72 ++++----- tests/qtest/fuzz/Makefile.include | 18 +++ tests/qtest/fuzz/fork_fuzz.c | 55 +++++++ tests/qtest/fuzz/fork_fuzz.h | 23 +++ tests/qtest/fuzz/fork_fuzz.ld | 37 +++++ tests/qtest/fuzz/fuzz.c | 179 +++++++++++++++++++++ tests/qtest/fuzz/fuzz.h | 95 +++++++++++ tests/qtest/fuzz/i440fx_fuzz.c | 193 +++++++++++++++++++++++ tests/qtest/fuzz/qos_fuzz.c | 234 ++++++++++++++++++++++++++++ tests/qtest/fuzz/qos_fuzz.h | 33 ++++ tests/qtest/fuzz/virtio_net_fuzz.c | 198 +++++++++++++++++++++++ tests/qtest/fuzz/virtio_scsi_fuzz.c | 214 +++++++++++++++++++++++++ tests/qtest/libqos/i2c.c | 10 +- tests/qtest/libqos/i2c.h | 4 +- tests/qtest/libqos/qos_external.c | 168 ++++++++++++++++++++ tests/qtest/libqos/qos_external.h | 28 ++++ tests/qtest/libqtest.c | 119 ++++++++++++-- tests/qtest/libqtest.h | 4 + tests/qtest/pca9552-test.c | 10 +- tests/qtest/qos-test.c | 132 +--------------- util/module.c | 7 + 35 files changed, 1958 insertions(+), 227 deletions(-) create mode 100644 docs/devel/fuzzing.txt create mode 100644 softmmu/Makefile.objs create mode 100644 softmmu/main.c rename vl.c => softmmu/vl.c (99%) create mode 100644 tests/qtest/fuzz/Makefile.include create mode 100644 tests/qtest/fuzz/fork_fuzz.c create mode 100644 tests/qtest/fuzz/fork_fuzz.h create mode 100644 tests/qtest/fuzz/fork_fuzz.ld create mode 100644 tests/qtest/fuzz/fuzz.c create mode 100644 tests/qtest/fuzz/fuzz.h create mode 100644 tests/qtest/fuzz/i440fx_fuzz.c create mode 100644 tests/qtest/fuzz/qos_fuzz.c create mode 100644 tests/qtest/fuzz/qos_fuzz.h create mode 100644 tests/qtest/fuzz/virtio_net_fuzz.c create mode 100644 tests/qtest/fuzz/virtio_scsi_fuzz.c create mode 100644 tests/qtest/libqos/qos_external.c create mode 100644 tests/qtest/libqos/qos_external.h
Signature · 1 line
2.25.0
PATCH v9 1/23 checkpatch: replace vl.c in the top of repo check
524b4c2c5c moves vl.c into softmmu/ , breaking the checkpatch top-of-kernel-tree check. Replace with checks for softmmu and linux-user
--- scripts/checkpatch.pl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
scripts/checkpatch.pl +1 -1
@@ -462,7 +462,7 @@ sub top_of_kernel_tree { my @tree_check = ( "COPYING", "MAINTAINERS", "Makefile", "README.rst", "docs", "VERSION", - "vl.c" + "softmmu", "linux-user" ); foreach my $check (@tree_check) {
Signature · 1 line
2.25.0
Re: [PATCH v9 01/23] checkpatch: replace vl.c in the top of repo check
Quoted 1 line · Alexander Bulekov
524b4c2c5cmovesvl.cinto softmmu/ , breaking the checkpatch
524b4c2c5c is a local git sha1. That commit will have a different sha1 when applied to qemu.git/master. Saying "The next patch" instead would be fine.
However, this patch leaves the tree in a state where checkpatch.pl will fail because softmmu/ doesn't exist yet! Please squash this patch into the next commit instead.
I guess you kept it separate because changing checkpatch.pl can be thought of as a separate change. However, they two need to happen in a single step in order for checkpatch.pl to function correctly at each commit. Therefore it's appropriate to combine them into a single commit.
Quoted 23 lines
top-of-kernel-tree check. Replace with checks for softmmu and linux-user
Signed-off-by Alexander Bulekov <[email protected]>--- scripts/checkpatch.pl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)scripts/checkpatch.pl +1
-1@@ -462,7 +462,7 @@ sub top_of_kernel_tree { my @tree_check = ( "COPYING", "MAINTAINERS", "Makefile", "README.rst", "docs", "VERSION", - "vl.c" + "softmmu", "linux-user" ); foreach my $check (@tree_check) {Signature · 1 line
2.25.0
PATCH v9 2/23 softmmu: move vl.c to softmmu/
In reply to Alexander Bulekov--- Makefile.objs | 2 -- Makefile.target | 1 + softmmu/Makefile.objs | 2 ++ vl.c => softmmu/vl.c | 0 4 files changed, 3 insertions(+), 2 deletions(-) create mode 100644 softmmu/Makefile.objs rename vl.c => softmmu/vl.c (100%)
Makefile.objs +0 -2
@@ -58,8 +58,6 @@ common-obj-y += ui/ common-obj-m += ui/ common-obj-y += dma-helpers.o -common-obj-y += vl.o -vl.o-cflags := $(GPROF_CFLAGS) $(SDL_CFLAGS) common-obj-$(CONFIG_TPM) += tpm.o common-obj-y += backends/
Makefile.target +1 -0
@@ -160,6 +160,7 @@ obj-y += qapi/ obj-y += memory.o obj-y += memory_mapping.o obj-y += migration/ram.o +obj-y += softmmu/ LIBS := $(libs_softmmu) $(LIBS) # Hardware support
softmmu/Makefile.objs new file +2 -0
@@ -0,0 +1,2 @@ +obj-y += vl.o +vl.o-cflags := $(GPROF_CFLAGS) $(SDL_CFLAGS)
softmmu/vl.c renamed from vl.c +0 -0
Signature · 1 line
2.25.0
Re: [PATCH v9 02/23] softmmu: move vl.c to softmmu/
Quoted 7 lines · Alexander Bulekov
Signed-off-by Alexander Bulekov <[email protected]>--- Makefile.objs | 2 -- Makefile.target | 1 + softmmu/Makefile.objs | 2 ++ vl.c => softmmu/vl.c | 0 4 files changed, 3 insertions(+), 2 deletions(-)
Please update the ./MAINTAINERS entry for vl.c here too.
There is also a top_of_tree check in scripts/get_maintainer.pl that needs to be updated in this commit.
PATCH v9 3/23 softmmu: split off vl.c:main() into main.c
In reply to Alexander BulekovA program might rely on functions implemented in vl.c, but implement its own main(). By placing main into a separate source file, there are no complaints about duplicate main()s when linking against vl.o. For example, the virtual-device fuzzer uses a main() provided by libfuzzer, and needs to perform some initialization before running the softmmu initialization. Now, main simply calls three vl.c functions which handle the guest initialization, main loop and cleanup.
--- Makefile.target | 2 +- include/sysemu/sysemu.h | 4 ++++ softmmu/Makefile.objs | 1 + softmmu/main.c | 53 +++++++++++++++++++++++++++++++++++++++++ softmmu/vl.c | 36 +++++++--------------------- 5 files changed, 68 insertions(+), 28 deletions(-) create mode 100644 softmmu/main.c
Makefile.target +1 -1
@@ -203,7 +203,7 @@ endif COMMON_LDADDS = ../libqemuutil.a # build either PROG or PROGW -$(QEMU_PROG_BUILD): $(all-obj-y) $(COMMON_LDADDS) +$(QEMU_PROG_BUILD): $(all-obj-y) $(COMMON_LDADDS) $(softmmu-main-y) $(call LINK, $(filter-out %.mak, $^)) ifdef CONFIG_DARWIN $(call quiet-command,Rez -append $(SRC_PATH)/pc-bios/qemu.rsrc -o $@,"REZ","$(TARGET_DIR)$@")
include/sysemu/sysemu.h +4 -0
@@ -116,6 +116,10 @@ QemuOpts *qemu_get_machine_opts(void);bool defaults_enabled (void ); +void qemu_init (int argc,char **argv,char **envp); +void qemu_main_loop (void ); +void qemu_cleanup (void ); +extern QemuOptsList qemu_legacy_drive_opts;extern QemuOptsList qemu_common_drive_opts;extern QemuOptsList qemu_drive_opts;
softmmu/Makefile.objs +1 -0
@@ -1,2 +1,3 @@ +softmmu-main-y = softmmu/main.o obj-y += vl.o vl.o-cflags := $(GPROF_CFLAGS) $(SDL_CFLAGS)
softmmu/main.c new file +53 -0
53 lines not on this page · raw message
softmmu/vl.c +9 -27
@@ -36,25 +36,6 @@#include "sysemu/seccomp.h" #include "sysemu/tcg.h" -#ifdef CONFIG_SDL -#if defined(__APPLE__) || defined(main) -#include <SDL.h> -int qemu_main (int argc,char **argv,char **envp); -int main (int argc,char **argv) -{ -return qemu_main (argc, argv,NULL ); -} -#undef main -#define main qemu_main -#endif -#endif /* CONFIG_SDL */ - -#ifdef CONFIG_COCOA -#undef main -#define main qemu_main -#endif /* CONFIG_COCOA */ - -#include "qemu/error-report.h" #include "qemu/sockets.h" #include "sysemu/accel.h"
@@ -1671,7 +1652,7 @@ static bool main_loop_should_exit(void)return false; } -static void main_loop (void ) +void qemu_main_loop (void ) {#ifdef CONFIG_PROFILER int64_t ti;
@@ -2839,7 +2820,7 @@ static void configure_accelerators(const char *progname) } } -int main (int argc,char **argv,char **envp) +void qemu_init (int argc,char **argv,char **envp) {int i;int snapshot, linux_boot;
@@ -3391,7 +3372,7 @@ int main(int argc, char **argv, char **envp)case QEMU_OPTION_watchdog:if (watchdog) {error_report ("only one watchdog option may be given" ); -return 1 ; +exit (1 ); } watchdog = optarg;break ;
@@ -4300,7 +4281,7 @@ int main(int argc, char **argv, char **envp)parse_numa_opts (current_machine);/* do monitor/qmp handling at preconfig state if requested */ -main_loop (); +qemu_main_loop ();audio_init_audiodevs ();
@@ -4418,7 +4399,7 @@ int main(int argc, char **argv, char **envp)if (vmstate_dump_file) {/* dump and exit */ dump_vmstate_json_to_file (vmstate_dump_file); -return 0 ; +exit (0 ); }if (incoming) {
@@ -4435,8 +4416,11 @@ int main(int argc, char **argv, char **envp)accel_setup_post (current_machine);os_setup_post (); -main_loop (); +return ; +} +void qemu_cleanup (void ) +{gdbserver_cleanup ();/*
@@ -4473,6 +4457,4 @@ int main(int argc, char **argv, char **envp)qemu_chr_cleanup ();user_creatable_cleanup ();/* TODO: unref root container, check all devices are ok */ - -return 0 ; }
Signature · 1 line
2.25.0
PATCH v9 4/23 module: check module wasn't already initialized
In reply to Alexander BulekovThe virtual-device fuzzer must initialize QOM, prior to running vl:qemu_init, so that it can use the qos_graph to identify the arguments required to initialize a guest for libqos-assisted fuzzing. This change prevents errors when vl:qemu_init tries to (re)initialize the previously initialized QOM module.
--- util/module.c | 7 +++++++ 1 file changed, 7 insertions(+)
util/module.c +7 -0
@@ -30,6 +30,7 @@ typedef struct ModuleEntry typedef QTAILQ_HEAD(, ModuleEntry) ModuleTypeList;static ModuleTypeList init_type_list[MODULE_INIT_MAX ]; +static bool modules_init_done[MODULE_INIT_MAX ];static ModuleTypeList dso_init_list;
@@ -91,11 +92,17 @@ void module_call_init(module_init_type type)ModuleTypeList *l;ModuleEntry *e; +if (modules_init_done[type]) { +return ; + } + l =find_type (type); QTAILQ_FOREACH(e, l, node) { e->init (); } + + modules_init_done[type] = true; }#ifdef CONFIG_MODULES
Signature · 1 line
2.25.0
PATCH v9 5/23 fuzz: add FUZZ_TARGET module type
In reply to Alexander Bulekov--- include/qemu/module.h | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
include/qemu/module.h +3 -1
@@ -46,6 +46,7 @@ typedef enum {MODULE_INIT_TRACE ,MODULE_INIT_XEN_BACKEND ,MODULE_INIT_LIBQOS , +MODULE_INIT_FUZZ_TARGET , MODULE_INIT_MAX } module_init_type;
@@ -56,7 +57,8 @@ typedef enum {#define xen_backend_init (function) module_init(function, \ MODULE_INIT_XEN_BACKEND)#define libqos_init (function) module_init(function, MODULE_INIT_LIBQOS) - +#define fuzz_target_init (function) module_init(function, \ + MODULE_INIT_FUZZ_TARGET)#define block_module_load_one (lib) module_load_one("block-", lib)#define ui_module_load_one (lib) module_load_one("ui-", lib)#define audio_module_load_one (lib) module_load_one("audio-", lib)
Signature · 1 line
2.25.0
PATCH v9 6/23 qtest: add qtest_server_send abstraction
In reply to Alexander Bulekovqtest_server_send is a function pointer specifying the handler used to transmit data to the qtest client. In the standard configuration, this calls the CharBackend handler, but now it is possible for other types of handlers, e.g direct-function calls if the qtest client and server exist within the same process (inproc)
--- include/sysemu/qtest.h | 3 +++ qtest.c | 18 ++++++++++++++++-- 2 files changed, 19 insertions(+), 2 deletions(-)
include/sysemu/qtest.h +3 -0
@@ -26,4 +26,7 @@ bool qtest_driver(void);void qtest_server_init (const char *qtest_chrdev,const char *qtest_log,Error **errp); +void qtest_server_set_send_handler (void (*send)(void *,const char *), +void *opaque); +#endif
qtest.c +16 -2
@@ -42,6 +42,8 @@ static GString *inbuf;static int irq_levels[MAX_IRQ ];static qemu_timeval start_time;static bool qtest_opened; +static void (*qtest_server_send)(void *,const char *); +static void *qtest_server_send_opaque;#define FMT_timeval "%ld.%06ld"
@@ -228,8 +230,10 @@ static void GCC_FMT_ATTR(1, 2) qtest_log_send(const char *fmt, ...)va_end (ap); } -static void do_qtest_send (CharBackend *chr,const char *str,size_t len) +static void qtest_server_char_be_send (void *opaque,const char *str) { +size_t len =strlen (str); +CharBackend * chr = (CharBackend *)opaque;qemu_chr_fe_write_all (chr, (uint8_t *)str, len);if (qtest_log_fp && qtest_opened) {fprintf (qtest_log_fp,"%s" , str);
@@ -238,7 +242,7 @@ static void do_qtest_send(CharBackend *chr, const char *str, size_t len)static void qtest_send (CharBackend *chr,const char *str) { -do_qtest_send (chr, str,strlen (str)); +qtest_server_send (qtest_server_send_opaque, str); } static void GCC_FMT_ATTR(2, 3) qtest_sendf(CharBackend *chr,
@@ -783,6 +787,16 @@ void qtest_server_init(const char *qtest_chrdev, const char *qtest_log, Error **qemu_chr_fe_set_echo (&qtest_chr, true); inbuf =g_string_new ("" ); + +if (!qtest_server_send) { +qtest_server_set_send_handler (qtest_server_char_be_send, &qtest_chr); + } +} + +void qtest_server_set_send_handler (void (*send)(void *,const char *),void *opaque) +{ + qtest_server_send = send; + qtest_server_send_opaque = opaque; }bool qtest_driver (void )
Signature · 1 line
2.25.0
PATCH v9 7/23 libqtest: add a layer of abstraction to send/recv
In reply to Alexander BulekovThis makes it simple to swap the transport functions for qtest commands to and from the qtest client. For example, now it is possible to directly pass qtest commands to a server handler that exists within the same process, without the standard way of writing to a file descriptor.
--- tests/qtest/libqtest.c | 48 ++++++++++++++++++++++++++++++++++-------- 1 file changed, 39 insertions(+), 9 deletions(-)
tests/qtest/libqtest.c +39 -9
@@ -35,6 +35,15 @@#define SOCKET_TIMEOUT 50#define SOCKET_MAX_FDS 16 + +typedef void (*QTestSendFn )(QTestState *s,const char *buf); +typedef GString * (*QTestRecvFn )(QTestState *); + +typedef struct QTestClientTransportOps { +QTestSendFn send;/* for sending qtest commands */ +QTestRecvFn recv_line;/* for receiving qtest command responses */ +}QTestTransportOps ; +struct QTestState {int fd;
@@ -45,6 +54,7 @@ struct QTestStatebool big_endian;bool irq_level[MAX_IRQ ];GString *rx; +QTestTransportOps ops; };static GHookList abrt_hooks;
@@ -52,6 +62,14 @@ static struct sigaction sigact_old;static int qtest_query_target_endianness (QTestState *s); +static void qtest_client_socket_send (QTestState *,const char *buf); +static void socket_send (int fd,const char *buf,size_t size); + +static GString *qtest_client_socket_recv_line (QTestState *); + +static void qtest_client_set_tx_handler (QTestState *s,QTestSendFn send); +static void qtest_client_set_rx_handler (QTestState *s,QTestRecvFn recv); +static int init_socket (const char *socket_path) {struct sockaddr_un addr;
@@ -234,6 +252,9 @@ QTestState *qtest_init_without_qmp_handshake(const char *extra_args) sock =init_socket (socket_path); qmpsock =init_socket (qmp_socket_path); +qtest_client_set_rx_handler (s, qtest_client_socket_recv_line); +qtest_client_set_tx_handler (s, qtest_client_socket_send); +qtest_add_abrt_handler (kill_qemu_hook_func, s); command = g_strdup_printf("exec %s "
@@ -379,13 +400,9 @@ static void socket_send(int fd, const char *buf, size_t size) } } -static void socket_sendf (int fd,const char *fmt,va_list ap) +static void qtest_client_socket_send (QTestState *s,const char *buf) { -gchar *str =g_strdup_vprintf (fmt, ap); -size_t size =strlen (str); - -socket_send (fd, str, size); -g_free (str); +socket_send (s->fd, buf,strlen (buf)); } static void GCC_FMT_ATTR(2, 3) qtest_sendf(QTestState *s, const char *fmt, ...)
@@ -393,8 +410,11 @@ static void GCC_FMT_ATTR(2, 3) qtest_sendf(QTestState *s, const char *fmt, ...)va_list ap;va_start (ap, fmt); -socket_sendf (s->fd, fmt, ap); +gchar *str =g_strdup_vprintf (fmt, ap);va_end (ap); + + s->ops.send (s, str); +g_free (str); }/* Sends a message and file descriptors to the socket.
@@ -431,7 +451,7 @@ static void socket_send_fds(int socket_fd, int *fds, size_t fds_num, g_assert_cmpint(ret, >, 0); } -static GString *qtest_recv_line (QTestState *s) +static GString *qtest_client_socket_recv_line (QTestState *s) {GString *line;size_t offset;
@@ -468,7 +488,7 @@ static gchar **qtest_rsp(QTestState *s, int expected_args)int i; redo: - line =qtest_recv_line (s); + line = s->ops.recv_line (s); words =g_strsplit (line->str," " ,0 );g_string_free (line, TRUE);
@@ -1337,3 +1357,13 @@ void qmp_assert_error_class(QDict *rsp, const char *class)qobject_unref (rsp); } + +static void qtest_client_set_tx_handler (QTestState *s, +QTestSendFn send) +{ + s->ops.send = send; +} +static void qtest_client_set_rx_handler (QTestState *s,QTestRecvFn recv) +{ + s->ops.recv_line = recv; +}
Signature · 1 line
2.25.0
PATCH v9 8/23 libqtest: make bufwrite rely on the TransportOps
In reply to Alexander BulekovWhen using qtest "in-process" communication, qtest_sendf directly calls a function in the server (qtest.c). Previously, bufwrite used socket_send, which bypasses the TransportOps enabling the call into qtest.c. This change replaces the socket_send calls with ops->send, maintaining the benefits of the direct socket_send call, while adding support for in-process qtest calls.
--- tests/qtest/libqtest.c | 71 ++++++++++++++++++++++++++++++++++++++++-- tests/qtest/libqtest.h | 4 +++ 2 files changed, 73 insertions(+), 2 deletions(-)
tests/qtest/libqtest.c +69 -2
@@ -37,10 +37,18 @@typedef void (*QTestSendFn )(QTestState *s,const char *buf); +typedef void (*ExternalSendFn )(void *s,const char *buf);typedef GString * (*QTestRecvFn )(QTestState *);typedef struct QTestClientTransportOps {QTestSendFn send;/* for sending qtest commands */ + +/* +* use external_send to send qtest command strings through functions which +* do not accept a QTestState as the first parameter. +*/ +ExternalSendFn external_send; +QTestRecvFn recv_line;/* for receiving qtest command responses */ }QTestTransportOps ;
@@ -1078,8 +1086,8 @@ void qtest_bufwrite(QTestState *s, uint64_t addr, const void *data, size_t size) bdata =g_base64_encode (data, size);qtest_sendf (s,"b64write 0x%" PRIx64" 0x%zx " , addr, size); -socket_send (s->fd, bdata,strlen (bdata)); -socket_send (s->fd,"\n" ,1 ); + s->ops.send (s, bdata); + s->ops.send (s,"\n" );qtest_rsp (s,0 );g_free (bdata); }
@@ -1367,3 +1375,62 @@ static void qtest_client_set_rx_handler(QTestState *s, QTestRecvFn recv) { s->ops.recv_line = recv; } +/* A type-safe wrapper for s->send() */ +static void send_wrapper (QTestState *s,const char *buf) +{ + s->ops.external_send (s, buf); +} + +static GString *qtest_client_inproc_recv_line (QTestState *s) +{ +GString *line; +size_t offset; +char *eol; + + eol =strchr (s->rx->str,'\n' ); + offset = eol - s->rx->str; + line =g_string_new_len (s->rx->str, offset); +g_string_erase (s->rx,0 , offset +1 ); +return line; +} + +QTestState *qtest_inproc_init (QTestState **s,bool log,const char * arch, +void (*send)(void *,const char *)) +{ +QTestState *qts; + qts =g_new0 (QTestState,1 ); + *s = qts;/* Expose qts early on, since the query endianness relies on it */ + qts->wstatus =0 ; +for (int i =0 ; i <MAX_IRQ ; i++) { + qts->irq_level[i] = false; + } + +qtest_client_set_rx_handler (qts, qtest_client_inproc_recv_line); + +/* send() may not have a matching protoype, so use a type-safe wrapper */ + qts->ops.external_send = send; +qtest_client_set_tx_handler (qts, send_wrapper); + + qts->big_endian =qtest_query_target_endianness (qts); + +/* +* Set a dummy path for QTEST_QEMU_BINARY. Doesn't need to exist, but this +* way, qtest_get_arch works for inproc qtest. +*/ +gchar *bin_path =g_strconcat ("/qemu-system-" , arch,NULL ); +setenv ("QTEST_QEMU_BINARY" , bin_path,0 ); +g_free (bin_path); + +return qts; +} + +void qtest_client_inproc_recv (void *opaque,const char *str) +{ +QTestState *qts = *(QTestState **)opaque; + +if (!qts->rx) { + qts->rx =g_string_new (NULL ); + } +g_string_append (qts->rx, str); +return ; +}
tests/qtest/libqtest.h +4 -0
@@ -729,4 +729,8 @@ bool qtest_probe_child(QTestState *s);*/ void qtest_set_expected_status (QTestState *s,int status); +QTestState *qtest_inproc_init (QTestState **s,bool log,const char * arch, +void (*send)(void *,const char *)); + +void qtest_client_inproc_recv (void *opaque,const char *str);#endif
Signature · 1 line
2.25.0
PATCH v9 9/23 qtest: add in-process incoming command handler
In reply to Alexander BulekovThe handler allows a qtest client to send commands to the server by directly calling a function, rather than using a file/CharBackend
--- include/sysemu/qtest.h | 1 + qtest.c | 13 +++++++++++++ 2 files changed, 14 insertions(+)
include/sysemu/qtest.h +1 -0
@@ -28,5 +28,6 @@ void qtest_server_init(const char *qtest_chrdev, const char *qtest_log, Error **void qtest_server_set_send_handler (void (*send)(void *,const char *),void *opaque); +void qtest_server_inproc_recv (void *opaque,const char *buf);#endif
qtest.c +13 -0
@@ -803,3 +803,16 @@ bool qtest_driver(void) {return qtest_chr.chr !=NULL ; } + +void qtest_server_inproc_recv (void *dummy,const char *buf) +{ +static GString *gstr; +if (!gstr) { + gstr =g_string_new (NULL ); + } +g_string_append (gstr, buf); +if (gstr->str[gstr->len -1 ] =='\n' ) { +qtest_process_inbuf (NULL , gstr); +g_string_truncate (gstr,0 ); + } +}
Signature · 1 line
2.25.0
PATCH v9 10/23 libqos: rename i2c_send and i2c_recv
In reply to Alexander BulekovThe names i2c_send and i2c_recv collide with functions defined in hw/i2c/core.c. This causes an error when linking against libqos and softmmu simultaneously (for example when using qtest inproc). Rename the libqos functions to avoid this.
--- tests/qtest/libqos/i2c.c | 10 +++++----- tests/qtest/libqos/i2c.h | 4 ++-- tests/qtest/pca9552-test.c | 10 +++++----- 3 files changed, 12 insertions(+), 12 deletions(-)
tests/qtest/libqos/i2c.c +5 -5
@@ -10,12 +10,12 @@#include "libqos/i2c.h" #include "libqtest.h" -void i2c_send (QI2CDevice *i2cdev,const uint8_t *buf,uint16_t len) +void qi2c_send (QI2CDevice *i2cdev,const uint8_t *buf,uint16_t len) { i2cdev->bus->send (i2cdev->bus, i2cdev->addr, buf, len); } -void i2c_recv (QI2CDevice *i2cdev,uint8_t *buf,uint16_t len) +void qi2c_recv (QI2CDevice *i2cdev,uint8_t *buf,uint16_t len) { i2cdev->bus->recv (i2cdev->bus, i2cdev->addr, buf, len); }
@@ -23,8 +23,8 @@ void i2c_recv(QI2CDevice *i2cdev, uint8_t *buf, uint16_t len)void i2c_read_block (QI2CDevice *i2cdev,uint8_t reg,uint8_t *buf,uint16_t len) { -i2c_send (i2cdev, ®,1 ); -i2c_recv (i2cdev, buf, len); +qi2c_send (i2cdev, ®,1 ); +qi2c_recv (i2cdev, buf, len); } void i2c_write_block(QI2CDevice *i2cdev, uint8_t reg,
@@ -33,7 +33,7 @@ void i2c_write_block(QI2CDevice *i2cdev, uint8_t reg,uint8_t *cmd =g_malloc (len +1 ); cmd[0 ] = reg;memcpy (&cmd[1 ], buf, len); -i2c_send (i2cdev, cmd, len +1 ); +qi2c_send (i2cdev, cmd, len +1 );g_free (cmd); }
tests/qtest/libqos/i2c.h +2 -2
@@ -47,8 +47,8 @@ struct QI2CDevice {void *i2c_device_create (void *i2c_bus,QGuestAllocator *alloc,void *addr);void add_qi2c_address (QOSGraphEdgeOptions *opts,QI2CAddress *addr); -void i2c_send (QI2CDevice *dev,const uint8_t *buf,uint16_t len); -void i2c_recv (QI2CDevice *dev,uint8_t *buf,uint16_t len); +void qi2c_send (QI2CDevice *dev,const uint8_t *buf,uint16_t len); +void qi2c_recv (QI2CDevice *dev,uint8_t *buf,uint16_t len);void i2c_read_block (QI2CDevice *dev,uint8_t reg,uint8_t *buf,uint16_t len);
tests/qtest/pca9552-test.c +5 -5
@@ -32,22 +32,22 @@ static void receive_autoinc(void *obj, void *data, QGuestAllocator *alloc)pca9552_init (i2cdev); -i2c_send (i2cdev, ®,1 ); +qi2c_send (i2cdev, ®,1 );/* PCA9552_LS0 */ -i2c_recv (i2cdev, &resp,1 ); +qi2c_recv (i2cdev, &resp,1 ); g_assert_cmphex(resp, ==, 0x54);/* PCA9552_LS1 */ -i2c_recv (i2cdev, &resp,1 ); +qi2c_recv (i2cdev, &resp,1 ); g_assert_cmphex(resp, ==, 0x55);/* PCA9552_LS2 */ -i2c_recv (i2cdev, &resp,1 ); +qi2c_recv (i2cdev, &resp,1 ); g_assert_cmphex(resp, ==, 0x55);/* PCA9552_LS3 */ -i2c_recv (i2cdev, &resp,1 ); +qi2c_recv (i2cdev, &resp,1 ); g_assert_cmphex(resp, ==, 0x54); }
Signature · 1 line
2.25.0
PATCH v9 11/23 libqos: split qos-test and libqos makefile vars
In reply to Alexander BulekovMost qos-related objects were specified in the qos-test-obj-y variable. qos-test-obj-y also included qos-test.o which defines a main(). This made it difficult to repurpose qos-test-obj-y to link anything beside tests/qos-test against libqos. This change separates objects that are libqos-specific and ones that are qos-test specific into different variables.
--- tests/qtest/Makefile.include | 71 ++++++++++++++++++------------------ 1 file changed, 36 insertions(+), 35 deletions(-)
tests/qtest/Makefile.include +36 -35
@@ -157,52 +157,53 @@ check-qtest-s390x-y += migration-test # libqos / qgraph : libqgraph-obj-y = tests/qtest/libqos/qgraph.o -libqos-obj-y = $(libqgraph-obj-y) tests/qtest/libqos/pci.o tests/qtest/libqos/fw_cfg.o -libqos-obj-y += tests/qtest/libqos/malloc.o -libqos-obj-y += tests/qtest/libqos/libqos.o -libqos-spapr-obj-y = $(libqos-obj-y) tests/qtest/libqos/malloc-spapr.o +libqos-core-obj-y = $(libqgraph-obj-y) tests/qtest/libqos/pci.o tests/qtest/libqos/fw_cfg.o +libqos-core-obj-y += tests/qtest/libqos/malloc.o +libqos-core-obj-y += tests/qtest/libqos/libqos.o +libqos-spapr-obj-y = $(libqos-core-obj-y) tests/qtest/libqos/malloc-spapr.o libqos-spapr-obj-y += tests/qtest/libqos/libqos-spapr.o libqos-spapr-obj-y += tests/qtest/libqos/rtas.o libqos-spapr-obj-y += tests/qtest/libqos/pci-spapr.o -libqos-pc-obj-y = $(libqos-obj-y) tests/qtest/libqos/pci-pc.o +libqos-pc-obj-y = $(libqos-core-obj-y) tests/qtest/libqos/pci-pc.o libqos-pc-obj-y += tests/qtest/libqos/malloc-pc.o tests/qtest/libqos/libqos-pc.o libqos-pc-obj-y += tests/qtest/libqos/ahci.o libqos-usb-obj-y = $(libqos-spapr-obj-y) $(libqos-pc-obj-y) tests/qtest/libqos/usb.o # qos devices: -qos-test-obj-y = tests/qtest/qos-test.o $(libqgraph-obj-y) -qos-test-obj-y += $(libqos-pc-obj-y) $(libqos-spapr-obj-y) -qos-test-obj-y += tests/qtest/libqos/e1000e.o -qos-test-obj-y += tests/qtest/libqos/i2c.o -qos-test-obj-y += tests/qtest/libqos/i2c-imx.o -qos-test-obj-y += tests/qtest/libqos/i2c-omap.o -qos-test-obj-y += tests/qtest/libqos/sdhci.o -qos-test-obj-y += tests/qtest/libqos/tpci200.o -qos-test-obj-y += tests/qtest/libqos/virtio.o -qos-test-obj-$(CONFIG_VIRTFS) += tests/qtest/libqos/virtio-9p.o -qos-test-obj-y += tests/qtest/libqos/virtio-balloon.o -qos-test-obj-y += tests/qtest/libqos/virtio-blk.o -qos-test-obj-y += tests/qtest/libqos/virtio-mmio.o -qos-test-obj-y += tests/qtest/libqos/virtio-net.o -qos-test-obj-y += tests/qtest/libqos/virtio-pci.o -qos-test-obj-y += tests/qtest/libqos/virtio-pci-modern.o -qos-test-obj-y += tests/qtest/libqos/virtio-rng.o -qos-test-obj-y += tests/qtest/libqos/virtio-scsi.o -qos-test-obj-y += tests/qtest/libqos/virtio-serial.o +libqos-obj-y = $(libqgraph-obj-y) +libqos-obj-y += $(libqos-pc-obj-y) $(libqos-spapr-obj-y) +libqos-obj-y += tests/qtest/libqos/e1000e.o +libqos-obj-y += tests/qtest/libqos/i2c.o +libqos-obj-y += tests/qtest/libqos/i2c-imx.o +libqos-obj-y += tests/qtest/libqos/i2c-omap.o +libqos-obj-y += tests/qtest/libqos/sdhci.o +libqos-obj-y += tests/qtest/libqos/tpci200.o +libqos-obj-y += tests/qtest/libqos/virtio.o +libqos-obj-$(CONFIG_VIRTFS) += tests/qtest/libqos/virtio-9p.o +libqos-obj-y += tests/qtest/libqos/virtio-balloon.o +libqos-obj-y += tests/qtest/libqos/virtio-blk.o +libqos-obj-y += tests/qtest/libqos/virtio-mmio.o +libqos-obj-y += tests/qtest/libqos/virtio-net.o +libqos-obj-y += tests/qtest/libqos/virtio-pci.o +libqos-obj-y += tests/qtest/libqos/virtio-pci-modern.o +libqos-obj-y += tests/qtest/libqos/virtio-rng.o +libqos-obj-y += tests/qtest/libqos/virtio-scsi.o +libqos-obj-y += tests/qtest/libqos/virtio-serial.o # qos machines: -qos-test-obj-y += tests/qtest/libqos/aarch64-xlnx-zcu102-machine.o -qos-test-obj-y += tests/qtest/libqos/arm-imx25-pdk-machine.o -qos-test-obj-y += tests/qtest/libqos/arm-n800-machine.o -qos-test-obj-y += tests/qtest/libqos/arm-raspi2-machine.o -qos-test-obj-y += tests/qtest/libqos/arm-sabrelite-machine.o -qos-test-obj-y += tests/qtest/libqos/arm-smdkc210-machine.o -qos-test-obj-y += tests/qtest/libqos/arm-virt-machine.o -qos-test-obj-y += tests/qtest/libqos/arm-xilinx-zynq-a9-machine.o -qos-test-obj-y += tests/qtest/libqos/ppc64_pseries-machine.o -qos-test-obj-y += tests/qtest/libqos/x86_64_pc-machine.o +libqos-obj-y += tests/qtest/libqos/aarch64-xlnx-zcu102-machine.o +libqos-obj-y += tests/qtest/libqos/arm-imx25-pdk-machine.o +libqos-obj-y += tests/qtest/libqos/arm-n800-machine.o +libqos-obj-y += tests/qtest/libqos/arm-raspi2-machine.o +libqos-obj-y += tests/qtest/libqos/arm-sabrelite-machine.o +libqos-obj-y += tests/qtest/libqos/arm-smdkc210-machine.o +libqos-obj-y += tests/qtest/libqos/arm-virt-machine.o +libqos-obj-y += tests/qtest/libqos/arm-xilinx-zynq-a9-machine.o +libqos-obj-y += tests/qtest/libqos/ppc64_pseries-machine.o +libqos-obj-y += tests/qtest/libqos/x86_64_pc-machine.o # qos tests: +qos-test-obj-y += tests/qtest/qos-test.o qos-test-obj-y += tests/qtest/ac97-test.o qos-test-obj-y += tests/qtest/ds1338-test.o qos-test-obj-y += tests/qtest/e1000-test.o
@@ -234,7 +235,7 @@ check-unit-y += tests/test-qgraph$(EXESUF) tests/test-qgraph$(EXESUF): tests/test-qgraph.o $(libqgraph-obj-y) check-qtest-generic-y += qos-test -tests/qtest/qos-test$(EXESUF): $(qos-test-obj-y) +tests/qtest/qos-test$(EXESUF): $(qos-test-obj-y) $(libqos-obj-y) # QTest dependencies: tests/qtest/qmp-test$(EXESUF): tests/qtest/qmp-test.o
Signature · 1 line
2.25.0
PATCH v9 12/23 libqos: move useful qos-test funcs to qos_external
In reply to Alexander BulekovThe moved functions are not specific to qos-test and might be useful elsewhere. For example the virtual-device fuzzer makes use of them for qos-assisted fuzz-targets.
--- tests/qtest/Makefile.include | 1 + tests/qtest/libqos/qos_external.c | 168 ++++++++++++++++++++++++++++++ tests/qtest/libqos/qos_external.h | 28 +++++ tests/qtest/qos-test.c | 132 +---------------------- 4 files changed, 198 insertions(+), 131 deletions(-) create mode 100644 tests/qtest/libqos/qos_external.c create mode 100644 tests/qtest/libqos/qos_external.h
tests/qtest/Makefile.include +1 -0
@@ -172,6 +172,7 @@ libqos-usb-obj-y = $(libqos-spapr-obj-y) $(libqos-pc-obj-y) tests/qtest/libqos/u # qos devices: libqos-obj-y = $(libqgraph-obj-y) libqos-obj-y += $(libqos-pc-obj-y) $(libqos-spapr-obj-y) +libqos-obj-y += tests/qtest/libqos/qos_external.o libqos-obj-y += tests/qtest/libqos/e1000e.o libqos-obj-y += tests/qtest/libqos/i2c.o libqos-obj-y += tests/qtest/libqos/i2c-imx.o
tests/qtest/libqos/qos_external.c new file +168 -0
168 lines not on this page · raw message
tests/qtest/libqos/qos_external.h new file +28 -0
@@ -0,0 +1,28 @@ +/* +* libqos driver framework +* +* Copyright (c) 2018 Emanuele Giuseppe Esposito <[email protected]> +* +* This library is free software; you can redistribute it and/or +* modify it under the terms of the GNU Lesser General Public +* License version 2 as published by the Free Software Foundation. +* +* This library is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +* Lesser General Public License for more details. +* +* You should have received a copy of the GNU Lesser General Public +* License along with this library; if not, see <http://www.gnu.org/licenses/> +*/ + +#ifndef QOS_EXTERNAL_H +#define QOS_EXTERNAL_H +#include "libqos/qgraph.h" + +void apply_to_node (const char *name,bool is_machine,bool is_abstract); +void apply_to_qlist (QList *list,bool is_machine); +QGuestAllocator *get_machine_allocator (QOSGraphObject *obj); +void *allocate_objects (QTestState *qts,char **path,QGuestAllocator **p_alloc); + +#endif
tests/qtest/qos-test.c +1 -131
154 lines not on this page · raw message
Signature · 1 line
2.25.0
PATCH v9 13/23 fuzz: add fuzzer skeleton
In reply to Alexander Bulekovtests/fuzz/fuzz.c serves as the entry point for the virtual-device fuzzer. Namely, libfuzzer invokes the LLVMFuzzerInitialize and LLVMFuzzerTestOneInput functions, both of which are defined in this file. This change adds a "FuzzTarget" struct, along with the fuzz_add_target function, which should be used to define new fuzz targets.
--- tests/qtest/fuzz/Makefile.include | 6 + tests/qtest/fuzz/fuzz.c | 179 ++++++++++++++++++++++++++++++ tests/qtest/fuzz/fuzz.h | 95 ++++++++++++++++ 3 files changed, 280 insertions(+) create mode 100644 tests/qtest/fuzz/Makefile.include create mode 100644 tests/qtest/fuzz/fuzz.c create mode 100644 tests/qtest/fuzz/fuzz.h
tests/qtest/fuzz/Makefile.include new file +6 -0
@@ -0,0 +1,6 @@ +QEMU_PROG_FUZZ=qemu-fuzz-$(TARGET_NAME)$(EXESUF) + +fuzz-obj-y += tests/qtest/libqtest.o +fuzz-obj-y += tests/qtest/fuzz/fuzz.o # Fuzzer skeleton + +FUZZ_CFLAGS += -I$(SRC_PATH)/tests -I$(SRC_PATH)/tests/qtest
tests/qtest/fuzz/fuzz.c new file +179 -0
179 lines not on this page · raw message
tests/qtest/fuzz/fuzz.h new file +95 -0
95 lines not on this page · raw message
Signature · 1 line
2.25.0
PATCH v9 14/23 exec: keep ram block across fork when using qtest
In reply to Alexander BulekovRam blocks were marked MADV_DONTFORK breaking fuzzing-tests which execute each test-input in a forked process.
--- exec.c | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-)
exec.c +10 -2
@@ -35,6 +35,7 @@#include "sysemu/kvm.h" #include "sysemu/sysemu.h" #include "sysemu/tcg.h" +#include "sysemu/qtest.h" #include "qemu/timer.h" #include "qemu/config-file.h" #include "qemu/error-report.h"
@@ -2306,8 +2307,15 @@ static void ram_block_add(RAMBlock *new_block, Error **errp, bool shared)if (new_block->host) {qemu_ram_setup_dump (new_block->host, new_block->max_length);qemu_madvise (new_block->host, new_block->max_length,QEMU_MADV_HUGEPAGE ); -/* MADV_DONTFORK is also needed by KVM in absence of synchronous MMU */ -qemu_madvise (new_block->host, new_block->max_length,QEMU_MADV_DONTFORK ); +/* +* MADV_DONTFORK is also needed by KVM in absence of synchronous MMU +* Configure it unless the machine is a qtest server, in which case +* KVM is not used and it may be forked (eg for fuzzing purposes). +*/ +if (!qtest_enabled ()) { +qemu_madvise (new_block->host, new_block->max_length, +QEMU_MADV_DONTFORK ); + }ram_block_notify_add (new_block->host, new_block->max_length); } }
Signature · 1 line
2.25.0
PATCH v9 15/23 main: keep rcu_atfork callback enabled for qtest
In reply to Alexander BulekovThe qtest-based fuzzer makes use of forking to reset-state between tests. Keep the callback enabled, so the call_rcu thread gets created within the child process.
--- softmmu/vl.c | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-)
softmmu/vl.c +11 -1
@@ -3813,7 +3813,17 @@ void qemu_init(int argc, char **argv, char **envp)set_memory_options (&ram_slots, &maxram_size, machine_class);os_daemonize (); -rcu_disable_atfork (); + +/* +* If QTest is enabled, keep the rcu_atfork enabled, since system processes +* may be forked testing purposes (e.g. fork-server based fuzzing) The fork +* should happen before a signle cpu instruction is executed, to prevent +* deadlocks. See commit 73c6e40, rcu: "completely disable pthread_atfork +* callbacks as soon as possible" +*/ +if (!qtest_enabled ()) { +rcu_disable_atfork (); + }if (pid_file && !qemu_write_pidfile (pid_file, &err)) {error_reportf_err (err,"cannot create PID file: " );
Signature · 1 line
2.25.0
PATCH v9 16/23 fuzz: support for fork-based fuzzing.
In reply to Alexander Bulekovfork() is a simple way to ensure that state does not leak in between fuzzing runs. Unfortunately, the fuzzer mutation engine relies on bitmaps which contain coverage information for each fuzzing run, and these bitmaps should be copied from the child to the parent(where the mutation occurs). These bitmaps are created through compile-time instrumentation and they are not shared with fork()-ed processes, by default. To address this, we create a shared memory region, adjust its size and map it _over_ the counter region. Furthermore, libfuzzer doesn't generally expose the globals that specify the location of the counters/coverage bitmap. As a workaround, we rely on a custom linker script which forces all of the bitmaps we care about to be placed in a contiguous region, which is easy to locate and mmap over.
--- tests/qtest/fuzz/Makefile.include | 5 +++ tests/qtest/fuzz/fork_fuzz.c | 55 +++++++++++++++++++++++++++++++ tests/qtest/fuzz/fork_fuzz.h | 23 +++++++++++++ tests/qtest/fuzz/fork_fuzz.ld | 37 +++++++++++++++++++++ 4 files changed, 120 insertions(+) create mode 100644 tests/qtest/fuzz/fork_fuzz.c create mode 100644 tests/qtest/fuzz/fork_fuzz.h create mode 100644 tests/qtest/fuzz/fork_fuzz.ld
tests/qtest/fuzz/Makefile.include +5 -0
@@ -2,5 +2,10 @@ QEMU_PROG_FUZZ=qemu-fuzz-$(TARGET_NAME)$(EXESUF) fuzz-obj-y += tests/qtest/libqtest.o fuzz-obj-y += tests/qtest/fuzz/fuzz.o # Fuzzer skeleton +fuzz-obj-y += tests/qtest/fuzz/fork_fuzz.o FUZZ_CFLAGS += -I$(SRC_PATH)/tests -I$(SRC_PATH)/tests/qtest + +# Linker Script to force coverage-counters into known regions which we can mark +# shared +FUZZ_LDFLAGS += -Xlinker -T$(SRC_PATH)/tests/qtest/fuzz/fork_fuzz.ld
tests/qtest/fuzz/fork_fuzz.c new file +55 -0
55 lines not on this page · raw message
tests/qtest/fuzz/fork_fuzz.h new file +23 -0
@@ -0,0 +1,23 @@ +/* +* Fork-based fuzzing helpers +* +* Copyright Red Hat Inc., 2019 +* +* Authors: +* Alexander Bulekov <[email protected]> +* +* This work is licensed under the terms of the GNU GPL, version 2 or later. +* See the COPYING file in the top-level directory. +* +*/ + +#ifndef FORK_FUZZ_H +#define FORK_FUZZ_H + +extern uint8_t __FUZZ_COUNTERS_START; +extern uint8_t __FUZZ_COUNTERS_END; + +void counter_shm_init (void ); + +#endif +
tests/qtest/fuzz/fork_fuzz.ld new file +37 -0
@@ -0,0 +1,37 @@ +/* We adjust linker script modification to place all of the stuff that needs to + * persist across fuzzing runs into a contiguous seciton of memory. Then, it is + * easy to re-map the counter-related memory as shared. +*/ + +SECTIONS +{ + .data.fuzz_start : ALIGN(4K) + { + __FUZZ_COUNTERS_START = .; + __start___sancov_cntrs = .; + *(_*sancov_cntrs); + __stop___sancov_cntrs = .; + + /* Lowest stack counter */ + *(__sancov_lowest_stack); + } + .data.fuzz_ordered : + { + /* Coverage counters. They're not necessary for fuzzing, but are useful + * for analyzing the fuzzing performance + */ + __start___llvm_prf_cnts = .; + *(*llvm_prf_cnts); + __stop___llvm_prf_cnts = .; + + /* Internal Libfuzzer TracePC object which contains the ValueProfileMap */ + FuzzerTracePC*(.bss*); + } + .data.fuzz_end : ALIGN(4K) + { + __FUZZ_COUNTERS_END = .; + } +} +/* Dont overwrite the SECTIONS in the default linker script. Instead insert the + * above into the default script */ +INSERT AFTER .data;
Signature · 1 line
2.25.0
PATCH v9 17/23 fuzz: add support for qos-assisted fuzz targets
In reply to Alexander Bulekov--- tests/qtest/fuzz/Makefile.include | 2 + tests/qtest/fuzz/qos_fuzz.c | 234 ++++++++++++++++++++++++++++++ tests/qtest/fuzz/qos_fuzz.h | 33 +++++ 3 files changed, 269 insertions(+) create mode 100644 tests/qtest/fuzz/qos_fuzz.c create mode 100644 tests/qtest/fuzz/qos_fuzz.h
tests/qtest/fuzz/Makefile.include +2 -0
@@ -1,8 +1,10 @@ QEMU_PROG_FUZZ=qemu-fuzz-$(TARGET_NAME)$(EXESUF) fuzz-obj-y += tests/qtest/libqtest.o +fuzz-obj-y += $(libqos-obj-y) fuzz-obj-y += tests/qtest/fuzz/fuzz.o # Fuzzer skeleton fuzz-obj-y += tests/qtest/fuzz/fork_fuzz.o +fuzz-obj-y += tests/qtest/fuzz/qos_fuzz.o FUZZ_CFLAGS += -I$(SRC_PATH)/tests -I$(SRC_PATH)/tests/qtest
tests/qtest/fuzz/qos_fuzz.c new file +234 -0
234 lines not on this page · raw message
tests/qtest/fuzz/qos_fuzz.h new file +33 -0
@@ -0,0 +1,33 @@ +/* +* QOS-assisted fuzzing helpers +* +* Copyright Red Hat Inc., 2019 +* +* Authors: +* Alexander Bulekov <[email protected]> +* +* This work is licensed under the terms of the GNU GPL, version 2 or later. +* See the COPYING file in the top-level directory. +*/ + +#ifndef _QOS_FUZZ_H_ +#define _QOS_FUZZ_H_ + +#include "tests/qtest/fuzz/fuzz.h" +#include "tests/qtest/libqos/qgraph.h" + +int qos_fuzz (const unsigned char *Data,size_t Size); +void qos_setup (void ); + +extern void *fuzz_qos_obj; +extern QGuestAllocator *fuzz_qos_alloc; + +void fuzz_add_qos_target ( +FuzzTarget *fuzz_opts, +const char *interface, +QOSGraphTestOptions *opts + ); + +void qos_init_path (QTestState *); + +#endif
Signature · 1 line
2.25.0
PATCH v9 18/23 fuzz: add target/fuzz makefile rules
In reply to Alexander Bulekov--- Makefile | 15 ++++++++++++++- Makefile.target | 16 ++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-)
Makefile +14 -1
@@ -477,7 +477,7 @@ config-host.h-timestamp: config-host.mak qemu-options.def: $(SRC_PATH)/qemu-options.hx $(SRC_PATH)/scripts/hxtool $(call quiet-command,sh $(SRC_PATH)/scripts/hxtool -h < $< > $@,"GEN","$@") -TARGET_DIRS_RULES := $(foreach t, all clean install, $(addsuffix /$(t), $(TARGET_DIRS))) +TARGET_DIRS_RULES := $(foreach t, all fuzz clean install, $(addsuffix /$(t), $(TARGET_DIRS))) SOFTMMU_ALL_RULES=$(filter %-softmmu/all, $(TARGET_DIRS_RULES)) $(SOFTMMU_ALL_RULES): $(authz-obj-y)
@@ -490,6 +490,15 @@ ifdef DECOMPRESS_EDK2_BLOBS $(SOFTMMU_ALL_RULES): $(edk2-decompressed) endif +SOFTMMU_FUZZ_RULES=$(filter %-softmmu/fuzz, $(TARGET_DIRS_RULES)) +$(SOFTMMU_FUZZ_RULES): $(authz-obj-y) +$(SOFTMMU_FUZZ_RULES): $(block-obj-y) +$(SOFTMMU_FUZZ_RULES): $(chardev-obj-y) +$(SOFTMMU_FUZZ_RULES): $(crypto-obj-y) +$(SOFTMMU_FUZZ_RULES): $(io-obj-y) +$(SOFTMMU_FUZZ_RULES): config-all-devices.mak +$(SOFTMMU_FUZZ_RULES): $(edk2-decompressed) + .PHONY: $(TARGET_DIRS_RULES) # The $(TARGET_DIRS_RULES) are of the form SUBDIR/GOAL, so that # $(dir $@) yields the sub-directory, and $(notdir $@) yields the sub-goal
@@ -540,6 +549,9 @@ subdir-slirp: slirp/all $(filter %/all, $(TARGET_DIRS_RULES)): libqemuutil.a $(common-obj-y) \ $(qom-obj-y) +$(filter %/fuzz, $(TARGET_DIRS_RULES)): libqemuutil.a $(common-obj-y) \ + $(qom-obj-y) $(crypto-user-obj-$(CONFIG_USER_ONLY)) + ROM_DIRS = $(addprefix pc-bios/, $(ROMS)) ROM_DIRS_RULES=$(foreach t, all clean, $(addsuffix /$(t), $(ROM_DIRS))) # Only keep -O and -g cflags
@@ -549,6 +561,7 @@ $(ROM_DIRS_RULES): .PHONY: recurse-all recurse-clean recurse-install recurse-all: $(addsuffix /all, $(TARGET_DIRS) $(ROM_DIRS)) +recurse-fuzz: $(addsuffix /fuzz, $(TARGET_DIRS) $(ROM_DIRS)) recurse-clean: $(addsuffix /clean, $(TARGET_DIRS) $(ROM_DIRS)) recurse-install: $(addsuffix /install, $(TARGET_DIRS)) $(addsuffix /install, $(TARGET_DIRS)): all
Makefile.target +16 -0
@@ -228,6 +228,22 @@ ifdef CONFIG_TRACE_SYSTEMTAP rm -f *.stp endif +ifdef CONFIG_FUZZ +include $(SRC_PATH)/tests/qtest/fuzz/Makefile.include +include $(SRC_PATH)/tests/qtest/Makefile.include + +fuzz: fuzz-vars +fuzz-vars: QEMU_CFLAGS := $(FUZZ_CFLAGS) $(QEMU_CFLAGS) +fuzz-vars: QEMU_LDFLAGS := $(FUZZ_LDFLAGS) $(QEMU_LDFLAGS) +fuzz-vars: $(QEMU_PROG_FUZZ) +dummy := $(call unnest-vars,, fuzz-obj-y) + + +$(QEMU_PROG_FUZZ): config-devices.mak $(all-obj-y) $(COMMON_LDADDS) $(fuzz-obj-y) + $(call LINK, $(filter-out %.mak, $^)) + +endif + install: all ifneq ($(PROGS),) $(call install-prog,$(PROGS),$(DESTDIR)$(bindir))
Signature · 1 line
2.25.0
PATCH v9 19/23 fuzz: add configure flag --enable-fuzzing
In reply to Alexander Bulekov--- configure | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+)
configure +39 -0
@@ -505,6 +505,7 @@ debug_mutex="no" libpmem="" default_devices="yes" plugins="no" +fuzzing="no" supported_cpu="no" supported_os="no"
@@ -635,6 +636,15 @@ int main(void) { return 0; } EOF } +write_c_fuzzer_skeleton() { + cat > $TMPC <<EOF +#include <stdint.h> +#include <sys/types.h> +int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size); +int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) { return 0; } +EOF +} + if check_define __linux__ ; then targetos="Linux" elif check_define _WIN32 ; then
@@ -1545,6 +1555,10 @@ for opt do ;; --disable-containers) use_containers="no" ;; + --enable-fuzzing) fuzzing=yes + ;; + --disable-fuzzing) fuzzing=no + ;; *) echo "ERROR: unknown option $opt" echo "Try '$0 --help' for more information"
@@ -6035,6 +6049,15 @@ EOF fi fi +########################################## +# checks for fuzzer +if test "$fuzzing" = "yes" ; then + write_c_fuzzer_skeleton + if compile_prog "$CPU_CFLAGS -Werror -fsanitize=address,fuzzer" ""; then + have_fuzzer=yes + fi +fi + ########################################## # check for libpmem
@@ -6621,6 +6644,7 @@ echo "libpmem support $libpmem" echo "libudev $libudev" echo "default devices $default_devices" echo "plugin support $plugins" +echo "fuzzing support $fuzzing" if test "$supported_cpu" = "no"; then echo
@@ -7456,6 +7480,16 @@ fi if test "$sheepdog" = "yes" ; then echo "CONFIG_SHEEPDOG=y" >> $config_host_mak fi +if test "$fuzzing" = "yes" ; then + if test "$have_fuzzer" = "yes"; then + FUZZ_LDFLAGS=" -fsanitize=address,fuzzer" + FUZZ_CFLAGS=" -fsanitize=address,fuzzer" + CFLAGS=" -fsanitize=address,fuzzer-no-link" + else + error_exit "Your compiler doesn't support -fsanitize=address,fuzzer" + exit 1 + fi +fi if test "$plugins" = "yes" ; then echo "CONFIG_PLUGIN=y" >> $config_host_mak
@@ -7556,6 +7590,11 @@ if test "$libudev" != "no"; then echo "CONFIG_LIBUDEV=y" >> $config_host_mak echo "LIBUDEV_LIBS=$libudev_libs" >> $config_host_mak fi +if test "$fuzzing" != "no"; then + echo "CONFIG_FUZZ=y" >> $config_host_mak + echo "FUZZ_CFLAGS=$FUZZ_CFLAGS" >> $config_host_mak + echo "FUZZ_LDFLAGS=$FUZZ_LDFLAGS" >> $config_host_mak +fi if test "$edk2_blobs" = "yes" ; then echo "DECOMPRESS_EDK2_BLOBS=y" >> $config_host_mak
Signature · 1 line
2.25.0
PATCH v9 20/23 fuzz: add i440fx fuzz targets
In reply to Alexander BulekovThese three targets should simply fuzz reads/writes to a couple ioports, but they mostly serve as examples of different ways to write targets. They demonstrate using qtest and qos for fuzzing, as well as using rebooting and forking to reset state, or not resetting it at all.
--- tests/qtest/fuzz/Makefile.include | 3 + tests/qtest/fuzz/i440fx_fuzz.c | 193 ++++++++++++++++++++++++++++++ 2 files changed, 196 insertions(+) create mode 100644 tests/qtest/fuzz/i440fx_fuzz.c
tests/qtest/fuzz/Makefile.include +3 -0
@@ -6,6 +6,9 @@ fuzz-obj-y += tests/qtest/fuzz/fuzz.o # Fuzzer skeleton fuzz-obj-y += tests/qtest/fuzz/fork_fuzz.o fuzz-obj-y += tests/qtest/fuzz/qos_fuzz.o +# Targets +fuzz-obj-y += tests/qtest/fuzz/i440fx_fuzz.o + FUZZ_CFLAGS += -I$(SRC_PATH)/tests -I$(SRC_PATH)/tests/qtest # Linker Script to force coverage-counters into known regions which we can mark
tests/qtest/fuzz/i440fx_fuzz.c new file +193 -0
193 lines not on this page · raw message
Signature · 1 line
2.25.0
PATCH v9 21/23 fuzz: add virtio-net fuzz target
In reply to Alexander BulekovThe virtio-net fuzz target feeds inputs to all three virtio-net virtqueues, and uses forking to avoid leaking state between fuzz runs.
--- tests/qtest/fuzz/Makefile.include | 1 + tests/qtest/fuzz/virtio_net_fuzz.c | 198 +++++++++++++++++++++++++++++ 2 files changed, 199 insertions(+) create mode 100644 tests/qtest/fuzz/virtio_net_fuzz.c
tests/qtest/fuzz/Makefile.include +1 -0
@@ -8,6 +8,7 @@ fuzz-obj-y += tests/qtest/fuzz/qos_fuzz.o # Targets fuzz-obj-y += tests/qtest/fuzz/i440fx_fuzz.o +fuzz-obj-y += tests/qtest/fuzz/virtio_net_fuzz.o FUZZ_CFLAGS += -I$(SRC_PATH)/tests -I$(SRC_PATH)/tests/qtest
tests/qtest/fuzz/virtio_net_fuzz.c new file +198 -0
198 lines not on this page · raw message
Signature · 1 line
2.25.0
PATCH v9 22/23 fuzz: add virtio-scsi fuzz target
In reply to Alexander BulekovThe virtio-scsi fuzz target sets up and fuzzes the available virtio-scsi queues. After an element is placed on a queue, the fuzzer can select whether to perform a kick, or continue adding elements.
--- tests/qtest/fuzz/Makefile.include | 1 + tests/qtest/fuzz/virtio_scsi_fuzz.c | 214 ++++++++++++++++++++++++++++ 2 files changed, 215 insertions(+) create mode 100644 tests/qtest/fuzz/virtio_scsi_fuzz.c
tests/qtest/fuzz/Makefile.include +1 -0
@@ -9,6 +9,7 @@ fuzz-obj-y += tests/qtest/fuzz/qos_fuzz.o # Targets fuzz-obj-y += tests/qtest/fuzz/i440fx_fuzz.o fuzz-obj-y += tests/qtest/fuzz/virtio_net_fuzz.o +fuzz-obj-y += tests/qtest/fuzz/virtio_scsi_fuzz.o FUZZ_CFLAGS += -I$(SRC_PATH)/tests -I$(SRC_PATH)/tests/qtest
tests/qtest/fuzz/virtio_scsi_fuzz.c new file +214 -0
214 lines not on this page · raw message
Signature · 1 line
2.25.0
Re: [PATCH v9 22/23] fuzz: add virtio-scsi fuzz target
Quoted 12 lines · Alexander Bulekov
tests/qtest/fuzz/virtio_scsi_fuzz.c new file +6
-0@@ -0,0 +1,214 @@ +/* +* virtio-serial Fuzzing Target +* +* Copyright Red Hat Inc., 2019 +* +* Authors:
tests/qtest/fuzz/virtio_scsi_fuzz.c line 16
Quoted 10 lines
tests/qtest/fuzz/virtio_scsi_fuzz.c new file +10
-0+* Alexander Bulekov <[email protected]> +* +* This work is licensed under the terms of the GNU GPL, version 2 or later. +* See the COPYING file in the top-level directory. +*/ + +#include "qemu/osdep.h" + +#include "tests/qtest/libqtest.h" +#include "tests/qtest/libqos/virtio-net.h"
virtio-net?
tests/qtest/fuzz/virtio_scsi_fuzz.c line 193
Quoted 5 lines
tests/qtest/fuzz/virtio_scsi_fuzz.c new file +5
-0+static void register_virtio_scsi_fuzz_targets (void ) +{ + fuzz_add_qos_target(&(FuzzTarget){ + .name = "virtio-scsi-fuzz", + .description = "Fuzz the virtio-net virtual queues, forking"
s/net/scsi/
tests/qtest/fuzz/virtio_scsi_fuzz.c line 204
Quoted 11 lines
tests/qtest/fuzz/virtio_scsi_fuzz.c new file +11
-0+ "for each fuzz run", + .pre_vm_init = &counter_shm_init, + .pre_fuzz = &virtio_scsi_pre_fuzz, + .fuzz = virtio_scsi_fork_fuzz,}, +"virtio-scsi" , + &(QOSGraphTestOptions ){.before = virtio_scsi_test_setup} + ); + + fuzz_add_qos_target(&(FuzzTarget){ + .name = "virtio-scsi-flags-fuzz", + .description = "Fuzz the virtio-net virtual queues, forking"
s/net/scsi/
PATCH v9 23/23 fuzz: add documentation to docs/devel/
In reply to Alexander Bulekov--- docs/devel/fuzzing.txt | 116 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 116 insertions(+) create mode 100644 docs/devel/fuzzing.txt
docs/devel/fuzzing.txt new file +116 -0
@@ -0,0 +1,116 @@ += Fuzzing = + +== Introduction == + +This document describes the virtual-device fuzzing infrastructure in QEMU and +how to use it to implement additional fuzzers. + +== Basics == + +Fuzzing operates by passing inputs to an entry point/target function. The +fuzzer tracks the code coverage triggered by the input. Based on these +findings, the fuzzer mutates the input and repeats the fuzzing. + +To fuzz QEMU, we rely on libfuzzer. Unlike other fuzzers such as AFL, libfuzzer +is an _in-process_ fuzzer. For the developer, this means that it is their +responsibility to ensure that state is reset between fuzzing-runs. + +== Building the fuzzers == + +NOTE: If possible, build a 32-bit binary. When forking, the 32-bit fuzzer is +much faster, since the page-map has a smaller size. This is due to the fact that +AddressSanitizer mmaps ~20TB of memory, as part of its detection. This results +in a large page-map, and a much slower fork(). + +To build the fuzzers, install a recent version of clang: +Configure with (substitute the clang binaries with the version you installed): + + CC=clang-8 CXX=clang++-8 /path/to/configure --enable-fuzzing + +Fuzz targets are built similarly to system/softmmu: + + make i386-softmmu/fuzz + +This builds ./i386-softmmu/qemu-fuzz-i386 + +The first option to this command is: --fuzz_taget=FUZZ_NAME +To list all of the available fuzzers run qemu-fuzz-i386 with no arguments. + +eg: + ./i386-softmmu/qemu-fuzz-i386 --fuzz-target=virtio-net-fork-fuzz + +Internally, libfuzzer parses all arguments that do not begin with "--". +Information about these is available by passing -help=1 + +Now the only thing left to do is wait for the fuzzer to trigger potential +crashes. + +== Adding a new fuzzer == +Coverage over virtual devices can be improved by adding additional fuzzers. +Fuzzers are kept in tests/qtest/fuzz/ and should be added to +tests/qtest/fuzz/Makefile.include + +Fuzzers can rely on both qtest and libqos to communicate with virtual devices. + +1. Create a new source file. For example ``tests/qtest/fuzz/foo-device-fuzz.c``. + +2. Write the fuzzing code using the libqtest/libqos API. See existing fuzzers +for reference. + +3. Register the fuzzer in ``tests/fuzz/Makefile.include`` by appending the +corresponding object to fuzz-obj-y + +Fuzzers can be more-or-less thought of as special qtest programs which can +modify the qtest commands and/or qtest command arguments based on inputs +provided by libfuzzer. Libfuzzer passes a byte array and length. Commonly the +fuzzer loops over the byte-array interpreting it as a list of qtest commands, +addresses, or values. + += Implementation Details = + +== The Fuzzer's Lifecycle == + +The fuzzer has two entrypoints that libfuzzer calls. libfuzzer provides it's +own main(), which performs some setup, and calls the entrypoints: + +LLVMFuzzerInitialize: called prior to fuzzing. Used to initialize all of the +necessary state + +LLVMFuzzerTestOneInput: called for each fuzzing run. Processes the input and +resets the state at the end of each run. + +In more detail: + +LLVMFuzzerInitialize parses the arguments to the fuzzer (must start with two +dashes, so they are ignored by libfuzzer main()). Currently, the arguments +select the fuzz target. Then, the qtest client is initialized. If the target +requires qos, qgraph is set up and the QOM/LIBQOS modules are initialized. +Then the QGraph is walked and the QEMU cmd_line is determined and saved. + +After this, the vl.c:qemu__main is called to set up the guest. There are +target-specific hooks that can be called before and after qemu_main, for +additional setup(e.g. PCI setup, or VM snapshotting). + +LLVMFuzzerTestOneInput: Uses qtest/qos functions to act based on the fuzz +input. It is also responsible for manually calling the main loop/main_loop_wait +to ensure that bottom halves are executed and any cleanup required before the +next input. + +Since the same process is reused for many fuzzing runs, QEMU state needs to +be reset at the end of each run. There are currently two implemented +options for resetting state: +1. Reboot the guest between runs. + Pros: Straightforward and fast for simple fuzz targets. + Cons: Depending on the device, does not reset all device state. If the + device requires some initialization prior to being ready for fuzzing + (common for QOS-based targets), this initialization needs to be done after + each reboot. + Example target: i440fx-qtest-reboot-fuzz +2. Run each test case in a separate forked process and copy the coverage + information back to the parent. This is fairly similar to AFL's "deferred" + fork-server mode [3] + Pros: Relatively fast. Devices only need to be initialized once. No need + to do slow reboots or vmloads. + Cons: Not officially supported by libfuzzer. Does not work well for devices + that rely on dedicated threads. + Example target: virtio-net-fork-fuzz
Signature · 1 line
2.25.0