From nobody Sat Feb 7 05:32:54 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; arc=pass (i=1dmarc=pass fromdomain=bu.edu) ARC-Seal: i=2; a=rsa-sha256; t=1573686283; cv=pass; d=zoho.com; s=zohoarc; b=JHjgH1RvogIxxGjbCMbzQWSEQg089xHhGeWgOoCp/vOWpCfvMQlJExGB4n8HS6/6iFaXYJa0RA8mnv4KdSkzXh+xPPOqwi+OoihoWcZlK5WlCeDRV7uSCcItvTcmFvlyJbprJm+VVcDdKA4YLNsXemUqgUdJqFhuhJi4cFOLZuc= ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1573686283; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=DD/P7JmfGBnQiJoStfBuNwmO6R80dbzcWVnzmfjO3Z4=; b=OthPSc1Z16Tgt1vwGdm+RxHyV0xa3mpHE089zslsonTqyoCeW+Nnq/Rft+opD4QDjW7zyhhTPwjLNSL0Xb4T4y7/DYthTP6ANwRNp+OENU7IiUAFT0kIRjPklRn2KOF3+DqAVORtvzuItI2UbModHFMYa98GZ2Oo7kPB8YSELDk= ARC-Authentication-Results: i=2; mx.zoho.com; dkim=pass; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; arc=pass (i=1dmarc=pass fromdomain=bu.edu) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1573686283955424.5580308762677; Wed, 13 Nov 2019 15:04:43 -0800 (PST) Received: from localhost ([::1]:52148 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iV1go-0003Ws-D5 for importer@patchew.org; Wed, 13 Nov 2019 18:04:42 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:34191) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iV1TY-000891-Ih for qemu-devel@nongnu.org; Wed, 13 Nov 2019 17:51:01 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1iV1TV-0007pR-Vn for qemu-devel@nongnu.org; Wed, 13 Nov 2019 17:50:59 -0500 Received: from mail-dm3nam03on0714.outbound.protection.outlook.com ([2a01:111:f400:fe49::714]:27886 helo=NAM03-DM3-obe.outbound.protection.outlook.com) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1iV1TV-0007jr-Oh for qemu-devel@nongnu.org; Wed, 13 Nov 2019 17:50:57 -0500 Received: from MN2PR03MB4800.namprd03.prod.outlook.com (20.179.82.78) by MN2PR03MB5024.namprd03.prod.outlook.com (52.132.169.72) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.2430.20; Wed, 13 Nov 2019 22:50:54 +0000 Received: from MN2PR03MB4800.namprd03.prod.outlook.com ([fe80::344f:b88:26f0:9f66]) by MN2PR03MB4800.namprd03.prod.outlook.com ([fe80::344f:b88:26f0:9f66%7]) with mapi id 15.20.2451.023; Wed, 13 Nov 2019 22:50:54 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=GRwfrQfY1q2rIC7fbc6Hy1eWb01F/iDR7+qH4hgsauYXmM4lX0W8Cj5aFfqINXYskjYiW78ib6KR/bJn4843a3ZgJh36J9OvLCkqKMTGlq0Wcw/QFXR+E09p0eTzAFqQP3holvdLFnVnY91N9fxg8CTshphju4it8fw2FpdO4WQTCbcOQGUOCjo3ATms8cI8zeChao5s6GnlzxmFeUOf7gX4rBXlq5iwDuDP6jBHd90XACxl/LWZEcq8tNzRvejHgLwtwa3FBqmypyY2JXLTpADYUfKF80wuZHcnodBncVEaqlO6V3t+jva/8MrNbPCJVN521y2Xe2k7CDCmJ5xBsA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=DD/P7JmfGBnQiJoStfBuNwmO6R80dbzcWVnzmfjO3Z4=; b=G0BXiKk5jX119E2ggNMOyi5zp0I4u9cbIptMkvAvkr0oa1nAhGKtdx4hMpLe0QDiDK0pWLBHOzIroX8epqNuo4ZJOc8D1aX/o7lnEjDhxtil6zY2BnfYeyE+49viOs59pkBCHiCCB4p5GJM7QrHXZhGJeekZLgRmV+KGVp3XNDi+UafFFWHVfq24w3K5eHkXLl8CipU15D+EcBm/87ZEiWsHuncF/uhWrzgxyFOHxf9Il7C3j2F5SVRAn/BVSA0aMOZvPg0fm6Z+TKGbTonN9j9XZ8QUBn3xkhZHtUpuelp/8ulxSjmeKfL7D2GkvmqAw7TllaOxPNeUQCml8cVqJg== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=bu.edu; dmarc=pass action=none header.from=bu.edu; dkim=pass header.d=bu.edu; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bushare.onmicrosoft.com; s=selector2-bushare-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=DD/P7JmfGBnQiJoStfBuNwmO6R80dbzcWVnzmfjO3Z4=; b=LcICT2vNZwX8LJ5fvunh+7bXs5xddnWH1XYjhpiXDsdfqq8o4ekhFh2nGFm1kgIfK0QrwR5zYjYovNqZ+nqzpO0gMe5LDvRQboT9PNDFeje0S1FiLA9kdmlMdOrmE+r3czhPjI6h9ohlQrbTnqmhlV6LU0bBvW+nplrNW7ynr1c= From: "Oleinik, Alexander" To: "qemu-devel@nongnu.org" Subject: [PATCH v5 20/20] fuzz: add documentation to docs/devel/ Thread-Topic: [PATCH v5 20/20] fuzz: add documentation to docs/devel/ Thread-Index: AQHVmnTOmgQ0G5owjUiCu5II/ELl4w== Date: Wed, 13 Nov 2019 22:50:54 +0000 Message-ID: <20191113225030.17023-21-alxndr@bu.edu> References: <20191113225030.17023-1-alxndr@bu.edu> In-Reply-To: <20191113225030.17023-1-alxndr@bu.edu> Accept-Language: en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-mailer: git-send-email 2.23.0 x-originating-ip: [128.197.127.33] x-clientproxiedby: MN2PR15CA0028.namprd15.prod.outlook.com (2603:10b6:208:1b4::41) To MN2PR03MB4800.namprd03.prod.outlook.com (2603:10b6:208:101::14) authentication-results: spf=none (sender IP is ) smtp.mailfrom=alxndr@bu.edu; x-ms-exchange-messagesentrepresentingtype: 1 x-ms-publictraffictype: Email x-ms-office365-filtering-correlation-id: b4827b9b-a9f3-4ac5-8f0a-08d7688bf086 x-ms-traffictypediagnostic: MN2PR03MB5024: x-ms-exchange-transport-forked: True x-microsoft-antispam-prvs: x-ms-oob-tlc-oobclassifiers: OLM:9508; x-forefront-prvs: 0220D4B98D x-forefront-antispam-report: SFV:NSPM; SFS:(10019020)(1496009)(396003)(366004)(136003)(376002)(346002)(39860400002)(199004)(189003)(2501003)(88552002)(75432002)(54906003)(446003)(7736002)(99286004)(11346002)(6436002)(6486002)(5640700003)(476003)(305945005)(256004)(786003)(2906002)(2351001)(14444005)(316002)(14454004)(6116002)(86362001)(2616005)(6512007)(6916009)(486006)(3846002)(71200400001)(71190400001)(4326008)(1076003)(36756003)(386003)(8676002)(478600001)(66066001)(76176011)(81166006)(186003)(66946007)(5660300002)(6506007)(81156014)(64756008)(52116002)(26005)(102836004)(50226002)(25786009)(8936002)(66476007)(66556008)(66446008); DIR:OUT; SFP:1102; SCL:1; SRVR:MN2PR03MB5024; H:MN2PR03MB4800.namprd03.prod.outlook.com; FPR:; SPF:None; LANG:en; PTR:InfoNoRecords; A:1; MX:1; Received-SPF: pass (zoho.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: None (protection.outlook.com: bu.edu does not designate permitted sender hosts) x-ms-exchange-senderadcheck: 1 x-microsoft-antispam: BCL:0; x-microsoft-antispam-message-info: ZSNQzu0yE9dXui9T7MHn0u7EX6l/yU/2Jf+6y/kPuOBfJsURPWWZAYXxE5pANj6kYEwpqVLcB60uRxBOFbgA70hA5HsEsaRdgb9YTs5MZ8aQI0VkmTXgOH9i9Z0cNH/2bNkySSQ3HMxp9rqWs6SsdZ0FNYM2stAcZallVI5IwqgJGQlO+IGVzVn5Pxrrg9YIQ5trK4UhpNxMbp+k9qBb7DIUP9tOCBTvEcTqRCxe5RL+R8A8lwiYC8HqWA7RSprYK4T8+lt/leaHEoveC4wmPjzGj4EsNyw8Vwv2g6rIIwyQN+/IwRzYbPJqCtFb/ICgwlwWrm33njw7/7KX9sZ9s/dkvTZ4YwpiVE20pNVrBrjh5PgmIZuW4RTXyULF+O4gGh47UhGe/Ikpqba1KHFjpGJ7V5WZPjKooxQVlHoTD89UCwhpf9t78B2WI3M+MvBD Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 X-OriginatorOrg: bu.edu X-MS-Exchange-CrossTenant-Network-Message-Id: b4827b9b-a9f3-4ac5-8f0a-08d7688bf086 X-MS-Exchange-CrossTenant-originalarrivaltime: 13 Nov 2019 22:50:54.4605 (UTC) X-MS-Exchange-CrossTenant-fromentityheader: Hosted X-MS-Exchange-CrossTenant-id: d57d32cc-c121-488f-b07b-dfe705680c71 X-MS-Exchange-CrossTenant-mailboxtype: HOSTED X-MS-Exchange-CrossTenant-userprincipalname: tAVM00OlQ5oYZaZcvjEzoqQm1aWG2cJrZTJrcbd4lHydNmRHi7Qz3QIwsPkw2SzJ X-MS-Exchange-Transport-CrossTenantHeadersStamped: MN2PR03MB5024 X-detected-operating-system: by eggs.gnu.org: Windows 7 or 8 [fuzzy] X-Received-From: 2a01:111:f400:fe49::714 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: "pbonzini@redhat.com" , "bsd@redhat.com" , "stefanha@redhat.com" , "Oleinik, Alexander" Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: pass (identity @bushare.onmicrosoft.com) Content-Type: text/plain; charset="utf-8" Signed-off-by: Alexander Bulekov --- docs/devel/fuzzing.txt | 119 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 119 insertions(+) create mode 100644 docs/devel/fuzzing.txt diff --git a/docs/devel/fuzzing.txt b/docs/devel/fuzzing.txt new file mode 100644 index 0000000000..b0cceb2a6b --- /dev/null +++ b/docs/devel/fuzzing.txt @@ -0,0 +1,119 @@ +=3D Fuzzing =3D + +=3D=3D Introduction =3D=3D + +This document describes the virtual-device fuzzing infrastructure in QEMU = and +how to use it to implement additional fuzzers. + +=3D=3D Basics =3D=3D + +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.=20 + +To fuzz QEMU, we rely on libfuzzer. Unlike other fuzzers such as AFL, libf= uzzer +is an _in-process_ fuzzer. For the developer, this means that it is their +responsibility to ensure that state is reset between fuzzing-runs. + +=3D=3D Building the fuzzers =3D=3D + +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 fac= t that +AddressSanitizer mmaps ~20TB of memory, as part of its detection. This res= ults +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 install= ed): + + CC=3Dclang-8 CXX=3Dclang++-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=3DFUZZ_NAME +To list all of the available fuzzers run qemu-fuzz-i386 with no arguments. + +eg: + ./i386-softmmu/qemu-fuzz-i386 --fuzz-target=3Dvirtio-net-fork-fuzz + +Internally, libfuzzer parses all arguments that do not begin with "--". +Information about these is available by passing -help=3D1 + +Now the only thing left to do is wait for the fuzzer to trigger potential +crashes. + +=3D=3D Adding a new fuzzer =3D=3D +Coverage over virtual devices can be improved by adding additional fuzzers= .=20 +Fuzzers are kept in tests/fuzz/ and should be added to +tests/fuzz/Makefile.include + +Fuzzers can rely on both qtest and libqos to communicate with virtual devi= ces. + +1. Create a new source file. For example ``tests/fuzz/fuzz-foo-device.c``. + +2. Write the fuzzing code using the libqtest/libqos API. See existing fuzz= ers +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 comman= ds, +addresses, or values. + + +=3D Implementation Details =3D + +=3D=3D The Fuzzer's Lifecycle =3D=3D + +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 t= he +necessary state + +LLVMFuzzerTestOneInput: called for each fuzzing run. Processes the input a= nd +resets the state at the end of each run. + +In more detail: + +LLVMFuzzerInitialize parses the arguments to the fuzzer (must start with t= wo +dashes, so they are ignored by libfuzzer main()). Currently, the arguments +select the fuzz target. Then, the qtest client is initialized. If the targ= et +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.=20 + + +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:=20 +1. Reboot the guest between runs. + Pros: Straightforward and fast for simple fuzz targets.=20 + 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 af= ter + 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 "deferr= ed" + 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 dev= ices + that rely on dedicated threads. + Example target: virtio-net-fork-fuzz + --=20 2.23.0