def test_deserialize_empty(self): bytes_in = b'' with self.assertRaises(ValueError): sexp_from_stream(io.BytesIO(bytes_in), to_sexp_f) with self.assertRaises(ValueError): sexp_buffer_from_stream(io.BytesIO(bytes_in))
def test_deserialize_truncated_size(self): # fe means the total number of bytes in the length-prefix is 7 # one for each bit set. 5 bytes is too few bytes_in = b'\xfe ' with self.assertRaises(ValueError): sexp_from_stream(io.BytesIO(bytes_in), to_sexp_f) with self.assertRaises(ValueError): sexp_buffer_from_stream(io.BytesIO(bytes_in))
def test_deserialize_truncated_blob(self): # this is a complete length prefix. The blob is supposed to be 63 bytes # the blob itself is truncated though, it's less than 63 bytes bytes_in = b'\xbf ' with self.assertRaises(ValueError): sexp_from_stream(io.BytesIO(bytes_in), to_sexp_f) with self.assertRaises(ValueError): sexp_buffer_from_stream(io.BytesIO(bytes_in))
def check_serde(self, s): v = to_sexp_f(s) b = v.as_bin() v1 = sexp_from_stream(io.BytesIO(b), to_sexp_f) if v != v1: print("%s: %d %s %s" % (v, len(b), b, v1)) breakpoint() b = v.as_bin() v1 = sexp_from_stream(io.BytesIO(b), to_sexp_f) self.assertEqual(v, v1)
def spend_bundle_to_coin_spend_entry_list(bundle: SpendBundle) -> List[Any]: r = [] for coin_spend in bundle.coin_spends: entry = [ coin_spend.coin.parent_coin_info, sexp_from_stream(io.BytesIO(bytes(coin_spend.puzzle_reveal)), SExp.to), coin_spend.coin.amount, sexp_from_stream(io.BytesIO(bytes(coin_spend.solution)), SExp.to), ] r.append(entry) return r
def test_deserialize_large_blob(self): # this length prefix is 7 bytes long, the last 6 bytes specifies the # length of the blob, which is 0xffffffffffff, or (2^48 - 1) # we don't support blobs this large, and we should fail immediately when # exceeding the max blob size, rather than trying to read this many # bytes from the stream bytes_in = b'\xfe' + b'\xff' * 6 with self.assertRaises(ValueError): sexp_from_stream(InfiniteStream(bytes_in), to_sexp_f) with self.assertRaises(ValueError): sexp_buffer_from_stream(InfiniteStream(bytes_in))
def check_serde(self, s): v = to_sexp_f(s) b = v.as_bin() v1 = sexp_from_stream(io.BytesIO(b), to_sexp_f) if v != v1: print("%s: %d %s %s" % (v, len(b), b, v1)) breakpoint() b = v.as_bin() v1 = sexp_from_stream(io.BytesIO(b), to_sexp_f) self.assertEqual(v, v1) # this copies the bytes that represent a single s-expression, just to # know where the message ends. It doesn't build a python representaion # of it buf = sexp_buffer_from_stream(io.BytesIO(b)) self.assertEqual(buf, b)
def _run(self, flags, *args) -> Tuple[int, Program]: # when multiple arguments are passed, concatenate them into a serialized # buffer. Some arguments may already be in serialized form (e.g. # SerializedProgram) so we don't want to de-serialize those just to # serialize them back again. This is handled by _serialize() serialized_args = b"" if len(args) > 1: # when we have more than one argument, serialize them into a list for a in args: serialized_args += b"\xff" serialized_args += _serialize(a) serialized_args += b"\x80" else: serialized_args += _serialize(args[0]) max_cost = 0 # TODO: move this ugly magic into `clvm` "dialects" native_opcode_names_by_opcode = dict( ("op_%s" % OP_REWRITE.get(k, k), op) for op, k in KEYWORD_FROM_ATOM.items() if k not in "qa.") cost, ret = deserialize_and_run_program( self._buf, serialized_args, KEYWORD_TO_ATOM["q"][0], KEYWORD_TO_ATOM["a"][0], native_opcode_names_by_opcode, max_cost, flags, ) # TODO this could be parsed lazily return cost, Program.to(sexp_from_stream(io.BytesIO(ret), SExp.to))
def get_tree_hash(self, *args: List[bytes32]) -> bytes32: """ Any values in `args` that appear in the tree are presumed to have been hashed already. """ tmp = sexp_from_stream(io.BytesIO(self._buf), SExp.to) return _tree_hash(tmp, set(args))
def opd(args=sys.argv): parser = argparse.ArgumentParser( description='Disassemble a compiled clvm script.') parser.add_argument("script", nargs="+", type=bytes.fromhex, help="hex version of clvm script") args = parser.parse_args(args=args[1:]) for blob in args.script: sexp = sexp_from_stream(io.BytesIO(blob), to_sexp_f) print(binutils.disassemble(sexp))
def _run(self, flags, *args) -> Tuple[int, SExp]: # when multiple arguments are passed, concatenate them into a serialized # buffer. Some arguments may already be in serialized form (e.g. # SerializedProgram) so we don't want to de-serialize those just to # serialize them back again. This is handled by _serialize() serialized_args = b"" if len(args) > 1: # when we have more than one argument, serialize them into a list for a in args: serialized_args += b"\xff" serialized_args += _serialize(a) serialized_args += b"\x80" else: serialized_args += _serialize(args[0]) max_cost = 0 cost, ret = serialize_and_run_program(self._buf, serialized_args, 1, 3, max_cost, flags) # TODO this could be parsed lazily return cost, sexp_from_stream(io.BytesIO(ret), SExp.to)
# tool to decompile a fuzz_run_program test case to human readable form import sys import io from ir import reader from clvm_tools import binutils from clvm.serialize import sexp_from_stream from clvm import to_sexp_f with open(sys.argv[1], 'rb') as f: blob = f.read() sexp = sexp_from_stream(io.BytesIO(blob), to_sexp_f) print(binutils.disassemble(sexp))
def parse(cls, f) -> "Program": return sexp_from_stream(f, cls.to)
def conversion(blob): sexp = sexp_from_stream(io.BytesIO(bytes.fromhex(blob)), to_sexp_f) return sexp, binutils.disassemble(sexp)
def from_stream(class_, f): return sexp_from_stream(f, class_.to)
def parse(cls, f): return sexp_from_stream(f, cls.to)
def launch_tool(args, tool_name, default_stage=0): sys.setrecursionlimit(20000) parser = argparse.ArgumentParser(description='Execute a clvm script.') parser.add_argument( "--strict", action="store_true", help="Unknown opcodes are always fatal errors in strict mode") parser.add_argument( "-x", "--hex", action="store_true", help="Read program and environment as hexadecimal bytecode") parser.add_argument("-s", "--stage", type=stage_import, help="stage number to include", default=stage_import(default_stage)) parser.add_argument( "-v", "--verbose", action="store_true", help="Display resolve of all reductions, for debugging") parser.add_argument( "-t", "--table", action="store_true", help="Print diagnostic table of reductions, for debugging") parser.add_argument("-c", "--cost", action="store_true", help="Show cost") parser.add_argument("--time", action="store_true", help="Print execution time") parser.add_argument("-m", "--max-cost", type=int, default=11000000000, help="Maximum cost") parser.add_argument("-d", "--dump", action="store_true", help="dump hex version of final output") parser.add_argument("--quiet", action="store_true", help="Suppress printing the program result") parser.add_argument("-y", "--symbol-table", type=pathlib.Path, help=".SYM file generated by compiler") parser.add_argument("-n", "--no-keywords", action="store_true", help="Output result as data, not as a program") parser.add_argument("--backend", type=str, help="force use of 'rust' or 'python' backend") parser.add_argument( "-i", "--include", type=pathlib.Path, help="add a search path for included files", action="append", default=[], ) parser.add_argument("path_or_code", type=path_or_code, help="filepath to clvm script, or a literal script") parser.add_argument("env", nargs="?", type=path_or_code, help="clvm script environment, as clvm src, or hex") args = parser.parse_args(args=args[1:]) keywords = {} if args.no_keywords else KEYWORD_FROM_ATOM if hasattr(args.stage, "run_program_for_search_paths"): run_program = args.stage.run_program_for_search_paths(args.include) else: run_program = args.stage.run_program input_serialized = None input_sexp = None time_start = time.perf_counter() if args.hex: assembled_serialized = bytes.fromhex(args.path_or_code) if not args.env: args.env = "80" env_serialized = bytes.fromhex(args.env) time_read_hex = time.perf_counter() input_serialized = b"\xff" + assembled_serialized + env_serialized else: src_text = args.path_or_code try: src_sexp = reader.read_ir(src_text) except SyntaxError as ex: print("FAIL: %s" % (ex)) return -1 assembled_sexp = binutils.assemble_from_ir(src_sexp) if not args.env: args.env = "()" env_ir = reader.read_ir(args.env) env = binutils.assemble_from_ir(env_ir) time_assemble = time.perf_counter() input_sexp = to_sexp_f((assembled_sexp, env)) pre_eval_f = None symbol_table = None log_entries = [] if args.symbol_table: with open(args.symbol_table) as f: symbol_table = json.load(f) pre_eval_f = make_trace_pre_eval(log_entries, symbol_table) elif args.verbose or args.table: pre_eval_f = make_trace_pre_eval(log_entries) run_script = getattr(args.stage, tool_name) cost = 0 cost_offset = calculate_cost_offset(run_program, run_script) try: output = "(didn't finish)" use_rust = ( (tool_name != "run") and not pre_eval_f and (args.backend == "rust" or (deserialize_and_run_program2 and args.backend != "python"))) max_cost = max( 0, args.max_cost - cost_offset if args.max_cost != 0 else 0) if use_rust: if input_serialized is None: input_serialized = input_sexp.as_bin() run_script = run_script.as_bin() time_parse_input = time.perf_counter() # build the opcode look-up table # this should eventually be subsumed by "Dialect" api native_opcode_names_by_opcode = dict( ("op_%s" % OP_REWRITE.get(k, k), op) for op, k in KEYWORD_FROM_ATOM.items() if k not in "qa.") cost, result = deserialize_and_run_program2( run_script, input_serialized, KEYWORD_TO_ATOM["q"][0], KEYWORD_TO_ATOM["a"][0], native_opcode_names_by_opcode, max_cost, STRICT_MODE if args.strict else 0, ) time_done = time.perf_counter() result = SExp.to(result) else: if input_sexp is None: input_sexp = sexp_from_stream(io.BytesIO(input_serialized), to_sexp_f) time_parse_input = time.perf_counter() cost, result = run_program(run_script, input_sexp, max_cost=max_cost, pre_eval_f=pre_eval_f, strict=args.strict) time_done = time.perf_counter() if args.cost: cost += cost_offset if cost > 0 else 0 print("cost = %d" % cost) if args.time: if args.hex: print('read_hex: %f' % (time_read_hex - time_start)) else: print('assemble_from_ir: %f' % (time_assemble - time_start)) print('to_sexp_f: %f' % (time_parse_input - time_assemble)) print('run_program: %f' % (time_done - time_parse_input)) if args.dump: blob = as_bin(lambda f: sexp_to_stream(result, f)) output = blob.hex() elif args.quiet: output = '' else: output = binutils.disassemble(result, keywords) except EvalError as ex: result = to_sexp_f(ex._sexp) output = "FAIL: %s %s" % (ex, binutils.disassemble(result, keywords)) return -1 except Exception as ex: output = str(ex) raise finally: print(output) if args.verbose or symbol_table: print() trace_to_text(log_entries, binutils.disassemble, symbol_table) if args.table: trace_to_table(log_entries, binutils.disassemble, symbol_table)