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

Make accessing Debugger registers more ergonomic #997

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
6 changes: 6 additions & 0 deletions dmoj/cptbox/_cptbox.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ PTBOX_ABI_COUNT: int
ALL_ABIS: List[int]
SUPPORTED_ABIS: List[int]

class ArgAccessor:
def __getitem__(self, reg : int) -> int: ...
def __setitem__(self, reg : int, val : int) -> None: ...

class Debugger:
syscall: int
result: int
Expand All @@ -22,6 +26,7 @@ class Debugger:
arg3: int
arg4: int
arg5: int
arg: ArgAccessor

uresult: int
uarg0: int
Expand All @@ -30,6 +35,7 @@ class Debugger:
uarg3: int
uarg4: int
uarg5: int
uarg: ArgAccessor

pid: int
tid: int
Expand Down
18 changes: 18 additions & 0 deletions dmoj/cptbox/_cptbox.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,22 @@ def memory_fd_seal(int fd):

cdef class Process

cdef class Debugger

cdef class ArgAccessor:
cdef Debugger debugger
cdef object base

def __cinit__(self, Debugger debugger, object base):
self.debugger = debugger
self.base = base

def __getitem__(self, reg):
return getattr(self.debugger, '%s%d' % (self.base, reg))
Copy link
Member

Choose a reason for hiding this comment

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

Why are you doing this in _cptbox? Maybe you should move this into AdvancedDebugger instead.


def __setitem__(self, reg, val):
setattr(self.debugger, '%s%d' % (self.base, reg), val)


cdef class Debugger:
cdef pt_debugger *thisptr
Expand All @@ -235,6 +251,8 @@ cdef class Debugger:
self.thisptr = new pt_debugger()
self.process = process
self.on_return_callback = {}
self.uarg = ArgAccessor(self, "uarg")
self.arg = ArgAccessor(self, "arg")

def __dealloc__(self):
del self.thisptr
Expand Down
9 changes: 4 additions & 5 deletions dmoj/cptbox/isolate.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ def _compile_fs_jail(self, fs) -> FilesystemPolicy:

def _dirfd_getter_from_reg(self, reg: int) -> DirFDGetter:
def getter(debugger: Debugger) -> int:
return getattr(debugger, 'uarg%d' % reg)
return debugger.uarg[reg]

return getter

Expand All @@ -222,7 +222,7 @@ def _dirfd_getter_cwd(self, debugger: Debugger) -> int:

def _fs_jail_getter_from_open_flags_reg(self, reg: int) -> FSJailGetter:
def getter(debugger: Debugger) -> FilesystemPolicy:
open_flags = getattr(debugger, 'uarg%d' % reg)
open_flags = debugger.uarg[reg]
for flag in open_write_flags:
# Strict equality is necessary here, since e.g. O_TMPFILE has multiple bits set,
# and O_DIRECTORY & O_TMPFILE > 0.
Expand Down Expand Up @@ -282,7 +282,7 @@ def check(debugger: Debugger) -> None:
# We already allowed this one way or another, don't check again.
return

dirfd = getattr(debugger, 'uarg%d' % dir_reg)
dirfd = debugger.uarg[dir_reg]
full_path = self.get_full_path_unnormalized(debugger, rel_file, dirfd=dirfd)
self._access_check(debugger, full_path, self.read_fs_jail)

Expand All @@ -299,9 +299,8 @@ def check(debugger: Debugger) -> None:
return check

def get_rel_file(self, debugger: Debugger, *, reg: int) -> str:
ptr = getattr(debugger, 'uarg%d' % reg)
try:
file = debugger.readstr(ptr)
file = debugger.readstr(debugger.uarg[reg])
except MaxLengthExceeded as e:
raise DeniedSyscall(ACCESS_ENAMETOOLONG, f'Overly long path: {e.args[0]}')
except UnicodeDecodeError as e:
Expand Down