From: George Dunlap Subject: Re: [PATCH v3 01/12] fuzz/x86_emulate: Clear errors after each iteration Date: Tue, 10 Oct 2017 17:47:04 +0100 Message-ID: References: <20171010162011.9629-1-george.dunlap@citrix.com> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="------------F1B72B5C7B55F956307DD763" Return-path: Received: from mail6.bemta3.messagelabs.com ([195.245.230.39]) by lists.xenproject.org with esmtp (Exim 4.84_2) (envelope-from ) id 1e22jv-00059H-CV for xen-devel@lists.xenproject.org; Tue, 10 Oct 2017 22:11:03 +0000 In-Reply-To: <20171010162011.9629-1-george.dunlap@citrix.com> Content-Language: en-US List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xen.org Sender: "Xen-devel" To: xen-devel@lists.xenproject.org Cc: Ian Jackson , Wei Liu , Jan Beulich , Andrew Cooper List-Id: xen-devel@lists.xenproject.org --------------F1B72B5C7B55F956307DD763 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 8bit On 10/10/2017 05:20 PM, George Dunlap wrote: > Once feof() returns true for a stream, it will continue to return true > for that stream until clearerr() is called (or the stream is closed > and re-opened). > > In llvm-clang-fast-mode, the same file descriptor is used for each > iteration of the loop, meaning that the "Input too large" check was > broken -- feof() would return true even if the fread() hadn't hit the > end of the file. The result is that AFL generates testcases of > arbitrary size. > > Fix this by fseek'ing to the beginning of the file on every iteration; > this resets the EOF marker and other state. > > Signed-off-by: George Dunlap > --- > Changes in v3: > - Fix the issue in the official sanctioned way Hmm, seems v2 of this patch was checked in; review had flagged up that "clearerr()" was too big of a hammer. Attached is a revised v1/12 patch that fixes this. -George --------------F1B72B5C7B55F956307DD763 Content-Type: text/x-patch; name="0001-fuzz-x86_emulate-Clear-errors-in-the-officially-sanc.patch" Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename*0="0001-fuzz-x86_emulate-Clear-errors-in-the-officially-sanc.pa"; filename*1="tch" =46rom d07b2d68085957bf3d7a2567dce9c4f031fb5966 Mon Sep 17 00:00:00 2001 From: George Dunlap Date: Wed, 4 Oct 2017 17:09:10 +0100 Subject: [PATCH] fuzz/x86_emulate: Clear errors in the officially sanctio= ned way Commit 849a1f10c9 was checked in in appropriately; review flagged up that clearerr() was too big a hammer, as it would clear both the EOF flag and stream errors. Stream errors shouldn't be cleared; we only want the EOF and other stream-related state cleared. To do this, it is sufficient to fseek() to zero. Signed-off-by: George Dunlap --- This is a candidate for backport to 4.9. CC: Ian Jackson CC: Wei Liu CC: Andrew Cooper CC: Jan Beulich --- tools/fuzz/x86_instruction_emulator/afl-harness.c | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/tools/fuzz/x86_instruction_emulator/afl-harness.c b/tools/fu= zz/x86_instruction_emulator/afl-harness.c index b4d15451b5..31ae1daef1 100644 --- a/tools/fuzz/x86_instruction_emulator/afl-harness.c +++ b/tools/fuzz/x86_instruction_emulator/afl-harness.c @@ -77,6 +77,17 @@ int main(int argc, char **argv) exit(-1); } } +#ifdef __AFL_HAVE_MANUAL_CONTROL + else + { + /*=20 + * This will ensure we're dealing with a clean stream + * state after the afl-fuzz process messes with the open + * file handle. + */ + fseek(fp, 0, SEEK_SET); + } +#endif =20 size =3D fread(input, 1, INPUT_SIZE, fp); =20 @@ -97,8 +108,6 @@ int main(int argc, char **argv) fclose(fp); fp =3D NULL; } - else - clearerr(fp); =20 LLVMFuzzerTestOneInput(input, size); } --=20 2.14.2 --------------F1B72B5C7B55F956307DD763 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: base64 Content-Disposition: inline X19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX18KWGVuLWRldmVs IG1haWxpbmcgbGlzdApYZW4tZGV2ZWxAbGlzdHMueGVuLm9yZwpodHRwczovL2xpc3RzLnhlbi5v cmcveGVuLWRldmVsCg== --------------F1B72B5C7B55F956307DD763--