Python-ELMO is a Python library which offers an encapsulation of the binary tool ELMO, in order to manipulate it easily in Python and SageMath script.

executorthread.py 3.0KB

4 年之前
4 年之前
4 年之前
4 年之前
4 年之前
4 年之前
4 年之前
4 年之前
4 年之前
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. from .server.servicethread import OneShotServiceThread
  2. import subprocess
  3. import shutil
  4. import os, re
  5. class ExecutorThread(OneShotServiceThread):
  6. def __init__(self, ip, port, clientsocket, **kwargs):
  7. super().__init__(ip, port, clientsocket)
  8. def execute(self):
  9. data = self.protocol.get_data()
  10. self.protocol.please_assert(data)
  11. # Set the input of ELMO
  12. self.protocol.please_assert('input' in data)
  13. with open('elmo/input.txt', 'w') as _input_file:
  14. _input_file.write(data['input'])
  15. self.protocol.send_ack()
  16. # Get the binary
  17. binary_content = self.protocol.get_file()
  18. with open('elmo/project.bin', 'wb') as _binary_file:
  19. _binary_file.write(binary_content)
  20. ### Generate the traces by launching ELMO
  21. command = './elmo ./project.bin'
  22. cwd = './elmo/'
  23. process = subprocess.Popen(
  24. command, shell=True, cwd=cwd,
  25. executable='/bin/bash',
  26. stdout=subprocess.PIPE,
  27. stderr=subprocess.PIPE,
  28. )
  29. output, error = process.communicate()
  30. output = output.decode('latin-1') if output else None
  31. error = error.decode('latin-1') if error else None
  32. if error:
  33. self.protocol.send_data({
  34. 'output': output,
  35. 'error': error,
  36. })
  37. self.protocol.close()
  38. return
  39. ### Get traces
  40. nb_traces = output.count('TRACE NO')
  41. trace_filenames = []
  42. for filename in os.listdir('elmo/output/traces/'):
  43. if len(trace_filenames) < nb_traces:
  44. if re.search(r'^trace\d+\.trc$', filename):
  45. trace_filenames.append('elmo/output/traces/{}'.format(filename))
  46. else:
  47. break
  48. assert len(trace_filenames) == nb_traces
  49. results = trace_filenames
  50. for i in range(len(results)):
  51. with open(results[i], 'r') as _file:
  52. results[i] = list(map(float, _file.readlines()))
  53. ### Get asmtrace and printed data
  54. asmtrace = None
  55. if not ('asmtrace' in data and not data['asmtrace']):
  56. with open('elmo/output/asmoutput/asmtrace00001.txt', 'r') as _file:
  57. asmtrace = ''.join(_file.readlines())
  58. printed_data = None
  59. if not ('printdata' in data and not data['printdata']):
  60. with open('elmo/output/printdata.txt', 'r') as _file:
  61. printed_data = list(map(lambda x: int(x, 16), _file.readlines()))
  62. ### Send results
  63. self.protocol.send_data({
  64. 'output': output,
  65. 'error': error,
  66. 'nb_traces': nb_traces,
  67. 'results': results,
  68. 'asmtrace': asmtrace,
  69. 'printed_data': printed_data,
  70. })
  71. self.protocol.close()