I'm thinking to do some bytecode manipulation (think genetic programming) in Python.
I came across a test case in crashers test section of Python source tree that states:
Broken bytecode objects can easily crash the interpreter. This is not going to be fixed.
Thus the question, how to validate given tweaked byte code that it will not crash interpreter? Is it even possible?
Test source, after http://nedbatchelder.com/blog/201206/eval_really_is_dangerous.html
cc = (lambda fc=(
lambda n: [
c for c in
().__class__.__bases__[0].__subclasses__()
if c.__name__ == n
][0]
):
fc("function")(
fc("code")(
0, 0, 0, 0, "KABOOM", (), (), (), "", "", 0, ""
), {}
)()
)
Here, this module defines cc
that, if called, mymod.cc()
crashes interpreter. Granted this is a very tricky example that created new code object with custom bytecode "KABOOM"
and then runs it.
I'd accept something that verifies predefined bytecode, e.g. from a .pyc
file.
Using a byte code Assembler does the Stack tracking across jumps, globally verifying stack level prediction consistency and automatically rejecting attempts to generate dead code. It is virtually impossible to accidentally generate bytecode that can crash the interpreter.
This Link might help you.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With