Skip to content

Commit 84ae88d

Browse files
authored
Merge pull request #907 from andrwnoskov/fix-memsearch
Fix memsearch
2 parents 8b9202b + cfe5d9a commit 84ae88d

File tree

2 files changed

+40
-2
lines changed

2 files changed

+40
-2
lines changed

qiling/os/memory.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ def search(self, needle: bytes, begin: int = None, end: int = None) -> Sequence[
309309

310310
assert begin < end, 'search arguments do not make sense'
311311

312-
ranges = [(max(begin, lbound), min(ubound, end)) for lbound, ubound, _, _ in self.map_info if (begin <= lbound < end) or (begin < ubound <= end)]
312+
ranges = [(max(begin, lbound), min(ubound, end)) for lbound, ubound, _, _ in self.map_info if not (end < lbound or ubound < begin)]
313313
results = []
314314

315315
for lbound, ubound in ranges:

tests/test_elf.py

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
# Cross Platform and Multi Architecture Advanced Binary Emulation Framework
44
#
55

6-
import sys, unittest, string, random, os, io
6+
import sys, unittest, string, random, os, io, re
77

88
sys.path.append("..")
99
from qiling import Qiling
@@ -1053,6 +1053,44 @@ def test_elf_linux_x86_getdents64(self):
10531053
self.assertTrue("bin\n" in ql.os.stdout.read().decode("utf-8"))
10541054

10551055
del ql
1056+
1057+
def test_memory_search(self):
1058+
ql = Qiling(code = b"\xCC", archtype = "x8664", ostype = "linux", verbose=QL_VERBOSE.DEBUG)
1059+
1060+
ql.mem.map(0x1000, 0x1000)
1061+
ql.mem.map(0x2000, 0x1000)
1062+
ql.mem.map(0x3000, 0x1000)
1063+
1064+
ql.mem.write(0x1000, b"\x47\x06\x0d\x1e\x0d\x1a\x53\x0f\x07\x06\x06\x09\x53\x0f\x01\x1e\x0d\x53\x11\x07\x1d\x53\x1d\x18\x4f\x53\x06\x0d\x1e\x0d\x1a\x53\x0f\x07\x06\x06\x09\x53\x04\x0d\x1c\x53\x11\x07\x1d\x53\x0c\x07\x1f\x06\x45")
1065+
ql.mem.write(0x2000, b"\x47\x06\x0d\x1e\x0d\x1a\x53\x0f\x07\x06\x06\x09\x53\x1a\x1d\x06\x53\x09\x1a\x07\x1d\x06\x0c\x53\x09\x06\x0c\x53\x0c\x0d\x1b\x0d\x1a\x1c\x53\x11\x07\x1d\x4f\x53\x06\x0d\x1e\x0d\x1a\x53\x0f\x07\x06\x06\x09\x53\x05\x09\x03\x0d\x53\x11\x07\x1d\x53\x0b\x1a\x11\x45")
1066+
ql.mem.write(0x3000, b"\x47\x06\x0d\x1e\x0d\x1a\x53\x0f\x07\x06\x06\x09\x53\x1b\x09\x11\x53\x0f\x07\x07\x0c\x0a\x11\x0d\x4f\x53\x06\x0d\x1e\x0d\x1a\x53\x0f\x07\x06\x06\x09\x53\x1c\x0d\x04\x04\x53\x09\x53\x04\x01\x0d\x53\x09\x06\x0c\x53\x00\x1d\x1a\x1c\x53\x11\x07\x1d\x45")
1067+
ql.mem.write(0x1FFB, b"\x1f\x00\x07\x53\x03\x06\x07\x1f\x1b")
1068+
1069+
# Needle not in haystack
1070+
self.assertEqual([], ql.mem.search(re.escape(b"\x3a\x01\x0b\x03\x53\x29\x1b\x1c\x04\x0d\x11")))
1071+
1072+
# Needle appears several times in haystack
1073+
self.assertEqual([0x1000 + 24, 0x2000 + 38, 0x3000 + 24], ql.mem.search(re.escape(b"\x4f\x53\x06\x0d\x1e\x0d\x1a")))
1074+
1075+
# Needle inside haystack
1076+
self.assertEqual([0x1000 + 13], ql.mem.search(re.escape(b"\x0f\x01\x1e\x0d\x53\x11\x07\x1d\x53\x1d\x18"), begin=0x1000 + 10, end=0x1000 + 30))
1077+
1078+
# Needle before haystack
1079+
self.assertEqual([], ql.mem.search(re.escape(b"\x04\x0d\x1c\x53\x11\x07\x1d\x53\x0c\x07\x1f\x06"), begin=0x1337))
1080+
1081+
# Needle after haystack
1082+
self.assertEqual([], ql.mem.search(re.escape(b"\x1b\x09\x11\x53\x0f\x07\x07\x0c\x0a\x11\x0d"), end=0x3000 + 13))
1083+
1084+
# Needle exactly inside haystack
1085+
self.assertEqual([0x2000 + 13], ql.mem.search(re.escape(b"\x1a\x1d\x06\x53\x09\x1a\x07\x1d\x06\x0c"), begin=0x2000 + 13, end=0x2000 + 23))
1086+
1087+
# Needle 'tears' two mapped regions
1088+
self.assertEqual([], ql.mem.search(re.escape(b"\x1f\x00\x07\x53\x03\x06\x07\x1f\x1b"), begin=0x1F00, end=0x200F))
1089+
1090+
# Needle is a regex
1091+
self.assertEqual([0x1000 + 11, 0x2000 + 11, 0x3000 + 43], ql.mem.search(b"\x09\x53(\x0f|\x1a|\x04)[^\x0d]"))
1092+
1093+
del ql
10561094

10571095
if __name__ == "__main__":
10581096
unittest.main()

0 commit comments

Comments
 (0)