proj: Restructure
This commit is contained in:
151
proj/server/src/intercept.py → proj/server/src/intercept/__init__.py
Executable file → Normal file
151
proj/server/src/intercept.py → proj/server/src/intercept/__init__.py
Executable file → Normal file
@@ -3,7 +3,6 @@
|
||||
|
||||
from typing import Optional, TypedDict, NotRequired
|
||||
from socketserver import UnixStreamServer, StreamRequestHandler, ThreadingMixIn
|
||||
import argparse
|
||||
import os
|
||||
import re
|
||||
|
||||
@@ -21,7 +20,8 @@ class ThreadedUnixStreamServer(ThreadingMixIn, UnixStreamServer):
|
||||
|
||||
|
||||
class Handler(StreamRequestHandler):
|
||||
pid: int
|
||||
pid: Optional[int]
|
||||
path: Optional[str]
|
||||
stack: list[tuple[int, str, tuple]]
|
||||
ret_addr: int
|
||||
|
||||
@@ -32,9 +32,11 @@ class Handler(StreamRequestHandler):
|
||||
|
||||
def handle(self):
|
||||
first = self.rfile.readline()
|
||||
self.pid = int(first.split(b':')[1])
|
||||
meta = {a[0]: a[1] for a in [tuple(p.decode('utf-8').split(':', 1)) for p in first.split(b' ', 1)[1].strip().split(b';')]}
|
||||
self.pid = int(meta['PID']) if 'PID' in meta else None
|
||||
self.path = meta['PATH'] if 'PATH' in meta else None
|
||||
self.stack = []
|
||||
print(f'Process with PID {self.pid} connected')
|
||||
print(f'Process with PID {self.pid} connected ({self.path})')
|
||||
self.before()
|
||||
try:
|
||||
while True:
|
||||
@@ -317,136 +319,6 @@ class Handler(StreamRequestHandler):
|
||||
raise NotImplementedError()
|
||||
|
||||
|
||||
class MemoryAllocationTester(Handler):
|
||||
allocated: dict[int, tuple[str, int, int]]
|
||||
max_allocated: int
|
||||
num_malloc: int
|
||||
num_realloc: int
|
||||
num_free: int
|
||||
num_invalid_free: int
|
||||
|
||||
def before(self):
|
||||
self.allocated = {}
|
||||
self.max_allocated = 0
|
||||
self.num_malloc = 0
|
||||
self.num_realloc = 0
|
||||
self.num_free = 0
|
||||
self.num_invalid_free = 0
|
||||
|
||||
def after(self):
|
||||
if len(self.allocated) > 0:
|
||||
print("Not free'd:")
|
||||
for ptr, (func, ret, size) in self.allocated.items():
|
||||
print(f' 0x{ptr:x}: {size} bytes ({func}, return address 0x{ret:x})')
|
||||
else:
|
||||
print("All blocks free'd!")
|
||||
print(f'Max allocated: {self.max_allocated} bytes')
|
||||
|
||||
def update_max_allocated(self):
|
||||
total = sum(a[2] for a in self.allocated.values())
|
||||
if total > self.max_allocated:
|
||||
self.max_allocated = total
|
||||
|
||||
def after_malloc(self, size, ret_value, errno=None) -> None:
|
||||
self.num_malloc += 1
|
||||
if ret_value != 0:
|
||||
self.allocated[ret_value] = ('malloc', self.ret_addr, size)
|
||||
self.update_max_allocated()
|
||||
|
||||
def after_calloc(self, nmemb, size, ret_value, errno=None) -> None:
|
||||
self.num_malloc += 1
|
||||
if ret_value != 0:
|
||||
self.allocated[ret_value] = ('calloc', self.ret_addr, nmemb * size)
|
||||
self.update_max_allocated()
|
||||
|
||||
def after_realloc(self, ptr, size, ret_value, errno=None) -> None:
|
||||
self.num_realloc += 1
|
||||
if ptr != 0:
|
||||
if ret_value != 0:
|
||||
v = self.allocated[ptr]
|
||||
del self.allocated[ptr]
|
||||
self.allocated[ret_value] = (v[0], v[1], size)
|
||||
self.update_max_allocated()
|
||||
|
||||
def after_reallocarray(self, ptr, nmemb, size, ret_value, errno=None) -> None:
|
||||
self.num_realloc += 1
|
||||
if ptr != 0:
|
||||
if ret_value != 0:
|
||||
v = self.allocated[ptr]
|
||||
del self.allocated[ptr]
|
||||
self.allocated[ret_value] = (v[0], v[1], nmemb * size)
|
||||
self.update_max_allocated()
|
||||
|
||||
def after_free(self, ptr) -> None:
|
||||
self.num_free += 1
|
||||
if ptr != 0:
|
||||
if ptr in self.allocated:
|
||||
del self.allocated[ptr]
|
||||
else:
|
||||
self.num_free -= 1
|
||||
self.num_invalid_free += 1
|
||||
|
||||
|
||||
class ReturnValueCheckTester(Handler):
|
||||
pass
|
||||
|
||||
|
||||
class InterruptedCheckTester(Handler):
|
||||
cycles: int = 50
|
||||
functions: dict[str, tuple[str or None, str]] = {
|
||||
'sem_wait': ('fail EINTR', 'return 0'),
|
||||
'sem_trywait': ('fail EINTR', 'return 0'),
|
||||
'sem_timedwait': ('fail EINTR', 'return 0'),
|
||||
'sem_post': (None, 'return 0'),
|
||||
'ftruncate': ('fail EINTR', 'ok'),
|
||||
}
|
||||
|
||||
counter: int = 0
|
||||
last_func_name: Optional[str] = None
|
||||
last_ret_addr: Optional[int] = None
|
||||
tested_functions: dict[tuple[str, int], str]
|
||||
|
||||
@property
|
||||
def while_testing(self) -> bool:
|
||||
return self.counter % self.cycles != 0
|
||||
|
||||
def before(self) -> None:
|
||||
self.tested_functions = {}
|
||||
|
||||
def after(self) -> None:
|
||||
if self.while_testing:
|
||||
self.error()
|
||||
for (name, ret_addr), status in self.tested_functions.items():
|
||||
print(f'{name} (0x{ret_addr:x}) -> {status}')
|
||||
|
||||
def error(self):
|
||||
print(f'Error: Return value and errno EINTR not handled correctly in {self.last_func_name} (return address 0x{self.last_ret_addr:x})')
|
||||
self.tested_functions[(self.last_func_name, self.last_ret_addr)] = 'failed'
|
||||
self.counter = 0
|
||||
self.last_func_name = None
|
||||
self.last_ret_addr = None
|
||||
|
||||
def before_fallback(self, func_name: str, *args) -> str:
|
||||
if self.while_testing and (self.last_func_name != func_name or self.last_ret_addr != self.ret_addr):
|
||||
self.error()
|
||||
return 'ok'
|
||||
elif func_name not in self.functions:
|
||||
return 'ok'
|
||||
elif self.functions[func_name][0] is None:
|
||||
return self.functions[func_name][1]
|
||||
self.counter += 1
|
||||
if self.while_testing:
|
||||
self.last_ret_addr = self.ret_addr
|
||||
self.last_func_name = func_name
|
||||
self.tested_functions[(self.last_func_name, self.last_ret_addr)] = 'running'
|
||||
return self.functions[func_name][0]
|
||||
else:
|
||||
self.tested_functions[(self.last_func_name, self.last_ret_addr)] = 'passed'
|
||||
self.last_ret_addr = None
|
||||
self.last_func_name = None
|
||||
return self.functions[func_name][1]
|
||||
|
||||
|
||||
def intercept(socket: str, handler: type[Handler]) -> None:
|
||||
try:
|
||||
with ThreadedUnixStreamServer(socket, handler) as server:
|
||||
@@ -459,14 +331,3 @@ def intercept(socket: str, handler: type[Handler]) -> None:
|
||||
os.unlink(socket)
|
||||
except FileNotFoundError:
|
||||
pass
|
||||
|
||||
|
||||
def main() -> None:
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument('socket', metavar='FILE')
|
||||
args = parser.parse_args()
|
||||
intercept(args.socket, Handler)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
165
proj/server/src/intercept/standard.py
Normal file
165
proj/server/src/intercept/standard.py
Normal file
@@ -0,0 +1,165 @@
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
from intercept import *
|
||||
|
||||
|
||||
FUNCTION_ERRORS: dict[str, list[str]] = {
|
||||
'malloc': ['ENOMEM'],
|
||||
'calloc': ['ENOMEM'],
|
||||
'realloc': ['ENOMEM'],
|
||||
'reallocarray': ['ENOMEM'],
|
||||
'close': ['EBADF'], # EINTR, EIO
|
||||
'sigaction': ['EINVAL'],
|
||||
'sem_init': ['EINVAL', 'ENOSYS'],
|
||||
'sem_open': ['EACCES', 'EEXIST', 'EINVAL', 'EMFILE', 'ENAMETOOLONG', 'ENFILE', 'ENOENT', 'ENOMEM'],
|
||||
'sem_post': ['EINVAL', 'EOVERFLOW'],
|
||||
'sem_wait': ['EINTR', 'EINVAL'],
|
||||
'sem_trywait': ['EAGAIN', 'EINTR', 'EINVAL'],
|
||||
'sem_timedwait': ['EINTR', 'EINVAL', 'ETIMEDOUT'],
|
||||
'sem_getvalule': ['EINVAL'],
|
||||
'sem_close': ['EINVAL'],
|
||||
'sem_unlink': ['EACCES', 'ENAMETOOLONG', 'ENOENT'],
|
||||
'sem_destroy': ['EINVAL'],
|
||||
'shm_open': ['EACCES', 'EEXIST', 'EINVAL', 'EMFILE', 'ENAMETOOLONG', 'ENFILE', 'ENOENT'],
|
||||
'shm_unlink': ['EACCES', 'ENAMETOOLONG', 'ENOENT'],
|
||||
'mmap': ['EACCES', 'EBADF', 'EINVAL', 'EMFILE', 'ENODEV', 'ENOMEM', 'ENOTSUP', 'ENXIO', 'EOVERFLOW'], # EAGAIN
|
||||
'munmap': ['EINVAL'],
|
||||
'ftruncate': ['EINTR', 'EINVAL', 'EFBIG', 'EIO', 'EBADF'],
|
||||
}
|
||||
|
||||
SKIP_ERRORS: list[str] = ['EINTR']
|
||||
IGNORE_ERRORS: list[str] = ['EINVAL', 'EBADF', 'EOVERFLOW', 'ENAMETOOLONG']
|
||||
|
||||
|
||||
class MemoryAllocationTester(Handler):
|
||||
allocated: dict[int, tuple[str, int, int]]
|
||||
max_allocated: int
|
||||
num_malloc: int
|
||||
num_realloc: int
|
||||
num_free: int
|
||||
num_invalid_free: int
|
||||
|
||||
def before(self):
|
||||
self.allocated = {}
|
||||
self.max_allocated = 0
|
||||
self.num_malloc = 0
|
||||
self.num_realloc = 0
|
||||
self.num_free = 0
|
||||
self.num_invalid_free = 0
|
||||
|
||||
def after(self):
|
||||
if len(self.allocated) > 0:
|
||||
print("Not free'd:")
|
||||
for ptr, (func, ret, size) in self.allocated.items():
|
||||
print(f' 0x{ptr:x}: {size} bytes ({func}, return address 0x{ret:x})')
|
||||
else:
|
||||
print("All blocks free'd!")
|
||||
print(f'Max allocated: {self.max_allocated} bytes')
|
||||
|
||||
def update_max_allocated(self):
|
||||
total = sum(a[2] for a in self.allocated.values())
|
||||
if total > self.max_allocated:
|
||||
self.max_allocated = total
|
||||
|
||||
def after_malloc(self, size, ret_value, errno=None) -> None:
|
||||
self.num_malloc += 1
|
||||
if ret_value != 0:
|
||||
self.allocated[ret_value] = ('malloc', self.ret_addr, size)
|
||||
self.update_max_allocated()
|
||||
|
||||
def after_calloc(self, nmemb, size, ret_value, errno=None) -> None:
|
||||
self.num_malloc += 1
|
||||
if ret_value != 0:
|
||||
self.allocated[ret_value] = ('calloc', self.ret_addr, nmemb * size)
|
||||
self.update_max_allocated()
|
||||
|
||||
def after_realloc(self, ptr, size, ret_value, errno=None) -> None:
|
||||
self.num_realloc += 1
|
||||
if ptr != 0:
|
||||
if ret_value != 0:
|
||||
v = self.allocated[ptr]
|
||||
del self.allocated[ptr]
|
||||
self.allocated[ret_value] = (v[0], v[1], size)
|
||||
self.update_max_allocated()
|
||||
|
||||
def after_reallocarray(self, ptr, nmemb, size, ret_value, errno=None) -> None:
|
||||
self.num_realloc += 1
|
||||
if ptr != 0:
|
||||
if ret_value != 0:
|
||||
v = self.allocated[ptr]
|
||||
del self.allocated[ptr]
|
||||
self.allocated[ret_value] = (v[0], v[1], nmemb * size)
|
||||
self.update_max_allocated()
|
||||
|
||||
def after_free(self, ptr) -> None:
|
||||
self.num_free += 1
|
||||
if ptr != 0:
|
||||
if ptr in self.allocated:
|
||||
del self.allocated[ptr]
|
||||
else:
|
||||
self.num_free -= 1
|
||||
self.num_invalid_free += 1
|
||||
|
||||
|
||||
class InterruptedCheckTester(Handler):
|
||||
cycles: int = 50
|
||||
functions: dict[str, tuple[str or None, str]] = {
|
||||
fn: ('fail EINTR' if fn not in ('sem_post',) else None,
|
||||
'return 0' if fn.startswith('sem_') else 'ok')
|
||||
for fn, errors in FUNCTION_ERRORS.items()
|
||||
if 'EINTR' in errors or fn in ('sem_post',)
|
||||
}
|
||||
|
||||
counter: int = 0
|
||||
last_func_name: Optional[str] = None
|
||||
last_ret_addr: Optional[int] = None
|
||||
tested_functions: dict[tuple[str, int], str]
|
||||
|
||||
@property
|
||||
def while_testing(self) -> bool:
|
||||
return self.counter % self.cycles != 0
|
||||
|
||||
def before(self) -> None:
|
||||
self.tested_functions = {}
|
||||
|
||||
def after(self) -> None:
|
||||
if self.while_testing:
|
||||
self.error()
|
||||
for (name, ret_addr), status in self.tested_functions.items():
|
||||
print(f'{name} (0x{ret_addr:x}) -> {status}')
|
||||
|
||||
def error(self):
|
||||
print(f'Error: Return value and errno EINTR not handled correctly in {self.last_func_name} (return address 0x{self.last_ret_addr:x})')
|
||||
self.tested_functions[(self.last_func_name, self.last_ret_addr)] = 'failed'
|
||||
self.counter = 0
|
||||
self.last_func_name = None
|
||||
self.last_ret_addr = None
|
||||
|
||||
def before_fallback(self, func_name: str, *args) -> str:
|
||||
if self.while_testing and (self.last_func_name != func_name or self.last_ret_addr != self.ret_addr):
|
||||
self.error()
|
||||
return 'ok'
|
||||
elif func_name not in self.functions:
|
||||
return 'ok'
|
||||
elif self.functions[func_name][0] is None:
|
||||
return self.functions[func_name][1]
|
||||
self.counter += 1
|
||||
if self.while_testing:
|
||||
self.last_ret_addr = self.ret_addr
|
||||
self.last_func_name = func_name
|
||||
self.tested_functions[(self.last_func_name, self.last_ret_addr)] = 'running'
|
||||
return self.functions[func_name][0]
|
||||
else:
|
||||
self.tested_functions[(self.last_func_name, self.last_ret_addr)] = 'passed'
|
||||
self.last_ret_addr = None
|
||||
self.last_func_name = None
|
||||
return self.functions[func_name][1]
|
||||
|
||||
|
||||
class ReturnValueCheckTester(Handler):
|
||||
functions: dict[str, list[str]] = {
|
||||
fn: [e for e in errors if e not in SKIP_ERRORS and e not in IGNORE_ERRORS]
|
||||
for fn, errors in FUNCTION_ERRORS.items()
|
||||
if len(set(errors) - set(SKIP_ERRORS) - set(IGNORE_ERRORS)) > 0
|
||||
}
|
||||
@@ -1,46 +0,0 @@
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
import argparse
|
||||
|
||||
import intercept
|
||||
|
||||
|
||||
class TestHandler(intercept.Handler):
|
||||
allocated: dict[int, int]
|
||||
|
||||
def before(self):
|
||||
self.allocated = {}
|
||||
|
||||
def after(self):
|
||||
if len(self.allocated) > 0:
|
||||
print("Not free'd:")
|
||||
for ptr, size in self.allocated.items():
|
||||
print(f' 0x{ptr:x}: {size} bytes')
|
||||
else:
|
||||
print("All blocks free'd!")
|
||||
|
||||
def after_malloc(self, size, ret_value, errno=None) -> None:
|
||||
if ret_value != 0:
|
||||
self.allocated[ret_value] = size
|
||||
|
||||
def before_free(self, ptr) -> str:
|
||||
if ptr != 0:
|
||||
del self.allocated[ptr]
|
||||
return 'ok'
|
||||
|
||||
|
||||
class GetoptHandler(intercept.Handler):
|
||||
def after_getopt(self, argc, argv, optstring, ret_value) -> None:
|
||||
print(argc, [v[1] for v in argv[1]], optstring[1], ret_value)
|
||||
|
||||
|
||||
def main() -> None:
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument('socket', metavar='FILE')
|
||||
args = parser.parse_args()
|
||||
intercept.intercept(args.socket, intercept.MemoryAllocationTester)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
28
proj/server/src/test-interrupts
Executable file
28
proj/server/src/test-interrupts
Executable file
@@ -0,0 +1,28 @@
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
import os
|
||||
import argparse
|
||||
import threading
|
||||
import subprocess
|
||||
|
||||
import intercept
|
||||
import intercept.standard
|
||||
|
||||
|
||||
def socket_thread(socket: str) -> None:
|
||||
intercept.intercept(socket, intercept.standard.InterruptedCheckTester)
|
||||
|
||||
|
||||
def main() -> None:
|
||||
parser = argparse.ArgumentParser()
|
||||
args, extra = parser.parse_known_args()
|
||||
socket_name = f'/tmp/intercept.interrupts.{os.getpid()}.sock'
|
||||
t1 = threading.Thread(target=socket_thread, args=(socket_name,))
|
||||
t1.daemon = True
|
||||
t1.start()
|
||||
subprocess.run(extra, env={'LD_PRELOAD': os.getcwd() + '/../../intercept/intercept.so', 'INTERCEPT': 'unix:' + socket_name})
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
28
proj/server/src/test-memory
Executable file
28
proj/server/src/test-memory
Executable file
@@ -0,0 +1,28 @@
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
import os
|
||||
import argparse
|
||||
import threading
|
||||
import subprocess
|
||||
|
||||
import intercept
|
||||
import intercept.standard
|
||||
|
||||
|
||||
def socket_thread(socket: str) -> None:
|
||||
intercept.intercept(socket, intercept.standard.MemoryAllocationTester)
|
||||
|
||||
|
||||
def main() -> None:
|
||||
parser = argparse.ArgumentParser()
|
||||
args, extra = parser.parse_known_args()
|
||||
socket_name = f'/tmp/intercept.memory.{os.getpid()}.sock'
|
||||
t1 = threading.Thread(target=socket_thread, args=(socket_name,))
|
||||
t1.daemon = True
|
||||
t1.start()
|
||||
subprocess.run(extra, env={'LD_PRELOAD': os.getcwd() + '/../../intercept/intercept.so', 'INTERCEPT': 'unix:' + socket_name})
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
28
proj/server/src/test-return-values
Executable file
28
proj/server/src/test-return-values
Executable file
@@ -0,0 +1,28 @@
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
import os
|
||||
import argparse
|
||||
import threading
|
||||
import subprocess
|
||||
|
||||
import intercept
|
||||
import intercept.standard
|
||||
|
||||
|
||||
def socket_thread(socket: str) -> None:
|
||||
intercept.intercept(socket, intercept.standard.ReturnValueCheckTester)
|
||||
|
||||
|
||||
def main() -> None:
|
||||
parser = argparse.ArgumentParser()
|
||||
args, extra = parser.parse_known_args()
|
||||
socket_name = f'/tmp/intercept.return-values.{os.getpid()}.sock'
|
||||
t1 = threading.Thread(target=socket_thread, args=(socket_name,))
|
||||
t1.daemon = True
|
||||
t1.start()
|
||||
subprocess.run(extra, env={'LD_PRELOAD': os.getcwd() + '/../../intercept/intercept.so', 'INTERCEPT': 'unix:' + socket_name})
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
Reference in New Issue
Block a user