Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support memory slicing #1151

Open
wants to merge 5 commits into
base: dev
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 38 additions & 1 deletion qiling/os/memory.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#

import os, re
from typing import Any, Callable, List, MutableSequence, Optional, Sequence, Tuple
from typing import Any, Callable, List, MutableSequence, Optional, Sequence, Tuple, Union

from unicorn import UC_PROT_NONE, UC_PROT_READ, UC_PROT_WRITE, UC_PROT_EXEC, UC_PROT_ALL

Expand Down Expand Up @@ -63,6 +63,43 @@ def __read_string(self, addr: int) -> str:
def __write_string(self, addr: int, s: str, encoding: str):
self.write(addr, bytes(s, encoding) + b'\x00')

def __getitem__(self, key: Union[slice, int]) -> bytearray:
if isinstance(key, slice):
start = key.start
stop = key.stop
step = key.step

if step is not None and step != 1:
# step != 1 means we have to do copy, don't allow it.
raise IndexError("Only support slicing continous memory")

return self.ql.mem.read(start, max(0, stop - start))
elif isinstance(key, int):
return self.ql.mem.read(key, 1)[0]
Copy link

@LukeSerne LukeSerne May 15, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line returns an int, not bytearray, which is somewhat counterintuitive, inconsistent with the case where a slice is given as argument, and goes against the type annotation. I think you should remove the [0]. Also, I'm not sure why this code uses self.ql.mem instead of self. In this class, they reference / point to the same thing, right?

Suggested change
return self.ql.mem.read(key, 1)[0]
return self.read(key, 1)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line returns an int, not bytearray, which is somewhat counterintuitive. I think you should remove the [0].

Because we (try to) provide the same interface as bytearray, e.g.:

In [1]: a = bytearray(b"\x01\x02")

In [2]: type(a[0])
Out[2]: int

In [3]:

Also, I'm not sure why this code uses self.ql.mem instead of self. In this class, they reference / point to the same thing, right?

Good point.

else:
raise KeyError("Wrong type of key")

def __setitem__(self, key: Union[slice, int], value: bytes):
if isinstance(key, int):
wtdcode marked this conversation as resolved.
Show resolved Hide resolved
self.ql.mem.write(key, bytes([value]))
elif isinstance(key, slice):
start = key.start
stop = key.stop
step = key.step

if step is not None and step != 1:
raise IndexError("Only support slicing continous memory")

if start is None:
raise IndexError("The start of memory is not supplied")

if stop is not None and len(value) > stop - start:
raise IndexError("Bytes to write are more than sliced memory")

self.ql.mem.write(start, value)
else:
raise KeyError("Wrong type of key")

# TODO: this is an obsolete utility method that should not be used anymore
# and here for backward compatibility. use QlOsUtils.read_cstring instead
def string(self, addr: int, value=None, encoding='utf-8') -> Optional[str]:
Expand Down