Skip to content

Commit 72e7d8f

Browse files
Fix elf tests, indicate in ChangeLog
1 parent 3d30f12 commit 72e7d8f

File tree

2 files changed

+74
-66
lines changed

2 files changed

+74
-66
lines changed

ChangeLog

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ BREAK CHANGE
77
- ql.filename is renamed to ql.argv.
88
- ql.output and ql.verbose now has slightly different meanings and can be adjusted runtime. See their docstring for details.
99
- ql.filter now accepts a regular expression.
10+
- syscall hooks now no longer uses ql.os.definesyscall_return, can now set errno with return value
1011

1112
------------------------------------
1213
[Version 1.2.1]: December [SOMETHING], 2020

tests/test_elf.py

Lines changed: 73 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -143,17 +143,17 @@ def test_syscall_read(ql, read_fd, read_buf, read_count, *args):
143143
print("test => read(%d, %s, %d)" % (read_fd, pathname, read_count))
144144
target = True
145145

146-
syscall.ql_syscall_read(ql, read_fd, read_buf, read_count, *args)
146+
regreturn = syscall.ql_syscall_read(ql, read_fd, read_buf, read_count, *args)
147147

148148
if target:
149149
real_path = ql.os.fd[read_fd].name
150150
with open(real_path) as fd:
151151
assert fd.read() == ql.mem.read(read_buf, read_count).decode()
152-
if ql.platform == QL_OS.WINDOWS:
153-
return
154-
else:
152+
if ql.platform != QL_OS.WINDOWS:
155153
os.remove(real_path)
156154

155+
return regreturn
156+
157157
def test_syscall_write(ql, write_fd, write_buf, write_count, *args):
158158
target = False
159159
pathname = ql.os.fd[write_fd].name.split('/')[-1]
@@ -162,17 +162,17 @@ def test_syscall_write(ql, write_fd, write_buf, write_count, *args):
162162
print("test => write(%d, %s, %d)" % (write_fd, pathname, write_count))
163163
target = True
164164

165-
syscall.ql_syscall_write(ql, write_fd, write_buf, write_count, *args)
165+
regreturn = syscall.ql_syscall_write(ql, write_fd, write_buf, write_count, *args)
166166

167167
if target:
168168
real_path = ql.os.fd[write_fd].name
169169
with open(real_path) as fd:
170170
assert fd.read() == 'Hello testing\x00'
171-
if ql.platform == QL_OS.WINDOWS:
172-
return
173-
else:
171+
if ql.platform != QL_OS.WINDOWS:
174172
os.remove(real_path)
175173

