Implemented getcwd sys call, along with a new helper function that wr… (#483)
* Implemented getcwd sys call, along with a new helper function that writes a NULL terminated string to memory. The reason for this commit is that the sys call was not implemented. * Fixed logging string for sys_getcwd imp * Removed semicolon in sys_getcwd function * Fixed logging string for sys_getcwd imp. The problem was that size is unsigned and the incorrect format symbol was used * Fixed the following: 1) Included the string param in the doc of the write_string function. 2) Added less verbose code to append a NULL to a string in the write_string function 3) Removed the assert to check if the written string to memory matches with one that is read at a given address in the sys_getcwd function 4) Fixed bug in the return values returned by sys_getcwd * Fixed error logs in the getcwd function. * Fixed return values of sys_getcwd function. * Fixed string format symbol in sys_getcwd function. * Removed dir exists check from the sys_getcwd function. * Arranged memory block check in the sys_getcwd function. * Removed new line after write_string function. (Thank you @johnfxgalea!)
This commit is contained in:
parent
d83fa737a7
commit
60c6813a80
@ -552,6 +552,21 @@ class Cpu(Eventful):
|
|||||||
result.append(Operators.CHR(self.read_int(where + i, 8)))
|
result.append(Operators.CHR(self.read_int(where + i, 8)))
|
||||||
return result
|
return result
|
||||||
|
|
||||||
|
def write_string(self, where, string, max_length=None):
|
||||||
|
'''
|
||||||
|
Writes a string to memory, appending a NULL-terminator at the end.
|
||||||
|
:param int where: Address to write the string to
|
||||||
|
:param str string: The string to write to memory
|
||||||
|
:param int max_length:
|
||||||
|
The size in bytes to cap the string at, or None [default] for no
|
||||||
|
limit. This includes the NULL terminator.
|
||||||
|
'''
|
||||||
|
|
||||||
|
if max_length is not None:
|
||||||
|
string = string[:max_length-1]
|
||||||
|
|
||||||
|
self.write_bytes(where, string + '\x00')
|
||||||
|
|
||||||
def read_string(self, where, max_length=None):
|
def read_string(self, where, max_length=None):
|
||||||
'''
|
'''
|
||||||
Read a NUL-terminated concrete buffer from memory.
|
Read a NUL-terminated concrete buffer from memory.
|
||||||
|
|||||||
@ -1029,6 +1029,34 @@ class Linux(Platform):
|
|||||||
except OSError as e:
|
except OSError as e:
|
||||||
return e.errno
|
return e.errno
|
||||||
|
|
||||||
|
def sys_getcwd(self, buf, size):
|
||||||
|
'''
|
||||||
|
getcwd - Get the current working directory
|
||||||
|
:param int buf: Pointer to dest array
|
||||||
|
:param size: size in bytes of the array pointed to by the buf
|
||||||
|
:return: buf (Success), or 0
|
||||||
|
'''
|
||||||
|
|
||||||
|
try:
|
||||||
|
current_dir = os.getcwd()
|
||||||
|
length = len(current_dir) + 1
|
||||||
|
|
||||||
|
if size > 0 and size < length:
|
||||||
|
logger.info("GETCWD: size is greater than 0, but is smaller than the length"
|
||||||
|
"of the path + 1. Returning ERANGE")
|
||||||
|
return -errno.ERANGE
|
||||||
|
|
||||||
|
if not self.current.memory.access_ok(slice(buf, buf+length), 'w'):
|
||||||
|
logger.info("GETCWD: buf within invalid memory. Returning EFAULT")
|
||||||
|
return -errno.EFAULT
|
||||||
|
|
||||||
|
self.current.write_string(buf, current_dir)
|
||||||
|
logger.debug("getcwd(0x%08x, %u) -> <%s> (Size %d)", buf, size, current_dir, length)
|
||||||
|
return length
|
||||||
|
|
||||||
|
except OSError as e:
|
||||||
|
return -e.errno
|
||||||
|
|
||||||
def sys_lseek(self, fd, offset, whence):
|
def sys_lseek(self, fd, offset, whence):
|
||||||
'''
|
'''
|
||||||
lseek - reposition read/write file offset
|
lseek - reposition read/write file offset
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user