test_libos.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. #!/usr/bin/env python3
  2. import mmap
  3. import os
  4. import sys
  5. import unittest
  6. import subprocess
  7. from regression import (
  8. HAS_SGX,
  9. RegressionTestCase,
  10. expectedFailureIf,
  11. )
  12. class TC_00_Bootstrap(RegressionTestCase):
  13. def test_100_basic_bootstrapping(self):
  14. stdout, stderr = self.run_binary(['bootstrap'])
  15. # Basic Bootstrapping
  16. self.assertIn('User Program Started', stdout)
  17. # One Argument Given
  18. self.assertIn('# of Arguments: 1', stdout)
  19. self.assertIn('argv[0] = file:bootstrap', stdout)
  20. def test_101_basic_bootstrapping_five_arguments(self):
  21. # Five Arguments Given
  22. stdout, stderr = self.run_binary(['bootstrap', 'a', 'b', 'c', 'd'])
  23. self.assertIn('# of Arguments: 5', stdout)
  24. self.assertIn('argv[0] = file:bootstrap', stdout)
  25. self.assertIn('argv[1] = a', stdout)
  26. self.assertIn('argv[2] = b', stdout)
  27. self.assertIn('argv[3] = c', stdout)
  28. self.assertIn('argv[4] = d', stdout)
  29. def test_110_basic_bootstrapping_cxx(self):
  30. stdout, stderr = self.run_binary(['bootstrap-c++'])
  31. # Basic Bootstrapping (C++)
  32. self.assertIn('User Program Started', stdout)
  33. def test_200_exec(self):
  34. stdout, stderr = self.run_binary(['exec'])
  35. # 2 page child binary
  36. self.assertIn(
  37. '00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 '
  38. '000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 '
  39. '000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 '
  40. '000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 '
  41. '000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 '
  42. '000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 '
  43. '000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 '
  44. '000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 '
  45. '000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 '
  46. '000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 '
  47. '000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 '
  48. '000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 '
  49. '000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 '
  50. '000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 '
  51. '000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 '
  52. '000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 ',
  53. stdout)
  54. def test_201_fork_and_exec(self):
  55. stdout, stderr = self.run_binary(['fork_and_exec'])
  56. # fork and exec 2 page child binary
  57. self.assertIn('child exited with status: 0', stdout)
  58. self.assertIn('test completed successfully', stdout)
  59. def test_202_vfork_and_exec(self):
  60. stdout, stderr = self.run_binary(['vfork_and_exec'])
  61. # vfork and exec 2 page child binary
  62. self.assertIn('child exited with status: 0', stdout)
  63. self.assertIn('test completed successfully', stdout)
  64. def test_210_exec_invalid_args(self):
  65. stdout, stderr = self.run_binary(['exec_invalid_args'])
  66. # Execve with invalid pointers in arguments
  67. self.assertIn(
  68. 'execve(invalid-path) correctly returned error', stdout)
  69. self.assertIn(
  70. 'execve(invalid-argv-ptr) correctly returned error', stdout)
  71. self.assertIn(
  72. 'execve(invalid-envp-ptr) correctly returned error', stdout)
  73. self.assertIn(
  74. 'execve(invalid-argv) correctly returned error', stdout)
  75. self.assertIn(
  76. 'execve(invalid-envp) correctly returned error', stdout)
  77. def test_300_shared_object(self):
  78. stdout, stderr = self.run_binary(['shared_object'])
  79. # Shared Object
  80. self.assertIn('Hello world', stdout)
  81. def test_400_exit(self):
  82. with self.expect_returncode(113):
  83. self.run_binary(['exit'])
  84. def test_500_init_fail(self):
  85. try:
  86. self.run_binary(['init_fail'])
  87. self.fail('expected to return nonzero (and != 42)')
  88. except subprocess.CalledProcessError as e:
  89. self.assertNotEqual(e.returncode, 42, 'expected returncode != 42')
  90. @unittest.skipUnless(HAS_SGX,
  91. 'This test is only meaningful on SGX PAL because only SGX catches raw '
  92. 'syscalls and redirects to Graphene\'s LibOS. If we will add seccomp to '
  93. 'Linux PAL, then we should allow this test on Linux PAL as well.')
  94. class TC_01_OpenMP(RegressionTestCase):
  95. def test_000_simple_for_loop(self):
  96. stdout, stderr = self.run_binary(['openmp'])
  97. # OpenMP simple for loop
  98. self.assertIn('first: 0, last: 9', stdout)
  99. class TC_30_Syscall(RegressionTestCase):
  100. def test_000_getcwd(self):
  101. stdout, stderr = self.run_binary(['getcwd'])
  102. # Getcwd syscall
  103. self.assertIn('[bss_cwd_buf] getcwd succeeded: /', stdout)
  104. self.assertIn('[mmapped_cwd_buf] getcwd succeeded: /', stdout)
  105. def test_010_stat_invalid_args(self):
  106. stdout, stderr = self.run_binary(['stat_invalid_args'])
  107. # Stat with invalid arguments
  108. self.assertIn('stat(invalid-path-ptr) correctly returned error', stdout)
  109. self.assertIn('stat(invalid-buf-ptr) correctly returned error', stdout)
  110. self.assertIn('lstat(invalid-path-ptr) correctly returned error', stdout)
  111. self.assertIn('lstat(invalid-buf-ptr) correctly returned error', stdout)
  112. def test_011_fstat_cwd(self):
  113. stdout, stderr = self.run_binary(['fstat_cwd'])
  114. # fstat on a directory
  115. self.assertIn('fstat returned the fd type as S_IFDIR', stdout)
  116. def test_020_getdents(self):
  117. # This doesn't catch extraneous entries, but should be fine
  118. # until the LTP test can be run (need symlink support)
  119. stdout, stderr = self.run_binary(['getdents'])
  120. self.assertIn('getdents: setup ok', stdout)
  121. # Directory listing (32-bit)
  122. self.assertIn('getdents32: . [0x4]', stdout)
  123. self.assertIn('getdents32: .. [0x4]', stdout)
  124. self.assertIn('getdents32: file1 [0x8]', stdout)
  125. self.assertIn('getdents32: file2 [0x8]', stdout)
  126. self.assertIn('getdents32: dir3 [0x4]', stdout)
  127. # Directory listing (64-bit)
  128. self.assertIn('getdents64: . [0x4]', stdout)
  129. self.assertIn('getdents64: .. [0x4]', stdout)
  130. self.assertIn('getdents64: file1 [0x8]', stdout)
  131. self.assertIn('getdents64: file2 [0x8]', stdout)
  132. self.assertIn('getdents64: dir3 [0x4]', stdout)
  133. def test_021_getdents_large_dir(self):
  134. stdout, stderr = self.run_binary(['large_dir_read', 'tmp/large_dir', '3000'])
  135. self.assertIn('Success!', stdout)
  136. def test_030_fopen(self):
  137. stdout, stderr = self.run_binary(['fopen_cornercases'])
  138. # fopen corner cases
  139. self.assertIn('Successfully read from file: Hello World', stdout)
  140. def test_040_futex_wake(self):
  141. stdout, stderr = self.run_binary(['futex'])
  142. # Futex Wake Test
  143. self.assertIn('Woke all kiddos', stdout)
  144. def test_041_futex_timeout(self):
  145. stdout, stderr = self.run_binary(['futex-timeout'])
  146. # Futex Timeout Test
  147. self.assertIn('futex correctly timed out', stdout)
  148. def test_050_mmap(self):
  149. stdout, stderr = self.run_binary(['mmap-file'], timeout=60)
  150. # Private mmap beyond file range
  151. self.assertIn('mmap test 6 passed', stdout)
  152. self.assertIn('mmap test 7 passed', stdout)
  153. # Private mmap beyond file range (after fork)
  154. self.assertIn('mmap test 1 passed', stdout)
  155. self.assertIn('mmap test 2 passed', stdout)
  156. self.assertIn('mmap test 3 passed', stdout)
  157. self.assertIn('mmap test 4 passed', stdout)
  158. @unittest.skipIf(HAS_SGX,
  159. 'On SGX, SIGBUS isn\'t always implemented correctly, for lack '
  160. 'of memory protection. For now, some of these cases won\'t work.')
  161. def test_051_mmap_sgx(self):
  162. stdout, stderr = self.run_binary(['mmap-file'], timeout=60)
  163. # SIGBUS test
  164. self.assertIn('mmap test 5 passed', stdout)
  165. self.assertIn('mmap test 8 passed', stdout)
  166. def test_52_large_mmap(self):
  167. stdout, stderr = self.run_binary(['large-mmap'], timeout=240)
  168. # Ftruncate
  169. self.assertIn('large-mmap: ftruncate OK', stdout)
  170. # Large mmap
  171. self.assertIn('large-mmap: mmap 1 completed OK', stdout)
  172. self.assertIn('large-mmap: mmap 2 completed OK', stdout)
  173. @unittest.skip('sigaltstack isn\'t correctly implemented')
  174. def test_060_sigaltstack(self):
  175. stdout, stderr = self.run_binary(['sigaltstack'])
  176. # Sigaltstack Test
  177. self.assertIn('OK on sigaltstack in main thread before alarm', stdout)
  178. self.assertIn('&act == 0x', stdout)
  179. self.assertIn('sig 14 count 1 goes off with sp=0x', stdout)
  180. self.assertIn('OK on signal stack', stdout)
  181. self.assertIn('OK on sigaltstack in handler', stdout)
  182. self.assertIn('sig 14 count 2 goes off with sp=0x', stdout)
  183. self.assertIn('OK on signal stack', stdout)
  184. self.assertIn('OK on sigaltstack in handler', stdout)
  185. self.assertIn('sig 14 count 3 goes off with sp=0x', stdout)
  186. self.assertIn('OK on signal stack', stdout)
  187. self.assertIn('OK on sigaltstack in handler', stdout)
  188. self.assertIn('OK on sigaltstack in main thread', stdout)
  189. self.assertIn('done exiting', stdout)
  190. @unittest.skipUnless(HAS_SGX,
  191. 'This test is only meaningful on SGX PAL because only SGX catches raw '
  192. 'syscalls and redirects to Graphene\'s LibOS. If we will add seccomp to '
  193. 'Linux PAL, then we should allow this test on Linux PAL as well.')
  194. class TC_31_SyscallSGX(RegressionTestCase):
  195. def test_000_syscall_redirect(self):
  196. stdout, stderr = self.run_binary(['syscall'])
  197. # Syscall Instruction Redirection
  198. self.assertIn('Hello world', stdout)
  199. class TC_40_FileSystem(RegressionTestCase):
  200. def test_000_base(self):
  201. stdout, stderr = self.run_binary(['proc'])
  202. # Base /proc files present
  203. self.assertIn('/proc/1/..', stdout)
  204. self.assertIn('/proc/1/cwd', stdout)
  205. self.assertIn('/proc/1/exe', stdout)
  206. self.assertIn('/proc/1/root', stdout)
  207. self.assertIn('/proc/1/fd', stdout)
  208. self.assertIn('/proc/1/maps', stdout)
  209. self.assertIn('/proc/.', stdout)
  210. self.assertIn('/proc/1', stdout)
  211. self.assertIn('/proc/self', stdout)
  212. self.assertIn('/proc/meminfo', stdout)
  213. self.assertIn('/proc/cpuinfo', stdout)
  214. def test_010_path(self):
  215. stdout, stderr = self.run_binary(['proc-path'])
  216. # Base /proc path present
  217. self.assertIn('proc path test success', stdout)
  218. def test_020_cpuinfo(self):
  219. stdout, stderr = self.run_binary(['proc_cpuinfo'], timeout=50)
  220. # proc/cpuinfo Linux-based formatting
  221. self.assertIn('cpuinfo test passed', stdout)
  222. class TC_80_Socket(RegressionTestCase):
  223. def test_000_getsockopt(self):
  224. stdout, stderr = self.run_binary(['getsockopt'])
  225. self.assertIn('getsockopt: Got socket type OK', stdout)
  226. def test_010_epoll_wait_timeout(self):
  227. stdout, stderr = self.run_binary(['epoll_wait_timeout', '8000'],
  228. timeout=50)
  229. # epoll_wait timeout
  230. self.assertIn('epoll_wait test passed', stdout)
  231. def test_100_socket_unix(self):
  232. stdout, stderr = self.run_binary(['unix'])
  233. self.assertIn('Data: This is packet 0', stdout)
  234. self.assertIn('Data: This is packet 1', stdout)
  235. self.assertIn('Data: This is packet 2', stdout)
  236. self.assertIn('Data: This is packet 3', stdout)
  237. self.assertIn('Data: This is packet 4', stdout)
  238. self.assertIn('Data: This is packet 5', stdout)
  239. self.assertIn('Data: This is packet 6', stdout)
  240. self.assertIn('Data: This is packet 7', stdout)
  241. self.assertIn('Data: This is packet 8', stdout)
  242. self.assertIn('Data: This is packet 9', stdout)
  243. def test_200_socket_udp(self):
  244. stdout, stderr = self.run_binary(['udp'], timeout=50)
  245. self.assertIn('Data: This is packet 0', stdout)
  246. self.assertIn('Data: This is packet 1', stdout)
  247. self.assertIn('Data: This is packet 2', stdout)
  248. self.assertIn('Data: This is packet 3', stdout)
  249. self.assertIn('Data: This is packet 4', stdout)
  250. self.assertIn('Data: This is packet 5', stdout)
  251. self.assertIn('Data: This is packet 6', stdout)
  252. self.assertIn('Data: This is packet 7', stdout)
  253. self.assertIn('Data: This is packet 8', stdout)
  254. self.assertIn('Data: This is packet 9', stdout)