From nobody Tue Feb 10 14:42:34 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=fail (Bad Signature) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1572447606124331.0337132977834; Wed, 30 Oct 2019 08:00:06 -0700 (PDT) Received: from localhost ([::1]:41262 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iPpS7-0002pU-Qf for importer@patchew.org; Wed, 30 Oct 2019 11:00:03 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:35477) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iPpIb-0005Fk-J8 for qemu-devel@nongnu.org; Wed, 30 Oct 2019 10:50:15 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1iPpIZ-0007cj-UX for qemu-devel@nongnu.org; Wed, 30 Oct 2019 10:50:13 -0400 Received: from mail-eopbgr690101.outbound.protection.outlook.com ([40.107.69.101]:31361 helo=NAM04-CO1-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 1iPpIZ-0007LI-LR for qemu-devel@nongnu.org; Wed, 30 Oct 2019 10:50:11 -0400 Received: from MN2PR03MB4800.namprd03.prod.outlook.com (20.179.82.78) by MN2PR03MB4959.namprd03.prod.outlook.com (52.132.168.18) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.2387.22; Wed, 30 Oct 2019 14:50:04 +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.2387.028; Wed, 30 Oct 2019 14:50:04 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=kpU1GqgDlZA6s8XakcUL8TDbE4Eq67h1ZOGHoKtrcqtbq2P85bXExXfR1NUD7ZL0wtJarf96rpwn6Qsdy0XytElC9sKFQmOXL+cv8R45Jwdrxh31g54UxhAkpumrqOJRcgJD/au+alze7Fxkgl+hDmmWm+bdBIRCSS0rjgegcnVGCGdEFvNtPSERogyMCHPUNCK4D0EynbkZeHXrGtEG2qt5R4GVYy89Is60u1wPEd7nV536AzSZoYi7wRRm3eb6a2CnAs0NVyNecuyDsrQsRJiBuhhkRmyM+e4Vs69QHjyh0TcH5i8JoUqe5JK2GpFcPGkWpY5tXTu1SGm021gONA== 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=I06QdC9/KgefgXf1SpYRHXJIr+nmQABh4Rg+kqbv7cg=; b=Yf+0kDaciLzskgNcFHxDzhAsZr3LaIz+IcqyVE2G2t5yRzpZR7JLIAFVifgPjp0tIlxE4FOMisfkTEGIjwJWLO5OGWpjs2k2bsWr0K2iw/nrOJfMmByYAmiEU5yF88FqtIzOqSQT+Z7OLvsSiwFjvknq5rlkWvS2udDZuINZAE7jxedUOtxClehFzvBySaa3xKzaye01FjNT/BBpZZsebPZTI2hsrnai+zTFp7GmaT3Ing+WR1XMKzaio9B46uDCWkxzAx6POhu9Y7cMFjUTXRuRsdsrTQYSB1lY/6PWc8Lf/VKvhEr6AMcYnLIRIYzu796DBhW/N7Q9uXJkCVSaJw== 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=I06QdC9/KgefgXf1SpYRHXJIr+nmQABh4Rg+kqbv7cg=; b=YHvTGJt51qOGZ+pIH+CgnKMDm0j5pnemHa2IXstheKKQb3K6A3W+aL4N5dX1EwqJjNh7EplhL7d6uNQb6IJ28At61uZbGExt7oBYim4wW5KJOvQt/JggQeY2oS/ZpZ/zPglxSetUcwE2Qxzs9S3Zt+hMissK+OSHz/yobd28vlM= From: "Oleinik, Alexander" To: "qemu-devel@nongnu.org" Subject: [PATCH v4 20/20] fuzz: add documentation to docs/devel/ Thread-Topic: [PATCH v4 20/20] fuzz: add documentation to docs/devel/ Thread-Index: AQHVjzFQWDhlqlxcSEW7sNPvkYqX2w== Date: Wed, 30 Oct 2019 14:50:04 +0000 Message-ID: <20191030144926.11873-21-alxndr@bu.edu> References: <20191030144926.11873-1-alxndr@bu.edu> In-Reply-To: <20191030144926.11873-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: DM5PR08CA0048.namprd08.prod.outlook.com (2603:10b6:4:60::37) 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: 2245822d-984b-48b8-8e76-08d75d4872ef x-ms-traffictypediagnostic: MN2PR03MB4959: x-ms-exchange-transport-forked: True x-microsoft-antispam-prvs: x-ms-oob-tlc-oobclassifiers: OLM:9508; x-forefront-prvs: 02065A9E77 x-forefront-antispam-report: SFV:NSPM; SFS:(10019020)(396003)(366004)(39860400002)(376002)(346002)(136003)(199004)(189003)(102836004)(386003)(6506007)(66946007)(66446008)(64756008)(66476007)(2616005)(186003)(2351001)(446003)(26005)(36756003)(66556008)(3846002)(6116002)(14444005)(2501003)(14454004)(11346002)(66066001)(256004)(4326008)(6916009)(305945005)(1076003)(6486002)(486006)(5660300002)(71190400001)(50226002)(71200400001)(476003)(86362001)(88552002)(7736002)(52116002)(76176011)(2906002)(478600001)(99286004)(786003)(316002)(8676002)(81166006)(5640700003)(81156014)(25786009)(6436002)(75432002)(8936002)(6512007); DIR:OUT; SFP:1102; SCL:1; SRVR:MN2PR03MB4959; 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: 4EnLwVTB5qZsTQrCiyGRGhI6Zld5G1A68NyMqmFsOyojZhhdGqSJdJTLsGpowOtyzyTGeXiPC63Btl5RX00U6H9zGgRxEW2/qWzpUbYSOGj/E9H19yXEhbv9QrNpiWTYfowVnldNxxY5akzDhZ++xsdjEHc6kHI/Obd+RkNyv5nF4WRQKBrnqXbwxUBSs1ahPFPqP7LPxS2QL/YJ57u+uuwGGOtMTE2sh2fxshsUUegrWoBMNDPkRP6DcHIKIS7qZYK+B5RMBNYRGBuZamLdVugsQOkr4MDw7PmsWOaep/3aEKesHkuZl6ScJPdI34p2zzMeZoXZfFgc6O/ReeAXLrpYtFa9hNFkE7rKhijjWoD2Bf5wD33liWc6YGJ5VJexLNk1cs2oO0/LLd0OLOeh/Ffc5efcGLkNUDxucECLrFSaGWvdxsXflG/pmWc4tI0r Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 X-OriginatorOrg: bu.edu X-MS-Exchange-CrossTenant-Network-Message-Id: 2245822d-984b-48b8-8e76-08d75d4872ef X-MS-Exchange-CrossTenant-originalarrivaltime: 30 Oct 2019 14:50:04.5474 (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: aiXRw5pqhj3KISAZF3O0YKCUmO534x5dnOm5L2Lifv6KgfqxN2JlZng4OugYr97z X-MS-Exchange-Transport-CrossTenantHeadersStamped: MN2PR03MB4959 X-detected-operating-system: by eggs.gnu.org: Windows 7 or 8 [fuzzy] X-Received-From: 40.107.69.101 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: "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" From: Alexander Oleinik Signed-off-by: Alexander Oleinik --- 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..825ff0af51 --- /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(). O + +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 Implmentation 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 initailized. +Then the QGraph is walked and the QEMU cmd_line is determined and saved. + +After this, the vl.c:real_main is called to set up the guest. After this, = the +fuzzer saves the initial vm/device state to ram, after which the initiliza= tion +is complete. + +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. Finally, it calls reset() which +restores state from the ramfile and/or resets the guest. + + +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