test-executables 4.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. #!/usr/bin/env python3
  2. import os
  3. import sys
  4. import common
  5. import lkmc.import_path
  6. import path_properties
  7. import signal
  8. import thread_pool
  9. class Main(common.TestCliFunction):
  10. def __init__(self, *args, **kwargs):
  11. if not 'description' in kwargs:
  12. kwargs['description'] = '''\
  13. Test userland executables in user mode, or baremetal executables in full system
  14. depending on the value of the --mode option. See also:
  15. * https://cirosantilli.com/linux-kernel-module-cheat#user-mode-tests
  16. * https://cirosantilli.com/linux-kernel-module-cheat#baremetal-tests
  17. * https://cirosantilli.com/linux-kernel-module-cheat#userland-setup-getting-started-natively
  18. '''
  19. if not 'defaults' in kwargs:
  20. kwargs['defaults'] = {}
  21. if not 'mode' in kwargs['defaults']:
  22. kwargs['defaults']['mode'] = 'userland'
  23. super().__init__(*args, **kwargs)
  24. self.add_argument(
  25. 'tests',
  26. nargs='*',
  27. help='''\
  28. If given, run only the given tests. Otherwise, run all tests.
  29. '''
  30. )
  31. def setup_one(self):
  32. self.env['tests'] = self.resolve_targets(
  33. [
  34. self.env['baremetal_source_dir'],
  35. self.env['userland_source_dir']
  36. ],
  37. self.env['tests']
  38. )
  39. def timed_main(self):
  40. run_args = self.get_common_args()
  41. if self.env['mode'] == 'userland' and self.env['emulator'] == 'gem5':
  42. run_args['userland_build_id'] = 'static'
  43. rootdir_abs_len = len(self.env['root_dir'])
  44. with thread_pool.ThreadPool(
  45. self.run_test,
  46. handle_output=self.handle_output_function,
  47. nthreads=self.env['nproc'],
  48. thread_id_arg='thread_id',
  49. submit_raise_exit=self.env['quit_on_fail'],
  50. ) as my_thread_pool:
  51. for test in self.env['tests']:
  52. for path, in_dirnames, in_filenames in self.sh.walk(test):
  53. path_abs = os.path.abspath(path)
  54. dirpath_relative_root = path_abs[rootdir_abs_len + 1:]
  55. for in_filename in in_filenames:
  56. if os.path.splitext(in_filename)[1] in self.env['build_in_exts']:
  57. path_relative_root = os.path.join(dirpath_relative_root, in_filename)
  58. my_path_properties = path_properties.get(path_relative_root)
  59. if my_path_properties.should_be_tested(self.env):
  60. cur_run_args = run_args.copy()
  61. cur_run_args.update({
  62. self.env['mode']: os.path.relpath(
  63. os.path.join(path_abs, in_filename),
  64. os.getcwd()
  65. ),
  66. })
  67. cur_run_args.update(my_path_properties['test_run_args'])
  68. run_test_args = {
  69. 'expected_exit_status': my_path_properties['exit_status'],
  70. 'run_args': cur_run_args,
  71. 'run_obj': lkmc.import_path.import_path_main('run'),
  72. 'test_id': '{} {}'.format(self.env['mode'], path_relative_root),
  73. }
  74. if (my_path_properties['qemu_unimplemented_instruction'] and
  75. self.env['emulator'] == 'qemu' and
  76. self.env['mode'] == 'userland'
  77. ):
  78. run_test_args['expected_exit_status'] = -signal.Signals.SIGILL.value
  79. my_signal = my_path_properties['signal_received']
  80. if my_signal is not None:
  81. if self.env['mode'] == 'baremetal':
  82. run_test_args['expected_exit_status'] = 128 + my_signal.value
  83. elif self.env['mode'] == 'userland':
  84. # Python subprocess reports signals differently from Bash's 128 + signal rule.
  85. run_test_args['expected_exit_status'] = -my_signal.value
  86. my_thread_pool.submit(run_test_args)
  87. return self._handle_thread_pool_errors(my_thread_pool)
  88. if __name__ == '__main__':
  89. Main().cli()