]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/unittests/search-memory-selftests.c
Automatic Copyright Year update after running gdb/copyright.py
[thirdparty/binutils-gdb.git] / gdb / unittests / search-memory-selftests.c
1 /* Self tests for simple_search_memory for GDB, the GNU debugger.
2
3 Copyright (C) 2020-2022 Free Software Foundation, Inc.
4
5 This file is part of GDB.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19
20 #include "gdbsupport/common-defs.h"
21 #include "gdbsupport/selftest.h"
22 #include "gdbsupport/search.h"
23
24 namespace selftests {
25 namespace search_memory_tests {
26
27 static void
28 run_tests ()
29 {
30 size_t size = 2 * SEARCH_CHUNK_SIZE + 1;
31
32 std::vector<gdb_byte> data (size);
33 data[size - 1] = 'x';
34
35 bool read_fully = false;
36 bool read_off_end = false;
37 auto read_memory = [&] (CORE_ADDR from, gdb_byte *out, size_t len)
38 {
39 if (from + len > data.size ())
40 read_off_end = true;
41 else
42 memcpy (out, &data[from], len);
43 if (from + len == data.size ())
44 read_fully = true;
45 return true;
46 };
47
48 gdb_byte pattern = 'x';
49
50 CORE_ADDR addr = 0;
51 int result = simple_search_memory (read_memory, 0, data.size (),
52 &pattern, 1, &addr);
53 /* In this case we don't care if read_fully was set or not. */
54 SELF_CHECK (result == 1);
55 SELF_CHECK (!read_off_end);
56 SELF_CHECK (addr == size - 1);
57
58 addr = 0;
59 read_fully = false;
60 read_off_end = false;
61 pattern = 'q';
62 result = simple_search_memory (read_memory, 0, data.size (),
63 &pattern, 1, &addr);
64 SELF_CHECK (result == 0);
65 SELF_CHECK (!read_off_end);
66 SELF_CHECK (read_fully);
67 SELF_CHECK (addr == 0);
68
69 /* Setup from PR gdb/17756. */
70 size = 0x7bb00;
71 data = std::vector<gdb_byte> (size);
72 const CORE_ADDR base_addr = 0x08370000;
73 const gdb_byte wpattern[] = { 0x90, 0x8b, 0x98, 0x8 };
74 const CORE_ADDR found_addr = 0x837bac8;
75 memcpy (&data[found_addr - base_addr], wpattern, sizeof (wpattern));
76
77 auto read_memory_2 = [&] (CORE_ADDR from, gdb_byte *out, size_t len)
78 {
79 memcpy (out, &data[from - base_addr], len);
80 return true;
81 };
82
83 result = simple_search_memory (read_memory_2, base_addr, data.size (),
84 wpattern, sizeof (wpattern), &addr);
85 SELF_CHECK (result == 1);
86 SELF_CHECK (addr == found_addr);
87 }
88
89 } /* namespace search_memory_tests */
90 } /* namespace selftests */
91
92
93 void _initialize_search_memory_selftests ();
94 void
95 _initialize_search_memory_selftests ()
96 {
97 selftests::register_test ("search_memory",
98 selftests::search_memory_tests::run_tests);
99 }