From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mx0a-001b2d01.pphosted.com (mx0a-001b2d01.pphosted.com [148.163.156.1]) by sourceware.org (Postfix) with ESMTPS id 84617396DC38 for ; Thu, 2 Jun 2022 13:36:22 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 84617396DC38 Received: from pps.filterd (m0098410.ppops.net [127.0.0.1]) by mx0a-001b2d01.pphosted.com (8.17.1.5/8.17.1.5) with ESMTP id 252CmuQU010364; Thu, 2 Jun 2022 13:36:19 GMT Received: from pps.reinject (localhost [127.0.0.1]) by mx0a-001b2d01.pphosted.com (PPS) with ESMTPS id 3geub93kkp-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Thu, 02 Jun 2022 13:36:19 +0000 Received: from m0098410.ppops.net (m0098410.ppops.net [127.0.0.1]) by pps.reinject (8.17.1.5/8.17.1.5) with ESMTP id 252DEV0G020733; Thu, 2 Jun 2022 13:36:19 GMT Received: from ppma05fra.de.ibm.com (6c.4a.5195.ip4.static.sl-reverse.com [149.81.74.108]) by mx0a-001b2d01.pphosted.com (PPS) with ESMTPS id 3geub93kk2-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Thu, 02 Jun 2022 13:36:18 +0000 Received: from pps.filterd (ppma05fra.de.ibm.com [127.0.0.1]) by ppma05fra.de.ibm.com (8.16.1.2/8.16.1.2) with SMTP id 252DYnHm021112; Thu, 2 Jun 2022 13:36:16 GMT Received: from b06cxnps3074.portsmouth.uk.ibm.com (d06relay09.portsmouth.uk.ibm.com [9.149.109.194]) by ppma05fra.de.ibm.com with ESMTP id 3gbc97ddcr-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Thu, 02 Jun 2022 13:36:16 +0000 Received: from d06av23.portsmouth.uk.ibm.com (d06av23.portsmouth.uk.ibm.com [9.149.105.59]) by b06cxnps3074.portsmouth.uk.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id 252DaDbC49217974 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Thu, 2 Jun 2022 13:36:13 GMT Received: from d06av23.portsmouth.uk.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 62774A4053; Thu, 2 Jun 2022 13:36:13 +0000 (GMT) Received: from d06av23.portsmouth.uk.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 0D230A4040; Thu, 2 Jun 2022 13:36:13 +0000 (GMT) Received: from heavy.ibmuc.com (unknown [9.171.42.87]) by d06av23.portsmouth.uk.ibm.com (Postfix) with ESMTP; Thu, 2 Jun 2022 13:36:12 +0000 (GMT) From: Ilya Leoshkevich To: Tom Tromey , Andrew Burgess , Pedro Alves Cc: Andreas Arnez , gdb-patches@sourceware.org, Ilya Leoshkevich Subject: [PATCH 4/5] gdbsupport: Add interval_tree fuzzing harness Date: Thu, 2 Jun 2022 15:35:45 +0200 Message-Id: <20220602133546.2948282-5-iii@linux.ibm.com> X-Mailer: git-send-email 2.35.3 In-Reply-To: <20220602133546.2948282-1-iii@linux.ibm.com> References: <20220602133546.2948282-1-iii@linux.ibm.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-TM-AS-GCONF: 00 X-Proofpoint-GUID: P5CTNpK_6XDkCRvnlKXr3A9g8ZA355dE X-Proofpoint-ORIG-GUID: ZThJm6kKjwd77ynJ_apBYifswDcTWjT7 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.205,Aquarius:18.0.874,Hydra:6.0.517,FMLib:17.11.64.514 definitions=2022-06-02_03,2022-06-02_01,2022-02-23_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 mlxscore=0 suspectscore=0 phishscore=0 impostorscore=0 spamscore=0 adultscore=0 lowpriorityscore=0 clxscore=1015 bulkscore=0 priorityscore=1501 malwarescore=0 mlxlogscore=857 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2204290000 definitions=main-2206020059 X-Spam-Status: No, score=-11.8 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_MSPIKE_H2, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gdb-patches@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 02 Jun 2022 13:36:24 -0000 Add a libFuzzer harness that exercises additions, removals and lookups. --- gdb/unittests/interval_tree-selftests.c | 137 ++++++++++++++++++++++++ 1 file changed, 137 insertions(+) diff --git a/gdb/unittests/interval_tree-selftests.c b/gdb/unittests/interval_tree-selftests.c index 98a3f4c15bd..85cf376bbd0 100644 --- a/gdb/unittests/interval_tree-selftests.c +++ b/gdb/unittests/interval_tree-selftests.c @@ -23,6 +23,12 @@ #include #include +#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION +#include +#undef gdb_assert +#define gdb_assert assert +#endif + #include "gdbsupport/interval_tree.h" /* A test type for storing in an interval tree. Interval tree must be able to @@ -259,5 +265,136 @@ void _initialize_interval_tree_selftests (); void _initialize_interval_tree_selftests () { +#ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION selftests::register_test ("interval_tree", test_interval_tree); +#endif +} + +/* Fuzzing harness. */ + +class FuzzerInput +{ +public: + FuzzerInput (const unsigned char *data, size_t size) + : m_data (data), m_size (size) + { + } + + bool + end () const + { + return m_size == 0; + } + + template + T + get () + { + T result = 0; + for (size_t i = 0; i < sizeof (T); i++) + result |= read_byte () << (i * 8); + return result; + } + +private: + unsigned char + read_byte () + { + if (end ()) + return 0; + m_data++; + m_size--; + return m_data[-1]; + } + + const unsigned char *m_data; + size_t m_size; +}; + +extern "C" int LLVMFuzzerTestOneInput (const unsigned char *, size_t); +extern "C" int +LLVMFuzzerTestOneInput (const unsigned char *data, size_t size) +{ + FuzzerInput input (data, size); + interval_tree t; + std::vector > t_iterators; + std::multiset exp; + std::vector exp_iterators; + size_t add_counter = 0; + + static const char *debug_str = getenv ("DEBUG"); + static int debug = debug_str == nullptr ? 0 : atoi (debug_str); + + while (!input.end ()) + { + switch (input.get () % 3) + { + case 0: + { + /* Add. */ + int low = input.get (), high = input.get (); + if (low > high) + std::swap (low, high); + if (debug) + std::cout << "auto it" << add_counter << " = check_emplace (t, " + << low << ", " << high << ");" << std::endl; + t_iterators.push_back ( + std::make_pair (t.emplace (low, high), add_counter)); + if (debug) + std::cout << "/*\n" << t << "*/" << std::endl; + else + std::ostringstream () << t; + exp_iterators.push_back (exp.emplace (low, high)); + add_counter += 1; + break; + } + case 1: + { + /* Find. */ + int low = input.get (), high = input.get (); + if (low > high) + std::swap (low, high); + if (debug) + std::cout << "check_iterator (t.find (" << low << ", " << high + << "), t.end ()" << std::flush; + auto it = t.find (low, high); + for (const test_interval &exp_interval : exp) + { + if (high < exp_interval.low || low > exp_interval.high) + continue; + if (debug) + std::cout << ", " << exp_interval.low << ", " + << exp_interval.high << std::flush; + gdb_assert (it->low == exp_interval.low + && it->high == exp_interval.high); + ++it; + } + if (debug) + std::cout << ");" << std::endl; + gdb_assert (it == t.end ()); + break; + } + case 2: + { + /* Remove. */ + if (!t_iterators.empty ()) + { + int index = input.get () % t_iterators.size (); + if (debug) + std::cout << "check_erase (t, it" + << t_iterators[index].second << ");" << std::endl; + t.erase (t_iterators[index].first); + t_iterators.erase (t_iterators.begin () + index); + exp.erase (exp_iterators[index]); + exp_iterators.erase (exp_iterators.begin () + index); + if (debug) + std::cout << "/*\n" << t << "*/" << std::endl; + else + std::ostringstream () << t; + } + break; + } + } + } + return 0; } -- 2.35.3