Skip to content

Commit

Permalink
Implemented sys_readv system call (#217)
Browse files Browse the repository at this point in the history
* Implemented sys_readv system call

* sys_readv and sys_writev are now independent of the system bitness
  • Loading branch information
saelo authored and offlinemark committed May 2, 2017
1 parent b19a158 commit af26764
Showing 1 changed file with 26 additions and 22 deletions.
48 changes: 26 additions & 22 deletions manticore/models/linux.py
Original file line number Diff line number Diff line change
Expand Up @@ -1288,34 +1288,34 @@ def sys_getegid(self, cpu):
'''
return 1000

def sys_writev(self, cpu, fd, iov, count):
def sys_readv(self, cpu, fd, iov, count):
'''
Works just like C{sys_write} except that multiple buffers are written out (for Linux 64 bits).
Works just like C{sys_read} except that data is read into multiple buffers.
:rtype: int
:param cpu: current CPU.
:param fd: the file descriptor of the file to write.
:param iov: the buffer where the the bytes to write are taken.
:param count: amount of C{iov} buffers to write into the file.
:return: the amount of bytes written in total.
:param fd: the file descriptor of the file to read.
:param iov: the buffer where the the bytes to read are stored.
:param count: amount of C{iov} buffers to read from the file.
:return: the amount of bytes read in total.
'''
ptrsize = cpu.address_bit_size
sizeof_iovec = 2 * (ptrsize // 8)
total = 0
for i in xrange(0, count):
buf = cpu.read_int(iov + i * 16, 64)
size = cpu.read_int(iov + i * 16 + 8, 64)
buf = cpu.read_int(iov + i * sizeof_iovec, ptrsize)
size = cpu.read_int(iov + i * sizeof_iovec + (sizeof_iovec // 2), ptrsize)

data = ""
for j in xrange(0,size):
data += Operators.CHR(cpu.read_int(buf + j, 8))
logger.debug("WRITEV(%r, %r, %r) -> <%r> (size:%r)"%(fd, buf, size, data, len(data)))
self.files[fd].write(data)
self.syscall_trace.append(("_write", fd, data))
total+=size
data = self.files[fd].read(size)
total += len(data)
cpu.write_bytes(buf, data)
self.syscall_trace.append(("_read", fd, data))
logger.debug("READV(%r, %r, %r) -> <%r> (size:%r)"%(fd, buf, size, data, len(data)))
return total

def sys_writev32(self, cpu, fd, iov, count):
def sys_writev(self, cpu, fd, iov, count):
'''
Works just like C{sys_write} except that multiple buffers are written out. (32 bit version)
Works just like C{sys_write} except that multiple buffers are written out.
:rtype: int
:param cpu: current CPU.
Expand All @@ -1324,16 +1324,18 @@ def sys_writev32(self, cpu, fd, iov, count):
:param count: amount of C{iov} buffers to write into the file.
:return: the amount of bytes written in total.
'''
ptrsize = cpu.address_bit_size
sizeof_iovec = 2 * (ptrsize // 8)
total = 0
for i in xrange(0, count):
buf = cpu.read_int(iov + i * 8, 32)
size = cpu.read_int(iov + i * 8 + 4, 32)
buf = cpu.read_int(iov + i * sizeof_iovec, ptrsize)
size = cpu.read_int(iov + i * sizeof_iovec + (sizeof_iovec // 2), ptrsize)

data = ""
for j in xrange(0,size):
data += Operators.CHR(cpu.read_int(buf + j, 8))
self.files[fd].write(Operators.CHR(cpu.read_int(buf + j, 8)))
logger.debug("WRITEV(%r, %r, %r) -> <%r> (size:%r)"%(fd, buf, size, data, len(data)))
self.files[fd].write(data)
self.syscall_trace.append(("_write", fd, data))
total+=size
return total
Expand Down Expand Up @@ -1462,6 +1464,7 @@ def syscall(self, cpu):
0x0000000000000015: self.sys_access,
0x000000000000000a: self.sys_mprotect,
0x000000000000000b: self.sys_munmap,
0x0000000000000013: self.sys_readv,
0x0000000000000014: self.sys_writev,
0x0000000000000004: self.sys_stat64,
0x0000000000000059: self.sys_acct,
Expand Down Expand Up @@ -1532,7 +1535,8 @@ def int80(self, cpu):
0x0000007d: self.sys_mprotect,
0x0000008c: self.sys_setpriority,
0x0000008d: self.sys_getpriority,
0x00000092: self.sys_writev32,
0x00000091: self.sys_readv,
0x00000092: self.sys_writev,
0x000000c0: self.sys_mmap2,
0x000000c3: self.sys_stat32,
0x000000c5: self.sys_fstat,
Expand Down

0 comments on commit af26764

Please sign in to comment.