mirror of
https://github.com/izzy2lost/xemu.git
synced 2026-07-06 00:20:22 -07:00
Merge remote-tracking branch 'remotes/huth-gitlab/tags/pull-request-2020-10-26' into staging
* qtest fixes (e.g. memory leaks) * Fix for Xen dummy cpu loop (which happened due to qtest accel rework) * Introduction of the generic device fuzzer * Run more check-acceptance tests in the gitlab-CI # gpg: Signature made Mon 26 Oct 2020 09:34:04 GMT # gpg: using RSA key 27B88847EEE0250118F3EAB92ED9D774FE702DB5 # gpg: issuer "thuth@redhat.com" # gpg: Good signature from "Thomas Huth <th.huth@gmx.de>" [full] # gpg: aka "Thomas Huth <thuth@redhat.com>" [full] # gpg: aka "Thomas Huth <huth@tuxfamily.org>" [full] # gpg: aka "Thomas Huth <th.huth@posteo.de>" [unknown] # Primary key fingerprint: 27B8 8847 EEE0 2501 18F3 EAB9 2ED9 D774 FE70 2DB5 * remotes/huth-gitlab/tags/pull-request-2020-10-26: (31 commits) tests/acceptance: Use .ppm extention for Portable PixMap files tests/acceptance: Remove unused import test/docker/dockerfiles: Add missing packages for acceptance tests tests/acceptance: Enable AVOCADO_ALLOW_UNTRUSTED_CODE in the gitlab-CI test/acceptance: Remove the CONTINUOUS_INTEGRATION tags tests/acceptance/ppc_prep_40p: Fix the URL to the NetBSD-4.0 archive scripts/oss-fuzz: ignore the generic-fuzz target scripts/oss-fuzz: use hardlinks instead of copying fuzz: register predefined generic-fuzz configs fuzz: add generic-fuzz configs for oss-fuzz fuzz: add an "opaque" to the FuzzTarget struct fuzz: Add instructions for using generic-fuzz scripts/oss-fuzz: Add crash trace minimization script scripts/oss-fuzz: Add script to reorder a generic-fuzzer trace fuzz: add a crossover function to generic-fuzzer fuzz: add a DISABLE_PCI op to generic-fuzzer fuzz: Add support for custom crossover functions fuzz: Add fuzzer callbacks to DMA-read functions fuzz: Declare DMA Read callback function fuzz: Add DMA support to the generic-fuzzer ... Signed-off-by: Peter Maydell <peter.maydell@linaro.org>
This commit is contained in:
@@ -66,6 +66,7 @@ include:
|
||||
- if [ -d ${CI_PROJECT_DIR}/avocado-cache ]; then
|
||||
du -chs ${CI_PROJECT_DIR}/avocado-cache ;
|
||||
fi
|
||||
- export AVOCADO_ALLOW_UNTRUSTED_CODE=1
|
||||
after_script:
|
||||
- cd build
|
||||
- python3 -c 'import json; r = json.load(open("tests/results/latest/results.json")); [print(t["logfile"]) for t in r["tests"] if t["status"] not in ("PASS", "SKIP", "CANCEL")]' | xargs cat
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* QTest accelerator code
|
||||
* Dummy cpu thread code
|
||||
*
|
||||
* Copyright IBM, Corp. 2011
|
||||
*
|
||||
@@ -13,26 +13,13 @@
|
||||
|
||||
#include "qemu/osdep.h"
|
||||
#include "qemu/rcu.h"
|
||||
#include "qapi/error.h"
|
||||
#include "qemu/module.h"
|
||||
#include "qemu/option.h"
|
||||
#include "qemu/config-file.h"
|
||||
#include "sysemu/accel.h"
|
||||
#include "sysemu/qtest.h"
|
||||
#include "sysemu/cpus.h"
|
||||
#include "sysemu/cpu-timers.h"
|
||||
#include "qemu/guest-random.h"
|
||||
#include "qemu/main-loop.h"
|
||||
#include "hw/core/cpu.h"
|
||||
|
||||
#include "qtest-cpus.h"
|
||||
|
||||
static void *qtest_cpu_thread_fn(void *arg)
|
||||
static void *dummy_cpu_thread_fn(void *arg)
|
||||
{
|
||||
#ifdef _WIN32
|
||||
error_report("qtest is not supported under Windows");
|
||||
exit(1);
|
||||
#else
|
||||
CPUState *cpu = arg;
|
||||
sigset_t waitset;
|
||||
int r;
|
||||
@@ -69,10 +56,9 @@ static void *qtest_cpu_thread_fn(void *arg)
|
||||
qemu_mutex_unlock_iothread();
|
||||
rcu_unregister_thread();
|
||||
return NULL;
|
||||
#endif
|
||||
}
|
||||
|
||||
static void qtest_start_vcpu_thread(CPUState *cpu)
|
||||
void dummy_start_vcpu_thread(CPUState *cpu)
|
||||
{
|
||||
char thread_name[VCPU_THREAD_NAME_SIZE];
|
||||
|
||||
@@ -81,11 +67,6 @@ static void qtest_start_vcpu_thread(CPUState *cpu)
|
||||
qemu_cond_init(cpu->halt_cond);
|
||||
snprintf(thread_name, VCPU_THREAD_NAME_SIZE, "CPU %d/DUMMY",
|
||||
cpu->cpu_index);
|
||||
qemu_thread_create(cpu->thread, thread_name, qtest_cpu_thread_fn, cpu,
|
||||
qemu_thread_create(cpu->thread, thread_name, dummy_cpu_thread_fn, cpu,
|
||||
QEMU_THREAD_JOINABLE);
|
||||
}
|
||||
|
||||
const CpusAccel qtest_cpus = {
|
||||
.create_vcpu_thread = qtest_start_vcpu_thread,
|
||||
.get_virtual_clock = qtest_get_virtual_clock,
|
||||
};
|
||||
@@ -5,3 +5,11 @@ subdir('kvm')
|
||||
subdir('tcg')
|
||||
subdir('xen')
|
||||
subdir('stubs')
|
||||
|
||||
dummy_ss = ss.source_set()
|
||||
dummy_ss.add(files(
|
||||
'dummy-cpus.c',
|
||||
))
|
||||
|
||||
specific_ss.add_all(when: ['CONFIG_SOFTMMU', 'CONFIG_POSIX'], if_true: dummy_ss)
|
||||
specific_ss.add_all(when: ['CONFIG_XEN'], if_true: dummy_ss)
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
qtest_ss = ss.source_set()
|
||||
qtest_ss.add(files(
|
||||
'qtest.c',
|
||||
'qtest-cpus.c',
|
||||
))
|
||||
|
||||
specific_ss.add_all(when: ['CONFIG_SOFTMMU', 'CONFIG_POSIX'], if_true: qtest_ss)
|
||||
|
||||
@@ -1,17 +0,0 @@
|
||||
/*
|
||||
* Accelerator CPUS Interface
|
||||
*
|
||||
* Copyright 2020 SUSE LLC
|
||||
*
|
||||
* 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 QTEST_CPUS_H
|
||||
#define QTEST_CPUS_H
|
||||
|
||||
#include "sysemu/cpus.h"
|
||||
|
||||
extern const CpusAccel qtest_cpus;
|
||||
|
||||
#endif /* QTEST_CPUS_H */
|
||||
+4
-1
@@ -25,7 +25,10 @@
|
||||
#include "qemu/main-loop.h"
|
||||
#include "hw/core/cpu.h"
|
||||
|
||||
#include "qtest-cpus.h"
|
||||
const CpusAccel qtest_cpus = {
|
||||
.create_vcpu_thread = dummy_start_vcpu_thread,
|
||||
.get_virtual_clock = qtest_get_virtual_clock,
|
||||
};
|
||||
|
||||
static int qtest_init_accel(MachineState *ms)
|
||||
{
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
#include "hw/xen/xen_pt.h"
|
||||
#include "chardev/char.h"
|
||||
#include "sysemu/accel.h"
|
||||
#include "sysemu/cpus.h"
|
||||
#include "sysemu/xen.h"
|
||||
#include "sysemu/runstate.h"
|
||||
#include "migration/misc.h"
|
||||
@@ -153,6 +154,10 @@ static void xen_setup_post(MachineState *ms, AccelState *accel)
|
||||
}
|
||||
}
|
||||
|
||||
const CpusAccel xen_cpus = {
|
||||
.create_vcpu_thread = dummy_start_vcpu_thread,
|
||||
};
|
||||
|
||||
static int xen_init(MachineState *ms)
|
||||
{
|
||||
MachineClass *mc = MACHINE_GET_CLASS(ms);
|
||||
@@ -180,6 +185,9 @@ static int xen_init(MachineState *ms)
|
||||
* opt out of system RAM being allocated by generic code
|
||||
*/
|
||||
mc->default_ram_id = NULL;
|
||||
|
||||
cpus_register_accel(&xen_cpus);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
@@ -125,6 +125,45 @@ 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.
|
||||
|
||||
== The Generic Fuzzer ==
|
||||
Writing a fuzz target can be a lot of effort (especially if a device driver has
|
||||
not be built-out within libqos). Many devices can be fuzzed to some degree,
|
||||
without any device-specific code, using the generic-fuzz target.
|
||||
|
||||
The generic-fuzz target is capable of fuzzing devices over their PIO, MMIO,
|
||||
and DMA input-spaces. To apply the generic-fuzz to a device, we need to define
|
||||
two env-variables, at minimum:
|
||||
|
||||
QEMU_FUZZ_ARGS= is the set of QEMU arguments used to configure a machine, with
|
||||
the device attached. For example, if we want to fuzz the virtio-net device
|
||||
attached to a pc-i440fx machine, we can specify:
|
||||
QEMU_FUZZ_ARGS="-M pc -nodefaults -netdev user,id=user0 \
|
||||
-device virtio-net,netdev=user0"
|
||||
|
||||
QEMU_FUZZ_OBJECTS= is a set of space-delimited strings used to identify the
|
||||
MemoryRegions that will be fuzzed. These strings are compared against
|
||||
MemoryRegion names and MemoryRegion owner names, to decide whether each
|
||||
MemoryRegion should be fuzzed. These strings support globbing. For the
|
||||
virtio-net example, we could use QEMU_FUZZ_OBJECTS=
|
||||
* 'virtio-net'
|
||||
* 'virtio*'
|
||||
* 'virtio* pcspk' (Fuzz the virtio devices and the PC speaker...)
|
||||
* '*' (Fuzz the whole machine)
|
||||
|
||||
The "info mtree" and "info qom-tree" monitor commands can be especially useful
|
||||
for identifying the MemoryRegion and Object names used for matching.
|
||||
|
||||
As a generic rule-of-thumb, the more MemoryRegions/Devices we match, the greater
|
||||
the input-space, and the smaller the probability of finding crashing inputs for
|
||||
individual devices. As such, it is usually a good idea to limit the fuzzer to
|
||||
only a few MemoryRegions.
|
||||
|
||||
To ensure that these env variables have been configured correctly, we can use:
|
||||
|
||||
./qemu-fuzz-i386 --fuzz-target=generic-fuzz -runs=0
|
||||
|
||||
The output should contain a complete list of matched MemoryRegions.
|
||||
|
||||
= Implementation Details =
|
||||
|
||||
== The Fuzzer's Lifecycle ==
|
||||
|
||||
@@ -42,6 +42,21 @@ typedef struct IOMMUMemoryRegionClass IOMMUMemoryRegionClass;
|
||||
DECLARE_OBJ_CHECKERS(IOMMUMemoryRegion, IOMMUMemoryRegionClass,
|
||||
IOMMU_MEMORY_REGION, TYPE_IOMMU_MEMORY_REGION)
|
||||
|
||||
#ifdef CONFIG_FUZZ
|
||||
void fuzz_dma_read_cb(size_t addr,
|
||||
size_t len,
|
||||
MemoryRegion *mr,
|
||||
bool is_write);
|
||||
#else
|
||||
static inline void fuzz_dma_read_cb(size_t addr,
|
||||
size_t len,
|
||||
MemoryRegion *mr,
|
||||
bool is_write)
|
||||
{
|
||||
/* Do Nothing */
|
||||
}
|
||||
#endif
|
||||
|
||||
extern bool global_dirty_log;
|
||||
|
||||
typedef struct MemoryRegionOps MemoryRegionOps;
|
||||
@@ -719,6 +734,11 @@ static inline FlatView *address_space_to_flatview(AddressSpace *as)
|
||||
return qatomic_rcu_read(&as->current_map);
|
||||
}
|
||||
|
||||
typedef int (*flatview_cb)(Int128 start,
|
||||
Int128 len,
|
||||
const MemoryRegion*, void*);
|
||||
|
||||
void flatview_for_each_range(FlatView *fv, flatview_cb cb , void *opaque);
|
||||
|
||||
/**
|
||||
* struct MemoryRegionSection: describes a fragment of a #MemoryRegion
|
||||
@@ -2442,6 +2462,7 @@ address_space_read_cached(MemoryRegionCache *cache, hwaddr addr,
|
||||
void *buf, hwaddr len)
|
||||
{
|
||||
assert(addr < cache->len && len <= cache->len - addr);
|
||||
fuzz_dma_read_cb(cache->xlat + addr, len, cache->mrs.mr, false);
|
||||
if (likely(cache->ptr)) {
|
||||
memcpy(buf, cache->ptr + addr, len);
|
||||
return MEMTX_OK;
|
||||
|
||||
@@ -28,6 +28,7 @@ static inline uint32_t ADDRESS_SPACE_LD_CACHED(l)(MemoryRegionCache *cache,
|
||||
hwaddr addr, MemTxAttrs attrs, MemTxResult *result)
|
||||
{
|
||||
assert(addr < cache->len && 4 <= cache->len - addr);
|
||||
fuzz_dma_read_cb(cache->xlat + addr, 4, cache->mrs.mr, false);
|
||||
if (likely(cache->ptr)) {
|
||||
return LD_P(l)(cache->ptr + addr);
|
||||
} else {
|
||||
@@ -39,6 +40,7 @@ static inline uint64_t ADDRESS_SPACE_LD_CACHED(q)(MemoryRegionCache *cache,
|
||||
hwaddr addr, MemTxAttrs attrs, MemTxResult *result)
|
||||
{
|
||||
assert(addr < cache->len && 8 <= cache->len - addr);
|
||||
fuzz_dma_read_cb(cache->xlat + addr, 8, cache->mrs.mr, false);
|
||||
if (likely(cache->ptr)) {
|
||||
return LD_P(q)(cache->ptr + addr);
|
||||
} else {
|
||||
@@ -50,6 +52,7 @@ static inline uint32_t ADDRESS_SPACE_LD_CACHED(uw)(MemoryRegionCache *cache,
|
||||
hwaddr addr, MemTxAttrs attrs, MemTxResult *result)
|
||||
{
|
||||
assert(addr < cache->len && 2 <= cache->len - addr);
|
||||
fuzz_dma_read_cb(cache->xlat + addr, 2, cache->mrs.mr, false);
|
||||
if (likely(cache->ptr)) {
|
||||
return LD_P(uw)(cache->ptr + addr);
|
||||
} else {
|
||||
|
||||
@@ -25,6 +25,9 @@ typedef struct CpusAccel {
|
||||
/* register accel-specific cpus interface implementation */
|
||||
void cpus_register_accel(const CpusAccel *i);
|
||||
|
||||
/* Create a dummy vcpu for CpusAccel->create_vcpu_thread */
|
||||
void dummy_start_vcpu_thread(CPUState *);
|
||||
|
||||
/* interface available for cpus accelerator threads */
|
||||
|
||||
/* For temporary buffers for forming a name */
|
||||
|
||||
@@ -42,6 +42,7 @@ static inline uint32_t glue(address_space_ldl_internal, SUFFIX)(ARG1_DECL,
|
||||
MO_32 | devend_memop(endian), attrs);
|
||||
} else {
|
||||
/* RAM case */
|
||||
fuzz_dma_read_cb(addr, 4, mr, false);
|
||||
ptr = qemu_map_ram_ptr(mr->ram_block, addr1);
|
||||
switch (endian) {
|
||||
case DEVICE_LITTLE_ENDIAN:
|
||||
@@ -110,6 +111,7 @@ static inline uint64_t glue(address_space_ldq_internal, SUFFIX)(ARG1_DECL,
|
||||
MO_64 | devend_memop(endian), attrs);
|
||||
} else {
|
||||
/* RAM case */
|
||||
fuzz_dma_read_cb(addr, 8, mr, false);
|
||||
ptr = qemu_map_ram_ptr(mr->ram_block, addr1);
|
||||
switch (endian) {
|
||||
case DEVICE_LITTLE_ENDIAN:
|
||||
@@ -175,6 +177,7 @@ uint32_t glue(address_space_ldub, SUFFIX)(ARG1_DECL,
|
||||
r = memory_region_dispatch_read(mr, addr1, &val, MO_8, attrs);
|
||||
} else {
|
||||
/* RAM case */
|
||||
fuzz_dma_read_cb(addr, 1, mr, false);
|
||||
ptr = qemu_map_ram_ptr(mr->ram_block, addr1);
|
||||
val = ldub_p(ptr);
|
||||
r = MEMTX_OK;
|
||||
@@ -212,6 +215,7 @@ static inline uint32_t glue(address_space_lduw_internal, SUFFIX)(ARG1_DECL,
|
||||
MO_16 | devend_memop(endian), attrs);
|
||||
} else {
|
||||
/* RAM case */
|
||||
fuzz_dma_read_cb(addr, 2, mr, false);
|
||||
ptr = qemu_map_ram_ptr(mr->ram_block, addr1);
|
||||
switch (endian) {
|
||||
case DEVICE_LITTLE_ENDIAN:
|
||||
|
||||
@@ -62,6 +62,9 @@ fi
|
||||
|
||||
mkdir -p "$DEST_DIR/lib/" # Copy the shared libraries here
|
||||
|
||||
mkdir -p "$DEST_DIR/bin/" # Copy executables that shouldn't
|
||||
# be treated as fuzzers by oss-fuzz here
|
||||
|
||||
# Build once to get the list of dynamic lib paths, and copy them over
|
||||
../configure --disable-werror --cc="$CC" --cxx="$CXX" --enable-fuzzing \
|
||||
--prefix="$DEST_DIR" --bindir="$DEST_DIR" --datadir="$DEST_DIR/data/" \
|
||||
@@ -88,13 +91,22 @@ make "-j$(nproc)" qemu-fuzz-i386 V=1
|
||||
# Copy over the datadir
|
||||
cp -r ../pc-bios/ "$DEST_DIR/pc-bios"
|
||||
|
||||
cp "./qemu-fuzz-i386" "$DEST_DIR/bin/"
|
||||
|
||||
# Run the fuzzer with no arguments, to print the help-string and get the list
|
||||
# of available fuzz-targets. Copy over the qemu-fuzz-i386, naming it according
|
||||
# to each available fuzz target (See 05509c8e6d fuzz: select fuzz target using
|
||||
# executable name)
|
||||
for target in $(./qemu-fuzz-i386 | awk '$1 ~ /\*/ {print $2}');
|
||||
do
|
||||
cp qemu-fuzz-i386 "$DEST_DIR/qemu-fuzz-i386-target-$target"
|
||||
# Ignore the generic-fuzz target, as it requires some environment variables
|
||||
# to be configured. We have some generic-fuzz-{pc-q35, floppy, ...} targets
|
||||
# that are thin wrappers around this target that set the required
|
||||
# environment variables according to predefined configs.
|
||||
if [ "$target" != "generic-fuzz" ]; then
|
||||
ln "$DEST_DIR/bin/qemu-fuzz-i386" \
|
||||
"$DEST_DIR/qemu-fuzz-i386-target-$target"
|
||||
fi
|
||||
done
|
||||
|
||||
echo "Done. The fuzzers are located in $DEST_DIR"
|
||||
|
||||
Executable
+157
@@ -0,0 +1,157 @@
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
"""
|
||||
This takes a crashing qtest trace and tries to remove superflous operations
|
||||
"""
|
||||
|
||||
import sys
|
||||
import os
|
||||
import subprocess
|
||||
import time
|
||||
import struct
|
||||
|
||||
QEMU_ARGS = None
|
||||
QEMU_PATH = None
|
||||
TIMEOUT = 5
|
||||
CRASH_TOKEN = None
|
||||
|
||||
write_suffix_lookup = {"b": (1, "B"),
|
||||
"w": (2, "H"),
|
||||
"l": (4, "L"),
|
||||
"q": (8, "Q")}
|
||||
|
||||
def usage():
|
||||
sys.exit("""\
|
||||
Usage: QEMU_PATH="/path/to/qemu" QEMU_ARGS="args" {} input_trace output_trace
|
||||
By default, will try to use the second-to-last line in the output to identify
|
||||
whether the crash occred. Optionally, manually set a string that idenitifes the
|
||||
crash by setting CRASH_TOKEN=
|
||||
""".format((sys.argv[0])))
|
||||
|
||||
def check_if_trace_crashes(trace, path):
|
||||
global CRASH_TOKEN
|
||||
with open(path, "w") as tracefile:
|
||||
tracefile.write("".join(trace))
|
||||
|
||||
rc = subprocess.Popen("timeout -s 9 {timeout}s {qemu_path} {qemu_args} 2>&1\
|
||||
< {trace_path}".format(timeout=TIMEOUT,
|
||||
qemu_path=QEMU_PATH,
|
||||
qemu_args=QEMU_ARGS,
|
||||
trace_path=path),
|
||||
shell=True,
|
||||
stdin=subprocess.PIPE,
|
||||
stdout=subprocess.PIPE)
|
||||
stdo = rc.communicate()[0]
|
||||
output = stdo.decode('unicode_escape')
|
||||
if rc.returncode == 137: # Timed Out
|
||||
return False
|
||||
if len(output.splitlines()) < 2:
|
||||
return False
|
||||
|
||||
if CRASH_TOKEN is None:
|
||||
CRASH_TOKEN = output.splitlines()[-2]
|
||||
|
||||
return CRASH_TOKEN in output
|
||||
|
||||
|
||||
def minimize_trace(inpath, outpath):
|
||||
global TIMEOUT
|
||||
with open(inpath) as f:
|
||||
trace = f.readlines()
|
||||
start = time.time()
|
||||
if not check_if_trace_crashes(trace, outpath):
|
||||
sys.exit("The input qtest trace didn't cause a crash...")
|
||||
end = time.time()
|
||||
print("Crashed in {} seconds".format(end-start))
|
||||
TIMEOUT = (end-start)*5
|
||||
print("Setting the timeout for {} seconds".format(TIMEOUT))
|
||||
print("Identifying Crashes by this string: {}".format(CRASH_TOKEN))
|
||||
|
||||
i = 0
|
||||
newtrace = trace[:]
|
||||
# For each line
|
||||
while i < len(newtrace):
|
||||
# 1.) Try to remove it completely and reproduce the crash. If it works,
|
||||
# we're done.
|
||||
prior = newtrace[i]
|
||||
print("Trying to remove {}".format(newtrace[i]))
|
||||
# Try to remove the line completely
|
||||
newtrace[i] = ""
|
||||
if check_if_trace_crashes(newtrace, outpath):
|
||||
i += 1
|
||||
continue
|
||||
newtrace[i] = prior
|
||||
|
||||
# 2.) Try to replace write{bwlq} commands with a write addr, len
|
||||
# command. Since this can require swapping endianness, try both LE and
|
||||
# BE options. We do this, so we can "trim" the writes in (3)
|
||||
if (newtrace[i].startswith("write") and not
|
||||
newtrace[i].startswith("write ")):
|
||||
suffix = newtrace[i].split()[0][-1]
|
||||
assert(suffix in write_suffix_lookup)
|
||||
addr = int(newtrace[i].split()[1], 16)
|
||||
value = int(newtrace[i].split()[2], 16)
|
||||
for endianness in ['<', '>']:
|
||||
data = struct.pack("{end}{size}".format(end=endianness,
|
||||
size=write_suffix_lookup[suffix][1]),
|
||||
value)
|
||||
newtrace[i] = "write {addr} {size} 0x{data}\n".format(
|
||||
addr=hex(addr),
|
||||
size=hex(write_suffix_lookup[suffix][0]),
|
||||
data=data.hex())
|
||||
if(check_if_trace_crashes(newtrace, outpath)):
|
||||
break
|
||||
else:
|
||||
newtrace[i] = prior
|
||||
|
||||
# 3.) If it is a qtest write command: write addr len data, try to split
|
||||
# it into two separate write commands. If splitting the write down the
|
||||
# middle does not work, try to move the pivot "left" and retry, until
|
||||
# there is no space left. The idea is to prune unneccessary bytes from
|
||||
# long writes, while accommodating arbitrary MemoryRegion access sizes
|
||||
# and alignments.
|
||||
if newtrace[i].startswith("write "):
|
||||
addr = int(newtrace[i].split()[1], 16)
|
||||
length = int(newtrace[i].split()[2], 16)
|
||||
data = newtrace[i].split()[3][2:]
|
||||
if length > 1:
|
||||
leftlength = int(length/2)
|
||||
rightlength = length - leftlength
|
||||
newtrace.insert(i+1, "")
|
||||
while leftlength > 0:
|
||||
newtrace[i] = "write {addr} {size} 0x{data}\n".format(
|
||||
addr=hex(addr),
|
||||
size=hex(leftlength),
|
||||
data=data[:leftlength*2])
|
||||
newtrace[i+1] = "write {addr} {size} 0x{data}\n".format(
|
||||
addr=hex(addr+leftlength),
|
||||
size=hex(rightlength),
|
||||
data=data[leftlength*2:])
|
||||
if check_if_trace_crashes(newtrace, outpath):
|
||||
break
|
||||
else:
|
||||
leftlength -= 1
|
||||
rightlength += 1
|
||||
if check_if_trace_crashes(newtrace, outpath):
|
||||
i -= 1
|
||||
else:
|
||||
newtrace[i] = prior
|
||||
del newtrace[i+1]
|
||||
i += 1
|
||||
check_if_trace_crashes(newtrace, outpath)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
if len(sys.argv) < 3:
|
||||
usage()
|
||||
|
||||
QEMU_PATH = os.getenv("QEMU_PATH")
|
||||
QEMU_ARGS = os.getenv("QEMU_ARGS")
|
||||
if QEMU_PATH is None or QEMU_ARGS is None:
|
||||
usage()
|
||||
# if "accel" not in QEMU_ARGS:
|
||||
# QEMU_ARGS += " -accel qtest"
|
||||
CRASH_TOKEN = os.getenv("CRASH_TOKEN")
|
||||
QEMU_ARGS += " -qtest stdio -monitor none -serial none "
|
||||
minimize_trace(sys.argv[1], sys.argv[2])
|
||||
+103
@@ -0,0 +1,103 @@
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
"""
|
||||
Use this to convert qtest log info from a generic fuzzer input into a qtest
|
||||
trace that you can feed into a standard qemu-system process. Example usage:
|
||||
|
||||
QEMU_FUZZ_ARGS="-machine q35,accel=qtest" QEMU_FUZZ_OBJECTS="*" \
|
||||
./i386-softmmu/qemu-fuzz-i386 --fuzz-target=generic-pci-fuzz
|
||||
# .. Finds some crash
|
||||
QTEST_LOG=1 FUZZ_SERIALIZE_QTEST=1 \
|
||||
QEMU_FUZZ_ARGS="-machine q35,accel=qtest" QEMU_FUZZ_OBJECTS="*" \
|
||||
./i386-softmmu/qemu-fuzz-i386 --fuzz-target=generic-pci-fuzz
|
||||
/path/to/crash 2> qtest_log_output
|
||||
scripts/oss-fuzz/reorder_fuzzer_qtest_trace.py qtest_log_output > qtest_trace
|
||||
./i386-softmmu/qemu-fuzz-i386 -machine q35,accel=qtest \
|
||||
-qtest stdin < qtest_trace
|
||||
|
||||
### Details ###
|
||||
|
||||
Some fuzzer make use of hooks that allow us to populate some memory range, just
|
||||
before a DMA read from that range. This means that the fuzzer can produce
|
||||
activity that looks like:
|
||||
[start] read from mmio addr
|
||||
[end] read from mmio addr
|
||||
[start] write to pio addr
|
||||
[start] fill a DMA buffer just in time
|
||||
[end] fill a DMA buffer just in time
|
||||
[start] fill a DMA buffer just in time
|
||||
[end] fill a DMA buffer just in time
|
||||
[end] write to pio addr
|
||||
[start] read from mmio addr
|
||||
[end] read from mmio addr
|
||||
|
||||
We annotate these "nested" DMA writes, so with QTEST_LOG=1 the QTest trace
|
||||
might look something like:
|
||||
[R +0.028431] readw 0x10000
|
||||
[R +0.028434] outl 0xc000 0xbeef # Triggers a DMA read from 0xbeef and 0xbf00
|
||||
[DMA][R +0.034639] write 0xbeef 0x2 0xAAAA
|
||||
[DMA][R +0.034639] write 0xbf00 0x2 0xBBBB
|
||||
[R +0.028431] readw 0xfc000
|
||||
|
||||
This script would reorder the above trace so it becomes:
|
||||
readw 0x10000
|
||||
write 0xbeef 0x2 0xAAAA
|
||||
write 0xbf00 0x2 0xBBBB
|
||||
outl 0xc000 0xbeef
|
||||
readw 0xfc000
|
||||
|
||||
I.e. by the time, 0xc000 tries to read from DMA, those DMA buffers have already
|
||||
been set up, removing the need for the DMA hooks. We can simply provide this
|
||||
reordered trace via -qtest stdio to reproduce the input
|
||||
|
||||
Note: this won't work for traces where the device tries to read from the same
|
||||
DMA region twice in between MMIO/PIO commands. E.g:
|
||||
[R +0.028434] outl 0xc000 0xbeef
|
||||
[DMA][R +0.034639] write 0xbeef 0x2 0xAAAA
|
||||
[DMA][R +0.034639] write 0xbeef 0x2 0xBBBB
|
||||
|
||||
The fuzzer will annotate suspected double-fetches with [DOUBLE-FETCH]. This
|
||||
script looks for these tags and warns the users that the resulting trace might
|
||||
not reproduce the bug.
|
||||
"""
|
||||
|
||||
import sys
|
||||
|
||||
__author__ = "Alexander Bulekov <alxndr@bu.edu>"
|
||||
__copyright__ = "Copyright (C) 2020, Red Hat, Inc."
|
||||
__license__ = "GPL version 2 or (at your option) any later version"
|
||||
|
||||
__maintainer__ = "Alexander Bulekov"
|
||||
__email__ = "alxndr@bu.edu"
|
||||
|
||||
|
||||
def usage():
|
||||
sys.exit("Usage: {} /path/to/qtest_log_output".format((sys.argv[0])))
|
||||
|
||||
|
||||
def main(filename):
|
||||
with open(filename, "r") as f:
|
||||
trace = f.readlines()
|
||||
|
||||
# Leave only lines that look like logged qtest commands
|
||||
trace[:] = [x.strip() for x in trace if "[R +" in x
|
||||
or "[S +" in x and "CLOSED" not in x]
|
||||
|
||||
for i in range(len(trace)):
|
||||
if i+1 < len(trace):
|
||||
if "[DMA]" in trace[i+1]:
|
||||
if "[DOUBLE-FETCH]" in trace[i+1]:
|
||||
sys.stderr.write("Warning: Likely double fetch on line"
|
||||
"{}.\n There will likely be problems "
|
||||
"reproducing behavior with the "
|
||||
"resulting qtest trace\n\n".format(i+1))
|
||||
trace[i], trace[i+1] = trace[i+1], trace[i]
|
||||
for line in trace:
|
||||
print(line.split("]")[-1].strip())
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
if len(sys.argv) == 1:
|
||||
usage()
|
||||
main(sys.argv[1])
|
||||
@@ -656,6 +656,19 @@ static void render_memory_region(FlatView *view,
|
||||
}
|
||||
}
|
||||
|
||||
void flatview_for_each_range(FlatView *fv, flatview_cb cb , void *opaque)
|
||||
{
|
||||
FlatRange *fr;
|
||||
|
||||
assert(fv);
|
||||
assert(cb);
|
||||
|
||||
FOR_EACH_FLAT_RANGE(fr, fv) {
|
||||
if (cb(fr->addr.start, fr->addr.size, fr->mr, opaque))
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
static MemoryRegion *memory_region_get_flatview_root(MemoryRegion *mr)
|
||||
{
|
||||
while (mr->enabled) {
|
||||
@@ -1420,6 +1433,7 @@ MemTxResult memory_region_dispatch_read(MemoryRegion *mr,
|
||||
unsigned size = memop_size(op);
|
||||
MemTxResult r;
|
||||
|
||||
fuzz_dma_read_cb(addr, size, mr, false);
|
||||
if (!memory_region_access_valid(mr, addr, size, false, attrs)) {
|
||||
*pval = unassigned_mem_read(mr, addr, size);
|
||||
return MEMTX_DECODE_ERROR;
|
||||
@@ -3233,6 +3247,19 @@ void memory_region_init_rom_device(MemoryRegion *mr,
|
||||
vmstate_register_ram(mr, owner_dev);
|
||||
}
|
||||
|
||||
/*
|
||||
* Support softmmu builds with CONFIG_FUZZ using a weak symbol and a stub for
|
||||
* the fuzz_dma_read_cb callback
|
||||
*/
|
||||
#ifdef CONFIG_FUZZ
|
||||
void __attribute__((weak)) fuzz_dma_read_cb(size_t addr,
|
||||
size_t len,
|
||||
MemoryRegion *mr,
|
||||
bool is_write)
|
||||
{
|
||||
}
|
||||
#endif
|
||||
|
||||
static const TypeInfo memory_region_info = {
|
||||
.parent = TYPE_OBJECT,
|
||||
.name = TYPE_MEMORY_REGION,
|
||||
|
||||
@@ -2832,6 +2832,7 @@ MemTxResult flatview_read_continue(FlatView *fv, hwaddr addr,
|
||||
stn_he_p(buf, l, val);
|
||||
} else {
|
||||
/* RAM case */
|
||||
fuzz_dma_read_cb(addr, len, mr, false);
|
||||
ram_ptr = qemu_ram_ptr_length(mr->ram_block, addr1, &l, false);
|
||||
memcpy(buf, ram_ptr, l);
|
||||
}
|
||||
@@ -3192,6 +3193,7 @@ void *address_space_map(AddressSpace *as,
|
||||
memory_region_ref(mr);
|
||||
*plen = flatview_extend_translation(fv, addr, len, mr, xlat,
|
||||
l, is_write, attrs);
|
||||
fuzz_dma_read_cb(addr, *plen, mr, is_write);
|
||||
ptr = qemu_ram_ptr_length(mr->ram_block, xlat, plen, true);
|
||||
|
||||
return ptr;
|
||||
|
||||
@@ -9,7 +9,6 @@ import os
|
||||
import re
|
||||
import time
|
||||
import logging
|
||||
import distutils.spawn
|
||||
|
||||
from avocado_qemu import Test
|
||||
from avocado import skipUnless
|
||||
@@ -70,7 +69,7 @@ class NextCubeMachine(Test):
|
||||
|
||||
@skipUnless(PIL_AVAILABLE, 'Python PIL not installed')
|
||||
def test_bootrom_framebuffer_size(self):
|
||||
screenshot_path = os.path.join(self.workdir, "dump.png")
|
||||
screenshot_path = os.path.join(self.workdir, "dump.ppm")
|
||||
self.check_bootrom_framebuffer(screenshot_path)
|
||||
|
||||
width, height = Image.open(screenshot_path).size
|
||||
@@ -79,7 +78,7 @@ class NextCubeMachine(Test):
|
||||
|
||||
@skipUnless(tesseract_available(3), 'tesseract v3 OCR tool not available')
|
||||
def test_bootrom_framebuffer_ocr_with_tesseract_v3(self):
|
||||
screenshot_path = os.path.join(self.workdir, "dump.png")
|
||||
screenshot_path = os.path.join(self.workdir, "dump.ppm")
|
||||
self.check_bootrom_framebuffer(screenshot_path)
|
||||
|
||||
console_logger = logging.getLogger('console')
|
||||
@@ -95,7 +94,7 @@ class NextCubeMachine(Test):
|
||||
# that it is still alpha-level software.
|
||||
@skipUnless(tesseract_available(4), 'tesseract v4 OCR tool not available')
|
||||
def test_bootrom_framebuffer_ocr_with_tesseract_v4(self):
|
||||
screenshot_path = os.path.join(self.workdir, "dump.png")
|
||||
screenshot_path = os.path.join(self.workdir, "dump.ppm")
|
||||
self.check_bootrom_framebuffer(screenshot_path)
|
||||
|
||||
console_logger = logging.getLogger('console')
|
||||
|
||||
@@ -22,7 +22,6 @@ class IbmPrep40pMachine(Test):
|
||||
# All rights reserved.
|
||||
# U.S. Government Users Restricted Rights - Use, duplication or disclosure
|
||||
# restricted by GSA ADP Schedule Contract with IBM Corp.
|
||||
@skipIf(os.getenv('CONTINUOUS_INTEGRATION'), 'Running on Travis-CI')
|
||||
@skipUnless(os.getenv('AVOCADO_ALLOW_UNTRUSTED_CODE'), 'untrusted code')
|
||||
def test_factory_firmware_and_netbsd(self):
|
||||
"""
|
||||
@@ -35,7 +34,7 @@ class IbmPrep40pMachine(Test):
|
||||
'7020-40p/P12H0456.IMG')
|
||||
bios_hash = '1775face4e6dc27f3a6ed955ef6eb331bf817f03'
|
||||
bios_path = self.fetch_asset(bios_url, asset_hash=bios_hash)
|
||||
drive_url = ('https://cdn.netbsd.org/pub/NetBSD/NetBSD-archive/'
|
||||
drive_url = ('https://archive.netbsd.org/pub/NetBSD-archive/'
|
||||
'NetBSD-4.0/prep/installation/floppy/generic_com0.fs')
|
||||
drive_hash = 'dbcfc09912e71bd5f0d82c7c1ee43082fb596ceb'
|
||||
drive_path = self.fetch_asset(drive_url, asset_hash=drive_hash)
|
||||
@@ -61,7 +60,6 @@ class IbmPrep40pMachine(Test):
|
||||
wait_for_console_pattern(self, '>> Memory: 192M')
|
||||
wait_for_console_pattern(self, '>> CPU type PowerPC,604')
|
||||
|
||||
@skipIf(os.getenv('CONTINUOUS_INTEGRATION'), 'Running on Travis-CI')
|
||||
def test_openbios_and_netbsd(self):
|
||||
"""
|
||||
:avocado: tags=arch:ppc
|
||||
|
||||
@@ -18,6 +18,7 @@ ENV PACKAGES \
|
||||
lzo-devel \
|
||||
make \
|
||||
mesa-libEGL-devel \
|
||||
nmap-ncat \
|
||||
nettle-devel \
|
||||
ninja-build \
|
||||
perl-Test-Harness \
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user