Debian Codegraph @

tests/test_search_memory.py

raw
1 # Copyright (c) Meta Platforms, Inc. and affiliates.
2 # SPDX-License-Identifier: LGPL-2.1-or-later
3
4 import unittest
5
6 import drgn
7 from drgn import (
8 Architecture,
9 Object,
10 Platform,
11 Program,
12 search_memory,
13 search_memory_regex,
14 search_memory_u32,
15 search_memory_u64,
16 search_memory_word,
17 )
18 from tests import (
19 MOCK_32BIT_BIG_ENDIAN_PLATFORM,
20 MOCK_32BIT_PLATFORM,
21 MOCK_BIG_ENDIAN_PLATFORM,
22 MOCK_PLATFORM,
23 IntWrapper,
24 MockMemorySegment,
25 TestCase,
26 add_mock_memory_segments,
27 with_default_prog,
28 )
29
30
31 def mock_search_memory_program(*segments, platform=MOCK_PLATFORM):
32 prog = Program(platform)
33 add_mock_memory_segments(prog, segments)
34 return prog
35
36
37 class TestSearchMemoryBytes(TestCase):
38 def test_one_match(self):
39 prog = mock_search_memory_program(MockMemorySegment(b"abcdef", 0x1000))
40 self.assertEqual(list(prog.search_memory(b"cde")), [0x1002])
41
42 def test_haystack_embedded_null(self):
43 prog = mock_search_memory_program(MockMemorySegment(b"a\0bcdef\0g", 0x1000))
44 self.assertEqual(list(prog.search_memory(b"cde")), [0x1003])
45
46 def test_needle_embedded_null(self):
47 prog = mock_search_memory_program(MockMemorySegment(b"ab\0c\0d", 0x1000))
48 self.assertEqual(list(prog.search_memory(b"b\0c")), [0x1001])
49
50 def test_multiple_matches(self):
51 prog = mock_search_memory_program(MockMemorySegment(b"abcd abcz", 0x1000))
52 self.assertEqual(list(prog.search_memory(b"abc")), [0x1000, 0x1005])
53
54 def test_non_overlapping(self):
55 prog = mock_search_memory_program(MockMemorySegment(b"aaaa", 0x1000))
56 self.assertEqual(list(prog.search_memory(b"aa")), [0x1000, 0x1002])
57
58 def test_address_range(self):
59 prog = mock_search_memory_program(MockMemorySegment(b"aaaa", 0x1000))
60
61 self.assertEqual(
62 list(prog.search_memory(b"aa").set_address_range(0x1000, 0x1003)),
63 [0x1000, 0x1002],
64 )
65
66 self.assertEqual(
67 list(prog.search_memory(b"aa").set_address_range(0x1001)), [0x1001]
68 )
69
70 self.assertEqual(
71 list(prog.search_memory(b"aa").set_address_range(0x1000, 0x1000)), []
72 )
73
74 def test_address_range_reuse(self):
75 prog = mock_search_memory_program(MockMemorySegment(b"aaaa", 0x1000))
76
77 it = prog.search_memory(b"aa")
78
79 it.set_address_range(0x1000, 0x1003)
80 self.assertEqual(list(it), [0x1000, 0x1002])
81
82 it.set_address_range(0x1001)
83 self.assertEqual(list(it), [0x1001])
84
85 it.set_address_range(0x1000, 0x1000)
86 self.assertEqual(list(it), [])
87
88 def test_invalid_address_range(self):
89 self.assertRaises(
90 ValueError,
91 mock_search_memory_program().search_memory(b"foo").set_address_range,
92 0x1000,
93 0xFFF,
94 )
95
96 def test_cross_boundary(self):
97 prog = mock_search_memory_program(MockMemorySegment(b"foo bar", 0x3FFFFFFA))
98 self.assertEqual(list(prog.search_memory(b"bar")), [0x3FFFFFFE])
99
100 def test_gap(self):
101 prog = mock_search_memory_program(
102 MockMemorySegment(b"abcdefgh", 0x1000),
103 MockMemorySegment(b"zcdevwxy", 0x1104),
104 )
105 self.assertEqual(list(prog.search_memory(b"cde")), [0x1002, 0x1105])
106
107 def test_at_max_address(self):
108 prog = mock_search_memory_program(
109 MockMemorySegment(b"aaaa", 0xFFFFFFFFFFFFFFFC)
110 )
111 self.assertEqual(
112 list(prog.search_memory(b"aa")), [0xFFFFFFFFFFFFFFFC, 0xFFFFFFFFFFFFFFFE]
113 )
114
115 def test_one_byte_at_max_address(self):
116 prog = mock_search_memory_program(MockMemorySegment(b"a", 0xFFFFFFFFFFFFFFFF))
117 self.assertEqual(list(prog.search_memory(b"a")), [0xFFFFFFFFFFFFFFFF])
118
119 def test_at_max_address_32_bit(self):
120 prog = mock_search_memory_program(
121 MockMemorySegment(b"aaaa", 0xFFFFFFFC),
122 platform=MOCK_32BIT_PLATFORM,
123 )
124 self.assertEqual(list(prog.search_memory(b"aa")), [0xFFFFFFFC, 0xFFFFFFFE])
125
126 def test_one_byte_at_max_address_32_bit(self):
127 prog = mock_search_memory_program(
128 MockMemorySegment(b"a", 0xFFFFFFFF),
129 platform=MOCK_32BIT_PLATFORM,
130 )
131 self.assertEqual(list(prog.search_memory(b"a")), [0xFFFFFFFF])
132
133 def test_alignment(self):
134 prog = mock_search_memory_program(
135 MockMemorySegment(b"aaaaaaaa", 0xFFC),
136 )
137 self.assertEqual(
138 list(prog.search_memory(b"aa", alignment=8)),
139 [0x1000],
140 )
141
142 def test_alignment_at_max_address(self):
143 prog = mock_search_memory_program(
144 MockMemorySegment(b"a" * 12, 0xFFFFFFFFFFFFFFF4),
145 )
146 self.assertEqual(
147 list(prog.search_memory(b"aa", alignment=8)),
148 [0xFFFFFFFFFFFFFFF8],
149 )
150
151 def test_u32(self):
152 prog = mock_search_memory_program(
153 MockMemorySegment(
154 b"".join([i.to_bytes(4, "little") for i in range(1, 4)]), 0x1000
155 ),
156 )
157 self.assertEqual(
158 list(prog.search_memory((2).to_bytes(4, "little"), alignment=4)), [0x1004]
159 )
160
161 def test_u32_big_endian(self):
162 prog = mock_search_memory_program(
163 MockMemorySegment(
164 b"".join([i.to_bytes(4, "big") for i in range(1, 4)]), 0x1000
165 ),
166 platform=MOCK_BIG_ENDIAN_PLATFORM,
167 )
168 self.assertEqual(
169 list(prog.search_memory((2).to_bytes(4, "big"), alignment=4)), [0x1004]
170 )
171
172 def test_u64(self):
173 prog = mock_search_memory_program(
174 MockMemorySegment(
175 b"".join([i.to_bytes(8, "little") for i in range(1, 4)]), 0x1000
176 ),
177 )
178 self.assertEqual(
179 list(prog.search_memory((2).to_bytes(8, "little"), alignment=8)), [0x1008]
180 )
181
182 def test_u64_big_endian(self):
183 prog = mock_search_memory_program(
184 MockMemorySegment(
185 b"".join([i.to_bytes(8, "big") for i in range(1, 4)]), 0x1000
186 ),
187 platform=MOCK_BIG_ENDIAN_PLATFORM,
188 )
189 self.assertEqual(
190 list(prog.search_memory((2).to_bytes(8, "big"), alignment=8)), [0x1008]
191 )
192
193 def test_no_segments(self):
194 prog = mock_search_memory_program()
195 self.assertEqual(list(prog.search_memory(b"foo")), [])
196
197 def test_empty(self):
198 with self.assertRaisesRegex(ValueError, "needle cannot be empty"):
199 next(mock_search_memory_program().search_memory(b""))
200
201 def test_invalid_alignment(self):
202 with self.assertRaisesRegex(ValueError, "alignment must be power of 2"):
203 next(mock_search_memory_program().search_memory(b"foo", alignment=3))
204
205 def test_invalid_needle_type(self):
206 self.assertRaises(TypeError, mock_search_memory_program().search_memory, {})
207
208 def test_default_program(self):
209 with with_default_prog(
210 mock_search_memory_program(MockMemorySegment(b"abcdef", 0x1000))
211 ):
212 self.assertEqual(list(search_memory(b"cde")), [0x1002])
213
214
215 class TestSearchMemoryStr(TestCase):
216 def test_valid_utf8(self):
217 prog = mock_search_memory_program(MockMemorySegment(b"abcdef", 0x1000))
218 self.assertEqual(list(prog.search_memory("cde")), [0x1002])
219
220 def test_haystack_embedded_null(self):
221 prog = mock_search_memory_program(MockMemorySegment(b"a\0bcdef\0g", 0x1000))
222 self.assertEqual(list(prog.search_memory("cde")), [0x1003])
223
224 def test_needle_embedded_null(self):
225 prog = mock_search_memory_program(MockMemorySegment(b"ab\0c\0d", 0x1000))
226 self.assertEqual(list(prog.search_memory("b\0c")), [0x1001])
227
228 def test_invalid_utf8(self):
229 prog = mock_search_memory_program(
230 MockMemorySegment(b"\xc3\x28abcdef\xa0\xa1", 0x1000)
231 )
232 self.assertEqual(list(prog.search_memory("cde")), [0x1004])
233
234 def test_empty(self):
235 with self.assertRaisesRegex(ValueError, "empty"):
236 next(mock_search_memory_program().search_memory(""))
237
238 def test_alignment(self):
239 prog = mock_search_memory_program(
240 MockMemorySegment(b"aaaaaaaa", 0xFFC),
241 )
242 self.assertEqual(
243 list(prog.search_memory("aa", alignment=8)),
244 [0x1000],
245 )
246
247 def test_invalid_alignment(self):
248 with self.assertRaisesRegex(ValueError, "alignment"):
249 next(mock_search_memory_program().search_memory("foo", alignment=3))
250
251 def test_default_program(self):
252 with with_default_prog(
253 mock_search_memory_program(MockMemorySegment(b"abcdef", 0x1000))
254 ):
255 self.assertEqual(list(search_memory("cde")), [0x1002])
256
257
258 class TestSearchMemoryInt(TestCase):
259 def test_64_bit(self):
260 prog = mock_search_memory_program(
261 MockMemorySegment(
262 b"\x02\x00\x00\x00\x02\x00\x00\x00\x02\x00\x00\x00\x00\x00\x00\x00",
263 0x1000,
264 ),
265 )
266 self.assertEqual(list(prog.search_memory(2)), [0x1008])
267
268 def test_64_bit_big_endian(self):
269 prog = mock_search_memory_program(
270 MockMemorySegment(
271 b"\x00\x00\x00\x02\x00\x00\x00\x02\x00\x00\x00\x00\x00\x00\x00\x02",
272 0x1000,
273 ),
274 platform=MOCK_BIG_ENDIAN_PLATFORM,
275 )
276 self.assertEqual(list(prog.search_memory(2)), [0x1008])
277
278 def test_32_bit(self):
279 prog = mock_search_memory_program(
280 MockMemorySegment(
281 b"\x02\x00\x00\x00\x02\x00\x00\x00\x02\x00\x00\x00\x00\x00\x00\x00",
282 0x1000,
283 ),
284 platform=MOCK_32BIT_PLATFORM,
285 )
286 self.assertEqual(list(prog.search_memory(2)), [0x1000, 0x1004, 0x1008])
287
288 def test_32_bit_big_endian(self):
289 prog = mock_search_memory_program(
290 MockMemorySegment(
291 b"\x00\x00\x00\x02\x00\x00\x00\x02\x00\x00\x00\x00\x00\x00\x00\x02",
292 0x1000,
293 ),
294 platform=MOCK_32BIT_BIG_ENDIAN_PLATFORM,
295 )
296 self.assertEqual(list(prog.search_memory(2)), [0x1000, 0x1004, 0x100C])
297
298 def test_index(self):
299 prog = mock_search_memory_program(
300 MockMemorySegment(
301 b"".join([i.to_bytes(8, "little") for i in range(1, 4)]), 0x1000
302 ),
303 )
304 self.assertEqual(list(prog.search_memory(IntWrapper(2))), [0x1008])
305
306 def test_invalid_alignment(self):
307 with self.assertRaisesRegex(TypeError, "alignment"):
308 next(mock_search_memory_program().search_memory(0, alignment=3))
309
310 def test_default_program(self):
311 with with_default_prog(
312 mock_search_memory_program(
313 MockMemorySegment(
314 b"\x02\x00\x00\x00\x02\x00\x00\x00\x02\x00\x00\x00\x00\x00\x00\x00",
315 0x1000,
316 ),
317 )
318 ):
319 self.assertEqual(list(search_memory(2)), [0x1008])
320
321
322 # Note that these tests use actual architectures so that they have alignment
323 # information.
324 class TestSearchMemoryObject(TestCase):
325 def test_u32(self):
326 prog = mock_search_memory_program(
327 MockMemorySegment(
328 b"".join([i.to_bytes(4, "little") for i in range(1, 4)]), 0x1000
329 ),
330 platform=Platform(Architecture.X86_64),
331 )
332 self.assertEqual(list(prog.search_memory(Object(prog, "int", 2))), [0x1004])
333
334 def test_u32_big_endian(self):
335 prog = mock_search_memory_program(
336 MockMemorySegment(
337 b"".join([i.to_bytes(4, "big") for i in range(1, 4)]), 0x1000
338 ),
339 platform=Platform(Architecture.S390X),
340 )
341 self.assertEqual(list(prog.search_memory(Object(prog, "int", 2))), [0x1004])
342
343 def test_u64(self):
344 prog = mock_search_memory_program(
345 MockMemorySegment(
346 b"".join([i.to_bytes(8, "little") for i in range(1, 4)]), 0x1000
347 ),
348 platform=Platform(Architecture.X86_64),
349 )
350 self.assertEqual(list(prog.search_memory(Object(prog, "long", 2))), [0x1008])
351
352 def test_u64_big_endian(self):
353 prog = mock_search_memory_program(
354 MockMemorySegment(
355 b"".join([i.to_bytes(8, "big") for i in range(1, 4)]), 0x1000
356 ),
357 platform=Platform(Architecture.S390X),
358 )
359 self.assertEqual(list(prog.search_memory(Object(prog, "long", 2))), [0x1008])
360
361 def test_small_object(self):
362 prog = mock_search_memory_program(
363 MockMemorySegment(b"\x01\x02\x03\x04", 0x1000),
364 platform=Platform(Architecture.X86_64),
365 )
366 self.assertEqual(list(prog.search_memory(Object(prog, "char", 2))), [0x1001])
367
368 def test_large_object(self):
369 prog = mock_search_memory_program(
370 MockMemorySegment(
371 b"".join([i.to_bytes(8, "little") for i in range(1, 9)]), 0x1000
372 ),
373 platform=Platform(Architecture.X86_64),
374 )
375 self.assertEqual(
376 list(prog.search_memory(Object(prog, "long [4]", [2, 3, 4, 5]))), [0x1008]
377 )
378
379 def test_alignment(self):
380 prog = mock_search_memory_program(
381 MockMemorySegment(b"\x02\x00\x00\x00" * 7, 0xFFC),
382 platform=Platform(Architecture.X86_64),
383 )
384 self.assertEqual(
385 list(prog.search_memory(Object(prog, "long long [3]", [0x200000002] * 3))),
386 [0x1000],
387 )
388
389 prog = mock_search_memory_program(
390 MockMemorySegment(b"\x02\x00\x00\x00" * 7, 0xFFC),
391 platform=Platform(Architecture.I386),
392 )
393 self.assertEqual(
394 list(prog.search_memory(Object(prog, "long long [3]", [0x200000002] * 3))),
395 [0xFFC],
396 )
397
398 def test_empty(self):
399 prog = mock_search_memory_program(platform=Platform(Architecture.X86_64))
400 with self.assertRaisesRegex(ValueError, "size 0"):
401 prog.search_memory(Object(prog, "int [0]", address=0x1000))
402
403 def test_invalid_alignment(self):
404 prog = mock_search_memory_program(platform=Platform(Architecture.X86_64))
405 with self.assertRaisesRegex(TypeError, "alignment"):
406 next(prog.search_memory(Object(prog, "int", 0), alignment=3))
407
408 def test_default_program(self):
409 prog = mock_search_memory_program(
410 MockMemorySegment(
411 b"".join([i.to_bytes(4, "little") for i in range(1, 4)]), 0x1000
412 ),
413 platform=Platform(Architecture.X86_64),
414 )
415 self.assertEqual(list(search_memory(Object(prog, "int", 2))), [0x1004])
416
417 def test_out_of_range(self):
418 with self.assertRaises(OverflowError):
419 next(
420 mock_search_memory_program(platform=MOCK_32BIT_PLATFORM).search_memory(
421 2**32
422 )
423 )
424 self.assertEqual(list(mock_search_memory_program().search_memory(2**32)), [])
425 with self.assertRaises(OverflowError):
426 next(mock_search_memory_program().search_memory(2**64))
427
428
429 class TestSearchMemoryUint(TestCase):
430 def test_one_u16(self):
431 prog = mock_search_memory_program(
432 MockMemorySegment(
433 b"".join([i.to_bytes(2, "little") for i in range(1, 4)]), 0x1000
434 ),
435 )
436 self.assertEqual(list(prog.search_memory_u16(2)), [(0x1002, 2)])
437
438 def test_one_u16_big_endian(self):
439 prog = mock_search_memory_program(
440 MockMemorySegment(
441 b"".join([i.to_bytes(2, "big") for i in range(1, 4)]), 0x1000
442 ),
443 platform=MOCK_BIG_ENDIAN_PLATFORM,
444 )
445 self.assertEqual(list(prog.search_memory_u16(2)), [(0x1002, 2)])
446
447 def test_one_u32(self):
448 prog = mock_search_memory_program(
449 MockMemorySegment(
450 b"".join([i.to_bytes(4, "little") for i in range(1, 4)]), 0x1000
451 ),
452 )
453 self.assertEqual(list(prog.search_memory_u32(2)), [(0x1004, 2)])
454
455 def test_one_u32_big_endian(self):
456 prog = mock_search_memory_program(
457 MockMemorySegment(
458 b"".join([i.to_bytes(4, "big") for i in range(1, 4)]), 0x1000
459 ),
460 platform=MOCK_BIG_ENDIAN_PLATFORM,
461 )
462 self.assertEqual(list(prog.search_memory_u32(2)), [(0x1004, 2)])
463
464 def test_one_u64(self):
465 prog = mock_search_memory_program(
466 MockMemorySegment(
467 b"".join([i.to_bytes(8, "little") for i in range(1, 4)]), 0x1000
468 ),
469 )
470 self.assertEqual(list(prog.search_memory_u64(2)), [(0x1008, 2)])
471
472 def test_one_u64_big_endian(self):
473 prog = mock_search_memory_program(
474 MockMemorySegment(
475 b"".join([i.to_bytes(8, "big") for i in range(1, 4)]), 0x1000
476 ),
477 platform=MOCK_BIG_ENDIAN_PLATFORM,
478 )
479 self.assertEqual(list(prog.search_memory_u64(2)), [(0x1008, 2)])
480
481 def test_word_64_bit(self):
482 prog = mock_search_memory_program(
483 MockMemorySegment(
484 b"\x02\x00\x00\x00\x02\x00\x00\x00\x02\x00\x00\x00\x00\x00\x00\x00",
485 0x1000,
486 ),
487 )
488 self.assertEqual(list(prog.search_memory_word(2)), [(0x1008, 2)])
489
490 def test_word_32_bit(self):
491 prog = mock_search_memory_program(
492 MockMemorySegment(
493 b"\x02\x00\x00\x00\x02\x00\x00\x00\x02\x00\x00\x00\x00\x00\x00\x00",
494 0x1000,
495 ),
496 platform=MOCK_32BIT_PLATFORM,
497 )
498 self.assertEqual(
499 list(prog.search_memory_word(2)), [(0x1000, 2), (0x1004, 2), (0x1008, 2)]
500 )
501
502 def test_multiple_u16(self):
503 prog = mock_search_memory_program(
504 MockMemorySegment(
505 b"".join([i.to_bytes(2, "little") for i in range(1, 5)]), 0x1000
506 ),
507 )
508 self.assertEqual(list(prog.search_memory_u16(2, 4)), [(0x1002, 2), (0x1006, 4)])
509
510 def test_multiple_u16_big_endian(self):
511 prog = mock_search_memory_program(
512 MockMemorySegment(
513 b"".join([i.to_bytes(2, "big") for i in range(1, 5)]), 0x1000
514 ),
515 platform=MOCK_BIG_ENDIAN_PLATFORM,
516 )
517 self.assertEqual(list(prog.search_memory_u16(2, 4)), [(0x1002, 2), (0x1006, 4)])
518
519 def test_multiple_u32(self):
520 prog = mock_search_memory_program(
521 MockMemorySegment(
522 b"".join([i.to_bytes(4, "little") for i in range(1, 5)]), 0x1000
523 ),
524 )
525 self.assertEqual(list(prog.search_memory_u32(2, 4)), [(0x1004, 2), (0x100C, 4)])
526
527 def test_multiple_u32_big_endian(self):
528 prog = mock_search_memory_program(
529 MockMemorySegment(
530 b"".join([i.to_bytes(4, "big") for i in range(1, 5)]), 0x1000
531 ),
532 platform=MOCK_BIG_ENDIAN_PLATFORM,
533 )
534 self.assertEqual(list(prog.search_memory_u32(2, 4)), [(0x1004, 2), (0x100C, 4)])
535
536 def test_multiple_u64(self):
537 prog = mock_search_memory_program(
538 MockMemorySegment(
539 b"".join([i.to_bytes(8, "little") for i in range(1, 5)]), 0x1000
540 ),
541 )
542 self.assertEqual(list(prog.search_memory_u64(2, 4)), [(0x1008, 2), (0x1018, 4)])
543
544 def test_multiple_u64_big_endian(self):
545 prog = mock_search_memory_program(
546 MockMemorySegment(
547 b"".join([i.to_bytes(8, "big") for i in range(1, 5)]), 0x1000
548 ),
549 platform=MOCK_BIG_ENDIAN_PLATFORM,
550 )
551 self.assertEqual(list(prog.search_memory_u64(2, 4)), [(0x1008, 2), (0x1018, 4)])
552
553 def test_ignore_mask_u32(self):
554 prog = mock_search_memory_program(
555 MockMemorySegment(
556 b"".join([i.to_bytes(4, "little") for i in range(1, 7)]), 0x1000
557 ),
558 )
559 self.assertEqual(
560 list(prog.search_memory_u32(3, ignore_mask=0x5)),
561 [(0x1004, 2), (0x1008, 3), (0x1014, 6)],
562 )
563
564 def test_ignore_mask_u32_big_endian(self):
565 prog = mock_search_memory_program(
566 MockMemorySegment(
567 b"".join([i.to_bytes(4, "big") for i in range(1, 7)]), 0x1000
568 ),
569 platform=MOCK_BIG_ENDIAN_PLATFORM,
570 )
571 self.assertEqual(
572 list(prog.search_memory_u32(3, ignore_mask=0x5)),
573 [(0x1004, 2), (0x1008, 3), (0x1014, 6)],
574 )
575
576 def test_ignore_mask_u64(self):
577 prog = mock_search_memory_program(
578 MockMemorySegment(
579 b"".join([i.to_bytes(8, "little") for i in range(1, 7)]), 0x1000
580 ),
581 )
582 self.assertEqual(
583 list(prog.search_memory_u64(3, ignore_mask=0x5)),
584 [(0x1008, 2), (0x1010, 3), (0x1028, 6)],
585 )
586
587 def test_ignore_mask_u64_big_endian(self):
588 prog = mock_search_memory_program(
589 MockMemorySegment(
590 b"".join([i.to_bytes(8, "big") for i in range(1, 7)]), 0x1000
591 ),
592 platform=MOCK_BIG_ENDIAN_PLATFORM,
593 )
594 self.assertEqual(
595 list(prog.search_memory_u64(3, ignore_mask=0x5)),
596 [(0x1008, 2), (0x1010, 3), (0x1028, 6)],
597 )
598
599 def test_u32_range(self):
600 prog = mock_search_memory_program(
601 MockMemorySegment(
602 b"".join([i.to_bytes(4, "little") for i in range(1, 5)]), 0x1000
603 ),
604 )
605 self.assertEqual(
606 list(prog.search_memory_u32((2, 3))), [(0x1004, 2), (0x1008, 3)]
607 )
608
609 def test_u32_range_big_endian(self):
610 prog = mock_search_memory_program(
611 MockMemorySegment(
612 b"".join([i.to_bytes(4, "big") for i in range(1, 5)]), 0x1000
613 ),
614 platform=MOCK_BIG_ENDIAN_PLATFORM,
615 )
616 self.assertEqual(
617 list(prog.search_memory_u32((2, 3))), [(0x1004, 2), (0x1008, 3)]
618 )
619
620 def test_u64_range(self):
621 prog = mock_search_memory_program(
622 MockMemorySegment(
623 b"".join([i.to_bytes(8, "little") for i in range(1, 5)]), 0x1000
624 ),
625 )
626 self.assertEqual(
627 list(prog.search_memory_u64((2, 3))), [(0x1008, 2), (0x1010, 3)]
628 )
629
630 def test_u64_range_big_endian(self):
631 prog = mock_search_memory_program(
632 MockMemorySegment(
633 b"".join([i.to_bytes(8, "big") for i in range(1, 5)]), 0x1000
634 ),
635 platform=MOCK_BIG_ENDIAN_PLATFORM,
636 )
637 self.assertEqual(
638 list(prog.search_memory_u64((2, 3))), [(0x1008, 2), (0x1010, 3)]
639 )
640
641 def test_combo(self):
642 prog = mock_search_memory_program(
643 MockMemorySegment(
644 b"".join([i.to_bytes(8, "little") for i in range(1, 7)]), 0x1000
645 ),
646 )
647 self.assertEqual(
648 list(
649 prog.search_memory_u64(
650 1, (2, 3), 6, (4, 4), (100, 200), ignore_mask=0x4
651 )
652 ),
653 [
654 (0x1000, 1),
655 (0x1008, 2),
656 (0x1010, 3),
657 (0x1018, 4),
658 (0x1020, 5),
659 (0x1028, 6),
660 ],
661 )
662
663 def test_index(self):
664 prog = mock_search_memory_program(
665 MockMemorySegment(
666 b"\x03\x00\x00\x00\x00\x00\x00\x00\x04\x00\x00\x00\x00\x00\x00\x00",
667 0x1000,
668 ),
669 )
670 self.assertEqual(
671 list(
672 prog.search_memory_u32(
673 IntWrapper(2),
674 (IntWrapper(4), IntWrapper(5)),
675 ignore_mask=IntWrapper(1),
676 )
677 ),
678 [(0x1000, 3), (0x1008, 4)],
679 )
680 self.assertEqual(
681 list(
682 prog.search_memory_u64(
683 IntWrapper(2),
684 (IntWrapper(4), IntWrapper(5)),
685 ignore_mask=IntWrapper(1),
686 )
687 ),
688 [(0x1000, 3), (0x1008, 4)],
689 )
690 self.assertEqual(
691 list(
692 prog.search_memory_word(
693 IntWrapper(2),
694 (IntWrapper(4), IntWrapper(5)),
695 ignore_mask=IntWrapper(1),
696 )
697 ),
698 [(0x1000, 3), (0x1008, 4)],
699 )
700
701 def test_default_program(self):
702 with with_default_prog(
703 mock_search_memory_program(
704 MockMemorySegment(
705 b"\x03\x00\x00\x00\x00\x00\x00\x00\x04\x00\x00\x00\x00\x00\x00\x00",
706 0x1000,
707 ),
708 )
709 ):
710 self.assertEqual(
711 list(search_memory_u32(2, (4, 5), ignore_mask=1)),
712 [(0x1000, 3), (0x1008, 4)],
713 )
714 self.assertEqual(
715 list(search_memory_u64(2, (4, 5), ignore_mask=1)),
716 [(0x1000, 3), (0x1008, 4)],
717 )
718 self.assertEqual(
719 list(search_memory_word(2, (4, 5), ignore_mask=1)),
720 [(0x1000, 3), (0x1008, 4)],
721 )
722
723 def test_out_of_range(self):
724 with self.assertRaises(OverflowError):
725 next(mock_search_memory_program().search_memory_u32(2**32))
726 with self.assertRaises(OverflowError):
727 next(mock_search_memory_program().search_memory_u32((0, 2**32)))
728 with self.assertRaises(OverflowError):
729 next(mock_search_memory_program().search_memory_u32((2**32, 0)))
730 with self.assertRaises(OverflowError):
731 next(mock_search_memory_program().search_memory_u32(0, ignore_mask=2**32))
732 with self.assertRaises((ValueError, OverflowError)):
733 next(mock_search_memory_program().search_memory_u32(-1))
734
735 with self.assertRaises(OverflowError):
736 next(mock_search_memory_program().search_memory_u64(2**64))
737 with self.assertRaises(OverflowError):
738 next(mock_search_memory_program().search_memory_u64((0, 2**64)))
739 with self.assertRaises(OverflowError):
740 next(mock_search_memory_program().search_memory_u64((2**64, 0)))
741 with self.assertRaises(OverflowError):
742 next(mock_search_memory_program().search_memory_u64(0, ignore_mask=2**64))
743 with self.assertRaises((ValueError, OverflowError)):
744 next(mock_search_memory_program().search_memory_u64(-1))
745
746 with self.assertRaises(OverflowError):
747 next(
748 mock_search_memory_program(
749 platform=MOCK_32BIT_PLATFORM
750 ).search_memory_word(2**32)
751 )
752 with self.assertRaises(OverflowError):
753 next(
754 mock_search_memory_program(
755 platform=MOCK_32BIT_PLATFORM
756 ).search_memory_word((0, 2**32))
757 )
758 with self.assertRaises(OverflowError):
759 next(
760 mock_search_memory_program(
761 platform=MOCK_32BIT_PLATFORM
762 ).search_memory_word((2**32, 0))
763 )
764 with self.assertRaises(OverflowError):
765 next(
766 mock_search_memory_program(
767 platform=MOCK_32BIT_PLATFORM
768 ).search_memory_word(0, ignore_mask=2**32)
769 )
770
771 self.assertEqual(
772 list(
773 mock_search_memory_program().search_memory_word(
774 2**32, (2**33, 2**34), ignore_mask=2**35
775 )
776 ),
777 [],
778 )
779
780 with self.assertRaises(OverflowError):
781 next(mock_search_memory_program().search_memory_word(2**64))
782 with self.assertRaises(OverflowError):
783 next(mock_search_memory_program().search_memory_word((0, 2**64)))
784 with self.assertRaises(OverflowError):
785 next(mock_search_memory_program().search_memory_word((2**64, 0)))
786 with self.assertRaises(OverflowError):
787 next(mock_search_memory_program().search_memory_word(0, ignore_mask=2**64))
788
789
790 @unittest.skipUnless(drgn._with_pcre2, "built without pcre2 support")
791 class TestSearchMemoryRegex(TestCase):
792 def test_fixed_size(self):
793 prog = mock_search_memory_program(MockMemorySegment(b"foo bar", 0x1000))
794 self.assertEqual(
795 list(prog.search_memory_regex(rb"foo|bar")),
796 [
797 (0x1000, b"foo"),
798 (0x1004, b"bar"),
799 ],
800 )
801
802 def test_non_overlapping(self):
803 prog = mock_search_memory_program(MockMemorySegment(b"abcd", 0x1000))
804 self.assertEqual(
805 list(prog.search_memory_regex(rb"[a-z]{1,2}")),
806 [(0x1000, b"ab"), (0x1002, b"cd")],
807 )
808
809 def test_variable_size(self):
810 prog = mock_search_memory_program(MockMemorySegment(b"caaaaat", 0x1000))
811 self.assertEqual(
812 list(prog.search_memory_regex(rb"a+")),
813 [(0x1001, b"aaaaa")],
814 )
815
816 def test_gap(self):
817 prog = mock_search_memory_program(
818 MockMemorySegment(b"caaaaa", 0x1000), MockMemorySegment(b"aaaaat", 0x1008)
819 )
820 self.assertEqual(
821 list(prog.search_memory_regex(rb"a+")),
822 [(0x1001, b"aaaaa"), (0x1008, b"aaaaa")],
823 )
824
825 def test_all_empty(self):
826 prog = mock_search_memory_program(MockMemorySegment(b"b", 0x1000))
827 self.assertEqual(
828 list(prog.search_memory_regex(rb"a*")),
829 [
830 (0x1000, b""),
831 (0x1001, b""),
832 ],
833 )
834
835 def test_empty_and_non_empty(self):
836 prog = mock_search_memory_program(MockMemorySegment(b"b", 0x1000))
837 self.assertEqual(
838 list(prog.search_memory_regex(rb"|b")),
839 [
840 (0x1000, b""),
841 (0x1000, b"b"),
842 (0x1001, b""),
843 ],
844 )
845
846 def test_empty_at_gap(self):
847 prog = mock_search_memory_program(
848 MockMemorySegment(b"b", 0x1000),
849 MockMemorySegment(b"c", 0x1008),
850 )
851 self.assertEqual(
852 list(prog.search_memory_regex(rb"a*")),
853 [
854 (0x1000, b""),
855 (0x1001, b""),
856 (0x1008, b""),
857 (0x1009, b""),
858 ],
859 )
860
861 def test_anchor_start(self):
862 prog = mock_search_memory_program(
863 MockMemorySegment(b"abc", 0x0), MockMemorySegment(b"abc", 0x1000)
864 )
865 self.assertEqual(
866 list(prog.search_memory_regex(rb"^abc")),
867 [(0x0, b"abc")],
868 )
869
870 def test_anchor_end(self):
871 prog = mock_search_memory_program(
872 MockMemorySegment(b"abc", 0xFFD),
873 MockMemorySegment(b"abc", 0xFFFFFFFFFFFFFFFD),
874 )
875 self.assertEqual(
876 list(prog.search_memory_regex(rb"abc$")),
877 [(0xFFFFFFFFFFFFFFFD, b"abc")],
878 )
879
880 def test_cross_boundary_fixed_size(self):
881 prog = mock_search_memory_program(MockMemorySegment(b"foo bar", 0x3FFFFFFE))
882 self.assertEqual(
883 list(prog.search_memory_regex(rb"foo|bar")),
884 [
885 (0x3FFFFFFE, b"foo"),
886 (0x40000002, b"bar"),
887 ],
888 )
889
890 def test_cross_boundary_multi_size(self):
891 prog = mock_search_memory_program(MockMemorySegment(b"foo bar", 0x3FFFFFFE))
892 self.assertEqual(
893 list(prog.search_memory_regex(rb"foo|b")),
894 [
895 (0x3FFFFFFE, b"foo"),
896 (0x40000002, b"b"),
897 ],
898 )
899
900 def test_cross_boundary_variable_size(self):
901 prog = mock_search_memory_program(MockMemorySegment(b"foo bar", 0x3FFFFFFE))
902 self.assertEqual(
903 list(prog.search_memory_regex(rb"fo*")),
904 [(0x3FFFFFFE, b"foo")],
905 )
906
907 @unittest.skipUnless(drgn._with_pcre2_utf, "PCRE2 does not support UTF-8")
908 def test_str_valid_utf8(self):
909 # 'ñ' is \xc3\xb1 in UTF-8. '±' is \xc2\xb1 in UTF-8. A Unicode search
910 # for the former shouldn't match the \xb1 byte in the latter.
911 prog = mock_search_memory_program(MockMemorySegment("piñata±".encode(), 0x1000))
912 self.assertEqual(
913 list(prog.search_memory_regex(r"[a-zñ]+")), [(0x1000, "piñata")]
914 )
915
916 @unittest.skipUnless(drgn._with_pcre2_utf, "PCRE2 does not support UTF-8")
917 def test_invalid_utf8(self):
918 prog = mock_search_memory_program(
919 MockMemorySegment(b"\xc3\x28abcdef\xa0\xa1", 0x1000)
920 )
921 self.assertEqual(
922 list(prog.search_memory_regex(r"[a-z]+")), [(0x1002, "abcdef")]
923 )
924
925 def test_no_lookbehind(self):
926 self.assertRaisesRegex(
927 ValueError,
928 "lookbehind",
929 mock_search_memory_program().search_memory_regex,
930 rb"(?<=foo)bar",
931 )
932 self.assertRaisesRegex(
933 ValueError,
934 "lookbehind",
935 mock_search_memory_program().search_memory_regex,
936 rb"(?<!foo)bar",
937 )
938
939 def test_default_program(self):
940 with with_default_prog(
941 mock_search_memory_program(MockMemorySegment(b"foo bar", 0x1000))
942 ):
943 self.assertEqual(
944 list(search_memory_regex(rb"foo|bar")),
945 [(0x1000, b"foo"), (0x1004, b"bar")],
946 )
947 if drgn._with_pcre2_utf:
948 self.assertEqual(
949 list(search_memory_regex(r"foo|bar")),
950 [(0x1000, "foo"), (0x1004, "bar")],
951 )