def finalize(self):
logger.debug("<STR_LIT>", self.count_states())<EOL>def finalizer(state_id):<EOL><INDENT>state_id = self._terminate_state_id(state_id)<EOL>st = self.load(state_id)<EOL>logger.debug("<STR_LIT>", state_id)<EOL>last_tx = st.platform.last_transaction<EOL>message = last_tx.result if last_tx else '<STR_LIT>'<EOL>self._publish_generate_testcase(st, message=message)<EOL><DEDENT>def worker_finalize(q):<EOL><INDENT>try:<EOL><INDENT>while True:<EOL><INDENT>finalizer(q.get_nowait())<EOL><DEDENT><DEDENT>except EmptyQueue:<EOL><INDENT>pass<EOL><DEDENT><DEDENT>q = Queue()<EOL>for state_id in self._all_state_ids:<EOL><INDENT>if state_id == -<NUM_LIT:1>:<EOL><INDENT>finalizer(-<NUM_LIT:1>)<EOL><DEDENT>else:<EOL><INDENT>q.put(state_id)<EOL><DEDENT><DEDENT>report_workers = []<EOL>for _ in range(self._config_procs):<EOL><INDENT>proc = Process(target=worker_finalize, args=(q,))<EOL>proc.start()<EOL>report_workers.append(proc)<EOL><DEDENT>for proc in report_workers:<EOL><INDENT>proc.join()<EOL><DEDENT>if len(self.global_findings):<EOL><INDENT>with self._output.save_stream('<STR_LIT>') as global_findings:<EOL><INDENT>for address, pc, finding, at_init in self.global_findings:<EOL><INDENT>global_findings.write('<STR_LIT>' % finding)<EOL>write_findings(global_findings, '<STR_LIT:U+0020>', address, pc, at_init)<EOL>md = self.get_metadata(address)<EOL>if md is not None:<EOL><INDENT>source_code_snippet = md.get_source_for(pc, runtime=not at_init)<EOL>global_findings.write('<STR_LIT>')<EOL>global_findings.write('<STR_LIT:U+0020>'.join(source_code_snippet.splitlines(True)))<EOL>global_findings.write('<STR_LIT:\n>')<EOL><DEDENT><DEDENT><DEDENT><DEDENT>self._save_run_data()<EOL>with self._output.save_stream('<STR_LIT>') as global_summary:<EOL><INDENT>global_summary.write("<STR_LIT>")<EOL>for address in self.contract_accounts.values():<EOL><INDENT>global_summary.write("<STR_LIT>".format(int(address), self.global_coverage(address)))<EOL>md = self.get_metadata(address)<EOL>if md is not None and len(md.warnings) > <NUM_LIT:0>:<EOL><INDENT>global_summary.write('<STR_LIT>' % md.name)<EOL>global_summary.write(md.warnings)<EOL><DEDENT><DEDENT><DEDENT>for address, md in self.metadata.items():<EOL><INDENT>with self._output.save_stream('<STR_LIT>' % md.name) as global_src:<EOL><INDENT>global_src.write(md.source_code)<EOL><DEDENT>with self._output.save_stream('<STR_LIT>' % md.name, binary=True) as global_runtime_bytecode:<EOL><INDENT>global_runtime_bytecode.write(md.runtime_bytecode)<EOL><DEDENT>with self._output.save_stream('<STR_LIT>' % md.name, binary=True) as global_init_bytecode:<EOL><INDENT>global_init_bytecode.write(md.init_bytecode)<EOL><DEDENT>with self._output.save_stream('<STR_LIT>' % md.name) as global_runtime_asm:<EOL><INDENT>runtime_bytecode = md.runtime_bytecode<EOL>with self.locked_context('<STR_LIT>') as seen:<EOL><INDENT>count, total = <NUM_LIT:0>, <NUM_LIT:0><EOL>for i in EVMAsm.disassemble_all(runtime_bytecode):<EOL><INDENT>if (address, i.pc) in seen:<EOL><INDENT>count += <NUM_LIT:1><EOL>global_runtime_asm.write('<STR_LIT:*>')<EOL><DEDENT>else:<EOL><INDENT>global_runtime_asm.write('<STR_LIT:U+0020>')<EOL><DEDENT>global_runtime_asm.write('<STR_LIT>' % (i.pc, i))<EOL>total += <NUM_LIT:1><EOL><DEDENT><DEDENT><DEDENT>with self._output.save_stream('<STR_LIT>' % md.name) as global_init_asm:<EOL><INDENT>with self.locked_context('<STR_LIT>') as seen:<EOL><INDENT>count, total = <NUM_LIT:0>, <NUM_LIT:0><EOL>for i in EVMAsm.disassemble_all(md.init_bytecode):<EOL><INDENT>if (address, i.pc) in seen:<EOL><INDENT>count += <NUM_LIT:1><EOL>global_init_asm.write('<STR_LIT:*>')<EOL><DEDENT>else:<EOL><INDENT>global_init_asm.write('<STR_LIT:U+0020>')<EOL><DEDENT>global_init_asm.write('<STR_LIT>' % (i.pc, i))<EOL>total += <NUM_LIT:1><EOL><DEDENT><DEDENT><DEDENT>with self._output.save_stream('<STR_LIT>' % md.name) as f:<EOL><INDENT>with self.locked_context('<STR_LIT>') as seen:<EOL><INDENT>visited = set((o for (a, o) in seen if a == address))<EOL>for o in sorted(visited):<EOL><INDENT>f.write('<STR_LIT>' % o)<EOL><DEDENT><DEDENT><DEDENT>with self._output.save_stream('<STR_LIT>' % md.name) as f:<EOL><INDENT>with self.locked_context('<STR_LIT>') as seen:<EOL><INDENT>visited = set()<EOL>for (a, o) in seen:<EOL><INDENT>if a == address:<EOL><INDENT>visited.add(o)<EOL><DEDENT><DEDENT>for o in sorted(visited):<EOL><INDENT>f.write('<STR_LIT>' % o)<EOL><DEDENT><DEDENT><DEDENT><DEDENT>for state_id in self._all_state_ids:<EOL><INDENT>if state_id != -<NUM_LIT:1>:<EOL><INDENT>self._executor._workspace.rm_state(state_id)<EOL><DEDENT><DEDENT>with self.locked_context('<STR_LIT>') as eth_context:<EOL><INDENT>eth_context['<STR_LIT>'] = set()<EOL>eth_context['<STR_LIT>'] = set()<EOL><DEDENT>
Terminate and generate testcases for all currently alive states (contract states that cleanly executed
to a STOP or RETURN in the last symbolic transaction).