proj/server: Update testers
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
from intercept import *
|
||||
import sys
|
||||
|
||||
|
||||
FUNCTION_ERRORS: dict[str, list[str]] = {
|
||||
@@ -9,30 +10,30 @@ FUNCTION_ERRORS: dict[str, list[str]] = {
|
||||
'calloc': ['ENOMEM'],
|
||||
'realloc': ['ENOMEM'],
|
||||
'reallocarray': ['ENOMEM'],
|
||||
'read': [],
|
||||
'pread': [],
|
||||
'write': [],
|
||||
'pwrite': [],
|
||||
'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'],
|
||||
'fork': ['EAGAIN', 'ENOMEM', 'ENOSYS'],
|
||||
'read': ['EINTR', 'EIO', 'ECONNRESET', 'ENOTCONN', 'ETIMEDOUT'],
|
||||
'pread': ['EINTR', 'EIO'],
|
||||
'write': ['EINTR', 'EIO', 'EDQUOT', 'EFBIG', 'ENOSPC', 'EPIPE', 'ECONNRESET'],
|
||||
'pwrite': ['EINTR', 'EIO', 'EDQUOT', 'EFBIG', 'ENOSPC', 'EPIPE'],
|
||||
'close': [],
|
||||
'sigaction': [],
|
||||
'sem_init': ['ENOSYS'],
|
||||
'sem_open': ['EACCES', 'EEXIST', 'EMFILE', 'ENFILE', 'ENOENT', 'ENOMEM'],
|
||||
'sem_post': [],
|
||||
'sem_wait': ['EINTR'],
|
||||
'sem_trywait': ['EINTR'],
|
||||
'sem_timedwait': ['EINTR', 'ETIMEDOUT'],
|
||||
'sem_getvalule': [],
|
||||
'sem_close': [],
|
||||
'sem_unlink': ['EACCES', 'ENOENT'],
|
||||
'sem_destroy': [],
|
||||
'shm_open': ['EACCES', 'EEXIST', 'EMFILE', 'ENFILE', 'ENOENT'],
|
||||
'shm_unlink': ['EACCES', 'ENOENT'],
|
||||
'mmap': ['EACCES', 'EBADF', 'EMFILE', 'ENODEV', 'ENOMEM', 'ENOTSUP', 'ENXIO'],
|
||||
'munmap': [],
|
||||
'ftruncate': ['EINTR', 'EFBIG', 'EIO', 'EBADF'],
|
||||
'fork': ['ENOMEM', 'ENOSYS'],
|
||||
'wait': ['ECHILD', 'EINTR'],
|
||||
'waitpid': ['ECHILD', 'EINTR', 'EINVAL'],
|
||||
'waitpid': ['ECHILD', 'EINTR'],
|
||||
'execl': ['ECHILD', 'EINTR'],
|
||||
'execlp': ['ECHILD', 'EINTR'],
|
||||
'execle': ['ECHILD', 'EINTR'],
|
||||
@@ -42,19 +43,20 @@ FUNCTION_ERRORS: dict[str, list[str]] = {
|
||||
'execve': ['ECHILD', 'EINTR'],
|
||||
'fexecve': ['ECHILD', 'EINTR'],
|
||||
'pipe': ['EMFILE', 'ENFILE'],
|
||||
'dup': ['EBADF', 'EMFILE'], # ENOMEM
|
||||
'dup2': ['EBADF', 'EBUSY', 'EINTR', 'EMFILE'], # ENOMEM
|
||||
'dup3': ['EBADF', 'EBUSY', 'EINTR', 'EINVAL', 'EMFILE'], # ENOMEM
|
||||
'dup': ['EBADF', 'EMFILE'],
|
||||
'dup2': ['EBADF', 'EBUSY', 'EINTR', 'EMFILE'],
|
||||
'dup3': ['EBADF', 'EBUSY', 'EINTR', 'EMFILE'],
|
||||
'send': ['EINTR'],
|
||||
'recv': ['EINTR'],
|
||||
}
|
||||
|
||||
SKIP_ERRORS: list[str] = ['EINTR']
|
||||
IGNORE_ERRORS: list[str] = ['EINVAL', 'EBADF', 'EOVERFLOW', 'ENAMETOOLONG']
|
||||
|
||||
|
||||
class MemoryAllocationTester(Handler):
|
||||
allocated: dict[int, tuple[str, int, int]]
|
||||
class MemoryAllocationParser(Parser):
|
||||
allocated: dict[int, tuple[str, str, str, int, int]]
|
||||
max_allocated: int
|
||||
num_malloc: int
|
||||
num_alloc: int
|
||||
num_realloc: int
|
||||
num_free: int
|
||||
num_invalid_free: int
|
||||
@@ -62,44 +64,44 @@ class MemoryAllocationTester(Handler):
|
||||
def before(self):
|
||||
self.allocated = {}
|
||||
self.max_allocated = 0
|
||||
self.num_malloc = 0
|
||||
self.num_alloc = 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})')
|
||||
print("\x1B[31;1mNot free'd:\x1B[0m", file=sys.stderr)
|
||||
for ptr, (func, fname, sname, ret, size) in self.allocated.items():
|
||||
print(f'\x1B[31;1m 0x{ptr:x}: {size} bytes ({func}, return address {fname}+0x{ret:x} {sname})\x1B[0m', file=sys.stderr)
|
||||
else:
|
||||
print("All blocks free'd!")
|
||||
print(f'Max allocated: {self.max_allocated} bytes')
|
||||
print("\x1B[32;1mAll blocks free'd!\x1B[0m", file=sys.stderr)
|
||||
print(f'Max allocated: {self.max_allocated} bytes', file=sys.stderr)
|
||||
|
||||
def update_max_allocated(self):
|
||||
total = sum(a[2] for a in self.allocated.values())
|
||||
total = sum(a[-1] 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
|
||||
self.num_alloc += 1
|
||||
if ret_value != 0:
|
||||
self.allocated[ret_value] = ('malloc', self.ret_addr, size)
|
||||
self.allocated[ret_value] = ('malloc', self.dli_file_name, self.dli_sym_name, self.rel_ret_addr, size)
|
||||
self.update_max_allocated()
|
||||
|
||||
def after_calloc(self, nmemb, size, ret_value, errno=None) -> None:
|
||||
self.num_malloc += 1
|
||||
self.num_alloc += 1
|
||||
if ret_value != 0:
|
||||
self.allocated[ret_value] = ('calloc', self.ret_addr, nmemb * size)
|
||||
self.allocated[ret_value] = ('calloc', self.dli_file_name, self.dli_sym_name, self.rel_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:
|
||||
if ret_value != 0 and ptr in self.allocated:
|
||||
v = self.allocated[ptr]
|
||||
del self.allocated[ptr]
|
||||
self.allocated[ret_value] = (v[0], v[1], size)
|
||||
self.allocated[ret_value] = (v[0], v[1], v[2], v[3], size)
|
||||
self.update_max_allocated()
|
||||
|
||||
def after_reallocarray(self, ptr, nmemb, size, ret_value, errno=None) -> None:
|
||||
@@ -108,9 +110,16 @@ class MemoryAllocationTester(Handler):
|
||||
if ret_value != 0:
|
||||
v = self.allocated[ptr]
|
||||
del self.allocated[ptr]
|
||||
self.allocated[ret_value] = (v[0], v[1], nmemb * size)
|
||||
self.allocated[ret_value] = (v[0], v[1], v[2], v[3], nmemb * size)
|
||||
self.update_max_allocated()
|
||||
|
||||
def after_getaddrinfo(self, node, service, hints, res_ptr, ret_value, errno=None, res=None) -> None:
|
||||
self.num_alloc += 1
|
||||
if ret_value[0] == 0 and res is not None:
|
||||
size = sum(48 + r['ai_addrlen'] for r in res[1])
|
||||
self.allocated[res[0]] = ('getaddrinfo', self.dli_file_name, self.dli_sym_name, self.rel_ret_addr, size)
|
||||
self.update_max_allocated()
|
||||
|
||||
def after_free(self, ptr) -> None:
|
||||
self.num_free += 1
|
||||
if ptr != 0:
|
||||
@@ -120,8 +129,21 @@ class MemoryAllocationTester(Handler):
|
||||
self.num_free -= 1
|
||||
self.num_invalid_free += 1
|
||||
|
||||
def after_freeaddrinfo(self, res: Pointer) -> None:
|
||||
self.num_free += 1
|
||||
if res != 0:
|
||||
if res in self.allocated:
|
||||
del self.allocated[res]
|
||||
else:
|
||||
self.num_free -= 1
|
||||
self.num_invalid_free += 1
|
||||
|
||||
class InterruptedCheckTester(Handler):
|
||||
|
||||
class MemoryAllocationTester(MemoryAllocationParser, Handler):
|
||||
pass
|
||||
|
||||
|
||||
class InterruptedCheckParser(Parser):
|
||||
cycles: int = 50
|
||||
functions: dict[str, tuple[str or None, str]] = {
|
||||
fn: ('fail EINTR' if fn not in ('sem_post',) else None,
|
||||
@@ -146,10 +168,13 @@ class InterruptedCheckTester(Handler):
|
||||
if self.while_testing:
|
||||
self.error()
|
||||
for (name, ret_addr), status in self.tested_functions.items():
|
||||
print(f'{name} (0x{ret_addr:x}) -> {status}')
|
||||
if status == 'passed':
|
||||
print(f'\x1B[32;1m{name} (0x{ret_addr:x}) -> {status}\x1B[0m', file=sys.stderr)
|
||||
else:
|
||||
print(f'\x1B[31;1m{name} (0x{ret_addr:x}) -> {status}\x1B[0m', file=sys.stderr)
|
||||
|
||||
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})')
|
||||
print(f'Error: Return value and errno EINTR not handled correctly in {self.last_func_name} (return address 0x{self.last_ret_addr:x})', file=sys.stderr)
|
||||
self.tested_functions[(self.last_func_name, self.last_ret_addr)] = 'failed'
|
||||
self.counter = 0
|
||||
self.last_func_name = None
|
||||
@@ -176,7 +201,11 @@ class InterruptedCheckTester(Handler):
|
||||
return self.functions[func_name][1]
|
||||
|
||||
|
||||
def get_return_value_check_tester() -> tuple[dict, type[Handler]]:
|
||||
class InterruptedCheckTester(InterruptedCheckParser, Handler):
|
||||
pass
|
||||
|
||||
|
||||
def get_return_value_check_tester() -> tuple[dict, type[Parser]]:
|
||||
ctx = {
|
||||
'state': 'init',
|
||||
'call_sequence': [],
|
||||
@@ -185,11 +214,11 @@ def get_return_value_check_tester() -> tuple[dict, type[Handler]]:
|
||||
'results': {},
|
||||
}
|
||||
|
||||
class ReturnValueCheckTester(Handler):
|
||||
class ReturnValueCheckTester(Parser):
|
||||
functions: dict[str, list[str]] = {
|
||||
fn: [e for e in errors if e not in SKIP_ERRORS and e not in IGNORE_ERRORS]
|
||||
fn: [e for e in errors if e not in SKIP_ERRORS]
|
||||
for fn, errors in FUNCTION_ERRORS.items()
|
||||
if len(set(errors) - set(SKIP_ERRORS) - set(IGNORE_ERRORS)) > 0
|
||||
if len(set(errors) - set(SKIP_ERRORS)) > 0
|
||||
}
|
||||
context: dict
|
||||
num: int = 0
|
||||
|
||||
Reference in New Issue
Block a user