Generates random Python functions using Markov chains
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

210 lines
6.7 KiB

  1. from code import interact
  2. import imp
  3. import opcode
  4. import os
  5. import random
  6. import re
  7. import sys
  8. import types
  9. import prettify
  10. OPMAP = opcode.opmap
  11. OP_HASBUILD = [OPMAP[n] for n in ("BUILD_TUPLE", "BUILD_LIST", "BUILD_MAP",
  12. "BUILD_SET")]
  13. OP_HASCALL = [OPMAP[n] for n in ("CALL_FUNCTION", "CALL_FUNCTION_VAR",
  14. "CALL_FUNCTION_KW", "CALL_FUNCTION_VAR_KW")]
  15. OP_HASMAKE = [OPMAP[n] for n in ("MAKE_FUNCTION", "MAKE_CLOSURE")]
  16. OP_LITERALARG = opcode.hasjabs + opcode.hasjrel + OP_HASBUILD + OP_HASMAKE
  17. MARKOV_START = -1
  18. MARKOV_END = -2
  19. def make_chain(funcs):
  20. chain = {}
  21. for func in funcs:
  22. _parse_func(func, chain)
  23. return chain
  24. def make_function(chain, name, argcount=1):
  25. codes, constants, names, varnames = _make_codes(chain)
  26. codestring = "".join([chr(code) for code in codes])
  27. lnotab = ""
  28. code = types.CodeType(argcount, len(varnames), 1024, 0, codestring,
  29. constants, names, varnames, "<smash>", name, 1,
  30. lnotab)
  31. func = types.FunctionType(code, globals(), name)
  32. return func
  33. def print_chain(chain):
  34. print "{"
  35. for code in sorted(chain.keys()):
  36. name = _opcode_to_opname(code)
  37. target_counts = {}
  38. for tcode in chain[code]:
  39. target = _opcode_to_opname(tcode[0])
  40. if tcode[0] >= opcode.HAVE_ARGUMENT:
  41. target = "{0}({1!r})".format(target, tcode[1])
  42. try:
  43. target_counts[target] += 1
  44. except KeyError:
  45. target_counts[target] = 1
  46. targets = []
  47. for target, count in target_counts.iteritems():
  48. if count == 1:
  49. targets.append(target)
  50. else:
  51. targets.append("{0}x {1}".format(count, target))
  52. targets.sort()
  53. print name.rjust(20), "=> [{0}]".format(", ".join(targets))
  54. print "}"
  55. def print_function(func):
  56. codeobj = func.__code__
  57. codestring = codeobj.co_code
  58. length = len(codestring)
  59. i = 0
  60. while i < length:
  61. code = ord(codestring[i])
  62. i += 1
  63. print opcode.opname[code].rjust(20),
  64. if code >= opcode.HAVE_ARGUMENT:
  65. arg = _get_argument(codeobj, codestring, i, code)
  66. i += 2
  67. if code in opcode.hascompare:
  68. print " ({0})".format(arg)
  69. elif code in opcode.hasjabs:
  70. print " (to {0})".format(arg)
  71. elif code in opcode.hasjrel:
  72. print " (+{0})".format(arg)
  73. elif code in OP_HASBUILD:
  74. print " ({0} items)".format(arg)
  75. elif code in OP_HASCALL:
  76. print " ({0} args, {1} kwargs)".format(*arg)
  77. elif code in OP_HASMAKE:
  78. print " ({0} defaults)".format(arg)
  79. else:
  80. print " ({0!r})".format(arg)
  81. else:
  82. print
  83. def run():
  84. try:
  85. path, name = os.path.split(sys.argv[1])
  86. name = re.sub("\.pyc?$", "", name)
  87. except IndexError:
  88. raise RuntimeError("Needs a filename as a command-line argument")
  89. file_obj, path, desc = imp.find_module(name, [path])
  90. try:
  91. module = imp.load_module(name, file_obj, path, desc)
  92. finally:
  93. file_obj.close()
  94. _demo(module.corpus)
  95. def _parse_func(func, chain):
  96. codeobj = func.__code__
  97. codestring = codeobj.co_code
  98. length = len(codestring)
  99. i = 0
  100. prevcode = MARKOV_START
  101. while i < length:
  102. code = ord(codestring[i])
  103. i += 1
  104. if code >= opcode.HAVE_ARGUMENT:
  105. arg = _get_argument(codeobj, codestring, i, code)
  106. i += 2
  107. else:
  108. arg = None
  109. _chain_append(chain, prevcode, (code, arg))
  110. prevcode = code
  111. _chain_append(chain, code, (MARKOV_END, None))
  112. def _get_argument(codeobj, codestring, i, code):
  113. arg = ord(codestring[i]) + ord(codestring[i + 1]) * 256
  114. if code in opcode.hasconst:
  115. return codeobj.co_consts[arg]
  116. elif code in opcode.hasname:
  117. return codeobj.co_names[arg]
  118. elif code in opcode.haslocal:
  119. return codeobj.co_varnames[arg]
  120. elif code in opcode.hascompare:
  121. return opcode.cmp_op[arg]
  122. elif code in OP_HASCALL:
  123. return (ord(codestring[i]), ord(codestring[i + 1]))
  124. elif code in OP_LITERALARG:
  125. return arg
  126. raise NotImplementedError(code, opcode.opname[code])
  127. def _chain_append(chain, first, second):
  128. try:
  129. chain[first].append(second)
  130. except KeyError:
  131. chain[first] = [second]
  132. def _make_codes(chain):
  133. codes = []
  134. instruction = random.choice(chain[MARKOV_START])
  135. constants, names, varnames = [], [], []
  136. while 1:
  137. code, arg = instruction
  138. if code == MARKOV_END:
  139. break
  140. codes.append(code)
  141. if code >= opcode.HAVE_ARGUMENT:
  142. if code in opcode.hasconst:
  143. if arg not in constants:
  144. constants.append(arg)
  145. _coerce_arg_into_codes(codes, constants.index(arg))
  146. elif code in opcode.hasname:
  147. if arg not in names:
  148. names.append(arg)
  149. _coerce_arg_into_codes(codes, names.index(arg))
  150. elif code in opcode.haslocal:
  151. if arg not in varnames:
  152. varnames.append(arg)
  153. _coerce_arg_into_codes(codes, varnames.index(arg))
  154. elif code in opcode.hascompare:
  155. _coerce_arg_into_codes(codes, opcode.cmp_op.index(arg))
  156. elif code in OP_HASCALL:
  157. codes.append(arg[0])
  158. codes.append(arg[1])
  159. elif code in OP_LITERALARG:
  160. _coerce_arg_into_codes(codes, arg)
  161. else:
  162. raise NotImplementedError(code, opcode.opname[code])
  163. instruction = random.choice(chain[code])
  164. return codes, tuple(constants), tuple(names), tuple(varnames)
  165. def _opcode_to_opname(code):
  166. if code == MARKOV_START:
  167. return "START"
  168. elif code == MARKOV_END:
  169. return "END"
  170. return opcode.opname[code]
  171. def _coerce_arg_into_codes(codes, arg):
  172. codes.append(arg % 256)
  173. codes.append(arg // 256)
  174. def _demo(corpus, arg=12.0):
  175. chain = make_chain(corpus)
  176. func = make_function(chain, "func")
  177. print "Using {0}-function corpus.".format(len(corpus))
  178. print "Smashed function disassembly:"
  179. print_function(func)
  180. print "\nFunction as python code:"
  181. prettify.prettify_function(func, indent=4)
  182. if not len(sys.argv) > 2 or "n" not in "".join(sys.argv[2:]):
  183. print
  184. print "func({0}) =".format(arg), func(arg)
  185. if len(sys.argv) > 2 and "i" in "".join(sys.argv[2:]):
  186. variables = dict(globals().items() + locals().items())
  187. interact(banner="", local=variables)
  188. if __name__ == "__main__":
  189. run()