From nobody Fri Sep 5 22:12:02 2025 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1603707979; cv=none; d=zohomail.com; s=zohoarc; b=MPeULoaJDZfsLKUFnv42h93AINnwhL5gWsC8dA3QH/z5ZvKsDBZfy4jqS2VQcuTf7f5+esNrD2SpwcZKXGggJ6nBXYgaD1CbMhAJPXoE062BV0wlIs0R05kJY4X+jlx0kWHxcOIBRCpt+z20cKVemHm4frzrH4gmiDg5R2GPvpo= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1603707979; h=Content-Type:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:Message-ID:References:Sender:Subject:To; bh=ijFbgbhA9U4wuK2uqFm4kvGlGVKw2BLSDoKtZ2VHRZs=; b=DCST1N5gD2t702ghZGUQw7+i+UQ/tAVl3g/1Z1G4K+CGOfHTSfTM8+52fI9NRUJKhJ5N52BzyrfLX90GmlKo6iqLY7mn+BVHh3JPA8OAZRd89Pj6+HT4GW1Jh9mqUGpayEKZiexxsi7vS3z5jtxX1OWu+wA3vlo3wv7JT8oiyvQ= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1603707979558267.4881845214261; Mon, 26 Oct 2020 03:26:19 -0700 (PDT) Received: from localhost ([::1]:35634 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1kWzhi-0003gc-Co for importer@patchew.org; Mon, 26 Oct 2020 06:26:18 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:38962) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kWzPH-0002me-N1 for qemu-devel@nongnu.org; Mon, 26 Oct 2020 06:07:15 -0400 Received: from us-smtp-delivery-124.mimecast.com ([63.128.21.124]:54820) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.90_1) (envelope-from ) id 1kWzPC-0006k7-ER for qemu-devel@nongnu.org; Mon, 26 Oct 2020 06:07:15 -0400 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-88-4eucRKiEPqK2DVn2Tj9hLA-1; Mon, 26 Oct 2020 06:07:04 -0400 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id A5F3585EE96; Mon, 26 Oct 2020 10:07:03 +0000 (UTC) Received: from thuth.com (ovpn-112-104.ams2.redhat.com [10.36.112.104]) by smtp.corp.redhat.com (Postfix) with ESMTP id AA79C8B842; Mon, 26 Oct 2020 10:07:02 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1603706829; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:content-type:content-type:in-reply-to:in-reply-to: references:references; bh=ijFbgbhA9U4wuK2uqFm4kvGlGVKw2BLSDoKtZ2VHRZs=; b=LakXWspRogGrS+WeAcB36qqy6cALcjAHJigJlHBsFdF9i0kTLyNe13KCArFZBK9ScYryH8 jyp+N/cHcWY2XbGdw8qNIIvW0oJh5Y24JSyIqoJkGL3zKxz4Uluh2aMHOqXx6InJoGmfUJ vrcxL1ACMfRFjAQ6BTGPQXka4IUHnmw= X-MC-Unique: 4eucRKiEPqK2DVn2Tj9hLA-1 From: Thomas Huth To: qemu-devel@nongnu.org, Peter Maydell Subject: [PULL 18/31] scripts/oss-fuzz: Add script to reorder a generic-fuzzer trace Date: Mon, 26 Oct 2020 11:06:19 +0100 Message-Id: <20201026100632.212530-19-thuth@redhat.com> In-Reply-To: <20201026100632.212530-1-thuth@redhat.com> References: <20201026100632.212530-1-thuth@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.15 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=thuth@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=63.128.21.124; envelope-from=thuth@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-detected-operating-system: by eggs.gnu.org: First seen = 2020/10/25 21:03:19 X-ACL-Warn: Detected OS = Linux 2.2.x-3.x [generic] [fuzzy] X-Spam_score_int: -30 X-Spam_score: -3.1 X-Spam_bar: --- X-Spam_report: (-3.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H5=-1, RCVD_IN_MSPIKE_WL=-0.01, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Alexander Bulekov Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: pass (identity @redhat.com) Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" From: Alexander Bulekov The generic-fuzzer uses hooks to fulfill DMA requests just-in-time. This means that if we try to use QTEST_LOG=3D1 to build a reproducer, the DMA writes will be logged _after_ the in/out/read/write that triggered the DMA read. To work work around this, the generic-fuzzer annotates these just-in time DMA fulfilments with a tag that we can use to discern them. This script simply iterates over a raw qtest trace (including log messages, errors, timestamps etc), filters it and re-orders it so that DMA fulfillments are placed directly _before_ the qtest command that will cause the DMA access. Signed-off-by: Alexander Bulekov Reviewed-by: Darren Kenny Message-Id: <20201023150746.107063-11-alxndr@bu.edu> Signed-off-by: Thomas Huth --- .../oss-fuzz/reorder_fuzzer_qtest_trace.py | 103 ++++++++++++++++++ 1 file changed, 103 insertions(+) create mode 100755 scripts/oss-fuzz/reorder_fuzzer_qtest_trace.py diff --git a/scripts/oss-fuzz/reorder_fuzzer_qtest_trace.py b/scripts/oss-f= uzz/reorder_fuzzer_qtest_trace.py new file mode 100755 index 0000000000..890e1def85 --- /dev/null +++ b/scripts/oss-fuzz/reorder_fuzzer_qtest_trace.py @@ -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=3D"-machine q35,accel=3Dqtest" QEMU_FUZZ_OBJECTS=3D"*" \ + ./i386-softmmu/qemu-fuzz-i386 --fuzz-target=3Dgeneric-pci-fuzz +# .. Finds some crash +QTEST_LOG=3D1 FUZZ_SERIALIZE_QTEST=3D1 \ +QEMU_FUZZ_ARGS=3D"-machine q35,accel=3Dqtest" QEMU_FUZZ_OBJECTS=3D"*" \ + ./i386-softmmu/qemu-fuzz-i386 --fuzz-target=3Dgeneric-pci-fuzz + /path/to/crash 2> qtest_log_output +scripts/oss-fuzz/reorder_fuzzer_qtest_trace.py qtest_log_output > qtest_tr= ace +./i386-softmmu/qemu-fuzz-i386 -machine q35,accel=3Dqtest \ + -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=3D1 the QTest tra= ce +might look something like: +[R +0.028431] readw 0x10000 +[R +0.028434] outl 0xc000 0xbeef # Triggers a DMA read from 0xbeef and 0x= bf00 +[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 al= ready +been set up, removing the need for the DMA hooks. We can simply provide th= is +reordered trace via -qtest stdio to reproduce the input + +Note: this won't work for traces where the device tries to read from the s= ame +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 m= ight +not reproduce the bug. +""" + +import sys + +__author__ =3D "Alexander Bulekov " +__copyright__ =3D "Copyright (C) 2020, Red Hat, Inc." +__license__ =3D "GPL version 2 or (at your option) any later version" + +__maintainer__ =3D "Alexander Bulekov" +__email__ =3D "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 =3D f.readlines() + + # Leave only lines that look like logged qtest commands + trace[:] =3D [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] =3D trace[i+1], trace[i] + for line in trace: + print(line.split("]")[-1].strip()) + + +if __name__ =3D=3D '__main__': + if len(sys.argv) =3D=3D 1: + usage() + main(sys.argv[1]) --=20 2.18.2