From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from rock.gnat.com (rock.gnat.com [205.232.38.15]) by sourceware.org (Postfix) with ESMTP id 058243972455 for ; Fri, 25 Sep 2020 19:49:17 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 058243972455 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=adacore.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=tromey@adacore.com Received: from localhost (localhost.localdomain [127.0.0.1]) by filtered-rock.gnat.com (Postfix) with ESMTP id D7615117ACF; Fri, 25 Sep 2020 15:49:16 -0400 (EDT) X-Virus-Scanned: Debian amavisd-new at gnat.com Received: from rock.gnat.com ([127.0.0.1]) by localhost (rock.gnat.com [127.0.0.1]) (amavisd-new, port 10024) with LMTP id Btr4CRX8WqKQ; Fri, 25 Sep 2020 15:49:16 -0400 (EDT) Received: from murgatroyd.Home (184-96-226-199.hlrn.qwest.net [184.96.226.199]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by rock.gnat.com (Postfix) with ESMTPSA id 722ED117A56; Fri, 25 Sep 2020 15:49:16 -0400 (EDT) From: Tom Tromey To: gdb-patches@sourceware.org Cc: Tom Tromey Subject: [PATCH v2 6/6] Add simple_search_memory unit tests Date: Fri, 25 Sep 2020 13:49:13 -0600 Message-Id: <20200925194913.1744541-7-tromey@adacore.com> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20200925194913.1744541-1-tromey@adacore.com> References: <20200925194913.1744541-1-tromey@adacore.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-10.8 required=5.0 tests=BAYES_00, GIT_PATCH_0, KAM_DMARC_STATUS, KAM_SHORT, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) 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: Fri, 25 Sep 2020 19:49:18 -0000 This adds some unit tests for simple_search_memory. I tried here to reproduce some bugs (PR gdb/11158 and PR gdb/17756), but was unable to. 2020-09-25 Tom Tromey * unittests/search-memory-selftests.c: New file. * Makefile.in (SELFTESTS_SRCS): Add unittests/search-memory-selftests.c. --- gdb/ChangeLog | 6 ++ gdb/Makefile.in | 1 + gdb/unittests/search-memory-selftests.c | 99 +++++++++++++++++++++++++ 3 files changed, 106 insertions(+) create mode 100644 gdb/unittests/search-memory-selftests.c diff --git a/gdb/Makefile.in b/gdb/Makefile.in index dbede7a9cfc..fcc518045b4 100644 --- a/gdb/Makefile.in +++ b/gdb/Makefile.in @@ -454,6 +454,7 @@ SELFTESTS_SRCS = \ unittests/scoped_fd-selftests.c \ unittests/scoped_mmap-selftests.c \ unittests/scoped_restore-selftests.c \ + unittests/search-memory-selftests.c \ unittests/string_view-selftests.c \ unittests/style-selftests.c \ unittests/tracepoint-selftests.c \ diff --git a/gdb/unittests/search-memory-selftests.c b/gdb/unittests/search-memory-selftests.c new file mode 100644 index 00000000000..5d2a1b9f906 --- /dev/null +++ b/gdb/unittests/search-memory-selftests.c @@ -0,0 +1,99 @@ +/* Self tests for simple_search_memory for GDB, the GNU debugger. + + Copyright (C) 2020 Free Software Foundation, Inc. + + This file is part of GDB. + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . */ + +#include "gdbsupport/common-defs.h" +#include "gdbsupport/selftest.h" +#include "gdbsupport/search.h" + +namespace selftests { +namespace search_memory_tests { + +static void +run_tests () +{ + size_t size = 2 * SEARCH_CHUNK_SIZE + 1; + + std::vector data (size); + data[size - 1] = 'x'; + + bool read_fully = false; + bool read_off_end = false; + auto read_memory = [&] (CORE_ADDR from, gdb_byte *out, size_t len) + { + if (from + len > data.size ()) + read_off_end = true; + else + memcpy (out, &data[from], len); + if (from + len == data.size ()) + read_fully = true; + return true; + }; + + gdb_byte pattern = 'x'; + + CORE_ADDR addr = 0; + int result = simple_search_memory (read_memory, 0, data.size (), + &pattern, 1, &addr); + /* In this case we don't care if read_fully was set or not. */ + SELF_CHECK (result == 1); + SELF_CHECK (!read_off_end); + SELF_CHECK (addr == size - 1); + + addr = 0; + read_fully = false; + read_off_end = false; + pattern = 'q'; + result = simple_search_memory (read_memory, 0, data.size (), + &pattern, 1, &addr); + SELF_CHECK (result == 0); + SELF_CHECK (!read_off_end); + SELF_CHECK (read_fully); + SELF_CHECK (addr == 0); + + /* Setup from PR gdb/17756. */ + size = 0x7bb00; + data = std::vector (size); + const CORE_ADDR base_addr = 0x08370000; + const gdb_byte wpattern[] = { 0x90, 0x8b, 0x98, 0x8 }; + const CORE_ADDR found_addr = 0x837bac8; + memcpy (&data[found_addr - base_addr], wpattern, sizeof (wpattern)); + + auto read_memory_2 = [&] (CORE_ADDR from, gdb_byte *out, size_t len) + { + memcpy (out, &data[from - base_addr], len); + return true; + }; + + result = simple_search_memory (read_memory_2, base_addr, data.size (), + wpattern, sizeof (wpattern), &addr); + SELF_CHECK (result == 1); + SELF_CHECK (addr == found_addr); +} + +} /* namespace search_memory_tests */ +} /* namespace selftests */ + + +void _initialize_search_memory_selftests (); +void +_initialize_search_memory_selftests () +{ + selftests::register_test ("search_memory", + selftests::search_memory_tests::run_tests); +} -- 2.26.2