From nobody Tue Feb 10 20:08:47 2026 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; 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 1613682938724298.57178903135514; Thu, 18 Feb 2021 13:15:38 -0800 (PST) Received: from localhost ([::1]:57834 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lCqdr-00050a-Cy for importer@patchew.org; Thu, 18 Feb 2021 16:15:19 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:60198) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lCqbq-00049C-PU for qemu-devel@nongnu.org; Thu, 18 Feb 2021 16:13:14 -0500 Received: from mail-mw2nam10on2129.outbound.protection.outlook.com ([40.107.94.129]:11137 helo=NAM10-MW2-obe.outbound.protection.outlook.com) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lCqbn-0004Bf-Qt for qemu-devel@nongnu.org; Thu, 18 Feb 2021 16:13:13 -0500 Received: from SN6PR03MB3871.namprd03.prod.outlook.com (2603:10b6:805:6d::32) by SN6PR03MB3936.namprd03.prod.outlook.com (2603:10b6:805:72::31) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3868.27; Thu, 18 Feb 2021 21:12:37 +0000 Received: from SN6PR03MB3871.namprd03.prod.outlook.com ([fe80::e828:cd84:e00c:6310]) by SN6PR03MB3871.namprd03.prod.outlook.com ([fe80::e828:cd84:e00c:6310%6]) with mapi id 15.20.3846.041; Thu, 18 Feb 2021 21:12:37 +0000 Received: from stormtrooper.vrmnet (72.74.210.193) by BL0PR02CA0066.namprd02.prod.outlook.com (2603:10b6:207:3d::43) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3868.28 via Frontend Transport; Thu, 18 Feb 2021 21:12:36 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=eOlRwUvvVOft5svXetVLCOkxxfgjc84BBmRA5C8H4Nz3R7k8jk8eg0uM36XGvgH5sNHlz1wuGnfazIvN8YxYY7vPUvvIE0c1IINmr746Csj/VGfUJHdi7BC4q6fyzf1wtB8bDqEg7C+cXNq76f/3kTbtRZsvSsNZQlYVEFzQS5yCXbFAhSxXRCvKJtxoY0eo0gq8JkB9HLXjlyKBLRrz1i9v0eH9sYUXalPlHIPMtO7oXHjBHH7tEvk+J2XcmWslCDC6ifhtz58yuVxy6w+LuMWvWNRYOpIFIVKbep2g5KU0UY1iDO6kLlWtm6Z5JyvMFtW0dHRULiX/EnXnqRmraA== 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=F4CviGRNADMEy0H7II6FfDzVrMMDGvPJ/gNliTBhIAE=; b=OPedb8uGRr5A8v1Sm/k3SxCaKUWzJDmbtD1Y+0bPmwzzI9Iw0kK3sBxi3Z3i0LC4l0QUfGXNASHgiZMSZGGNV+Zs5xjfYmxSLUjTBwuDldTuMWIDIZNLn3b+zRB8/QISoH+uavHQfwYfBo2L4LWEcun9ATPkzuv7FscaW7NIqKu6yyLRXlM1t6EcZNwl6xRLjSjFQ24r3UIIoVGr+IiUas9xwH945PZn6byOYoI/DrgsPnzrH1HPBFLavOqkXM8VPE+JFd9Y2FOnXjr73PHD0EI37rUJxkinMmJ9g/W6GIq+sB+tESRYSk7Scn7R5oZsQQfDMqkXxOumqFxniQvo/A== 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=F4CviGRNADMEy0H7II6FfDzVrMMDGvPJ/gNliTBhIAE=; b=q1wqSa6mtTktYjJP/eW63PWv7BofWikJ0gHmrM13x7LTulEcsZKBg/2QOBhXqvUo3SnijqBNtB9Ai13aa9ZCG3VsCiulYHaCOT8VwmS+OZu/tmxed2Sz7rL6eSM+uNUtzWghcnrd8abSuk4LmgfSuvLI1D1J/xjgsGBT4Mw5MPE= Authentication-Results: nongnu.org; dkim=none (message not signed) header.d=none;nongnu.org; dmarc=none action=none header.from=bu.edu; From: Alexander Bulekov To: qemu-devel@nongnu.org Subject: [RFC PATCH 1/5] fuzz: add a script to build reproducers Date: Thu, 18 Feb 2021 16:12:19 -0500 Message-Id: <20210218211224.2513413-2-alxndr@bu.edu> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20210218211224.2513413-1-alxndr@bu.edu> References: <20210218211224.2513413-1-alxndr@bu.edu> Content-Transfer-Encoding: quoted-printable X-Originating-IP: [72.74.210.193] X-ClientProxiedBy: BL0PR02CA0066.namprd02.prod.outlook.com (2603:10b6:207:3d::43) To SN6PR03MB3871.namprd03.prod.outlook.com (2603:10b6:805:6d::32) MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 89e5a865-1356-4744-4908-08d8d451eb1c X-MS-TrafficTypeDiagnostic: SN6PR03MB3936: X-MS-Exchange-Transport-Forked: True X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:561; X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: 24Fp/iltUckst2SIdKD9qMzQU1/ABo6uydOuygATjNLcDryQaEsOes23SHNDBtKQEG/EOEVUOZ0w5ZO7tByJg3JlEr+xG7iyUdz0XccoluqWlvw6Ngy+MQKR6l/2+Y3pYIF2F5/EqgFyVs7/OBsxfU5MKDN55cv6p5E4gTCEIgHo0++Sl5af0trQ49g3IctpAfpIffBX5Z0J5tRPa5IffRS4aDCQVuEZ1xb7dg7jKm7bnzINYA1jhciIyffgtivyG0OAd6AF9KKGV4qljK3N4QQ/n0Exr0/AS4mH+DkzR8rjumaF+/m3Uz6dhpO5EjUahaI6RttBtAaJEDkzTbOID5zWHb90qy44pfhz08YPKSGWFy4KyXQ667VteOWMl62uVj7922Qcv+M0aqMkIPmaVsFM3Ne9I1FhZJawJRjzGybMIK8jGr9N75pm42U99TdtwImTGH81xfOmnwhgOBSK57o0k+G5BhmitXnpQVoDoSZSLMa4cuP8y6Kdug+ZfeIjyHfuG8IGeRhXqTkX68pJ4w== X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:SN6PR03MB3871.namprd03.prod.outlook.com; PTR:; CAT:NONE; SFS:(4636009)(376002)(396003)(366004)(39860400002)(346002)(136003)(52116002)(6916009)(86362001)(6506007)(2906002)(478600001)(83380400001)(8676002)(75432002)(8936002)(26005)(4326008)(6486002)(2616005)(956004)(1076003)(66946007)(54906003)(66476007)(6666004)(16526019)(36756003)(316002)(186003)(66556008)(786003)(6512007)(5660300002); DIR:OUT; SFP:1102; X-MS-Exchange-AntiSpam-MessageData: =?us-ascii?Q?H8jAMU4HLqTkHsS3p4Nr+aKl6elA/H9YTaM6/MgN4KFmthiZ667ZAFCAu4ZJ?= =?us-ascii?Q?xBLRZV/HLUTk4IwFvSALb1jsagSzmXUIy40iFH+KGd+MJnWfVU/PDGhlO/+a?= =?us-ascii?Q?gytWmGq5QeGpadL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DyLS5qMD145MJYvj3utK?= =?us-ascii?Q?XSALrri5jQuKJJflLs7KfCUoHtXS/Wy/T/iDKM9mM17iwRzxhLcoE7bHPvEn?= =?us-ascii?Q?Zblrw6PoqqqcWiKopRFYN5p8iS0sQXIFwxM+ZGtMPkRBJxVnYQRlMdp9E9Ej?= =?us-ascii?Q?lmTLpaxApf7OhYIjQQCEKPZHHJP7luNIZn/YUVFbIXhzf2EUmHv8f/JXfqT9?= =?us-ascii?Q?fJ3l1sq9cs4PbsTQpwOc7nA2xbCSbgENgzJOL7jvMCa4Peb6owOnPFAqCXYR?= =?us-ascii?Q?xF+UeVXRVk++qjQI34HgAScTPbC3LdA2NkZVphfUDOJLL2fqMxBB+0u3ebws?= =?us-ascii?Q?Wl6xMVuUIi18bsEmkDjNYsbM?= X-OriginatorOrg: bu.edu X-MS-Exchange-CrossTenant-Network-Message-Id: 89e5a865-1356-4744-4908-08d8d451eb1c X-MS-Exchange-CrossTenant-AuthSource: SN6PR03MB3871.namprd03.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 18 Feb 2021 21:12:37.7302 (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: BvmuGe5Kn7RCky3CuOduEJTiN+a3KVivmkEgaADkzBp+MQYUnWVVTYBV3KxUXH0G X-MS-Exchange-Transport-CrossTenantHeadersStamped: SN6PR03MB3936 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=40.107.94.129; envelope-from=alxndr@bu.edu; helo=NAM10-MW2-obe.outbound.protection.outlook.com X-Spam_score_int: -8 X-Spam_score: -0.9 X-Spam_bar: / X-Spam_report: (-0.9 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, HK_RANDOM_ENVFROM=0.001, HK_RANDOM_FROM=0.999, MSGID_FROM_MTA_HEADER=0.001, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H2=-0.001, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=no 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: Thomas Huth , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Alexander Bulekov , Bandan Das , Stefan Hajnoczi , Paolo Bonzini , Bin Meng 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" Currently, bash and C crash reproducers are be built manually. This is a problem, as we want to integrate reproducers into the tree, for regression testing. This patch adds a script that converts a sequence of QTest commands into a pasteable Bash reproducer, or a libqtest-based C program. This will try to wrap pasteable reproducers to 72 chars, but the generated C code will not have nice formatting. Therefore, the C output of this script should be piped through an auto-formatter, such as clang-format Signed-off-by: Alexander Bulekov --- scripts/oss-fuzz/output_reproducer.py | 161 ++++++++++++++++++++++++++ 1 file changed, 161 insertions(+) create mode 100755 scripts/oss-fuzz/output_reproducer.py diff --git a/scripts/oss-fuzz/output_reproducer.py b/scripts/oss-fuzz/outpu= t_reproducer.py new file mode 100755 index 0000000000..f6f5f1fb55 --- /dev/null +++ b/scripts/oss-fuzz/output_reproducer.py @@ -0,0 +1,161 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- + +""" +Convert plain qtest traces to C or Bash reproducers + +Use this to help build bug-reports or create in-tree reproducers for bugs. +Note: This will not format C code for you. Pipe the output through +clang-format -style=3D"{BasedOnStyle: llvm, IndentWidth: 4, ColumnLimit: 9= 0}" +or similar +""" + +import sys +import os +import argparse +import textwrap +from datetime import date + +__author__ =3D "Alexander Bulekov " +__copyright__ =3D "Copyright (C) 2021, 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 c_header(owner): + return """/* + * Autogenerated Fuzzer Test Case + * + * Copyright (c) {date} {owner} + * + * This work is licensed under the terms of the GNU GPL, version 2 or late= r. + * See the COPYING file in the top-level directory. + */ + +#include "qemu/osdep.h" + +#include "libqos/libqtest.h" + + """.format(date=3Ddate.today().year, owner=3Downer) + +def c_comment(s): + """ Return a multi-line C comment. Assume the text is already wrapped = """ + return "/*\n * " + "\n * ".join(s.splitlines()) + "\n*/" + +def print_c_function(s): + print("/* ") + for l in s.splitlines(): + print(" * {}".format(l)) + +def bash_reproducer(path, args, trace): + result =3D '\\\n'.join(textwrap.wrap("cat << EOF | {} {}".format(path,= args), + 72, break_on_hyphens=3DFalse, + drop_whitespace=3DFalse)) + for l in trace.splitlines(): + result +=3D "\n" + '\\\n'.join(textwrap.wrap(l,72,drop_whitespace= =3DFalse)) + result +=3D "\nEOF" + return result + +def c_reproducer(name, args, trace): + result =3D [] + result.append("""static void {}(void)\n{{""".format(name)) + + # libqtest will add its own qtest args, so get rid of them + args =3D args.replace("-accel qtest","") + args =3D args.replace(",accel=3Dqtest","") + args =3D args.replace("-machine accel=3Dqtest","") + args =3D args.replace("-qtest stdio","") + result.append("""QTestState *s =3D qtest_init("{}");""".format(args)) + for l in trace.splitlines(): + param =3D l.split() + cmd =3D param[0] + if cmd =3D=3D "write": + buf =3D param[3][2:] #Get the 0x... buffer and trim the "0x" + assert len(buf)%2 =3D=3D 0 + bufbytes =3D [buf[i:i+2] for i in range(0, len(buf), 2)] + bufstring =3D '\\x'+'\\x'.join(bufbytes) + addr =3D param[1] + size =3D param[2] + result.append("""qtest_bufwrite(s, {}, "{}", {});""".format( + addr, bufstring, size)) + elif cmd.startswith("in") or cmd.startswith("read"): + result.append("qtest_{}(s, {});".format( + cmd, param[1])) + elif cmd.startswith("out") or cmd.startswith("write"): + result.append("qtest_{}(s, {}, {});".format( + cmd, param[1], param[2])) + elif cmd =3D=3D "clock_step": + if len(param) =3D=3D1: + result.append("qtest_clock_step_next(s);") + else: + result.append("qtest_clock_step(s, {});".format(param[1])) + result.append("qtest_quit(s);\n}") + return "\n".join(result) + +def c_main(name, arch): + return """int main(int argc, char **argv) +{{ + const char *arch =3D qtest_get_arch(); + + g_test_init(&argc, &argv, NULL); + + if (strcmp(arch, "{arch}") =3D=3D 0) {{ + qtest_add_func("fuzz/{name}",{name}); + }} + + return g_test_run(); +}}""".format(name=3Dname, arch=3Darch) + +def main(): + parser =3D argparse.ArgumentParser() + group =3D parser.add_mutually_exclusive_group() + group.add_argument("-bash", help=3D"Only output a copy-pastable bash c= ommand", + action=3D"store_true") + group.add_argument("-c", help=3D"Only output a c function", + action=3D"store_true") + parser.add_argument('-owner', help=3D"If generating complete C source = code, \ + this specifies the Copyright owner", + nargs=3D'?', default=3D"") + parser.add_argument("-no_comment", help=3D"Don't include a bash reprod= ucer \ + as a comment in the C reproducers", + action=3D"store_true") + parser.add_argument('-name', help=3D"The name of the c function", + nargs=3D'?', default=3D"test_fuzz") + parser.add_argument('input_trace', help=3D"input QTest command sequenc= e \ + (stdin by default)", + nargs=3D'?', type=3Dargparse.FileType('r'), + default=3Dsys.stdin) + args =3D parser.parse_args() + + qemu_path =3D os.getenv("QEMU_PATH") + qemu_args =3D os.getenv("QEMU_ARGS") + if not qemu_args or not qemu_path: + print("Please set QEMU_PATH and QEMU_ARGS environment variables") + sys.exit(1) + + bash_args =3D qemu_args + if " -qtest stdio" not in qemu_args: + bash_args +=3D " -qtest stdio" + + arch =3D qemu_path.split("-")[-1] + trace =3D args.input_trace.read().strip() + + if args.bash : + print(bash_reproducer(qemu_path, bash_args, trace)) + print(c_reproducer(args.name, qemu_args, trace)) + else: + output =3D "" + if not args.c: + output +=3D c_header(args.owner) + "\n" + if not args.no_comment: + output +=3D c_comment(bash_reproducer(qemu_path, bash_args, tr= ace)) + output +=3D c_reproducer(args.name, qemu_args, trace) + if not args.c: + output +=3D c_main(args.name, arch) + print(output) + + +if __name__ =3D=3D '__main__': + main() --=20 2.28.0