jwilk / python-afl

American Fuzzy Lop fork server and instrumentation for pure-Python code
https://jwilk.net/software/python-afl
MIT License
350 stars 33 forks source link

opcode tracing #15

Open lazka opened 4 years ago

lazka commented 4 years ago

Hey,

I was wondering if the opcode tracing added with Python 3.7 might be useful:

def wrap(frame, event, arg):
    frame.f_trace_lines = False
    frame.f_trace_opcodes = True
    code = frame.f_code
    if event == "opcode":
        filename = code.co_filename
        linenumber = frame.f_lineno
        opcode = dis.opname[code.co_code[frame.f_lasti]]
        print(level * 4 * " ", [event, filename, linenumber, opcode])
    else:
        ....
 ['call', <frame at 0x7f1dfd1cb040, file 'settrace.py', line 44, code mytestfunction>, None]
     ['opcode', 'settrace.py', 45, 'LOAD_CONST']
     ['opcode', 'settrace.py', 45, 'LOAD_FAST']
     ['opcode', 'settrace.py', 45, 'COMPARE_OP']
     ['opcode', 'settrace.py', 45, 'POP_JUMP_IF_FALSE']
     ['opcode', 'settrace.py', 45, 'LOAD_CONST']
     ['opcode', 'settrace.py', 45, 'LOAD_FAST']
     ['opcode', 'settrace.py', 45, 'COMPARE_OP']
     ['opcode', 'settrace.py', 45, 'POP_JUMP_IF_FALSE']
     ['opcode', 'settrace.py', 45, 'LOAD_CONST']
     ['opcode', 'settrace.py', 45, 'LOAD_FAST']
     ['opcode', 'settrace.py', 45, 'COMPARE_OP']
     ['opcode', 'settrace.py', 45, 'POP_JUMP_IF_FALSE']
     ['opcode', 'settrace.py', 45, 'LOAD_CONST']
     ['opcode', 'settrace.py', 45, 'RETURN_VALUE']
     ['return', <frame at 0x7f1dfd1cb040, file 'settrace.py', line 45, code mytestfunction>, None]

From what I understand it would make it easier to get coverage for multipl conditions per line like

if foo and bar and quux:
    ...

But not sure and not sure what the performance impact is.

jwilk commented 4 years ago

Sounds interesting, but I won't have time to work on this any time soon.

lazka commented 4 years ago

Thanks, maybe I will

After fixing the 1000 bugs this has found for me :P, in other words, thanks for this!