174+
return regreturn
175+
176176
def test_syscall_openat(ql, openat_fd, openat_path, openat_flags, openat_mode, *args):
177177
target = False
178178
pathname = ql.mem.string(openat_path)
@@ -181,16 +181,16 @@ def test_syscall_openat(ql, openat_fd, openat_path, openat_flags, openat_mode, *
181181
print("test => openat(%d, %s, 0x%x, 0%o)" % (openat_fd, pathname, openat_flags, openat_mode))
182182
target = True
183183

184-
syscall.ql_syscall_openat(ql, openat_fd, openat_path, openat_flags, openat_mode, *args)
184+
regreturn = syscall.ql_syscall_openat(ql, openat_fd, openat_path, openat_flags, openat_mode, *args)
185185

186186
if target:
187187
real_path = ql.os.transform_to_real_path(pathname)
188188
assert os.path.isfile(real_path) == True
189-
if ql.platform == QL_OS.WINDOWS:
190-
return
191-
else:
189+
if ql.platform != QL_OS.WINDOWS:
192190
os.remove(real_path)
193191

192+
return regreturn
193+
194194
def test_syscall_unlink(ql, unlink_pathname, *args):
195195
target = False
196196
pathname = ql.mem.string(unlink_pathname)
@@ -199,12 +199,14 @@ def test_syscall_unlink(ql, unlink_pathname, *args):
199199
print("test => unlink(%s)" % (pathname))
200200
target = True
201201

202-
syscall.ql_syscall_unlink(ql, unlink_pathname, *args)
202+
regreturn = syscall.ql_syscall_unlink(ql, unlink_pathname, *args)
203203

204204
if target:
205205
real_path = ql.os.transform_to_real_path(pathname)
206206
assert os.path.isfile(real_path) == False
207207

208+
return regreturn
209+
208210
def test_syscall_truncate(ql, trunc_pathname, trunc_length, *args):
209211
target = False
210212
pathname = ql.mem.string(trunc_pathname)
@@ -213,16 +215,16 @@ def test_syscall_truncate(ql, trunc_pathname, trunc_length, *args):
213215
print("test => truncate(%s, 0x%x)" % (pathname, trunc_length))
214216
target = True
215217

216-
syscall.ql_syscall_truncate(ql, trunc_pathname, trunc_length, *args)
218+
regreturn = syscall.ql_syscall_truncate(ql, trunc_pathname, trunc_length, *args)
217219

218220
if target:
219221
real_path = ql.os.transform_to_real_path(pathname)
220222
assert os.stat(real_path).st_size == 0
221-
if ql.platform == QL_OS.WINDOWS:
222-
return
223-
else:
223+
if ql.platform != QL_OS.WINDOWS:
224224
os.remove(real_path)
225225

226+
return regreturn
227+
226228
def test_syscall_ftruncate(ql, ftrunc_fd, ftrunc_length, *args):
227229
target = False
228230
pathname = ql.os.fd[ftrunc_fd].name.split('/')[-1]
@@ -235,16 +237,16 @@ def test_syscall_ftruncate(ql, ftrunc_fd, ftrunc_length, *args):
235237
print("test => ftruncate(%d, 0x%x)" % (ftrunc_fd, ftrunc_length))
236238
target = True
237239

238-
syscall.ql_syscall_ftruncate(ql, ftrunc_fd, ftrunc_length, *args)
240+
regreturn = syscall.ql_syscall_ftruncate(ql, ftrunc_fd, ftrunc_length, *args)
239241

240242
if target:
241243
real_path = ql.os.transform_to_real_path(pathname)
242244
assert os.stat(real_path).st_size == 0x10
243-
if ql.platform == QL_OS.WINDOWS:
244-
return
245-
else:
245+
if ql.platform != QL_OS.WINDOWS:
246246
os.remove(real_path)
247247

248+
return regreturn
249+
248250
ql = Qiling(["../examples/rootfs/x86_linux/bin/x86_posix_syscall"], "../examples/rootfs/x86_linux", output="debug")
249251
ql.set_syscall(0x3, test_syscall_read)
250252
ql.set_syscall(0x4, test_syscall_write)
@@ -429,26 +431,26 @@ def test_elf_linux_arm64_posix_syscall(self):
429431
def test_syscall_read(ql, read_fd, read_buf, read_count, *args):
430432
target = False
431433
pathname = ql.os.fd[read_fd].name.split('/')[-1]
432-
434+
433435
reg = ql.reg.read("x0")
434436
print("reg : 0x%x" % reg)
435437
ql.reg.x0 = reg
436-
438+
437439
if pathname == "test_syscall_read.txt":
438440
print("test => read(%d, %s, %d)" % (read_fd, pathname, read_count))
439441
target = True
440442

441-
syscall.ql_syscall_read(ql, read_fd, read_buf, read_count, *args)
443+
regreturn = syscall.ql_syscall_read(ql, read_fd, read_buf, read_count, *args)
442444

443445
if target:
444446
real_path = ql.os.fd[read_fd].name
445447
with open(real_path) as fd:
446448
assert fd.read() == ql.mem.read(read_buf, read_count).decode()
447-
if ql.platform == QL_OS.WINDOWS:
448-
return
449-
else:
449+
if ql.platform != QL_OS.WINDOWS:
450450
os.remove(real_path)
451451

452+
return regreturn
453+
452454

453455
def test_syscall_write(ql, write_fd, write_buf, write_count, *args):
454456
target = False
@@ -458,17 +460,17 @@ def test_syscall_write(ql, write_fd, write_buf, write_count, *args):
458460
print("test => write(%d, %s, %d)" % (write_fd, pathname, write_count))
459461
target = True
460462

461-
syscall.ql_syscall_write(ql, write_fd, write_buf, write_count, *args)
463+
regreturn = syscall.ql_syscall_write(ql, write_fd, write_buf, write_count, *args)
462464

463465
if target:
464466
real_path = ql.os.fd[write_fd].name
465467
with open(real_path) as fd:
466468
assert fd.read() == 'Hello testing\x00'
467-
if ql.platform == QL_OS.WINDOWS:
468-
return
469-
else:
469+
if ql.platform != QL_OS.WINDOWS:
470470
os.remove(real_path)
471471

472+
return regreturn
473+
472474

473475
def test_syscall_openat(ql, openat_fd, openat_path, openat_flags, openat_mode, *args):
474476
target = False
@@ -478,16 +480,16 @@ def test_syscall_openat(ql, openat_fd, openat_path, openat_flags, openat_mode, *
478480
print("test => openat(%d, %s, 0x%x, 0%o)" % (openat_fd, pathname, openat_flags, openat_mode))
479481
target = True
480482

481-
syscall.ql_syscall_openat(ql, openat_fd, openat_path, openat_flags, openat_mode, *args)
483+
regreturn = syscall.ql_syscall_openat(ql, openat_fd, openat_path, openat_flags, openat_mode, *args)
482484

483485
if target:
484486
real_path = ql.os.transform_to_real_path(pathname)
485487
assert os.path.isfile(real_path) == True
486-
if ql.platform == QL_OS.WINDOWS:
487-
return
488-
else:
488+
if ql.platform != QL_OS.WINDOWS:
489489
os.remove(real_path)
490490

491+
return regreturn
492+
491493

492494
def test_syscall_unlink(ql, unlink_pathname, *args):
493495
target = False
@@ -497,12 +499,14 @@ def test_syscall_unlink(ql, unlink_pathname, *args):
497499
print("test => unlink(%s)" % (pathname))
498500
target = True
499501

500-
syscall.ql_syscall_unlink(ql, unlink_pathname, *args)
502+
regreturn = syscall.ql_syscall_unlink(ql, unlink_pathname, *args)
501503

502504
if target:
503505
real_path = ql.os.transform_to_real_path(pathname)
504506
assert os.path.isfile(real_path) == False
505507

508+
return regreturn
509+
506510

507511
def test_syscall_truncate(ql, trunc_pathname, trunc_length, *args):
508512
target = False
@@ -512,16 +516,16 @@ def test_syscall_truncate(ql, trunc_pathname, trunc_length, *args):
512516
print("test => truncate(%s, 0x%x)" % (pathname, trunc_length))
513517
target = True
514518

515-
syscall.ql_syscall_truncate(ql, trunc_pathname, trunc_length, *args)
519+
regreturn = syscall.ql_syscall_truncate(ql, trunc_pathname, trunc_length, *args)
516520

517521
if target:
518522
real_path = ql.os.transform_to_real_path(pathname)
519523
assert os.stat(real_path).st_size == 0
520-
if ql.platform == QL_OS.WINDOWS:
521-
return
522-
else:
524+
if ql.platform != QL_OS.WINDOWS:
523525
os.remove(real_path)
524526

527+
return regreturn
528+
525529

526530
def test_syscall_ftruncate(ql, ftrunc_fd, ftrunc_length, *args):
527531
target = False
@@ -531,16 +535,16 @@ def test_syscall_ftruncate(ql, ftrunc_fd, ftrunc_length, *args):
531535
print("test => ftruncate(%d, 0x%x)" % (ftrunc_fd, ftrunc_length))
532536
target = True
533537

534-
syscall.ql_syscall_ftruncate(ql, ftrunc_fd, ftrunc_length, *args)
538+
regreturn = syscall.ql_syscall_ftruncate(ql, ftrunc_fd, ftrunc_length, *args)
535539

536540
if target:
537541
real_path = ql.os.transform_to_real_path(pathname)
538542
assert os.stat(real_path).st_size == 0x10
539-
if ql.platform == QL_OS.WINDOWS:
540-
return
541-
else:
543+
if ql.platform != QL_OS.WINDOWS:
542544
os.remove(real_path)
543545

546+
return regreturn
547+
544548
ql = Qiling(["../examples/rootfs/arm64_linux/bin/arm64_posix_syscall"], "../examples/rootfs/arm64_linux", output="debug")
545549
ql.set_syscall(0x3f, test_syscall_read)
546550
ql.set_syscall(0x40, test_syscall_write)
@@ -583,17 +587,18 @@ def test_syscall_read(ql, read_fd, read_buf, read_count, *args):
583587
print("test => read(%d, %s, %d)" % (read_fd, pathname, read_count))
584588
target = True
585589

586-
syscall.ql_syscall_read(ql, read_fd, read_buf, read_count, *args)
590+
regreturn = syscall.ql_syscall_read(ql, read_fd, read_buf, read_count, *args)
587591

588592
if target:
589593
real_path = ql.os.fd[read_fd].name
590594
with open(real_path) as fd:
591595
assert fd.read() == ql.mem.read(read_buf, read_count).decode()
592-
if ql.platform == QL_OS.WINDOWS:
593-
return
594-
else:
596+
if ql.platform != QL_OS.WINDOWS:
595597
os.remove(real_path)
596-
598+
599+
return regreturn
600+
601+
597602
def test_syscall_write(ql, write_fd, write_buf, write_count, *args):
598603
target = False
599604
pathname = ql.os.fd[write_fd].name.split('/')[-1]
@@ -602,17 +607,17 @@ def test_syscall_write(ql, write_fd, write_buf, write_count, *args):
602607
print("test => write(%d, %s, %d)" % (write_fd, pathname, write_count))
603608
target = True
604609

605-
syscall.ql_syscall_write(ql, write_fd, write_buf, write_count, *args)
610+
regreturn = syscall.ql_syscall_write(ql, write_fd, write_buf, write_count, *args)
606611

607612
if target:
608613
real_path = ql.os.fd[write_fd].name
609614
with open(real_path) as fd:
610615
assert fd.read() == 'Hello testing\x00'
611-
if ql.platform == QL_OS.WINDOWS:
612-
return
613-
else:
616+
if ql.platform != QL_OS.WINDOWS:
614617
os.remove(real_path)
615618

619+
return regreturn
620+
616621
def test_syscall_open(ql, open_pathname, open_flags, open_mode, *args):
617622
target = False
618623
pathname = ql.mem.string(open_pathname)
@@ -621,16 +626,16 @@ def test_syscall_open(ql, open_pathname, open_flags, open_mode, *args):
621626
print("test => open(%s, 0x%x, 0%o)" % (pathname, open_flags, open_mode))
622627
target = True
623628

624-
syscall.ql_syscall_open(ql, open_pathname, open_flags, open_mode, *args)
629+
regreturn = syscall.ql_syscall_open(ql, open_pathname, open_flags, open_mode, *args)
625630

626631
if target:
627632
real_path = ql.os.transform_to_real_path(pathname)
628633
assert os.path.isfile(real_path) == True
629-
if ql.platform == QL_OS.WINDOWS:
630-
return
631-
else:
634+
if ql.platform != QL_OS.WINDOWS:
632635
os.remove(real_path)
633636

637+
return regreturn
638+
634639
def test_syscall_unlink(ql, unlink_pathname, *args):
635640
target = False
636641
pathname = ql.mem.string(unlink_pathname)
@@ -639,12 +644,14 @@ def test_syscall_unlink(ql, unlink_pathname, *args):
639644
print("test => unlink(%s)" % (pathname))
640645
target = True
641646

642-
syscall.ql_syscall_unlink(ql, unlink_pathname, *args)
647+
regreturn = syscall.ql_syscall_unlink(ql, unlink_pathname, *args)
643648

644649
if target:
645650
real_path = ql.os.transform_to_real_path(pathname)
646651
assert os.path.isfile(real_path) == False
647652

653+
return regreturn
654+
648655
def test_syscall_truncate(ql, trunc_pathname, trunc_length, *args):
649656
target = False
650657
pathname = ql.mem.string(trunc_pathname)
@@ -653,16 +660,16 @@ def test_syscall_truncate(ql, trunc_pathname, trunc_length, *args):
653660
print("test => truncate(%s, 0x%x)" % (pathname, trunc_length))
654661
target = True
655662

656-
syscall.ql_syscall_truncate(ql, trunc_pathname, trunc_length, *args)
663+
regreturn = syscall.ql_syscall_truncate(ql, trunc_pathname, trunc_length, *args)
657664

658665
if target:
659666
real_path = ql.os.transform_to_real_path(pathname)
660667
assert os.stat(real_path).st_size == 0
661-
if ql.platform == QL_OS.WINDOWS:
662-
return
663-
else:
668+
if ql.platform != QL_OS.WINDOWS:
664669
os.remove(real_path)
665670

671+
return regreturn
672+
666673
def test_syscall_ftruncate(ql, ftrunc_fd, ftrunc_length, *args):
667674
target = False
668675
pathname = ql.os.fd[ftrunc_fd].name.split('/')[-1]
@@ -671,16 +678,16 @@ def test_syscall_ftruncate(ql, ftrunc_fd, ftrunc_length, *args):
671678
print("test => ftruncate(%d, 0x%x)" % (ftrunc_fd, ftrunc_length))
672679
target = True
673680

674-
syscall.ql_syscall_ftruncate(ql, ftrunc_fd, ftrunc_length, *args)
681+
regreturn = syscall.ql_syscall_ftruncate(ql, ftrunc_fd, ftrunc_length, *args)
675682

676683
if target:
677684
real_path = ql.os.transform_to_real_path(pathname)
678685
assert os.stat(real_path).st_size == 0x10
679-
if ql.platform == QL_OS.WINDOWS:
680-
return
681-
else:
686+
if ql.platform != QL_OS.WINDOWS:
682687
os.remove(real_path)
683688

689+
return regreturn
690+
684691
ql = Qiling(["../examples/rootfs/mips32el_linux/bin/mips32el_posix_syscall"], "../examples/rootfs/mips32el_linux", output="debug")
685692
ql.set_syscall(4003, test_syscall_read)
686693
ql.set_syscall(4004, test_syscall_write)

0 commit comments

Comments
 (0)