proj/test-interrupts: Output well-formatted report
This commit is contained in:
@@ -26,6 +26,40 @@ StructMsgHdr = TypedDict('StructMsgHdr', {})
|
||||
RET_ADDR_RE = re.compile(r': *((0x)?[0-9a-fA-Fx]+) *\((.+?)\+(.+?)(, *([^:]+?))?(, *(([^:]+?):([0-9]+)))?\)$')
|
||||
|
||||
|
||||
class FunctionCallId:
|
||||
func_name: str
|
||||
ret_addr: int
|
||||
obj_path: str
|
||||
rel_ret_addr: int
|
||||
sym_name: Optional[str]
|
||||
src_file_name: Optional[str]
|
||||
src_line_num: Optional[int]
|
||||
|
||||
@property
|
||||
def obj_name(self) -> str:
|
||||
return self.obj_path.split('/')[-1]
|
||||
|
||||
@property
|
||||
def discriminator(self) -> str:
|
||||
discr = [f'{self.obj_name}+0x{self.rel_ret_addr:x}']
|
||||
if self.sym_name:
|
||||
discr.append(self.sym_name)
|
||||
if self.src_file_name and self.src_line_num:
|
||||
discr.append(f'{self.src_file_name}:{self.src_line_num}')
|
||||
return ', '.join(discr)
|
||||
|
||||
def __eq__(self, other) -> bool:
|
||||
if not isinstance(other, FunctionCallId):
|
||||
return False
|
||||
return (self.func_name, self.obj_path, self.rel_ret_addr) == (other.func_name, other.obj_path, other.rel_ret_addr)
|
||||
|
||||
def __hash__(self) -> int:
|
||||
return hash((self.func_name, self.obj_path, self.rel_ret_addr))
|
||||
|
||||
def __str__(self) -> str:
|
||||
return self.func_name + ', ' + self.discriminator
|
||||
|
||||
|
||||
class ThreadedUnixStreamServer(ThreadingMixIn, UnixStreamServer):
|
||||
pass
|
||||
|
||||
@@ -51,6 +85,17 @@ class Parser:
|
||||
self.pid = None
|
||||
self.path = None
|
||||
|
||||
def get_call_id(self, func_name: str) -> FunctionCallId:
|
||||
call_id = FunctionCallId()
|
||||
call_id.func_name = func_name
|
||||
call_id.ret_addr = self.ret_addr
|
||||
call_id.obj_path = self.dli_file_name
|
||||
call_id.rel_ret_addr = self.rel_ret_addr
|
||||
call_id.sym_name = self.dli_sym_name
|
||||
call_id.src_file_name = self.src_file_name
|
||||
call_id.src_line_num = self.src_line_num
|
||||
return call_id
|
||||
|
||||
def before(self) -> None: pass
|
||||
def after(self) -> None: pass
|
||||
def before_fallback(self, func_name: str, *args) -> str: pass
|
||||
|
||||
@@ -2,7 +2,6 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
from intercept import *
|
||||
import sys
|
||||
|
||||
|
||||
FUNCTION_ERRORS: dict[str, list[str]] = {
|
||||
@@ -54,8 +53,8 @@ SKIP_ERRORS: list[str] = ['EINTR']
|
||||
|
||||
|
||||
class MemoryAllocationParser(Parser):
|
||||
allocated: dict[int, tuple[str, int, str, str, int, str, int]]
|
||||
invalid_frees: list[tuple[str, int, str, str, int, str, int]]
|
||||
allocated: dict[int, tuple[FunctionCallId, int]]
|
||||
invalid_frees: list[tuple[FunctionCallId, int]]
|
||||
max_allocated: int
|
||||
num_alloc: int
|
||||
num_realloc: int
|
||||
@@ -77,56 +76,46 @@ class MemoryAllocationParser(Parser):
|
||||
def after_malloc(self, size, ret_value, errno=None) -> None:
|
||||
self.num_alloc += 1
|
||||
if ret_value != 0:
|
||||
self.allocated[ret_value] = ('malloc', size,
|
||||
self.dli_file_name, self.dli_sym_name, self.rel_ret_addr,
|
||||
self.src_file_name, self.src_line_num)
|
||||
self.allocated[ret_value] = (self.get_call_id('malloc'), size)
|
||||
self.update_max_allocated()
|
||||
|
||||
def after_calloc(self, nmemb, size, ret_value, errno=None) -> None:
|
||||
self.num_alloc += 1
|
||||
if ret_value != 0:
|
||||
self.allocated[ret_value] = ('calloc', nmemb * size,
|
||||
self.dli_file_name, self.dli_sym_name, self.rel_ret_addr,
|
||||
self.src_file_name, self.src_line_num)
|
||||
self.allocated[ret_value] = (self.get_call_id('calloc'), nmemb * size)
|
||||
self.update_max_allocated()
|
||||
|
||||
def after_realloc(self, ptr, size, ret_value, errno=None) -> None:
|
||||
if ptr == 0:
|
||||
self.num_alloc += 1
|
||||
if ret_value != 0:
|
||||
self.allocated[ret_value] = ('realloc', size,
|
||||
self.dli_file_name, self.dli_sym_name, self.rel_ret_addr,
|
||||
self.src_file_name, self.src_line_num)
|
||||
self.allocated[ret_value] = (self.get_call_id('realloc'), size)
|
||||
else:
|
||||
self.num_realloc += 1
|
||||
if ret_value != 0 and ptr in self.allocated:
|
||||
v = self.allocated[ptr]
|
||||
del self.allocated[ptr]
|
||||
self.allocated[ret_value] = (v[0], size, v[2], v[3], v[4], v[5], v[6])
|
||||
self.allocated[ret_value] = (v[0], size)
|
||||
self.update_max_allocated()
|
||||
|
||||
def after_reallocarray(self, ptr, nmemb, size, ret_value, errno=None) -> None:
|
||||
if ptr == 0:
|
||||
self.num_alloc += 1
|
||||
if ret_value != 0:
|
||||
self.allocated[ret_value] = ('reallocarray', nmemb * size,
|
||||
self.dli_file_name, self.dli_sym_name, self.rel_ret_addr,
|
||||
self.src_file_name, self.src_line_num)
|
||||
self.allocated[ret_value] = (self.get_call_id('reallocarray'), nmemb * size)
|
||||
else:
|
||||
self.num_realloc += 1
|
||||
if ret_value != 0:
|
||||
v = self.allocated[ptr]
|
||||
del self.allocated[ptr]
|
||||
self.allocated[ret_value] = (v[0], nmemb * size, v[2], v[3], v[4], v[5], v[6])
|
||||
self.allocated[ret_value] = (v[0], 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', size,
|
||||
self.dli_file_name, self.dli_sym_name, self.rel_ret_addr,
|
||||
self.src_file_name, self.src_line_num)
|
||||
self.allocated[res[0]] = (self.get_call_id('getaddrinfo'), size)
|
||||
self.update_max_allocated()
|
||||
|
||||
def after_free(self, ptr) -> None:
|
||||
@@ -136,9 +125,7 @@ class MemoryAllocationParser(Parser):
|
||||
del self.allocated[ptr]
|
||||
else:
|
||||
self.num_free -= 1
|
||||
self.invalid_frees.append(('free', ptr,
|
||||
self.dli_file_name, self.dli_sym_name, self.rel_ret_addr,
|
||||
self.src_file_name, self.src_line_num))
|
||||
self.invalid_frees.append((self.get_call_id('free'), ptr))
|
||||
|
||||
def after_freeaddrinfo(self, res: Pointer) -> None:
|
||||
self.num_free += 1
|
||||
@@ -147,9 +134,7 @@ class MemoryAllocationParser(Parser):
|
||||
del self.allocated[res]
|
||||
else:
|
||||
self.num_free -= 1
|
||||
self.invalid_frees.append(('freeaddrinfo', res,
|
||||
self.dli_file_name, self.dli_sym_name, self.rel_ret_addr,
|
||||
self.src_file_name, self.src_line_num))
|
||||
self.invalid_frees.append((self.get_call_id('freeaddrinfo'), res))
|
||||
|
||||
|
||||
class MemoryAllocationTester(MemoryAllocationParser, Handler):
|
||||
@@ -164,37 +149,32 @@ class InterruptedCheckParser(Parser):
|
||||
for fn, errors in FUNCTION_ERRORS.items()
|
||||
if 'EINTR' in errors or fn in ('sem_post',)
|
||||
}
|
||||
context: dict
|
||||
|
||||
counter: int = 0
|
||||
last_func_name: Optional[str] = None
|
||||
last_ret_addr: Optional[int] = None
|
||||
tested_functions: dict[tuple[str, int], str]
|
||||
last_call: Optional[FunctionCallId] = None
|
||||
tested_function_calls: dict[FunctionCallId, str]
|
||||
|
||||
@property
|
||||
def while_testing(self) -> bool:
|
||||
return self.counter % self.cycles != 0
|
||||
|
||||
def before(self) -> None:
|
||||
self.tested_functions = {}
|
||||
self.tested_function_calls = {}
|
||||
|
||||
def after(self) -> None:
|
||||
if self.while_testing:
|
||||
self.error()
|
||||
for (name, ret_addr), status in self.tested_functions.items():
|
||||
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)
|
||||
self.context['results'] = self.tested_function_calls
|
||||
|
||||
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})', file=sys.stderr)
|
||||
self.tested_functions[(self.last_func_name, self.last_ret_addr)] = 'failed'
|
||||
self.tested_function_calls[self.last_call] = 'failed'
|
||||
self.counter = 0
|
||||
self.last_func_name = None
|
||||
self.last_ret_addr = None
|
||||
self.last_call = 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):
|
||||
call = self.get_call_id(func_name)
|
||||
if self.while_testing and self.last_call != call:
|
||||
self.error()
|
||||
return 'ok'
|
||||
elif func_name not in self.functions:
|
||||
@@ -203,14 +183,12 @@ class InterruptedCheckParser(Parser):
|
||||
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'
|
||||
self.last_call = call
|
||||
self.tested_function_calls[self.last_call] = '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
|
||||
self.tested_function_calls[self.last_call] = 'passed'
|
||||
self.last_call = None
|
||||
return self.functions[func_name][1]
|
||||
|
||||
|
||||
@@ -218,91 +196,97 @@ class InterruptedCheckTester(InterruptedCheckParser, Handler):
|
||||
pass
|
||||
|
||||
|
||||
def get_return_value_check_tester() -> tuple[dict, type[Parser]]:
|
||||
ctx = {
|
||||
'state': 'init',
|
||||
'call_sequence': [],
|
||||
'next': None,
|
||||
'last_error': None,
|
||||
'results': {},
|
||||
class ReturnValueCheckParser(Parser):
|
||||
functions: dict[str, list[str]] = {
|
||||
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)) > 0
|
||||
}
|
||||
context: dict
|
||||
num: int = 0
|
||||
|
||||
class ReturnValueCheckTester(Parser):
|
||||
functions: dict[str, list[str]] = {
|
||||
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)) > 0
|
||||
}
|
||||
context: dict
|
||||
num: int = 0
|
||||
@property
|
||||
def call_sequence(self) -> list[str]:
|
||||
return self.context['call_sequence']
|
||||
|
||||
@property
|
||||
def call_sequence(self) -> list[str]:
|
||||
return self.context['call_sequence']
|
||||
@property
|
||||
def is_init(self) -> bool:
|
||||
return self.context['state'] == 'init'
|
||||
|
||||
@property
|
||||
def is_init(self) -> bool:
|
||||
return self.context['state'] == 'init'
|
||||
@property
|
||||
def is_testing(self) -> bool:
|
||||
return self.context['state'] == 'testing'
|
||||
|
||||
@property
|
||||
def is_testing(self) -> bool:
|
||||
return self.context['state'] == 'testing'
|
||||
@property
|
||||
def is_waiting(self) -> bool:
|
||||
return self.context['state'] == 'waiting'
|
||||
|
||||
@property
|
||||
def is_waiting(self) -> bool:
|
||||
return self.context['state'] == 'waiting'
|
||||
@property
|
||||
def is_failed(self) -> bool:
|
||||
return self.context['state'] == 'failed'
|
||||
|
||||
@property
|
||||
def is_failed(self) -> bool:
|
||||
return self.context['state'] == 'failed'
|
||||
def next(self, last: Optional[tuple[int, Optional[str]]]) -> tuple[int, Optional[str]]:
|
||||
if last is None:
|
||||
if len(self.call_sequence) == 0:
|
||||
return 0, None
|
||||
err = self.functions.get(self.call_sequence[0], [None])[0]
|
||||
return (0, err) if err else self.next((0, err))
|
||||
last_fn, last_er = last
|
||||
if last_er is None:
|
||||
if last_fn + 1 >= len(self.call_sequence):
|
||||
return last_fn + 1, None
|
||||
err = self.functions.get(self.call_sequence[last_fn + 1], [None])[0]
|
||||
return (last_fn + 1, err) if err else self.next((last_fn + 1, err))
|
||||
errors = self.functions.get(self.call_sequence[last_fn], [])
|
||||
i = errors.index(last_er)
|
||||
return (last_fn, errors[i + 1]) if i + 1 < len(errors) else self.next((last_fn, None))
|
||||
|
||||
def next(self, last: Optional[tuple[int, Optional[str]]]) -> tuple[int, Optional[str]]:
|
||||
if last is None:
|
||||
if len(self.call_sequence) == 0:
|
||||
return 0, None
|
||||
err = self.functions.get(self.call_sequence[0], [None])[0]
|
||||
return (0, err) if err else self.next((0, err))
|
||||
last_fn, last_er = last
|
||||
if last_er is None:
|
||||
if last_fn + 1 >= len(self.call_sequence):
|
||||
return last_fn + 1, None
|
||||
err = self.functions.get(self.call_sequence[last_fn + 1], [None])[0]
|
||||
return (last_fn + 1, err) if err else self.next((last_fn + 1, err))
|
||||
errors = self.functions.get(self.call_sequence[last_fn], [])
|
||||
i = errors.index(last_er)
|
||||
return (last_fn, errors[i + 1]) if i + 1 < len(errors) else self.next((last_fn, None))
|
||||
def before(self) -> None:
|
||||
if 'state' not in self.context:
|
||||
self.context['state'] = 'init'
|
||||
self.context['state'] = 'init'
|
||||
self.context['call_sequence'] = []
|
||||
self.context['next'] = None
|
||||
self.context['last_error'] = None
|
||||
self.context['results'] = {}
|
||||
if self.is_waiting or self.is_failed:
|
||||
self.context['state'] = 'testing'
|
||||
|
||||
def before(self) -> None:
|
||||
self.context = ctx
|
||||
if self.is_waiting or self.is_failed:
|
||||
self.context['state'] = 'testing'
|
||||
def after(self) -> None:
|
||||
if self.is_init:
|
||||
self.context['state'] = 'testing'
|
||||
self.context['next'] = self.next(None)
|
||||
elif self.is_testing and self.context['next'][0] > len(self.context['call_sequence']):
|
||||
self.context['state'] = 'finished'
|
||||
elif self.is_waiting:
|
||||
self.context['results'][(self.num - 1, self.context['last_error'])] = 'passed'
|
||||
|
||||
def after(self) -> None:
|
||||
if self.is_init:
|
||||
self.context['state'] = 'testing'
|
||||
self.context['next'] = self.next(None)
|
||||
elif self.is_testing and self.context['next'][0] > len(self.context['call_sequence']):
|
||||
self.context['state'] = 'finished'
|
||||
elif self.is_waiting:
|
||||
self.context['results'][(self.num - 1, self.context['last_error'])] = 'passed'
|
||||
def before_fallback(self, func_name: str, *args) -> str:
|
||||
if self.is_init:
|
||||
self.call_sequence.append(func_name)
|
||||
return 'ok'
|
||||
elif self.is_waiting or self.is_failed:
|
||||
print(func_name)
|
||||
if func_name not in ('malloc', 'free', 'close', 'exit'):
|
||||
self.context['state'] = 'failed'
|
||||
self.context['results'][(self.num - 1, self.context['last_error'])] = 'failed'
|
||||
return 'ok'
|
||||
self.num += 1
|
||||
nxt = self.context['next']
|
||||
if self.num - 1 != nxt[0] or nxt[1] is None:
|
||||
return 'ok'
|
||||
self.context['next'] = self.next(nxt)
|
||||
self.context['state'] = 'waiting' if self.context['next'][1] else 'finished'
|
||||
self.context['last_error'] = nxt[1]
|
||||
return 'fail ' + nxt[1]
|
||||
|
||||
def before_fallback(self, func_name: str, *args) -> str:
|
||||
if self.is_init:
|
||||
self.call_sequence.append(func_name)
|
||||
return 'ok'
|
||||
elif self.is_waiting or self.is_failed:
|
||||
print(func_name)
|
||||
if func_name not in ('malloc', 'free', 'close', 'exit'):
|
||||
self.context['state'] = 'failed'
|
||||
self.context['results'][(self.num - 1, self.context['last_error'])] = 'failed'
|
||||
return 'ok'
|
||||
self.num += 1
|
||||
nxt = self.context['next']
|
||||
if self.num - 1 != nxt[0] or nxt[1] is None:
|
||||
return 'ok'
|
||||
self.context['next'] = self.next(nxt)
|
||||
self.context['state'] = 'waiting' if self.context['next'][1] else 'finished'
|
||||
self.context['last_error'] = nxt[1]
|
||||
return 'fail ' + nxt[1]
|
||||
|
||||
return ctx, ReturnValueCheckTester
|
||||
class ReturnValueCheckTester(ReturnValueCheckParser, Handler):
|
||||
pass
|
||||
|
||||
|
||||
def init_with_ctx(parser: type[Parser]) -> tuple[dict, type[Parser]]:
|
||||
ctx = {}
|
||||
class ContextParser(parser):
|
||||
context: dict = ctx
|
||||
return ctx, ContextParser
|
||||
|
||||
Reference in New Issue
Block a user