From patchwork Fri Aug 25 16:43:35 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: George Dunlap X-Patchwork-Id: 9922501 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 3FC476022E for ; Fri, 25 Aug 2017 16:46:28 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 30B50281D2 for ; Fri, 25 Aug 2017 16:46:28 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 25C59283D1; Fri, 25 Aug 2017 16:46:28 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-4.2 required=2.0 tests=BAYES_00, RCVD_IN_DNSWL_MED autolearn=ham version=3.3.1 Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) (using TLSv1.2 with cipher AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id C86E4281D2 for ; Fri, 25 Aug 2017 16:46:27 +0000 (UTC) Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.84_2) (envelope-from ) id 1dlHi5-0004xL-0D; Fri, 25 Aug 2017 16:43:53 +0000 Received: from mail6.bemta6.messagelabs.com ([193.109.254.103]) by lists.xenproject.org with esmtp (Exim 4.84_2) (envelope-from ) id 1dlHi3-0004wU-Vp for xen-devel@lists.xenproject.org; Fri, 25 Aug 2017 16:43:52 +0000 Received: from [85.158.143.35] by server-1.bemta-6.messagelabs.com id 6F/15-03765-7C350A95; Fri, 25 Aug 2017 16:43:51 +0000 X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFmpmkeJIrShJLcpLzFFi42JxWrohUvdY8IJ Ig09z9C2+b5nM5MDocfjDFZYAxijWzLyk/IoE1ozTe1pYC5aIVWzv/M/YwHhboIuRk0NCwF/i 5f4DzCA2m4CexLzjX1m6GDk4RARUJG7vNehi5OJgFtjPKNH79QcbSI2wgLvEwjdL2EFsFgFVi We/HoLZvAK2Em9mvWKFmCkvce7BbWaQOZwCdhK7TnKBmEJAJS/O6IFUCAF1Ln5wFKpTUOLkzC csIDazgITEwRcvmCcw8s5CkpqFJLWAkWkVo0ZxalFZapGuoYFeUlFmekZJbmJmDpBnppebWly cmJ6ak5hUrJecn7uJERg4DECwg/H4+7hDjJIcTEqivNYv50cK8SXlp1RmJBZnxBeV5qQWH2KU 4eBQkuCdELQgUkiwKDU9tSItMwcYwjBpCQ4eJRHeNpA0b3FBYm5xZjpE6hSjopQ4bylIQgAkk VGaB9cGi5tLjLJSwryMQIcI8RSkFuVmlqDKv2IU52BUEub9Hwg0hSczrwRu+iugxUxAiyedmA OyuCQRISXVwLi81E26ZeOMxqvHHGNkaraczkoRmuPy5kN1V9jTr62CHy7tu5v++Fshx/wgRcW dry9dsaiZ8GqbSuqlJVmuS5Zmf/R7orX38oWtnyp+mySV+WqUfihoP1pea7uST3vCtD/J/Rvb v8x0lpr9+vexDec+h8/3EL9t6n3LfUX2FJu+a3HJLUfF355UYinOSDTUYi4qTgQAbasY75YCA AA= X-Env-Sender: prvs=4030414b0=George.Dunlap@citrix.com X-Msg-Ref: server-11.tower-21.messagelabs.com!1503679427!76491514!3 X-Originating-IP: [66.165.176.89] X-SpamReason: No, hits=0.0 required=7.0 tests=sa_preprocessor: VHJ1c3RlZCBJUDogNjYuMTY1LjE3Ni44OSA9PiAyMDMwMDc=\n, received_headers: No Received headers X-StarScan-Received: X-StarScan-Version: 9.4.45; banners=-,-,- X-VirusChecked: Checked Received: (qmail 7157 invoked from network); 25 Aug 2017 16:43:50 -0000 Received: from smtp.citrix.com (HELO SMTP.CITRIX.COM) (66.165.176.89) by server-11.tower-21.messagelabs.com with RC4-SHA encrypted SMTP; 25 Aug 2017 16:43:50 -0000 X-IronPort-AV: E=Sophos;i="5.41,426,1498521600"; d="scan'208";a="436909231" From: George Dunlap To: Date: Fri, 25 Aug 2017 17:43:35 +0100 Message-ID: <20170825164343.29015-6-george.dunlap@citrix.com> X-Mailer: git-send-email 2.14.1 In-Reply-To: <20170825164343.29015-1-george.dunlap@citrix.com> References: <20170825164343.29015-1-george.dunlap@citrix.com> MIME-Version: 1.0 Cc: Ian Jackson , Wei Liu , George Dunlap , Jan Beulich , Andrew Cooper Subject: [Xen-devel] [PATCH 06/14] fuzz/x86_emulate: Implement dread() and davail() X-BeenThere: xen-devel@lists.xen.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xen.org Sender: "Xen-devel" X-Virus-Scanned: ClamAV using ClamSMTP Rather than open-coding the "read" from the input file. Signed-off-by: George Dunlap --- CC: Ian Jackson CC: Wei Liu CC: Andrew Cooper CC: Jan Beulich --- tools/fuzz/x86_instruction_emulator/fuzz-emul.c | 31 ++++++++++++++++++------- 1 file changed, 22 insertions(+), 9 deletions(-) diff --git a/tools/fuzz/x86_instruction_emulator/fuzz-emul.c b/tools/fuzz/x86_instruction_emulator/fuzz-emul.c index 7f9a369421..0f5ff0b265 100644 --- a/tools/fuzz/x86_instruction_emulator/fuzz-emul.c +++ b/tools/fuzz/x86_instruction_emulator/fuzz-emul.c @@ -52,6 +52,22 @@ struct fuzz_state struct x86_emulate_ops ops; }; +static inline int davail(struct fuzz_state *s, size_t size) +{ + return s->data_index + size < s->data_num; +} + +static inline int dread(struct fuzz_state *s, void *dst, size_t size) +{ + if ( !davail(s, size) ) + return 0; + + memcpy(dst, &s->corpus->data[s->data_index], size); + s->data_index += size; + + return 1; +} + char *x86emul_return_string[] = { [X86EMUL_OKAY]="X86EMUL_OKAY", [X86EMUL_UNHANDLEABLE]="X86EMUL_UNHANDLEABLE", @@ -68,10 +84,10 @@ static int maybe_fail(struct x86_emulate_ctxt *ctxt, const char *why, bool exception) { struct fuzz_state *s = ctxt->data; - const struct fuzz_corpus *c = s->corpus; + unsigned char c; int rc; - if ( s->data_index >= s->data_num ) + if ( !dread(s, &c, sizeof(c)) ) rc = X86EMUL_EXCEPTION; else { @@ -80,13 +96,12 @@ static int maybe_fail(struct x86_emulate_ctxt *ctxt, * 25% unhandlable * 25% exception */ - if ( c->data[s->data_index] > 0xc0 ) + if ( c > 0xc0 ) rc = X86EMUL_EXCEPTION; - else if ( c->data[s->data_index] > 0x80 ) + else if ( c > 0x80 ) rc = X86EMUL_UNHANDLEABLE; else rc = X86EMUL_OKAY; - s->data_index++; } if ( rc == X86EMUL_EXCEPTION && !exception ) @@ -106,11 +121,10 @@ static int data_read(struct x86_emulate_ctxt *ctxt, const char *why, void *dst, unsigned int bytes) { struct fuzz_state *s = ctxt->data; - const struct fuzz_corpus *c = s->corpus; unsigned int i; int rc; - if ( s->data_index + bytes > s->data_num ) + if ( !davail(s, bytes) ) { /* * Fake up a segment limit violation. System segment limit volations @@ -128,8 +142,7 @@ static int data_read(struct x86_emulate_ctxt *ctxt, if ( rc == X86EMUL_OKAY ) { - memcpy(dst, &c->data[s->data_index], bytes); - s->data_index += bytes; + dread(s, dst, bytes); printf("%s: ", why); for ( i = 0; i < bytes; i++ )