proj: Restructure
This commit is contained in:
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
|
||||
}
|
||||
Reference in New Issue
Block a user